示例#1
0
文件: networks.py 项目: falconjhc/MLP
def network_model(features,
                  layer_channel_num_list,
                  device,
                  is_training,
                  initializer,
                  weight_decay,
                  weight_decay_rate,
                  reuse=False):

    network_name_prefix = "FullyConnected_%dLayers_Channels" % len(
        layer_channel_num_list)
    for ii in layer_channel_num_list:
        network_name_prefix = network_name_prefix + '_%d' % ii

    if is_training:
        print(print_separater)
        print("Training on %s" % network_name_prefix)
        print(print_separater)
        drop_v = 0.5
    else:
        drop_v = 0

    with tf.variable_scope(network_name_prefix):
        if reuse:
            tf.get_variable_scope().reuse_variables()

        for ii in range(len(layer_channel_num_list)):
            if ii == 0:
                input_feature = features
            else:
                input_feature = previous_output_feature
            if not ii == len(layer_channel_num_list) - 1:
                current_output = dropout(relu(
                    batch_norm(
                        x=fc(x=input_feature,
                             output_size=layer_channel_num_list[ii],
                             weight_decay_rate=weight_decay_rate,
                             weight_decay=weight_decay,
                             parameter_update_device=device,
                             initializer=initializer,
                             scope='fc%d' % (ii + 1),
                             name_prefix=network_name_prefix),
                        is_training=is_training,
                        scope='bn%d' % (ii + 1),
                        parameter_update_device=device,
                    )),
                                         drop_v=drop_v)
            else:
                current_output = fc(x=input_feature,
                                    output_size=1,
                                    weight_decay_rate=weight_decay_rate,
                                    weight_decay=weight_decay,
                                    parameter_update_device=device,
                                    initializer=initializer,
                                    scope='fc%d' % (ii + 1),
                                    name_prefix=network_name_prefix)
            previous_output_feature = current_output

        output = tf.nn.sigmoid(previous_output_feature)
    return previous_output_feature, output, network_name_prefix
