def OutShape(self,shape): self.conv1=Conv2D(output_channels=7,ksize=self.ksize,stride=self.stride,method="SAME") self.bn1=BN() self.relu1=Relu() self.conv2=Conv2D(output_channels=shape[-1],ksize=self.ksize,stride=self.stride,method="SAME") self.bn2=BN() self.relu2=Relu() out_shape=shape out_shape=self.conv1.OutShape(out_shape) out_shape=self.conv2.OutShape(out_shape) return out_shape
def __init__(self, batchSize): # network structure self.batchSize = batchSize self.conv1 = Conv2D([self.batchSize, 28, 28, 1], 6, 5) self.relu1 = Relu(self.conv1.output_shape) self.pool1 = MaxPooling(self.relu1.output_shape) self.fc1 = FullyConnect(self.pool1.output_shape_resize, 120) self.relu2 = Relu(self.fc1.output_shape) self.fc2 = FullyConnect(self.relu2.output_shape, 10) self.soft = Softmax(self.fc2.output_shape)
class res_block(object): def __init__(self, ksize=3, stride=1): self.stride = stride self.ksize = ksize def OutShape(self,shape): self.conv1=Conv2D(output_channels=7,ksize=self.ksize,stride=self.stride,method="SAME") self.bn1=BN() self.relu1=Relu() self.conv2=Conv2D(output_channels=shape[-1],ksize=self.ksize,stride=self.stride,method="SAME") self.bn2=BN() self.relu2=Relu() out_shape=shape out_shape=self.conv1.OutShape(out_shape) out_shape=self.conv2.OutShape(out_shape) return out_shape def forward(self, x): out=x out=self.conv1.forward(out) out=self.bn1.forward(out) out=self.relu1.forward(out) out=self.conv2.forward(out) out=self.bn2.forward(out) out=self.relu2.forward(x+out) return out def backward(self, eta): out=eta out=self.relu2.backward(out) outx=out out=self.bn2.backward(out) out=self.conv2.backward(out) out=self.relu1.backward(out) out=self.bn1.backward(out) out=self.conv1.backward(out) return out+outx def gradient(self, alpha=0.00001, weight_decay=0.0004): self.conv1.gradient(alpha=alpha,weight_decay=weight_decay) self.conv2.gradient(alpha=alpha,weight_decay=weight_decay)
def make_cnn(input_dim, num_of_classes): conv1 = Convolution(input_dim=input_dim, pad=2, stride=2, num_filters=10, filter_size=3, seed=1) relu1 = Relu() maxpool1 = Maxpool(input_dim=conv1.output_dim, filter_size=2, stride=1) flatten = Flatten(seed=1) dense1 = Dense(input_dim=np.prod(maxpool1.output_dim), output_dim=num_of_classes, seed=1) layers = [conv1, relu1, maxpool1, flatten, dense1] return layers
def __init__(self, input_size, hidden_size, output_size, \ weight_init_std=0.01): # Initialize weights. self.params = {} self.params['W1'] = weight_init_std * \ np.random.randn(input_size, hidden_size) self.params['b1'] = np.zeros(hidden_size) self.params['W2'] = weight_init_std * \ np.random.randn(hidden_size, output_size) self.params['b2'] = np.zeros(output_size) # Initialize layers. self.layers = OrderedDict() self.layers['Affine1'] = \ Affine(self.params['W1'], self.params['b1']) self.layers['Relu1'] = Relu() self.layers['Affine2'] = \ Affine(self.params['W2'], self.params['b2']) self.lastLayer = SoftmaxWithLoss()
conv1_out = op.Conv2D((3, 3, 3, 3), input_variable=a, name='conv1', padding='VALID').output_variables relu1_out = op.Relu(input_variable=conv1_out, name='relu1').output_variables pool1_out = op.MaxPooling(ksize=2, input_variable=relu1_out, name='pool1').output_variables fc1_out = op.FullyConnect(output_num=10, input_variable=pool1_out, name='fc1').output_variables sf_out = op.SoftmaxLoss(predict=fc1_out, label=label, name='sf').loss new_conv1 = op.GLOBAL_VARIABLE_SCOPE['conv1'] new_fc1 = op.GLOBAL_VARIABLE_SCOPE['fc1'] conv1 = Conv2D([1, 128, 128, 3], 3, 3, 1, method='VALID') relu1 = Relu(conv1.output_shape) pool1 = MaxPooling(conv1.output_shape) fc1 = FullyConnect(pool1.output_shape, 10) sf = Softmax(fc1.output_shape) conv1.weights = new_conv1.weights.data conv1.bias = new_conv1.bias.data fc1.weights = new_fc1.weights.data fc1.bias = new_fc1.bias.data out = sf.cal_loss( fc1.forward(pool1.forward(relu1.forward(conv1.forward(img)))), label.data) sf.gradient() eta = conv1.gradient(relu1.gradient(pool1.gradient(fc1.gradient(sf.eta)))) #
class Cnn(object): def __init__(self, batchSize): # network structure self.batchSize = batchSize self.conv1 = Conv2D([self.batchSize, 28, 28, 1], 6, 5) self.relu1 = Relu(self.conv1.output_shape) self.pool1 = MaxPooling(self.relu1.output_shape) self.fc1 = FullyConnect(self.pool1.output_shape_resize, 120) self.relu2 = Relu(self.fc1.output_shape) self.fc2 = FullyConnect(self.relu2.output_shape, 10) self.soft = Softmax(self.fc2.output_shape) def inference(self, input): # inference self.input = input conv1_out = self.conv1.forward(input) relu1_out = self.relu1.forward(conv1_out) pool1_out = self.pool1.forward(relu1_out) fc1_out = self.fc1.forward(pool1_out) relu2_out = self.relu2.forward(fc1_out) fc2_out = self.fc2.forward(relu2_out) predict = self.soft.predict(fc2_out) predict = np.argmax(predict, 1).reshape([self.batchSize, 1]) return predict def forward(self, input, label): # forward self.input = input self.label = label conv1_out = self.conv1.forward(input) relu1_out = self.relu1.forward(conv1_out) pool1_out = self.pool1.forward(relu1_out) pool2_out = pool1_out.reshape([self.batchSize, -1]) fc1_out = self.fc1.forward(pool2_out) relu2_out = self.relu2.forward(fc1_out) fc2_out = self.fc2.forward(relu2_out) predict = self.soft.predict(fc2_out) loss = self.soft.cal_loss(fc2_out, self.label) predict = np.argmax(predict, 1).reshape([self.batchSize, 1]) return predict, loss def backward(self, epoch, maxEpoch): learning_rate = [0.001, 0.0001] if epoch < maxEpoch // 3: current_learning_rate = learning_rate[0] else: current_learning_rate = learning_rate[1] self.conv1.backward(alpha=current_learning_rate, err=self.relu1.backward(self.pool1.backward( self.fc1.backward(alpha=current_learning_rate, err=self.relu2.backward( self.fc2.backward(alpha=current_learning_rate, err=self.soft.backward())))))) def _saveParasToJson(self, epoch): name_dict = {'conv1': self.conv1, 'fc1': self.fc1, 'fc2': self.fc2} model = {'conv1': {}, 'fc1': {}, 'fc2': {}} for layer in model: if 'weights' not in model[layer]: model[layer]['weights'] = name_dict[layer].weights if 'bias' not in model[layer]: model[layer]['bias'] = name_dict[layer].bias modelname = open(os.path.join(MODEL_PATH, 'modelcnn{}.pkl'.format(epoch)), 'wb') pickle.dump(model, modelname, -1) modelname.close() def saveTheModel(self, epoch): if (epoch + 1) % 1 == 0: self._saveParasToJson(epoch) def getTheParas(self, model_path): f = open(model_path, 'rb') model_dict = pickle.load(f) self.conv1.weights = np.array(model_dict['conv1']['weights']) self.conv1.bias = np.array(model_dict['conv1']['bias']) self.fc1.weights = np.array(model_dict['fc1']['weights']) self.fc1.bias = np.array(model_dict['fc1']['bias']) self.fc2.weights = np.array(model_dict['fc2']['weights']) self.fc2.bias = np.array(model_dict['fc2']['bias']) f.close()
labels = np.fromfile(lbpath, dtype=np.uint8) with open(images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII', imgpath.read(16)) images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784) return images, labels images, labels = load_mnist('./data/mnist') test_images, test_labels = load_mnist('./data/mnist', 't10k') batch_size = 64 conv1 = Conv2D([batch_size, 28, 28, 1], 12, 5, 1) relu1 = Relu(conv1.output_shape) pool1 = MaxPooling(relu1.output_shape) conv2 = Conv2D(pool1.output_shape, 24, 3, 1) relu2 = Relu(conv2.output_shape) pool2 = MaxPooling(relu2.output_shape) fc = FullyConnect(pool2.output_shape, 10) sf = Softmax(fc.output_shape) # train_loss_record = [] # train_acc_record = [] # val_loss_record = [] # val_acc_record = [] for epoch in range(20): # if epoch < 5: # learning_rate = 0.00001
#超参数 batch_size = 32 learning_rate = 0.01 # epochs=500 #定义网络结构 #不同的参数和结构会有不同的准确率,由于每次初始化也会影响到准确率, #下面这个网络在epochs=5时准确率在98.6%到98.75%之间 #下面这个网络在epochs=87时准确率99% #每个epoch耗时85秒 epochs = 5 seq = [ Conv2D(output_channels=8, ksize=5, stride=1), BN(), Relu(), MaxPooling(), Conv2D(output_channels=16, ksize=5, stride=1), BN(), Relu(), # Sigmoid(), Dropout(p=0.2), Dense(output_num=200), Relu(), Dense(output_num=10), # Sigmoid() Softmax() ] #残差网络 #每个epoch耗时97秒