예제 #1
0
    def forward(self, x):
        hidden = conv_layer(x, 5, 3, 6, self.regularizer)
        hidden = conv_layer(hidden, 3, 6, 12, self.regularizer)
        hidden = tf.reshape(hidden, [-1, 20 * 40 * 12])
        y = fc_layer(hidden, 20 * 40 * 12, 10, 'layer_after_conv')

        return y
예제 #2
0
def get_inference(images_ph, dropout_keep_prob_ph):
    #subtract average image
    with tf.variable_scope('centering') as scope:
        mean = tf.constant(vgg.average_image, dtype=tf.float32, name='avg_image')
        images_ph = tf.sub(images_ph, mean, name='subtract_avg')

    #get layers from vgg19
    vgg_layers = vgg.get_VGG_layers(images_ph, dropout_keep_prob_ph, train_fc_layers=True)

    #################################################
    ### Add more layers for semantic segmentation ###
    #################################################

    # convolution on top of pool4 to 21 chammenls (to make coarse predictions)
    with tf.variable_scope('conv9') as scope:
        conv9 = conv_layer(vgg_layers['pool4'], 21, 1, 'conv9')

    # convolution on top of conv7 (fc7) to 21 chammenls (to make coarse predictions)
    with tf.variable_scope('conv8') as scope:
        conv8 = conv_layer(vgg_layers['dropout2'], 21, 1, 'conv8')

    # 2x upsampling from last layer
    with tf.variable_scope('deconv1') as scope:
        shape = tf.shape(conv8)
        out_shape = tf.pack([shape[0], shape[1]*2, shape[2]*2, 21])
        weights = tf.Variable(tf.truncated_normal(mean=MEAN, stddev=0.1, shape=(4, 4, 21, 21)), name='weights')
        deconv1 = tf.nn.conv2d_transpose( value=conv8,
                                          filter=weights,
                                          output_shape=out_shape,
                                          strides=(1, 2, 2, 1),
                                          padding='SAME',
                                          name='deconv1')

        # slice 2x upsampled tensor in the last layer to fit pool4
        shape = tf.shape(conv9)
        size = tf.pack([-1, shape[1], shape[2], -1])
        deconv1 = tf.slice(deconv1, begin=[0,0,0,0], size=size, name="deconv1_slice")

    # combine preductions from last layer and pool4
    with tf.variable_scope('combined_pred') as scope:
        combined_pred = tf.add(deconv1, conv9, name="combined_pred")

    # 16x upsampling
    with tf.variable_scope('deconv2') as scope:
        shape = tf.shape(combined_pred)
        out_shape = tf.pack([shape[0], shape[1]*16, shape[2]*16, 21])
        weights = tf.Variable(tf.truncated_normal(mean=MEAN, stddev=0.1, shape=(32, 32, 21, 21)), name='weights')
        deconv2 = tf.nn.conv2d_transpose(value=combined_pred,
                                          filter=weights,
                                          output_shape=out_shape,
                                          strides=(1, 16, 16, 1),
                                          padding='SAME',
                                          name='deconv2')

        # slice upsampled tensor to original shape
        orig_shape = tf.shape(images_ph)
        size = tf.pack([-1, orig_shape[1], orig_shape[2], -1])
        logits = tf.slice(deconv2, begin=[0,0,0,0], size=size, name='logits')

    return logits
예제 #3
0
def get_VGG_layers(images_ph, dropout_keep_prob_ph, train_fc_layers=False):
    """
    Args:
        images: Images placeholder in NHWC.
    Returns:
        dict of tensors, keys are original layer names.
    """
    x = images_ph
    layers = dict()
    for layer in VGG_layers:
        layer_type = layer[0]
        layer_name = layer[1]
        if layer_type == 'conv':
            with tf.variable_scope(layer_name) as scope:
                x = conv_layer(x, layer[2], 3, layer_name, train=False)
        elif layer_type == 'pool':
            with tf.variable_scope(layer_name) as scope:
                x = max_pool_2x2(x, layer_name)
        elif layer_type == 'fc':
            with tf.variable_scope(layer_name) as scope:
                x = conv_layer(x, layer[2], layer[3], layer_name, train=train_fc_layers)
        elif layer_type == 'relu':
            x = tf.nn.relu(x, layer_name)
        elif layer_type == 'dropout':
            x = tf.nn.dropout(x, dropout_keep_prob_ph, name=layer_name)

        layers[layer_name] = x

    return layers
