def inference_conv(image):
    # incomplete :/
    image_reshaped = tf.reshape(image, [-1, IMAGE_SIZE, IMAGE_SIZE, 1])
    with tf.name_scope("conv1") as scope:
        W_conv1 = utils.weight_variable([3, 3, 1, 32], name="W_conv1")
        b_conv1 = utils.bias_variable([32], name="b_conv1")
        add_to_reg_loss_and_summary(W_conv1, b_conv1)
        h_conv1 = tf.nn.tanh(
            utils.conv2d_basic(image_reshaped, W_conv1, b_conv1))

    with tf.name_scope("conv2") as scope:
        W_conv2 = utils.weight_variable([3, 3, 32, 64], name="W_conv2")
        b_conv2 = utils.bias_variable([64], name="b_conv2")
        add_to_reg_loss_and_summary(W_conv2, b_conv2)
        h_conv2 = tf.nn.tanh(utils.conv2d_strided(h_conv1, W_conv2, b_conv2))

    with tf.name_scope("conv3") as scope:
        W_conv3 = utils.weight_variable([3, 3, 64, 128], name="W_conv3")
        b_conv3 = utils.bias_variable([128], name="b_conv3")
        add_to_reg_loss_and_summary(W_conv3, b_conv3)
        h_conv3 = tf.nn.tanh(utils.conv2d_strided(h_conv2, W_conv3, b_conv3))

    with tf.name_scope("conv4") as scope:
        W_conv4 = utils.weight_variable([3, 3, 128, 256], name="W_conv4")
        b_conv4 = utils.bias_variable([256], name="b_conv4")
        add_to_reg_loss_and_summary(W_conv4, b_conv4)
        h_conv4 = tf.nn.tanh(utils.conv2d_strided(h_conv3, W_conv4, b_conv4))
Esempio n. 2
0
def encoder_conv(image):
    with tf.name_scope("enc_conv1") as scope:
        W_conv1 = utils.weight_variable([3, 3, 3, 32], name="W_conv1")
        b_conv1 = utils.bias_variable([32], name="b_conv1")
        h_conv1 = tf.nn.tanh(utils.conv2d_strided(image, W_conv1, b_conv1))

    with tf.name_scope("enc_conv2") as scope:
        W_conv2 = utils.weight_variable([3, 3, 32, 64], name="W_conv2")
        b_conv2 = utils.bias_variable([64], name="b_conv2")
        h_conv2 = tf.nn.tanh(utils.conv2d_strided(h_conv1, W_conv2, b_conv2))

    with tf.name_scope("enc_conv3") as scope:
        W_conv3 = utils.weight_variable([3, 3, 64, 128], name="W_conv3")
        b_conv3 = utils.bias_variable([128], name="b_conv3")
        h_conv3 = tf.nn.tanh(utils.conv2d_strided(h_conv2, W_conv3, b_conv3))

    with tf.name_scope("enc_conv4") as scope:
        W_conv4 = utils.weight_variable([3, 3, 128, 256], name="W_conv4")
        b_conv4 = utils.bias_variable([256], name="b_conv4")
        h_conv4 = tf.nn.tanh(utils.conv2d_strided(h_conv3, W_conv4, b_conv4))

    with tf.name_scope("enc_fc") as scope:
        image_size = IMAGE_SIZE // 16
        h_conv4_flatten = tf.reshape(h_conv4,
                                     [-1, image_size * image_size * 256])
        W_fc5 = utils.weight_variable([image_size * image_size * 256, 512],
                                      name="W_fc5")
        b_fc5 = utils.bias_variable([512], name="b_fc5")
        encoder_val = tf.matmul(h_conv4_flatten, W_fc5) + b_fc5

    return encoder_val
def inference_conv(image):
    # incomplete :/
    image_reshaped = tf.reshape(image, [-1, IMAGE_SIZE, IMAGE_SIZE, 1])
    with tf.name_scope("conv1") as scope:
        W_conv1 = utils.weight_variable([3, 3, 1, 32], name="W_conv1")
        b_conv1 = utils.bias_variable([32], name="b_conv1")
        add_to_reg_loss_and_summary(W_conv1, b_conv1)
        h_conv1 = tf.nn.tanh(utils.conv2d_basic(image_reshaped, W_conv1, b_conv1))

    with tf.name_scope("conv2") as scope:
        W_conv2 = utils.weight_variable([3, 3, 32, 64], name="W_conv2")
        b_conv2 = utils.bias_variable([64], name="b_conv2")
        add_to_reg_loss_and_summary(W_conv2, b_conv2)
        h_conv2 = tf.nn.tanh(utils.conv2d_strided(h_conv1, W_conv2, b_conv2))

    with tf.name_scope("conv3") as scope:
        W_conv3 = utils.weight_variable([3, 3, 64, 128], name="W_conv3")
        b_conv3 = utils.bias_variable([128], name="b_conv3")
        add_to_reg_loss_and_summary(W_conv3, b_conv3)
        h_conv3 = tf.nn.tanh(utils.conv2d_strided(h_conv2, W_conv3, b_conv3))

    with tf.name_scope("conv4") as scope:
        W_conv4 = utils.weight_variable([3, 3, 128, 256], name="W_conv4")
        b_conv4 = utils.bias_variable([256], name="b_conv4")
        add_to_reg_loss_and_summary(W_conv4, b_conv4)
        h_conv4 = tf.nn.tanh(utils.conv2d_strided(h_conv3, W_conv4, b_conv4))
