def row_column_max_pooling(bottom, prefix='', window=(7, 7)):
    column_mx = slim.max_pool2d(bottom, [window[0], 1],
        stride=[window[0], 1], scope=prefix + '_column_max')
    row_mx = slim.max_pool2d(bottom, [1, window[1]],
        stride=[1, window[1]], scope=prefix + '_row_max')

    column_mean = slim.avg_pool2d(column_mx, [1, window[1]],
        stride=[1, window[1]], scope=prefix + '_column_mean')
    row_mean = slim.avg_pool2d(row_mx, [window[0], 1],
        stride=[window[0], 1], scope=prefix + '_row_mean')

    return row_mean + column_mean
Пример #2
0
def build_single_inceptionv1(train_tfdata, is_train, dropout_keep_prob):
    with slim.arg_scope(inception.inception_v1_arg_scope()):
        identity, end_points = inception.inception_v1(train_tfdata, dropout_keep_prob = dropout_keep_prob, is_training=is_train)
        net = slim.avg_pool2d(end_points['Mixed_5c'], [7, 7], stride=1, scope='MaxPool_0a_7x7')
        net = slim.dropout(net, dropout_keep_prob, scope='Dropout_0b')
        feature = tf.squeeze(net, [1, 2])
    return identity, feature
Пример #3
0
    def SSIM(self, x, y):
        C1 = 0.01 ** 2
        C2 = 0.03 ** 2

        mu_x = slim.avg_pool2d(x, 3, 1, 'SAME')
        mu_y = slim.avg_pool2d(y, 3, 1, 'SAME')

        sigma_x  = slim.avg_pool2d(x ** 2, 3, 1, 'SAME') - mu_x ** 2
        sigma_y  = slim.avg_pool2d(y ** 2, 3, 1, 'SAME') - mu_y ** 2
        sigma_xy = slim.avg_pool2d(x * y , 3, 1, 'SAME') - mu_x * mu_y

        SSIM_n = (2 * mu_x * mu_y + C1) * (2 * sigma_xy + C2)
        SSIM_d = (mu_x ** 2 + mu_y ** 2 + C1) * (sigma_x + sigma_y + C2)

        SSIM = SSIM_n / SSIM_d

        return tf.clip_by_value((1 - SSIM) / 2, 0, 1)
Пример #4
0
def inception_v3(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 prediction_fn=slim.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 scope='InceptionV3'):
    with tf.variable_scope(scope,'InceptionV3',[inputs,num_classes],reuse=reuse) as scope:
        with slim.arg_scope([slim.batch_norm,slim.dropout],
                            is_training=is_training):
            net,end_points=inception_v3_base(inputs,scope=scope)
            with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d], stride=1, padding='SAME'):
                aux_logits = end_points['mixed_6e']
                with tf.variable_scope('AuxLogits'):
                    aux_logits = slim.avg_pool2d(aux_logits, [5, 5],
                                                 stride=3, padding='VALID', scope='avgpool_1a_5x5')
                    aux_logits = slim.conv2d(aux_logits, 128, [1, 1], scope='conv2d_1b_1x1')
                    aux_logits = slim.conv2d(
                        aux_logits, 768, [5, 5],
                        weights_initializer=trunc_normal(0.01),
                        padding='VALID', scope='conv2d_2a_5x5'
                    )
                    aux_logits = slim.conv2d(
                        aux_logits, num_classes, [1, 1], activation_fn=None,
                        normalizer_fn=None, weights_initializer=trunc_normal(0.001),
                        scope='conv2d_2b_1x1'
                    )
                    if spatial_squeeze:
                        aux_logits = tf.squeeze(aux_logits, [1, 2], name='SpatialSqueeze')
                    end_points['AuxLogits'] = aux_logits

                    with tf.variable_scope('Logits'):
                        net = slim.avg_pool2d(net, [8, 8], padding='VALID', scope='avgpool_1a_8x8')
                        net = slim.dropout(net, keep_prob=dropout_keep_prob, scope='dropout_1b')
                        end_points['PreLogits'] = net
                        logits = slim.conv2d(net, num_classes, [1, 1], activation_fn=None,
                                             normalizer_fn=None, scope='conv2d_1c_1x1')
                        if spatial_squeeze:
                            logits = tf.squeeze(logits, [1, 2], name='SpatialSqueeze')
                    end_points['Logits'] = logits
                    end_points['Predictions'] = prediction_fn(logits, scope='Predictions')
                return logits, end_points
Пример #5
0
  def __init__(self, images, pretrained_ckpt, reuse=False,
               random_projection=False, random_projection_dim=32):
    # Build InceptionV3 graph.
    (inception_output,
     self.inception_variables,
     self.init_fn) = build_inceptionv3_graph(
         images, 'Mixed_7c', False, pretrained_ckpt, reuse)

    # Pool 8x8x2048 -> 1x1x2048.
    embedding = slim.avg_pool2d(inception_output, [8, 8], stride=1)
    embedding = tf.squeeze(embedding, [1, 2])

    if random_projection:
      embedding = tf.matmul(
          embedding, tf.random_normal(
              shape=[2048, random_projection_dim], seed=123))
    self.embedding = embedding
    def build_model(self, input_image, center_map, batch_size):
        self.batch_size = batch_size
        self.input_image = input_image
        self.center_map = center_map
        with tf.variable_scope('pooled_center_map'):
            # center map is a gaussion template which gather the respose
            self.center_map = slim.avg_pool2d(self.center_map,
                                              [9, 9], stride=8,
                                              padding='SAME',
                                              scope='center_map')

        with slim.arg_scope([slim.conv2d],
                            padding='SAME',
                            activation_fn=tf.nn.relu,
                            weights_initializer=tf.contrib.layers.xavier_initializer()):
            with tf.variable_scope('sub_stages'):
                net = slim.conv2d(input_image, 64, [3, 3], scope='sub_conv1')
                net = slim.conv2d(net, 64, [3, 3], scope='sub_conv2')
                net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='sub_pool1')
                net = slim.conv2d(net, 128, [3, 3], scope='sub_conv3')
                net = slim.conv2d(net, 128, [3, 3], scope='sub_conv4')
                net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='sub_pool2')
                net = slim.conv2d(net, 256, [3, 3], scope='sub_conv5')
                net = slim.conv2d(net, 256, [3, 3], scope='sub_conv6')
                net = slim.conv2d(net, 256, [3, 3], scope='sub_conv7')
                net = slim.conv2d(net, 256, [3, 3], scope='sub_conv8')
                net = slim.max_pool2d(net, [2, 2], padding='SAME', scope='sub_pool3')
                net = slim.conv2d(net, 512, [3, 3], scope='sub_conv9')
                net = slim.conv2d(net, 512, [3, 3], scope='sub_conv10')
                net = slim.conv2d(net, 512, [3, 3], scope='sub_conv11')
                net = slim.conv2d(net, 512, [3, 3], scope='sub_conv12')
                net = slim.conv2d(net, 512, [3, 3], scope='sub_conv13')
                net = slim.conv2d(net, 512, [3, 3], scope='sub_conv14')

                self.sub_stage_img_feature = slim.conv2d(net, 128, [3, 3],
                                                         scope='sub_stage_img_feature')

            with tf.variable_scope('stage_1'):
                conv1 = slim.conv2d(self.sub_stage_img_feature, 512, [1, 1],
                                    scope='conv1')
                self.stage_heatmap.append(slim.conv2d(conv1, self.joints, [1, 1],
                                                      scope='stage_heatmap'))

            for stage in range(2, self.stages + 1):
                self._middle_conv(stage)
Пример #7
0
def custom_residual_block(x, neurons, kernel_size, stride, name, is_training,
                          wt_decay=0.0001, use_residual=True,
                          residual_stride_conv=True, conv_fn=slim.conv2d,
                          batch_norm_param=None):
  
  # batch norm x and relu
  init_var = np.sqrt(2.0/(kernel_size**2)/neurons)
  with arg_scope([conv_fn], 
                 weights_regularizer=slim.l2_regularizer(wt_decay),
                 weights_initializer=tf.random_normal_initializer(stddev=init_var),
                 biases_initializer=tf.zeros_initializer()): 
    
    if batch_norm_param is None:
      batch_norm_param = {'center': True, 'scale': False, 
                          'activation_fn':tf.nn.relu, 
                          'is_training': is_training}
    
    y = slim.batch_norm(x, scope=name+'_bn', **batch_norm_param)

    y = conv_fn(y, num_outputs=neurons, kernel_size=kernel_size, stride=stride,
                activation_fn=None, scope=name+'_1',
                normalizer_fn=slim.batch_norm,
                normalizer_params=batch_norm_param)
    
    y = conv_fn(y, num_outputs=neurons, kernel_size=kernel_size,
                    stride=1, activation_fn=None, scope=name+'_2')

    if use_residual:
      if stride != 1 or x.get_shape().as_list()[-1] != neurons:
        batch_norm_param_ = dict(batch_norm_param)
        batch_norm_param_['activation_fn'] = None
        x = conv_fn(x, num_outputs=neurons, kernel_size=1,
                        stride=stride if residual_stride_conv else 1,
                        activation_fn=None, scope=name+'_0_1x1',
                        normalizer_fn=slim.batch_norm,
                        normalizer_params=batch_norm_param_)
        if not residual_stride_conv:
          x = slim.avg_pool2d(x, 1, stride=stride, scope=name+'_0_avg')
  
      y = tf.add(x, y, name=name+'_add')
    
    return y
def inference(images, keep_probability, phase_train=True, bottleneck_layer_size=128, weight_decay=0.0, reuse=None):
    batch_norm_params = {
        # Decay for the moving averages.
        'decay': 0.995,
        # epsilon to prevent 0s in variance.
        'epsilon': 0.001,
        # force in-place updates of mean and variance estimates
        'updates_collections': None,
        # Moving averages ends up in the trainable variables collection
        'variables_collections': [ tf.GraphKeys.TRAINABLE_VARIABLES ],
    }
    with slim.arg_scope([slim.conv2d, slim.fully_connected],
                        weights_initializer=slim.xavier_initializer_conv2d(uniform=True),
                        weights_regularizer=slim.l2_regularizer(weight_decay),
                        normalizer_fn=slim.batch_norm,
                        normalizer_params=batch_norm_params):
        with tf.variable_scope('squeezenet', [images], reuse=reuse):
            with slim.arg_scope([slim.batch_norm, slim.dropout],
                                is_training=phase_train):
                net = slim.conv2d(images, 96, [7, 7], stride=2, scope='conv1')
                net = slim.max_pool2d(net, [3, 3], stride=2, scope='maxpool1')
                net = fire_module(net, 16, 64, scope='fire2')
                net = fire_module(net, 16, 64, scope='fire3')
                net = fire_module(net, 32, 128, scope='fire4')
                net = slim.max_pool2d(net, [2, 2], stride=2, scope='maxpool4')
                net = fire_module(net, 32, 128, scope='fire5')
                net = fire_module(net, 48, 192, scope='fire6')
                net = fire_module(net, 48, 192, scope='fire7')
                net = fire_module(net, 64, 256, scope='fire8')
                net = slim.max_pool2d(net, [3, 3], stride=2, scope='maxpool8')
                net = fire_module(net, 64, 256, scope='fire9')
                net = slim.dropout(net, keep_probability)
                net = slim.conv2d(net, 1000, [1, 1], activation_fn=None, normalizer_fn=None, scope='conv10')
                net = slim.avg_pool2d(net, net.get_shape()[1:3], scope='avgpool10')
                net = tf.squeeze(net, [1, 2], name='logits')
                net = slim.fully_connected(net, bottleneck_layer_size, activation_fn=None, 
                        scope='Bottleneck', reuse=False)
    return net, None
