示例#1
0
    def _discriminator(self,
                       input_images,
                       dims,
                       train_phase,
                       activation=tf.nn.relu,
                       scope_name="discriminator",
                       scope_reuse=False):
        N = len(dims)
        with tf.variable_scope(scope_name) as scope:
            if scope_reuse:
                scope.reuse_variables()
            h = input_images
            skip_bn = True  # First layer of discriminator skips batch norm
            for index in range(N - 2):
                W = utils.weight_variable([4, 4, dims[index], dims[index + 1]],
                                          name="W_%d" % index)
                b = tf.zeros([dims[index + 1]])
                h_conv = utils.conv2d_strided(h, W, b)
                if skip_bn:
                    h_bn = h_conv
                    skip_bn = False
                else:
                    #d_bn = ops.batch_norm(name='d_bn{0}'.format(index))
                    h_bn = d_bn(h_conv, train=train_phase)

                h = activation(h_bn, name="h_%d" % index)
                utils.add_activation_summary(h)

            W_pred = utils.weight_variable([4, 4, dims[-2], dims[-1]],
                                           name="W_pred")
            b = tf.zeros([dims[-1]])
            h_pred = utils.conv2d_strided(h, W_pred, b)
        return None, h_pred, None  # Return the last convolution output. None values are returned to maintatin disc from other GAN
示例#2
0
    def _discriminator(self, input_images, dims, train_phase, activation=tf.nn.relu, scope_name="discriminator",
                       scope_reuse=False):
        N = len(dims)
        with tf.variable_scope(scope_name) as scope:
            if scope_reuse:
                scope.reuse_variables()
            h = input_images
            skip_bn = True  # First layer of discriminator skips batch norm
            for index in range(N - 2):
                W = utils.weight_variable([5, 5, dims[index], dims[index + 1]], name="W_%d" % index)
                b = utils.bias_variable([dims[index + 1]], name="b_%d" % index)
                h_conv = utils.conv2d_strided(h, W, b)
                if skip_bn:
                    h_bn = h_conv
                    skip_bn = False
                else:
                    h_bn = utils.batch_norm(h_conv, dims[index + 1], train_phase, scope="disc_bn%d" % index)
                h = activation(h_bn, name="h_%d" % index)
                utils.add_activation_summary(h)

            shape = h.get_shape().as_list()
            image_size = self.resized_image_size // (2 ** (N - 2))  # dims has input dim and output dim
            h_reshaped = tf.reshape(h, [self.batch_size, image_size * image_size * shape[3]])
            W_pred = utils.weight_variable([image_size * image_size * shape[3], dims[-1]], name="W_pred")
            b_pred = utils.bias_variable([dims[-1]], name="b_pred")
            h_pred = tf.matmul(h_reshaped, W_pred) + b_pred

        return tf.nn.sigmoid(h_pred), h_pred, h
示例#3
0
def subnet1(image, reuse=False):
    with tf.variable_scope("subnet1", reuse=reuse):
        def _vgg_net(weights, image):
            print('setting up vgg model initialized params --> extractor1')
            layers = (
                'conv1_1', 'relu1_1', 'conv1_2', 'relu1_2', 'pool1',
                'conv2_1', 'relu2_1', 'conv2_2', 'relu2_2', 'pool2',
                'conv3_1', 'relu3_1', 'conv3_2', 'relu3_2', 'conv3_3',
                'relu3_3', 'conv3_4', 'relu3_4', 'pool3',
                'conv4_1', 'relu4_1', 'conv4_2', 'relu4_2', 'conv4_3',
                'relu4_3', 'conv4_4', 'relu4_4', 'pool4',
                'conv5_1', 'relu5_1', 'conv5_2', 'relu5_2', 'conv5_3',
                'relu5_3', 'conv5_4', 'relu5_4'
            )
            net = {}
            current = image
            for i, name in enumerate(layers):
                kind = name[:4]
                if kind == 'conv':
                    kernels, bias = weights[i][0][0][0][0]
                    # kernels are [width, height, in_channles, out_channles]
                    # tensorflow are [height, width, in channles, out_channles]
                    kernels = utils.get_variable(
                        np.transpose(kernels, (1, 0, 2, 3)), name=name + '_w')
                    bias = utils.get_variable(bias.reshape(-1), name=name + '_b')
                    current = utils.conv2d_basic(current, kernels, bias)
                elif kind == 'relu':
                    current = tf.nn.relu(current, name=name)
                elif kind == 'pool':
                    current = utils.avg_pool_2x2(current)
                net[name] = current
            return net

        model_data = utils.get_model_data("data", MODEL_URL)
        weights = np.squeeze(model_data['layers'])

        with tf.variable_scope('inference'):
            image = tf.reshape(image, [-1, IMAGE_SIZE, IMAGE_SIZE, IMAGE_CHANNLES])
            image_net = _vgg_net(weights, image)
            conv_final_layer = image_net['relu5_4']

            pool5 = utils.max_pool_2x2(conv_final_layer)

            W6 = utils.weights_variable([4, 4, 512, 4096], name="W6")
            b6 = utils.bias_variable([4096], name='b6')
            conv6 = utils.conv2d_strided(pool5, W6, b6, stride=1)
            relu6 = tf.nn.relu(conv6, name='relu6')

            relu6 = utils.max_pool_2x2(relu6)

            disc_out = tf.reshape(relu6, [-1, 4096])

            return disc_out
示例#4
0
    def _discriminator(self,
                       input_images,
                       dims,
                       train_phase,
                       activation=tf.nn.relu,
                       scope_name="discriminator",
                       scope_reuse=False):
        N = len(dims)
        with tf.variable_scope(scope_name) as scope:
            if scope_reuse:
                scope.reuse_variables()
            h = input_images
            skip_bn = True  # First layer of discriminator skips batch norm
            for index in range(N - 2):
                W = utils.weight_variable([4, 4, dims[index], dims[index + 1]],
                                          name="W_%d" % index)
                b = tf.zeros([dims[index + 1]])
                h_conv = utils.conv2d_strided(h, W, b)
                if skip_bn:
                    h_bn = h_conv
                    skip_bn = False
                else:
                    # h_bn = tf.contrib.layers.batch_norm(inputs=h_conv, decay=0.9, epsilon=1e-5, is_training=train_phase,
                    #                                     scope="disc_bn%d" % index)
                    h_bn = utils.batch_norm("disc_bn%d" % index,
                                            h_conv,
                                            True,
                                            'NHWC',
                                            train_phase,
                                            bn_epsilon=1e-5,
                                            bn_ema=0.9)
                    # h_bn = utils.batch_norm(h_conv, dims[index + 1], train_phase, scope="disc_bn%d" % index)
                h = activation(h_bn, name="h_%d" % index)
                utils.add_activation_summary(h)

            W_pred = utils.weight_variable([4, 4, dims[-2], dims[-1]],
                                           name="W_pred")
            b = tf.zeros([dims[-1]])
            h_pred = utils.conv2d_strided(h, W_pred, b)
        return None, h_pred, None  # Return the last convolution output. None values are returned to maintatin disc from other GAN