def encoder_conv(image):
    with tf.name_scope("enc_conv1") as scope:
        W_conv1 = utils.weight_variable([3, 3, 3, 32], name="W_conv1")
        b_conv1 = utils.bias_variable([32], name="b_conv1")
        h_conv1 = tf.nn.tanh(utils.conv2d_strided(image, W_conv1, b_conv1))

    with tf.name_scope("enc_conv2") as scope:
        W_conv2 = utils.weight_variable([3, 3, 32, 64], name="W_conv2")
        b_conv2 = utils.bias_variable([64], name="b_conv2")
        h_conv2 = tf.nn.tanh(utils.conv2d_strided(h_conv1, W_conv2, b_conv2))

    with tf.name_scope("enc_conv3") as scope:
        W_conv3 = utils.weight_variable([3, 3, 64, 128], name="W_conv3")
        b_conv3 = utils.bias_variable([128], name="b_conv3")
        h_conv3 = tf.nn.tanh(utils.conv2d_strided(h_conv2, W_conv3, b_conv3))

    with tf.name_scope("enc_conv4") as scope:
        W_conv4 = utils.weight_variable([3, 3, 128, 256], name="W_conv4")
        b_conv4 = utils.bias_variable([256], name="b_conv4")
        h_conv4 = tf.nn.tanh(utils.conv2d_strided(h_conv3, W_conv4, b_conv4))

    with tf.name_scope("enc_fc") as scope:
        image_size = IMAGE_SIZE // 16
        h_conv4_flatten = tf.reshape(h_conv4, [-1, image_size * image_size * 256])
        W_fc5 = utils.weight_variable([image_size * image_size * 256, 512], name="W_fc5")
        b_fc5 = utils.bias_variable([512], name="b_fc5")
        encoder_val = tf.matmul(h_conv4_flatten, W_fc5) + b_fc5

    return encoder_val
Esempio n. 5
0
def conv2d_layer(x,
                 name,
                 W_s,
                 pool_,
                 if_relu=False,
                 stride=2,
                 stddev=0.02,
                 if_dropout=False,
                 keep_prob_=1):
    '''Conv2d operator
    Args:
        pool_: if pool_==0:not pooling else pooling
    '''
    W = utils.weight_variable(W_s, stddev=stddev, name='W' + name)
    b = utils.bias_variable([W_s[3]], name='b' + name)
    #conv = utils.conv2d_strided_valid(x, W, b, stride)
    conv = utils.conv2d_strided(x, W, b, stride)

    print('shape after conv: ', conv.shape)
    print('--------------------------------')

    if if_dropout:
        conv = tf.nn.dropout(conv, keep_prob_)

    if if_relu:
        conv = tf.nn.relu(conv, name=name + '_relu')

    if pool_:
        conv = utils.max_pool(conv, pool_, 2)
        print('shape after pool: ', conv.shape)
    return conv
def inference_fully_convolutional(dataset):
    '''
    Fully convolutional inference on notMNIST dataset
    :param datset: [batch_size, 28*28*1] tensor
    :return: logits
    '''
    dataset_reshaped = tf.reshape(dataset, [-1, 28, 28, 1])
    with tf.name_scope("conv1") as scope:
        W_conv1 = utils.weight_variable_xavier_initialized([3, 3, 1, 32],
                                                           name="W_conv1")
        b_conv1 = utils.bias_variable([32], name="b_conv1")
        h_conv1 = tf.nn.relu(
            utils.conv2d_strided(dataset_reshaped, W_conv1, b_conv1))

    with tf.name_scope("conv2") as scope:
        W_conv2 = utils.weight_variable_xavier_initialized([3, 3, 32, 64],
                                                           name="W_conv2")
        b_conv2 = utils.bias_variable([64], name="b_conv2")
        h_conv2 = tf.nn.relu(utils.conv2d_strided(h_conv1, W_conv2, b_conv2))

    with tf.name_scope("conv3") as scope:
        W_conv3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128],
                                                           name="W_conv3")
        b_conv3 = utils.bias_variable([128], name="b_conv3")
        h_conv3 = tf.nn.relu(utils.conv2d_strided(h_conv2, W_conv3, b_conv3))

    with tf.name_scope("conv4") as scope:
        W_conv4 = utils.weight_variable_xavier_initialized([3, 3, 128, 256],
                                                           name="W_conv4")
        b_conv4 = utils.bias_variable([256], name="b_conv4")
        h_conv4 = tf.nn.relu(utils.conv2d_strided(h_conv3, W_conv4, b_conv4))

    with tf.name_scope("conv5") as scope:
        # W_conv5 = utils.weight_variable_xavier_initialized([2, 2, 256, 512], name="W_conv5")
        # b_conv5 = utils.bias_variable([512], name="b_conv5")
        # h_conv5 = tf.nn.relu(utils.conv2d_strided(h_conv4, W_conv5, b_conv5))
        h_conv5 = utils.avg_pool_2x2(h_conv4)

    with tf.name_scope("conv6") as scope:
        W_conv6 = utils.weight_variable_xavier_initialized([1, 1, 256, 10],
                                                           name="W_conv6")
        b_conv6 = utils.bias_variable([10], name="b_conv6")
        logits = tf.nn.relu(utils.conv2d_basic(h_conv5, W_conv6, b_conv6))
        print logits.get_shape()
        logits = tf.reshape(logits, [-1, 10])
    return logits