Пример #9
0
def inception_v3_base(inputs,scope=None):
    end_points={}
    with tf.variable_scope(scope,'InceptionV3',[inputs]):
        with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],
                            stride=1,padding='VALID'):
            net=slim.conv2d(inputs,32,[3,3],stride=2,scope='conv2d_1a_3x3')
            net=slim.conv2d(net,32,[3,3],scope='conv2d_2a_3x3')
            net=slim.conv2d(net,64,[3,3],padding='SAME',scope='conv2d_2b_3x3')
            net=slim.max_pool2d(net,[3,3],stride=2,scope='maxpool_3a_3x3')
            net=slim.conv2d(net,80,[1,1],scope='conv2d_3b_1x1')
            net=slim.conv2d(net,192,[3,3],scope='conv2d_4a_3x3')
            net=slim.max_pool2d(net,[3,3],stride=2,scope='maxpool_5a_3x3')

    with slim.arg_scope([slim.conv2d,slim.max_pool2d,slim.avg_pool2d],
                        stride=1,padding='SAME'):
        with tf.variable_scope('mixed_5b'):
            with tf.variable_scope('branch_0'):
                branch_0=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,48,[1,1],scope='conv2d_01_1x1')
                branch_1=slim.conv2d(branch_1,64,[5,5],scope='conv2d_0b_5x5')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,96,[3,3],scope='conv2d_0b_3x3')
                branch_2=slim.conv2d(branch_2, 96, [3, 3], scope='conv2d_0c_3x3')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,32,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0,branch_1,branch_2,branch_3],3)

        with tf.variable_scope('mixed_5c'):
            with tf.variable_scope('branch_0'):
                branch_0=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,48,[1,1],scope='conv2d_01_1x1')
                branch_1=slim.conv2d(branch_1,64,[5,5],scope='conv2d_0b_5x5')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,96,[3,3],scope='conv2d_0b_3x3')
                branch_2=slim.conv2d(branch_2, 96, [3, 3], scope='conv2d_0c_3x3')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,64,[1,1],scope='conv2d_0b_1x1')
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)

        with tf.variable_scope('mixed_5d'):
            with tf.variable_scope('branch_0'):
                branch_0=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,48,[1,1],scope='conv2d_01_1x1')
                branch_1=slim.conv2d(branch_1,64,[5,5],scope='conv2d_0b_5x5')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,96,[3,3],scope='conv2d_0b_3x3')
                branch_2 = slim.conv2d(branch_2, 96, [3, 3], scope='conv2d_0c_3x3')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,64,[1,1],scope='conv2d_0b_1x1')
            net = tf.concat([branch_0, branch_1, branch_2, branch_3], 3)

        with tf.variable_scope('mixed_6a'):
            with tf.variable_scope('branch_0'):
                branch_0 = slim.conv2d(net, 384, [3, 3],stride=2,padding='VALID',scope='conv2d_1a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,64,[1,1],scope='conv2d_0a_1x1')
                branch_1=slim.conv2d(branch_1,96,[3,3],scope='conv2d_0b_3x3')
                branch_1=slim.conv2d(branch_1, 96, [3, 3], stride=2,padding='VALID',scope='conv2d_1a_3x3')
            with tf.variable_scope('branch_2'):
                branch_2=slim.max_pool2d(net,[3,3],stride=2,padding='VALID',scope='maxpool_1a_3x3')
            net=tf.concat([branch_0, branch_1, branch_2], 3)

        with tf.variable_scope('mixed_6b'):
            with tf.variable_scope('branch_0'):
                branch_0 = slim.conv2d(net, 192, [1, 1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,128,[1,1],scope='conv2d_0a_1x1')
                branch_1=slim.conv2d(branch_1,128,[1,7],scope='conv2d_0b_1x7')
                branch_1=slim.conv2d(branch_1,192, [7, 1],scope='conv2d_1a_7x1')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,128,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,128,[7,1],scope='conv2d_oc_7x1')
                branch_2=slim.conv2d(branch_2,128,[1,7],scope='conv2d_0c_1x7')
                branch_2=slim.conv2d(branch_2,128,[7,1],scope='conv2d_od_7x1')
                branch_2=slim.conv2d(branch_2,192,[1,7],scope='conv2d_0e_1x7')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,192,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0, branch_1, branch_2,branch_3], 3)

        with tf.variable_scope('mixed_6c'):
            with tf.variable_scope('branch_0'):
                branch_0 = slim.conv2d(net, 192, [1, 1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,160,[1,1],scope='conv2d_0a_1x1')
                branch_1=slim.conv2d(branch_1,160,[1,7],scope='conv2d_0b_1x7')
                branch_1=slim.conv2d(branch_1,192, [7, 1],scope='conv2d_1a_7x1')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,160,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,160,[7,1],scope='conv2d_oc_7x1')
                branch_2=slim.conv2d(branch_2,160,[1,7],scope='conv2d_0c_1x7')
                branch_2=slim.conv2d(branch_2,160,[7,1],scope='conv2d_od_7x1')
                branch_2=slim.conv2d(branch_2,192,[1,7],scope='conv2d_0e_1x7')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,192,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0, branch_1, branch_2,branch_3], 3)

        with tf.variable_scope('mixed_6d'):
            with tf.variable_scope('branch_0'):
                branch_0 = slim.conv2d(net, 192, [1, 1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,160,[1,1],scope='conv2d_0a_1x1')
                branch_1=slim.conv2d(branch_1,160,[1,7],scope='conv2d_0b_1x7')
                branch_1=slim.conv2d(branch_1,192, [7, 1],scope='conv2d_1a_7x1')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,160,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,160,[7,1],scope='conv2d_oc_7x1')
                branch_2=slim.conv2d(branch_2,160,[1,7],scope='conv2d_0c_1x7')
                branch_2=slim.conv2d(branch_2,160,[7,1],scope='conv2d_od_7x1')
                branch_2=slim.conv2d(branch_2,192,[1,7],scope='conv2d_0e_1x7')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,192,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0, branch_1, branch_2,branch_3], 3)

        with tf.variable_scope('mixed_6e'):
            with tf.variable_scope('branch_0'):
                branch_0 = slim.conv2d(net, 192, [1, 1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,160,[1,1],scope='conv2d_0a_1x1')
                branch_1=slim.conv2d(branch_1,160,[1,7],scope='conv2d_0b_1x7')
                branch_1=slim.conv2d(branch_1,192, [7, 1],scope='conv2d_1a_7x1')
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,160,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,160,[7,1],scope='conv2d_oc_7x1')
                branch_2=slim.conv2d(branch_2,160,[1,7],scope='conv2d_0c_1x7')
                branch_2=slim.conv2d(branch_2,160,[7,1],scope='conv2d_od_7x1')
                branch_2=slim.conv2d(branch_2,192,[1,7],scope='conv2d_0e_1x7')
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,192,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0, branch_1, branch_2,branch_3], 3)
        end_points['mixed_6e']=net

        with tf.variable_scope('mixed_7a'):
            with tf.variable_scope('branch_0'):
                branch_0=slim.conv2d(net,192,[1,1],scope='conv2d_0a_1x1')
                branch_0=slim.conv2d(branch_0,320,[3,3],stride=2,padding='VALID',scope='conv2d_1a_3x3')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,192,[1,1],scope='conv2d_0a_1x1')
                branch_1=slim.conv2d(branch_1,192,[1,7],scope='conv2d_0b_1x7')
                branch_1=slim.conv2d(branch_1, 192, [7, 1], scope='conv2d_0c_7x1')
                branch_1=slim.conv2d(branch_1, 192, [3, 3],stride=2,padding='VALID',scope='conv2d_1a_3x3')
            with tf.variable_scope('branch_2'):
                branch_2=slim.max_pool2d(net,[3,3],stride=2,padding='VALID',scope='maxpool_1a_3x3')
            net=tf.concat([branch_0,branch_1,branch_2],3)

        with tf.variable_scope('mixed_7b'):
            with tf.variable_scope('branch_0'):
                branch_0=slim.conv2d(net,320,[1,1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,384,[1,1],scope='conv2d_0a_1x1')
                branch_1=tf.concat([
                    slim.conv2d(branch_1,384,[1,3],scope='conv2d_0b_1x3'),
                    slim.conv2d(branch_1, 384, [3, 1], scope='conv2d_0b_3x1')],3)
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,448,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,384,[3,3],scope='conv2d_0b_3x3')
                branch_2=tf.concat([
                    slim.conv2d(branch_2, 384, [1, 3], scope='conv2d_0c_1x3'),
                    slim.conv2d(branch_2, 384, [3, 1], scope='conv2d_0d_3x1')], 3)
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,192,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0,branch_1,branch_2,branch_3],3)

        with tf.variable_scope('mixed_7c'):
            with tf.variable_scope('branch_0'):
                branch_0=slim.conv2d(net,320,[1,1],scope='conv2d_0a_1x1')
            with tf.variable_scope('branch_1'):
                branch_1=slim.conv2d(net,384,[1,1],scope='conv2d_0a_1x1')
                branch_1=tf.concat([
                    slim.conv2d(branch_1,384,[1,3],scope='conv2d_0b_1x3'),
                    slim.conv2d(branch_1, 384, [3, 1], scope='conv2d_0b_3x1')],3)
            with tf.variable_scope('branch_2'):
                branch_2=slim.conv2d(net,448,[1,1],scope='conv2d_0a_1x1')
                branch_2=slim.conv2d(branch_2,384,[3,3],scope='conv2d_0b_3x3')
                branch_2=tf.concat([
                    slim.conv2d(branch_2, 384, [1, 3], scope='conv2d_0c_1x3'),
                    slim.conv2d(branch_2, 384, [3, 1], scope='conv2d_0d_3x1')], 3)
            with tf.variable_scope('branch_3'):
                branch_3=slim.avg_pool2d(net,[3,3],scope='avgpool_0a_3x3')
                branch_3=slim.conv2d(branch_3,192,[1,1],scope='conv2d_0b_1x1')
            net=tf.concat([branch_0,branch_1,branch_2,branch_3],3)
    return net,end_points
Пример #10
0
def mobilenet(inputs,
              num_classes=1000,
              is_training=True,
              width_multiplier=1,
              scope='MobileNet'):
    """ MobileNet
  More detail, please refer to Google's paper(https://arxiv.org/abs/1704.04861).

  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.
    scope: Optional scope for the variables.
  Returns:
    logits: the pre-softmax activations, a tensor of size
      [batch_size, `num_classes`]
    end_points: a dictionary from components of the network to the corresponding
      activation.
  """
    def _depthwise_separable_conv(inputs,
                                  num_pwc_filters,
                                  width_multiplier,
                                  sc,
                                  downsample=False):
        """ Helper function to build the depth-wise separable convolution layer.
    """
        num_pwc_filters = round(num_pwc_filters * width_multiplier)
        _stride = 2 if downsample else 1

        # skip pointwise by setting num_outputs=None
        depthwise_conv = slim.separable_convolution2d(inputs,
                                                      num_outputs=None,
                                                      stride=_stride,
                                                      depth_multiplier=1,
                                                      kernel_size=[3, 3],
                                                      scope=sc +
                                                      '/depthwise_conv')

        bn = slim.batch_norm(depthwise_conv, scope=sc + '/dw_batch_norm')
        pointwise_conv = slim.convolution2d(bn,
                                            num_pwc_filters,
                                            kernel_size=[1, 1],
                                            scope=sc + '/pointwise_conv')
        bn = slim.batch_norm(pointwise_conv, scope=sc + '/pw_batch_norm')
        return bn

    with tf.variable_scope(scope) as sc:
        end_points_collection = sc.name + '_end_points'
        with slim.arg_scope([slim.convolution2d, slim.separable_convolution2d],
                            activation_fn=None,
                            outputs_collections=[end_points_collection]):
            with slim.arg_scope([slim.batch_norm],
                                is_training=is_training,
                                activation_fn=tf.nn.relu,
                                fused=True):
                net = slim.convolution2d(inputs,
                                         round(32 * width_multiplier), [3, 3],
                                         stride=2,
                                         padding='SAME',
                                         scope='conv_1')
                net = slim.batch_norm(net, scope='conv_1/batch_norm')
                net = _depthwise_separable_conv(net,
                                                64,
                                                width_multiplier,
                                                sc='conv_ds_2')
                net = _depthwise_separable_conv(net,
                                                128,
                                                width_multiplier,
                                                downsample=True,
                                                sc='conv_ds_3')
                net = _depthwise_separable_conv(net,
                                                128,
                                                width_multiplier,
                                                sc='conv_ds_4')
                net = _depthwise_separable_conv(net,
                                                256,
                                                width_multiplier,
                                                downsample=True,
                                                sc='conv_ds_5')
                net = _depthwise_separable_conv(net,
                                                256,
                                                width_multiplier,
                                                sc='conv_ds_6')
                net = _depthwise_separable_conv(net,
                                                512,
                                                width_multiplier,
                                                downsample=True,
                                                sc='conv_ds_7')

                net = _depthwise_separable_conv(net,
                                                512,
                                                width_multiplier,
                                                sc='conv_ds_8')
                net = _depthwise_separable_conv(net,
                                                512,
                                                width_multiplier,
                                                sc='conv_ds_9')
                net = _depthwise_separable_conv(net,
                                                512,
                                                width_multiplier,
                                                sc='conv_ds_10')
                net = _depthwise_separable_conv(net,
                                                512,
                                                width_multiplier,
                                                sc='conv_ds_11')
                net = _depthwise_separable_conv(net,
                                                512,
                                                width_multiplier,
                                                sc='conv_ds_12')

                net = _depthwise_separable_conv(net,
                                                1024,
                                                width_multiplier,
                                                downsample=True,
                                                sc='conv_ds_13')
                net = _depthwise_separable_conv(net,
                                                1024,
                                                width_multiplier,
                                                sc='conv_ds_14')
                net = slim.avg_pool2d(net, [7, 7], scope='avg_pool_15')

        end_points = slim.utils.convert_collection_to_dict(
            end_points_collection)
        net = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
        end_points['squeeze'] = net
        logits = slim.fully_connected(net,
                                      num_classes,
                                      activation_fn=None,
                                      scope='fc_16')
        predictions = slim.softmax(logits, scope='Predictions')

        end_points['Logits'] = logits
        end_points['Predictions'] = predictions

    return logits, end_points
