def vgg_bn(): return [ #1 Conv2D([7, 7], 64, [1, 3, 3, 1]), Conv2DBatchNorm(64), Activation(tf.nn.relu), MaxPool([1,4,4,1],[1,1,1,1]), #2 Convolutional_block(f = 3, filters = [64,64,256],s = 1), MaxPool([1,5,5,1],[1,1,1,1]), Dropout(0.5), Identity_block(f = 3, filters=[64,64,256]), Dropout(0.5), Identity_block(f = 3, filters=[64,64,256]), Dropout(0.5), MaxPool([1,2,2,1],[1,1,1,1]), #3 Convolutional_block(f = 3, filters = [128,128,512],s = 2), Dropout(0.5), Identity_block(f = 3, filters=[128,128,512]), Dropout(0.5), Identity_block(f = 3, filters=[128,128,512]), Dropout(0.5), MaxPool([1,2,2,1],[1,1,1,1]), #4 Convolutional_block(f = 3, filters = [256,256,1024],s = 2), Identity_block(f = 3, filters=[256,256,1024]), Identity_block(f = 3, filters=[256,256,1024]), Identity_block(f = 3, filters=[256,256,1024]), Identity_block(f = 3, filters=[256,256,1024]), Identity_block(f = 3, filters=[256,256,1024]), Flatten(), Dense(128), Activation(tf.sigmoid), Dropout(0.5), Dense(10), #Fully_connected(), Activation(tf.nn.softmax), ]
def main(): c = color_codes() mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) try: net = load_model('/home/mariano/Desktop/test.tf') except IOError: x = Input([784]) x_image = Reshape([28, 28, 1])(x) x_conv1 = Conv(filters=32, kernel_size=(5, 5), activation='relu', padding='same')(x_image) h_pool1 = MaxPool((2, 2), padding='same')(x_conv1) h_conv2 = Conv(filters=64, kernel_size=(5, 5), activation='relu', padding='same')(h_pool1) h_pool2 = MaxPool((2, 2), padding='same')(h_conv2) h_fc1 = Dense(1024, activation='relu')(h_pool2) h_drop = Dropout(0.5)(h_fc1) y_conv = Dense(10)(h_drop) net = Model(x, y_conv, optimizer='adam', loss='categorical_cross_entropy', metrics='accuracy') print(c['c'] + '[' + strftime("%H:%M:%S") + '] ' + c['g'] + c['b'] + 'Original (MNIST)' + c['nc'] + c['g'] + ' net ' + c['nc'] + c['b'] + '(%d parameters)' % net.count_trainable_parameters() + c['nc']) net.fit(mnist.train.images, mnist.train.labels, val_data=mnist.test.images, val_labels=mnist.test.labels, patience=10, epochs=200, batch_size=1024) save_model(net, '/home/mariano/Desktop/test.tf')
def addMaxPool(self, **kwargs): """ Add MaxPooling activation layer. """ input_layer = self.input_layer if not self.all_layers\ else self.all_layers[-1] self.n_maxpool_layers += 1 name = "maxpool%i" % self.n_maxpool_layers new_layer = MaxPool(input_layer, name=name, **kwargs) self.all_layers += (new_layer, )
def inner_model(trainable, x): layers_list = [ Reshape([-1, 28, 28, 1]), Conv(32), BatchNormalization(), Relu(), MaxPool(), Conv(64), BatchNormalization(), Relu(), MaxPool(), Reshape([-1, 7 * 7 * 64]), FullyConnected(1024), Relu(), FullyConnected(10) ] variable_saver = VariableSaver() signal = x print('shape', signal.get_shape()) for idx, layer in enumerate(layers_list): signal = layer.contribute(signal, idx, trainable, variable_saver.save_variable) print('shape', signal.get_shape()) return signal, variable_saver.var_list
def __init__(self): super(Resnet, self).__init__() self.conv_1 = Convolution_Layer(kernel_height=7, kernel_width=7, channel_in=3, channel_out=24, stride=2, padding='SAME') self.batch_norm_1 = Batch_Normalization(depth=24, decay=0.99, convolution=True) self.max_pool_1 = MaxPool(kernel_size=3, strides=1, padding='VALID') self.block_ident_1 = Identity(kernel_height=3, kernel_width=3, channel_in=24, channel_out=48, stride=1, decay=0.99, batch_size=batch_size, output_dim=48) self.block_ident_2 = Identity(kernel_height=3, kernel_width=3, channel_in=48, channel_out=64, stride=1, decay=0.99, batch_size=batch_size, output_dim=48) self.block_ident_3 = Identity(kernel_height=3, kernel_width=3, channel_in=64, channel_out=96, stride=1, decay=0.99, batch_size=batch_size, output_dim=48) self.avg_pool = AvgPooling(kernel_height=7, kernel_width=7, strides=2, padding='VALID') self.flat_1 = Flatten_layer() self.dense_1 = Dense_layer(42336, 512) self.dense_2 = Dense_layer(512, num_classes)
def __init__(self, sizes, batch_size, epoch_num, use_trained_params=False, filename=None, img_dim=(3, 32, 32), conv_param={ 'filter_num': 32, 'filter_size': 3, 'padding': 1, 'stride': 1 }, optimizer='Adam', activation='ReLU', use_dropout=True, dropout_p=0.2, use_bn=True): self.num_layers = len(sizes) self.sizes = sizes self.batch_size = batch_size self.epoch_num = epoch_num # self.learning_rate = learning_rate self.activation = activation self.use_dropout = use_dropout self.dropout_p = dropout_p self.use_bn = use_bn self.filter_num = conv_param['filter_num'] self.filter_size = conv_param['filter_size'] self.filter_padding = conv_param['padding'] self.filter_stride = conv_param['stride'] self.img_c = img_dim[0] self.img_wh = img_dim[1] self.conv_output_size = int( (img_dim[1] - self.filter_size + 2 * self.filter_padding) / self.filter_stride) + 1 self.pool_output_size = int(self.filter_num * (self.conv_output_size / 2) * (self.conv_output_size / 2)) self.opt = optimizer optimizers = { 'SGD': SGD, 'Momentum_SGD': Momentum_SGD, 'AdaGrad': AdaGrad, 'RMSProp': RMSProp, 'AdaDelta': AdaDelta, 'Adam': Adam } self.optimizer = optimizers[self.opt]() if use_trained_params: path = os.path.dirname(os.path.abspath(__file__)) loaded_params = np.load(os.path.join(path, filename)) self.W1 = loaded_params['W1'] self.b1 = loaded_params['b1'] self.W2 = loaded_params['W2'] self.b2 = loaded_params['b2'] self.W3 = loaded_params['W3'] self.b3 = loaded_params['b3'] self.gamma = loaded_params['gamma'] self.beta = loaded_params['beta'] if use_bn: self.running_mean = loaded_params['running_mean'] self.running_var = loaded_params['running_var'] else: np.random.seed(12) # Conv層重み self.W1 = np.sqrt(1 / sizes[0]) * np.random.randn( self.filter_num, img_dim[0], self.filter_size, self.filter_size) self.b1 = np.sqrt(1 / sizes[0]) * np.random.randn(self.filter_num) # BatchNorm層 self.gamma = np.ones(self.filter_num * self.conv_output_size * self.conv_output_size) self.beta = np.zeros(self.filter_num * self.conv_output_size * self.conv_output_size) # FullyConnected層重み(中間層) self.W2 = np.sqrt(1 / self.pool_output_size) * np.random.randn( self.pool_output_size, self.sizes[1]) #(pool,100) self.b2 = np.sqrt(1 / self.pool_output_size) * np.random.randn( self.sizes[1]) # Fullyconnected層重み(出力層) self.W3 = np.sqrt(1 / sizes[1]) * np.random.randn( self.sizes[1], self.sizes[2]) self.b3 = np.sqrt(1 / sizes[1]) * np.random.randn(self.sizes[2]) # layers of network activation_function = {'Sigmoid': Sigmoid, 'ReLU': ReLU} self.layers = {} self.layers['Conv'] = Conv2D(self.W1, self.b1, self.filter_stride, self.filter_padding) if self.use_bn: if use_trained_params: self.layers['BatchNorm'] = BatchNorm(self.gamma, self.beta,\ running_mean=self.running_mean,running_var=self.running_var) else: self.layers['BatchNorm'] = BatchNorm(self.gamma, self.beta) self.layers['Activation'] = activation_function[self.activation]() if self.use_dropout: self.layers['Dropout'] = Dropout(self.dropout_p) self.layers['Pool'] = MaxPool(pool_h=2, pool_w=2, stride=2) self.layers['FullyConnected1'] = FullyConnected(self.W2, self.b2) self.layers['Activation2'] = activation_function[self.activation]() self.layers['FullyConnected2'] = FullyConnected(self.W3, self.b3) self.lastLayer = SoftmaxLoss()
reg = 0.0 model.add_layer( Convolution(32, (3, 3), input_shape=(batch_size, X_tr.shape[1], X_tr.shape[2], X_tr.shape[3]), weight_initializer=NormalInitializer(std))) model.add_layer(ReLuActivation()) model.add_layer(BatchNormalization()) model.add_layer( Convolution(32, (3, 3), weight_initializer=NormalInitializer(std), padding='same')) model.add_layer(ReLuActivation()) model.add_layer(MaxPool((2, 2))) model.add_layer(Flatten()) model.add_layer( Affine(100, weight_initializer=NormalInitializer(std), reg=reg)) model.add_layer(ReLuActivation()) model.add_layer(DropoutLayer(drop_rate=0.3)) model.add_layer( Affine(n_classes, weight_initializer=NormalInitializer(std), reg=reg)) model.initialize(loss=CrossEntropyLoss(), optimizer=Adam(learning_rate=0.001, decay_fst_mom=0.9, decay_sec_mom=0.999)) # with open('model_90_49.14262959724404', 'rb') as file: # model = pickle.load(file)
def vgg_bn(): return [ #1 Conv2D([3, 3], 64, [1, 1, 1, 1], padding='SAME'), Conv2DBatchNorm(64), Activation(tf.nn.relu), #2 Conv2D([3, 3], 64, [1, 1, 1, 1], padding='SAME'), Conv2DBatchNorm(64), Activation(tf.nn.relu), #3 MaxPool([1,2,2,1],[1,2,2,1],padding='SAME'), #4 Conv2D([3, 3], 128, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(128), Activation(tf.nn.relu), #5 Conv2D([3, 3], 128, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(128), Activation(tf.nn.relu), #6 MaxPool([1,2,2,1],[1,2,2,1],padding='SAME'), #7 Conv2D([3, 3], 256, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(256), Activation(tf.nn.relu), #8 Conv2D([3, 3], 256, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(256), Activation(tf.nn.relu), #9 Conv2D([3, 3], 256, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(256), Activation(tf.nn.relu), #10 MaxPool([1,2,2,1],[1,2,2,1],padding='SAME'), #11 Conv2D([3, 3], 512, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(512), Activation(tf.nn.relu), #12 Conv2D([3, 3], 512, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(512), Activation(tf.nn.relu), #13 Conv2D([3, 3], 512, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(512), Activation(tf.nn.relu), #14 MaxPool([1,2,2,1],[1,2,2,1],padding='SAME'), #15 Conv2D([3, 3], 512, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(512), Activation(tf.nn.relu), #16 Conv2D([3, 3], 512, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(512), Activation(tf.nn.relu), #17 Conv2D([3, 3], 512, [1, 1, 1, 1],padding='SAME'), Conv2DBatchNorm(512), Activation(tf.nn.relu), #18 MaxPool([1,2,2,1],[1,2,2,1],padding='SAME'), Flatten(), Dense(4096), Activation(tf.nn.relu), Dropout(0.5), Dense(4096), Activation(tf.nn.relu), Dense(1000), Activation(tf.nn.relu), Dense(10), Activation(tf.nn.softmax), ]