def full_conv(x, K, is_training, wd): with tf.variable_scope('conv1x1'): current = common.batchNormalization(x, is_training=is_training) current = tf.nn.relu(current) current = common.spatialConvolution(current, 1, 1, 4 * K, wd=wd) with tf.variable_scope('conv3x3'): current = common.batchNormalization(current, is_training=is_training) current = tf.nn.relu(current) current = common.spatialConvolution(current, 3, 1, K, wd=wd) return current
def block(x, spec, wd, is_training): with tf.variable_scope('conv1'): nin = common.spatialConvolution(x, spec[0], spec[1], spec[2], wd= wd) nin = common.batchNormalization(nin, is_training= is_training) nin = tf.nn.relu(nin) with tf.variable_scope('conv2'): nin = common.spatialConvolution(nin, 1, 1, spec[2], wd= wd) nin = common.batchNormalization(nin, is_training= is_training) nin = tf.nn.relu(nin) with tf.variable_scope('conv3'): nin = common.spatialConvolution(nin, 1, 1, spec[2], wd= wd) nin = common.batchNormalization(nin, is_training= is_training) nin = tf.nn.relu(nin) return nin
def inference(x, num_output, wd, dropout_rate, is_training, transfer_mode= False): with tf.variable_scope('conv1'): network = common.spatialConvolution(x, 11, 4, 64, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu (network) #common.activation_summary(network) network = common.maxPool(network, 3, 2) with tf.variable_scope('conv2'): network = common.spatialConvolution(network, 5, 1, 192, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu(network) #common.activation_summary(network) network = common.maxPool(network, 3, 2) with tf.variable_scope('conv3'): network = common.spatialConvolution(network, 3, 1, 384, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu(network) #common.activation_summary(network) with tf.variable_scope('conv4'): network = common.spatialConvolution(network, 3, 1, 256, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu(network) with tf.variable_scope('conv5'): network = common.spatialConvolution(network, 3, 1, 256, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu(network) network = common.maxPool(network, 3, 2) network = common.flatten(network) with tf.variable_scope('fc1'): network = tf.nn.dropout(network, dropout_rate) network = common.fullyConnected(network, 4096, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu(network) with tf.variable_scope('fc2'): network = tf.nn.dropout(network, dropout_rate) network = common.fullyConnected(network, 4096, wd= wd) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.relu(network) if not transfer_mode: with tf.variable_scope('output'): network = common.fullyConnected(network, num_output, wd= wd) else: with tf.variable_scope('transfer_output'): network = common.fullyConnected(network, num_output, wd= wd) return network
def transition(x, K, wd, is_training): with tf.variable_scope('conv'): current = common.batchNormalization(x, is_training=is_training) current = tf.nn.relu(current) shape = current.get_shape().as_list() dim = math.floor(shape[3] * 0.5) current = common.spatialConvolution(current, 1, 1, dim, wd=wd) current = common.avgPool(current, 2, 2) return current
def inception(x, conv1_size, conv3_size, conv5_size, pool1_size, wd, is_training): with tf.variable_scope("conv_1"): conv1 = common.spatialConvolution(x, 1, 1, conv1_size, wd=wd) conv1 = common.batchNormalization(conv1, is_training=is_training) conv1 = tf.nn.relu(conv1) with tf.variable_scope("conv_3_1"): conv3 = common.spatialConvolution(x, 1, 1, conv3_size[0], wd=wd) conv3 = common.batchNormalization(conv3, is_training=is_training) conv3 = tf.nn.relu(conv3) with tf.variable_scope("conv_3_2"): conv3 = common.spatialConvolution(conv3, 3, 1, conv3_size[1], wd=wd) conv3 = common.batchNormalization(conv3, is_training=is_training) conv3 = tf.nn.relu(conv3) with tf.variable_scope("conv_5_1"): conv5 = common.spatialConvolution(x, 1, 1, conv5_size[0], wd=wd) conv5 = common.batchNormalization(conv5, is_training=is_training) conv5 = tf.nn.relu(conv5) with tf.variable_scope("conv_5_2"): conv5 = common.spatialConvolution(conv5, 5, 1, conv5_size[1], wd=wd) conv5 = common.batchNormalization(conv5, is_training=is_training) conv5 = tf.nn.relu(conv5) with tf.variable_scope("pool_1"): pool1 = common.maxPool(x, 3, 1) pool1 = common.spatialConvolution(pool1, 1, 1, pool1_size, wd=wd) pool1 = common.batchNormalization(pool1, is_training=is_training) pool1 = tf.nn.relu(pool1) return tf.concat([conv1, conv3, conv5, pool1], 3)
def inference(x, num_output, wd, dropout_rate, is_training, transfer_mode= False, model_type= 'A'): # Create tables describing VGG configurations A, B, D, E if model_type == 'A': config = [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'] elif model_type == 'B': config = [64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'] elif model_type == 'D': config = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512, 512, 512, 'M'] elif model_type == 'E': config = [64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512, 'M', 512, 512, 512, 512, 'M'] else: print('Unknown model type: ' + model_type + ' | Please specify a modelType A or B or D or E') network= x for k,v in enumerate(config): if v == 'M': network= common.maxPool(network, 2, 2) else: with tf.variable_scope('conv'+str(k)): network = common.spatialConvolution(network, 3, 1, v, wd= wd) network = tf.nn.relu(network) network= common.flatten(network) with tf.variable_scope('fc1'): network = common.fullyConnected(network, 4096, wd= wd) network = tf.nn.relu(network) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.dropout(network, dropout_rate) with tf.variable_scope('fc2'): network = common.fullyConnected(network, 4096, wd= wd) network = tf.nn.relu(network) network = common.batchNormalization(network, is_training= is_training) network = tf.nn.dropout(network, dropout_rate) if not transfer_mode: with tf.variable_scope('output'): network = common.fullyConnected(network, num_output, wd= wd) else: with tf.variable_scope('transfer_output'): network = common.fullyConnected(network, num_output, wd= wd) return network
def getModel(x, num_output, K, stages, wd, is_training, transfer_mode=False): with tf.variable_scope('conv1'): x = common.spatialConvolution(x, 7, 2, 2 * K, wd=wd) x = common.batchNormalization(x, is_training=is_training) x = tf.nn.relu(x) x = common.maxPool(x, 3, 2) with tf.variable_scope('block1'): x = block(x, stages[0], K, is_training=is_training, wd=wd) with tf.variable_scope('trans1'): x = transition(x, K, wd=wd, is_training=is_training) with tf.variable_scope('block2'): x = block(x, stages[1], K, is_training=is_training, wd=wd) with tf.variable_scope('trans2'): x = transition(x, K, wd=wd, is_training=is_training) with tf.variable_scope('block3'): x = block(x, stages[2], K, is_training=is_training, wd=wd) with tf.variable_scope('trans3'): x = transition(x, K, wd=wd, is_training=is_training) with tf.variable_scope('block4'): x = block(x, stages[3], K, is_training=is_training, wd=wd) x = common.avgPool(x, 7, 1, padding='VALID') x = common.flatten(x) if not transfer_mode: with tf.variable_scope('output'): x = common.fullyConnected(x, num_output, wd=wd) else: with tf.variable_scope('transfer_output'): x = common.fullyConnected(x, num_output, wd=wd) return x
def getModel( x, num_output, wd, is_training, num_blocks=[3, 4, 6, 3], # defaults to 50-layer network bottleneck=True): conv_weight_initializer = tf.truncated_normal_initializer(stddev=0.1) fc_weight_initializer = tf.truncated_normal_initializer(stddev=0.01) with tf.variable_scope('scale1'): x = common.spatialConvolution( x, 7, 2, 64, weight_initializer=conv_weight_initializer, wd=wd) x = common.batchNormalization(x, is_training=is_training) x = tf.nn.relu(x) with tf.variable_scope('scale2'): x = common.maxPool(x, 3, 2) x = common.resnetStack(x, num_blocks[0], 1, 64, bottleneck, wd=wd, is_training=is_training) with tf.variable_scope('scale3'): x = common.resnetStack(x, num_blocks[1], 2, 128, bottleneck, wd=wd, is_training=is_training) with tf.variable_scope('scale4'): x = common.resnetStack(x, num_blocks[2], 2, 256, bottleneck, wd=wd, is_training=is_training) with tf.variable_scope('scale5'): x = common.resnetStack(x, num_blocks[3], 2, 512, bottleneck, wd=wd, is_training=is_training) # post-net x = tf.reduce_mean(x, reduction_indices=[1, 2], name="avg_pool") with tf.variable_scope('output'): x = common.fullyConnected(x, num_output, weight_initializer=fc_weight_initializer, bias_initializer=tf.zeros_initializer, wd=wd) return x
def inference(x, num_output, wd, dropout_rate, is_training, transfer_mode=False): with tf.variable_scope('features'): with tf.variable_scope('conv1'): network = common.spatialConvolution(x, 7, 2, 64, wd=wd) network = common.batchNormalization(network, is_training=is_training) network = tf.nn.relu(network) network = common.maxPool(network, 3, 2) with tf.variable_scope('conv2'): network = common.spatialConvolution(network, 1, 1, 64, wd=wd) network = common.batchNormalization(network, is_training=is_training) network = tf.nn.relu(network) with tf.variable_scope('conv3'): network = common.spatialConvolution(network, 3, 1, 192, wd=wd) network = common.batchNormalization(network, is_training=is_training) network = tf.nn.relu(network) network = common.maxPool(network, 3, 2) with tf.variable_scope('inception3a'): network = inception(network, 64, [96, 128], [16, 32], 32, wd=wd, is_training=is_training) with tf.variable_scope('inception3b'): network = inception(network, 128, [128, 192], [32, 96], 64, wd=wd, is_training=is_training) network = common.maxPool(network, 3, 2) with tf.variable_scope('inception4a'): network = inception(network, 192, [96, 208], [16, 48], 64, wd=wd, is_training=is_training) with tf.variable_scope('inception4b'): network = inception(network, 160, [112, 224], [24, 64], 64, wd=wd, is_training=is_training) with tf.variable_scope('inception4c'): network = inception(network, 128, [128, 256], [24, 64], 64, wd=wd, is_training=is_training) with tf.variable_scope('inception4d'): network = inception(network, 112, [144, 288], [32, 64], 64, wd=wd, is_training=is_training) with tf.variable_scope('mainb'): with tf.variable_scope('inception4e'): main_branch = inception(network, 256, [160, 320], [32, 128], 128, wd=wd, is_training=is_training) main_branch = common.maxPool(main_branch, 3, 2) with tf.variable_scope('inception5a'): main_branch = inception(main_branch, 256, [160, 320], [32, 128], 128, wd=wd, is_training=is_training) with tf.variable_scope('inception5b'): main_branch = inception(main_branch, 384, [192, 384], [48, 128], 128, wd=wd, is_training=is_training) main_branch = common.avgPool(main_branch, 7, 1) main_branch = common.flatten(main_branch) main_branch = tf.nn.dropout(main_branch, dropout_rate) if not transfer_mode: with tf.variable_scope('output'): main_branch = common.fullyConnected(main_branch, num_output, wd=wd) else: with tf.variable_scope('transfer_output'): main_branch = common.fullyConnected(main_branch, num_output, wd=wd) with tf.variable_scope('auxb'): aux_classifier = common.avgPool(network, 5, 3) with tf.variable_scope('conv1'): aux_classifier = common.spatialConvolution(aux_classifier, 1, 1, 128, wd=wd) aux_classifier = common.batchNormalization(aux_classifier, is_training=is_training) aux_classifier = tf.nn.relu(aux_classifier) aux_classifier = common.flatten(aux_classifier) with tf.variable_scope('fc1'): aux_classifier = common.fullyConnected(aux_classifier, 1024, wd=wd) aux_classifier = tf.nn.dropout(aux_classifier, dropout_rate) if not transfer_mode: with tf.variable_scope('output'): aux_classifier = common.fullyConnected(aux_classifier, num_output, wd=wd) else: with tf.variable_scope('transfer_output'): aux_classifier = common.fullyConnected(aux_classifier, num_output, wd=wd) return tf.concat([main_branch, aux_classifier], 1)