Пример #11
0
def create_ds_cnn_model(fingerprint_input, model_settings, model_size_info, 
                          is_training):
  """Builds a model with depthwise separable convolutional neural network
  Model definition is based on https://arxiv.org/abs/1704.04861 and
  Tensorflow implementation: https://github.com/Zehaos/MobileNet

  model_size_info: defines number of layers, followed by the DS-Conv layer
    parameters in the order {number of conv features, conv filter height, 
    width and stride in y,x dir.} for each of the layers. 
  Note that first layer is always regular convolution, but the remaining 
    layers are all depthwise separable convolutions.
  """

  def ds_cnn_arg_scope(weight_decay=0):
    """Defines the default ds_cnn argument scope.
    Args:
      weight_decay: The weight decay to use for regularizing the model.
    Returns:
      An `arg_scope` to use for the DS-CNN model.
    """
    with slim.arg_scope(
        [slim.convolution2d, slim.separable_convolution2d],
        weights_initializer=slim.initializers.xavier_initializer(),
        biases_initializer=slim.init_ops.zeros_initializer(),
        weights_regularizer=slim.l2_regularizer(weight_decay)) as sc:
      return sc

  def _depthwise_separable_conv(inputs,
                                num_pwc_filters,
                                sc,
                                kernel_size,
                                stride):
    """ Helper function to build the depth-wise separable convolution layer.
    """

    # skip pointwise by setting num_outputs=None
    depthwise_conv = slim.separable_convolution2d(inputs,
                                                  num_outputs=None,
                                                  stride=stride,
                                                  depth_multiplier=1,
                                                  kernel_size=kernel_size,
                                                  scope=sc+'/depthwise_conv')

    bn = slim.batch_norm(depthwise_conv, scope=sc+'/dw_batch_norm')
    pointwise_conv = slim.convolution2d(bn,
                                        num_pwc_filters,
                                        kernel_size=[1, 1],
                                        scope=sc+'/pointwise_conv')
    bn = slim.batch_norm(pointwise_conv, scope=sc+'/pw_batch_norm')
    return bn


  if is_training:
    dropout_prob = tf.placeholder(tf.float32, name='dropout_prob')

  label_count = model_settings['label_count']
  input_frequency_size = model_settings['dct_coefficient_count']
  input_time_size = model_settings['spectrogram_length']
  fingerprint_4d = tf.reshape(fingerprint_input,
                              [-1, input_time_size, input_frequency_size, 1])
 
  t_dim = input_time_size
  f_dim = input_frequency_size

  #Extract model dimensions from model_size_info
  num_layers = model_size_info[0]
  conv_feat = [None]*num_layers
  conv_kt = [None]*num_layers
  conv_kf = [None]*num_layers
  conv_st = [None]*num_layers
  conv_sf = [None]*num_layers
  i=1
  for layer_no in range(0,num_layers):
    conv_feat[layer_no] = model_size_info[i]
    i += 1
    conv_kt[layer_no] = model_size_info[i]
    i += 1
    conv_kf[layer_no] = model_size_info[i]
    i += 1
    conv_st[layer_no] = model_size_info[i]
    i += 1
    conv_sf[layer_no] = model_size_info[i]
    i += 1

  scope = 'DS-CNN'
  with tf.variable_scope(scope) as sc:
    end_points_collection = sc.name + '_end_points'
    with slim.arg_scope([slim.convolution2d, slim.separable_convolution2d],
                        activation_fn=None,
                        weights_initializer=slim.initializers.xavier_initializer(),
                        biases_initializer=slim.init_ops.zeros_initializer(),
                        outputs_collections=[end_points_collection]):
      with slim.arg_scope([slim.batch_norm],
                          is_training=is_training,
                          decay=0.96,
                          updates_collections=None,
                          activation_fn=tf.nn.relu):
        for layer_no in range(0,num_layers):
          if layer_no==0:
            net = slim.convolution2d(fingerprint_4d, conv_feat[layer_no],\
                      [conv_kt[layer_no], conv_kf[layer_no]], stride=[conv_st[layer_no], conv_sf[layer_no]], padding='SAME', scope='conv_1')
            net = slim.batch_norm(net, scope='conv_1/batch_norm')
          else:
            net = _depthwise_separable_conv(net, conv_feat[layer_no], \
                      kernel_size = [conv_kt[layer_no],conv_kf[layer_no]], \
                      stride = [conv_st[layer_no],conv_sf[layer_no]], sc='conv_ds_'+str(layer_no))
          t_dim = math.ceil(t_dim/float(conv_st[layer_no]))
          f_dim = math.ceil(f_dim/float(conv_sf[layer_no]))

        net = slim.avg_pool2d(net, [t_dim, f_dim], scope='avg_pool')

    net = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
    logits = slim.fully_connected(net, label_count, activation_fn=None, scope='fc1')

  if is_training:
    return logits, dropout_prob
  else:
    return logits
Пример #12
0
def tower(inputs,
          is_training,
          dropout_probability,
          input_noise,
          normalize_input,
          flip_horizontally,
          translate,
          num_logits,
          is_initialization=False,
          name=None):
    with tf.name_scope(name, "tower"):
        default_conv_args = dict(
            padding='SAME',
            kernel_size=[3, 3],
            activation_fn=nn.lrelu,
            init=is_initialization
        )
        training_mode_funcs = [
            nn.random_translate, nn.flip_randomly, nn.gaussian_noise, slim.dropout,
            wn.fully_connected, wn.conv2d
        ]
        training_args = dict(
            is_training=is_training
        )

        with \
        slim.arg_scope([wn.conv2d], **default_conv_args), \
        slim.arg_scope(training_mode_funcs, **training_args):
            #pylint: disable=no-value-for-parameter
            net = inputs
            assert_shape(net, [None, 32, 32, 3])

            net = tf.cond(normalize_input,
                          lambda: slim.layer_norm(net,
                                                  scale=False,
                                                  center=False,
                                                  scope='normalize_inputs'),
                          lambda: net)
            assert_shape(net, [None, 32, 32, 3])

            net = nn.flip_randomly(net,
                                   horizontally=flip_horizontally,
                                   vertically=False,
                                   name='random_flip')
            net = tf.cond(translate,
                          lambda: nn.random_translate(net, scale=2, name='random_translate'),
                          lambda: net)
            net = nn.gaussian_noise(net, scale=input_noise, name='gaussian_noise')

            net = wn.conv2d(net, 128, scope="conv_1_1")
            net = wn.conv2d(net, 128, scope="conv_1_2")
            net = wn.conv2d(net, 128, scope="conv_1_3")
            net = slim.max_pool2d(net, [2, 2], scope='max_pool_1')
            net = slim.dropout(net, 1 - dropout_probability, scope='dropout_probability_1')
            assert_shape(net, [None, 16, 16, 128])

            net = wn.conv2d(net, 256, scope="conv_2_1")
            net = wn.conv2d(net, 256, scope="conv_2_2")
            net = wn.conv2d(net, 256, scope="conv_2_3")
            net = slim.max_pool2d(net, [2, 2], scope='max_pool_2')
            net = slim.dropout(net, 1 - dropout_probability, scope='dropout_probability_2')
            assert_shape(net, [None, 8, 8, 256])

            net = wn.conv2d(net, 512, padding='VALID', scope="conv_3_1")
            assert_shape(net, [None, 6, 6, 512])
            net = wn.conv2d(net, 256, kernel_size=[1, 1], scope="conv_3_2")
            net = wn.conv2d(net, 128, kernel_size=[1, 1], scope="conv_3_3")
            net = slim.avg_pool2d(net, [6, 6], scope='avg_pool')
            assert_shape(net, [None, 1, 1, 128])

            net = slim.flatten(net)
            assert_shape(net, [None, 128])

            primary_logits = wn.fully_connected(net, 10, init=is_initialization)
            secondary_logits = wn.fully_connected(net, 10, init=is_initialization)

            with tf.control_dependencies([tf.assert_greater_equal(num_logits, 1),
                                          tf.assert_less_equal(num_logits, 2)]):
                secondary_logits = tf.case([
                    (tf.equal(num_logits, 1), lambda: primary_logits),
                    (tf.equal(num_logits, 2), lambda: secondary_logits),
                ], exclusive=True, default=lambda: primary_logits)

            assert_shape(primary_logits, [None, 10])
            assert_shape(secondary_logits, [None, 10])
            return primary_logits, secondary_logits
