Exemple #1
0
    def build_G(self, name, image=None, style_image=None, reuse=False):
        with tf.variable_scope('Generator_' + name, reuse=reuse):
            if image is None:
                self.x_input = tf.placeholder(
                    tf.float32, [None, self.input_size, self.input_size, 3])
                self.y_input = tf.placeholder(
                    tf.float32, [None, self.input_size, self.input_size, 3])
                x = tf.concat(self.x_input, self.y_input, 2)
            elif image is not None and style_image is not None:
                x = tf.concat([image, style_image], 3)

            # filter_size:9, nb_filter: 3->32, step:1
            x = ops.conv_layer('first', x, 9, 6, 32, 1)
            # filter_size:3, nb_filter: 32->64, step:1
            x = ops.conv_layer('dsample_1', x, 3, 32, 64, 2)
            # filter_size:3, nb_filter:64->128, step:1
            x = ops.conv_layer('dsample_2', x, 3, 64, 128, 2)

            for i in range(5):  # five residual blocks
                name = ('res_block_{}'.format(i))
                # filter_size:3, nb_filter:128->128, step:1
                x = ops.residual(name, x, 128, 128, 1)

            x = ops.upsample_nearest_neighbor('usample_1', x, 3, 128, 64, 2)
            x = ops.upsample_nearest_neighbor('usample_2', x, 3, 64, 32, 2)
            x = ops.conv_layer('last', x, 9, 32, 3, 1, activation=False)
            out = tf.nn.tanh(x) * 150 + 255. / 2

            return out
Exemple #2
0
    def _resnet(self, input):
        with tf.variable_scope(self.name, reuse=self._reuse):
            num_filters = [128, 256, 512, 512]
            if self._image_size == 256:
                num_filters.append(512)

            E = input
            E = ops.conv_block(E,
                               64,
                               'C{}_{}'.format(64, 0),
                               4,
                               2,
                               self._is_train,
                               self._reuse,
                               norm=None,
                               activation='leaky',
                               bias=True)
            for i, n in enumerate(num_filters):
                E = ops.residual(E,
                                 n,
                                 'res{}_{}'.format(n, i + 1),
                                 self._is_train,
                                 self._reuse,
                                 norm=self._norm,
                                 bias=True)
                E = tf.nn.avg_pool(E, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME')
            E = tf.nn.relu(E)
            E = tf.nn.avg_pool(E, [1, 8, 8, 1], [1, 8, 8, 1], 'SAME')
            E = ops.flatten(E)
            mu = ops.mlp(E,
                         self._latent_dim,
                         'FC8_mu',
                         self._is_train,
                         self._reuse,
                         norm=None,
                         activation=None)
            log_sigma = ops.mlp(E,
                                self._latent_dim,
                                'FC8_sigma',
                                self._is_train,
                                self._reuse,
                                norm=None,
                                activation=None)

            z = mu + tf.random_normal(
                shape=tf.shape(self._latent_dim)) * tf.exp(log_sigma)

            self._reuse = True
            self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                              self.name)
            return z, mu, log_sigma
Exemple #3
0
    def build_G_AdaIN(self,
                      name,
                      input_size=256,
                      image=None,
                      style_image=None):
        with tf.variable_scope('Generator_encoder_' + name):
            if image is None:
                x_input = tf.placeholder(
                    tf.float32, [None, self.input_size, self.input_size, 3])
            else:
                x_input = image

            # filter_size:9, nb_filter: 3->32, step:1
            x = ops.conv_layer('first', x_input, 9, 3, 32, 1)
            # filter_size:3, nb_filter: 32->64, step:1
            x = ops.conv_layer('dsample_1', x, 3, 32, 64, 2)
            # filter_size:3, nb_filter:64->128, step:1
            x = ops.conv_layer('dsample_2', x, 3, 64, 128, 2)

        with tf.variable_scope('Generator_encoder_c_' + name, reuse=True):
            if image is None:
                y_input = tf.placeholder(
                    tf.float32, [None, self.input_size, self.input_size, 3])
            else:
                y_input = style_image

            # filter_size:9, nb_filter: 3->32, step:1
            y = ops.conv_layer('first', y_input, 9, 3, 32, 1)
            # filter_size:3, nb_filter: 32->64, step:1
            y = ops.conv_layer('dsample_1', x, 3, 32, 64, 2)
            # filter_size:3, nb_filter:64->128, step:1
            y = ops.conv_layer('dsample_2', x, 3, 64, 128, 2)

        with tf.variable_scope('Generator_decoder_' + name):
            for i in range(5):  # five residual blocks
                name = ('res_block_{}'.format(i))
                # filter_size:3, nb_filter:128->128, step:1
                x = ops.residual(name, x, 128, 128, 1, norm='AdaIN', y=y)

            x = ops.upsample_nearest_neighbor('usample_1', x, 3, 128, 64, 2)
            x = ops.upsample_nearest_neighbor('usample_2', x, 3, 64, 32, 2)
            x = ops.conv_layer('last', x, 9, 32, 3, 1, activation=False)
            out = tf.nn.tanh(x) * 150 + 255. / 2

            return out
    def __call__(self, input):
        with tf.variable_scope(self.name, reuse=self._reuse):
            G = ops.conv_block(input,
                               32,
                               'c7s1-32',
                               7,
                               1,
                               self._is_train,
                               self._reuse,
                               self._norm,
                               self._activation,
                               pad='REFLECT')
            G = ops.conv_block(G, 64, 'd64', 3, 2, self._is_train, self._reuse,
                               self._norm, self._activation)
            G = ops.conv_block(G, 128, 'd128', 3, 2, self._is_train,
                               self._reuse, self._norm, self._activation)
            for i in range(self._num_res_block):
                G = ops.residual(G, 128, 'R128_{}'.format(i), self._is_train,
                                 self._reuse, self._norm)
            G = ops.deconv_block(G, 64, 'u64', 3, 2, self._is_train,
                                 self._reuse, self._norm, self._activation)
            G = ops.deconv_block(G, 32, 'u32', 3, 2, self._is_train,
                                 self._reuse, self._norm, self._activation)
            G = ops.conv_block(G,
                               3,
                               'c7s1-3',
                               7,
                               1,
                               self._is_train,
                               self._reuse,
                               norm=None,
                               activation='tanh',
                               pad='REFLECT')

            self._reuse = True
            self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                              self.name)
            return G