示例#2
0
def seg_stack(net, mdl_im_feats, roi_im_feats, scope='ImSeg_Net'):
    mdl_im_bs = net.mdl_im_tensor_shape[1]
    with tf.variable_scope(scope):
        mdl_im_feats = uncollapse_dims(mdl_im_feats, net.batch_size, mdl_im_bs)
        cat_mdl_feats = mdl_im_feats[:, 0, :, :, :]
        for v in range(1, mdl_im_bs):
            cat_mdl_feats = tf.concat(
                [cat_mdl_feats, mdl_im_feats[:, v, :, :, :]], -1)
        stack = tf.concat([cat_mdl_feats, roi_im_feats], -1)
        net.seg_net[scope + '_stack'] = stack
        conv1 = conv2d('conv1',
                       stack,
                       2,
                       1024,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        net.seg_net[scope + '_conv1'] = conv1
        conv2 = conv2d('conv2',
                       conv1,
                       2,
                       512,
                       stride=2,
                       norm=net.norm,
                       mode=net.mode)
        net.seg_net[scope + '_conv2'] = conv2
        conv3 = conv2d('conv3',
                       conv2,
                       3,
                       256,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        net.seg_net[scope + '_conv3'] = conv3
        conv4 = conv2d('conv4',
                       conv3,
                       3,
                       256,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        conv4 = dropout(conv4, net.keep_prob)
        net.seg_net[scope + '_conv4'] = conv4
        deconv1 = deconv2d('deconv1',
                           conv4,
                           2,
                           256,
                           stride=2,
                           norm=net.norm,
                           mode=net.mode)
        net.seg_net[scope + '_deconv1'] = deconv1
        out = conv2d('out',
                     deconv1,
                     1,
                     1,
                     stride=1,
                     norm=net.norm,
                     mode=net.mode)
        net.seg_net[scope + '_out'] = out
    return out
示例#3
0
 def testCreateDropout(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height, width, 3), seed=1)
         output = ops.dropout(images)
         self.assertEquals(output.op.name, 'Dropout/dropout/mul')
         output.get_shape().assert_is_compatible_with(images.get_shape())
    def bottleneck_layer(self, x, scope):
        # print(x)
        with tf.name_scope(scope):
            #             x = conv_dropout(x, 4 * self.nf, ks=[3,3], s=1, scope)
            x = batch_norm(x, self.phase, scope + '_batch1')
            x = relu(x)
            x = conv2d(x, 4 * self.nf, ks=[1, 1], s=1, name=scope + '_conv1')
            x = dropout(x, self.dropout_rate, self.phase)

            x = batch_norm(x, self.phase, scope + '_batch2')
            x = relu(x)
            x = conv2d(x, self.nf, ks=[3, 3], s=1, name=scope + '_conv2')
            x = dropout(x, self.dropout_rate, self.phase)

            # print(x)

            return x
示例#5
0
 def testCreateDropoutNoTraining(self):
     height, width = 3, 3
     with self.test_session():
         images = tf.random_uniform((5, height, width, 3),
                                    seed=1,
                                    name='images')
         output = ops.dropout(images, is_training=False)
         self.assertEquals(output, images)
    def transition_layer(self, x, scope):
        with tf.name_scope(scope):
            x = batch_norm(x, self.phase, scope + '_batch1')
            x = relu(x)
            x = conv2d(x, self.nf, ks=[1, 1], s=1, name=scope + '_conv1')
            x = dropout(x, self.dropout_rate, self.phase)

            x = average_pooling(x, ks=[2, 2], s=2)

            return x
def AlexNet(X, keep_prob, is_train):
    net = conv_2d(X, 7, 2, 96, 'CONV1', trainable=True)
    net = lrn(net)
    net = max_pool(net, 3, 2, 'MaxPool1')
    net = conv_2d(net, 5, 2, 256, 'CONV2', trainable=True)
    net = lrn(net)
    net = max_pool(net, 3, 2, 'MaxPool2')
    net = conv_2d(net, 3, 1, 384, 'CONV3', trainable=True)
    net = conv_2d(net, 3, 1, 384, 'CONV4', trainable=True)
    net = conv_2d(net, 3, 1, 256, 'CONV5', trainable=True)
    net = max_pool(net, 3, 2, 'MaxPool3')
    layer_flat = flatten_layer(net)
    net = fc_layer(layer_flat, 512, 'FC1', trainable=True, use_relu=True)
    net = dropout(net, keep_prob)
    return net
def AlexNet_target_task(X, keep_prob, num_cls):
    net = conv_2d(X, 7, 2, 96, 'CONV1', trainable=False)
    net = lrn(net)
    net = max_pool(net, 3, 2, 'MaxPool1')
    net = conv_2d(net, 5, 2, 256, 'CONV2', trainable=False)
    net = lrn(net)
    net = max_pool(net, 3, 2, 'MaxPool2')
    net = conv_2d(net, 3, 1, 384, 'CONV3', trainable=False)
    net = conv_2d(net, 3, 1, 384, 'CONV4', trainable=False)
    net = conv_2d(net, 3, 1, 256, 'CONV5', trainable=False)
    net = max_pool(net, 3, 2, 'MaxPool3')
    layer_flat = flatten_layer(net)
    net = fc_layer(layer_flat, 512, 'FC_1', trainable=True, use_relu=True)
    net = dropout(net, keep_prob)
    net = fc_layer(net, num_cls, 'FC_2', trainable=True, use_relu=False)
    return net
def classifier(images, options, reuse=False, name='classifier'):
    x = relu(
        batch_norm(conv2d(images, options.nf, ks=5, s=2, name='conv1'),
                   options.phase, 'bn1'))  # 32*32*nf
    x = relu(
        batch_norm(conv2d(x, 2 * options.nf, ks=5, s=2, name='conv2'),
                   options.phase, 'bn2'))  # 16*16*(2*nf)
    x = relu(
        batch_norm(conv2d(x, 4 * options.nf, ks=5, s=2, name='conv3'),
                   options.phase, 'bn3'))  # 8*8*(4*nf)
    #    x = relu(batch_norm(conv2d(x, 4*options.nf, ks=5, s=2, name='conv4'), options.phase, 'bn4')) # 4*4*(4*nf)

    x = linear(tf.reshape(x, [options.batch_size, 4 * 4 * (4 * options.nf)]),
               100,
               name='linear1')
    x = dropout(x, 0.5, options.phase)
    x = linear(x, options.label_n, name='linear2')
    return tf.nn.softmax(x)
示例#10
0
def inception_v3(inputs,
                 dropout_keep_prob=0.8,
                 num_classes=1000,
                 is_training=True,
                 restore_logits=True,
                 scope=''):
  """Latest Inception from http://arxiv.org/abs/1512.00567.

    "Rethinking the Inception Architecture for Computer Vision"

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

  Args:
    inputs: a tensor of size [batch_size, height, width, channels].
    dropout_keep_prob: dropout keep_prob.
    num_classes: number of predicted classes.
    is_training: whether is training or not.
    restore_logits: whether or not the logits layers should be restored.
      Useful for fine-tuning a model with different num_classes.
    scope: Optional scope for name_scope.

  Returns:
    a list containing 'logits', 'aux_logits' Tensors.
  """
  # end_points will collect relevant activations for external use, for example
  # summaries or losses.
  end_points = {}
  with tf.name_scope(scope, 'inception_v3', [inputs]):
    with scopes.arg_scope([ops.conv2d, ops.fc, ops.batch_norm, ops.dropout],
                          is_training=is_training):
      with scopes.arg_scope([ops.conv2d, ops.max_pool, ops.avg_pool],
                            stride=1, padding='VALID'):
        # 299 x 299 x 3
        end_points['conv0'] = ops.conv2d(inputs, 32, [3, 3], stride=2,
                                         scope='conv0')
        # 149 x 149 x 32
        end_points['conv1'] = ops.conv2d(end_points['conv0'], 32, [3, 3],
                                         scope='conv1')
        # 147 x 147 x 32
        end_points['conv2'] = ops.conv2d(end_points['conv1'], 64, [3, 3],
                                         padding='SAME', scope='conv2')
        # 147 x 147 x 64
        end_points['pool1'] = ops.max_pool(end_points['conv2'], [3, 3],
                                           stride=2, scope='pool1')
        # 73 x 73 x 64
        end_points['conv3'] = ops.conv2d(end_points['pool1'], 80, [1, 1],
                                         scope='conv3')
        # 73 x 73 x 80.
        end_points['conv4'] = ops.conv2d(end_points['conv3'], 192, [3, 3],
                                         scope='conv4')
        # 71 x 71 x 192.
        end_points['pool2'] = ops.max_pool(end_points['conv4'], [3, 3],
                                           stride=2, scope='pool2')
        # 35 x 35 x 192.
        net = end_points['pool2']
      # Inception blocks
      with scopes.arg_scope([ops.conv2d, ops.max_pool, ops.avg_pool],
                            stride=1, padding='SAME'):
        # mixed: 35 x 35 x 256.
        with tf.variable_scope('mixed_35x35x256a'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 64, [1, 1])
          with tf.variable_scope('branch5x5'):
            branch5x5 = ops.conv2d(net, 48, [1, 1])
            branch5x5 = ops.conv2d(branch5x5, 64, [5, 5])
          with tf.variable_scope('branch3x3dbl'):
            branch3x3dbl = ops.conv2d(net, 64, [1, 1])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 32, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch5x5, branch3x3dbl, branch_pool])
          end_points['mixed_35x35x256a'] = net
        # mixed_1: 35 x 35 x 288.
        with tf.variable_scope('mixed_35x35x288a'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 64, [1, 1])
          with tf.variable_scope('branch5x5'):
            branch5x5 = ops.conv2d(net, 48, [1, 1])
            branch5x5 = ops.conv2d(branch5x5, 64, [5, 5])
          with tf.variable_scope('branch3x3dbl'):
            branch3x3dbl = ops.conv2d(net, 64, [1, 1])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 64, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch5x5, branch3x3dbl, branch_pool])
          end_points['mixed_35x35x288a'] = net
        # mixed_2: 35 x 35 x 288.
        with tf.variable_scope('mixed_35x35x288b'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 64, [1, 1])
          with tf.variable_scope('branch5x5'):
            branch5x5 = ops.conv2d(net, 48, [1, 1])
            branch5x5 = ops.conv2d(branch5x5, 64, [5, 5])
          with tf.variable_scope('branch3x3dbl'):
            branch3x3dbl = ops.conv2d(net, 64, [1, 1])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 64, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch5x5, branch3x3dbl, branch_pool])
          end_points['mixed_35x35x288b'] = net
        # mixed_3: 17 x 17 x 768.
        with tf.variable_scope('mixed_17x17x768a'):
          with tf.variable_scope('branch3x3'):
            branch3x3 = ops.conv2d(net, 384, [3, 3], stride=2, padding='VALID')
          with tf.variable_scope('branch3x3dbl'):
            branch3x3dbl = ops.conv2d(net, 64, [1, 1])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 96, [3, 3],
                                      stride=2, padding='VALID')
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.max_pool(net, [3, 3], stride=2, padding='VALID')
          net = tf.concat(axis=3, values=[branch3x3, branch3x3dbl, branch_pool])
          end_points['mixed_17x17x768a'] = net
        # mixed4: 17 x 17 x 768.
        with tf.variable_scope('mixed_17x17x768b'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 192, [1, 1])
          with tf.variable_scope('branch7x7'):
            branch7x7 = ops.conv2d(net, 128, [1, 1])
            branch7x7 = ops.conv2d(branch7x7, 128, [1, 7])
            branch7x7 = ops.conv2d(branch7x7, 192, [7, 1])
          with tf.variable_scope('branch7x7dbl'):
            branch7x7dbl = ops.conv2d(net, 128, [1, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 128, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 128, [1, 7])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 128, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [1, 7])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 192, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch7x7, branch7x7dbl, branch_pool])
          end_points['mixed_17x17x768b'] = net
        # mixed_5: 17 x 17 x 768.
        with tf.variable_scope('mixed_17x17x768c'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 192, [1, 1])
          with tf.variable_scope('branch7x7'):
            branch7x7 = ops.conv2d(net, 160, [1, 1])
            branch7x7 = ops.conv2d(branch7x7, 160, [1, 7])
            branch7x7 = ops.conv2d(branch7x7, 192, [7, 1])
          with tf.variable_scope('branch7x7dbl'):
            branch7x7dbl = ops.conv2d(net, 160, [1, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 160, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 160, [1, 7])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 160, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [1, 7])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 192, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch7x7, branch7x7dbl, branch_pool])
          end_points['mixed_17x17x768c'] = net
        # mixed_6: 17 x 17 x 768.
        with tf.variable_scope('mixed_17x17x768d'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 192, [1, 1])
          with tf.variable_scope('branch7x7'):
            branch7x7 = ops.conv2d(net, 160, [1, 1])
            branch7x7 = ops.conv2d(branch7x7, 160, [1, 7])
            branch7x7 = ops.conv2d(branch7x7, 192, [7, 1])
          with tf.variable_scope('branch7x7dbl'):
            branch7x7dbl = ops.conv2d(net, 160, [1, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 160, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 160, [1, 7])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 160, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [1, 7])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 192, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch7x7, branch7x7dbl, branch_pool])
          end_points['mixed_17x17x768d'] = net
        # mixed_7: 17 x 17 x 768.
        with tf.variable_scope('mixed_17x17x768e'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 192, [1, 1])
          with tf.variable_scope('branch7x7'):
            branch7x7 = ops.conv2d(net, 192, [1, 1])
            branch7x7 = ops.conv2d(branch7x7, 192, [1, 7])
            branch7x7 = ops.conv2d(branch7x7, 192, [7, 1])
          with tf.variable_scope('branch7x7dbl'):
            branch7x7dbl = ops.conv2d(net, 192, [1, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [1, 7])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [7, 1])
            branch7x7dbl = ops.conv2d(branch7x7dbl, 192, [1, 7])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 192, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch7x7, branch7x7dbl, branch_pool])
          end_points['mixed_17x17x768e'] = net
        # Auxiliary Head logits
        aux_logits = tf.identity(end_points['mixed_17x17x768e'])
        with tf.variable_scope('aux_logits'):
          aux_logits = ops.avg_pool(aux_logits, [5, 5], stride=3,
                                    padding='VALID')
          aux_logits = ops.conv2d(aux_logits, 128, [1, 1], scope='proj')
          # Shape of feature map before the final layer.
          shape = aux_logits.get_shape()
          aux_logits = ops.conv2d(aux_logits, 768, shape[1:3], stddev=0.01,
                                  padding='VALID')
          aux_logits = ops.flatten(aux_logits)
          aux_logits = ops.fc(aux_logits, num_classes, activation=None,
                              stddev=0.001, restore=restore_logits)
          end_points['aux_logits'] = aux_logits
        # mixed_8: 8 x 8 x 1280.
        # Note that the scope below is not changed to not void previous
        # checkpoints.
        # (TODO) Fix the scope when appropriate.
        with tf.variable_scope('mixed_17x17x1280a'):
          with tf.variable_scope('branch3x3'):
            branch3x3 = ops.conv2d(net, 192, [1, 1])
            branch3x3 = ops.conv2d(branch3x3, 320, [3, 3], stride=2,
                                   padding='VALID')
          with tf.variable_scope('branch7x7x3'):
            branch7x7x3 = ops.conv2d(net, 192, [1, 1])
            branch7x7x3 = ops.conv2d(branch7x7x3, 192, [1, 7])
            branch7x7x3 = ops.conv2d(branch7x7x3, 192, [7, 1])
            branch7x7x3 = ops.conv2d(branch7x7x3, 192, [3, 3],
                                     stride=2, padding='VALID')
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.max_pool(net, [3, 3], stride=2, padding='VALID')
          net = tf.concat(axis=3, values=[branch3x3, branch7x7x3, branch_pool])
          end_points['mixed_17x17x1280a'] = net
        # mixed_9: 8 x 8 x 2048.
        with tf.variable_scope('mixed_8x8x2048a'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 320, [1, 1])
          with tf.variable_scope('branch3x3'):
            branch3x3 = ops.conv2d(net, 384, [1, 1])
            branch3x3 = tf.concat(axis=3, values=[ops.conv2d(branch3x3, 384, [1, 3]),
                                                  ops.conv2d(branch3x3, 384, [3, 1])])
          with tf.variable_scope('branch3x3dbl'):
            branch3x3dbl = ops.conv2d(net, 448, [1, 1])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 384, [3, 3])
            branch3x3dbl = tf.concat(axis=3, values=[ops.conv2d(branch3x3dbl, 384, [1, 3]),
                                                     ops.conv2d(branch3x3dbl, 384, [3, 1])])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 192, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch3x3, branch3x3dbl, branch_pool])
          end_points['mixed_8x8x2048a'] = net
        # mixed_10: 8 x 8 x 2048.
        with tf.variable_scope('mixed_8x8x2048b'):
          with tf.variable_scope('branch1x1'):
            branch1x1 = ops.conv2d(net, 320, [1, 1])
          with tf.variable_scope('branch3x3'):
            branch3x3 = ops.conv2d(net, 384, [1, 1])
            branch3x3 = tf.concat(axis=3, values=[ops.conv2d(branch3x3, 384, [1, 3]),
                                                  ops.conv2d(branch3x3, 384, [3, 1])])
          with tf.variable_scope('branch3x3dbl'):
            branch3x3dbl = ops.conv2d(net, 448, [1, 1])
            branch3x3dbl = ops.conv2d(branch3x3dbl, 384, [3, 3])
            branch3x3dbl = tf.concat(axis=3, values=[ops.conv2d(branch3x3dbl, 384, [1, 3]),
                                                     ops.conv2d(branch3x3dbl, 384, [3, 1])])
          with tf.variable_scope('branch_pool'):
            branch_pool = ops.avg_pool(net, [3, 3])
            branch_pool = ops.conv2d(branch_pool, 192, [1, 1])
          net = tf.concat(axis=3, values=[branch1x1, branch3x3, branch3x3dbl, branch_pool])
          end_points['mixed_8x8x2048b'] = net
        # Final pooling and prediction
        with tf.variable_scope('logits'):
          shape = net.get_shape()
          net = ops.avg_pool(net, shape[1:3], padding='VALID', scope='pool')
          # 1 x 1 x 2048
          net = ops.dropout(net, dropout_keep_prob, scope='dropout')
          net = ops.flatten(net, scope='flatten')
          # 2048
          logits = ops.fc(net, num_classes, activation=None, scope='logits',
                          restore=restore_logits)
          # 1000
          end_points['logits'] = logits
          end_points['predictions'] = tf.nn.softmax(logits, name='predictions')
      return logits, end_points