Пример #13
0
def inception_v3_base(inputs=None,
                      final_endpoint='Mixed_7c',
                      min_depth=16,
                      depth_multiplier=1.0,
                      scope=None):
    """Inception model from http://arxiv.org/abs/1512.00567.

    Constructs an Inception V3 network from inputs to the given final endpoint.
    This method can construct the network up to the final inception block
    Mixed_7c.

    Note the names of the layers in the paper do not correspond to the names
    of endpoints registered by this function although they build the same
    network.

    Here is a mapping from the old_names to the new names:
    Old name          | New name
    =======================================
    conv0             | Conv2d_1a_3x3  stride=2
    conv1             | Conv2d_2a_3x3
    conv2             | Conv2d_2b_3x3
    pool1             | MaxPool_3a_3x3 stride=2
    conv3             | conv2d_3b_3x3
    conv4             | conv2d_4a_3x3
    pool2             | MaxPool_5a_3x3 stride=2
    mixed_35x35x256a  | Mixed_5b
    mixed_35x35x288a  | Mixed_5c
    mixed_35x35x288b  | Mixed_5d
    mixed_17x17x768a  | Mixed_6a  stride=2
    mixed_17x17x768a  | Mixed_6b
    mixed_17x17x768b  | Mixed_6c
    mixed_17x17x768a  | Mixed_6d
    mixed_17x17x768b  | Mixed_6e
    mixed_8x8x1280a   | Mixed_7a  stride=2
    mixed_8x8x2048a   | Mixed_7b
    mixed_8x8x2048b   | Mixed_7c

    Args:
        inputs: a tensor of size [batch_size, height, width, channels].
            final_endpoint: specifies the endpoint to construct the network up to.
        min_depth: Minimum depth value (number of channels) for all convolution ops.
            Enforced when depth_multiplier < 1,and not an active contraint when
            depth_multiplier >= 1
        depth_multiplier: Float multiplier for the depth (number of channels) for all
            convolution ops. The value must greater than zero. Typical usage will be to
            set this value in (0, 1) to reduce the number of parameters or computation
            cost of the model.
        scope: Optional variable_scope

    Returns:
        tensor_out: output tensor corresponding to the final_endpoint.
        end_points: a set of activations for external use, for example summaries or
                    losses.

    Raises:
        ValueError: if final_endpoint is not set to one of the predifined values,
                    or depth_multiplier <= 0
        """

    end_points = {}

    if depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')
    depth = lambda d: max(int(d * depth_multiplier), min_depth)

    with tf.variable_scope(scope, 'InceptionV3', [inputs]):
        with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                            stride=1, padding='VALID'):
            # 299 x 299 x 3
            end_point='Conv2d_1a_3x3'
            net = slim.conv2d(inputs, depth(32), [3, 3], stride=2, scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 149 x 149 x 32
            end_point='Conv2d_2a_3x3'
            net = slim.conv2d(net, depth(32), [3, 3], scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 147 x 147 x 32
            end_point = 'Conv2d_2b_3x3'
            net = slim.conv2d(net, depth(64), [3, 3], padding='SAME', scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 147 x 147 x 64
            end_point='MaxPool_3a_3x3'
            net = slim.max_pool2d(net, [3, 3], stride=2, scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 73 x 73 x 64
            end_point = 'Conv2d_3b_1x1'
            net = slim.conv2d(net, depth(80), [1, 1], scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 73 x 73 x 80
            end_point = 'Conv2d_4a_3x3'
            net = slim.conv2d(net, depth(192), [3, 3], scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 71 x 71 x 192
            end_point = 'MaxPool_5a_3x3'
            net = slim.max_pool2d(net, [3, 3],stride=2, scope=end_point)
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # 35 x 35 x 192

        # Inception block
        with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                            stride=1, padding='SAME'):
            # mixed: 35 x 35 x 256
            end_point = 'Mixed_5b'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(64), [5, 5],
                                           scope='Conv2d_0b_5x5')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
                                           scope='Conv2d_0b_3x3')
                    branch_2 = slim.conv2d(branch_2, depth(96), [3, 3],
                                           scope='Conv2d_0c_3x3')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(32), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_1: 35 x 35 x 288
            end_point = 'Mixed_5c'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0b_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(64), [5, 5], scope='Conv_1_0c_5x5')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
                    branch_2 = slim.conv2d(branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(64), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_2: 35 x 35 x 288
            end_point = 'Mixed_5d'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(48), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(64), [5, 5], scope='Conv2d_0b_5x5')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(96), [3, 3], scope='Conv2d_0b_3x3')
                    branch_2 = slim.conv2d(branch_2, depth(96), [3, 3], scope='Conv2d_0c_3x3')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(64), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points

            # mixed_3: 17 x 17 x 768
            end_point = 'Mixed_6a'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(384), [3, 3], stride=2,
                                           padding='VALID', scope='Conv2d_1a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(64), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(96), [3, 3], scope='Conv2d_0b_3x3')
                    branch_1 = slim.conv2d(branch_1, depth(96), [3, 3], stride=2,
                                           padding='VALID', scope='Conv2d_1a_1x1')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.max_pool2d(net, [3, 3], stride=2,
                                               padding='VALID', scope='MaxPool_1a_3x3')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points

            # mixed_4: 17 x 17 x 768
            end_point = 'Mixed_6b'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(128), [1, 7], scope='Conv2d_0b_1x7')
                    branch_1 = slim.conv2d(branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(128), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(128), [7, 1], scope='Conv2d_0b_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(128), [1, 7], scope='Conv2d_0c_1x7')
                    branch_2 = slim.conv2d(branch_2, depth(128), [7, 1], scope='Conv2d_0d_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool2d_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_5: 17 x 17 x 768
            end_point = 'Mixed_6c'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(160), [1, 7], scope='Conv2d_0b_1x7')
                    branch_1 = slim.conv2d(branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(160), [7, 1], scope='Conv2d_0b_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(160), [1, 7], scope='Conv2d_0c_1x7')
                    branch_2 = slim.conv2d(branch_2, depth(160), [7, 1], scope='Conv2d_0d_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool2d_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_6: 17 x 17 x 768
            end_point = 'Mixed_6d'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(160), [1, 7], scope='Conv2d_0b_1x7')
                    branch_1 = slim.conv2d(branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(160), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(160), [7, 1], scope='Conv2d_0b_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(160), [1, 7], scope='Conv2d_0c_1x7')
                    branch_2 = slim.conv2d(branch_2, depth(160), [7, 1], scope='Conv2d_0d_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool2d_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_7: 17 x 17 x 768
            end_point = 'Mixed_6e'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(192), [1, 7], scope='Conv2d_0b_1x7')
                    branch_1 = slim.conv2d(branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(192), [7, 1], scope='Conv2d_0b_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(192), [1, 7], scope='Conv2d_0c_1x7')
                    branch_2 = slim.conv2d(branch_2, depth(192), [7, 1], scope='Conv2d_0d_7x1')
                    branch_2 = slim.conv2d(branch_2, depth(192), [1, 7], scope='Conv2d_0e_1x7')
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool2d_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_8: 8 x 8 x 1280
            end_point = 'Mixed_7a'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                    branch_0 = slim.conv2d(branch_0, depth(320), [3, 3],
                                           stride = 2, padding = 'VALID', scope='Conv2d_1a_3x3')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(192), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = slim.conv2d(branch_1, depth(192), [1, 7], scope='Conv2d_0b_1x7')
                    branch_1 = slim.conv2d(branch_1, depth(192), [7, 1], scope='Conv2d_0c_7x1')
                    branch_1 = slim.conv2d(branch_1, depth(192), [3, 3],
                                           stride=2, padding='VALID', scope='Conv2d_1a_3x3')
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.avg_pool2d(net, [3, 3],
                                               stride=2, padding='VALID', scope='MaxPool_1a_3x3')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_9: 8 x 8 x 2048
            end_point = 'Mixed_7b'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = tf.concat(axis=3, values=[
                        slim.conv2d(branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
                        slim.conv2d(branch_1, depth(384), [3, 1], scope='Conv2d_0b_3x1')])
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
                    branch_2 = tf.concat(axis=3, values=[
                        slim.conv2d(branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
                        slim.conv2d(branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')])
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
            # mixed_9: 8 x 8 x 2048
            end_point = 'Mixed_7c'
            with tf.variable_scope(end_point):
                with tf.variable_scope('Branch_0'):
                    branch_0 = slim.conv2d(net, depth(320), [1, 1], scope='Conv2d_0a_1x1')
                with tf.variable_scope('Branch_1'):
                    branch_1 = slim.conv2d(net, depth(384), [1, 1], scope='Conv2d_0a_1x1')
                    branch_1 = tf.concat(axis=3, values=[
                        slim.conv2d(branch_1, depth(384), [1, 3], scope='Conv2d_0b_1x3'),
                        slim.conv2d(branch_1, depth(384), [3, 1], scope='Conv2d_0c_3x1')])
                with tf.variable_scope('Branch_2'):
                    branch_2 = slim.conv2d(net, depth(448), [1, 1], scope='Conv2d_0a_1x1')
                    branch_2 = slim.conv2d(branch_2, depth(384), [3, 3], scope='Conv2d_0b_3x3')
                    branch_2 = tf.concat(axis=3, values=[
                        slim.conv2d(branch_2, depth(384), [1, 3], scope='Conv2d_0c_1x3'),
                        slim.conv2d(branch_2, depth(384), [3, 1], scope='Conv2d_0d_3x1')])
                with tf.variable_scope('Branch_3'):
                    branch_3 = slim.avg_pool2d(net, [3, 3], scope='AvgPool_0a_3x3')
                    branch_3 = slim.conv2d(branch_3, depth(192), [1, 1], scope='Conv2d_0b_1x1')
                net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])
            end_points[end_point] = net
            if final_endpoint == end_point: return net, end_points
        raise ValueError('finnal_endpoint: %s, Unknown final endpoint %s' % (final_endpoint, end_point))
Пример #14
0
def inception_v3(inputs,
                 num_classes=1000,
                 is_training=True,
                 dropout_keep_prob=0.8,
                 min_depth=16,
                 depth_multiplier=1.0,
                 prediction_fn=slim.softmax,
                 spatial_squeeze=True,
                 reuse=None,
                 create_aux_logits=True,
                 scope='InceptionV3',
                 global_pool=False):
    """Inception model from http://arxiv.org/abs/1512.00567.

    "Rethinking the Inception Architecture for Computer Vision"

    Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens,
    Zbigniew Wojna.

    with the default arguments this method constructs the exact model defined in
    the paper. However, one can experiment with variations of the inception_v3
    network by changing arguments dropout_keep_prob, min_depth, and
    depth_multiplier.

    The default image size used to train this network is 299x299.

    Args:
        inputs: a tensor of size [batch_size, height, width, channels].
        num_classes: number of predicted classes. If 0 or None, the logits layer
            is omitted and the input features to the logits layer (before dropout)
            are returned instead.
        is_training: Whether is training or not.
        dropout_keep_prob: the percentage of activation values that are retained.
        min_depth: Minimum depth value (number of channels) for all convolution ops.
            Enforced when depth_multiplier < 1 ,and not an active constraint when
            depth_multiplier >= 1.
        depth_multiplier: Float multiplier for the depth (number of channels)
            for all convolution ops. The value must be greater than zero. Typical
            usage will be to set this value in (0, 1) to reduce the number of
            parameters or computation cost of the model.
        prediction_fn: a function to get predictions out of logits.
        spatial_squeeze: if True, logits is of shape [B, C], if False logits is of
            shape [B, 1, 1, C]
    """
    if depth_multiplier <= 0:
        raise ValueError('depth_multiplier is not greater than zero.')
    depth = lambda d: max(d * depth_multiplier, min_depth)

    with tf.variable_scope(name_or_scope=scope, values=[inputs], reuse=reuse) as scope:
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
            net, end_points = inception_v3_base(
                inputs, depth_multiplier=depth_multiplier,
                min_depth=min_depth,
                scope = scope)

            # Auxiliary Head logits
            if create_aux_logits and num_classes:
                with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                                    stride=1, padding='SAME'):
                    aux_logits = end_points['Mixed_6e']
                    with tf.variable_scope('AuxLogits'):
                        aux_logits = slim.avg_pool2d(
                            aux_logits, [5, 5], stride=3, padding='VALID',
                            scope='AvgPool_1a_5x5')
                        aux_logits = slim.conv2d(aux_logits, depth(128), [1, 1],
                                                 scope='Conv2d_1b_1x1')

                        # Shape of feature map before the final layer.
                        kernel_size = _reduced_kernel_size_for_small_input(
                            aux_logits, [5, 5])
                        # 1 x 1 x 768
                        aux_logits = slim.conv2d(
                            aux_logits, depth(768), kernel_size,
                            weights_initializer=trunc_normal(0.01),
                            padding='VALID', scope='Conv2d_2a_{}x{}'.format(*kernel_size))
                        # 1 x 1 x num_classes
                        aux_logits = slim.conv2d(
                            aux_logits, num_classes, [1, 1], activation_fn=None,
                            normalizer_fn=None, weights_initializer=trunc_normal(0.001),
                            scope='Conv2d_2b_1x1')
                        if spatial_squeeze:
                            aux_logits = tf.squeeze(aux_logits, [1, 2], name='SpatialSqueeze')
                        end_points['AuxLogits'] = aux_logits

            # Final pooling and prediction
            with tf.variable_scope('Logits'):
                if global_pool:
                    # Global Average Pooling.
                    net = tf.reduce_mean(net, [1, 2], keep_dims=True, name='GlobalPool')
                    end_points['global_pool'] = net
                else:
                    # Pooling with a fixed kernel size.
                    kernel_size = _reduced_kernel_size_for_small_input(net, [8, 8])
                    net = slim.avg_pool2d(net, kernel_size, padding='VALID',
                                          scope='AvgPool_1a_{}x{}'.format(*kernel_size))
                    end_points['AvgPool_1a'] = net
                if not num_classes:
                    return net, end_points
                # 1 x 1 x 2048
                net = slim.dropout(net, keep_prob=dropout_keep_prob, scope='Dropout_1b')
                end_points['PreLogits'] = net
                # 2048
                logits = slim.conv2d(net, num_classes, [1, 1], activation_fn = None,
                                  normalizer_fn=None, scope='conv2d_1c_1x1')
                if spatial_squeeze:
                    logits = tf.squeeze(logits, [1, 2], name='SpatialSqueeze')
                end_points['Logits'] = logits
            end_points['Predictions'] = prediction_fn(logits, 'Predictions')
        return logits, end_points
Пример #15
0
def L_O_Net(inputs,
            label=None,
            bbox_target=None,
            animoji_target=None,
            training=True):
    # batch_norm_params = {
    # 'decay': 0.995,
    # 'epsilon': 0.001,
    # 'updates_collections': None,
    # 'variables_collections': [ tf.GraphKeys.TRAINABLE_VARIABLES ],
    # }
    # with slim.arg_scope([slim.conv2d, slim.fully_connected],
    # activation_fn = prelu,
    # weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
    # biases_initializer=tf.zeros_initializer(),
    # weights_regularizer=slim.l2_regularizer(0.0005),
    # normalizer_fn=slim.batch_norm,
    # normalizer_params=batch_norm_params
    # ):

    with slim.arg_scope([slim.conv2d],
                        activation_fn=prelu,
                        weights_initializer=slim.xavier_initializer(),
                        biases_initializer=tf.zeros_initializer(),
                        weights_regularizer=slim.l2_regularizer(0.0005),
                        padding='valid'):
        print('L_O_Net network shape')
        print(inputs.get_shape())
        net = slim.conv2d(inputs,
                          num_outputs=32,
                          kernel_size=[3, 3],
                          stride=1,
                          scope='conv1',
                          padding='valid')
        print(net.get_shape())
        net = slim.max_pool2d(net,
                              kernel_size=[2, 2],
                              stride=2,
                              scope='pool1',
                              padding='SAME')
        print(net.get_shape())
        net = slim.conv2d(net,
                          num_outputs=64,
                          kernel_size=[3, 3],
                          stride=1,
                          scope='conv2',
                          padding='valid')
        print(net.get_shape())
        net = slim.max_pool2d(net,
                              kernel_size=[2, 2],
                              stride=2,
                              scope='pool2',
                              padding='valid')
        print(net.get_shape())
        net = slim.conv2d(net,
                          num_outputs=64,
                          kernel_size=[3, 3],
                          stride=1,
                          scope='conv3',
                          padding='valid')
        print(net.get_shape())
        net = slim.max_pool2d(net,
                              kernel_size=[2, 2],
                              stride=2,
                              scope='pool3',
                              padding='SAME')
        print(net.get_shape())
        net = slim.conv2d(net,
                          num_outputs=128,
                          kernel_size=[3, 3],
                          stride=1,
                          scope='conv4',
                          padding='valid')
        print(net.get_shape())
        net = slim.avg_pool2d(net,
                              kernel_size=[2, 2],
                              stride=1,
                              scope='pool4',
                              padding='valid')
        print(net.get_shape())

        #################
        ## mobilenet_v2##
        #################
        # exp = 6  # expansion ratio

        # net = conv2d_block(inputs, 32, 3, 2, training, name='conv1_1')  # size/2
        # print(net.get_shape())
        # net = res_block(net, 1, 16, 1, training, name='res2_1')
        # print(net.get_shape())
        # net = res_block(net, exp, 24, 2, training, name='res3_1')  # size/4
        # net = res_block(net, exp, 24, 1, training, name='res3_2')
        # print(net.get_shape())
        # net = res_block(net, exp, 32, 2, training, name='res4_1')  # size/8
        # net = res_block(net, exp, 32, 1, training, name='res4_2')
        # net = res_block(net, exp, 32, 1, training, name='res4_3')
        # print(net.get_shape())
        # net = res_block(net, exp, 64, 1, training, name='res5_1')
        # net = res_block(net, exp, 64, 1, training, name='res5_2')
        # net = res_block(net, exp, 64, 1, training, name='res5_3')
        # net = res_block(net, exp, 64, 1, training, name='res5_4')
        # print(net.get_shape())
        # net = res_block(net, exp, 96, 2, training, name='res6_1')  # size/16
        # net = res_block(net, exp, 96, 1, training, name='res6_2')
        # net = res_block(net, exp, 96, 1, training, name='res6_3')
        # print(net.get_shape())
        # net = res_block(net, exp, 160, 2, training, name='res7_1')  # size/32
        # net = res_block(net, exp, 160, 1, training, name='res7_2')
        # net = res_block(net, exp, 160, 1, training, name='res7_3')
        # print(net.get_shape())
        # net = res_block(net, exp, 320, 1, training, name='res8_1', shortcut=False)
        # print(net.get_shape())
        # net = pwise_block(net, 1280, training, name='conv9_1')
        # net = global_avg(net)
        # print(net.get_shape())
        # fc_flatten = flatten(conv_1x1(net,96, name='fc_flatten'))
        # print(fc_flatten.get_shape())

        net = tf.transpose(net, perm=[0, 3, 1, 2])
        print(net.get_shape())
        fc_flatten = slim.flatten(net)
        print(fc_flatten.get_shape())
        fc1 = slim.fully_connected(fc_flatten,
                                   num_outputs=256,
                                   scope='fc1',
                                   activation_fn=prelu)
        print(fc1.get_shape())

        cls_prob = slim.fully_connected(fc1,
                                        num_outputs=2,
                                        scope='cls_fc',
                                        activation_fn=tf.nn.softmax)
        print(cls_prob.get_shape())

        bbox_pred = slim.fully_connected(fc1,
                                         num_outputs=4,
                                         scope='bbox_fc',
                                         activation_fn=None)
        print(bbox_pred.get_shape())

        # landmark_pred = slim.fully_connected(fc1,num_outputs=10,scope='landmark_fc',activation_fn=None)
        # print(landmark_pred.get_shape())

        animoji_pred = slim.fully_connected(fc1,
                                            num_outputs=140,
                                            scope='animoji_fc',
                                            activation_fn=None)
        print(animoji_pred.get_shape())

        if training:
            cls_loss = cls_ohem(cls_prob, label)
            bbox_loss = bbox_ohem(bbox_pred, bbox_target, label)
            accuracy, recall = cal_accuracy(cls_prob, label)
            # landmark_loss = landmark_ohem(landmark_pred, landmark_target,label)
            animoji_loss = animoji_ohem(animoji_pred, animoji_target, label)
            print(tf.losses.get_regularization_losses())
            L2_loss = tf.add_n(tf.losses.get_regularization_losses())
            # return cls_loss,bbox_loss,landmark_loss,animoji_loss,L2_loss,accuracy, recall
            return cls_loss, bbox_loss, animoji_loss, L2_loss, accuracy, recall
        else:
            # return cls_prob,bbox_pred,landmark_pred,animoji_pred
            return cls_prob, bbox_pred, animoji_pred
    def build_network(self):

        print "Building Network"
        with tf.variable_scope('train_data_queue'):
            filename_train_queue = tf.train.string_input_producer(
                [self.tf_train_record_file_path], num_epochs=self.num_epochs)
            self.train_images, self.train_labels = self.load_from_tfRecord(
                filename_train_queue)
        with tf.variable_scope('val_data_queue'):
            filename_val_queue = tf.train.string_input_producer(
                [self.tf_val_record_file_path],
                num_epochs=self.num_epochs *
                (int(self.it_per_epoch / self.perform_val_after_steps) + 1))
            self.val_images, self.val_labels = self.load_from_tfRecord(
                filename_val_queue)

        self.X = tf.placeholder(tf.float32,
                                shape=(self.batch_size, self.img_height,
                                       self.img_width, self.channels),
                                name='input')
        self.Y = tf.placeholder(tf.int32,
                                shape=(self.batch_size),
                                name='labels')
        self.keep_prob = tf.placeholder(tf.float32,
                                        shape=[],
                                        name='dropout_prob')
        self.resnet_mode_flag = tf.placeholder(tf.bool,
                                               shape=[],
                                               name='resnet_mode_flag')

        if self.use_vgg:
            print "Using VGG"
            self.net = self.VGG16(self.X, self.keep_prob)

        if self.use_resnet_50:
            print "Using Resnet 50"
            self.net = self.resnet_50(self.X, self.keep_prob,
                                      self.resnet_mode_flag)
            self.net = tf.squeeze(self.net, axis=[1, 2])

        if self.use_resnet_101:
            print "Using Resnet 101"
            self.net = self.resnet_101(self.X, self.keep_prob,
                                       self.resnet_mode_flag)
            self.net = tf.squeeze(self.net, axis=[1, 2])

        if self.use_squeezenet:
            print "Using Squeezenet"
            self.net = self.squeezenet(self.X, self.keep_prob)
            self.net = slim.dropout(self.net,
                                    self.keep_prob,
                                    scope='dropout_fire9')
            with slim.arg_scope(
                [slim.conv2d],
                    activation_fn=tf.nn.relu,
                    weights_initializer=tf.truncated_normal_initializer(
                        0.0, 0.01)):
                self.net_hm = slim.conv2d(self.net,
                                          self.num_classes, [1, 1],
                                          1,
                                          scope='out_classification')
            self.net = slim.avg_pool2d(
                self.net_hm,
                [self.net_hm.get_shape()[1],
                 self.net_hm.get_shape()[2]],
                stride=[1, 1])
            self.net = tf.squeeze(self.net, axis=[1, 2])

        if self.use_VGGCAM:
            print "Using VGGCAM"
            self.net, self.cam_conv = self.VGG16CAM(self.X)
            self.CAM = self.get_CAM(self.X,
                                    scope='vgg_cam/out_classification/weights')

        if self.use_resnet_101_CAM:
            print "Using Resnet 101 CAM"
            self.net, self.cam_conv = self.resnet_101_CAM(
                self.X, self.keep_prob, self.resnet_mode_flag)
            self.CAM = self.get_CAM(self.X, scope='out_classification/weights')

        self.cross_entropy_loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=tf.one_hot(
                self.Y, self.num_classes),
                                                    logits=self.net))

        # self.minimize_loss = tf.train.AdamOptimizer(0.0001).minimize(self.cross_entropy_loss,var_list=[p for p in tf.trainable_variables()
        # 	if (('fc_classification' in p.name) or ('out_classification' in p.name))])

        self.minimize_loss = tf.train.MomentumOptimizer(
            1e-4, 0.9, use_nesterov=True).minimize(
                self.cross_entropy_loss
            )  #,var_list=[p for p in tf.trainable_variables()
        # if (('fc_classification' in p.name) or ('out_classification' in p.name))])
        #self.minimize_loss = tf.train.AdamOptimizer(1e-7).minimize(self.cross_entropy_loss)

        self.accuracy = tf.reduce_mean(
            tf.cast(
                tf.equal(tf.cast(tf.argmax(self.net, 1), tf.int32), self.Y),
                tf.float32))

        self.saver = tf.train.Saver(max_to_keep=4,
                                    keep_checkpoint_every_n_hours=2)
        self.best_saver = tf.train.Saver(max_to_keep=10)

        return self.cross_entropy_loss
Пример #17
0
    def ImgNet(self):
        net = self.images  # [n,128,64,3]

        if train == 1:  # 训练时做数据增强,其他情况没必要做
            # 数据增强
            x1 = []
            for i in range(net.shape[0]):
                # boxes = tf.constant([[[0.05, 0.05, 0.9, 0.7], [0.35, 0.47, 0.5, 0.56]]])
                x2 = tf.squeeze(
                    tf.slice(net, [i, 0, 0, 0],
                             [1, img_Pixels_h, img_Pixels_w, img_Pixels_c]), 0)
                x1.append(
                    preprocess_for_train(x2, img_Pixels_h, img_Pixels_w, None))

            net = tf.convert_to_tensor(x1, tf.float32)

        net = slim.conv2d(net,
                          32,
                          7, (2, 1),
                          padding='SAME',
                          activation_fn=tf.nn.leaky_relu,
                          normalizer_fn=slim.layers.batch_norm,
                          reuse=self.reuse,
                          trainable=self.trainable,
                          scope='conv1')  # [n,64,64,32]
        net = tf.layers.max_pooling2d(net, 3, 2, 'same',
                                      name='pool1')  # [n,32,32,32]
        # Dense Block-1
        branch_1 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db1_1_1')
        branch_1 = slim.conv2d(branch_1,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db1_1_2')  # [n,32,32,32]

        net = tf.concat((net, branch_1), -1)  # [n,32,32,64]

        branch_2 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db1_2_1')  # [n,32,32,32]
        branch_2 = slim.conv2d(branch_2,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db1_2_2')  # [n,32,32,64]

        net = tf.concat((net, branch_2), -1)  # [n,32,32,128]

        branch_3 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db1_3_1')  # [n,32,32,64]
        branch_3 = slim.conv2d(branch_3,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db1_3_2')  # [n,32,32,128]

        net = tf.concat((net, branch_3), -1)  # [n,32,32,256]

        # transition layer
        net = slim.conv2d(net,
                          net.shape[-1] // 2,
                          1,
                          1,
                          padding='same',
                          activation_fn=tf.nn.leaky_relu,
                          normalizer_fn=slim.layers.batch_norm,
                          reuse=self.reuse,
                          trainable=self.trainable,
                          scope='tl1_1')  # [n,32,32,128]
        net = slim.avg_pool2d(net, 2, 2, 'same',
                              scope='pool2')  # [n,16,16,128]

        net = tf.layers.dropout(net, self.dropout)

        # Dense Block-2
        branch_1 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db2_1_1')
        branch_1 = slim.conv2d(branch_1,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db2_1_2')  # [n,16,16,128]

        net = tf.concat((net, branch_1), -1)  # [n,32,32,256]

        branch_2 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db2_2_1')  # [n,16,16,128]
        branch_2 = slim.conv2d(branch_2,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db2_2_2')  # [n,16,16,256]

        net = tf.concat((net, branch_2), -1)  # [n,16,16,512]

        branch_3 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db2_3_1')  # [n,16,16,256]
        branch_3 = slim.conv2d(branch_3,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db2_3_2')  # [n,16,16,512]

        net = tf.concat((net, branch_3), -1)  # [n,16,16,1024]

        # transition layer
        net = slim.conv2d(net,
                          net.shape[-1] // 2,
                          1,
                          1,
                          padding='same',
                          activation_fn=tf.nn.leaky_relu,
                          normalizer_fn=slim.layers.batch_norm,
                          reuse=self.reuse,
                          trainable=self.trainable,
                          scope='tl2_1')  # [n,16,16,512]
        net = slim.avg_pool2d(net, 2, 2, 'same', scope='pool2')  # [n,8,8,512]

        net = tf.layers.dropout(net, self.dropout)

        # Dense Block-3
        branch_1 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db3_1_1')
        branch_1 = slim.conv2d(branch_1,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db3_1_2')  # [n,16,16,128]

        net = tf.concat((net, branch_1), -1)  # [n,32,32,256]

        branch_2 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db3_2_1')  # [n,16,16,128]
        branch_2 = slim.conv2d(branch_2,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db3_2_2')  # [n,16,16,256]

        net = tf.concat((net, branch_2), -1)  # [n,16,16,512]

        branch_3 = slim.conv2d(net,
                               net.shape[-1] // 2,
                               1,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db3_3_1')  # [n,16,16,256]
        branch_3 = slim.conv2d(branch_3,
                               net.shape[-1],
                               3,
                               1,
                               padding='same',
                               activation_fn=tf.nn.leaky_relu,
                               normalizer_fn=slim.layers.batch_norm,
                               reuse=self.reuse,
                               trainable=self.trainable,
                               scope='db3_3_2')  # [n,16,16,512]

        net = tf.concat((net, branch_3), -1)  # [n,16,16,1024]

        # transition layer
        net = slim.conv2d(net,
                          net.shape[-1] // 2,
                          1,
                          1,
                          padding='same',
                          activation_fn=tf.nn.leaky_relu,
                          normalizer_fn=slim.layers.batch_norm,
                          reuse=self.reuse,
                          trainable=self.trainable,
                          scope='tl3_1')  # [n,16,16,512]
        net = slim.avg_pool2d(net, 2, 2, 'same', scope='pool3')  # [n,4,4,512]

        net = tf.layers.dropout(net, self.dropout)

        # Classifications
        net = slim.avg_pool2d(net, 4, 1, 'valid', scope='pool2')  # [n,1,1,512]

        net = tf.reshape(net, [-1, np.int(np.prod(net.get_shape()[1:]))],
                         name='flatten1')  # [n,512]

        net = slim.fully_connected(net,
                                   512,
                                   activation_fn=tf.nn.relu,
                                   trainable=self.trainable,
                                   scope='fc1')

        self.ImgNet_fc = net

        net = slim.fully_connected(net,
                                   m,
                                   activation_fn=None,
                                   scope='coding_layer')  # sigmoid ,softmax

        self.ImgNet_label = tf.nn.sigmoid(net, name='ImgNet_label')
Пример #18
0
def inception_resnet_v2(inputs, is_training=True,
                        dropout_keep_prob=0.8,
                        bottleneck_layer_size=128,
                        reuse=None,
                        scope='InceptionResnetV2'):
    """Creates the Inception Resnet V2 model.
    Args:
      inputs: a 4-D tensor of size [batch_size, height, width, 3].
      num_classes: number of predicted classes.
      is_training: whether is training or not.
      dropout_keep_prob: float, the fraction to keep before final layer.
      reuse: whether or not the network and its variables should be reused. To be
        able to reuse 'scope' must be given.
      scope: Optional variable_scope.
    Returns:
      logits: the logits outputs of the model.
      end_points: the set of end_points from the inception model.
    """
    end_points = {}
  
    with tf.variable_scope(scope, 'InceptionResnetV2', [inputs], reuse=reuse):
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
            with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                                stride=1, padding='SAME'):
      
                # 149 x 149 x 32
                net = slim.conv2d(inputs, 32, 3, stride=2, padding='VALID',
                                  scope='Conv2d_1a_3x3')
                end_points['Conv2d_1a_3x3'] = net
                # 147 x 147 x 32
                net = slim.conv2d(net, 32, 3, padding='VALID',
                                  scope='Conv2d_2a_3x3')
                end_points['Conv2d_2a_3x3'] = net
                # 147 x 147 x 64
                net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3')
                end_points['Conv2d_2b_3x3'] = net
                # 73 x 73 x 64
                net = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                      scope='MaxPool_3a_3x3')
                end_points['MaxPool_3a_3x3'] = net
                # 73 x 73 x 80
                net = slim.conv2d(net, 80, 1, padding='VALID',
                                  scope='Conv2d_3b_1x1')
                end_points['Conv2d_3b_1x1'] = net
                # 71 x 71 x 192
                net = slim.conv2d(net, 192, 3, padding='VALID',
                                  scope='Conv2d_4a_3x3')
                end_points['Conv2d_4a_3x3'] = net
                # 35 x 35 x 192
                net = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                      scope='MaxPool_5a_3x3')
                end_points['MaxPool_5a_3x3'] = net
        
                # 35 x 35 x 320
                with tf.variable_scope('Mixed_5b'):
                    with tf.variable_scope('Branch_0'):
                        tower_conv = slim.conv2d(net, 96, 1, scope='Conv2d_1x1')
                    with tf.variable_scope('Branch_1'):
                        tower_conv1_0 = slim.conv2d(net, 48, 1, scope='Conv2d_0a_1x1')
                        tower_conv1_1 = slim.conv2d(tower_conv1_0, 64, 5,
                                                    scope='Conv2d_0b_5x5')
                    with tf.variable_scope('Branch_2'):
                        tower_conv2_0 = slim.conv2d(net, 64, 1, scope='Conv2d_0a_1x1')
                        tower_conv2_1 = slim.conv2d(tower_conv2_0, 96, 3,
                                                    scope='Conv2d_0b_3x3')
                        tower_conv2_2 = slim.conv2d(tower_conv2_1, 96, 3,
                                                    scope='Conv2d_0c_3x3')
                    with tf.variable_scope('Branch_3'):
                        tower_pool = slim.avg_pool2d(net, 3, stride=1, padding='SAME',
                                                     scope='AvgPool_0a_3x3')
                        tower_pool_1 = slim.conv2d(tower_pool, 64, 1,
                                                   scope='Conv2d_0b_1x1')
                    net = tf.concat([tower_conv, tower_conv1_1,
                                        tower_conv2_2, tower_pool_1], 3)
        
                end_points['Mixed_5b'] = net
                net = slim.repeat(net, 10, block35, scale=0.17)
        
                # 17 x 17 x 1024
                with tf.variable_scope('Mixed_6a'):
                    with tf.variable_scope('Branch_0'):
                        tower_conv = slim.conv2d(net, 384, 3, stride=2, padding='VALID',
                                                 scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_1'):
                        tower_conv1_0 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
                        tower_conv1_1 = slim.conv2d(tower_conv1_0, 256, 3,
                                                    scope='Conv2d_0b_3x3')
                        tower_conv1_2 = slim.conv2d(tower_conv1_1, 384, 3,
                                                    stride=2, padding='VALID',
                                                    scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_2'):
                        tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                    net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3)
        
                end_points['Mixed_6a'] = net
                net = slim.repeat(net, 20, block17, scale=0.10)
        
                with tf.variable_scope('Mixed_7a'):
                    with tf.variable_scope('Branch_0'):
                        tower_conv = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
                        tower_conv_1 = slim.conv2d(tower_conv, 384, 3, stride=2,
                                                   padding='VALID', scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_1'):
                        tower_conv1 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
                        tower_conv1_1 = slim.conv2d(tower_conv1, 288, 3, stride=2,
                                                    padding='VALID', scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_2'):
                        tower_conv2 = slim.conv2d(net, 256, 1, scope='Conv2d_0a_1x1')
                        tower_conv2_1 = slim.conv2d(tower_conv2, 288, 3,
                                                    scope='Conv2d_0b_3x3')
                        tower_conv2_2 = slim.conv2d(tower_conv2_1, 320, 3, stride=2,
                                                    padding='VALID', scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_3'):
                        tower_pool = slim.max_pool2d(net, 3, stride=2, padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                    net = tf.concat([tower_conv_1, tower_conv1_1,
                                        tower_conv2_2, tower_pool], 3)
        
                end_points['Mixed_7a'] = net
        
                net = slim.repeat(net, 9, block8, scale=0.20)
                net = block8(net, activation_fn=None)
        
                net = slim.conv2d(net, 1536, 1, scope='Conv2d_7b_1x1')
                end_points['Conv2d_7b_1x1'] = net
        
                with tf.variable_scope('Logits'):
                    end_points['PrePool'] = net
                    #pylint: disable=no-member
                    net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID',
                                          scope='AvgPool_1a_8x8')
                    net = slim.flatten(net)
          
                    net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
                                       scope='Dropout')
          
                    end_points['PreLogitsFlatten'] = net
                
                net = slim.fully_connected(net, bottleneck_layer_size, activation_fn=None, 
                        scope='Bottleneck', reuse=False)
  
    return net, end_points
Пример #19
0
def inception_resnet_v2(inputs,
                        is_training=True,
                        dropout_keep_prob=0.8,
                        bottleneck_layer_size=128,
                        reuse=None,
                        scope='InceptionResnetV2'):
    """Creates the Inception Resnet V2 model.
    Args:
      inputs: a 4-D tensor of size [batch_size, height, width, 3].
      num_classes: number of predicted classes.
      is_training: whether is training or not.
      dropout_keep_prob: float, the fraction to keep before final layer.
      reuse: whether or not the network and its variables should be reused. To be
        able to reuse 'scope' must be given.
      scope: Optional variable_scope.
    Returns:
      logits: the logits outputs of the model.
      end_points: the set of end_points from the inception model.
    """
    end_points = {}

    with tf.variable_scope(scope, 'InceptionResnetV2', [inputs], reuse=reuse):
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
            with slim.arg_scope(
                [slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                    stride=1,
                    padding='SAME'):

                # 149 x 149 x 32
                net = slim.conv2d(inputs,
                                  32,
                                  3,
                                  stride=2,
                                  padding='VALID',
                                  scope='Conv2d_1a_3x3')
                end_points['Conv2d_1a_3x3'] = net
                # 147 x 147 x 32
                net = slim.conv2d(net,
                                  32,
                                  3,
                                  padding='VALID',
                                  scope='Conv2d_2a_3x3')
                end_points['Conv2d_2a_3x3'] = net
                # 147 x 147 x 64
                net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3')
                end_points['Conv2d_2b_3x3'] = net
                # 73 x 73 x 64
                net = slim.max_pool2d(net,
                                      3,
                                      stride=2,
                                      padding='VALID',
                                      scope='MaxPool_3a_3x3')
                end_points['MaxPool_3a_3x3'] = net
                # 73 x 73 x 80
                net = slim.conv2d(net,
                                  80,
                                  1,
                                  padding='VALID',
                                  scope='Conv2d_3b_1x1')
                end_points['Conv2d_3b_1x1'] = net
                # 71 x 71 x 192
                net = slim.conv2d(net,
                                  192,
                                  3,
                                  padding='VALID',
                                  scope='Conv2d_4a_3x3')
                end_points['Conv2d_4a_3x3'] = net
                # 35 x 35 x 192
                net = slim.max_pool2d(net,
                                      3,
                                      stride=2,
                                      padding='VALID',
                                      scope='MaxPool_5a_3x3')
                end_points['MaxPool_5a_3x3'] = net

                # 35 x 35 x 320
                with tf.variable_scope('Mixed_5b'):
                    with tf.variable_scope('Branch_0'):
                        tower_conv = slim.conv2d(net,
                                                 96,
                                                 1,
                                                 scope='Conv2d_1x1')
                    with tf.variable_scope('Branch_1'):
                        tower_conv1_0 = slim.conv2d(net,
                                                    48,
                                                    1,
                                                    scope='Conv2d_0a_1x1')
                        tower_conv1_1 = slim.conv2d(tower_conv1_0,
                                                    64,
                                                    5,
                                                    scope='Conv2d_0b_5x5')
                    with tf.variable_scope('Branch_2'):
                        tower_conv2_0 = slim.conv2d(net,
                                                    64,
                                                    1,
                                                    scope='Conv2d_0a_1x1')
                        tower_conv2_1 = slim.conv2d(tower_conv2_0,
                                                    96,
                                                    3,
                                                    scope='Conv2d_0b_3x3')
                        tower_conv2_2 = slim.conv2d(tower_conv2_1,
                                                    96,
                                                    3,
                                                    scope='Conv2d_0c_3x3')
                    with tf.variable_scope('Branch_3'):
                        tower_pool = slim.avg_pool2d(net,
                                                     3,
                                                     stride=1,
                                                     padding='SAME',
                                                     scope='AvgPool_0a_3x3')
                        tower_pool_1 = slim.conv2d(tower_pool,
                                                   64,
                                                   1,
                                                   scope='Conv2d_0b_1x1')
                    net = tf.concat([
                        tower_conv, tower_conv1_1, tower_conv2_2, tower_pool_1
                    ], 3)

                end_points['Mixed_5b'] = net
                net = slim.repeat(net, 10, block35, scale=0.17)

                # 17 x 17 x 1024
                with tf.variable_scope('Mixed_6a'):
                    with tf.variable_scope('Branch_0'):
                        tower_conv = slim.conv2d(net,
                                                 384,
                                                 3,
                                                 stride=2,
                                                 padding='VALID',
                                                 scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_1'):
                        tower_conv1_0 = slim.conv2d(net,
                                                    256,
                                                    1,
                                                    scope='Conv2d_0a_1x1')
                        tower_conv1_1 = slim.conv2d(tower_conv1_0,
                                                    256,
                                                    3,
                                                    scope='Conv2d_0b_3x3')
                        tower_conv1_2 = slim.conv2d(tower_conv1_1,
                                                    384,
                                                    3,
                                                    stride=2,
                                                    padding='VALID',
                                                    scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_2'):
                        tower_pool = slim.max_pool2d(net,
                                                     3,
                                                     stride=2,
                                                     padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                    net = tf.concat([tower_conv, tower_conv1_2, tower_pool], 3)

                end_points['Mixed_6a'] = net
                net = slim.repeat(net, 20, block17, scale=0.10)

                with tf.variable_scope('Mixed_7a'):
                    with tf.variable_scope('Branch_0'):
                        tower_conv = slim.conv2d(net,
                                                 256,
                                                 1,
                                                 scope='Conv2d_0a_1x1')
                        tower_conv_1 = slim.conv2d(tower_conv,
                                                   384,
                                                   3,
                                                   stride=2,
                                                   padding='VALID',
                                                   scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_1'):
                        tower_conv1 = slim.conv2d(net,
                                                  256,
                                                  1,
                                                  scope='Conv2d_0a_1x1')
                        tower_conv1_1 = slim.conv2d(tower_conv1,
                                                    288,
                                                    3,
                                                    stride=2,
                                                    padding='VALID',
                                                    scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_2'):
                        tower_conv2 = slim.conv2d(net,
                                                  256,
                                                  1,
                                                  scope='Conv2d_0a_1x1')
                        tower_conv2_1 = slim.conv2d(tower_conv2,
                                                    288,
                                                    3,
                                                    scope='Conv2d_0b_3x3')
                        tower_conv2_2 = slim.conv2d(tower_conv2_1,
                                                    320,
                                                    3,
                                                    stride=2,
                                                    padding='VALID',
                                                    scope='Conv2d_1a_3x3')
                    with tf.variable_scope('Branch_3'):
                        tower_pool = slim.max_pool2d(net,
                                                     3,
                                                     stride=2,
                                                     padding='VALID',
                                                     scope='MaxPool_1a_3x3')
                    net = tf.concat([
                        tower_conv_1, tower_conv1_1, tower_conv2_2, tower_pool
                    ], 3)

                end_points['Mixed_7a'] = net

                net = slim.repeat(net, 9, block8, scale=0.20)
                net = block8(net, activation_fn=None)

                net = slim.conv2d(net, 1536, 1, scope='Conv2d_7b_1x1')
                end_points['Conv2d_7b_1x1'] = net

                with tf.variable_scope('Logits'):
                    end_points['PrePool'] = net
                    #pylint: disable=no-member
                    net = slim.avg_pool2d(net,
                                          net.get_shape()[1:3],
                                          padding='VALID',
                                          scope='AvgPool_1a_8x8')
                    net = slim.flatten(net)

                    net = slim.dropout(net,
                                       dropout_keep_prob,
                                       is_training=is_training,
                                       scope='Dropout')

                    end_points['PreLogitsFlatten'] = net

                net = slim.fully_connected(net,
                                           bottleneck_layer_size,
                                           activation_fn=None,
                                           scope='Bottleneck',
                                           reuse=False)

    return net, end_points
Пример #20
0
def mobilenet(inputs,
          num_classes=1000,
          is_training=True,
          width_multiplier=1,
          scope='MobileNet'):
  """ MobileNet
  More detail, please refer to Google's paper(https://arxiv.org/abs/1704.04861).
  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.
    scope: Optional scope for the variables.
  Returns:
    logits: the pre-softmax activations, a tensor of size
      [batch_size, `num_classes`]
    end_points: a dictionary from components of the network to the corresponding
      activation.
  """

  def _depthwise_separable_conv(inputs,
                                num_pwc_filters,
                                width_multiplier,
                                sc,
                                downsample=False):
    """ Helper function to build the depth-wise separable convolution layer.
    """
    num_pwc_filters = round(num_pwc_filters * width_multiplier)
    _stride = 2 if downsample else 1

    # skip pointwise by setting num_outputs=None
    depthwise_conv = slim.separable_convolution2d(inputs,
                                                  num_outputs=None,
                                                  stride=_stride,
                                                  depth_multiplier=1,
                                                  kernel_size=[3, 3],
                                                  scope=sc+'/depthwise_conv')

    bn = slim.batch_norm(depthwise_conv, scope=sc+'/dw_batch_norm')
    pointwise_conv = slim.convolution2d(bn,
                                        num_pwc_filters,
                                        kernel_size=[1, 1],
                                        scope=sc+'/pointwise_conv')
    bn = slim.batch_norm(pointwise_conv, scope=sc+'/pw_batch_norm')
    return bn

  with tf.variable_scope(scope) as sc:
    end_points_collection = sc.name + '_end_points'
    with slim.arg_scope([slim.convolution2d, slim.separable_convolution2d],
                        activation_fn=None,
                        outputs_collections=[end_points_collection]):
      with slim.arg_scope([slim.batch_norm],
                          is_training=is_training,
                          activation_fn=tf.nn.relu,
                          fused=True):
        net = slim.convolution2d(inputs, round(32 * width_multiplier), [3, 3], stride=2, padding='SAME', scope='conv_1')
        net = slim.batch_norm(net, scope='conv_1/batch_norm')
        net = _depthwise_separable_conv(net, 64, width_multiplier, sc='conv_ds_2')
        net = _depthwise_separable_conv(net, 128, width_multiplier, downsample=True, sc='conv_ds_3')
        net = _depthwise_separable_conv(net, 128, width_multiplier, sc='conv_ds_4')
        net = _depthwise_separable_conv(net, 256, width_multiplier, downsample=True, sc='conv_ds_5')
        net = _depthwise_separable_conv(net, 256, width_multiplier, sc='conv_ds_6')
        net = _depthwise_separable_conv(net, 512, width_multiplier, downsample=True, sc='conv_ds_7')

        net = _depthwise_separable_conv(net, 512, width_multiplier, sc='conv_ds_8')
        net = _depthwise_separable_conv(net, 512, width_multiplier, sc='conv_ds_9')
        net = _depthwise_separable_conv(net, 512, width_multiplier, sc='conv_ds_10')
        net = _depthwise_separable_conv(net, 512, width_multiplier, sc='conv_ds_11')
        net = _depthwise_separable_conv(net, 512, width_multiplier, sc='conv_ds_12')

        net = _depthwise_separable_conv(net, 1024, width_multiplier, downsample=True, sc='conv_ds_13')
        net = _depthwise_separable_conv(net, 1024, width_multiplier, sc='conv_ds_14')
        net = slim.avg_pool2d(net, [7, 7], scope='avg_pool_15')

    end_points = slim.utils.convert_collection_to_dict(end_points_collection)
    net = tf.squeeze(net, [1, 2], name='SpatialSqueeze')
    end_points['squeeze'] = net
    logits = slim.fully_connected(net, num_classes, activation_fn=None, scope='fc_16')
    predictions = slim.softmax(logits, scope='Predictions')

    end_points['Logits'] = logits
    end_points['Predictions'] = predictions

  return logits, end_points
def inception_resnet_v1(inputs, is_training=True,
                        dropout_keep_prob=0.8,
                        reuse=None,
                        scope='InceptionResnetV1'):
    """Creates the Inception Resnet V1 model.
    Args:
      inputs: a 4-D tensor of size [batch_size, height, width, 3].
      num_classes: number of predicted classes.
      is_training: whether is training or not.
      dropout_keep_prob: float, the fraction to keep before final layer.
      reuse: whether or not the network and its variables should be reused. To be
        able to reuse 'scope' must be given.
      scope: Optional variable_scope.
    Returns:
      logits: the logits outputs of the model.
      end_points: the set of end_points from the inception model.
    """
    end_points = {}

    with tf.variable_scope(scope, 'InceptionResnetV1', [inputs], reuse=reuse):
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
            with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                                stride=1, padding='SAME'):

                # 64 x 64 x 16
                net = slim.conv2d(inputs, 16, 3, stride=2, padding='SAME',
                                  scope='Conv2d_1a_3x3')
                end_points['Conv2d_1a_3x3'] = net
                #  64 x 64 x 16
                # net = slim.conv2d(net, 16, 3, padding='SAME',
                #                   scope='Conv2d_2a_3x3')
                # end_points['Conv2d_2a_3x3'] = net
                #  64 x 64 x 32
                net = slim.conv2d(net, 32, 3, scope='Conv2d_2b_3x3')
                end_points['Conv2d_2b_3x3'] = net
                # 32 x 32 x 32
                net = slim.max_pool2d(net, 3, stride=2, padding='SAME',
                                      scope='MaxPool_3a_3x3')
                end_points['MaxPool_3a_3x3'] = net
                # 32 x 32 x 40
                net = slim.conv2d(net, 40, 1, padding='SAME',
                                  scope='Conv2d_3b_1x1')
                end_points['Conv2d_3b_1x1'] = net
                # 32 x 32 x 96
                net = slim.conv2d(net, 96, 3, padding='SAME',
                                  scope='Conv2d_4a_3x3')
                end_points['Conv2d_4a_3x3'] = net
                # 32 x 32 x 128
                net = slim.conv2d(net, 128, 3, stride=2, padding='SAME',
                                  scope='Conv2d_4b_3x3')
                end_points['Conv2d_4b_3x3'] = net

                # 1 x Inception-resnet-A
                net = slim.repeat(net, 2, block35, scale=0.17)

                # Reduction-A
                with tf.variable_scope('Mixed_6a'):
                    net = reduction_a(net, 96, 96, 128, 192)
                end_points['Mixed_6a'] = net

                # 1 x Inception-Resnet-B
                net = slim.repeat(net, 2, block17, scale=0.10)

                # Reduction-B
                with tf.variable_scope('Mixed_7a'):
                    net = reduction_b(net, 128)
                end_points['Mixed_7a'] = net

                # 1 x Inception-Resnet-C
                net = slim.repeat(net, 2, block8, scale=0.20)
                net = block8(net, kernel_size=96, activation_fn=None)

                with tf.variable_scope('Logits'):
                    end_points['PrePool'] = net
                    # pylint: disable=no-member
                    net = slim.avg_pool2d(net, net.get_shape()[1:3], padding='VALID',
                                          scope='AvgPool_1a_8x8')
                    net = slim.flatten(net)

                    net = slim.dropout(net, dropout_keep_prob, is_training=is_training,
                                       scope='Dropout')

                    end_points['PreLogitsFlatten'] = net

    return net, end_points
Пример #22
0
def V1_slim(inputs,num_cls,is_train = False,keep_prob=0.4,spatital_squeeze=True):
    with tf.name_scope('reshape'):
        net = tf.reshape(inputs, [-1, 224, 224, 3])

    with tf.variable_scope('GoogLeNet_V1'):
        with slim.arg_scope(
                [slim.conv2d, slim.fully_connected],
                weights_regularizer=slim.l2_regularizer(5e-4),
                weights_initializer=slim.xavier_initializer(),
        ):
            with slim.arg_scope(
                [slim.conv2d,slim.max_pool2d,slim.avg_pool2d],
                    padding='SAME',
                    stride=1,
            ):
                net = slim.conv2d(net,64,7,stride=2,scope='layer1')
                net = slim.max_pool2d(net,3,stride=2,scope='layer2')
                net = tf.nn.lrn(net)
                net = slim.conv2d(net,64,1,scope='layer3')
                net = slim.conv2d(net,192,3,scope='layer4')
                net = tf.nn.lrn(net)
                net = slim.max_pool2d(net,3,stride=2,scope='layer5')
                net = inception_moudle_v1(net,'layer6',[64,96,128,16,32,32])
                net = inception_moudle_v1(net,'layer8',[128,128,192,32,96,64])
                net = slim.max_pool2d(net,3,stride=2,scope='layer10')
                net = inception_moudle_v1(net,'layer11',[192,96,208,16,48,64])
                net = inception_moudle_v1(net,'layer13',[160,112,224,24,64,64])
                net_1 = net
                net = inception_moudle_v1(net,'layer15',[128,128,256,24,64,64])
                net = inception_moudle_v1(net,'layer17',[112,144,288,32,64,64])
                net_2 = net
                net = inception_moudle_v1(net,'lauer19',[256,160,320,32,128,128])
                net = slim.max_pool2d(net,3,stride=2,scope='layer21')
                net = inception_moudle_v1(net,'layer22',[256,160,320,32,128,128])
                net = inception_moudle_v1(net,'layer24',[384,192,384,48,128,128])

                net = slim.avg_pool2d(net,7,stride=1,padding='VALID',scope='layer26')
                net = slim.dropout(net,keep_prob=keep_prob,scope='dropout')
                net = slim.conv2d(net,num_cls,1,activation_fn=None, normalizer_fn=None,scope='layer27')
                if spatital_squeeze:
                    net = tf.squeeze(net,[1,2],name='squeeze')
                net = slim.softmax(net,scope='softmax2')

                if is_train:
                    net_1 = slim.avg_pool2d(net_1, 5, padding='VALID', stride=3, scope='auxiliary0_avg')
                    net_1 = slim.conv2d(net_1, 128, 1, scope='auxiliary0_conv_1X1')
                    net_1 = slim.flatten(net_1)
                    net_1 = slim.fully_connected(net_1,1024,scope='auxiliary0_fc1')
                    net_1 = slim.dropout(net_1, 0.7)
                    net_1 = slim.fully_connected(net_1,num_cls,activation_fn=None,scope='auxiliary0_fc2')
                    net_1 = slim.softmax(net_1, scope='softmax0')

                    net_2 = slim.avg_pool2d(net_2, 5, padding='VALID', stride=3, scope='auxiliary1_avg')
                    net_2 = slim.conv2d(net_2, 128, 1, scope='auxiliary1_conv_1X1')
                    net_2 = slim.flatten(net_2)
                    net_2 = slim.fully_connected(net_2,1024,scope='auxiliary1_fc1')
                    net_2 = slim.dropout(net_2, 0.7)
                    net_2 = slim.fully_connected(net_2,num_cls,activation_fn=None,scope='auxiliary1_fc2')
                    net_2 = slim.softmax(net_2, scope='softmax1')

                    net = net_1 * 0.3 + net_2 * 0.3 + net * 0.4
                    print(net.shape)

    return net
Пример #23
0
def inception_v1(inputs, keep_prob, num_classes):
	with tf.variable_scope('InceptionV1'):
		with slim.arg_scope([slim.conv2d, slim.fully_connected],
			weights_initializer=tf.truncated_normal_initializer(stddev=0.001)):
			with slim.arg_scope([slim.conv2d, slim.max_pool2d],
				stride=1, padding='SAME'):
				with slim.arg_scope([slim.conv2d],
					normalizer_fn=slim.batch_norm):
					with slim.arg_scope([slim.batch_norm, slim.dropout],
						is_training=True):

						net = slim.conv2d(inputs, 64, [7, 7], stride=2, scope='Conv2d_1a_7x7')
						net = slim.max_pool2d(net, [3, 3], stride=2, scope='MaxPool_2a_3x3')
						net = slim.conv2d(net, 64, [1, 1], scope='Conv2d_2b_1x1')
						net = slim.conv2d(net, 192, [3, 3], scope='Conv2d_2c_3x3')
						net = slim.max_pool2d(net, [3, 3], stride=2, scope='MaxPool_3a_3x3')

						with tf.variable_scope('Mixed_3b'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 64, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 96, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 128, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 16, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 32, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 32, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Mixed_3c'):
							with tf.variable_scope('Branch_0'):
			 					branch_0 = slim.conv2d(net, 128, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 128, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 192, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 32, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 96, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 64, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						net = slim.max_pool2d(net, [3, 3], stride=2, scope='MaxPool_4a_3x3')

						with tf.variable_scope('Mixed_4b'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 96, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 208, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 16, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 48, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 64, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Mixed_4c'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 160, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 112, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 224, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 24, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 64, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 64, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Mixed_4d'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 128, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 128, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 256, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 24, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 64, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 64, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Mixed_4e'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 112, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 144, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 288, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 32, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 64, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 64, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Mixed_4f'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 256, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 160, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 320, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 32, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 128, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 128, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						net = slim.max_pool2d(net, [2, 2], stride=2, scope='MaxPool_5a_2x2')

						with tf.variable_scope('Mixed_5b'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 256, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 160, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 320, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 32, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 128, [3, 3], scope='Conv2d_0a_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 128, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Mixed_5c'):
							with tf.variable_scope('Branch_0'):
								branch_0 = slim.conv2d(net, 384, [1, 1], scope='Conv2d_0a_1x1')
							with tf.variable_scope('Branch_1'):
								branch_1 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
								branch_1 = slim.conv2d(branch_1, 384, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_2'):
								branch_2 = slim.conv2d(net, 48, [1, 1], scope='Conv2d_0a_1x1')
								branch_2 = slim.conv2d(branch_2, 128, [3, 3], scope='Conv2d_0b_3x3')
							with tf.variable_scope('Branch_3'):
								branch_3 = slim.max_pool2d(net, [3, 3], scope='MaxPool_0a_3x3')
								branch_3 = slim.conv2d(branch_3, 128, [1, 1], scope='Conv2d_0b_1x1')
							net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2, branch_3])

						with tf.variable_scope('Logits'):
							net = slim.avg_pool2d(net, [7, 7], stride=1, scope='AvgPool_0a_7x7')
							net = slim.dropout(net, keep_prob, scope='Dropout_0b')
							logits = slim.conv2d(net, num_classes, [1, 1], activation_fn=None,
								normalizer_fn=None, scope='Conv2d_0c_1x1')

							return logits
Пример #24
0
def inception_resnet_v1(inputs,
                        is_training=True,
                        dropout_keep_prob=0.8,
                        bottleneck_layer_size=128,
                        reuse=None,
                        scope='InceptionResnetV1'):
    """Creates the Inception Resnet V1 model.
    Args:
      inputs: a 4-D tensor of size [batch_size, height, width, 3].
      is_training: whether is training or not.
      dropout_keep_prob: float, the fraction to keep before final layer.
      reuse: whether or not the network and its variables should be reused. To be
        able to reuse 'scope' must be given.
      scope: Optional variable_scope.
    Returns:
      logits: the logits outputs of the model.
      end_points: the set of end_points from the inception model.
    """
    end_points = {}

    with tf.variable_scope(scope, 'InceptionResnetV1', [inputs], reuse=reuse):
        with slim.arg_scope([slim.batch_norm, slim.dropout],
                            is_training=is_training):
            with slim.arg_scope(
                [slim.conv2d, slim.max_pool2d, slim.avg_pool2d],
                    stride=1,
                    padding='SAME'):
                # 149 x 149 x 32
                net = slim.conv2d(inputs,
                                  32,
                                  3,
                                  stride=2,
                                  padding='VALID',
                                  scope='Conv2d_1a_3x3')
                end_points['Conv2d_1a_3x3'] = net
                # 147 x 147 x 32
                net = slim.conv2d(net,
                                  32,
                                  3,
                                  padding='VALID',
                                  scope='Conv2d_2a_3x3')
                end_points['Conv2d_2a_3x3'] = net
                # 147 x 147 x 64
                net = slim.conv2d(net, 64, 3, scope='Conv2d_2b_3x3')
                end_points['Conv2d_2b_3x3'] = net
                # 73 x 73 x 64
                net = slim.max_pool2d(net,
                                      3,
                                      stride=2,
                                      padding='VALID',
                                      scope='MaxPool_3a_3x3')
                end_points['MaxPool_3a_3x3'] = net
                # 73 x 73 x 80
                net = slim.conv2d(net,
                                  80,
                                  1,
                                  padding='VALID',
                                  scope='Conv2d_3b_1x1')
                end_points['Conv2d_3b_1x1'] = net
                # 71 x 71 x 192
                net = slim.conv2d(net,
                                  192,
                                  3,
                                  padding='VALID',
                                  scope='Conv2d_4a_3x3')
                end_points['Conv2d_4a_3x3'] = net
                # 35 x 35 x 256
                net = slim.conv2d(net,
                                  256,
                                  3,
                                  stride=2,
                                  padding='VALID',
                                  scope='Conv2d_4b_3x3')
                end_points['Conv2d_4b_3x3'] = net

                # 5 x Inception-resnet-A
                net = slim.repeat(net, 5, block35, scale=0.17)
                end_points['Mixed_5a'] = net

                # Reduction-A
                with tf.variable_scope('Mixed_6a'):
                    net = reduction_a(net, 192, 192, 256, 384)
                end_points['Mixed_6a'] = net

                # 10 x Inception-Resnet-B
                net = slim.repeat(net, 10, block17, scale=0.10)
                end_points['Mixed_6b'] = net

                # Reduction-B
                with tf.variable_scope('Mixed_7a'):
                    net = reduction_b(net)
                end_points['Mixed_7a'] = net

                # 5 x Inception-Resnet-C
                net = slim.repeat(net, 5, block8, scale=0.20)
                end_points['Mixed_8a'] = net

                net = block8(net, activation_fn=None)
                end_points['Mixed_8b'] = net

                with tf.variable_scope('Logits'):
                    end_points['PrePool'] = net
                    # pylint: disable=no-member
                    net = slim.avg_pool2d(net,
                                          net.get_shape()[1:3],
                                          padding='VALID',
                                          scope='AvgPool_1a_8x8')
                    net = slim.flatten(net)

                    net = slim.dropout(net,
                                       dropout_keep_prob,
                                       is_training=is_training,
                                       scope='Dropout')

                    end_points['PreLogitsFlatten'] = net

                net = slim.fully_connected(net,
                                           bottleneck_layer_size,
                                           activation_fn=None,
                                           scope='Bottleneck',
                                           reuse=False)

    return net, end_points
Пример #25
0
def network():
    images = tf.placeholder(dtype=tf.float32, shape=[None, 28, 280, 1], name='image_batch')
    labels = tf.placeholder(dtype=tf.int32, shape=[None, 10], name='label_batch')
    endpoints = {}
    conv_1 = slim.conv2d(images, 32, [5,5],1, padding='SAME')
    avg_pool_1 = slim.avg_pool2d(conv_1, [2,2],[1,1], padding='SAME')
    conv_2 = slim.conv2d(avg_pool_1, 32, [5,5], 1,padding='SAME')
    avg_pool_2 = slim.avg_pool2d(conv_2, [2,2],[1,1], padding='SAME')
    conv_3 = slim.conv2d(avg_pool_2, 32, [3,3])
    avg_pool_3 = slim.avg_pool2d(conv_3, [2,2], [1,1])
    flatten = slim.flatten(avg_pool_3)
    fc1 = slim.fully_connected(flatten, 512, activation_fn=None)
    out0 = slim.fully_connected(fc1,2, activation_fn=None)
    out1 = slim.fully_connected(fc1,2, activation_fn=None)
    out2 = slim.fully_connected(fc1,2, activation_fn=None)
    out3 = slim.fully_connected(fc1,2, activation_fn=None)
    out4 = slim.fully_connected(fc1,2, activation_fn=None)
    out5 = slim.fully_connected(fc1,2, activation_fn=None)
    out6 = slim.fully_connected(fc1,2, activation_fn=None)
    out7 = slim.fully_connected(fc1,2, activation_fn=None)
    out8 = slim.fully_connected(fc1,2, activation_fn=None)
    out9 = slim.fully_connected(fc1,2, activation_fn=None)
    global_step = tf.Variable(initial_value=0)
    out0_argmax = tf.expand_dims(tf.argmax(out0, 1), 1)
    out1_argmax = tf.expand_dims(tf.argmax(out1, 1), 1)
    out2_argmax = tf.expand_dims(tf.argmax(out2, 1), 1)
    out3_argmax = tf.expand_dims(tf.argmax(out3, 1), 1)
    out4_argmax = tf.expand_dims(tf.argmax(out4, 1), 1)
    out5_argmax = tf.expand_dims(tf.argmax(out5, 1), 1)
    out6_argmax = tf.expand_dims(tf.argmax(out6, 1), 1)
    out7_argmax = tf.expand_dims(tf.argmax(out7, 1), 1)
    out8_argmax = tf.expand_dims(tf.argmax(out8, 1), 1)
    out9_argmax = tf.expand_dims(tf.argmax(out9, 1), 1)
    out_score = tf.concat([out0, out1, out2, out3, out4, out5, out6, out7, out8, out9], axis=1)
    out_final = tf.cast(tf.concat([out0_argmax, out1_argmax, out2_argmax, out3_argmax, out4_argmax, out5_argmax, out6_argmax, out7_argmax, out8_argmax, out9_argmax], axis=1), tf.int32)

    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out0, labels=tf.one_hot(labels[:,0],depth=2)))
    loss1 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out1, labels=tf.one_hot(labels[:,1],depth=2)))
    loss2 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out2, labels=tf.one_hot(labels[:,2],depth=2)))
    loss3 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out3, labels=tf.one_hot(labels[:,3],depth=2)))
    loss4 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out4, labels=tf.one_hot(labels[:,4],depth=2)))
    loss5 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out5, labels=tf.one_hot(labels[:,5],depth=2)))
    loss6 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out6, labels=tf.one_hot(labels[:,6],depth=2)))
    loss7 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out7, labels=tf.one_hot(labels[:,7],depth=2)))
    loss8 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out8, labels=tf.one_hot(labels[:,8],depth=2)))
    loss9 = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=out9, labels=tf.one_hot(labels[:,9],depth=2)))
    loss_list= [loss, loss1, loss2, loss3,loss4, loss5, loss6, loss7, loss8, loss9]
    loss_sum = tf.reduce_sum(loss_list)
    train_op = tf.train.AdamOptimizer(learning_rate=0.0001).minimize(loss_sum, global_step=global_step)
    accuracy = tf.reduce_mean(tf.cast(tf.reduce_all(tf.equal(out_final, labels), axis=1), tf.float32))
    tf.summary.scalar('loss_sum', loss_sum)
    tf.summary.scalar('accuracy', accuracy)
    merged_summary_op = tf.summary.merge_all()

    endpoints['global_step'] = global_step
    endpoints['images'] = images
    endpoints['labels'] = labels
    endpoints['train_op'] = train_op
    endpoints['loss_sum'] = loss_sum
    endpoints['accuracy'] = accuracy
    endpoints['merged_summary_op'] = merged_summary_op
    endpoints['out_final'] = out_final
    endpoints['out_score'] = out_score
    return endpoints
