def model(is_training, resue, num_classes=5): common_args = common_layer_args(is_training, reuse) conv_args = make_args( untie_biases=True, batch_norm=batch_norm, **common_args) logit_args = make_args(activation=prelu, **common_args) inputs = input((None, crop_size[1], crop_size[0], 3), **common_args) net = conv2d(inputs, 32, filter_size=(3, 3), stride=( 2, 2), name='conv1', **conv_params) net = conv2d(net, 64, name='conv2', **conv_params) net = bottleneck_v1(net, num_unit=128, name='block_v1_1', **conv_args) net = bottleneck_v1(net, num_unit=256, name='block_v1_2', **conv_args) net = bottleneck_v1(net, num_unit=728, name='block_v1_3', **conv_args) for i in range(8): prefix = 'block_v2_' + str(i + 5) net = bottleneck_v2(net, num_unit=728, name=prefix, **kwargs) net = bottleneck_v1(net, num_unit=1024, name='block_v1_4', **conv_args) net = separable_conv2d(net, 1536, filter_size=(3, 3), stride=(1, 1), name='sconv1', **kwargs) net = separable_conv2d(net, 2048, filter_size=(3, 3), stride=(1, 1), name='sconv2', **kwargs) with tf.variable_scope('Logits'): net = avg_pool_2d(net, net.get_shape()[1:3], name='AvgPool_1a') net = dropout( net, is_training, drop_p=1 - dropout_keep_prob, name='Dropout_1b') logits = fully_connected(net, num_classes, name='logits', **logit_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def model(is_training, reuse, dropout_keep_prob=0.5): common_args = common_layer_args(is_training, reuse) conv_args = make_args(batch_norm=True, activation=prelu, w_init=initz.he_normal( scale=1), untie_biases=False, **common_args) pool_args = make_args(padding='SAME', **common_args) inputs = input((None, crop_size[1], crop_size[0], 3), **common_args) with tf.variable_scope('squeezenet', values=[inputs]): net = conv2d(inputs, 96, stride=(2, 2), name='conv1', **conv_args) net = max_pool(net, name='maxpool1', **pool_args) net = fire_module(net, 16, 64, name='fire2', **conv_args) net = fire_module(net, 16, 64, name='fire3', **conv_args) net = fire_module(net, 32, 128, name='fire4', **conv_args) net = max_pool(net, name='maxpool4', **pool_args) net = fire_module(net, 32, 128, name='fire5', **conv_args) net = fire_module(net, 48, 192, name='fire6', **conv_args) net = fire_module(net, 48, 192, name='fire7', **conv_args) net = fire_module(net, 64, 256, name='fire8', **conv_args) net = max_pool(net, name='maxpool8', **pool_args) net = fire_module(net, 64, 256, name='fire9', **conv_args) # Reversed avg and conv layers per 'Network in Network' net = dropout(net, drop_p=1 - dropout_keep_prob, name='dropout6', **common_args) net = conv2d(net, 10, filter_size=(1, 1), name='conv10', **conv_args) logits = global_avg_pool(net, name='logits', **pool_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def model(is_training, reuse): common_args = common_layer_args(is_training, reuse) fc_args = make_args(activation=relu, **common_args) logit_args = make_args(activation=None, **common_args) x = input((None, height * width), **common_args) x = fully_connected(x, n_output=100, name='fc1', **fc_args) logits = fully_connected(x, n_output=10, name="logits", **logit_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def model(is_training, reuse): common_args = common_layer_args(is_training, reuse) x = input((None, 7, 7, 512), **common_args) # x = batch_norm_tf(x, **common_args) x = fully_connected(x, 512, activation=relu, name='fc1', **common_args) x = dropout(x, drop_p=0.5, name='dropout1', **common_args) logits = fully_connected(x, 6, activation=None, name='logits', **common_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def model(is_training, reuse): common_args = common_layer_args(is_training, reuse) conv_args = make_args(batch_norm=None, activation=prelu, **common_args) fc_args = make_args(activation=prelu, **common_args) logit_args = make_args(activation=None, **common_args) x = input((None, crop_size[1], crop_size[0], 1), **common_args) x = conv2d(x, 32, name='conv1_1', **conv_args) x = conv2d(x, 32, name='conv1_2', **conv_args) x = max_pool(x, name='pool1', **common_args) x = dropout(x, drop_p=0.25, name='dropout1', **common_args) x = fully_connected(x, n_output=128, name='fc1', **fc_args) x = dropout(x, drop_p=0.5, name='dropout2', **common_args) logits = fully_connected(x, n_output=36, name="logits", **logit_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def model(is_training, reuse): common_args = common_layer_args(is_training, reuse) conv_args = make_args(activation=relu, **common_args) pool_args = make_args(filter_size=(2, 2), **common_args) fc_args = make_args(activation=relu, **common_args) logit_args = make_args(activation=None, **common_args) x = input((None, crop_size[1], crop_size[0], 3), **common_args) x = conv2d(x, 64, name='conv1_1', **conv_args) x = conv2d(x, 64, name='conv1_2', **conv_args) x = max_pool(x, name='maxpool1', **pool_args) x = conv2d(x, 128, name='conv2_1', **conv_args) x = conv2d(x, 128, name='conv2_2', **conv_args) x = max_pool(x, name='maxpool2', **pool_args) x = conv2d(x, 256, name='conv3_1', **conv_args) x = conv2d(x, 256, name='conv3_2', **conv_args) x = conv2d(x, 256, name='conv3_3', **conv_args) x = max_pool(x, name='maxpool3', **pool_args) x = conv2d(x, 512, name='conv4_1', **conv_args) x = conv2d(x, 512, name='conv4_2', **conv_args) x = conv2d(x, 512, name='conv4_3', **conv_args) x = max_pool(x, name='maxpool4', **pool_args) x = conv2d(x, 512, name='conv5_1', **conv_args) x = conv2d(x, 512, name='conv5_2', **conv_args) x = conv2d(x, 512, name='conv5_3', **conv_args) x = max_pool(x, name='maxpool5', **pool_args) x = fully_connected(x, n_output=4096, name='fc6', **fc_args) x = dropout(x, drop_p=0.5, name='dropout1', **common_args) x = fully_connected(x, n_output=4096, name='fc7', **fc_args) x = dropout(x, drop_p=0.5, name='dropout2', **common_args) logits = fully_connected(x, n_output=1000, name="logits", **logit_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def model(is_training, reuse, flexi_inputs=False): common_args = common_layer_args(is_training, reuse) conv_args = make_args(activation=relu, **common_args) pool_args = make_args(filter_size=(2, 2), **common_args) logit_args = make_args(activation=None, **common_args) if flexi_inputs: inputs_shape = (None, None, None, 3) else: inputs_shape = (None, crop_size[1], crop_size[0], 3) net_inputs = input(inputs_shape, **common_args) x = net_inputs with tf.variable_scope('vgg_16', reuse=reuse): mean_rgb = tf.get_variable(name='mean_rgb', initializer=tf.truncated_normal(shape=[3]), trainable=False) x = x - mean_rgb with tf.variable_scope('conv1'): x = conv2d(x, 64, name='conv1_1', **conv_args) x = conv2d(x, 64, name='conv1_2', **conv_args) x = max_pool(x, name='maxpool1', **pool_args) with tf.variable_scope('conv2'): x = conv2d(x, 128, name='conv2_1', **conv_args) x = conv2d(x, 128, name='conv2_2', **conv_args) x = max_pool(x, name='maxpool2', **pool_args) with tf.variable_scope('conv3'): x = conv2d(x, 256, name='conv3_1', **conv_args) x = conv2d(x, 256, name='conv3_2', **conv_args) x = conv2d(x, 256, name='conv3_3', **conv_args) x = max_pool(x, name='maxpool3', **pool_args) with tf.variable_scope('conv4'): x = conv2d(x, 512, name='conv4_1', **conv_args) x = conv2d(x, 512, name='conv4_2', **conv_args) x = conv2d(x, 512, name='conv4_3', **conv_args) x = max_pool(x, name='maxpool4', **pool_args) with tf.variable_scope('conv5'): x = conv2d(x, 512, name='conv5_1', **conv_args) x = conv2d(x, 512, name='conv5_2', **conv_args) x = conv2d(x, 512, name='conv5_3', **conv_args) x = max_pool(x, name='maxpool5', **pool_args) x = conv2d(x, 4096, name='fc6', filter_size=(7, 7), padding='VALID', **conv_args) x = dropout(x, drop_p=0.5, name='dropout6', **common_args) x = conv2d(x, 4096, name='fc7', filter_size=(1, 1), **conv_args) x = dropout(x, drop_p=0.5, name='dropout7', **common_args) x = conv2d(x, 1000, name='fc8', filter_size=(1, 1), **logit_args) if flexi_inputs: logits = alias(x, name='logits', **common_args) else: logits = squeeze(x, axis=[1, 2], name='logits', **common_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(is_training)
def vgg_16(is_training, reuse, num_classes=1000, dropout_keep_prob=0.5, spatial_squeeze=True, name='vgg_16'): """Oxford Net VGG 16-Layers version D Example. Note: All the fully_connected layers have been transformed to conv2d layers. To use in classification mode, resize input to 224x224. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_classes: number of predicted classes. is_training: whether or not the model is being trained. dropout_keep_prob: the probability that activations are kept in the dropout layers during training. spatial_squeeze: whether or not should squeeze the spatial dimensions of the outputs. Useful to remove unnecessary dimensions for classification. name: Optional name for the variables. Returns: the last op containing the log predictions and end_points dict. """ common_args = common_layer_args(is_training, reuse) conv_args = make_args(batch_norm=True, activation=prelu, w_init=initz.he_normal( scale=1), untie_biases=False, **common_args) logit_args = make_args( activation=None, w_init=initz.he_normal(scale=1), **common_args) pred_args = make_args( activation=prelu, w_init=initz.he_normal(scale=1), **common_args) pool_args = make_args(padding='SAME', **common_args) inputs = input((None, crop_size[1], crop_size[0], 3), **common_args) with tf.variable_scope(name, 'vgg_16', [inputs]): net = repeat(inputs, 2, conv2d, 64, filter_size=(3, 3), name='conv1', **conv_args) net = max_pool(net, name='pool1', **pool_args) net = repeat(net, 2, conv2d, 128, filter_size=( 3, 3), name='conv2', **conv_args) net = max_pool(net, name='pool2', **pool_args) net = repeat(net, 3, conv2d, 256, filter_size=( 3, 3), name='conv3', **conv_args) net = max_pool(net, name='pool3', **pool_args) net = repeat(net, 3, conv2d, 512, filter_size=( 3, 3), name='conv4', **conv_args) net = max_pool(net, name='pool4', **pool_args) net = repeat(net, 3, conv2d, 512, filter_size=( 3, 3), name='conv5', **conv_args) net = max_pool(net, name='pool5', **pool_args) # Use conv2d instead of fully_connected layers. net = conv2d(net, 4096, filter_size=(7, 7), name='fc6', **conv_args) net = dropout(net, drop_p=1 - dropout_keep_prob, is_training=is_training, name='dropout6', **common_args) net = conv2d(net, 4096, filter_size=(1, 1), name='fc7', **conv_args) net = dropout(net, drop_p=1 - dropout_keep_prob, is_training=is_training, name='dropout7', **common_args) logits = conv2d(net, num_classes, filter_size=(1, 1), activation=None, name='logits', **logit_args) # Convert end_points_collection into a end_point dict. if spatial_squeeze: logits = tf.squeeze(logits, [1, 2], name='logits/squeezed') predictions = softmax(logits, name='predictions', **pred_args) return end_points(is_training)
def model(is_training, reuse, inputs=None): common_trainable_args = common_layer_args(is_training, reuse, trainable=True) common_frozen_args = common_layer_args(is_training, reuse, trainable=False) conv_args = make_conv_args(activation=relu, **common_frozen_args) logit_args = make_args(activation=None, **common_trainable_args) common_args = common_frozen_args # move this down to train only a few layers common_args = common_trainable_args if inputs is None: net = input((None, crop_size[1], crop_size[0], 3), **common_args) else: net = inputs with tf.variable_scope('resnet_v1_50', reuse=reuse): mean_rgb = tf.get_variable(name='mean_rgb', initializer=tf.truncated_normal(shape=[3]), trainable=False) net = net - mean_rgb net = conv2d_same(net, 64, filter_size=(7, 7), stride=(2, 2), name='conv1', **conv_args) net = max_pool(net, filter_size=(3, 3), stride=(2, 2), padding='SAME', name='pool1') with tf.variable_scope('block1') as sc: with tf.variable_scope('unit_1'): net = bottleneck(net, 256, 64, 1, **common_args) with tf.variable_scope('unit_2'): net = bottleneck(net, 256, 64, 1, **common_args) with tf.variable_scope('unit_3'): net = bottleneck(net, 256, 64, 2, **common_args) net = collect_named_outputs(common_args['outputs_collections'], sc.name, net) with tf.variable_scope('block2') as sc: with tf.variable_scope('unit_1'): net = bottleneck(net, 512, 128, 1, **common_args) with tf.variable_scope('unit_2'): net = bottleneck(net, 512, 128, 1, **common_args) with tf.variable_scope('unit_3'): net = bottleneck(net, 512, 128, 1, **common_args) with tf.variable_scope('unit_4'): net = bottleneck(net, 512, 128, 2, **common_args) net = collect_named_outputs(common_args['outputs_collections'], sc.name, net) with tf.variable_scope('block3') as sc: with tf.variable_scope('unit_1'): net = bottleneck(net, 1024, 256, 1, **common_args) with tf.variable_scope('unit_2'): net = bottleneck(net, 1024, 256, 1, **common_args) with tf.variable_scope('unit_3'): net = bottleneck(net, 1024, 256, 1, **common_args) with tf.variable_scope('unit_4'): net = bottleneck(net, 1024, 256, 1, **common_args) with tf.variable_scope('unit_5'): net = bottleneck(net, 1024, 256, 1, **common_args) with tf.variable_scope('unit_6'): net = bottleneck(net, 1024, 256, 2, **common_args) net = collect_named_outputs(common_args['outputs_collections'], sc.name, net) with tf.variable_scope('block4') as sc: with tf.variable_scope('unit_1'): net = bottleneck(net, 2048, 512, 1, **common_args) with tf.variable_scope('unit_2'): net = bottleneck(net, 2048, 512, 1, **common_args) with tf.variable_scope('unit_3'): net = bottleneck(net, 2048, 512, 1, **common_args) net = collect_named_outputs(common_args['outputs_collections'], sc.name, net) net = tf.reduce_mean(net, [1, 2], name='pool5', keep_dims=True) net = conv2d(net, 1000, filter_size=(1, 1), name='logits', **logit_args) logits = squeeze(net, axis=[1, 2], name='logits', **common_args) predictions = softmax(logits, name='predictions', **common_args) return end_points(common_args['is_training'])
def alexnet_v2(is_training, reuse, num_classes=1000, dropout_keep_prob=0.5, spatial_squeeze=True, scope='alexnet_v2'): """AlexNet version 2. Described in: http://arxiv.org/pdf/1404.5997v2.pdf Parameters from: github.com/akrizhevsky/cuda-convnet2/blob/master/layers/ layers-imagenet-1gpu.cfg Note: All the fully_connected layers have been transformed to conv2d layers. To use in classification mode, resize input to 224x224. To use in fully convolutional mode, set spatial_squeeze to false. The LRN layers have been removed and change the initializers from random_normal_initializer to xavier_initializer. Args: inputs: a tensor of size [batch_size, height, width, channels]. num_classes: number of predicted classes. is_training: whether or not the model is being trained. dropout_keep_prob: the probability that activations are kept in the dropout layers during training. spatial_squeeze: whether or not should squeeze the spatial dimensions of the outputs. Useful to remove unnecessary dimensions for classification. scope: Optional scope for the variables. Returns: the last op containing the log predictions and end_points dict. """ common_args = common_layer_args(is_training, reuse) conv_args = make_args(batch_norm=True, activation=prelu, w_init=initz.he_normal(scale=1), untie_biases=False, **common_args) logit_args = make_args(activation=None, w_init=initz.he_normal(scale=1), **common_args) pred_args = make_args(activation=prelu, w_init=initz.he_normal(scale=1), **common_args) pool_args = make_args(padding='SAME', **common_args) inputs = input((None, crop_size[1], crop_size[0], 3), **common_args) with tf.variable_scope(scope, 'alexnet_v2', [inputs]): net = conv2d(inputs, 64, filter_size=(11, 11), stride=(4, 4), scope='conv1', **conv_args) net = max_pool(net, stride=(2, 2), scope='pool1', **pool_args) net = conv2d(net, 192, filter_size=(5, 5), scope='conv2', **conv_args) net = max_pool(net, stride=(2, 2), scope='pool2', **pool_args) net = conv2d(net, 384, scope='conv3', **conv_args) net = conv2d(net, 384, scope='conv4', **conv_args) net = conv2d(net, 256, scope='conv5', **conv_args) net = max_pool(net, stride=(2, 2), scope='pool5', **pool_args) # Use conv2d instead of fully_connected layers. net = conv2d(net, 4096, filter_size=(5, 5), scope='fc6', **conv_args) net = dropout(net, drop_p=1 - dropout_keep_prob, scope='dropout6', **common_args) net = conv2d(net, 4096, filter_size=(1, 1), scope='fc7', **conv_args) net = dropout(net, drop_p=1 - dropout_keep_prob, scope='dropout7', **common_args) logits = conv2d(net, num_classes, filter_size=(1, 1), activation=None, scope='logits', **logit_args) if spatial_squeeze: logits = tf.squeeze(logits, [1, 2], name='fc8/squeezed') predictions = softmax(logits, name='predictions', **pred_args) return end_points(is_training)