def decoder(self,input_z,name = 'generate_img',is_training = True): hidden_num = 64 output_dim = 64 with tf.variable_scope(name,reuse = tf.AUTO_REUSE): x = ly.fc(input_z, hidden_num * 8 * (output_dim // 16) * (output_dim // 16),name = 'gen_fc_0') x = tf.reshape(x, shape=[self.imle_deep, output_dim // 16, output_dim // 16, hidden_num * 8]) ## 4, 4, 8*64 x = ly.deconv2d(x,hidden_num * 4,name = 'g_deconv2d_0') ### 8,8, 256 x = ly.batch_normal(x,name = 'g_deconv_bn_0',is_training = is_training) x = ly.relu(x) x = ly.deconv2d(x,hidden_num * 2,name = 'g_deconv2d_1') ### 16,16, 128 x = ly.batch_normal(x,name = 'g_deconv_bn_1',is_training = is_training) x = ly.relu(x) x = ly.deconv2d(x,hidden_num,name = 'g_deconv2d_2') ### 32,32, 64 x = ly.batch_normal(x,name = 'g_deconv_bn_2',is_training = is_training) x = ly.relu(x) x = ly.deconv2d(x, 3, name = 'g_deconv2d_3') ### 64,64, 3 x = ly.batch_normal(x,name = 'g_deconv_bn_3',is_training = is_training) x = tf.nn.tanh(x) return x
def discriminator(self, x, name='discriminator_img', is_training=True): ## 64,64,3 with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x = ly.conv2d(x, 64, strides=2, use_bias=True, name='d_conv_0') ## 32,32,64 x = ly.batch_normal(x, name='d_bn_0', is_training=is_training) x = ly.relu(x, 0.2) x = ly.conv2d(x, 128, strides=2, use_bias=True, name='d_conv_1') ## 16,16,128 x = ly.batch_normal(x, name='d_bn_1', is_training=is_training) x = ly.relu(x, 0.2) x = ly.conv2d(x, 256, strides=2, use_bias=True, name='d_conv_2') ## 8,8,256 x = ly.batch_normal(x, name='d_bn_2', is_training=is_training) x = ly.relu(x, 0.2) x = ly.conv2d(x, 512, strides=2, use_bias=True, name='d_conv_3') ## 4,4,512 x = ly.batch_normal(x, name='d_bn_3', is_training=is_training) x = ly.relu(x, 0.2) x = ly.fc(x, 1, name='fc_0') x = tf.nn.sigmoid(x) return x
def __call__(self, input): with tf.variable_scope(self.name, reuse=self.reuse): input = ly.conv2d(input, 64, strides=2, name='conv_0') ## (-1,150,150,64) input = ly.batch_normal(input, name='bn_0') input = tf.nn.leaky_relu(input) input = ly.conv2d(input, 128, strides=2, name='conv_1') ## (-1,75,75,128) input = ly.batch_normal(input, name='bn_1') input = tf.nn.leaky_relu(input) input = ly.conv2d(input, 256, strides=2, name='conv_2') ## (-1,38,38,256) input = ly.batch_normal(input, name='bn_2') input = tf.nn.leaky_relu(input) input = ly.conv2d(input, 512, strides=2, name='conv_3') ## (-1,19,19,512) input = ly.batch_normal(input, name='bn_3') input = tf.nn.leaky_relu(input) print(input.shape) input = ly.conv2d(input, 512, strides=2, name='conv_4') ## (-1,10,10,512) input = ly.batch_normal(input, name='bn_4') input = tf.nn.leaky_relu(input) ## avg input = tf.reduce_mean(input, axis=[1, 2]) input = tf.nn.dropout(input, keep_prob=0.5) input = ly.fc(input, 1, name='fc_0') # input = ly.batch_normal(input, name='bn_5') input = tf.nn.sigmoid(input) return input
def classify(self, d_opt=None, name='classify', is_training=True): ### 64,64,1 with tf.variable_scope(name, reuse=tf.AUTO_REUSE): x = tf.pad(self.input_img, [[0, 0], [5, 5], [5, 5], [0, 0]], "REFLECT") x = ly.conv2d(x, 64, kernal_size=11, name='conv_0', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_0', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 32,32,64 x = tf.pad(x, [[0, 0], [3, 3], [3, 3], [0, 0]], "REFLECT") x = ly.conv2d(x, 128, kernal_size=7, name='conv_1', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_1', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 16,16,128 x = tf.pad(x, [[0, 0], [2, 2], [2, 2], [0, 0]], "REFLECT") x = ly.conv2d(x, 256, kernal_size=5, name='conv_2', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_2', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 8,8,256 x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], "REFLECT") x = ly.conv2d(x, 512, kernal_size=3, name='conv_3', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_3', is_training=is_training) x = ly.relu(x) x = tf.pad(x, [[0, 0], [1, 1], [1, 1], [0, 0]], "REFLECT") x = ly.conv2d(x, 512, kernal_size=3, name='conv_4', padding='VALID', use_bias=True) x = ly.batch_normal(x, name='bn_4', is_training=is_training) x = ly.relu(x) x = ly.maxpooling2d(x) ## 4,4,512 x = ly.fc(x, 1024, name='fc_0', use_bias=True) x = ly.batch_normal(x, name='bn_5', is_training=is_training) x = ly.relu(x) x = tf.nn.dropout(x, keep_prob=0.5) x = ly.fc(x, self.class_num, name='fc_1', use_bias=True) self.pred_x_index = tf.argmax(tf.nn.softmax(x), axis=-1) self.pred_x_value = tf.reduce_max(tf.nn.softmax(x), axis=-1) if (is_training): cross_loss = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits_v2( labels=self.input_label, logits=x), axis=0) l2_loss = 0.0005 * tf.reduce_sum([ tf.nn.l2_loss(var) for var in self.get_single_var('classify/fc') ]) loss = cross_loss + l2_loss self.summaries.append(tf.summary.scalar('loss', loss)) _grad = d_opt.compute_gradients( loss, var_list=self.get_vars('classify')) train_op = d_opt.apply_gradients(_grad) return train_op
def __call__(self, input): with tf.variable_scope(self.name, reuse=self.reuse): input = ly.conv2d(input, 64, kernel_size=7, strides=1, name='g_conv2d_0') input = ly.batch_normal(input, name='g_bn_0') input = tf.nn.relu(input) input = ly.conv2d(input, 128, kernel_size=3, strides=2, name='g_conv2d_1') input = ly.batch_normal(input, name='g_bn_1') input = tf.nn.relu(input) input = ly.conv2d(input, 256, kernel_size=3, strides=2, name='g_conv2d_2') input = ly.batch_normal(input, name='g_bn_2') input = tf.nn.relu(input) ### resnet for i in range(8): cell = ly.conv2d(input, 256, kernel_size=3, strides=1, name='g_conv2d_res_%s' % i) cell = ly.batch_normal(cell, name='g_res_%s' % i) cell = tf.nn.relu(cell) input = cell input = ly.deconv2d(input, kernel_size=3, strides=2, name='g_deconv2d_0') input = ly.batch_normal(input, name='g_bn_3') input = tf.nn.relu(input) input = ly.deconv2d(input, kernel_size=3, strides=2, name='g_deconv2d_1') input = ly.batch_normal(input, name='g_bn_4') input = tf.nn.relu(input) input = ly.conv2d(input, 3, kernel_size=7, strides=1, name='g_conv2d_3') input = ly.batch_normal(input, name='g_bn_5') input = tf.nn.tanh(input) input = tf.image.resize_images(input, (299, 299)) return input ## (-1,28,28,1)