def SVHN(x, n_classes): import tools ''' Args: images: 4D tensor [batch_size, img_width, img_height, img_channel] Notes: In each conv layer, the kernel size is: [kernel_size, kernel_size, number of input channels, number of output channels]. number of input channels are from previuous layer, if previous layer is THE input layer, number of input channels should be image's channels. ''' x = tools.conv('conv1', x, 64) x = tools.pool('pool1', x) x = tools.conv('conv2', x, 64) x = tools.pool('pool2', x) x = tools.conv('conv3', x, 128) x = tools.pool('pool3', x) x = tools.FC_layer('fc4', x, out_nodes=64) x = tools.drop_out('drop_out', x, keep_prob=0.5) x = tools.final_layer('softmax', x, out_nodes=n_classes) return x
def Encoder_VGG(x, z_dim, n_class, is_pretrain=True, reuse = False): if (reuse): tf.get_variable_scope().reuse_variables() x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) x = tools.batch_norm(x) z_mu = tools.FC_layer('fc8', x, out_nodes=z_dim) z_lv = tools.FC_layer('fc9', x, out_nodes=z_dim) y_predict = tools.FC_layer('fc10', z_mu, out_nodes=n_class) return z_mu, z_lv, y_predict
def VGG16_comp(x, num_class): x = tools.conv_bn('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv5_1', x, 1024 , kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv5_2', x, 1024 , kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv5_3', x, 1024 , kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096, isActive=True) x = tools.FC_layer('fc7', x, out_nodes=2048, isActive=True) x = tf.nn.dropout(x,0.7) x = tools.FC_layer('fc8', x, out_nodes=num_class) return x
def VGG16(x, num_class): x = tools.conv_bn('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.conv_bn('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_bn('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) #x = tools.conv_bn('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) #x = tools.conv_bn('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) #x = tools.conv_bn('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) #x = tools.conv_bn('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) #x = tools.conv_bn('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1]) #x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv_1x1('act_1x1' ,x,256,isActive=True) x = tools.conv_1x1('down_1x1',x,128,isActive=False) x = tools.GAP('GAP',x,is_max_pool=False) x = tools.FC_layer('fc6', x, out_nodes=64, isActive=True) #x = tools.batch_norm(x,'fc6') #x = tools.batch_norm(x,'fc7') x = tf.nn.dropout(x,0.5) x = tools.FC_layer('fc7', x, out_nodes=num_class) return x
def VGG16(x, n_classes, is_pretrain=True): x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) x = tools.batch_norm(x) x = tools.FC_layer('fc7', x, out_nodes=4096) x = tools.batch_norm(x) x = tools.FC_layer('fc8', x, out_nodes=n_classes) return x
def lenet5(self): with tf.name_scope('LeNet5'): self.conv1 = tools.conv('conv1', self.input, 32, kernel_size=[5, 5], stride=[1, 1, 1, 1], is_trainable=self.is_trainable) self.pool1 = tools.pool('pool1', self.conv1, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.conv2 = tools.conv('conv2', self.pool1, 64, kernel_size=[5, 5], stride=[1, 1, 1, 1], is_trainable=self.is_trainable) self.pool2 = tools.pool('pool2', self.conv2, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.fc1 = tools.fc_layer('fc1', self.pool2, out_nodes=512) self.dropout1 = tools.dropout('dropout1', self.fc1, self.keep_prob) self.logits = tools.fc_layer('fc2', self.dropout1, use_relu=False, out_nodes=self.n_classes)
def VGG16(x, n_classes, is_pretrain=True): x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) #x = tools.batch_norm(x) x = tools.FC_layer('fc7', x, out_nodes=4096) #x = tools.batch_norm(x) x = tools.FC_layer('fc8', x, out_nodes=n_classes) return x
def VGG16N(x, n_classes, is_pretrain=True): with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) with tf.name_scope('batch_norm1'): x = tools.batch_norm(x) feature = tools.FC_layer('fc7', x, out_nodes=4096) with tf.name_scope('batch_norm2'): x = tools.batch_norm(feature) x = tools.FC_layer('fc8', x, out_nodes=n_classes) return x,feature #%%
def VGG16N(x, n_classes, is_pretrain=True): # is_pretrain=false : no tranining, use parameter from vgg16.npy # is_pretrain=true: trainning new parameter with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) # x = tf.nn.dropout(x, 0.5) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) # x = tf.nn.dropout(x, 0.5) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=False) # x = tf.nn.dropout(x, 0.5) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True) # x = tf.nn.dropout(x, 0.5) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True) x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True) x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=True) x = tf.nn.dropout(x, 0.5) with tf.name_scope('pool5'): x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) x = tf.nn.dropout(x, 0.5) #with tf.name_scope('batch_norm1'): # x = tools.batch_norm(x) x = tools.FC_layer('fc7', x, out_nodes=4096) #with tf.name_scope('batch_norm2'): x = tf.nn.dropout(x,0.5) # x = tools.batch_norm(x) x = tools.FC_layer('fc8', x, out_nodes=n_classes) return x
def VGG16(x, isSty, is_pretrain=False): x = x - np.array([ 123.68 , 116.779, 103.939]) every_layer_output = {} x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) every_layer_output['conv1_2'] = x #conv1_2 x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) every_layer_output['conv2_2'] = x #conv2_2 if isSty: x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) every_layer_output['conv3_3'] = x #conv3_3 x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) every_layer_output['conv4_3'] = x #conv4_3 # x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # # x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # # x = tools.FC_layer('fc6', x, out_nodes=4096) # #x = tools.batch_norm(x) # x = tools.FC_layer('fc7', x, out_nodes=4096) # #x = tools.batch_norm(x) # x = tools.FC_layer('fc8', x, out_nodes=n_classes) return every_layer_output
def Network(x, n_classes, dropout): global is_training x = tf.reshape(x, [-1, 28, 28, 1]) x = tools.conv('conv1', x, 32, kernel_size=[5, 5], is_pretrain=is_pretrain) x = tools.pool('pool1', x, is_max_pool=True) x = tools.conv('conv2', x, 64, is_pretrain=is_pretrain) x = tools.pool('pool2', x, is_max_pool=True) fc_x = tools.FC_layer('FC1', x, 1024) fc_x = tf.layers.dropout(fc_x, rate=dropout, training=is_training) out = tools.FC_layer('FC2', fc_x, n_classes) return out
def Model_finetune(layer, n_classes, is_pretrain=True): # ''' # Model_finetune: that uses varies tools.py function to setup CNN and returns the logits # Arg: layer(tensor), n_classes(int), is_pretrain(Booleen) # layer: list of images that are decoded in tensor # n_classes: batch size for the training # is_pretrain: number of classification for output # Return: layer(logits) # ''' with tf.name_scope('Model_finetune'): # first conv + pool layer = tools.conv('conv1_1', layer, 64, kernel_size=[7,7], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): layer = tools.pool('pool1', layer, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # second conv + pool layer = tools.conv('conv2_1', layer, 128, kernel_size=[7,7], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): layer = tools.pool('pool2', layer, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # thrid conv + pool layer = tools.conv('conv3_1', layer, 256, kernel_size=[7,7], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): layer = tools.pool('pool3', layer, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # fourth conv + pool layer = tools.conv('conv4_1', layer, 512, kernel_size=[7,7], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): layer = tools.pool('pool4', layer, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # fifth conv + pool layer = tools.conv('conv5_1', layer, 512, kernel_size=[7,7], stride=[1,1,1,1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): layer = tools.pool('pool5', layer, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) # 3 fully connected layers, last one is softmax layer = tools.FC_layer('fc6', layer, out_nodes=2048) with tf.name_scope('batch_norm1'): layer = tools.batch_norm(layer) layer = tools.FC_layer('fc7', layer, out_nodes=2048) with tf.name_scope('batch_norm2'): layer = tools.batch_norm(layer) layer = tools.FC_layer('fc8', layer, out_nodes=n_classes) return layer
def VGG16N_CNN(x, n_classes, is_pretrain=True): with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): cnn_x = tools.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) shape = cnn_x.get_shape() size = shape[1].value * shape[2].value * shape[3].value cnn_x = tf.reshape(cnn_x, [-1, size]) # flatten into 1D # x = tools.FC_layer('fc6', cnn_x, out_nodes=4096) # # with tf.name_scope('batch_norm1'): # # x = tools.batch_norm(x) # x = tools.FC_layer('fc7', cnn_x, out_nodes=4096) # # with tf.name_scope('batch_norm2'): # # x = tools.batch_norm(x) # x = tools.FC_layer('fc8', cnn_x, out_nodes=n_classes) return cnn_x
def VGG16(x, n_class, is_pretrain=True): # using the name scope, the tensorboard maybe look better with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): x = tools.pool('pool1', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): x = tools.pool('pool2', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): x = tools.pool('pool3', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): x = tools.pool('pool4', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 2, 2, 1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): x = tools.pool('pool5', x, ksize=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) with tf.name_scope('batch_norma1'): x = tools.batch_norm(x) # batch norm can avoid overfit, more efficient than dropout x = tools.FC_layer('fc7', x, out_nodes=4096) with tf.name_scope('batch_norm2'): x = tools.batch_norm(x) x = tools.FC_layer('fc8', x, out_nodes=n_class) return x
def Network(x, n_classes, dropout, reuse, is_training): with tf.variable_scope('ConvNet', reuse=reuse): x = x['images'] x = tf.reshape(x, [-1, 28, 28, 1]) x = tools.conv('conv1', x, 32, kernel_size=[5, 5], is_pretrain=is_pretrain) x = tools.pool('pool1', x, is_max_pool=True) x = tools.conv('conv2', x, 64, is_pretrain=is_pretrain) x = tools.pool('pool2', x, is_max_pool=True) fc_x = tools.FC_layer('FC1', x, 1024) fc_x = tf.layers.dropout(fc_x, rate=dropout, training=is_training) out = tools.FC_layer('FC2', fc_x, n_classes) return out
def VGG16_no_fc(x, keep_prob=0.5, is_pretrain=True): with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): x = tools.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) y = tools.FC_layer('fc6', x, out_nodes=2048, keep_prob=keep_prob) # with tf.name_scope('batch_norm1'): # x = tools.batch_norm(x) y = tools.FC_layer('fc7', x, out_nodes=512, keep_prob=keep_prob) # with tf.name_scope('batch_norm2'): # x = tools.batch_norm(x) y = tools.FC_layer('fc8', x, out_nodes=2, keep_prob=keep_prob) return x
def LeNet5(self): with tf.name_scope('LeNet5'): self.conv1 = tools.conv('conv1', self.input, 16, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool1 = tools.pool('pool1', self.conv1, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) self.conv2 = tools.conv('conv2', self.pool1, 16, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool2 = tools.pool('pool2', self.conv2, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) self.fc1 = tools.FC_layer('local3', self.pool2, out_nodes=128) self.norm1 = tools.batch_norm('batch_norm1', self.fc1) self.fc2 = tools.FC_layer('local4', self.norm1, out_nodes=128) self.norm2 = tools.batch_norm('batch_norm2', self.fc2) self.fc3 = tools.FC_layer('softmax_linear', self.norm2, out_nodes=self.n_classes, use_relu=False)
def AlexNet(x, n_classes, is_pretrain=True): with tf.name_scope('AlexNet'): x = tools.conv('conv1', x, 16, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool1', x, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) x = tools.conv('conv2', x, 16, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool2', x, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) x = tools.FC_layer('local3', x, out_nodes=128) x = tools.batch_norm('batch_norm1', x) x = tools.FC_layer('local4', x, out_nodes=128) x = tools.batch_norm('batch_norm2', x) x = tools.FC_layer('softmax_linear', x, out_nodes=n_classes) return x
def AlexNet(x, n_class): x = tools.conv_layer('conv1', x, filter_size=11, strides=4, in_channels=3, out_channels=96, activation=True) x = tools.pool('pool1', x, p_ksize=3, p_strides=2, is_max_pool=True) x = tools.conv_layer('conv2', x, filter_size=5, strides=1, in_channels=96, out_channels=256, activation=True) x = tools.pool('pool2', x, p_ksize=3, p_strides=2, is_max_pool=True) x = tools.conv_layer('conv3', x, filter_size=3, strides=1, in_channels=256, out_channels=384, activation=True) x = tools.conv_layer('conv4', x, filter_size=3, strides=1, in_channels=384, out_channels=384, activation=True) x = tools.conv_layer('conv5', x, filter_size=3, strides=1, in_channels=384, out_channels=256, activation=True) x = tools.pool('pool3', x, p_ksize=3, p_strides=2, is_max_pool=True) x = tools.FC_layer('FC1', x, out_nodes=4096, if_relu=True) x = tools.FC_layer('FC2', x, out_nodes=4096, if_relu=True) x = tools.FC_layer('FC3', x, out_nodes=n_class, if_relu=None) return x
def mnist_net(x, prob): with tf.variable_scope('mnist_net'): x = tf.reshape(x, shape=[-1, 28, 28, 1]) # first convolution layer x = tools.conv('conv_layer_1', x, 32, kernel_size=[5, 5], stride=[1, 1, 1, 1]) with tf.name_scope('max_pool_1'): x = tools.pool('max_pool_1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) # second convolution layer x = tools.conv('conv_layer_2', x, 64, kernel_size=[5, 5], stride=[1, 1, 1, 1]) with tf.name_scope('max_pool_2'): x = tools.pool('max_pool_2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) # fully convolution layers x = tools.FC_layer('fc_layer_1', x, 1024) x = tf.nn.relu(x) x = tools.drop(x, prob) x = tools.FC_layer('fc_layer_2', x, 10) return x
def VGG19(x, n_classes, is_pretrain=True): x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_4', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_4', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_4', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1]) x = tools.FC_layer('fc6', x, out_nodes=4096) x = tf.nn.dropout(x, keep_prob=0.5) x = tools.FC_layer('fc7', x, out_nodes=4096) x = tf.nn.dropout(x, keep_prob=0.5) x = tools.FC_layer('fc8', x, out_nodes=n_classes) return x
def VGG16N(x, n_classes, keep_prob,is_pretrain=True): with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # with tf.name_scope('batch_norm1'): # x = tools.batch_norm(x) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # with tf.name_scope('batch_norm2'): # x = tools.batch_norm(x) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # with tf.name_scope('batch_norm3'): # x = tools.batch_norm(x) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # with tf.name_scope('batch_norm4'): # x = tools.batch_norm(x) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3,3], stride=[1,1,1,1], is_pretrain=is_pretrain) # with tf.name_scope('batch_norm5'): # x = tools.batch_norm(x) with tf.name_scope('pool5'): x = tools.pool('pool5', x, kernel=[1,2,2,1], stride=[1,2,2,1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) # with tf.name_scope('batch_norm6'): # x = tools.batch_norm(x) #dropouut x=tf.nn.dropout(x, keep_prob) x = tools.FC_layer('fc7', x, out_nodes=4096) # with tf.name_scope('batch_norm7'): # x = tools.batch_norm(x) #dropout x=tf.nn.dropout(x, keep_prob) fc_output=x x = tools.softmax_layer('fc8', x, out_nodes=n_classes) return x,fc_output #%%
def convlayers(self): self.parameters = [] # zero-mean input with tf.name_scope('preprocess') as scope: mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean') images = self.imgs - mean # images = self.imgs # conv1_1 # do_conv(name, input_tensor, out_channel, ksize, stride=[1, 1, 1, 1], is_pretrain=True): self.conv1_1 = do_conv('conv1_1', images, 64, [3, 3], is_pretrain=self.trainable['conv1_1'], regularizer=self.regularizer) self.conv1_2 = do_conv('conv1_2', self.conv1_1, 64, [3, 3], is_pretrain=self.trainable['conv1_2'], regularizer=self.regularizer) self.pooling1 = pool('pooling1', self.conv1_2, is_max_pool=True) self.conv2_1 = do_conv('conv2_1', self.pooling1, 128, [3, 3], is_pretrain=self.trainable['conv2_1'], regularizer=self.regularizer) self.conv2_2 = do_conv('conv2_2', self.conv2_1, 128, [3, 3], is_pretrain=self.trainable['conv2_2'], regularizer=self.regularizer) self.pooling2 = pool('pooling2', self.conv2_2, is_max_pool=True) self.conv3_1 = do_conv('conv3_1', self.pooling2, 256, [3, 3], is_pretrain=self.trainable['conv3_1'], regularizer=self.regularizer) self.conv3_2 = do_conv('conv3_2', self.conv3_1, 256, [3, 3], is_pretrain=self.trainable['conv3_2'], regularizer=self.regularizer) self.conv3_3 = do_conv('conv3_3', self.conv3_2, 256, [3, 3], is_pretrain=self.trainable['conv3_3'], regularizer=self.regularizer) self.pooling3 = pool('pooing3', self.conv3_3, is_max_pool=True) self.conv4_1 = do_conv('conv4_1', self.pooling3, 512, [3, 3], is_pretrain=self.trainable['conv4_1'], regularizer=self.regularizer) self.conv4_2 = do_conv('conv4_2', self.conv4_1, 512, [3, 3], is_pretrain=self.trainable['conv4_2'], regularizer=self.regularizer) self.conv4_3 = do_conv('conv4_3', self.conv4_2, 512, [3, 3], is_pretrain=self.trainable['conv4_3'], regularizer=self.regularizer) self.pooling4 = pool('pooling4', self.conv4_3, is_max_pool=True) self.conv5_1 = do_conv('conv5_1', self.pooling4, 512, [3, 3], is_pretrain=self.trainable['conv5_1'], regularizer=self.regularizer) self.conv5_2 = do_conv('conv5_2', self.conv5_1, 512, [3, 3], is_pretrain=self.trainable['conv5_2'], regularizer=self.regularizer) self.conv5_3 = do_conv('conv5_3', self.conv5_2, 512, [3, 3], is_pretrain=self.trainable['conv5_3'], regularizer=self.regularizer) self.pooling5 = pool('pooling5', self.conv5_3, is_max_pool=True) self.convs_output = self.pooling5
def VGG16N(x, n_classes, IS_PRETRAIN): import tools x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[0]) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[1]) x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[2]) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[3]) x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[4]) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[5]) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[6]) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[7]) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[8]) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[9]) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[10]) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[11]) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=IS_PRETRAIN[12]) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096, is_pretrain=IS_PRETRAIN[13]) x = tools.batch_norm(x) x = tools.FC_layer('fc7', x, out_nodes=4096, is_pretrain=IS_PRETRAIN[14]) x = tools.batch_norm(x) x = tools.final_layer('fc8', x, out_nodes=n_classes) return x
def VGG16N(x, n_classes, is_pretrain=True): with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 8, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 8, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 16, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 16, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 32, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 32, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 32, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): x = tools.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=512) x = tf.nn.dropout(x, 0.5) #with tf.name_scope('batch_norm1'): #x = tools.batch_norm(x) x = tools.FC_layer('fc7', x, out_nodes=512) x = tf.nn.dropout(x, 0.5) #with tf.name_scope('batch_norm2'): #x = tools.batch_norm(x) x = tools.FC_layer('fc8', x, out_nodes=512) x = tf.nn.dropout(x, 0.5) x = tools.FC_layer('fc9', x, out_nodes=24) return x #%%
def VGG16_DP(x, n_classes, is_pretrain=True, flag=0): with tf.name_scope('VGG16'): x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool1'): x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool2'): x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool3'): x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool4'): x = tools.pool('pool4', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) with tf.name_scope('pool5'): x = tools.pool('pool5', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) with tf.name_scope('dropout1'): if flag == 0: dropout1 = tf.nn.dropout(x, 0.5) else: dropout1 = tf.nn.dropout(x, 1) x = tf.nn.relu(dropout1) x = tools.FC_layer('fc7', x, out_nodes=4096) with tf.name_scope('dropout2'): if flag == 0: dropout2 = tf.nn.dropout(x, 0.5) else: dropout2 = tf.nn.dropout(x, 1) x = tf.nn.relu(dropout2) x = tools.last_FC('fc8', x, out_nodes=n_classes) return x
def VGG16PlanInferencet(x, keep_prob, n_classes=12, is_pretrain=True): x = tools.conv('conv1_1', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv1_2', x, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool1', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv2_1', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv2_2', x, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool2', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv3_1', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_2', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv3_3', x, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv4_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv4_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.conv('conv5_1', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_2', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.conv('conv5_3', x, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) x = tools.pool('pool3', x, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) x = tools.FC_layer('fc6', x, out_nodes=4096) x = tools.batch_norm(x) x = tools.FC_layer('fc7', x, out_nodes=4096) x = tools.batch_norm(x) x_drop = tf.nn.dropout(x, keep_prob) x = tools.FC_layer('fc8', x_drop, out_nodes=n_classes) return x
def VGG16(self): with tf.name_scope('VGG16'): self.conv1_1 = tools.conv('conv1_1', self.input, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv1_2 = tools.conv('conv1_2', self.conv1_1, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool1 = tools.pool('pool1', self.conv1_2, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.conv2_1 = tools.conv('conv2_1', self.pool1, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv2_2 = tools.conv('conv2_2', self.conv2_1, 128, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool2 = tools.pool('pool2', self.conv2_2, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.conv3_1 = tools.conv('conv3_1', self.pool2, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv3_2 = tools.conv('conv3_2', self.conv3_1, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv3_3 = tools.conv('conv3_3', self.conv3_2, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool3 = tools.pool('pool3', self.conv3_3, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.conv4_1 = tools.conv('conv4_1', self.pool3, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv4_2 = tools.conv('conv4_2', self.conv4_1, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv4_3 = tools.conv('conv4_3', self.conv4_2, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool4 = tools.pool('pool4', self.conv4_3, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.conv5_1 = tools.conv('conv5_1', self.pool4, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv5_2 = tools.conv('conv5_2', self.conv5_1, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv5_3 = tools.conv('conv5_3', self.conv5_2, 512, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool5 = tools.pool('pool5', self.conv5_3, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) self.fc6 = tools.FC_layer('fc6', self.pool5, out_nodes=4096) self.batch_norm1 = tools.batch_norm('batch_norm1', self.fc6) self.fc7 = tools.FC_layer('fc7', self.batch_norm1, out_nodes=4096) self.batch_norm2 = tools.batch_norm('batch_norm2', self.fc7) self.fc8 = tools.FC_layer('fc8', self.batch_norm2, out_nodes=self.n_classes)
with tf.name_scope('inputs'): x = tf.placeholder(tf.float32, shape=[None, 784]) y_ = tf.placeholder(tf.float32, shape=[None, 10]) x_images = tf.reshape(x, [-1, 28, 28, 1]) with tf.name_scope('conv_net'): outputs = tools.conv('conv1_1', x_images, 32, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) #outputs = tools.conv('conv1_2', outputs, 32, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) outputs = tools.pool('pool1', outputs, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) outputs = tools.conv('conv2_1', outputs, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) #outputs = tools.conv('conv2_2', outputs, 64, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=is_pretrain) outputs = tools.pool('pool2', outputs, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True)
def AlexNet(self): with tf.name_scope('AlexNet'): self.conv1 = tools.conv('conv1', self.input, 96, kernel_size=[11, 11], stride=[1, 4, 4, 1], is_pretrain=self.is_pretrain) self.pool1 = tools.pool('pool1', self.conv1, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) self.conv2 = tools.conv('conv2', self.pool1, 256, kernel_size=[5, 5], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool2 = tools.pool('pool2', self.conv2, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) self.conv3 = tools.conv('conv3', self.pool2, 384, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv4 = tools.conv('conv4', self.conv3, 384, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.conv5 = tools.conv('conv5', self.conv4, 256, kernel_size=[3, 3], stride=[1, 1, 1, 1], is_pretrain=self.is_pretrain) self.pool5 = tools.pool('pool5', self.conv5, kernel=[1, 3, 3, 1], stride=[1, 2, 2, 1], is_max_pool=True, is_norm=True) self.fc1 = tools.FC_layer('fc6', self.pool5, out_nodes=4096) self.norm1 = tools.batch_norm('batch_norm1', self.fc1) self.fc2 = tools.FC_layer('fc7', self.norm1, out_nodes=4096) self.norm2 = tools.batch_norm('batch_norm2', self.fc2) self.fc3 = tools.FC_layer('softmax_linear', self.norm2, out_nodes=self.n_classes, use_relu=False)
cat = tf.cast(cat, tf.float32) #[360, 300, 3] x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3] # First conv with tf.variable_scope('conv1'): w = tools.weight([3, 3, 3, 16], is_uniform=True) x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') b = tools.bias([16]) x_b = tf.nn.bias_add(x_w, b) x_relu = tf.nn.relu(x_b) x_pool = tools.pool('test1', x_relu, kernel=[1, 2, 2, 1], stride=[1, 2, 2, 1], is_max_pool=True) # Second conv with tf.variable_scope('conv2'): w2 = tools.weight([3, 3, 16, 32], is_uniform=True) x_w2 = tf.nn.conv2d(x_pool, w2, strides=[1, 1, 1, 1], padding='SAME') b2 = tools.bias([32]) x_b2 = tf.nn.bias_add(x_w2, b2) x_relu2 = tf.nn.relu(x_b2) x_pool2 = tools.pool('test2', x_relu2,
cat = tf.cast(cat, tf.float32) #[360, 300, 3] x = tf.reshape(cat, [1, 360, 300, 3]) #[1, 360, 300, 3] #%% # First conv with tf.variable_scope('conv1'): w = tools.weight([3,3,3,16], is_uniform=True) x_w = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME') b = tools.bias([16]) x_b = tf.nn.bias_add(x_w, b) x_relu = tf.nn.relu(x_b) x_pool = tools.pool('test1', x_relu, kernel=[1,2,2,1], stride=[1,2,2,1],is_max_pool=True) # Second conv with tf.variable_scope('conv2'): w2 = tools.weight([3,3,16,32], is_uniform=True) x_w2 = tf.nn.conv2d(x_pool, w2, strides=[1, 1, 1, 1], padding='SAME') b2 = tools.bias([32]) x_b2 = tf.nn.bias_add(x_w2, b2) x_relu2 = tf.nn.relu(x_b2) x_pool2 = tools.pool('test2',x_relu2, kernel=[1,2,2,1],stride=[1,2,2,1], is_max_pool=False) x_BN = tools.batch_norm(x_pool2)