Beispiel #1
0
    def build_image_generator(self, img_z, sen_rep):
        with tf.variable_scope('img_generator'):
            # now, calculate the size of output during the deconv upsampling
            # note that we only use stride 2 during the conv
            assert self.config.generator_l1_nchannel % 8 == 0, \
                logger.error('[ERROR] Invalid channel size')
            l5_h, l5_w, l5_c = 64, 64, 3
            l4_h, l4_w, l4_c = 32, 32, 64
            l3_h, l3_w, l3_c = 16, 16, 128
            l2_h, l2_w, l2_c = 8, 8, 256
            l1_h, l1_w, l1_c = 4, 4, 512

            # construct the network layer by layer
            # layer 0: combines the conditional vec with the noise vec
            sen_rep = op.linear(sen_rep, 128, 'conditional_vec')
            self.l0 = tf.concat(1, [img_z, op.lrelu(sen_rep)])

            # layer 1: the linear projection
            self.l1 = op.linear(self.l0, l1_w * l1_h * l1_c, 'l0_lin')
            self.l1 = tf.reshape(self.l1, [self.batch_size, l1_h, l1_w, l1_c])
            self.l1_bn = op.batch_norm(name='l1_bn0')
            self.l1 = tf.nn.relu(self.l1_bn(self.l1, train=self.train))

            # layer 2: first conv1
            self.l2 = op.deconv2d(self.l1, [self.batch_size, l2_h, l2_w, l2_c],
                                  name='l2')
            self.l2_bn = op.batch_norm(name='l2_bn0')
            self.l2 = tf.nn.relu(self.l2_bn(self.l2, train=self.train))

            # layer 3: conv2
            self.l3 = op.deconv2d(self.l2, [self.batch_size, l3_h, l3_w, l3_c],
                                  name='l3')
            self.l3_bn = op.batch_norm(name='l3_bn0')
            self.l3 = tf.nn.relu(self.l3_bn(self.l3, train=self.train))

            # layer 4: conv4
            self.l4 = op.deconv2d(self.l3, [self.batch_size, l4_h, l4_w, l4_c],
                                  name='l4')
            self.l4_bn = op.batch_norm(name='l4_bn0')
            self.l4 = tf.nn.relu(self.l4_bn(self.l4, train=self.train))

            # layer 5: conv5 / final
            self.l5 = op.deconv2d(self.l4, [self.batch_size, l5_h, l5_w, l5_c],
                                  name='l5')

            self.fake_img = tf.nn.tanh(self.l5)  # [-1, 1]
            img_shape = self.fake_img.get_shape()

            # check the size of the image
            assert (img_shape[1] == 64) and \
                (img_shape[2] == 64) and (img_shape[3] == 3), \
                logger.error('Wrong fake image dimension: {}'.format(img_shape))
        return
Beispiel #2
0
    def build_image_generator(self, img_z):
        with tf.variable_scope('img_generator'):
            nf = 64
            # layer 1: a projection after the noise into 4, 4, 512
            self.l1 = op.linear(img_z, nf * 8 * 4 * 4)  # 'l0_b*[4*4*512]'
            self.l1 = tf.reshape(
                self.l1, [self.batch_size, 4, 4, nf * 8])  # 'l0_b*4*4*4*512')
            self.l1_bn = op.batch_norm(name='l1_bn0')
            self.l1 = tf.nn.relu(self.l1_bn(self.l1, train=self.train))
            print('name: {}, size: {}'.format(self.l1.name,
                                              self.l1.get_shape()))

            # layer 2: first conv1
            self.l2 = op.deconv2d(self.l1, [self.batch_size, 8, 8, nf * 4],
                                  name='l2')
            self.l2_bn = op.batch_norm(name='l2_bn0')
            self.l2 = tf.nn.relu(self.l2_bn(self.l2, train=self.train))

            # layer 3: conv2
            self.l3 = op.deconv2d(self.l2, [self.batch_size, 16, 16, nf * 2],
                                  name='l3')
            self.l3_bn = op.batch_norm(name='l3_bn0')
            self.l3 = tf.nn.relu(self.l3_bn(self.l3, train=self.train))

            # layer 4: conv4
            self.l4 = op.deconv2d(self.l3, [self.batch_size, 32, 32, nf],
                                  name='l4')
            self.l4_bn = op.batch_norm(name='l4_bn0')
            self.l4 = tf.nn.relu(self.l4_bn(self.l4, train=self.train))

            # layer 5: conv5 / final
            self.l5 = op.deconv2d(self.l4, [self.batch_size, 64, 64, 3],
                                  name='l5')
            # self.l5_bn = op.batch_norm(name='l5_bn0')
            # self.l5 = tf.nn.relu(self.l5_bn(self.l5, train=self.train))

            self.fake_img = tf.nn.tanh(self.l5)
            img_shape = self.fake_img.get_shape()

            # check the size of the image
            assert (img_shape[1] == 64) and \
                (img_shape[2] == 64) and (img_shape[3] == 3), \
                logger.error('Wrong fake image dimension: {}'.format(img_shape))
        return
 def decode_layer(x, output_width, output_filters, layer, enc_layer, dropout=False, do_concat=True):
     dec = deconv2d(tf.nn.relu(x), [self.batch_size, output_width,
                                    output_width, output_filters], scope="g_d%d_deconv" % layer)
     if layer != 8:
         # IMPORTANT: normalization for last layer
         # Very important, otherwise GAN is unstable
         dec = batch_norm(dec, is_training, scope="g_d%d_bn" % layer)
     if dropout:
         dec = tf.nn.dropout(dec, 0.5)
     if do_concat:
         dec = tf.concat([dec, enc_layer], 3)
     return dec
Beispiel #4
0
 def decode_layer(x,
                  output_width,
                  output_filters,
                  layer,
                  dropout=False):
     dec = deconv2d(tf.nn.relu(x), [
         self.batch_size, output_width, output_width, output_filters
     ],
                    scope="d_d%d_deconv" % layer)
     if layer != 8:
         dec = batch_norm(dec,
                          is_training,
                          scope="d_d%d_bn" % layer)
     if dropout:
         dec = tf.nn.dropout(dec, 0.5)
     return dec