示例#1
0
def inference(x, y_, keep_prob, phase_train):
    with tf.variable_scope('conv_1'):
        conv1 = Convolution2D(x, (28, 28), 1, 32, (5, 5), activation='none')
        conv1_bn = batch_norm(conv1.output(), 32, phase_train)
        conv1_out = tf.nn.relu(conv1_bn)

        pool1 = MaxPooling2D(conv1_out)
        pool1_out = pool1.output()

    with tf.variable_scope('conv_2'):
        conv2 = Convolution2D(pool1_out, (14, 14),
                              32,
                              64, (5, 5),
                              activation='none')
        conv2_bn = batch_norm(conv2.output(), 64, phase_train)
        conv2_out = tf.nn.relu(conv2_bn)

        pool2 = MaxPooling2D(conv2_out)
        pool2_out = pool2.output()

        pool2_flat = tf.reshape(pool2_out, [-1, 7 * 7 * 64])

    with tf.variable_scope('fc1'):
        fc1 = FullConnected(pool2_flat, 7 * 7 * 64, 1024)
        fc1_out = fc1.output()
        fc1_dropped = tf.nn.dropout(fc1_out, keep_prob)

    y_pred = ReadOutLayer(fc1_dropped, 1024, 10).output()

    loss = tf.reduce_mean(
        -tf.reduce_sum(y_ * tf.log(y_pred), reduction_indices=[1]))
    accuracy = evaluation(y_pred, y_)

    return loss, accuracy, y_pred
示例#2
0
def mk_nn_model(x, y_):
    # Encoding phase
    # x_image = tf.reshape(x, [-1, 28, 28, 1]
    x_image = x
    conv1 = Convolution2D(x_image, (28, 28), 1, 16, (3, 3), activation='relu')
    conv1_out = conv1.output()

    pool1 = MaxPooling2D(conv1_out)
    pool1_out = pool1.output()

    conv2 = Convolution2D(pool1_out, (14, 14),
                          16,
                          8, (3, 3),
                          activation='relu')
    conv2_out = conv2.output()

    pool2 = MaxPooling2D(conv2_out)
    pool2_out = pool2.output()

    conv3 = Convolution2D(pool2_out, (7, 7), 8, 8, (3, 3), activation='relu')
    conv3_out = conv3.output()

    pool3 = MaxPooling2D(conv3_out)
    pool3_out = pool3.output()
    # at this point the representation is (8, 4, 4) i.e. 128-dimensional
    # Decoding phase
    conv_t1 = Conv2Dtranspose(pool3_out, (7, 7),
                              8,
                              8, (3, 3),
                              activation='relu')
    conv_t1_out = conv_t1.output()

    conv_t2 = Conv2Dtranspose(conv_t1_out, (14, 14),
                              8,
                              8, (3, 3),
                              activation='relu')
    conv_t2_out = conv_t2.output()

    conv_t3 = Conv2Dtranspose(conv_t2_out, (28, 28),
                              8,
                              16, (3, 3),
                              activation='relu')
    conv_t3_out = conv_t3.output()

    conv_last = Convolution2D(conv_t3_out, (28, 28),
                              16,
                              1, (3, 3),
                              activation='sigmoid')
    decoded = conv_last.output()

    decoded = tf.reshape(decoded, [-1, 784])
    cross_entropy = -1. * x * tf.log(decoded) - (1. - x) * tf.log(1. - decoded)
    loss = tf.reduce_mean(cross_entropy)

    return loss, decoded
示例#3
0
 def create_encoder_conv(self, conf):
     for i in range(len(conf)):
         conv = Convolution2D(self.layers[-1],
                              (self.layers[-1].get_shape().as_list()[1],
                               self.layers[-1].get_shape().as_list()[2]),
                              self.layers[-1].get_shape().as_list()[3],
                              conf[i], (2, 6),
                              activation='leaky_relu')
         self.layers.append(conv.output())
         pool = MaxPooling2D(self.layers[-1])
         self.layers.append(pool.output())
