def _gradient_penalty(self, X, X_hat, L, G, Z, Z_hat, EPSILON): ax1 = [i for i in range(1, len(X.get_shape()))] ax2 = [i for i in range(1, len(Z.get_shape()))] with tf.name_scope('grad_penalty'): with tf.name_scope('x'): x_inter = ops.interpolate(X, X_hat, EPSILON, name='x_inter') d_inter_x = self._discriminator(x_inter, L, is_training=True, reuse=True) x_grads, z_grads = tf.gradients([d_inter_x], [x_inter, L], name="grads_x") x_slopes = tf.sqrt(tf.reduce_sum(x_grads**2, axis=ax1)) tf.summary.scalar('x_slopes', tf.reduce_mean(x_slopes)) delta_x = X - X_hat x_unit = delta_x / ops.norm(delta_x, ax1, True) x_alpha = tf.reduce_sum(x_grads * x_unit, ax1) / x_slopes tf.summary.scalar('x_alpha', tf.reduce_mean(x_alpha)) tf.summary.scalar('x_unit_grads', tf.reduce_mean(ops.norm(x_unit, ax1))) x_pen = tf.sqrt(tf.reduce_sum((x_grads - x_unit)**2, axis=ax1)) gp_loss_x = tf.reduce_mean(x_pen, name='gp_loss_x') tf.add_to_collection('losses', gp_loss_x) tf.summary.scalar('gp_loss_x', gp_loss_x) with tf.name_scope('z'): z_inter = ops.interpolate(Z, Z_hat, EPSILON, name='z_inter') d_inter_z = -self._discriminator( G, z_inter, is_training=True, reuse=True) _, z_grads = tf.gradients([d_inter_z], [G, z_inter], name="grads_z") delta_z = Z - Z_hat z_unit = delta_z / ops.norm(delta_z, ax2, True) z_pen = tf.sqrt(tf.reduce_sum((z_grads - z_unit)**2, axis=ax2)) gp_loss_z = tf.reduce_mean(z_pen, name='gp_loss_z') tf.add_to_collection('losses', gp_loss_z) tf.summary.scalar('gp_loss_z', gp_loss_z) gp_loss = gp_loss_x # gp_loss = gp_loss_x + gp_loss_z tf.add_to_collection('losses', gp_loss) tf.summary.scalar('gp_loss', gp_loss) return gp_loss
def loadModel(self, filename, translate, scale, normobj=False): model = obj(filename) light = [0, 0, 1] for face in model.faces: vertCount = len(face) if normobj: for vert in range(vertCount): v0 = model.vertices[int(face[vert][0]) - 1] v1 = model.vertices[int(face[(vert + 1) % vertCount][0]) - 1] x0 = int(v0[0] * scale[0] + translate[0]) y0 = int(v0[1] * scale[1] + translate[1]) x1 = int(v1[0] * scale[0] + translate[0]) y1 = int(v1[1] * scale[1] + translate[1]) self.glLineWin(x0, y0, x1, y1) else: v0 = model.vertices[face[0][0] - 1] v1 = model.vertices[face[1][0] - 1] v2 = model.vertices[face[2][0] - 1] v0 = self.transform1(v0, translate, scale) v1 = self.transform1(v1, translate, scale) v2 = self.transform1(v2, translate, scale) #polycolor = color(random.randint(0,255) / 255, # random.randint(0,255) / 255, # random.randint(0,255) / 255) normal = op.cross(op.subtract(v1, v0), op.subtract(v2, v0)) normal = op.divide(normal, op.norm(normal)) intensity = op.dot(normal, light) if intensity >= 0: self.triangle_bc( v0, v1, v2, self.glColor(intensity, intensity, intensity)) if vertCount > 3: #asumamos que 4, un cuadrado v3 = model.vertices[face[3][0] - 1] if intensity >= 0: self.triangle_bc( v0, v2, v3, color(intensity, intensity, intensity))
def __call__(self, EC_input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): EC_input = tf.nn.dropout(EC_input, keep_prob=self.keep_prob) with tf.variable_scope("conv0", reuse=self.reuse): conv0 = tf.layers.conv2d( inputs=EC_input, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / 9.0, stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv0') norm0 = ops.norm(conv0) relu0 = ops.relu(norm0) # pool1 with tf.variable_scope("conv1", reuse=self.reuse): conv1 = tf.layers.conv2d( inputs=relu0, filters=2 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv1') norm1 = ops._norm(conv1) relu1 = ops.relu(norm1) # w/2,h/2 with tf.variable_scope("conv2", reuse=self.reuse): conv2 = tf.layers.conv2d( inputs=relu1, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv2') norm2 = ops._norm(conv2) relu2 = ops.relu(norm2) # pool2 with tf.variable_scope("conv3", reuse=self.reuse): conv3 = tf.layers.conv2d( inputs=relu2, filters=4 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv3') norm3 = ops._norm(conv3) relu3 = ops.relu(norm3) # w/4,h/4 with tf.variable_scope("conv4", reuse=self.reuse): conv4 = tf.layers.conv2d( inputs=relu3, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv4') norm4 = ops._norm(conv4) relu4 = ops.relu(norm4) with tf.variable_scope("conv5", reuse=self.reuse): conv5 = tf.layers.conv2d( inputs=relu4, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv5') norm5 = ops._norm(conv5) relu5 = tf.nn.relu(norm5) # pool3 with tf.variable_scope("conv6", reuse=self.reuse): conv6 = tf.layers.conv2d( inputs=relu5, filters=6 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv6') norm6 = ops._norm(conv6) relu6 = ops.relu(norm6) # w/8,h/8 18 23 with tf.variable_scope("conv7", reuse=self.reuse): conv7 = tf.layers.conv2d( inputs=relu6, filters=6 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 6 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv7') norm7 = ops._norm(conv7) relu7 = ops.relu(norm7) # pool4 with tf.variable_scope("conv8", reuse=self.reuse): conv8 = tf.layers.conv2d( inputs=relu7, filters=8 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 6 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv8') norm8 = ops._norm(conv8) relu8 = tf.nn.relu(norm8) # 9 12 with tf.variable_scope("conv9", reuse=self.reuse): conv9 = tf.layers.conv2d( inputs=relu8, filters=8 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv9') norm9 = ops._norm(conv9) relu9 = tf.nn.relu(norm9) # pool5 with tf.variable_scope("conv10", reuse=self.reuse): conv10 = tf.layers.conv2d( inputs=relu9, filters=12 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv10') norm10 = ops._norm(conv10) relu10 = tf.nn.relu(norm10) conv_output = tf.layers.flatten(relu10) # 5 6 with tf.variable_scope("dense1", reuse=self.reuse): mean = tf.layers.dense(conv_output, units=4096, name="dense1") with tf.variable_scope("dense2", reuse=self.reuse): log_var = tf.layers.dense(conv_output, units=4096, name="dense2") self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return mean, log_var
def __call__(self, DC_input): """ Args: input: batch_size x width x height x N Returns: output: same size as input """ with tf.variable_scope(self.name, reuse=self.reuse): with tf.variable_scope("dense0", reuse=self.reuse): dense0 = tf.layers.dense( DC_input, units=DC_input.get_shape().as_list()[0] * 6 * 5 * self.ngf, name="dense0") with tf.variable_scope("dense1", reuse=self.reuse): dense1 = tf.layers.dense( dense0, units=DC_input.get_shape().as_list()[0] * 6 * 5 * 12 * self.ngf, name="dense0") dense1 = tf.reshape(dense1, shape=[ DC_input.get_shape().as_list()[0], 6, 5, 12 * self.ngf ]) # 6,5 with tf.variable_scope("conv0_1", reuse=self.reuse): conv0_1 = tf.layers.conv2d( inputs=dense1, filters=12 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 12 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv0_1') norm0_1 = ops.norm(conv0_1) relu0_1 = ops.relu(norm0_1) # 6,5 with tf.variable_scope("deconv0_1_r", reuse=self.reuse): resize0_1 = ops.uk_resize(relu0_1, reuse=self.reuse, output_size=[12, 9], name='resize') deconv0_1_r = tf.layers.conv2d( inputs=resize0_1, filters=8 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 12 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv0_1_r') deconv0_1_norm1_r = ops.norm(deconv0_1_r) deconv0_1_relu1 = ops.relu(deconv0_1_norm1_r) # 12,9 with tf.variable_scope("conv0_2", reuse=self.reuse): conv0_2 = tf.layers.conv2d( inputs=deconv0_1_relu1, filters=8 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv0_2') norm0_2 = ops.norm(conv0_2) relu0_2 = ops.relu(norm0_2) # 12,9 with tf.variable_scope("deconv0_2_r", reuse=self.reuse): resize0_2 = ops.uk_resize(relu0_2, reuse=self.reuse, output_size=[23, 18], name='resize') deconv0_2_r = tf.layers.conv2d( inputs=resize0_2, filters=6 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv0_2_r') deconv0_2_norm1_r = ops.norm(deconv0_2_r) deconv0_2_relu1 = ops.relu(deconv0_2_norm1_r) # 23, 18 with tf.variable_scope("conv0_3", reuse=self.reuse): conv0_3 = tf.layers.conv2d( inputs=deconv0_2_relu1, filters=6 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 6 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv0_3') norm0_3 = ops.norm(conv0_3) relu0_3 = ops.relu(norm0_3) # 23, 18 with tf.variable_scope("deconv0_3_r", reuse=self.reuse): resize0_3 = ops.uk_resize(relu0_3, reuse=self.reuse, name='resize') deconv0_3_r = tf.layers.conv2d( inputs=resize0_3, filters=6 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 6 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv0_3_r') deconv0_3_norm1_r = ops.norm(deconv0_3_r) add0 = ops.relu(deconv0_3_norm1_r) # 46, 36 with tf.variable_scope("conv1", reuse=self.reuse): conv1 = tf.layers.conv2d( inputs=add0, filters=6 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 6 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv1') norm1 = ops.norm(conv1) relu1 = ops.relu(norm1) with tf.variable_scope("deconv1_r", reuse=self.reuse): resize1 = ops.uk_resize(relu1, reuse=self.reuse, name='resize') deconv1_r = tf.layers.conv2d( inputs=resize1, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 6 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv1_r') deconv1_norm1_r = ops.norm(deconv1_r) add1 = ops.relu(deconv1_norm1_r) with tf.variable_scope("add1_conv1", reuse=self.reuse): add1_conv1 = tf.layers.conv2d( inputs=add1, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add1_conv1') add1_norm1 = ops.norm(add1_conv1) add1_relu1 = ops.relu(add1_norm1) with tf.variable_scope("add1_conv2", reuse=self.reuse): add1_conv2 = tf.layers.conv2d( inputs=add1_relu1, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add1_conv2') add1_norm2 = ops.norm(add1_conv2) add1_relu2 = ops.relu(add1_norm2) with tf.variable_scope("deconv2_r", reuse=self.reuse): resize2 = ops.uk_resize(add1_relu2, reuse=self.reuse, name='resize') deconv2_r = tf.layers.conv2d( inputs=resize2, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv2_r') deconv2_norm1_r = ops.norm(deconv2_r) add2 = ops.relu(deconv2_norm1_r) with tf.variable_scope("add2_conv1", reuse=self.reuse): add2_conv1 = tf.layers.conv2d( inputs=add2, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add2_conv1') add2_norm1 = ops.norm(add2_conv1) add2_relu1 = ops.relu(add2_norm1) with tf.variable_scope("add2_conv2", reuse=self.reuse): add2_conv = tf.layers.conv2d( inputs=add2_relu1, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add2_conv2') add2_norm2 = ops.norm(add2_conv) add2_relu2 = ops.relu(add2_norm2) with tf.variable_scope("conv2", reuse=self.reuse): conv2 = tf.layers.conv2d( inputs=add2_relu2, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv2') norm2 = ops.norm(conv2) relu2 = ops.relu(norm2) with tf.variable_scope("lastconv", reuse=self.reuse): lastconv = tf.layers.conv2d( inputs=relu2, filters=self.output_channl, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='lastconv') lastnorm = ops.norm(lastconv) output = tf.nn.sigmoid(lastnorm) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, EC_input): """ Args: input: batch_size x width x height x 3 Returns: output: same size as input """ with tf.variable_scope(self.name): EC_input = tf.nn.dropout(EC_input, keep_prob=self.keep_prob) with tf.variable_scope("conv1", reuse=self.reuse): conv1 = tf.layers.conv2d( inputs=EC_input, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 1), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv1') norm1 = ops.norm(conv1) relu1 = ops.relu(norm1) with tf.variable_scope("conv2", reuse=self.reuse): conv2 = tf.layers.conv2d( inputs=relu1, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv2') norm2 = ops.norm(conv2) relu2 = ops.relu(norm2) # pool1 with tf.variable_scope("conv3", reuse=self.reuse): conv3 = tf.layers.conv2d( inputs=relu2, filters=2 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv3') norm3 = ops.norm(conv3) relu3 = ops.relu(norm3) # w/2,h/2 with tf.variable_scope("conv4", reuse=self.reuse): conv4 = tf.layers.conv2d( inputs=relu3, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv4') norm4 = ops.norm(conv4) relu4 = ops.relu(norm4) with tf.variable_scope("conv5", reuse=self.reuse): conv5 = tf.layers.conv2d( inputs=relu4, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv5') norm5 = ops.norm(conv5) relu5 = tf.nn.relu(norm5) # pool2 with tf.variable_scope("conv6", reuse=self.reuse): conv6 = tf.layers.conv2d( inputs=relu5, filters=8 * self.ngf, kernel_size=3, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv6') norm6 = ops.norm(conv6) relu6 = ops.relu(norm6) # w/4,h/4 with tf.variable_scope("conv7", reuse=self.reuse): conv7 = tf.layers.conv2d( inputs=relu6, filters=8 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv7') norm7 = ops.norm(conv7) output = ops.relu(norm7) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
import ops as op import numpy as np a = [1, 2, 3] b = [4, 5, 6] print(op.norm(a)) print(np.linalg.norm(a))
def discriminator(self, input_, cond_): if self.norm_type == "batch_norm": from ops import batch_norm as norm elif self.norm_type == "instance_norm": from ops import instance_norm as norm else: raise Exception("Unknown normalization layer!!!") if not self.norm_type == "instance_norm": input_ = tf.nn.dropout(input_, self.kp_) if self.d_arch == 0: h0 = lrelu(conv1d(input_, 128, k_w=4, name="conv1d_h0")) h1 = lrelu(norm(conv1d(h0, 256, k_w=4, name="conv1d_h1"), "bn1")) h1 = tf.concat([h1, tf.tile(cond_, [1, int(h1.shape[1]), 1])], axis=-1) h2 = lrelu(norm(conv1d(h1, 512, k_w=4, name="conv1d_h2"), "bn2")) h2 = tf.concat([h2, tf.tile(cond_, [1, int(h2.shape[1]), 1])], axis=-1) h3 = lrelu(norm(conv1d(h2, 1024, k_w=4, name="conv1d_h3"), "bn3")) h3 = tf.concat([h3, tf.tile(cond_, [1, int(h3.shape[1]), 1])], axis=-1) logits = conv1d(h3, 1, k_w=4, name="logits", padding="VALID") elif self.d_arch == 1: h0 = lrelu(conv1d(input_, 64, k_w=4, name="conv1d_h0")) h1 = lrelu(norm(conv1d(h0, 128, k_w=4, name="conv1d_h1"), "bn1")) h1 = tf.concat([h1, tf.tile(cond_, [1, int(h1.shape[1]), 1])], axis=-1) h2 = lrelu(norm(conv1d(h1, 256, k_w=4, name="conv1d_h2"), "bn2")) h2 = tf.concat([h2, tf.tile(cond_, [1, int(h2.shape[1]), 1])], axis=-1) h3 = lrelu(norm(conv1d(h2, 512, k_w=4, name="conv1d_h3"), "bn3")) h3 = tf.concat([h3, tf.tile(cond_, [1, int(h3.shape[1]), 1])], axis=-1) logits = conv1d(h3, 1, k_w=4, name="logits", padding="VALID") elif self.d_arch == 2: h0 = lrelu(conv1d(input_, 32, k_w=4, name="conv1d_h0")) h1 = lrelu(norm(conv1d(h0, 64, k_w=4, name="conv1d_h1"), "bn1")) h1 = tf.concat([h1, tf.tile(cond_, [1, int(h1.shape[1]), 1])], axis=-1) h2 = lrelu(norm(conv1d(h1, 128, k_w=4, name="conv1d_h2"), "bn2")) h2 = tf.concat([h2, tf.tile(cond_, [1, int(h2.shape[1]), 1])], axis=-1) h3 = lrelu(norm(conv1d(h2, 256, k_w=4, name="conv1d_h3"), "bn3")) h3 = tf.concat([h3, tf.tile(cond_, [1, int(h3.shape[1]), 1])], axis=-1) logits = conv1d(h3, 1, k_w=4, name="logits", padding="VALID") elif self.d_arch == 3: h0 = lrelu(conv1d(input_, 16, k_w=4, name="conv1d_h0")) h1 = lrelu(norm(conv1d(h0, 32, k_w=4, name="conv1d_h1"), "bn1")) h1 = tf.concat([h1, tf.tile(cond_, [1, int(h1.shape[1]), 1])], axis=-1) h2 = lrelu(norm(conv1d(h1, 64, k_w=4, name="conv1d_h2"), "bn2")) h2 = tf.concat([h2, tf.tile(cond_, [1, int(h2.shape[1]), 1])], axis=-1) h3 = lrelu(norm(conv1d(h2, 128, k_w=4, name="conv1d_h3"), "bn3")) h3 = tf.concat([h3, tf.tile(cond_, [1, int(h3.shape[1]), 1])], axis=-1) logits = conv1d(h3, 1, k_w=4, name="logits", padding="VALID") else: raise Exception("Unknown discriminator architecture!!!") return tf.reshape(logits, [self.batch_size, 1])
def __call__(self, DC_input): """ Args: input: batch_size x width x height x N Returns: output: same size as input """ with tf.variable_scope(self.name, reuse=self.reuse): DC_input = tf.nn.dropout(DC_input, keep_prob=self.keep_prob) with tf.variable_scope("conv1", reuse=self.reuse): conv1 = tf.layers.conv2d(inputs=DC_input, filters=8 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv1') norm1 = ops.norm(conv1) relu1 = ops.relu(norm1) with tf.variable_scope("deconv1_r", reuse=self.reuse): resize1 = ops.uk_resize(relu1, reuse=self.reuse, name='resize1') deconv1_r = tf.layers.conv2d(inputs=resize1, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 8 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv1_r') deconv1_norm1_r = ops.norm(deconv1_r) add1 = ops.relu(deconv1_norm1_r) with tf.variable_scope("add1_conv1", reuse=self.reuse): add1_conv1 = tf.layers.conv2d(inputs=add1, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add1_conv1') add1_norm1 = ops.norm(add1_conv1) add1_relu1 = ops.relu(add1_norm1) with tf.variable_scope("add1_conv2", reuse=self.reuse): add1_conv2 = tf.layers.conv2d(inputs=add1_relu1, filters=4 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add1_conv2') add1_norm2 = ops.norm(add1_conv2) add1_relu2 = ops.relu(add1_norm2) with tf.variable_scope("deconv2_r", reuse=self.reuse): resize2 = ops.uk_resize(add1_relu2, reuse=self.reuse, name='resize1') deconv2_r = tf.layers.conv2d(inputs=resize2, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 4 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='deconv2_r') deconv2_norm1_r = ops.norm(deconv2_r) add2 = ops.relu(deconv2_norm1_r) with tf.variable_scope("add2_conv1", reuse=self.reuse): add2_conv1 = tf.layers.conv2d(inputs=add2, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add2_conv1') add2_norm1 = ops.norm(add2_conv1) add2_relu1 = ops.relu(add2_norm1) with tf.variable_scope("add2_conv2", reuse=self.reuse): add2_conv = tf.layers.conv2d(inputs=add2_relu1, filters=2 * self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='add2_conv2') add2_norm2 = ops.norm(add2_conv) add2_relu2 = ops.relu(add2_norm2) with tf.variable_scope("conv2", reuse=self.reuse): conv2 = tf.layers.conv2d(inputs=add2_relu2, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * 2 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv2') norm2 = ops.norm(conv2) relu2 = ops.relu(norm2) with tf.variable_scope("lastconv", reuse=self.reuse): lastconv = tf.layers.conv2d(inputs=relu2, filters=self.output_channl, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=1.0 / (9.0 * self.ngf), stddev=0.000001, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='lastconv') lastnorm = ops.norm(lastconv) output = tf.nn.sigmoid(lastnorm) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output
def __call__(self, FD_input): """ Args: input: batch_size x image_size x image_size x 3 Returns: output: 4D tensor batch_size x out_size x out_size x 1 (default 1x5x5x1) filled with 0.9 if real, 0.0 if fake """ with tf.variable_scope(self.name, reuse=self.reuse): FD_input = tf.nn.dropout(FD_input, keep_prob=self.keep_prob) with tf.variable_scope("conv1", reuse=self.reuse): conv1 = tf.layers.conv2d(inputs=FD_input, filters=self.ngf, kernel_size=7, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=0.0, stddev=0.02, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv1') norm1 = ops.norm(conv1) relu1 = ops.relu(norm1) with tf.variable_scope("conv2", reuse=self.reuse): conv2 = tf.layers.conv2d(inputs=relu1, filters=self.ngf, kernel_size=5, strides=2, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=0.0, stddev=0.02, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv2') norm2 = ops.norm(conv2) relu2 = ops.relu(norm2) with tf.variable_scope("conv3", reuse=self.reuse): conv3 = tf.layers.conv2d(inputs=relu2, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=0.0, stddev=0.02, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv3') norm3 = ops.norm(conv3) relu3 = ops.relu(norm3) with tf.variable_scope("conv4", reuse=self.reuse): conv4 = tf.layers.conv2d(inputs=relu3, filters=self.ngf, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=0.0, stddev=0.02, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv4') norm4 = ops.norm(conv4) relu4 = ops.relu(norm4) with tf.variable_scope("conv5", reuse=self.reuse): output = tf.layers.conv2d(inputs=relu4, filters=1, kernel_size=3, strides=1, padding="SAME", activation=None, kernel_initializer=tf.random_normal_initializer( mean=0.0, stddev=0.02, dtype=tf.float32), bias_initializer=tf.constant_initializer(0.0), name='conv5') self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) return output