def create_network(X, h, keep_prob, numClasses):
    num_channels = X.get_shape().as_list()[-1]
    res1 = new_conv_layer(inputs=X,
                          layer_name='res1',
                          stride=2,
                          num_inChannel=num_channels,
                          filter_size=4,
                          num_filters=32,
                          batch_norm=True,
                          use_relu=True)

    #res1 = max_pool(res1, ksize=2, stride=2, name='res1_max_pool')
    print('---------------------')
    print('Res1')
    print(res1.get_shape())
    print('---------------------')
    # Res2
    with tf.variable_scope('Res2'):
        res2a = bottleneck_block(res1,
                                 32,
                                 block_name='res2a',
                                 s1=1,
                                 k1=1,
                                 nf1=32,
                                 name1='res2a_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=32,
                                 name2='res2a_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=64,
                                 name3='res2a_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res2a_branch1',
                                 first_block=True)
        print('Res2a')
        print(res2a.get_shape())
        print('---------------------')
        res2b = bottleneck_block(res2a,
                                 64,
                                 block_name='res2b',
                                 s1=1,
                                 k1=1,
                                 nf1=32,
                                 name1='res2b_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=32,
                                 name2='res2b_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=64,
                                 name3='res2b_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res2b_branch1',
                                 first_block=False)
        print('Res2b')
        print(res2b.get_shape())
        print('---------------------')
        res2c = bottleneck_block(res2b,
                                 64,
                                 block_name='res2c',
                                 s1=1,
                                 k1=1,
                                 nf1=32,
                                 name1='res2c_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=32,
                                 name2='res2c_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=64,
                                 name3='res2c_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res2c_branch1',
                                 first_block=False)
        print('Res2c')
        print(res2c.get_shape())
        print('---------------------')

    # Res3
    with tf.variable_scope('Res3'):
        res3a = bottleneck_block(res2c,
                                 64,
                                 block_name='res3a',
                                 s1=2,
                                 k1=1,
                                 nf1=48,
                                 name1='res3a_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=48,
                                 name2='res3a_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=128,
                                 name3='res3a_branch2c',
                                 s4=2,
                                 k4=1,
                                 name4='res3a_branch1',
                                 first_block=True)
        print('Res3a')
        print(res3a.get_shape())
        print('---------------------')
        res3b = bottleneck_block(res3a,
                                 128,
                                 block_name='res3b',
                                 s1=1,
                                 k1=1,
                                 nf1=48,
                                 name1='res3b_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=48,
                                 name2='res3b_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=128,
                                 name3='res3b_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res2b_branch1',
                                 first_block=False)
        print('Res3b')
        print(res3b.get_shape())
        print('---------------------')
        res3c = bottleneck_block(res3b,
                                 128,
                                 block_name='res3c',
                                 s1=1,
                                 k1=1,
                                 nf1=48,
                                 name1='res3c_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=48,
                                 name2='res3c_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=128,
                                 name3='res3c_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res3c_branch1',
                                 first_block=False)
        print('Res3c')
        print(res3c.get_shape())
        print('---------------------')
        res3d = bottleneck_block(res3c,
                                 128,
                                 block_name='res3d',
                                 s1=1,
                                 k1=1,
                                 nf1=48,
                                 name1='res3d_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=48,
                                 name2='res3d_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=128,
                                 name3='res3d_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res3d_branch1',
                                 first_block=False)
        print('Res3d')
        print(res3d.get_shape())
        print('---------------------')

    # Res4
    with tf.variable_scope('Res4'):
        res4a = bottleneck_block(res3d,
                                 128,
                                 block_name='res4a',
                                 s1=2,
                                 k1=1,
                                 nf1=64,
                                 name1='res4a_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=64,
                                 name2='res4a_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=256,
                                 name3='res4a_branch2c',
                                 s4=2,
                                 k4=1,
                                 name4='res4a_branch1',
                                 first_block=True)
        print('---------------------')
        print('Res4a')
        print(res4a.get_shape())
        print('---------------------')
        res4b = bottleneck_block(res4a,
                                 256,
                                 block_name='res4b',
                                 s1=1,
                                 k1=1,
                                 nf1=64,
                                 name1='res4b_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=64,
                                 name2='res4b_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=256,
                                 name3='res4b_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res4b_branch1',
                                 first_block=False)
        print('Res4b')
        print(res4b.get_shape())
        print('---------------------')
        res4c = bottleneck_block(res4b,
                                 256,
                                 block_name='res4c',
                                 s1=1,
                                 k1=1,
                                 nf1=64,
                                 name1='res4c_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=64,
                                 name2='res4c_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=256,
                                 name3='res4c_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res4c_branch1',
                                 first_block=False)
        print('Res4c')
        print(res4c.get_shape())
        print('---------------------')
        res4d = bottleneck_block(res4c,
                                 256,
                                 block_name='res4d',
                                 s1=1,
                                 k1=1,
                                 nf1=64,
                                 name1='res4d_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=64,
                                 name2='res4d_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=256,
                                 name3='res4d_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res4d_branch1',
                                 first_block=False)
        print('Res4d')
        print(res4d.get_shape())
        print('---------------------')
        res4e = bottleneck_block(res4d,
                                 256,
                                 block_name='res4e',
                                 s1=1,
                                 k1=1,
                                 nf1=64,
                                 name1='res4e_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=64,
                                 name2='res4e_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=256,
                                 name3='res4e_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res4e_branch1',
                                 first_block=False)
        print('Res4e')
        print(res4e.get_shape())
        print('---------------------')
        res4f = bottleneck_block(res4e,
                                 256,
                                 block_name='res4f',
                                 s1=1,
                                 k1=1,
                                 nf1=64,
                                 name1='res4f_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=64,
                                 name2='res4f_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=256,
                                 name3='res4f_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res4f_branch1',
                                 first_block=False)
        print('Res4f')
        print(res4f.get_shape())
        print('---------------------')

    # Res5
    with tf.variable_scope('Res5'):
        res5a = bottleneck_block(res4f,
                                 256,
                                 block_name='res5a',
                                 s1=1,
                                 k1=1,
                                 nf1=128,
                                 name1='res5a_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=128,
                                 name2='res5a_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=512,
                                 name3='res5a_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res5a_branch1',
                                 first_block=True)
        print('---------------------')
        print('Res5a')
        print(res5a.get_shape())
        print('---------------------')
        res5b = bottleneck_block(res5a,
                                 512,
                                 block_name='res5b',
                                 s1=1,
                                 k1=1,
                                 nf1=128,
                                 name1='res5b_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=128,
                                 name2='res5b_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=512,
                                 name3='res5b_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res5b_branch1',
                                 first_block=False)
        print('Res5b')
        print(res5b.get_shape())
        print('---------------------')
        res5c = bottleneck_block(res5b,
                                 512,
                                 block_name='res5c',
                                 s1=1,
                                 k1=1,
                                 nf1=128,
                                 name1='res5c_branch2a',
                                 s2=1,
                                 k2=3,
                                 nf2=128,
                                 name2='res5c_branch2b',
                                 s3=1,
                                 k3=1,
                                 nf3=512,
                                 name3='res5c_branch2c',
                                 s4=1,
                                 k4=1,
                                 name4='res5c_branch1',
                                 first_block=False)
        print('Res5c')
        print(res5c.get_shape())

        res5c = avg_pool(res5c, ksize=4, stride=1, name='res5_avg_pool')
        print('---------------------')
        print('Res5c after AVG_POOL')
        print(res5c.get_shape())
        print('---------------------')

    net_flatten, _ = flatten_layer(res5c)
    print('---------------------')
    print('Matrix dimension to the first FC layer')
    print(net_flatten.get_shape())
    print('---------------------')
    net = fc_layer(net_flatten,
                   h,
                   'FC1',
                   batch_norm=True,
                   add_reg=True,
                   use_relu=True)
    net = dropout(net, keep_prob)
    net = fc_layer(net,
                   numClasses,
                   'FC2',
                   batch_norm=True,
                   add_reg=True,
                   use_relu=False)

    return net