Пример #26
0
def add_final_training_ops(class_count, final_tensor_name, bottleneck_tensor):
    with tf.name_scope('input'):
        bottleneck_input = tf.placeholder_with_default(
            bottleneck_tensor,
            shape=BOTTLENECK_TENSOR_SHAPE,
            name='BottleneckInputPlaceholder')

        ground_truth_input = tf.placeholder(tf.float32, [None, class_count],
                                            name='GroundTruthInput')
        #返回batch_size的大小
        batch = tf.shape(bottleneck_input)[0]

    # Organizing the following ops as `final_training_ops` so they're easier
    # to see in TensorBoard
    net = bottleneck_input

    with tf.variable_scope('mix6'):
        with tf.variable_scope('Branch_0'):
            branch1x1 = slim.conv2d(net, 192, [1, 1])
        with tf.variable_scope('Branch_1'):
            branch7x7 = slim.conv2d(net, 160, [1, 1])
            branch7x7 = slim.conv2d(branch7x7, 160, [1, 7])
            branch7x7 = slim.conv2d(branch7x7, 192, [7, 1])
        with tf.variable_scope('Branch_2'):
            branch7x7dbl = slim.conv2d(net, 160, [1, 1])
            branch7x7dbl = slim.conv2d(branch7x7dbl, 160, [7, 1])
            branch7x7dbl = slim.conv2d(branch7x7dbl, 160, [1, 7])
            branch7x7dbl = slim.conv2d(branch7x7dbl, 160, [7, 1])
            branch7x7dbl = slim.conv2d(branch7x7dbl, 192, [1, 7])
        with tf.variable_scope('Branch_3'):
            branch_pool = slim.avg_pool2d(net, [3, 3],
                                          padding='SAME',
                                          stride=[1, 1])
            branch_pool = slim.conv2d(branch_pool, 192, [1, 1])
        net = tf.concat(
            axis=3, values=[branch1x1, branch7x7, branch7x7dbl, branch_pool])

    with tf.variable_scope('mix7'):
        with tf.variable_scope('Branch_0'):
            branch_0 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
        with tf.variable_scope('Branch_1'):
            branch_1 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
            branch_1 = slim.conv2d(branch_1,
                                   192, [1, 7],
                                   scope='Conv2d_0b_1x7')
            branch_1 = slim.conv2d(branch_1,
                                   192, [7, 1],
                                   scope='Conv2d_0c_7x1')
        with tf.variable_scope('Branch_2'):
            branch_2 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
            branch_2 = slim.conv2d(branch_2,
                                   192, [7, 1],
                                   scope='Conv2d_0b_7x1')
            branch_2 = slim.conv2d(branch_2,
                                   192, [1, 7],
                                   scope='Conv2d_0c_1x7')
            branch_2 = slim.conv2d(branch_2,
                                   192, [7, 1],
                                   scope='Conv2d_0d_7x1')
            branch_2 = slim.conv2d(branch_2,
                                   192, [1, 7],
                                   scope='Conv2d_0e_1x7')
        with tf.variable_scope('Branch_3'):
            branch_3 = slim.avg_pool2d(net, [3, 3],
                                       scope='AvgPool_0a_3x3',
                                       padding='SAME',
                                       stride=[1, 1])
            branch_3 = slim.conv2d(branch_3,
                                   192, [1, 1],
                                   scope='Conv2d_0b_1x1')
        net = tf.concat(axis=3,
                        values=[branch_0, branch_1, branch_2, branch_3])

    with tf.variable_scope('mix8'):
        with tf.variable_scope('Branch_0'):
            branch_0 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
            branch_0 = slim.conv2d(branch_0,
                                   320, [3, 3],
                                   stride=2,
                                   padding='VALID',
                                   scope='Conv2d_1a_3x3')
        with tf.variable_scope('Branch_1'):
            branch_1 = slim.conv2d(net, 192, [1, 1], scope='Conv2d_0a_1x1')
            branch_1 = slim.conv2d(branch_1,
                                   192, [1, 7],
                                   scope='Conv2d_0b_1x7')
            branch_1 = slim.conv2d(branch_1,
                                   192, [7, 1],
                                   scope='Conv2d_0c_7x1')
            branch_1 = slim.conv2d(branch_1,
                                   192, [3, 3],
                                   stride=2,
                                   padding='VALID',
                                   scope='Conv2d_1a_3x3')
        with tf.variable_scope('Branch_2'):
            branch_2 = slim.max_pool2d(net, [3, 3],
                                       stride=2,
                                       padding='VALID',
                                       scope='MaxPool_1a_3x3')
        net = tf.concat(axis=3, values=[branch_0, branch_1, branch_2])

    with tf.variable_scope('mix9'):
        with tf.variable_scope('Branch_0'):
            branch_0 = slim.conv2d(net, 320, [1, 1], scope='Conv2d_0a_1x1')
        with tf.variable_scope('Branch_1'):
            branch_1 = slim.conv2d(net, 384, [1, 1], scope='Conv2d_0a_1x1')
            branch_1 = tf.concat(axis=3,
                                 values=[
                                     slim.conv2d(branch_1,
                                                 384, [1, 3],
                                                 scope='Conv2d_0b_1x3'),
                                     slim.conv2d(branch_1,
                                                 384, [3, 1],
                                                 scope='Conv2d_0b_3x1')
                                 ])
        with tf.variable_scope('Branch_2'):
            branch_2 = slim.conv2d(net, 448, [1, 1], scope='Conv2d_0a_1x1')
            branch_2 = slim.conv2d(branch_2,
                                   384, [3, 3],
                                   scope='Conv2d_0b_3x3')
            branch_2 = tf.concat(axis=3,
                                 values=[
                                     slim.conv2d(branch_2,
                                                 384, [1, 3],
                                                 scope='Conv2d_0c_1x3'),
                                     slim.conv2d(branch_2,
                                                 384, [3, 1],
                                                 scope='Conv2d_0d_3x1')
                                 ])
        with tf.variable_scope('Branch_3'):
            branch_3 = slim.avg_pool2d(net, [3, 3],
                                       scope='AvgPool_0a_3x3',
                                       padding='SAME',
                                       stride=[1, 1])
            branch_3 = slim.conv2d(branch_3,
                                   192, [1, 1],
                                   scope='Conv2d_0b_1x1')
        net = tf.concat(axis=3,
                        values=[branch_0, branch_1, branch_2, branch_3])

    with tf.variable_scope('mix10'):
        with tf.variable_scope('Branch_0'):
            branch_0 = slim.conv2d(net, 320, [1, 1], scope='Conv2d_0a_1x1')
        with tf.variable_scope('Branch_1'):
            branch_1 = slim.conv2d(net, 384, [1, 1], scope='Conv2d_0a_1x1')
            branch_1 = tf.concat(axis=3,
                                 values=[
                                     slim.conv2d(branch_1,
                                                 384, [1, 3],
                                                 scope='Conv2d_0b_1x3'),
                                     slim.conv2d(branch_1,
                                                 384, [3, 1],
                                                 scope='Conv2d_0c_3x1')
                                 ])
        with tf.variable_scope('Branch_2'):
            branch_2 = slim.conv2d(net, 448, [1, 1], scope='Conv2d_0a_1x1')
            branch_2 = slim.conv2d(branch_2,
                                   384, [3, 3],
                                   scope='Conv2d_0b_3x3')
            branch_2 = tf.concat(axis=3,
                                 values=[
                                     slim.conv2d(branch_2,
                                                 384, [1, 3],
                                                 scope='Conv2d_0c_1x3'),
                                     slim.conv2d(branch_2,
                                                 384, [3, 1],
                                                 scope='Conv2d_0d_3x1')
                                 ])
        with tf.variable_scope('Branch_3'):
            branch_3 = slim.max_pool2d(net, [3, 3],
                                       scope='AvgPool_0a_3x3',
                                       padding='SAME',
                                       stride=[1, 1])
            branch_3 = slim.conv2d(branch_3,
                                   192, [1, 1],
                                   scope='Conv2d_0b_1x1')
        net = tf.concat(axis=3,
                        values=[branch_0, branch_1, branch_2, branch_3])

    with tf.name_scope('my_pool'):
        net = tf.nn.avg_pool(net,
                             ksize=[1, 8, 8, 1],
                             strides=[1, 1, 1, 1],
                             padding='VALID')
        net = tf.reshape(net, [batch, 2048])

    with tf.name_scope('second_layer'):
        with tf.name_scope('weights'):
            layer_weights = tf.Variable(tf.truncated_normal(
                [BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001),
                                        name='final_weights')
            variable_summaries(layer_weights)
        with tf.name_scope('biases'):
            layer_biases = tf.Variable(tf.zeros([class_count]),
                                       name='final_biases')
            variable_summaries(layer_biases)
        with tf.name_scope('Wx_plus_b'):
            logits = tf.matmul(net, layer_weights) + layer_biases
            tf.summary.histogram('pre_activations', logits)

    final_tensor = tf.nn.softmax(logits, name=final_tensor_name)
    tf.summary.histogram('activations', final_tensor)

    with tf.name_scope('cross_entropy'):
        cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
            labels=ground_truth_input, logits=logits)
        with tf.name_scope('total'):
            cross_entropy_mean = tf.reduce_mean(cross_entropy)
    tf.summary.scalar('cross_entropy', cross_entropy_mean)

    with tf.name_scope('train'):
        train_step = tf.train.GradientDescentOptimizer(
            FLAGS.learning_rate).minimize(cross_entropy_mean)

    return (train_step, cross_entropy_mean, bottleneck_input,
            ground_truth_input, final_tensor)