示例#4
0
def mk_nn_model(x, y_):
    # Encoding phase
    x_image = tf.reshape(x, [-1, 227, 227, 3])

    #1st conv.
    conv1 = Convolution2D(x_image, (227, 227),
                          3,
                          96, (7, 7),
                          activation='relu',
                          S=4)
    conv1_out = conv1.output()

    #1st pooling
    pool1 = MaxPooling2D(conv1_out, ksize=[1, 3, 3, 1], S=2)
    pool1_out = pool1.output()

    #dropout?
    #    pool1_out = tf.nn.dropout(pool1_out,keep_prob=0.2)

    #LRN1
    norm1 = tf.nn.local_response_normalization(pool1_out,
                                               depth_radius=5,
                                               alpha=0.0001,
                                               beta=0.75)

    #2nd conv.
    conv2 = Convolution2D(norm1, (29, 29),
                          96,
                          256, (5, 5),
                          activation='relu',
                          S=1)  # pad=2 - how to do it????
    conv2_out = conv2.output()

    #2nd pooling
    pool2 = MaxPooling2D(conv2_out, ksize=(1, 3, 3, 1), S=2)
    pool2_out = pool2.output()
    #    pool2_out = tf.nn.dropout(pool2_out,keep_prob=0.2)
    norm2 = tf.nn.local_response_normalization(pool2_out,
                                               depth_radius=5,
                                               alpha=0.0001,
                                               beta=0.75)

    #3rd conv.
    conv3 = Convolution2D(norm2, (15, 15),
                          256,
                          384, (3, 3),
                          activation='relu',
                          S=1)  # pad=2 - how to do it????
    conv3_out = conv3.output()

    #3rd pooling
    pool3 = MaxPooling2D(conv3_out, ksize=(1, 3, 3, 1), S=2)
    pool3_out = pool3.output()

    # at this point the representation is (4, 28, 28) i.e. 128*16-dimensional
    po = tf.reshape(pool3_out, [-1, 384 * 8 * 8])

    fc6 = FullConnected(po, 384 * 8 * 8, 512, activation='relu')
    fc6_out = fc6.output()

    drop6 = tf.nn.dropout(fc6_out, keep_prob=0.5)

    fc7 = FullConnected(drop6, 512, 512, activation='relu')
    fc7_out = fc7.output()

    drop7 = tf.nn.dropout(fc7_out, keep_prob=0.5)

    fc8 = FullConnected(drop7, 512, 8, activation='relu')
    fc8_out = fc8.output()

    # crossentry for  classifier
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(labels=y_,
                                                            logits=fc8_out)
    loss = tf.reduce_mean(cross_entropy)

    # accuracy of the trained model, between 0 (worst) and 1 (best)
    correct_prediction = tf.equal(tf.argmax(fc8_out, 1), tf.argmax(y_, 1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    return loss, accuracy, fc8_out
示例#5
0
def mk_nn_model(x_image, y_image, encoded_y_image=None):
    # Encoding phase
    conv1 = Convolution2D(x_image, (28, 28), 1, 16, (3, 3), activation='relu')
    conv1_out = conv1.output()

    pool1 = MaxPooling2D(conv1_out)
    pool1_out = pool1.output()

    conv2 = Convolution2D(pool1_out, (14, 14),
                          16,
                          8, (3, 3),
                          activation='relu')
    conv2_out = conv2.output()

    pool2 = MaxPooling2D(conv2_out)
    pool2_out = pool2.output()

    conv3 = Convolution2D(pool2_out, (7, 7), 8, 8, (3, 3), activation='relu')
    conv3_out = conv3.output()

    pool3 = MaxPooling2D(conv3_out, name='encoded')
    pool3_out = pool3.output()

    encode = Convolution2D(pool3_out, (4, 4),
                           8,
                           latent_num, (2, 2),
                           activation='relu')
    encode_out = encode.output()
    encoded = encode_out
    # print(encoded.shape)
    # at this point the representation is (8, 4, 4) i.e. 128-dimensional
    # Decoding phase
    conv_t1 = Conv2Dtranspose(encode_out, (7, 7),
                              latent_num,
                              8, (3, 3),
                              activation='relu')
    conv_t1_out = conv_t1.output()

    conv_t2 = Conv2Dtranspose(conv_t1_out, (14, 14),
                              8,
                              8, (3, 3),
                              activation='relu')
    conv_t2_out = conv_t2.output()

    conv_t3 = Conv2Dtranspose(conv_t2_out, (28, 28),
                              8,
                              16, (3, 3),
                              activation='relu')
    conv_t3_out = conv_t3.output()

    conv_last = Convolution2D(conv_t3_out, (28, 28),
                              16,
                              1, (3, 3),
                              activation='sigmoid')
    decoded = conv_last.output()
    print(decoded.shape)

    # decoded = tf.reshape(decoded, [-1, 784])
    cross_entropy = -1. * y_image * tf.log(decoded) - (
        1. - y_image) * tf.log(1. - decoded)
    loss = tf.reduce_mean(cross_entropy)
    tf.summary.scalar('loss', loss)

    # encod_loss = 0
    # if encoded_y_image is not None:
    # encoded_cross_entropy = -1. * encoded_y_image * tf.log(encoded) - (1. - encoded_y_image) * tf.log(1. - encoded)
    # encod_loss = tf.reduce_mean(encoded_cross_entropy)
    encod_loss = tf.reduce_sum(tf.square(encoded_y_image - encoded))
    tf.summary.scalar('encoded_loss', encod_loss)
    merged = tf.summary.merge_all()

    return loss, decoded, encoded, encod_loss, merged
示例#6
0
def mk_nn_model(x_image, y_image, encoded_y_image=None):
    # Encoding phase
    conv1 = Convolution2D(x_image, (28, 28), 1, 16, (3, 3), activation='relu')
    conv1_out = conv1.output()

    pool1 = MaxPooling2D(conv1_out)
    pool1_out = pool1.output()

    conv2 = Convolution2D(pool1_out, (14, 14),
                          16,
                          8, (3, 3),
                          activation='relu')
    conv2_out = conv2.output()

    pool2 = MaxPooling2D(conv2_out)
    pool2_out = pool2.output()

    conv3 = Convolution2D(pool2_out, (7, 7), 8, 1, (3, 3), activation='relu')
    conv3_out = conv3.output()

    pool3 = MaxPooling2D(conv3_out, name='encoded')
    pool3_out = pool3.output()
    encoded = pool3_out
    # at this point the representation is (1, 4, 4) i.e. 128-dimensional
    # Decoding phase
    conv_t1 = Conv2Dtranspose(pool3_out, (7, 7),
                              1,
                              8, (3, 3),
                              activation='relu')
    conv_t1_out = conv_t1.output()

    conv_t2 = Conv2Dtranspose(conv_t1_out, (14, 14),
                              8,
                              8, (3, 3),
                              activation='relu')
    conv_t2_out = conv_t2.output()

    conv_t3 = Conv2Dtranspose(conv_t2_out, (28, 28),
                              8,
                              16, (3, 3),
                              activation='relu')
    conv_t3_out = conv_t3.output()

    conv_last = Convolution2D(conv_t3_out, (28, 28),
                              16,
                              1, (3, 3),
                              activation='sigmoid')
    decoded = conv_last.output()
    print(decoded.shape)

    # decoded = tf.reshape(decoded, [-1, 784])
    cross_entropy = -1. * y_image * tf.log(decoded) - (
        1. - y_image) * tf.log(1. - decoded)
    loss = tf.reduce_mean(cross_entropy)
    # encod_loss = 0
    # if encoded_y_image is not None:
    encoded_cross_entropy = -1. * encoded_y_image * tf.log(encoded) - (
        1. - encoded_y_image) * tf.log(1. - encoded)
    encod_loss = tf.reduce_mean(encoded_cross_entropy)

    return loss, decoded, encoded, encod_loss