示例#12
0
    def _generator(z, zy):
        with tf.variable_scope(params.gen_scope):
            imh, imw = params.dataset.image_size, params.dataset.image_size

            hidden_layers_num = 3
            imdiv = 2**hidden_layers_num

            h0 = tf.concat([z, zy], axis=1)

            h1 = ops.fully_connected(h0, (imh // imdiv) * (imw // imdiv) *
                                     params.gen_filters * 4, 'h1')
            if params.use_batch_norm:
                h1 = ops.batch_norm(h1, name='bn1')
            h1 = tf.reshape(
                h1, [-1, imh // imdiv, imw // imdiv, params.gen_filters * 4])
            h1 = ops.lrelu(h1)
            h1 = ops.dropout(h1,
                             training=training,
                             keep=params.gen_keep_dropout,
                             name='dropout1')
            h1 = ops.concat(h1, zy)

            h2 = ops.deconvolution(h1,
                                   params.gen_filters_size,
                                   params.gen_filters * 2,
                                   name='h2')
            if params.use_batch_norm:
                h2 = ops.batch_norm(h2, name='bn2')
            h2 = ops.lrelu(h2)
            h2 = ops.dropout(h2,
                             training=training,
                             keep=params.gen_keep_dropout,
                             name='dropout2')
            h2 = ops.concat(h2, zy)

            h3_pure = ops.deconvolution(h2,
                                        params.gen_filters_size,
                                        params.gen_filters,
                                        name='h3')
            h3 = h3_pure
            if params.use_batch_norm:
                h3 = ops.batch_norm(h3, name='bn3')
            h3 = ops.lrelu(h3)
            h3 = ops.dropout(h3,
                             training=training,
                             keep=params.gen_keep_dropout,
                             name='dropout3')
            h3 = ops.concat(h3, zy)

            h4 = ops.deconvolution(h3,
                                   params.gen_filters_size,
                                   params.dataset.channels_size,
                                   name='h4')
            return tf.nn.tanh(h4), {
                'h0': h0,
                'h1': h1,
                'h2': h2,
                'h3': h3,
                'h3_pure': h3_pure,
                'h4': h4
            }
示例#13
0
def im_vgg16(net, ims):
    net.im_net = {}
    bs, h, w, ch = tf_static_shape(ims)
    with tf.variable_scope('ImNet_UNet', reuse=tf.AUTO_REUSE):
        #VGG16 layers
        conv1_1 = conv2d('conv1_1', ims, 3, 64, mode=net.mode, act=None)
        net.im_net['conv1_1'] = conv1_1
        conv1_2 = conv2d('conv1_2', conv1_1, 3, 64, mode=net.mode)
        net.im_net['conv1_2'] = conv1_2
        pool1 = tf.layers.max_pooling2d(conv1_2,
                                        2,
                                        2,
                                        padding='same',
                                        name='pool1')
        conv2_1 = conv2d('conv2_1', pool1, 3, 128, mode=net.mode)
        net.im_net['conv2_1'] = conv2_1
        conv2_2 = conv2d('conv2_2', conv2_1, 3, 128, mode=net.mode)
        net.im_net['conv2_2'] = conv2_2
        pool2 = tf.layers.max_pooling2d(conv2_2,
                                        2,
                                        2,
                                        padding='same',
                                        name='pool2')
        net.im_net['pool2'] = pool2
        conv3_1 = conv2d('conv3_1', pool2, 3, 256, mode=net.mode)
        net.im_net['conv3_1'] = conv3_1
        conv3_2 = conv2d('conv3_2', conv3_1, 3, 256, mode=net.mode)
        net.im_net['conv3_2'] = conv3_2
        conv3_3 = conv2d('conv3_3', conv3_2, 3, 256, mode=net.mode)
        net.im_net['conv3_3'] = conv3_3
        pool3 = tf.layers.max_pooling2d(conv3_3,
                                        2,
                                        2,
                                        padding='same',
                                        name='pool3')
        net.im_net['pool3'] = pool3
        conv4_1 = conv2d('conv4_1', pool3, 3, 512, mode=net.mode)
        net.im_net['conv4_1'] = conv4_1
        conv4_2 = conv2d('conv4_2', conv4_1, 3, 512, mode=net.mode)
        net.im_net['conv4_2'] = conv4_2
        conv4_3 = conv2d('conv4_3', conv4_2, 3, 512, mode=net.mode)
        net.im_net['conv4_3'] = conv4_3
        pool4 = tf.layers.max_pooling2d(conv4_3,
                                        2,
                                        2,
                                        padding='same',
                                        name='pool4')
        net.im_net['pool4'] = pool4
        conv5_1 = conv2d('conv5_1', pool4, 3, 512, mode=net.mode)
        net.im_net['conv5_1'] = conv5_1
        conv5_2 = conv2d('conv5_2', conv5_1, 3, 512, mode=net.mode)
        net.im_net['conv5_2'] = conv5_2
        conv5_3 = conv2d('conv5_3', conv5_2, 3, 512, mode=net.mode)
        net.im_net['conv5_3'] = conv5_3
        #Deconv layers
        feat_conv5 = conv2d('feat_conv5',
                            conv5_3,
                            1,
                            64,
                            norm=net.norm,
                            mode=net.mode)
        net.im_net['feat_conv5'] = feat_conv5
        upfeat_conv5 = deconv_pcnn(feat_conv5,
                                   4,
                                   4,
                                   64,
                                   2,
                                   2,
                                   name='upfeat_conv5',
                                   trainable=False)
        # upfeat_conv5 = deconv2d('upfeat_conv5', conv5_3, 4, 64, stride=2, padding="SAME", norm=net.norm, mode=net.mode)
        net.im_net['upfeat_conv5'] = upfeat_conv5
        feat_conv4 = conv2d('feat_conv4',
                            conv4_3,
                            1,
                            64,
                            norm=net.norm,
                            mode=net.mode)
        net.im_net['feat_conv4'] = feat_conv4
        add_feat = tf.add_n([upfeat_conv5, feat_conv4], name='add_feat')
        add_feat = dropout(add_feat, net.keep_prob)
        net.im_net['add_feat'] = add_feat
        upfeat = deconv_pcnn(add_feat,
                             16,
                             16,
                             64,
                             8,
                             8,
                             name='upfeat',
                             trainable=False)
        # upfeat = deconv2d('upfeat', add_feat, 16, 64, stride=8, padding="SAME", norm=net.norm, mode=net.mode)
        net.im_net['upfeat'] = upfeat
    return upfeat
示例#14
0
def quat_inception(net, vp_mask):
    net.quat_net = {}
    with tf.variable_scope('Viewpoint_Net', reuse=tf.AUTO_REUSE):
        vp_mask = tf.expand_dims(vp_mask, -1)
        # Output (bs, 64, 64, ch)
        conv1 = conv2d('conv1',
                       vp_mask,
                       3,
                       256,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode,
                       act=None)
        net.quat_net['conv1'] = conv1
        conv2 = conv2d('conv2',
                       conv1,
                       1,
                       128,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        net.quat_net['conv2'] = conv2
        conv3 = conv2d('conv3',
                       conv2,
                       1,
                       128,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        net.quat_net['conv3'] = conv3
        # Output (bs, 32, 32, ch)
        pool1 = tf.layers.max_pooling2d(conv3,
                                        2,
                                        2,
                                        padding='same',
                                        name='pool1')
        net.quat_net['pool1'] = pool1
        conv4 = conv2d('conv4',
                       pool1,
                       3,
                       512,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        net.quat_net['conv4'] = conv4
        conv5 = conv2d('conv5',
                       conv4,
                       1,
                       256,
                       stride=1,
                       norm=net.norm,
                       mode=net.mode)
        conv5 = dropout(conv5, net.keep_prob)
        net.quat_net['conv5'] = conv5
        # Output (bs, 16, 16, ch)
        pool2 = tf.layers.max_pooling2d(conv5,
                                        2,
                                        2,
                                        padding='same',
                                        name='pool2')
        pool2 = dropout(pool2, net.keep_prob)
        net.quat_net['pool2'] = pool2
        fc1 = fully_connected('fc1', pool2, 1024)
        net.quat_net['fc1'] = fc1
        fc2 = fully_connected('fc2', fc1, 4 * net.num_classes)
        # fc2 = tf.tanh(fc2)
        net.quat_net['fc2'] = fc2
        out = fc2
        net.quat_net['out'] = out
    return out
示例#15
0
def quat_res(net, vp_mask):
    net.quat_net = {}
    with tf.variable_scope('Quat_Net', reuse=tf.AUTO_REUSE):
        vp_mask = tf.expand_dims(vp_mask, -1)
        # Output (bs, 32, 32, 64)
        conv1 = conv2d('conv1',
                       vp_mask,
                       7,
                       64,
                       stride=2,
                       norm=net.norm,
                       mode=net.mode,
                       act=None)
        net.quat_net['conv1'] = conv1
        # Output (bs, 16, 16, 64)
        pool1 = tf.layers.max_pooling2d(conv1,
                                        3,
                                        2,
                                        padding='same',
                                        name='pool1')
        net.quat_net['pool1'] = pool1

        # Output (bs, 16, 16, 64)
        conv2_1a = conv2d('conv2_1a',
                          pool1,
                          3,
                          64,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv2_1a'] = conv2_1a
        conv2_2a = conv2d('conv2_2a',
                          conv2_1a,
                          3,
                          64,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv2_2a'] = conv2_2a
        res_2a = tf.add_n([conv2_2a, pool1], name='res_2a')

        conv2_1b = conv2d('conv2_1b',
                          res_2a,
                          3,
                          64,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv2_1b'] = conv2_1b
        conv2_2b = conv2d('conv2_2b',
                          conv2_1b,
                          3,
                          64,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv2_2b'] = conv2_2b
        res_2b = tf.add_n([conv2_2b, res_2a], name='res_2b')

        # Output (bs, 8, 8, 128)
        conv3_1a = conv2d('conv3_1a',
                          res_2b,
                          3,
                          128,
                          stride=2,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv3_1a'] = conv3_1a
        conv3_2a = conv2d('conv3_2a',
                          conv3_1a,
                          3,
                          128,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv3_2a'] = conv3_2a
        res_2b_skip = conv2d('res_2b_skip',
                             res_2b,
                             1,
                             128,
                             stride=2,
                             norm=net.norm,
                             mode=net.mode)
        res_3a = tf.add_n([conv3_2a, res_2b_skip], name='res_3a')

        conv3_1b = conv2d('conv3_1b',
                          res_3a,
                          3,
                          128,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv3_1b'] = conv3_1b
        conv3_2b = conv2d('conv3_2b',
                          conv3_1b,
                          3,
                          128,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv3_2b'] = conv3_2b
        res_3b = tf.add_n([conv3_2b, res_3a], name='res_3b')

        # Output (bs, 4, 4, 256)
        conv4_1a = conv2d('conv4_1a',
                          res_3b,
                          3,
                          256,
                          stride=2,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv4_1a'] = conv4_1a
        conv4_2a = conv2d('conv4_2a',
                          conv4_1a,
                          3,
                          256,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv4_2a'] = conv4_2a
        res_3b_skip = conv2d('res_3b_skip',
                             res_3b,
                             1,
                             256,
                             stride=2,
                             norm=net.norm,
                             mode=net.mode)
        res_4a = tf.add_n([conv4_2a, res_3b_skip], name='res_4a')

        conv4_1b = conv2d('conv4_1b',
                          res_4a,
                          3,
                          256,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv4_1b'] = conv4_1b
        conv4_2b = conv2d('conv4_2b',
                          conv4_1b,
                          3,
                          256,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv4_2b'] = conv4_2b
        res_4b = tf.add_n([conv4_2b, res_4a], name='res_4b')

        # Output (bs, 2, 2, 512)
        conv5_1a = conv2d('con5_1a',
                          res_4b,
                          3,
                          512,
                          stride=2,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['con5_1a'] = conv5_1a
        conv5_2a = conv2d('con5_2a',
                          conv5_1a,
                          3,
                          512,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['con5_2a'] = conv5_2a
        res_4b_skip = conv2d('res_4b_skip',
                             res_4b,
                             1,
                             512,
                             stride=2,
                             norm=net.norm,
                             mode=net.mode)
        res_5a = tf.add_n([conv5_2a, res_4b_skip], name='res_5a')

        conv5_1b = conv2d('conv5_1b',
                          res_5a,
                          3,
                          512,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv5_1b'] = conv5_1b
        conv5_2b = conv2d('conv5_2b',
                          conv5_1b,
                          3,
                          512,
                          stride=1,
                          norm=net.norm,
                          mode=net.mode)
        net.quat_net['conv5_2b'] = conv5_2b
        res_5b = tf.add_n([conv5_2b, res_5a], name='res_5b')
        res_5b = dropout(res_5b, net.keep_prob)

        # Output (bs, 4*num_classes)
        fc1 = fully_connected('fc1', res_5b, 512)
        net.quat_net['fc1'] = fc1
        fc2 = fully_connected('fc2', fc1, 4 * net.num_classes)
        net.quat_net['fc2'] = fc2
        # out = tf.tanh(fc2)
        out = fc2
        net.quat_net['out'] = out

    return out
示例#16
0
    def build(self):
        params = self.params

        # Placeholders
        features = tf.placeholder('int32',
                                  shape=[params.batch_size],
                                  name='features')
        dates = tf.placeholder('int32',
                               shape=[params.batch_size],
                               name='dates')
        times = tf.placeholder('int32',
                               shape=[params.batch_size],
                               name='times')
        wait_times = tf.placeholder('int32',
                                    shape=[params.batch_size],
                                    name='wait_times')
        is_training = tf.placeholder(tf.bool)

        # Prepare parameters

        with tf.variable_scope("CNN"):
            cnn_output = cnn(features, params.feature_maps, params.kernels,
                             dates)

            # Regularizations [batch norm and dropout]
            output = batch_norm(cnn_output, is_training=is_training)
            output = dropout(output, params.d_rate, is_training)
        """"
        with tf.variable_scope("LSTM") as scope:
            lstm_outputs = []
            loss = 0

            cell = tf.nn.rnn_cell.BasicLSTMCell(params.rnn_size)
            stacked_cell = tf.nn.rnn_cell.MultiRNNCell([cell] * params.layer_depth)

            outputs, _ = tf.nn.rnn(stacked_cell, output)
            for idx, (pred_y, true_y) in enumerate(zip(outputs, self.wait_times)):
                output = dropout(pred_y, params.d_rate, is_training)
                if idx != 0:
                    scope.reuse_variables()
                output = _linear(output, h)
                lstm_outputs.append(output)
                loss += tf.nn.sparse_softmax_cross_entropy_with_logits(lstm_outputs[idx], tf.squeeze(self.wait_times))
        """
        with tf.name_scope('Loss'):
            # Cross Entropy Loss
            cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
                output, wait_times)
            loss = tf.reduce_mean(cross_entropy)
            total_loss = loss + params.weight_decay * tf.add_n(
                tf.get_collection('l2'))

        # Optimization with ADAM
        optimizer = tf.train.AdamOptimizer(params.learning_rate)
        train_step = optimizer.minimize(total_loss,
                                        global_step=self.global_step)

        # Data variables
        self.features = features
        self.wait_times = wait_times
        self.dates = dates
        self.times = times
        self.is_training = is_training

        # Training variables
        self.mean_error = mean_error
        self.total_loss = total_loss
        self.train_step = train_step