def _make_generator(self, input, phase_train): s_h, s_w = self.img_size, self.img_size s_h2, s_w2 = self._conv_out_size_same(s_h, 2), self._conv_out_size_same( s_w, 2) s_h4, s_w4 = self._conv_out_size_same(s_h2, 2), self._conv_out_size_same( s_w2, 2) #s_h8, s_w8 = self._conv_out_size_same(s_h4, 2), self._conv_out_size_same(s_w4, 2) #s_h16, s_w16 = self._conv_out_size_same(s_h8, 2), self._conv_out_size_same(s_w8, 2) # project `z` and reshape self.z_, self.h0_w, self.h0_b = ops.linear(input, self.gf_dim * 8 * s_h4 * s_w4, 'g_h0_lin', with_w=True) normalized_value = ops.batch_norm(self.z_, name='g_bn0', axes=[0], phase_train=phase_train) self.h0 = tf.reshape(normalized_value, [-1, s_h4, s_w4, self.gf_dim * 8]) h0 = ops.lrelu(self.h0) self.h1, self.h1_w, self.h1_b = ops.deconv2d( h0, [self.batch_size, s_h2, s_w2, self.gf_dim * 4], name='g_h1', with_w=True) h1 = ops.lrelu( ops.batch_norm(self.h1, name='g_bn1', phase_train=phase_train)) # h2, self.h2_w, self.h2_b = ops.deconv2d( # h1, [self.batch_size, s_h4, s_w4, self.gf_dim*2], name='g_h2', with_w=True) # h2 = tf.nn.relu(ops.batch_norm(h2, name='g_bn2')) # # h3, self.h3_w, self.h3_b = ops.deconv2d( # h2, [self.batch_size, s_h2, s_w2, self.gf_dim*1], name='g_h3', with_w=True) # h3 = tf.nn.relu(ops.batch_norm(h3, name='g_bn3')) h2, self.h2_w, self.h2_b = ops.deconv2d( h1, [self.batch_size, s_h, s_w, self.c_dim], name='g_h4', with_w=True) h2_non_linear = ops.lrelu(h2, leak=0) return h2_non_linear
def decode_layer(x, output_width, output_filters, layer, enc_layer, keep_rate=1.0): dec = deconv2d(tf.nn.relu(x), [ self.batch_size, output_width, output_width, output_filters ], scope="g_d%d_deconv" % layer) if layer != 8: # normalization for last layer is very important, otherwise GAN is unstable dec = batch_norm(dec, is_training, scope="g_d%d_bn" % layer) dec = tf.nn.dropout(dec, keep_prob=keep_rate) return dec
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: # normalization for last layer is 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
def generator(input_, angles, reuse=False): """ Generator. Parameters ---------- input_: tensor, input images. angles: tensor, target gaze direction. reuse: bool, reuse the net if True. Returns ------- x: tensor, generated image. """ channel = 64 style_dim = angles.get_shape().as_list()[-1] angles_reshaped = tf.reshape(angles, [-1, 1, 1, style_dim]) angles_tiled = tf.tile(angles_reshaped, [1, tf.shape(input_)[1], tf.shape(input_)[2], 1]) x = tf.concat([input_, angles_tiled], axis=3) with tf.compat.v1.variable_scope('generator', reuse=reuse): # input layer x = conv2d(x, channel, d_h=1, d_w=1, scope='conv2d_input', use_bias=False, pad=3, conv_filters_dim=7) x = instance_norm(x, scope='in_input') x = relu(x) # encoder for i in range(2): x = conv2d(x, 2 * channel, d_h=2, d_w=2, scope='conv2d_%d' % i, use_bias=False, pad=1, conv_filters_dim=4) x = instance_norm(x, scope='in_conv_%d' % i) x = relu(x) channel = 2 * channel # bottleneck for i in range(6): x_a = conv2d(x, channel, conv_filters_dim=3, d_h=1, d_w=1, pad=1, use_bias=False, scope='conv_res_a_%d' % i) x_a = instance_norm(x_a, 'in_res_a_%d' % i) x_a = relu(x_a) x_b = conv2d(x_a, channel, conv_filters_dim=3, d_h=1, d_w=1, pad=1, use_bias=False, scope='conv_res_b_%d' % i) x_b = instance_norm(x_b, 'in_res_b_%d' % i) x = x + x_b # decoder for i in range(2): x = deconv2d(x, int(channel / 2), conv_filters_dim=4, d_h=2, d_w=2, use_bias=False, scope='deconv_%d' % i) x = instance_norm(x, scope='in_decon_%d' % i) x = relu(x) channel = int(channel / 2) x = conv2d(x, 3, conv_filters_dim=7, d_h=1, d_w=1, pad=3, use_bias=False, scope='output') x = tanh(x) return x
def generator(): """ Generator. Parameters ---------- input_: tensor, input images. angles: tensor, target gaze direction. reuse: bool, reuse the net if True. Returns ------- x: tensor, generated image. """ load_size = 64 style_dim = 2 angles = tf.compat.v1.placeholder(tf.float32, shape=[None, 2]) input_ = tf.compat.v1.placeholder(tf.float32, shape=[None, load_size, load_size, 3]) angles_reshaped = tf.reshape(angles, [-1, 1, 1, style_dim]) angles_tiled = tf.tile(angles_reshaped, [1, tf.shape(input_)[1], tf.shape(input_)[2], 1]) x = tf.concat([input_, angles_tiled], axis=3) with tf.compat.v1.variable_scope("generator", reuse=False): # input layer x = conv2d( x, load_size, d_h=1, d_w=1, scope="conv2d_input", use_bias=False, pad=3, conv_filters_dim=7, ) x = instance_norm(x, scope="in_input") x = relu(x) # encoder for i in range(2): x = conv2d( x, 2 * load_size, d_h=2, d_w=2, scope="conv2d_%d" % i, use_bias=False, pad=1, conv_filters_dim=4, ) x = instance_norm(x, scope="in_conv_%d" % i) x = relu(x) load_size = 2 * load_size # bottleneck for i in range(6): x_a = conv2d( x, load_size, conv_filters_dim=3, d_h=1, d_w=1, pad=1, use_bias=False, scope="conv_res_a_%d" % i, ) x_a = instance_norm(x_a, "in_res_a_%d" % i) x_a = relu(x_a) x_b = conv2d( x_a, load_size, conv_filters_dim=3, d_h=1, d_w=1, pad=1, use_bias=False, scope="conv_res_b_%d" % i, ) x_b = instance_norm(x_b, "in_res_b_%d" % i) x = x + x_b # decoder for i in range(2): x = deconv2d( x, int(load_size / 2), conv_filters_dim=4, d_h=2, d_w=2, use_bias=False, scope="deconv_%d" % i, ) x = instance_norm(x, scope="in_decon_%d" % i) x = relu(x) load_size = int(load_size / 2) x = conv2d( x, 3, conv_filters_dim=7, d_h=1, d_w=1, pad=3, use_bias=False, scope="output", ) x = tanh(x) return x, angles, input_