Beispiel #1
0
    def build(self, x, reuse=None):
        """ TODO: define your model (2 conv layers and 2 fc layers?)
        x: input image
        logit: network output w/o softmax """
        with tf.variable_scope('model', reuse=reuse):

            W1 = tf.Variable(tf.random_normal([3, 3, 1, 32], stddev=0.01))

            L1 = relu(conv2d(x, W1, strides=[1, 1, 1, 1], padding='SAME'))
            L1 = max_pool(L1, ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1], padding='SAME')

            W2 = tf.Variable(tf.random_normal([3, 3, 32, 64], stddev=0.01))
            L2 = relu(conv2d(L1, W2, strides=[1, 1, 1, 1], padding='SAME'))
            L2 = max_pool(L2, ksize=[1, 2, 2, 1],
                            strides=[1, 2, 2, 1], padding='SAME')

            L2_flat = tf.reshape(L2, [-1, 7 * 7 * 64])
            W3 = tf.get_variable("W3", shape=[7 * 7 * 64, 10],
                                 initializer=xavier_initializer())
            b = tf.Variable(tf.random_normal([10]))

            logit = tf.matmul(L2_flat, W3) + b


        return logit
Beispiel #2
0
def convolution_layer(data, nChannels, filter_size, nFilters):
    kernelShape = [filter_size, filter_size, nChannels, nFilters]
    weights = tensorflow.Variable(
        tensorflow.truncated_normal(kernelShape, stddev=0.05))
    biases = tensorflow.Variable(tensorflow.constant(0.05, shape=[nFilters]))

    layer = nn.conv2d(data, weights, [1, 1, 1, 1], 'SAME')
    layer = nn.bias_add(layer, biases)
    layer = nn.max_pool(layer, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME')
    layer = nn.relu(layer)

    return layer
Beispiel #3
0
from scipy.misc import imread, imresize
from os import listdir
from os.path import splitext
from random import seed, shuffle
from time import time
from numpy import zeros
from tensorflow import Variable, truncated_normal, constant, nn

weights = lambda shape: Variable(truncated_normal(shape, stddev=0.1))
biases = lambda shape: Variable(constant(0.1, shape=shape))
conv2d = lambda x, W: nn.conv2d(x, W, strides=[1, 2, 2, 1], padding='SAME')
max_pool = lambda x: nn.max_pool(
    x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')


def load_image(path, shape=False):
    readed_img = imread(path)
    if shape:
        readed_img = imresize(readed_img, shape)
    return readed_img


def extend_children(path, ftype=False):
    allpaths = [path + '/' + child for child in listdir(path)]
    if ftype != False:
        # remember to include the period in ftype (ie .jpg)
        # pass '' to include only folders
        ret = []
        for v in allpaths:
            if splitext(v)[1] == ftype:
                ret.append(v)
Beispiel #4
0
    def __init__(self, h_size, env, name, LEARNING_RATE, n_step):
        # The network recieves a frame from the game, flattened into an array.
        # It then resizes it and processes it through four convolutional layers.

        WINDOW_SIZE = env.win_size
        CONV_FILTER_SIZE_X = [3, 3, 3, 3]
        CONV_FILTER_SIZE_Y = [3, 3, 3, 3]
        CONV_STRIDE_X = [3, 1, 1, 3]
        CONV_STRIDE_Y = [3, 1, 1, 3]
        CONV_LAYER_NUM = 4
        CONV_FILTER_NUM = [8, 32, 32, 64]
        IMAGE_SIZE = [2 * (WINDOW_SIZE + 2), 8, 3]
        self.scalarInput = tf.placeholder(
            shape=[None, IMAGE_SIZE[0] * IMAGE_SIZE[1] * IMAGE_SIZE[2]],
            dtype=tf.float32)
        self.imageIn = tf.reshape(
            self.scalarInput,
            shape=[-1, IMAGE_SIZE[0], IMAGE_SIZE[1], IMAGE_SIZE[2]])
        depthwise_filter1 = tf.get_variable(shape=(CONV_FILTER_SIZE_X[0],
                                                   CONV_FILTER_SIZE_Y[0], 3,
                                                   1),
                                            name=name + "_depthwise_filter1")
        pointwise_filter1 = tf.get_variable(
            shape=[1, 1, 3, CONV_FILTER_NUM[0]],
            name=name + "_pointwise_filter1")
        self.conv1 = nn.separable_conv2d(
            self.imageIn,
            depthwise_filter1,
            pointwise_filter1,
            strides=[1, CONV_STRIDE_X[0], CONV_STRIDE_Y[0], 1],
            padding='SAME')
        print(np.shape(self.conv1))
        self.relu1 = nn.relu(self.conv1, name=name + "_relu1")
        print(np.shape(self.relu1))
        depthwise_filter2 = tf.get_variable(shape=(CONV_FILTER_SIZE_X[1],
                                                   CONV_FILTER_SIZE_Y[1],
                                                   CONV_FILTER_NUM[0], 1),
                                            name=name + "_depthwise_filter2")
        pointwise_filter2 = tf.get_variable(
            shape=[1, 1, CONV_FILTER_NUM[0], CONV_FILTER_NUM[1]],
            name=name + "_pointwise_filter2")
        self.conv2 = nn.separable_conv2d(
            self.relu1,
            depthwise_filter2,
            pointwise_filter2,
            strides=[1, CONV_STRIDE_X[1], CONV_STRIDE_Y[1], 1],
            padding='SAME')
        print(np.shape(self.conv2))
        self.relu2 = nn.relu(self.conv2, name=name + "_relu2")
        print(np.shape(self.relu2))
        depthwise_filter3 = tf.get_variable(shape=(CONV_FILTER_SIZE_X[2],
                                                   CONV_FILTER_SIZE_Y[2],
                                                   CONV_FILTER_NUM[1], 1),
                                            name=name + "_depthwise_filter3")
        pointwise_filter3 = tf.get_variable(
            shape=[1, 1, CONV_FILTER_NUM[1], CONV_FILTER_NUM[2]],
            name=name + "_pointwise_filter3")
        self.conv3 = nn.separable_conv2d(
            self.relu2,
            depthwise_filter3,
            pointwise_filter3,
            strides=[1, CONV_STRIDE_X[2], CONV_STRIDE_Y[2], 1],
            padding='SAME')
        print(np.shape(self.conv3))
        self.relu3 = nn.relu(self.conv3, name=name + "_relu3")
        print(np.shape(self.relu3))
        self.maxpool1 = nn.max_pool(self.relu3,
                                    ksize=[1, 3, 1, 1],
                                    strides=[1, 3, 1, 1],
                                    padding='VALID')
        print(np.shape(self.maxpool1))
        if np.ceil(np.floor(np.ceil(2 * (WINDOW_SIZE + 2) / 3) / 3) / 3) >= 2:
            conv_filter4 = tf.get_variable(
                shape=(CONV_FILTER_SIZE_X[3], CONV_FILTER_SIZE_Y[3],
                       CONV_FILTER_NUM[2], CONV_FILTER_NUM[3]),
                name=name + "_conv_filter4")
            self.conv4 = nn.conv2d(
                self.maxpool1,
                conv_filter4,
                strides=[1, CONV_STRIDE_X[3], CONV_STRIDE_Y[3], 1],
                padding='SAME')
            print(np.shape(self.conv4))
            self.relu4 = nn.relu(self.conv4, name=name + "_relu4")
            print(np.shape(self.relu4))
            self.maxpool2 = nn.max_pool(self.relu4,
                                        ksize=[1, 2, 1, 1],
                                        strides=[1, 2, 1, 1],
                                        padding='VALID')
            LAST_CONV_FILTER = [
                np.floor(
                    np.ceil(
                        np.floor(np.ceil(2 * (WINDOW_SIZE + 2) / 3) / 3) / 3) /
                    2), 1
            ]
            conv_filter5 = tf.get_variable(shape=(LAST_CONV_FILTER[0],
                                                  LAST_CONV_FILTER[1],
                                                  CONV_FILTER_NUM[3], h_size),
                                           name=name + "_conv_filter5")
            self.conv5 = nn.conv2d(
                self.maxpool2,
                conv_filter5,
                strides=[1, CONV_STRIDE_X[3], CONV_STRIDE_Y[3], 1],
                padding='VALID')
            print(np.shape(self.maxpool2))
        else:
            LAST_CONV_FILTER = [
                np.floor(np.ceil(2 * (WINDOW_SIZE + 2) / 3) / 3), 3
            ]
            conv_filter5 = tf.get_variable(shape=(LAST_CONV_FILTER[0],
                                                  LAST_CONV_FILTER[1],
                                                  CONV_FILTER_NUM[2], h_size),
                                           name=name + "_conv_filter5")
            self.conv5 = nn.conv2d(
                self.maxpool1,
                conv_filter5,
                strides=[1, CONV_STRIDE_X[3], CONV_STRIDE_Y[3], 1],
                padding='VALID')
        print(np.shape(self.conv5))
        self.relu5 = nn.relu(self.conv5, name=name + "_relu5")
        print(np.shape(self.relu5))

        # We take the output from the final convolutional layer and split it into separate advantage and value streams.
        self.streamAC, self.streamVC = tf.split(self.relu5, 2, 3)
        self.streamA = slim.flatten(self.streamAC)
        self.streamV = slim.flatten(self.streamVC)
        xavier_init = tf.contrib.layers.xavier_initializer()
        self.AW = tf.Variable(xavier_init([h_size // 2, env.actions]))
        self.VW = tf.Variable(xavier_init([h_size // 2, 1]))
        print(self.conv5)
        print(self.streamA)
        print(self.AW)
        self.Advantage = tf.matmul(self.streamA, self.AW)
        self.Value = tf.matmul(self.streamV, self.VW)

        # Then combine them together to get our final Q-values.
        self.Qout = self.Value + tf.subtract(
            self.Advantage,
            tf.reduce_mean(self.Advantage, axis=1, keep_dims=True))
        self.predict = tf.argmax(self.Qout, 1)

        # Below we obtain the loss by taking the sum of squares difference between the target and prediction Q values.
        self.targetQ = tf.placeholder(shape=[None], dtype=tf.float32)
        self.actions = tf.placeholder(shape=[None], dtype=tf.int32)
        self.actions_onehot = tf.one_hot(self.actions,
                                         env.actions,
                                         dtype=tf.float32)

        self.Q = tf.reduce_sum(tf.multiply(self.Qout, self.actions_onehot),
                               axis=1)

        self.td_error = tf.square(self.targetQ - self.Q)
        self.loss = tf.reduce_mean(self.td_error)
        self.trainer = tf.train.AdamOptimizer(learning_rate=LEARNING_RATE)
        self.updateModel = self.trainer.minimize(self.loss)
Beispiel #5
0
    def build_model(self):
        with tf.name_scope('Input'):
            content_img = tf.constant(self.content_img_value,
                                      name='content_image')
            style_img = tf.constant(self.style_img_value, name='style_image')
            self.magical_img = tf.Variable(initial_value=content_img,
                                           name='magical_image')
            input_tensor = tf.concat([
                tf.expand_dims(content_img, axis=0),
                tf.expand_dims(style_img, axis=0),
                tf.expand_dims(self.magical_img, axis=0)
            ],
                                     axis=0)

        with tf.name_scope('conv1'):
            conv1_1 = relu(bias_add(
                conv2d(input_tensor,
                       tf.constant(self.vgg16_weights['block1_conv1'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block1_conv1'][1]),
                           name='conv1_1')
            conv1_2 = relu(bias_add(
                conv2d(conv1_1,
                       tf.constant(self.vgg16_weights['block1_conv2'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block1_conv2'][1]),
                           name='conv1_2')
            pool1 = max_pool(conv1_2, [1, 2, 2, 1], [1, 2, 2, 1],
                             'SAME',
                             name='pool1')

        with tf.name_scope('conv2'):
            conv2_1 = relu(bias_add(
                conv2d(pool1,
                       tf.constant(self.vgg16_weights['block2_conv1'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block2_conv1'][1]),
                           name='conv2_1')
            conv2_2 = relu(bias_add(
                conv2d(conv2_1,
                       tf.constant(self.vgg16_weights['block2_conv2'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block2_conv2'][1]),
                           name='conv2_2')
            pool2 = max_pool(conv2_2, [1, 2, 2, 1], [1, 2, 2, 1],
                             'SAME',
                             name='pool2')

        with tf.name_scope('conv3'):
            conv3_1 = relu(bias_add(
                conv2d(pool2,
                       tf.constant(self.vgg16_weights['block3_conv1'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block3_conv1'][1]),
                           name='conv3_1')
            conv3_2 = relu(bias_add(
                conv2d(conv3_1,
                       tf.constant(self.vgg16_weights['block3_conv2'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block3_conv2'][1]),
                           name='conv3_2')
            conv3_3 = relu(bias_add(
                conv2d(conv3_2,
                       tf.constant(self.vgg16_weights['block3_conv3'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block3_conv3'][1]),
                           name='conv3_3')
            pool3 = max_pool(conv3_3, [1, 2, 2, 1], [1, 2, 2, 1],
                             'SAME',
                             name='pool3')

        with tf.name_scope('conv4'):
            conv4_1 = relu(bias_add(
                conv2d(pool3,
                       tf.constant(self.vgg16_weights['block4_conv1'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block4_conv1'][1]),
                           name='conv4_1')
            conv4_2 = relu(bias_add(
                conv2d(conv4_1,
                       tf.constant(self.vgg16_weights['block4_conv2'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block4_conv2'][1]),
                           name='conv4_2')
            conv4_3 = relu(bias_add(
                conv2d(conv4_2,
                       tf.constant(self.vgg16_weights['block4_conv3'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block4_conv3'][1]),
                           name='conv4_3')
            pool4 = max_pool(conv4_3, [1, 2, 2, 1], [1, 2, 2, 1],
                             'SAME',
                             name='pool4')

        with tf.name_scope('conv5'):
            conv5_1 = relu(bias_add(
                conv2d(pool4,
                       tf.constant(self.vgg16_weights['block5_conv1'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block5_conv1'][1]),
                           name='conv5_1')
            conv5_2 = relu(bias_add(
                conv2d(conv5_1,
                       tf.constant(self.vgg16_weights['block5_conv2'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block5_conv2'][1]),
                           name='conv5_2')
            conv5_3 = relu(bias_add(
                conv2d(conv5_2,
                       tf.constant(self.vgg16_weights['block5_conv3'][0]),
                       strides=[1, 1, 1, 1],
                       padding='SAME'), self.vgg16_weights['block5_conv3'][1]),
                           name='conv5_3')
            pool5 = max_pool(conv5_3, [1, 2, 2, 1], [1, 2, 2, 1],
                             'SAME',
                             name='pool5')

        c_loss = self.content_loss([input_tensor])
        s_loss = self.style_loss([conv1_1, conv2_1, conv3_1, conv4_1, conv5_1])

        self.loss = self.content_weight * c_loss + self.style_weight * s_loss
        self.train_op = tf.train.AdamOptimizer(learning_rate=self.lr).minimize(
            self.loss)

        if self.graph_write:
            writer = tf.summary.FileWriter('logs',
                                           graph=tf.get_default_graph())
            writer.flush()
            writer.close()
Beispiel #6
0
 def implement(self, x):
     return nn.max_pool(x,
                        ksize=[1, self.size[0], self.size[1], 1],
                        strides=self.strides,
                        padding=self.padding)
Beispiel #7
0
def build_model(training=True):
    depth = config.depth[config.versions.index(config.version)]
    width = config.width[config.versions.index(config.version)]

    inputs = layers.Input([config.image_size, config.image_size, 3])
    x = nn.space_to_depth(inputs, 2)
    x = conv(x, int(round(width * 64)), 3)
    x = conv(x, int(round(width * 128)), 3, 2)
    x = csp(x, int(round(width * 128)), int(round(depth * 3)))

    x = conv(x, int(round(width * 256)), 3, 2)
    x = csp(x, int(round(width * 256)), int(round(depth * 9)))
    x1 = x

    x = conv(x, int(round(width * 512)), 3, 2)
    x = csp(x, int(round(width * 512)), int(round(depth * 9)))
    x2 = x

    x = conv(x, int(round(width * 1024)), 3, 2)
    x = conv(x, int(round(width * 512)), 1, 1)
    x = layers.concatenate([
        x,
        nn.max_pool(x, 5, 1, 'SAME'),
        nn.max_pool(x, 9, 1, 'SAME'),
        nn.max_pool(x, 13, 1, 'SAME')
    ])
    x = conv(x, int(round(width * 1024)), 1, 1)
    x = csp(x, int(round(width * 1024)), int(round(depth * 3)), False)

    x = conv(x, int(round(width * 512)), 1)
    x3 = x
    x = layers.UpSampling2D()(x)
    x = layers.concatenate([x, x2])
    x = csp(x, int(round(width * 512)), int(round(depth * 3)), False)

    x = conv(x, int(round(width * 256)), 1)
    x4 = x
    x = layers.UpSampling2D()(x)
    x = layers.concatenate([x, x1])
    x = csp(x, int(round(width * 256)), int(round(depth * 3)), False)
    p3 = layers.Conv2D(3 * (len(config.class_dict) + 5),
                       1,
                       name=f'p3_{len(config.class_dict)}',
                       kernel_initializer=initializer,
                       kernel_regularizer=l2)(x)

    x = conv(x, int(round(width * 256)), 3, 2)
    x = layers.concatenate([x, x4])
    x = csp(x, int(round(width * 512)), int(round(depth * 3)), False)
    p4 = layers.Conv2D(3 * (len(config.class_dict) + 5),
                       1,
                       name=f'p4_{len(config.class_dict)}',
                       kernel_initializer=initializer,
                       kernel_regularizer=l2)(x)

    x = conv(x, int(round(width * 512)), 3, 2)
    x = layers.concatenate([x, x3])
    x = csp(x, int(round(width * 1024)), int(round(depth * 3)), False)
    p5 = layers.Conv2D(3 * (len(config.class_dict) + 5),
                       1,
                       name=f'p5_{len(config.class_dict)}',
                       kernel_initializer=initializer,
                       kernel_regularizer=l2)(x)

    if training:
        return tf.keras.Model(inputs, [p5, p4, p3])
    else:
        return tf.keras.Model(inputs, Predict()([p5, p4, p3]))
 def apply_max_pool(self, x, k_size, stride_size):
     return nn.max_pool(x,
                        k_size=[1, k_size, k_size, 1],
                        strides=[1, stride_size, stride_size, 1],
                        padding='SAME')