Exemplo n.º 1
0
    def fc_layers(self, trainable):
        # fc1
        with tf.name_scope('fc1') as scope:
            shape = int(np.prod(self.pool5.get_shape()[1:]))
            fc1w = tf.Variable(tf.truncated_normal([shape, 4096],
                                                   dtype=tf.float32,
                                                   stddev=1e-1),
                               name='weights',
                               trainable=trainable)
            fc1b = tf.Variable(tf.constant(1.0, shape=[4096],
                                           dtype=tf.float32),
                               trainable=trainable,
                               name='biases')
            pool5_flat = tf.reshape(self.pool5, [-1, shape])
            fc1l = tf.nn.bias_add(tf.matmul(pool5_flat, fc1w), fc1b)
            self.fc1 = tf.nn.relu(fc1l)
            self.after_relus.append(self.fc1)
            self.parameters += [fc1w, fc1b]

        # fc2
        with tf.name_scope('fc2') as scope:
            fc2w = tf.Variable(tf.truncated_normal([4096, 4096],
                                                   dtype=tf.float32,
                                                   stddev=1e-1),
                               name='weights',
                               trainable=trainable)
            fc2b = tf.Variable(tf.constant(1.0, shape=[4096],
                                           dtype=tf.float32),
                               trainable=trainable,
                               name='biases')
            fc2l = tf.nn.bias_add(tf.matmul(self.fc1, fc2w), fc2b)
            self.fc2 = tf.nn.relu(fc2l)
            self.after_relus.append(self.fc2)

            self.parameters += [fc2w, fc2b]

        if self.stop_at_fc2:
            return

        # fc3
        with tf.name_scope('fc3') as scope:
            fc3w = tf.Variable(tf.truncated_normal([4096, 1000],
                                                   dtype=tf.float32,
                                                   stddev=1e-1),
                               name='weights',
                               trainable=trainable)
            fc3b = tf.Variable(tf.constant(1.0, shape=[1000],
                                           dtype=tf.float32),
                               trainable=trainable,
                               name='biases')
            self.fc3l = tf.nn.bias_add(tf.matmul(self.fc2, fc3w), fc3b)
            self.parameters += [fc3w, fc3b]
Exemplo n.º 2
0
def dense_layer_ops(X, num_X, num_Y, config):
    W = tf.Variable(tf.truncated_normal([num_X, num_Y],
                                        mean=0.0,
                                        stddev=np.float32(
                                            config.var_init_stddev)),
                    name='W')
    B = tf.Variable(tf.constant(value=np.float32(config.bias_init),
                                dtype=tf.float32,
                                shape=[num_Y]),
                    name='B')
    Y = tf.add(tf.matmul(X, W), B, name='Y')

    return W, B, Y
Exemplo n.º 3
0
def get_reg_term(config, vars_to_reg):
    reg_term = tf.constant(value=0.0, dtype=tf.float32)
    l2reg = np.float32(config.l2reg)
    l1reg = np.float32(config.l1reg)
    if (l2reg > 0.0 or l1reg > 0.0) and len(vars_to_reg) > 0:
        for x_var in vars_to_reg:
            if l2reg > 0.0:
                x_squared = x_var * x_var
                l2norm = tf.reduce_sum(x_squared)
                reg_term += l2reg * l2norm
            if l1reg > 0.0:
                x_abs = tf.abs(x_var)
                l1norm = tf.reduce_sum(x_abs)
                reg_term += l1reg * l1norm
    return reg_term
Exemplo n.º 4
0
    def convlayers(self, trainable):
        self.parameters = []

        images = self.imgs

        # conv1_1
        with tf.name_scope('conv1_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 3, 64],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
            self.conv_input_to_conv1_1 = conv
            biases = tf.Variable(tf.constant(0.0, shape=[64],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv1_1 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv1_1)
            self.parameters += [kernel, biases]

        # conv1_2
        with tf.name_scope('conv1_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv1_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv1_2 = conv
            biases = tf.Variable(tf.constant(0.0, shape=[64],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv1_2 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv1_2)
            self.parameters += [kernel, biases]

        # pool1
        self.pool1 = tf.nn.max_pool(self.conv1_2,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='pool1')

        # conv2_1
        with tf.name_scope('conv2_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.pool1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv2_1 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[128],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv2_1 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv2_1)
            self.parameters += [kernel, biases]

        # conv2_2
        with tf.name_scope('conv2_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv2_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv2_2 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[128],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv2_2 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv2_2)
            self.parameters += [kernel, biases]

        # pool2
        self.pool2 = tf.nn.max_pool(self.conv2_2,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='pool2')

        # conv3_1
        with tf.name_scope('conv3_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 256],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)

            conv = tf.nn.conv2d(self.pool2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv3_1 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[256],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv3_1 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv3_1)
            self.parameters += [kernel, biases]

        # conv3_2
        with tf.name_scope('conv3_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv3_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv3_2 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[256],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv3_2 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv3_2)
            self.parameters += [kernel, biases]

        # conv3_3
        with tf.name_scope('conv3_3') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv3_2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv3_3 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[256],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv3_3 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv3_3)
            self.parameters += [kernel, biases]

        # pool3
        self.pool3 = tf.nn.max_pool(self.conv3_3,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='pool3')

        # conv4_1
        with tf.name_scope('conv4_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.pool3,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv4_1 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv4_1 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv4_1)
            self.parameters += [kernel, biases]

        # conv4_2
        with tf.name_scope('conv4_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv4_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv4_2 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv4_2 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv4_2)
            self.parameters += [kernel, biases]

        # conv4_3
        with tf.name_scope('conv4_3') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv4_2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv4_3 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv4_3 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv4_3)
            self.parameters += [kernel, biases]

        # pool4
        self.pool4 = tf.nn.max_pool(self.conv4_3,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='pool4')

        # conv5_1
        with tf.name_scope('conv5_1') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.pool4,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv5_1 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv5_1 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv5_1)
            self.parameters += [kernel, biases]

        # conv5_2
        with tf.name_scope('conv5_2') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv5_1,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv5_2 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv5_2 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv5_2)
            self.parameters += [kernel, biases]

        # conv5_3
        with tf.name_scope('conv5_3') as scope:
            kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512],
                                                     dtype=tf.float32,
                                                     stddev=1e-1),
                                 name='weights',
                                 trainable=trainable)
            conv = tf.nn.conv2d(self.conv5_2,
                                kernel, [1, 1, 1, 1],
                                padding='SAME')
            self.conv_input_to_conv5_3 = conv
            biases = tf.Variable(tf.constant(0.0,
                                             shape=[512],
                                             dtype=tf.float32),
                                 trainable=trainable,
                                 name='biases')
            out = tf.nn.bias_add(conv, biases)
            self.conv5_3 = tf.nn.relu(out, name=scope)
            self.after_relus.append(self.conv5_3)
            self.parameters += [kernel, biases]

        # pool5
        self.pool5 = tf.nn.max_pool(self.conv5_3,
                                    ksize=[1, 2, 2, 1],
                                    strides=[1, 2, 2, 1],
                                    padding='SAME',
                                    name='pool4')