def discriminator(inputs, is_train=True, reuse=False): dfs = 64 gamma_init = tf.random_normal_initializer(1., 0.02) W_init = tf.random_normal_initializer(stddev=0.02) with tf.variable_scope('discriminator', reuse=reuse): tl.layers.set_name_reuse(reuse) d = InputLayer(inputs, name='d/inputs') d = Conv2d(d, dfs, (5, 5), (2, 2), W_init=W_init, act=lambda x: tl.act.lrelu(x, 0.2), name='d/conv1') d = Conv2d(d, dfs * 2, (5, 5), (2, 2), W_init=W_init, act=None, name='d/conv2') d = BatchNormLayer(d, act=lambda x: tl.act.lrelu(x, 0.2), is_train=is_train, gamma_init=gamma_init, name='d/bn3') d = Conv2d(d, dfs * 4, (5, 5), (2, 2), W_init=W_init, act=None, name='d/conv4') d = BatchNormLayer(d, act=lambda x: tl.act.lrelu(x, 0.2), is_train=is_train, gamma_init=gamma_init, name='d/bn5') d = Conv2d(d, dfs * 8, (5, 5), (2, 2), W_init=W_init, act=None, name='d/conv6') d = BatchNormLayer(d, act=lambda x: tl.act.lrelu(x, 0.2), is_train=is_train, gamma_init=gamma_init, name='d/bn7') d = FlattenLayer(d, name='d/flt8') d = DenseLayer(d, 1, act=tl.act.identity, W_init=W_init, name='d/output') logits = d.outputs d.outputs = tf.nn.sigmoid(d.outputs) return d, logits
def discriminator(input, is_train=False, reuse=False): """ Cartoon GAN discriminator neural network :param input: TF Tensor input tensor :param is_train: boolean train or test flag :param reuse: boolean whether to reuse the discriminator neural network :return: """ w_init = tf.random_normal_initializer(stddev=0.02) gamma_init = tf.random_normal_initializer(1.0, stddev=0.02) leaky_relu = lambda x: tl.act.lrelu(x, 0.2) with tf.variable_scope('CartoonGAN_D', reuse=reuse): tl.layers.set_name_reuse(reuse) n = InputLayer(input, name='d_input') n = Conv2d(n, 32, (3, 3), (1, 1), act=leaky_relu, padding='SAME', W_init=w_init, name='block1/c') n = Conv2d(n, 64, (3, 3), (2, 2), act=leaky_relu, padding='SAME', W_init=w_init, name='block2/c1') n = Conv2d(n, 128, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='block2/c2') n = BatchNormLayer(n, act=leaky_relu, is_train=is_train, gamma_init=gamma_init, name='block2/b') n = Conv2d(n, 128, (3, 3), (2, 2), act=leaky_relu, padding='SAME', W_init=w_init, name='block3/c1') n = Conv2d(n, 256, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='block3/c2') n = BatchNormLayer(n, act=leaky_relu, is_train=is_train, gamma_init=gamma_init, name='block3/b') n = Conv2d(n, 256, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='block4/c') n = BatchNormLayer(n, act=leaky_relu, is_train=is_train, gamma_init=gamma_init, name='block4/b') n = Conv2d(n, 1, (3, 3), (1, 1), act=None, padding='SAME', W_init=w_init, name='d_output') n = FlattenLayer(n) n = DenseLayer(n, n_units=1, name='d_output') logits = n.outputs n.outputs = tf.nn.sigmoid(n.outputs) return n, logits, n.outputs
def __get_network_rnnfc__(self, model_name, encode_seqs, class_label_seqs, reuse=False, is_train=True): with tf.variable_scope(model_name, reuse=reuse): tl.layers.set_name_reuse(reuse) net_word_embed = InputLayer( inputs=encode_seqs, name="in_word_embed" ) net_class_label_embed = InputLayer( inputs=class_label_seqs, name="in_class_label_embed" ) net_class_label_embed.outputs = tf.slice( net_class_label_embed.outputs, [0, 0, 0], [config.batch_size, 1, self.word_embedding_dim], name="slice_word" ) net_class_label_embed.outputs = tf.squeeze( net_class_label_embed.outputs, name="squeeze_word" ) net_in = ConcatLayer( [net_word_embed], concat_dim=-1, name='concat_vw' ) net_rnn = RNNLayer( net_in, cell_fn=tf.contrib.rnn.BasicLSTMCell, n_hidden = 512, n_steps = self.max_length, return_last = True, name = 'lstm' ) net_fc = ConcatLayer([net_rnn, net_class_label_embed], concat_dim=-1) net_fc = DropoutLayer(net_fc, keep=0.5, is_fix=True, is_train=is_train, name='drop1') net_fc = DenseLayer( net_fc, n_units=400, act=tf.nn.relu, name="fc_1" ) net_fc = DropoutLayer(net_fc, keep=0.5, is_fix=True, is_train=is_train, name='drop2') # dbpedia net_fc = DenseLayer( net_fc, n_units=100, act=tf.nn.relu, name="fc_2" ) net_fc = DropoutLayer(net_fc, keep=0.5, is_fix=True, is_train=is_train, name='drop3') net_fc = DenseLayer( net_fc, n_units=1, act=tf.nn.sigmoid, name="fc_3" ) return net_fc
def __get_network_cnnfc__(self, model_name, encode_seqs, class_label_seqs, reuse=False, is_train=True): with tf.variable_scope(model_name, reuse=reuse): tl.layers.set_name_reuse(reuse) net_word_embed = InputLayer( inputs=encode_seqs, name="in_word_embed" ) net_class_label_embed = InputLayer( inputs=class_label_seqs, name="in_class_label_embed" ) net_class_label_embed.outputs = tf.slice( net_class_label_embed.outputs, [0, 0, 0], [config.batch_size, 1, self.word_embedding_dim], name="slice_word" ) net_class_label_embed.outputs = tf.squeeze( net_class_label_embed.outputs, name="squeeze_word" ) net_in = ConcatLayer( [net_word_embed], concat_dim=-1, name='concat_vw' ) filter_length = [2, 4, 8] # dbpedia n_filter = 600 # n_filter = 200 net_cnn_list = list() for fsz in filter_length: net_cnn = Conv1d( net_in, n_filter=n_filter, filter_size=fsz, stride=1, act=tf.nn.relu, name="cnn%d" % fsz ) net_cnn.outputs = tf.reduce_max(net_cnn.outputs, axis=1, name="global_maxpool%d" % fsz) net_cnn_list.append(net_cnn) net_cnn = ConcatLayer(net_cnn_list + [net_class_label_embed], concat_dim=-1) net_fc = DropoutLayer(net_cnn, keep=0.5, is_fix=True, is_train=is_train, name='drop1') net_fc = DenseLayer( net_fc, n_units=400, act=tf.nn.relu, name="fc_1" ) net_fc = DropoutLayer(net_fc, keep=0.5, is_fix=True, is_train=is_train, name='drop2') # dbpedia net_fc = DenseLayer( net_fc, n_units=100, act=tf.nn.relu, name="fc_2" ) net_fc = DropoutLayer(net_fc, keep=0.5, is_fix=True, is_train=is_train, name='drop3') net_fc = DenseLayer( net_fc, n_units=1, act=tf.nn.sigmoid, name="fc_3" ) return net_fc
def generator(inputs, is_train=True, reuse=False): img_size = CFG.img_size s2, s4, s8, s16 = [int(img_size / i) for i in [2, 4, 8, 16]] gfs = 64 channels = CFG.channels batch_size = CFG.batch_size W_init = tf.random_normal_initializer(stddev=0.02) gamma_init = tf.random_normal_initializer(1., 0.02) with tf.variable_scope('generator', reuse=reuse): tl.layers.set_name_reuse(reuse) g = InputLayer(inputs, name='g/inputs') g = DenseLayer(g, gfs * 8 * s16 * s16, W_init=W_init, act=tl.act.identity, name='g/fc1') g = ReshapeLayer(g, shape=(-1, s16, s16, gfs * 8), name='g/reshape2') g = BatchNormLayer(g, act=tf.nn.relu, is_train=is_train, gamma_init=gamma_init, name='g/bn3') g = DeConv2d(g, gfs * 4, (5, 5), out_size=(s8, s8), strides=(2, 2), batch_size=batch_size, act=None, W_init=W_init, name='g/dconv4') g = BatchNormLayer(g, act=tf.nn.relu, is_train=is_train, gamma_init=gamma_init, name='g/bn5') g = DeConv2d(g, gfs * 2, (5, 5), out_size=(s4, s4), strides=(2, 2), batch_size=batch_size, act=None, W_init=W_init, name='g/dconv6') g = BatchNormLayer(g, act=tf.nn.relu, is_train=is_train, gamma_init=gamma_init, name='g/bn7') g = DeConv2d(g, gfs, (5, 5), out_size=(s2, s2), strides=(2, 2), batch_size=batch_size, act=None, W_init=W_init, name='g/dconv8') g = BatchNormLayer(g, act=tf.nn.relu, is_train=is_train, gamma_init=gamma_init, name='g/bn9') g = DeConv2d(g, channels, (5, 5), out_size=(img_size, img_size), strides=(2, 2), batch_size=batch_size, act=None, W_init=W_init, name='g/dconv10') logits = g.outputs g.outputs = tf.nn.tanh(g.outputs) return g, logits
def UNet_A(lf_extra, n_slices, output_size, is_train=True, reuse=False, name='unet'): '''U-net based VCD-Net for light field reconstruction. Params: lf_extra: tf.tensor In shape of [batch, height, width, n_num^2], the extracted views from the light field image n_slices: int The slices number of the 3-D reconstruction. output_size: list of int Lateral size of the 3-D reconstruction, i.e., [height, width]. is_train: boolean Sees tl.layers.BatchNormLayer. reuse: boolean Whether to reuse the variables or not. See tf.variable_scope() for details. name: string The name of the variable scope. Return: The 3-D reconstruction in shape of [batch, height, width, depth=n_slices] ''' n_interp = 4 # _, w, h, _ = lf_extra.shape #channels_interp = in_channels.value channels_interp = 128 act = tf.nn.relu with tf.variable_scope(name, reuse=reuse): n = InputLayer(lf_extra, 'lf_extra') n = conv2d(n, n_filter=channels_interp, filter_size=7, name='conv1') ## Up-scale input with tf.variable_scope('interp'): for i in range(n_interp): channels_interp = channels_interp / 2 n = SubpixelConv2d(n, scale=2, name='interp/subpixel%d' % i) n = conv2d(n, n_filter=channels_interp, filter_size=3, name='conv%d' % i) n = conv2d(n, n_filter=channels_interp, filter_size=3, name='conv_final') # 176*176 n = batch_norm(n, is_train=is_train, name='bn_final') n = ReluLayer(n, name='reul_final') pyramid_channels = [ 128, 256, 512, 512, 512 ] # output channels number of each conv layer in the encoder encoder_layers = [] with tf.variable_scope('encoder'): n = conv2d(n, n_filter=64, filter_size=3, stride=1, name='conv0') n = batch_norm(n, is_train=is_train, name='bn_0') n = ReluLayer(n, name='reul0') for idx, nc in enumerate(pyramid_channels): encoder_layers.append( n ) # append n0, n1, n2, n3, n4 (but without n5)to the layers list print('encoder %d : %s' % (idx, str(n.outputs.get_shape()))) n = conv2d(n, n_filter=nc, filter_size=3, stride=1, name='conv%d' % (idx + 1)) n = batch_norm(n, is_train=is_train, name='bn%d' % (idx + 1)) n = ReluLayer(n, name='reul%d' % (idx + 1)) n1 = PadDepth(encoder_layers[-1], desired_channels=nc) n = merge([n, n1], name='add%d' % (idx + 1)) n = tl.layers.MaxPool2d(n, filter_size=(3, 3), strides=(2, 2), name='maxplool%d' % (idx + 1)) nl = len(encoder_layers) with tf.variable_scope('decoder'): _, h, w, _ = encoder_layers[-1].outputs.shape.as_list() n = UpSampling2dLayer(n, size=(h, w), is_scale=False, name='upsamplimg') for idx in range(nl - 1, -1, -1): # idx = 4,3,2,1,0 if idx > 0: _, h, w, _ = encoder_layers[idx - 1].outputs.shape.as_list() out_size = (h, w) out_channels = pyramid_channels[idx - 1] else: #out_size = None out_channels = n_slices print('decoder %d : %s' % (idx, str(n.outputs.get_shape()))) n = ConcatLayer([encoder_layers[idx], n], concat_dim=-1, name='concat%d' % (nl - idx)) n = conv2d(n, out_channels, filter_size=3, stride=1, name='conv%d' % (nl - idx + 1)) n = ReluLayer(n, name='relu%d' % (nl - idx + 1)) n = batch_norm(n, is_train=is_train, name='bn%d' % (nl - idx + 1)) #n = UpConv(n, 512, filter_size=4, factor=2, name='upconv2') n = UpSampling2dLayer(n, size=out_size, is_scale=False, name='upsamplimg%d' % (nl - idx + 1)) #n = DropoutLayer(n, keep=0.5, is_fix=True, is_train=is_train, name='dropout1') if n.outputs.shape[1] != output_size[0]: n = UpSampling2dLayer(n, size=output_size, is_scale=False, name='resize_final') #n = conv2d(n, n_slices, filter_size=3, stride=1,name='conv_final' ) n.outputs = tf.tanh(n.outputs) #n.outputs = tf.nn.relu(n.outputs) #n = conv2d(n, n_filter=n_slices, filter_size=3, act=tf.tanh, name='out') return n