def encoder(dataset, train_mode):
    with tf.variable_scope("Encoder"):
        with tf.name_scope("enc_conv1") as scope:
            W_conv1 = utils.weight_variable_xavier_initialized([3, 3, 3, 32], name="W_conv1")
            b_conv1 = utils.bias_variable([32], name="b_conv1")
            h_conv1 = utils.conv2d_strided(dataset, W_conv1, b_conv1)
            h_bn1 = utils.batch_norm(h_conv1, 32, train_mode, scope="conv1_bn")
            h_relu1 = tf.nn.relu(h_bn1)

        with tf.name_scope("enc_conv2") as scope:
            W_conv2 = utils.weight_variable_xavier_initialized([3, 3, 32, 64], name="W_conv2")
            b_conv2 = utils.bias_variable([64], name="b_conv2")
            h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
            h_bn2 = utils.batch_norm(h_conv2, 64, train_mode, scope="conv2_bn")
            h_relu2 = tf.nn.relu(h_bn2)

        with tf.name_scope("enc_conv3") as scope:
            W_conv3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128], name="W_conv3")
            b_conv3 = utils.bias_variable([128], name="b_conv3")
            h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
            h_bn3 = utils.batch_norm(h_conv3, 128, train_mode, scope="conv3_bn")
            h_relu3 = tf.nn.relu(h_bn3)

        with tf.name_scope("enc_conv4") as scope:
            W_conv4 = utils.weight_variable_xavier_initialized([3, 3, 128, 256], name="W_conv4")
            b_conv4 = utils.bias_variable([256], name="b_conv4")
            h_conv4 = utils.conv2d_strided(h_relu3, W_conv4, b_conv4)
            h_bn4 = utils.batch_norm(h_conv4, 256, train_mode, scope="conv4_bn")
            h_relu4 = tf.nn.relu(h_bn4)

        with tf.name_scope("enc_conv5") as scope:
            W_conv5 = utils.weight_variable_xavier_initialized([3, 3, 256, 512], name="W_conv5")
            b_conv5 = utils.bias_variable([512], name="b_conv5")
            h_conv5 = utils.conv2d_strided(h_relu4, W_conv5, b_conv5)
            h_bn5 = utils.batch_norm(h_conv5, 512, train_mode, scope="conv5_bn")
            h_relu5 = tf.nn.relu(h_bn5)

        with tf.name_scope("enc_fc") as scope:
            image_size = IMAGE_SIZE // 32
            h_relu5_flatten = tf.reshape(h_relu5, [-1, image_size * image_size * 512])
            W_fc = utils.weight_variable([image_size * image_size * 512, 1024], name="W_fc")
            b_fc = utils.bias_variable([1024], name="b_fc")
            encoder_val = tf.matmul(h_relu5_flatten, W_fc) + b_fc

    return encoder_val