예제 #4
0
파일: model.py 프로젝트: raotnameh/net
    def __init__(self):
        super(feature_r, self).__init__()

        #input 28*28
        self.layerb1 = conv_layer(512,
                                  512,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.layerb2 = conv_layer(512,
                                  512,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.drop_layer1 = nn.Dropout(0.1)
        #input 28*28
        self.layerb3 = conv_layer(512,
                                  512,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.layerb4 = conv_layer(512,
                                  512,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.drop_layer2 = nn.Dropout(0.1)
    def __init__(self, conv_dim=64):
        super(Dy, self).__init__()

        self.conv1 = conv_layer(3, conv_dim, 4, batch_norm=False)
        self.conv2 = conv_layer(conv_dim, conv_dim * 2, 4)
        self.conv3 = conv_layer(conv_dim * 2, conv_dim * 4, 4)

        self.fc = conv_layer(conv_dim * 4, 1, 4, 1, 0, batch_norm=False)
예제 #6
0
    def graph(self, input, is_training):
        with tf.name_scope('model'):
            net = ut.conv_layer(input, 64, 7, 2, name='conv1')
            net = ut.bottleneck(net,
                                128,
                                stride=1,
                                training=is_training,
                                name='res1')
            net = ut.max_pool(net, 2, 2, 'max_pool')
            net = ut.bottleneck(net,
                                int(self.nFeats / 2),
                                stride=1,
                                training=is_training,
                                name='res2')
            net = ut.bottleneck(net,
                                self.nFeats,
                                stride=1,
                                training=is_training,
                                name='res3')

            with tf.name_scope('stacks'):
                stack_out = []
                with tf.name_scope('stage_0'):
                    hg = ut.hourglass(net, self.nLow, self.nFeats, 'hourglass')
                    drop = ut.dropout(hg, self.dropout_rate, is_training,
                                      'dropout')
                    ll = ut.conv_layer_bn(drop, self.nFeats, 1, 1, is_training)
                    out = ut.conv_layer(ll, self.num_points, 1, 1, name='out')
                    out_ = ut.conv_layer(out, self.nFeats, 1, 1, name='out_')
                    sum_ = tf.add(net, out_, name='merge')
                    stack_out.append(out)
                for i in range(1, self.nStacks):
                    with tf.name_scope('stage_' + str(i)):
                        hg = ut.hourglass(sum_, self.nLow, self.nFeats,
                                          'hourglass')
                        drop = ut.dropout(hg, self.dropout_rate, is_training,
                                          'dropout')
                        ll = ut.conv_layer_bn(drop, self.nFeats, 1, 1,
                                              is_training)
                        out = ut.conv_layer(ll,
                                            self.num_points,
                                            1,
                                            1,
                                            name='out')
                        out_ = ut.conv_layer(ll,
                                             self.nFeats,
                                             1,
                                             1,
                                             name='out_')
                        sum_ = tf.add(sum_, out_, name='merge')
                        stack_out.append(out)
            with tf.name_scope('upsampling'):
                net = ut.batch_norm(sum_, is_training)
                net = ut.conv_layer_bn(net, self.nFeats, 3, 1, is_training)
                up1 = ut.deconv_layer(net, self.num_points, 1, 2, name='up_1')
                net = ut.conv_layer_bn(up1, self.nFeats, 3, 1, is_training)
                up2 = ut.deconv_layer(net, self.num_points, 1, 2, name='up_2')
            return tf.stack(stack_out, axis=1, name='stack_out'), up1, up2
    def __init__(self, conv_dim=64):
        super(Gxy, self).__init__()

        # encoding block
        self.conv1 = conv_layer(1, conv_dim, 4)
        self.conv2 = conv_layer(conv_dim, conv_dim * 2, 4)

        # residual block (will use res layer when I have good GPU :P)
        self.conv3 = conv_layer(conv_dim * 2, conv_dim * 2, 3, 1, 1)
        self.conv4 = conv_layer(conv_dim * 2, conv_dim * 2, 3, 1, 1)

        # docoding block
        self.deconv1 = deconv_layer(conv_dim * 2, conv_dim, 4)
        self.deconv2 = deconv_layer(conv_dim, 3, 4, batch_norm=False)
    def __init__(self, conv_dim=64):
        super(Gyx, self).__init__()

        # encoding block
        self.conv1 = conv_layer(3, conv_dim, 4)
        self.conv2 = conv_layer(conv_dim, conv_dim * 2, 4)

        # residual block
        self.conv3 = conv_layer(conv_dim * 2, conv_dim * 2, 3, 1, 1)
        self.conv4 = conv_layer(conv_dim * 2, conv_dim * 2, 3, 1, 1)

        # docoding block
        self.deconv1 = deconv_layer(conv_dim * 2, conv_dim, 4)
        self.deconv2 = deconv_layer(conv_dim, 1, 4, batch_norm=False)
예제 #9
0
파일: model.py 프로젝트: raotnameh/net
    def __init__(self):
        super(decision_r, self).__init__()

        #input 28*28
        self.layerb1 = conv_layer(512,
                                  1024,
                                  kernel_size=3,
                                  stride=(2, 2),
                                  padding=1)
        #input 14*14
        self.layerb2 = conv_layer(1024,
                                  4096,
                                  kernel_size=3,
                                  stride=(2, 2),
                                  padding=1)
예제 #10
0
    def __call__(self, loc):
        glimpse_input = self.get_glimpse(loc)
        #glimpse_input = tf.reshape(glimpse_input,
        #                          (tf.shape(loc)[0], self.sensor_size))
        glimpse_input = tf.reshape(
            glimpse_input,
            (tf.shape(loc)[0], self.win_size, self.win_size, self.depth))

        #g = tf.nn.relu(tf.nn.xw_plus_b(glimpse_input, self.w_g0, self.b_g0)) # replace with a CNN?
        # CNN
        g = glimpse_input
        for i in range(1, self.gcnn_depth + 1):
            w = self.glimpse_params['w_gconv{}'.format(i)]
            b = self.glimpse_params['b_gconv{}'.format(i)]
            g = conv_layer(g,
                           w,
                           b,
                           'glimpse_conv{}'.format(i),
                           stride=1,
                           padding='SAME')

        g = tf.reshape(g, (tf.shape(g)[0], -1))
        for i in range(1, self.gfc_depth + 1):
            w = self.glimpse_params['w_gfc{}'.format(i)]
            b = self.glimpse_params['b_gfc{}'.format(i)]
            g = tf.nn.xw_plus_b(g, w, b)

        l = tf.nn.relu(tf.nn.xw_plus_b(
            loc, self.w_l0,
            self.b_l0))  # what's the point other than transforming the dim?
        l = tf.nn.xw_plus_b(l, self.w_l1, self.b_l1)
        g = tf.nn.relu(g + l)
        return g
예제 #11
0
파일: conv_vae.py 프로젝트: seahailang/GAN
    def builde_encode(self, input):
        # output shape 64*28*28*64
        input = tf.reshape(
            input,
            shape=[self.batch_size, self.image_w, self.image_h, self.channel])
        tf.summary.image('input_image', input)
        with tf.variable_scope('conv_layer1'):
            tensor = conv_layer(input,
                                filters=self.filters[0],
                                k_size=self.kernel[0],
                                strides=1)

        # output shape = 64*14*14*64
        with tf.variable_scope('max_pool_layer_1'):
            ksize = [1, 5, 5, 1]
            strides = [1, 2, 2, 1]
            tensor = tf.nn.max_pool(tensor,
                                    ksize=ksize,
                                    strides=strides,
                                    padding='SAME')
        # output shape = 64*14*14*64
        with tf.variable_scope('conv_layer2'):
            tensor = conv_layer(tensor,
                                self.filters[1],
                                self.kernel[1],
                                strides=1)
        # output shape = 64*7*7*64
        with tf.variable_scope('max_pool_layer_1'):
            ksize = [1, 5, 5, 1]
            strides = [1, 2, 2, 1]
            tensor = tf.nn.max_pool(tensor,
                                    ksize=ksize,
                                    strides=strides,
                                    padding='SAME')
            # output_size = tf.shape(pool)[1,2]
        tensor = tf.reshape(tensor, [self.batch_size, -1])

        with tf.variable_scope('linear'):
            tensor = linear_layer(tensor, self.linear_units)

        with tf.variable_scope('latent'):
            tensor = linear_layer(tensor, self.variation_dim, lambda x: x)
            tensor2 = linear_layer(tensor,
                                   self.variation_dim,
                                   lambda x: x,
                                   name='2')
        return tensor, tensor2
예제 #12
0
파일: model.py 프로젝트: raotnameh/net
    def __init__(self):
        super(feature_b, self).__init__()

        #input 224*224
        self.layerb1 = conv_layer(3,
                                  64,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.layerb2 = conv_layer(64,
                                  64,
                                  kernel_size=3,
                                  stride=(2, 2),
                                  padding=1)
        self.drop_layer1 = nn.Dropout(0.1)
        #input 112*112
        self.layerb3 = conv_layer(64,
                                  128,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.layerb4 = conv_layer(128,
                                  128,
                                  kernel_size=3,
                                  stride=(2, 2),
                                  padding=1)
        self.drop_layer2 = nn.Dropout(0.1)
        #input 56*56
        self.layerb5 = conv_layer(128,
                                  256,
                                  kernel_size=3,
                                  stride=(1, 1),
                                  padding=1)
        self.layerb6 = conv_layer(256,
                                  512,
                                  kernel_size=3,
                                  stride=(2, 2),
                                  padding=1)
        self.drop_layer3 = nn.Dropout(0.1)
예제 #13
0
    def warper_11(self, input_LR, input_g, is_training, reuse=False):
        with tf.variable_scope('warper', reuse=reuse):
            x = tf.concat([input_LR, input_g], axis=3)
            with tf.variable_scope('input_encoder'):
                with tf.variable_scope('conv1'):
                    x = utils.conv_layer(x, 3, 64, 1)
                    x = utils.relu_layer(x)

                with tf.variable_scope('conv2'):
                    x = utils.conv_layer(x, 3, 64, 2)
                    x = utils.relu_layer(x)

                with tf.variable_scope('conv3'):
                    x = utils.conv_layer(x, 3, 64, 1)
                    x = utils.relu_layer(x)

                with tf.variable_scope('conv4'):
                    x = utils.conv_layer(x, 3, 64, 2)
                    x = utils.relu_layer(x)

                with tf.variable_scope('conv5'):
                    x = utils.conv_layer(x, 3, 64, 1)
                    x = utils.relu_layer(x)

                with tf.variable_scope('conv6'):
                    x = utils.conv_layer(x, 3, 64, 2)
                    x = utils.relu_layer(x)

                with tf.variable_scope('conv_input_res'):
                    x = utils.conv_layer(x, 3, 64, 1, False)
                    conv_1 = x
                with tf.variable_scope('resblocks'):
                    for i in range(8):
                        with tf.variable_scope('resblock{}'.format(i + 1)):
                            x = utils.resBlock_EDSR(x,
                                                    filter_size=3,
                                                    num_filters=64)

                with tf.variable_scope('conv_before_skip'):
                    x = utils.conv_layer(x, 3, 64, 1, False)

                with tf.variable_scope('flow_decoder_input'):
                    x += conv_1

            with tf.variable_scope('flow_decoder'):
                with tf.variable_scope('upsampler1'):
                    x = utils.upsample_EDSR(x, 2, 64, False)

                with tf.variable_scope('upsampler2'):
                    x = utils.upsample_EDSR(x, 2, 64, False)

                with tf.variable_scope('upsampler3'):
                    x = utils.upsample_EDSR(x, 2, 64, False)

                with tf.variable_scope('conv1'):
                    num_input_channels = x.get_shape().as_list()[3]
                    shape = [3, 3, num_input_channels, 2]
                    length = [2]
                    weights1 = tf.get_variable(
                        name='weight',
                        shape=shape,
                        dtype=tf.float32,
                        initializer=tf.contrib.layers.xavier_initializer())
                    biases1 = tf.get_variable(
                        name='bias',
                        shape=length,
                        dtype=tf.float32,
                        initializer=tf.constant_initializer(0.0))
                    x = tf.nn.conv2d(x, weights1, [1, 1, 1, 1], padding='SAME')
                    x = tf.nn.bias_add(x, biases1)

        self.w_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                             scope='warper')
        return x
예제 #14
0
    def model_guidance_9(self, input_LR, input_g, reuse=False):
        with tf.variable_scope('generator', reuse):
            with tf.variable_scope('input_g_preprocessing'):
                with tf.variable_scope('conv1'):
                    x_g = utils.conv_layer(input_g, 3, 64, 1)
                    x_g = utils.relu_layer(x_g)

                with tf.variable_scope('conv2'):
                    x_g = utils.conv_layer(x_g, 3, 64, 2)
                    x_g = utils.relu_layer(x_g)

                with tf.variable_scope('conv3'):
                    x_g = utils.conv_layer(x_g, 3, 64, 1)
                    x_g = utils.relu_layer(x_g)

                with tf.variable_scope('conv4'):
                    x_g = utils.conv_layer(x_g, 3, 64, 2)
                    x_g = utils.relu_layer(x_g)

                with tf.variable_scope('conv5'):
                    x_g = utils.conv_layer(x_g, 3, 64, 1)
                    x_g = utils.relu_layer(x_g)

                with tf.variable_scope('conv6'):
                    x_g = utils.conv_layer(x_g, 3, 64, 2)
                    x_g = utils.relu_layer(x_g)

            with tf.variable_scope('conv1'):
                x = utils.conv_layer(input_LR, 3, self.args.feature_size, 1,
                                     False)
            with tf.variable_scope('conv1_g'):
                x_g = utils.conv_layer(x_g, 3, self.args.feature_size, 1,
                                       False)
            conv_1 = x

            with tf.variable_scope('resblocks'):
                with tf.variable_scope('resblocks_1'):
                    for i in range(self.args.merge_resblock):
                        with tf.variable_scope('resblock{}'.format(i + 1)):
                            x = utils.resBlock_EDSR(
                                x,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)
                        with tf.variable_scope('resblock_g{}'.format(i + 1)):
                            x_g = utils.resBlock_EDSR(
                                x_g,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)
                    x = tf.concat([x, x_g], axis=3)
                    x = utils.conv_layer(x, 3, self.args.feature_size, 1,
                                         False)

                with tf.variable_scope('resblocks_2'):
                    for i in range(self.args.merge_resblock):
                        with tf.variable_scope('resblock{}'.format(i + 1)):
                            x = utils.resBlock_EDSR(
                                x,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)
                        with tf.variable_scope('resblock_g{}'.format(i + 1)):
                            x_g = utils.resBlock_EDSR(
                                x_g,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)
                    x = tf.concat([x, x_g], axis=3)
                    x = utils.conv_layer(x, 3, self.args.feature_size, 1,
                                         False)

                with tf.variable_scope('resblocks_3'):
                    for i in range(self.args.merge_resblock):
                        with tf.variable_scope('resblock{}'.format(i + 1)):
                            x = utils.resBlock_EDSR(
                                x,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)
                        with tf.variable_scope('resblock_g{}'.format(i + 1)):
                            x_g = utils.resBlock_EDSR(
                                x_g,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)
                    x = tf.concat([x, x_g], axis=3)
                    x = utils.conv_layer(x, 3, self.args.feature_size, 1,
                                         False)

                with tf.variable_scope('resblocks_4'):
                    for i in range(self.args.merge_resblock):
                        with tf.variable_scope('resblock{}'.format(i + 1)):
                            x = utils.resBlock_EDSR(
                                x,
                                3,
                                num_filters=self.args.feature_size,
                                scale=self.args.scaling_factor)

            with tf.variable_scope('conv_before_skip'):
                x = utils.conv_layer(x, 3, self.args.feature_size, 1, False)

            with tf.variable_scope('upsampler_input'):
                x += conv_1

            with tf.variable_scope('upsamplers'):
                for i in range(3):
                    with tf.variable_scope('upsampler{}'.format(i + 1)):
                        x = utils.upsample_EDSR(x, 2, self.args.feature_size,
                                                False)

            with tf.variable_scope('conv_final'):
                x = utils.conv_layer(x, 3, self.args.output_channels, 1, False)
        self.g_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                             scope='generator')
        return x
예제 #15
0
# Step 1: Get data
train_data, test_data = utils.get_mnist_dataset(batch_size, MNIST)

iterator = tf.data.Iterator.from_structure(train_data.output_types,
                                           train_data.output_shapes)
img, label = iterator.get_next()
img = tf.cast(img, tf.float32)
img = tf.reshape(img, [-1, 28, 28, 1])

train_init = iterator.make_initializer(
    train_data)  # initializer for train_data
test_init = iterator.make_initializer(test_data)  # initializer for train_data

# Step 2: Set layers structure

H1 = utils.conv_layer(img, n_filters, filter_size)
H1_relu = tf.nn.relu(H1)
H1_pool = tf.nn.max_pool(H1_relu,
                         ksize=[1, 2, 2, 1],
                         strides=[1, 2, 2, 1],
                         padding="SAME")
feature_dim = H1_pool.shape[1] * H1_pool.shape[2] * H1_pool.shape[3]
logits = tf.layers.dense(tf.reshape(H1_pool, [-1, feature_dim]),
                         10,
                         kernel_initializer=tf.random_normal_initializer(
                             0, 0.01),
                         name='logits')
#H2 = tf.layers.dense(tf.sigmoid(H1),100,kernel_initializer=tf.random_normal_initializer(0, 0.01),name = 'H2')

# Step 3: define loss function
# use cross entropy of softmax of logits as the loss function
예제 #16
0
def VGG16(x, n_classes, is_pretrain = True):
    x = utils.conv_layer('conv1_1', x, 64, filter = [3,3], strides = [1,1,1,1], is_pretrain = is_pretrain)
    x = utils.conv_layer('conv1_2', x, 64, filter = [3,3], strides = [1,1,1,1], is_pretrain = is_pretrain)
    x = utils.pool_layer('pool1', x, filter = [1,2,2,1], strides = [1,2,2,1], is_max_pool = True)

    x = utils.conv_layer('conv2_1', x, 128, filter = [3,3], strides = [1,1,1,1], is_pretrain = is_pretrain)
    x = utils.conv_layer('conv2_2', x, 128, filter = [3,3], strides = [1,1,1,1], is_pretrain = is_pretrain)
    x = utils.pool_layer('pool2', x, filter = [1,2,2,1], strides = [1,2,2,1], is_max_pool = True)

    x = utils.conv_layer('conv3_1', x, 256, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.conv_layer('conv3_2', x, 256, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.conv_layer('conv3_3', x, 256, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.pool_layer('pool3', x, filter=[1,2,2,1], strides=[1,2,2,1], is_max_pool=True)

    x = utils.conv_layer('conv4_1', x, 512, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.conv_layer('conv4_2', x, 512, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.conv_layer('conv4_3', x, 512, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.pool_layer('pool4', x, filter=[1,2,2,1], strides=[1,2,2,1], is_max_pool=True)

    x = utils.conv_layer('conv5_1', x, 512, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.conv_layer('conv5_2', x, 512, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.conv_layer('conv5_3', x, 512, filter=[3, 3], strides=[1, 1, 1, 1], is_pretrain=is_pretrain)
    x = utils.pool_layer('pool5', x, filter=[1, 2, 2, 1], strides=[1, 2, 2, 1], is_max_pool=True)

    x = utils.fc_layer('fc6', x, num_output = 4096)
    x = utils.batch_normalization(x)
    x = utils.fc_layer('fc7', x, num_output = 4096)
    x = utils.batch_normalization(x)
    x = utils.fc_layer('fc8', x, num_output = n_classes)

    return x
예제 #17
0
    def cnn(self,
            input,
            ksizes,
            strides,
            n_channels,
            use_dropout=False,
            reuse=True):
        '''Create all the conv layers as specified in the paper.'''

        assert len(ksizes) == len(strides) == len(n_channels), (
            'Kernel, stride and channel specs '
            'must have same length')
        outer_scope_name = flownet_prefix if self.use_flownet else 'cnn'
        with tf.variable_scope(outer_scope_name, reuse=reuse):

            # biases initialise with a small constant
            bias_initializer = tf.constant_initializer(0.01)

            # kernels initialise according to He et al.
            def kernel_initializer(k):
                return tf.random_normal_initializer(stddev=np.sqrt(2 / k))

            output = input

            for index, [ksize, stride,
                        channels] in enumerate(zip(ksizes, strides,
                                                   n_channels)):
                inner_scope_name = flownet_layer_names[
                    index] if self.use_flownet else f'conv{index}'
                with tf.variable_scope(inner_scope_name):
                    # no relu for last layer
                    activation = tf.nn.relu if index < len(
                        ksizes) - 1 else None

                    if not self.use_flownet:
                        # I know we shouldn't use tf.layers but since dense + conv are easy to
                        # implement by oneself we decided to remove this possible source of errors
                        # and focus on the many others
                        output = tf.layers.conv2d(
                            output,
                            channels,
                            kernel_size=[ksize, ksize],
                            strides=stride,
                            padding='SAME',
                            activation=activation,
                            kernel_initializer=kernel_initializer(ksize),
                            bias_initializer=bias_initializer)
                    else:
                        # since we need control over the variable namings, we cannot use
                        # tf.layers.conv2d
                        bias_name = flownet_bias_suffix
                        kernel_name = flownet_kernel_suffix
                        output = conv_layer(
                            output,
                            channels,
                            kernel_width=ksize,
                            strides=stride,
                            activation=activation,
                            kernel_initializer=kernel_initializer(ksize),
                            bias_initializer=bias_initializer,
                            use_bias=True,
                            padding='SAME',
                            var_names=(kernel_name, bias_name),
                            trainable=False)

            return output
예제 #18
0
    def forward(self, inputs):
        # inputs tensor should be defined like this
        # inputs = tf.placeholder(dtype=tf.float32,
        #                         name='input_images',
        #                         shape=[self.BATCH, self.WIDTH, self.HEIGHT, self.CHANNELS])

        # The first layer is input images
        layers = {-1: inputs}
        previous_filters = self.CHANNELS

        # weights list
        weights_list = []

        # YOLO outputs
        outputs = None

        for index, block in enumerate(self.config_blocks[1:]):

            # ====================== convolutional layer ======================
            if block['name'] == 'convolutional':

                filters = int(block['filters'])
                size = int(block['size'])
                stride = int(block['stride'])
                pad = int(block['pad'])
                activation = block['activation']
                try:
                    batch_normalize = int(block['batch_normalize'])
                except KeyError:
                    batch_normalize = 0

                if pad:
                    pad = 'SAME'
                else:
                    pad = 'VALID'

                output, weights = utils.conv_layer(
                    layer_index=index,
                    layer_input=layers[index - 1],
                    input_filters=previous_filters,
                    output_filters=filters,
                    size=size,
                    stride=stride,
                    pad=pad,
                    activation=activation,
                    batch_normalize=batch_normalize)

                weights_list += weights

                previous_filters = filters

                # Finally, add this layer output to list
                layers[index] = output

            # ====================== max pooling layer ======================
            elif block['name'] == 'maxpool':

                size = int(block['size'])
                stride = int(block['stride'])

                output = tf.layers.max_pooling2d(
                    inputs=layers[index - 1],
                    pool_size=[size, size],
                    strides=stride,
                    padding='same',
                    name='{}_maxpool'.format(index))

                # Filters doesn't change, just add this layer output to list
                layers[index] = output

            # ====================== shortcut layer ======================
            elif block['name'] == 'shortcut':

                from_layer = int(block['from'])

                # Just add 2 layers (the previous and the layers from_layer)
                # So the number of filters will not change
                output = tf.add(layers[index - 1],
                                layers[index + from_layer],
                                name='{}_shortcut_add_{}_{}'.format(
                                    index, index - 1, index + from_layer))

                # print(output)
                # Finally, add this layer output to list
                layers[index] = output

            # ====================== route layer ======================
            elif block['name'] == 'route':

                routes = block['layers'].split(',')

                start = int(routes[0])
                try:
                    end = int(routes[1])
                except IndexError:
                    end = 0

                # Calculate the number of step from index to start and end
                if start > 0:
                    start = start - index
                if end > 0:
                    end = end - index

                if end < 0:
                    output = tf.concat(
                        values=[layers[index + start], layers[index + end]],
                        axis=-1,
                        name='{}_route_concat_{}_{}'.format(
                            index, index + start, index + end))
                else:
                    output = layers[index + start]
                    output = tf.identity(output,
                                         name='{}_route_to_{}'.format(
                                             index, index + start))

                previous_filters = output.get_shape().as_list()[-1]

                # print(output)
                # Finally, add this layer output to list
                layers[index] = output

            # ====================== upsample layer ======================
            elif block['name'] == 'upsample':
                stride = int(block['stride'])

                # Just increase size
                old_shape = layers[index - 1].get_shape().as_list()
                old_width = old_shape[1]
                old_height = old_shape[2]

                new_width = old_width * stride
                new_height = old_height * stride

                with tf.variable_scope('{}_upsample'.format(index)):
                    output = tf.image.resize_images(
                        images=layers[index - 1],
                        size=[new_width, new_height],
                        align_corners=True,
                        method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)

                # print(output)
                # Finally, add this layer output to list
                layers[index] = output

            # ====================== yolo layer ======================
            elif block['name'] == 'yolo':

                mask = block['mask'].split(',')
                mask = [int(x) for x in mask]

                anchors = block['anchors'].split(',')
                anchors = [int(x) for x in anchors]
                anchors = [(anchors[i], anchors[i + 1])
                           for i in range(0, len(anchors), 2)]
                anchors = [anchors[i] for i in mask]

                num_classes = int(block['classes'])
                self.ANCHORS.append(anchors)
                self.GRID_SIZES.append(layers[index -
                                              1].get_shape().as_list()[1])
                self.NUM_CLASSES = num_classes

                output = utils.transform_features_map(
                    input_size=self.WIDTH,
                    layer_index=index,
                    features_map=layers[index - 1],
                    anchors=anchors,
                    num_classes=num_classes)

                if outputs is None:
                    outputs = output
                else:
                    outputs = tf.concat(values=[outputs, output], axis=1)

                # Finally, add this layer output to list
                layers[index] = output

        return weights_list, outputs
예제 #19
0
stride = 1
filter_size = 3
filter_nb_1 = 10
filter_nb_2 = 13
filter_nb_3 = 18
filter_nb_4 = 25

filter_nb_5 = 100

activation_func = tf.nn.relu
activation_func = tf.nn.elu

hidden1 = conv_layer(x_, [filter_size, filter_size, 1, filter_nb_1],
                     'conv-1',
                     stride,
                     keep_prob,
                     is_training,
                     act=activation_func)
hidden2 = conv_layer(hidden1,
                     [filter_size, filter_size, filter_nb_1, filter_nb_1],
                     'conv-2',
                     stride,
                     keep_prob,
                     is_training,
                     act=activation_func)
hidden4 = conv_layer(hidden2,
                     [filter_size, filter_size, filter_nb_1, filter_nb_1],
                     'conv-3',
                     stride,
                     keep_prob,
                     is_training,