def inference(self, input_): conv1 = layers.conv2d_same(input_, self.num_kernel, name='conv1') res_block1 = self.res_block(conv1, self.num_kernel * 2, is_downsizing=False, name='res_block1') res_block2 = self.res_block(res_block1, self.num_kernel * 4, is_downsizing=True, name='res_block2') res_block3 = self.res_block(res_block2, self.num_kernel * 8, is_downsizing=True, name='res_block3') act = self.res_act(res_block3) pool = layers.avg_pool(act, k_h=self.pool_kernel, k_w=self.pool_kernel, d_h=1, d_w=1, name='pool') flat = layers.flatten(pool, 'flat') linear = layers.linear(flat, self.num_class, name='linear') return linear
def latent(x, latent_size, reuse=False): """Add latant nodes to the graph. Args: x: Tensor, output from encoder. latent_size: Integer, size of latent vector. reuse: Boolean, whether to reuse variables. """ with arg_scope([dense], reuse = reuse): x = flatten(x) x = dense(x, 32*4*4, latent_size, name='d1') return x
def latent(x, batch_size, latent_size, reuse=False): """Adds latent nodes for sampling and reparamaterization. Args: x: Tensor, input images. batch_size: Integer, batch size. latent_size: Integer, size of latent vector. reuse: Boolean, whether to reuse variables. """ with arg_scope([dense], reuse=reuse): flat = flatten(x) z_mean = dense(flat, 32 * 4 * 4, latent_size, name='d1') z_stddev = dense(flat, 32 * 4 * 4, latent_size, name='d2') samples = tf.random_normal([batch_size, latent_size], 0, 1) z = (z_mean + (z_stddev * samples)) return (samples, z, z_mean, z_stddev)
def inference(self, input_): conv1 = layers.conv2d_same_repeat(input_, self.kernel_num, num_repeat=2, name="down1") pool1 = layers.max_pool(conv1, name="pool1") conv2 = layers.conv2d_same_repeat(pool1, self.kernel_num * 2, num_repeat=2, name="down2") pool2 = layers.max_pool(conv2, name="pool2") conv3 = layers.conv2d_same_repeat(pool2, self.kernel_num * 4, num_repeat=3, name="down3") pool3 = layers.max_pool(conv3, name="pool3") conv4 = layers.conv2d_same_repeat(pool3, self.kernel_num * 8, num_repeat=3, name="down4") pool4 = layers.max_pool(conv4, name="pool4") conv5 = layers.conv2d_same_repeat(pool4, self.kernel_num * 8, num_repeat=3, name="down5") pool5 = layers.max_pool(conv5, name="pool5") flat = layers.flatten(pool5, 'flat') linear = layers.linear(flat, flat.get_shape().as_list()[-1], name='linear') logits = layers.linear(linear, self.num_class, name='logits') return logits
def inference(self, input_, reuse=False): with tf.variable_scope('ResNet') as scope: if reuse: scope.reuse_variables() conv1 = layers.conv2d_same_act(input_, self.num_kernel, k_h=7, k_w=7, d_h=2, d_w=2, activation_fn=self.act_fn, name='conv_1') pool1 = layers.max_pool(conv1, k_h=self.pool_kernel, k_w=self.pool_kernel, padding='SAME', name='pool1') layer_blocks = self.layer_repeat(pool1, self.layer_def, name='layers') pool2 = layers.global_avg_pool(layer_blocks, name='pool2') flat = layers.flatten(pool2, 'flat') linear = layers.linear(flat, self.num_class, name='linear') logit = tf.sigmoid(linear, name='logit') return logit
def inference(self, input_): conv1 = layers.conv2d_same_repeat(input_, self.kernel_num, num_repeat=2, activation_fn=self.act_fn, name="down1") pool1 = layers.max_pool(conv1, name="pool1") conv2 = layers.conv2d_same_repeat(pool1, self.kernel_num * 2, num_repeat=2, activation_fn=self.act_fn, name="down2") pool2 = layers.max_pool(conv2, name="pool2") conv3 = layers.conv2d_same_repeat(pool2, self.kernel_num * 4, num_repeat=3, activation_fn=self.act_fn, name="down3") pool3 = layers.max_pool(conv3, name="pool3") conv4 = layers.conv2d_same_repeat(pool3, self.kernel_num * 8, num_repeat=3, activation_fn=self.act_fn, name="down4") pool4 = layers.max_pool(conv4, name="pool4") conv5 = layers.conv2d_same_repeat(pool4, self.kernel_num * 8, num_repeat=3, activation_fn=self.act_fn, name="down5") pool5 = layers.global_avg_pool(conv5, name="pool5") conv6 = layers.bottleneck_act(pool5, self.kernel_num * 8, activation_fn=self.act_fn, name="down6") conv7 = layers.bottleneck_layer(conv6, self.num_class, name="down7") logits = layers.flatten(conv7, 'flat') return logits
def encode(self, x, reuse=False, is_training=True): with tf.variable_scope(self.name): with tf.variable_scope('Encoder') as vs: if reuse: vs.reuse_variables() for i in range(3): filters = self.first_filters * (2**i) x = conv_block(x, filters=filters, sampling='same', is_training=is_training, **self.conv_block_params) x = conv_block(x, filters=filters, sampling='down', is_training=is_training, **self.conv_block_params) self.feature_shape = x.get_shape().as_list()[1:] x = flatten(x) x = dense(x, 128, activation_='lrelu') x = dense(x, self.latent_dim) return x
def pixel_wise_cross_entropy(input_, target_, lamb=1.0, name="pixel_ce"): flat = layers.flatten(input_) return cross_entropy_loss(flat, target_, lamb, name)
def gan(x, args): """Initialize model. Args: x: Tensor, the real images. args: Argparse structure. """ g_opt, d_opt = init_optimizer(args), init_optimizer(args) g_tower_grads, d_tower_grads = [], [] x = flatten(x) x = 2 * (x - 0.5) # rescale [0,1] to [-1,1] depending on model # if args.model in ['wgan', 'iwgan']: # x = 2 * (x - 0.5) for x, scope, gpu_id in tower_scope_range(x, args.n_gpus, args.batch_size): # model with tf.variable_scope('generator'): g = generator(args.batch_size, args.latent_size, args, reuse=(gpu_id > 0)) with tf.variable_scope('discriminator'): d_real = discriminator(x, args, reuse=(gpu_id > 0)) d_fake = discriminator(g, args, reuse=True) # losses g_loss, d_loss = losses(x, g, d_fake, d_real, args) # compute gradients g_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator') d_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator') g_tower_grads.append(g_opt.compute_gradients(g_loss, var_list=g_params)) d_tower_grads.append(d_opt.compute_gradients(d_loss, var_list=d_params)) # only need one batchnorm update (ends up being updates for last tower) batchnorm_updates = tf.get_collection(tf.GraphKeys.UPDATE_OPS, scope) # summaries summaries(g, x, args) # average and apply gradients g_grads = average_gradients(g_tower_grads) d_grads = average_gradients(d_tower_grads) summarize_gradients(g_grads + d_grads) global_step = tf.train.get_global_step() g_apply_grads = g_opt.apply_gradients(g_grads, global_step=global_step) d_apply_grads = d_opt.apply_gradients(d_grads, global_step=global_step) # training if args.model == 'gan': train_func = _train_gan(g_apply_grads, d_apply_grads, batchnorm_updates) elif args.model == 'wgan': train_func = _train_wgan(g_apply_grads, g_params, d_apply_grads, d_params, batchnorm_updates) elif args.model == 'iwgan': train_func = _train_iwgan(g_apply_grads, d_apply_grads, batchnorm_updates) return train_func
def _inference(self, input_): conv1 = layers.conv2d_same_act(input_, 16, activation_fn=self.activation_fn, name='conv1') skip1 = layers.bottleneck_layer(conv1, 32, name='skip1') _, conv2 = layers.conv2d_same_repeat(conv1, 32, num_repeat=2, activation_fn=self.activation_fn, with_logit=True, name='conv2') res1 = tf.add(skip1, conv2, name='res1') res_act1 = self.res_act(res1) _, conv3 = layers.conv2d_same_repeat(res_act1, 32, num_repeat=2, activation_fn=self.activation_fn, with_logit=True, name='conv3') res2 = tf.add(conv3, res1, name='res2') res_act2 = self.res_act(res2) skip2 = layers.bottleneck_layer(res_act2, 64, d_h=2, d_w=2, name='skip2') conv4 = layers.conv2d_same_act(res_act2, 64, d_h=2, d_w=2, activation_fn=self.activation_fn, name='conv4') conv5 = layers.conv2d_same(conv4, 64, name='conv5') res3 = tf.add(skip2, conv5, name='res3') res_act3 = self.res_act(res3) _, conv6 = layers.conv2d_same_repeat(res_act1, 64, num_repeat=2, activation_fn=self.activation_fn, with_logit=True, name='conv3') res4 = tf.add(res3, conv6, name='res4') res_act4 = self.res_act(res4) skip3 = layers.bottleneck_layer(res_act4, 128, d_h=2, d_w=2, name='skip3') conv7 = layers.conv2d_same_act(res_act4, 128, d_h=2, d_w=2, activation_fn=self.activation_fn, name='conv7') conv8 = layers.conv2d_same(conv7, 128, name='conv8') res5 = tf.add(skip3, conv8, name='res5') res_act5 = self.res_act(res5) _, conv9 = layers.conv2d_same_repeat(res_act5, 128, num_repeat=2, activation_fn=self.activation_fn, with_logit=True, name='conv9') res6 = tf.add(res5, conv9, name='res6') res_act6 = self.res_act(res6) pool = layers.avg_pool(res_act6, k_h=8, k_w=8, d_h=1, d_w=1, name='pool') flat = layers.flatten(pool, 'flat') linear = layers.linear(flat, self.num_class, name='linear') return linear