def discriminator(input_images, train_mode):
    # dropout_prob = 1.0
    # if train_mode:
    #     dropout_prob = 0.5
    W_conv0 = utils.weight_variable([5, 5, NUM_OF_CHANNELS, 64 * 1],
                                    name="W_conv0")
    b_conv0 = utils.bias_variable([64 * 1], name="b_conv0")
    h_conv0 = utils.conv2d_strided(input_images, W_conv0, b_conv0)
    h_bn0 = h_conv0  # utils.batch_norm(h_conv0, 64 * 1, train_mode, scope="disc_bn0")
    h_relu0 = utils.leaky_relu(h_bn0, 0.2, name="h_relu0")
    utils.add_activation_summary(h_relu0)

    W_conv1 = utils.weight_variable([5, 5, 64 * 1, 64 * 2], name="W_conv1")
    b_conv1 = utils.bias_variable([64 * 2], name="b_conv1")
    h_conv1 = utils.conv2d_strided(h_relu0, W_conv1, b_conv1)
    h_bn1 = utils.batch_norm(h_conv1, 64 * 2, train_mode, scope="disc_bn1")
    h_relu1 = utils.leaky_relu(h_bn1, 0.2, name="h_relu1")
    utils.add_activation_summary(h_relu1)

    W_conv2 = utils.weight_variable([5, 5, 64 * 2, 64 * 4], name="W_conv2")
    b_conv2 = utils.bias_variable([64 * 4], name="b_conv2")
    h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
    h_bn2 = utils.batch_norm(h_conv2, 64 * 4, train_mode, scope="disc_bn2")
    h_relu2 = utils.leaky_relu(h_bn2, 0.2, name="h_relu2")
    utils.add_activation_summary(h_relu2)

    W_conv3 = utils.weight_variable([5, 5, 64 * 4, 64 * 8], name="W_conv3")
    b_conv3 = utils.bias_variable([64 * 8], name="b_conv3")
    h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
    h_bn3 = utils.batch_norm(h_conv3, 64 * 8, train_mode, scope="disc_bn3")
    h_relu3 = utils.leaky_relu(h_bn3, 0.2, name="h_relu3")
    utils.add_activation_summary(h_relu3)

    shape = h_relu3.get_shape().as_list()
    h_3 = tf.reshape(
        h_relu3,
        [FLAGS.batch_size, (IMAGE_SIZE // 16) * (IMAGE_SIZE // 16) * shape[3]])
    W_4 = utils.weight_variable([h_3.get_shape().as_list()[1], 1], name="W_4")
    b_4 = utils.bias_variable([1], name="b_4")
    h_4 = tf.matmul(h_3, W_4) + b_4

    return tf.nn.sigmoid(h_4), h_4, h_relu3
def inference_strided(input_image):
    W1 = utils.weight_variable([9, 9, 3, 32])
    b1 = utils.bias_variable([32])
    tf.histogram_summary("W1", W1)
    tf.histogram_summary("b1", b1)
    h_conv1 = tf.nn.relu(utils.conv2d_basic(input_image, W1, b1))

    W2 = utils.weight_variable([3, 3, 32, 64])
    b2 = utils.bias_variable([64])
    tf.histogram_summary("W2", W2)
    tf.histogram_summary("b2", b2)
    h_conv2 = tf.nn.relu(utils.conv2d_strided(h_conv1, W2, b2))

    W3 = utils.weight_variable([3, 3, 64, 128])
    b3 = utils.bias_variable([128])
    tf.histogram_summary("W3", W3)
    tf.histogram_summary("b3", b3)
    h_conv3 = tf.nn.relu(utils.conv2d_strided(h_conv2, W3, b3))

    # upstrides
    W4 = utils.weight_variable([3, 3, 64, 128])
    b4 = utils.bias_variable([64])
    tf.histogram_summary("W4", W4)
    tf.histogram_summary("b4", b4)
    # print h_conv3.get_shape()
    # print W4.get_shape()
    h_conv4 = tf.nn.relu(utils.conv2d_transpose_strided(h_conv3, W4, b4))

    W5 = utils.weight_variable([3, 3, 32, 64])
    b5 = utils.bias_variable([32])
    tf.histogram_summary("W5", W5)
    tf.histogram_summary("b5", b5)
    h_conv5 = tf.nn.relu(utils.conv2d_transpose_strided(h_conv4, W5, b5))

    W6 = utils.weight_variable([9, 9, 32, 3])
    b6 = utils.bias_variable([3])
    tf.histogram_summary("W6", W6)
    tf.histogram_summary("b6", b6)
    pred_image = tf.nn.tanh(utils.conv2d_basic(h_conv5, W6, b6))

    return pred_image
def inference_fully_convolutional(dataset):
    '''
    Fully convolutional inference on notMNIST dataset
    :param datset: [batch_size, 28*28*1] tensor
    :return: logits
    '''
    dataset_reshaped = tf.reshape(dataset, [-1, 28, 28, 1])
    with tf.name_scope("conv1") as scope:
        W_conv1 = utils.weight_variable_xavier_initialized([3, 3, 1, 32], name="W_conv1")
        b_conv1 = utils.bias_variable([32], name="b_conv1")
        h_conv1 = tf.nn.relu(utils.conv2d_strided(dataset_reshaped, W_conv1, b_conv1))

    with tf.name_scope("conv2") as scope:
        W_conv2 = utils.weight_variable_xavier_initialized([3, 3, 32, 64], name="W_conv2")
        b_conv2 = utils.bias_variable([64], name="b_conv2")
        h_conv2 = tf.nn.relu(utils.conv2d_strided(h_conv1, W_conv2, b_conv2))

    with tf.name_scope("conv3") as scope:
        W_conv3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128], name="W_conv3")
        b_conv3 = utils.bias_variable([128], name="b_conv3")
        h_conv3 = tf.nn.relu(utils.conv2d_strided(h_conv2, W_conv3, b_conv3))

    with tf.name_scope("conv4") as scope:
        W_conv4 = utils.weight_variable_xavier_initialized([3, 3, 128, 256], name="W_conv4")
        b_conv4 = utils.bias_variable([256], name="b_conv4")
        h_conv4 = tf.nn.relu(utils.conv2d_strided(h_conv3, W_conv4, b_conv4))

    with tf.name_scope("conv5") as scope:
        # W_conv5 = utils.weight_variable_xavier_initialized([2, 2, 256, 512], name="W_conv5")
        # b_conv5 = utils.bias_variable([512], name="b_conv5")
        # h_conv5 = tf.nn.relu(utils.conv2d_strided(h_conv4, W_conv5, b_conv5))
        h_conv5 = utils.avg_pool_2x2(h_conv4)

    with tf.name_scope("conv6") as scope:
        W_conv6 = utils.weight_variable_xavier_initialized([1, 1, 256, 10], name="W_conv6")
        b_conv6 = utils.bias_variable([10], name="b_conv6")
        logits = tf.nn.relu(utils.conv2d_basic(h_conv5, W_conv6, b_conv6))
        print logits.get_shape()
        logits = tf.reshape(logits, [-1, 10])
    return logits
Esempio n. 11
0
def discriminator(input_images, train_mode):
    # dropout_prob = 1.0
    # if train_mode:
    #     dropout_prob = 0.5
    W_conv0 = utils.weight_variable([5, 5, NUM_OF_CHANNELS, 64 * 1], name="W_conv0")
    b_conv0 = utils.bias_variable([64 * 1], name="b_conv0")
    h_conv0 = utils.conv2d_strided(input_images, W_conv0, b_conv0)
    h_bn0 = h_conv0  # utils.batch_norm(h_conv0, 64 * 1, train_mode, scope="disc_bn0")
    h_relu0 = utils.leaky_relu(h_bn0, 0.2, name="h_relu0")
    utils.add_activation_summary(h_relu0)

    W_conv1 = utils.weight_variable([5, 5, 64 * 1, 64 * 2], name="W_conv1")
    b_conv1 = utils.bias_variable([64 * 2], name="b_conv1")
    h_conv1 = utils.conv2d_strided(h_relu0, W_conv1, b_conv1)
    h_bn1 = utils.batch_norm(h_conv1, 64 * 2, train_mode, scope="disc_bn1")
    h_relu1 = utils.leaky_relu(h_bn1, 0.2, name="h_relu1")
    utils.add_activation_summary(h_relu1)

    W_conv2 = utils.weight_variable([5, 5, 64 * 2, 64 * 4], name="W_conv2")
    b_conv2 = utils.bias_variable([64 * 4], name="b_conv2")
    h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
    h_bn2 = utils.batch_norm(h_conv2, 64 * 4, train_mode, scope="disc_bn2")
    h_relu2 = utils.leaky_relu(h_bn2, 0.2, name="h_relu2")
    utils.add_activation_summary(h_relu2)

    W_conv3 = utils.weight_variable([5, 5, 64 * 4, 64 * 8], name="W_conv3")
    b_conv3 = utils.bias_variable([64 * 8], name="b_conv3")
    h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
    h_bn3 = utils.batch_norm(h_conv3, 64 * 8, train_mode, scope="disc_bn3")
    h_relu3 = utils.leaky_relu(h_bn3, 0.2, name="h_relu3")
    utils.add_activation_summary(h_relu3)

    shape = h_relu3.get_shape().as_list()
    h_3 = tf.reshape(h_relu3, [FLAGS.batch_size, (IMAGE_SIZE // 16) * (IMAGE_SIZE // 16) * shape[3]])
    W_4 = utils.weight_variable([h_3.get_shape().as_list()[1], 1], name="W_4")
    b_4 = utils.bias_variable([1], name="b_4")
    h_4 = tf.matmul(h_3, W_4) + b_4

    return tf.nn.sigmoid(h_4), h_4, h_relu3
Esempio n. 12
0
def conv2d_layer(x, name, W_s, pool_, if_relu=False, stride=2):
    '''Conv2d operator
    Args:
        pool_: if pool_==0:not pooling else pooling
    '''
    W = utils.weight_variable(W_s, name='W'+name)
    b = utils.bias_variable([W_s[3]], name='b'+name)
    conv = utils.conv2d_strided(x, W, b, stride)

    if if_relu:
        conv = tf.nn.relu(conv, name=name+'_relu')

    if pool_:
        conv = utils.max_pool(conv, pool_, 2)

    return conv
Esempio n. 13
0
def inference(image, keep_prob):
    """
    Semantic segmentation network definition    # 语义分割网络定义
    :param image: input image. Should have values in range 0-255
    :param keep_prob:
    :return:
    """
    # 获取预训练网络VGG
    print("setting up vgg initialized conv layers ...")
    # model_dir Model_zoo/
    # MODEL_URL 下载VGG19网址
    model_data = utils.get_model_data(FLAGS.model_dir,
                                      MODEL_URL)  # 返回VGG19模型中内容

    mean = model_data['normalization'][0][0][0]  # 获得图像均值
    mean_pixel = np.mean(mean, axis=(0, 1))  # RGB

    weights = np.squeeze(model_data['layers'])  # 压缩VGG网络中参数,把维度是1的维度去掉 剩下的就是权重

    processed_image = utils.process_image(image, mean_pixel)  # 图像减均值

    with tf.variable_scope("inference"):  # 命名作用域 是inference
        image_net = vgg_net(weights, processed_image)  # 传入权重参数和预测图像,获得所有层输出结果
        # conv_final_layer = image_net["conv5_3"]                         # 获得输出结果
        conv_final_layer = image_net["relu4_4"]
        w5_0 = utils.weight_variable([3, 3, 512, 512],
                                     name="W5_0")  #取消pool4降采样操作,改成3*3/s1
        b5_0 = utils.bias_variable([512], name="b5_0")
        conv5_0 = utils.conv2d_strided(conv_final_layer, w5_0, b5_0)

        w5_1 = utils.weight_variable([3, 3, 512, 512], name="W5_1")
        b5_1 = utils.bias_variable([512], name="b5_1")
        conv5_1 = utils.conv2d_atrous_2(conv5_0, w5_1, b5_1)
        w5_2 = utils.weight_variable([3, 3, 512, 512],
                                     name="W5_2")  #将第五层的conv5_1,2,3改成2-空洞卷积
        b5_2 = utils.bias_variable([512], name="b5_2")
        conv5_2 = utils.conv2d_atrous_2(conv5_1, w5_2, b5_2)
        w5_3 = utils.weight_variable([3, 3, 512, 512], name="W5_3")
        b5_3 = utils.bias_variable([512], name="b5_3")
        conv5_3 = utils.conv2d_atrous_2(conv5_2, w5_3, b5_3)
        # pool5 = utils.max_pool_2x2(conv_final_layer)                    # /32 缩小32倍

        # W6 = utils.weight_variable([7, 7, 512, 4096], name="W6")        # 初始化第6层的w b
        # b6 = utils.bias_variable([4096], name="b6")
        # conv6 = utils.conv2d_basic(pool5, W6, b6)
        # relu6 = tf.nn.relu(conv6, name="relu6")
        # if FLAGS.debug:
        #     utils.add_activation_summary(relu6)
        # relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob)

        w6_0 = utils.weight_variable([3, 3, 512, 4096],
                                     name="W6_0")  # 取消pool5降采样操作,改成3*3/s1
        b6_0 = utils.bias_variable([4096], name="b6_0")
        conv6_0 = utils.conv2d_strided(conv5_3, w6_0, b6_0)

        w6 = utils.weight_variable([3, 3, 4096, 4096], name="W7")
        b6 = utils.bias_variable([4096], name="b6")  #第6层为4-空洞卷积
        conv6 = utils.conv2d_atrous_4(conv6_0, w6, b6)
        relu6 = tf.nn.relu(conv6, name="relu6")
        if FLAGS.debug:
            utils.add_activation_summary(relu6)
        relu_dropout6 = tf.nn.dropout(relu6, keep_prob=keep_prob)
        # W7 = utils.weight_variable([1, 1, 4096, 4096], name="W7")       # 第7层卷积层
        # b7 = utils.bias_variable([4096], name="b7")
        # conv7 = utils.conv2d_basic(relu_dropout6, W7, b7)
        # relu7 = tf.nn.relu(conv7, name="relu7")
        # if FLAGS.debug:
        #     utils.add_activation_summary(relu7)
        # relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob)
        w7 = utils.weight_variable([1, 1, 4096, 4096], name="w7")
        b7 = utils.bias_variable([4096], name="b7")  #第7层为4—空洞卷积
        conv7 = utils.conv2d_atrous_4(relu_dropout6, w7, b7)
        relu7 = tf.nn.relu(conv7, name="relu6")
        if FLAGS.debug:
            utils.add_activation_summary(relu7)
        relu_dropout7 = tf.nn.dropout(relu7, keep_prob=keep_prob)

        # W8 = utils.weight_variable([1, 1, 4096, NUM_OF_CLASSESS], name="W8")
        # b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8")
        # conv8 = utils.conv2d_basic(relu_dropout7, W8, b8)               # 第8层卷积层 分类151类
        # # annotation_pred1 = tf.argmax(conv8, dimension=3, name="prediction1")
        w8 = utils.weight_variable([1, 1, 4096, NUM_OF_CLASSESS], name="W8")
        b8 = utils.bias_variable([NUM_OF_CLASSESS], name="b8")  # 第8层为4—空洞卷积
        conv8 = utils.conv2d_atrous_4(relu_dropout7, w8, b8)
        # conv8 = utils.max_pool_2x2(conv8)
        print(conv8.shape)
        # now to upscale to actual image size
        # deconv_shape1 = image_net["pool4"].get_shape()                  # 将pool4 1/16结果尺寸拿出来 做融合 [b,h,w,c]
        # # 定义反卷积层的 W,B [H, W, OUTC, INC]  输出个数为pool4层通道个数,输入为conv8通道个数
        # # 扩大两倍  所以stride = 2  kernel_size = 4
        # W_t1 = utils.weight_variable([4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1")
        # b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1")
        # # 输入为conv8特征图,使得其特征图大小扩大两倍,并且特征图个数变为pool4的通道数
        # conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=tf.shape(image_net["pool4"]))
        # fuse_1 = tf.add(conv_t1, image_net["pool4"], name="fuse_1")     # 进行融合 逐像素相加

        deconv_shape1 = image_net["pool4"].get_shape(
        )  # 将pool4 1/16结果尺寸拿出来 做融合 [b,h,w,c]
        # 定义反卷积层的 W,B [H, W, OUTC, INC]  输出个数为pool4层通道个数,输入为conv8通道个数
        # 扩大两倍  所以stride = 2  kernel_size = 4
        W_t1 = utils.weight_variable(
            [4, 4, deconv_shape1[3].value, NUM_OF_CLASSESS], name="W_t1")
        b_t1 = utils.bias_variable([deconv_shape1[3].value], name="b_t1")
        # 输入为conv8特征图,使得其特征图大小扩大两倍,并且特征图个数变为pool4的通道数
        conv_t1 = utils.conv2d_transpose_strided(conv8,
                                                 W_t1,
                                                 b_t1,
                                                 output_shape=tf.shape(
                                                     image_net["pool4"]))
        fuse_1 = tf.add(conv_t1, image_net["pool4"],
                        name="fuse_1")  # 进行融合 逐像素相加

        # 获得pool3尺寸 是原图大小的1/8
        deconv_shape2 = image_net["pool3"].get_shape()
        # 输出通道数为pool3通道数,  输入通道数为pool4通道数
        W_t2 = utils.weight_variable(
            [4, 4, deconv_shape2[3].value, deconv_shape1[3].value],
            name="W_t2")
        b_t2 = utils.bias_variable([deconv_shape2[3].value], name="b_t2")
        # 将上一层融合结果fuse_1在扩大两倍,输出尺寸和pool3相同
        conv_t2 = utils.conv2d_transpose_strided(fuse_1,
                                                 W_t2,
                                                 b_t2,
                                                 output_shape=tf.shape(
                                                     image_net["pool3"]))
        # 融合操作deconv(fuse_1) + pool3
        fuse_2 = tf.add(conv_t2, image_net["pool3"], name="fuse_2")

        shape = tf.shape(image)  # 获得原始图像大小
        # 堆叠列表,反卷积输出尺寸,[b,原图H,原图W,类别个数]
        deconv_shape3 = tf.stack(
            [shape[0], shape[1], shape[2], NUM_OF_CLASSESS])
        # 建立反卷积w[8倍扩大需要ks=16, 输出通道数为类别个数, 输入通道数pool3通道数]
        W_t3 = utils.weight_variable(
            [16, 16, deconv_shape2[3].value, NUM_OF_CLASSESS], name="W_t3")
        b_t3 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t3")
        # 反卷积,fuse_2反卷积,输出尺寸为 [b,原图H,原图W,类别个数]
        conv_t3 = utils.conv2d_transpose_strided(fuse_2,
                                                 W_t3,
                                                 b_t3,
                                                 output_shape=deconv_shape3,
                                                 stride=8)

        # deconv_shape3 = tf.stack([shape[0], shape[1], shape[2], NUM_OF_CLASSESS])
        # # 建立反卷积w[8倍扩大需要ks=16, 输出通道数为类别个数, 输入通道数pool3通道数]
        # W_t1 = utils.weight_variable([16, 16, NUM_OF_CLASSESS, NUM_OF_CLASSESS], name="W_t1")  ##反卷积生成原图大小
        # b_t1 = utils.bias_variable([NUM_OF_CLASSESS], name="b_t1")
        # # 反卷积,fuse_2反卷积,输出尺寸为 [b,原图H,原图W,类别个数]
        # conv_t1 = utils.conv2d_transpose_strided(conv8, W_t1, b_t1, output_shape=deconv_shape3, stride=8)

        # 目前conv_t3的形式为size为和原始图像相同的size,通道数与分类数相同
        # 这句我的理解是对于每个像素位置,根据第3维度(通道数)通过argmax能计算出这个像素点属于哪个分类
        # 也就是对于每个像素而言,NUM_OF_CLASSESS个通道中哪个数值最大,这个像素就属于哪个分类
        # 每个像素点有21个值,哪个值最大就属于那一类
        # 返回一张图,每一个点对于其来别信息shape=[b,h,w]
        # annotation_pred = tf.argmax(conv_t1, dimension=3, name="prediction")
        annotation_pred = tf.argmax(conv_t3, axis=3, name="prediction")
    # 从第三维度扩展 形成[b,h,w,c] 其中c=1, conv_t3最后具有21深度的特征图
    # return tf.expand_dims(annotation_pred, dim=3), conv_t1

    return tf.expand_dims(annotation_pred, axis=3), conv_t3
Esempio n. 14
0
def res_net(weights, image):
    layers = ('conv1', 'res2a', 'res2b', 'res2c', 'res3a', 'res3b1', 'res3b2',
              'res3b3', 'res4a', 'res4b1', 'res4b2', 'res4b3', 'res4b4',
              'res4b5', 'res4b6', 'res4b7', 'res4b8', 'res4b9', 'res4b10',
              'res4b11', 'res4b12', 'res4b13', 'res4b14', 'res4b15', 'res4b16',
              'res4b17', 'res4b18', 'res4b19', 'res4b20', 'res4b21', 'res4b22',
              'res5a', 'res5b', 'res5c')
    res_branch1_parm = {
        #branch1:padding(if 0 then 'VALID' else 'SMAE'),stride
        'res2a': [0, 1, 0, 1, 1, 1, 0, 1],
        'res3a': [0, 2, 0, 2, 1, 1, 0, 1],
        'res4a': [0, 2, 0, 2, 1, 1, 0, 1],
        'res5a': [0, 2, 0, 2, 1, 1, 0, 1]
    }

    net = {}
    current = image
    n = 0
    for i, name in enumerate(layers):
        kind = name[:3]
        print('-----------------layer: %s-----------------------' % name)
        print('Input: ', current.shape)
        #convolutional
        if kind == 'con':
            kernels, bias, n = get_kernel_bias(n, weights, name)
            print('kernel size: ', kernels.shape, 'bias size', bias.shape)
            # matconvnet: weights are [width, height, in_channels, out_channels]
            # tensorflow: weights are [height, width, in_channels, out_channels]
            current = utils.conv2d_strided(current, kernels, bias)
            current = utils.max_pool_2x2(current, 3)
        #resnet
        elif kind == 'res':
            sub_kind = name[4]
            #not blockneck
            if sub_kind == 'a':
                res_param = res_branch1_parm[name]
                branch1_name = '%s_%s' % (name, 'branch1')
                branch1_w, branch1_b, out_chan_t, n = get_kernel_bias_res(
                    n, weights, branch1_name, 2)
                print('branch1:kernel size: ', branch1_w.shape, 'bias size',
                      branch1_b.shape)
            else:
                res_param = None
                branch1_w = None
            branch2a_name = '%s_%s' % (name, 'branch2a')
            branch2a_w, branch2a_b, out_chan_t2, n = get_kernel_bias_res(
                n, weights, branch2a_name, 0)
            print('branch2a:kernel size: ', branch2a_w.shape, 'bias size',
                  branch2a_b.shape)
            branch2b_name = '%s_%s' % (name, 'branch2b')
            branch2b_w, branch2b_b, _, n = get_kernel_bias_res(
                n, weights, branch2b_name, 0)
            print('branch2b:kernel size: ', branch2b_w.shape, 'bias size',
                  branch2b_b.shape)
            branch2c_name = '%s_%s' % (name, 'branch2c')
            branch2c_w, branch2c_b, out_chan2, n = get_kernel_bias_res(
                n, weights, branch2c_name, 3)
            print('branch2c:kernel size: ', branch2c_w.shape, 'bias size',
                  branch2c_b.shape)
            if sub_kind == 'a':
                out_chan1 = out_chan_t
            else:
                out_chan1 = out_chan_t2
            current = utils.bottleneck_unit(current, res_param, branch1_w,
                                            branch1_b, branch2a_w, branch2a_b,
                                            branch2b_w, branch2b_b, branch2c_w,
                                            branch2c_b, out_chan1, out_chan2,
                                            False, False, name)
        print('layer output ', current.shape)
        net[name] = current
    current = utils.avg_pool(current, 7, 1)
    print('resnet final sz ', current.shape)
    #return net
    return current
def encoder(dataset, train_mode):
    with tf.variable_scope("Encoder"):
        with tf.name_scope("enc_conv1") as scope:
            W_conv1 = utils.weight_variable_xavier_initialized([3, 3, 3, 32],
                                                               name="W_conv1")
            b_conv1 = utils.bias_variable([32], name="b_conv1")
            h_conv1 = utils.conv2d_strided(dataset, W_conv1, b_conv1)
            h_bn1 = utils.batch_norm(h_conv1, 32, train_mode, scope="conv1_bn")
            h_relu1 = tf.nn.relu(h_bn1)

        with tf.name_scope("enc_conv2") as scope:
            W_conv2 = utils.weight_variable_xavier_initialized([3, 3, 32, 64],
                                                               name="W_conv2")
            b_conv2 = utils.bias_variable([64], name="b_conv2")
            h_conv2 = utils.conv2d_strided(h_relu1, W_conv2, b_conv2)
            h_bn2 = utils.batch_norm(h_conv2, 64, train_mode, scope="conv2_bn")
            h_relu2 = tf.nn.relu(h_bn2)

        with tf.name_scope("enc_conv3") as scope:
            W_conv3 = utils.weight_variable_xavier_initialized([3, 3, 64, 128],
                                                               name="W_conv3")
            b_conv3 = utils.bias_variable([128], name="b_conv3")
            h_conv3 = utils.conv2d_strided(h_relu2, W_conv3, b_conv3)
            h_bn3 = utils.batch_norm(h_conv3,
                                     128,
                                     train_mode,
                                     scope="conv3_bn")
            h_relu3 = tf.nn.relu(h_bn3)

        with tf.name_scope("enc_conv4") as scope:
            W_conv4 = utils.weight_variable_xavier_initialized(
                [3, 3, 128, 256], name="W_conv4")
            b_conv4 = utils.bias_variable([256], name="b_conv4")
            h_conv4 = utils.conv2d_strided(h_relu3, W_conv4, b_conv4)
            h_bn4 = utils.batch_norm(h_conv4,
                                     256,
                                     train_mode,
                                     scope="conv4_bn")
            h_relu4 = tf.nn.relu(h_bn4)

        with tf.name_scope("enc_conv5") as scope:
            W_conv5 = utils.weight_variable_xavier_initialized(
                [3, 3, 256, 512], name="W_conv5")
            b_conv5 = utils.bias_variable([512], name="b_conv5")
            h_conv5 = utils.conv2d_strided(h_relu4, W_conv5, b_conv5)
            h_bn5 = utils.batch_norm(h_conv5,
                                     512,
                                     train_mode,
                                     scope="conv5_bn")
            h_relu5 = tf.nn.relu(h_bn5)

        with tf.name_scope("enc_fc") as scope:
            image_size = IMAGE_SIZE // 32
            h_relu5_flatten = tf.reshape(h_relu5,
                                         [-1, image_size * image_size * 512])
            W_fc = utils.weight_variable([image_size * image_size * 512, 1024],
                                         name="W_fc")
            b_fc = utils.bias_variable([1024], name="b_fc")
            encoder_val = tf.matmul(h_relu5_flatten, W_fc) + b_fc

    return encoder_val