Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Abuggoproject/CnnCode.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
141 lines (101 sloc)
4.09 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mxnet import nd | |
import random | |
import math | |
from mxnet import gluon, init, nd, autograd | |
from mxnet import autograd, nd | |
from mxnet.gluon import nn | |
from mxnet.gluon import loss as gloss | |
class Inception(nn.Block): | |
def __init__(self,c1,c2,c3,c4,**kwargs): | |
super(Inception, self).__init__(**kwargs) | |
self.p1_1 = nn.Conv2D(c1, kernel_size=1, activation='relu') | |
# 线路 2, 1 x 1 卷积层后接 3 x 3 卷积层 | |
self.p2_1 = nn.Conv2D(c2[0], kernel_size=1, activation='relu') | |
self.p2_2 = nn.Conv2D(c2[1], kernel_size=3, padding=1, | |
activation='relu') | |
# 线路 3, 1 x 1 卷积层后接 5 x 5 卷积层 | |
self.p3_1 = nn.Conv2D(c3[0], kernel_size=1, activation='relu') | |
self.p3_2 = nn.Conv2D(c3[1], kernel_size=5, padding=2, | |
activation='relu') | |
# 线路 4, 3 x 3 最⼤池化层后接 1 x 1 卷积层 | |
self.p4_1 = nn.MaxPool2D(pool_size=3, strides=1, padding=1) | |
self.p4_2 = nn.Conv2D(c4, kernel_size=1, activation='relu') | |
def forward(self, x): | |
p1 = self.p1_1(x) | |
p2 = self.p2_2(self.p2_1(x)) | |
p3 = self.p3_2(self.p3_1(x)) | |
p4 = self.p4_2(self.p4_1(x)) | |
return nd.concat(p1, p2, p3, p4, dim=1) | |
def batch_norm(X, gamma, beta, moving_mean, moving_var, eps, momentum): | |
# 通过 autograd 来判断当前模式是训练模式还是预测模式 | |
if not autograd.is_training(): | |
X_hat=(X-moving_mean)/nd.sqrt(moving_var+eps) | |
else: | |
assert len(X.shape) in (2,4) | |
if len(X.shape)==2: | |
mean=X.mean(axis=0) | |
var=((X-mean)**2).mean(axis=0) | |
else: | |
mean=X.mean(axis=(0,2,3),keepdims=True) | |
var=((X-mean)**2).mean(axis=(0,2,3),keepdims=True) | |
X_hat=(X-mean)/nd.sqrt(var+eps) | |
moving_mean=momentum*moving_mean+(1.0-momentum)*mean | |
moving_var=momentum*moving_var+(1.0-momentum)*var | |
Y=gamma*X_hat+beta | |
return Y,moving_mean,moving_var | |
class BatchNorm(nn.Block): | |
def __init__(self, num_features, num_dims, **kwargs): | |
super(BatchNorm, self).__init__(**kwargs) | |
if num_dims == 2: | |
shape = (1, num_features) | |
else: | |
shape=(1,num_features,1,1) | |
self.gamma=self.params.get('gamma',shape=shape,init=init.One()) | |
self.beta=self.params.get('beta',shape=shape,init=init.Zero()) | |
self.moving_mean=nd.zeros(shape) | |
self.moving_var=nd.zeros(shape) | |
def forward(self, X): | |
if self.moving_mean.context!=X.context: | |
self.moving_mean=self.moving_mean.copyto(X.context) | |
self.moving_var=self.moving_var.copyto(X.context) | |
Y, self.moving_mean, self.moving_var = batch_norm( | |
X, self.gamma.data(), self.beta.data(), self.moving_mean, | |
self.moving_var, eps=1e-5, momentum=0.9) | |
return Y | |
b1 = nn.Sequential() | |
b1.add( | |
nn.Conv2D(64, kernel_size=7, strides=2, padding=3, activation='relu'), | |
nn.MaxPool2D(pool_size=3, strides=2, padding=1) | |
) | |
b2 = nn.Sequential() | |
b2.add(nn.Conv2D(64, kernel_size=1, activation='relu'), | |
nn.Conv2D(192, kernel_size=3, padding=1, activation='relu'),\ | |
nn.MaxPool2D(pool_size=3, strides=2, padding=1)) | |
b3 = nn.Sequential() | |
b3.add(Inception(64, (96, 128), (16, 32), 32), | |
Inception(128, (128, 192), (32, 96), 64), | |
nn.MaxPool2D(pool_size=3, strides=2, padding=1)) | |
b4 = nn.Sequential() | |
b4.add(Inception(192, (96, 208), (16, 48), 64), | |
Inception(160, (112, 224), (24, 64), 64), | |
Inception(128, (128, 256), (24, 64), 64), | |
Inception(112, (144, 288), (32, 64), 64), | |
Inception(256, (160, 320), (32, 128), 128), | |
nn.MaxPool2D(pool_size=3, strides=2, padding=1)) | |
b5 = nn.Sequential() | |
b5.add(Inception(256, (160, 320), (32, 128), 128),\ | |
Inception(384, (192, 384), (48, 128), 128), | |
nn.GlobalAvgPool2D()) | |
net = nn.Sequential() | |
X = nd.random.uniform(shape=(1, 1, 96, 96)) | |
net.add(b1, b2, b3, b4, b5, nn.Dense(19*19)) | |
# | |
# for layer in net: | |
# | |
# X = layer(X) | |
# | |
# print(layer.name, 'output shape:\t', X.shape) | |
# if __name__ == '__main__': | |
# X = nd.random.uniform(shape=(1, 1, 19, 19)) | |
# res=net(X) | |
# print(res) |