def began_decoder(opts, noise, is_training=False, reuse=False): output_shape = datashapes[opts['dataset']] num_units = opts['g_num_filters'] num_layers = opts['g_num_layers'] batch_size = tf.shape(noise)[0] h0 = ops.linear(opts, noise, num_units * 8 * 8, scope='h0_lin') h0 = tf.reshape(h0, [-1, 8, 8, num_units]) layer_x = h0 for i in range(num_layers): if i % 3 < 2: # Don't change resolution layer_x = ops.conv2d(opts, layer_x, num_units, d_h=1, d_w=1, scope='h%d_conv' % i) layer_x = tf.nn.elu(layer_x) else: if i != num_layers - 1: # Upsampling by factor of 2 with NN scale = 2 ** (i // 3 + 1) layer_x = ops.upsample_nn(layer_x, [scale * 8, scale * 8], scope='h%d_upsample' % i, reuse=reuse) # Skip connection append = ops.upsample_nn(h0, [scale * 8, scale * 8], scope='h%d_skipup' % i, reuse=reuse) layer_x = tf.concat([layer_x, append], axis=3) last_h = ops.conv2d(opts, layer_x, output_shape[-1], d_h=1, d_w=1, scope='hfinal_conv') if opts['input_normalize_sym']: return tf.nn.tanh(last_h), last_h else: return tf.nn.sigmoid(last_h), last_h
def began_encoder(opts, inputs, is_training=False, reuse=False): num_units = opts['e_num_filters'] assert num_units == opts['g_num_filters'], \ 'BEGAN requires same number of filters in encoder and decoder' num_layers = opts['e_num_layers'] layer_x = ops.conv2d(opts, inputs, num_units, scope='hfirst_conv') for i in range(num_layers): if i % 3 < 2: if i != num_layers - 2: ii = i - (i / 3) scale = (ii + 1 - ii / 2) else: ii = i - (i / 3) scale = (ii - (ii - 1) / 2) layer_x = ops.conv2d(opts, layer_x, num_units * scale, d_h=1, d_w=1, scope='h%d_conv' % i) layer_x = tf.nn.elu(layer_x) else: if i != num_layers - 1: layer_x = ops.downsample(layer_x, scope='h%d_maxpool' % i, reuse=reuse) # Tensor should be [N, 8, 8, filters] at this point if opts['e_noise'] != 'gaussian': res = ops.linear(opts, layer_x, opts['zdim'], scope='hfinal_lin') return res else: mean = ops.linear(opts, layer_x, opts['zdim'], scope='mean_lin') log_sigmas = ops.linear(opts, layer_x, opts['zdim'], scope='log_sigmas_lin') return mean, log_sigmas
def dcgan_encoder(opts, inputs, is_training=False, reuse=False): num_units = opts['e_num_filters'] num_layers = opts['e_num_layers'] layer_x = inputs for i in range(num_layers): scale = 2**(num_layers - i - 1) layer_x = ops.conv2d(opts, layer_x, num_units // scale, scope='h%d_conv' % i) if opts['batch_norm']: layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='h%d_bn' % i) layer_x = tf.nn.relu(layer_x) if opts['e_noise'] != 'gaussian': res = ops.linear(opts, layer_x, opts['zdim'], scope='hfinal_lin') return res else: mean = ops.linear(opts, layer_x, opts['zdim'], scope='mean_lin') log_sigmas = ops.linear(opts, layer_x, opts['zdim'], scope='log_sigmas_lin') return mean, log_sigmas
def ali_decoder(opts, noise, is_training=False, reuse=False): output_shape = datashapes[opts['dataset']] batch_size = tf.shape(noise)[0] noise_size = int(noise.get_shape()[1]) data_height = output_shape[0] data_width = output_shape[1] data_channels = output_shape[2] noise = tf.reshape(noise, [-1, 1, 1, noise_size]) num_units = opts['g_num_filters'] layer_params = [] layer_params.append([4, 1, num_units]) layer_params.append([4, 2, num_units // 2]) layer_params.append([4, 1, num_units // 4]) layer_params.append([4, 2, num_units // 8]) layer_params.append([5, 1, num_units // 8]) # For convolution: (n - k) / stride + 1 = s # For transposed: (s - 1) * stride + k = n layer_x = noise height = 1 width = 1 for i, (kernel, stride, channels) in enumerate(layer_params): height = (height - 1) * stride + kernel width = height layer_x = ops.deconv2d( opts, layer_x, [batch_size, height, width, channels], d_h=stride, d_w=stride, scope='h%d_deconv' % i, conv_filters_dim=kernel, padding='VALID') if opts['batch_norm']: layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='h%d_bn' % i) layer_x = ops.lrelu(layer_x, 0.1) assert height == data_height assert width == data_width # Then two 1x1 convolutions. layer_x = ops.conv2d(opts, layer_x, num_units // 8, d_h=1, d_w=1, scope='conv2d_1x1', conv_filters_dim=1) if opts['batch_norm']: layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='hfinal_bn') layer_x = ops.lrelu(layer_x, 0.1) layer_x = ops.conv2d(opts, layer_x, data_channels, d_h=1, d_w=1, scope='conv2d_1x1_2', conv_filters_dim=1) if opts['input_normalize_sym']: return tf.nn.tanh(layer_x), layer_x else: return tf.nn.sigmoid(layer_x), layer_x
def ali_encoder(opts, inputs, is_training=False, reuse=False): num_units = opts['e_num_filters'] layer_params = [] layer_params.append([5, 1, num_units // 8]) layer_params.append([4, 2, num_units // 4]) layer_params.append([4, 1, num_units // 2]) layer_params.append([4, 2, num_units]) layer_params.append([4, 1, num_units * 2]) # For convolution: (n - k) / stride + 1 = s # For transposed: (s - 1) * stride + k = n layer_x = inputs height = int(layer_x.get_shape()[1]) width = int(layer_x.get_shape()[2]) assert height == width for i, (kernel, stride, channels) in enumerate(layer_params): height = (height - kernel) // stride + 1 width = height layer_x = ops.conv2d( opts, layer_x, channels, d_h=stride, d_w=stride, scope='h%d_conv' % i, conv_filters_dim=kernel, padding='VALID') if opts['batch_norm']: layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='h%d_bn' % i) layer_x = ops.lrelu(layer_x, 0.1) assert height == 1 assert width == 1 # Then two 1x1 convolutions. layer_x = ops.conv2d(opts, layer_x, num_units * 2, d_h=1, d_w=1, scope='conv2d_1x1', conv_filters_dim=1) if opts['batch_norm']: layer_x = ops.batch_norm(opts, layer_x, is_training, reuse, scope='hfinal_bn') layer_x = ops.lrelu(layer_x, 0.1) layer_x = ops.conv2d(opts, layer_x, num_units // 2, d_h=1, d_w=1, scope='conv2d_1x1_2', conv_filters_dim=1) if opts['e_noise'] != 'gaussian': res = ops.linear(opts, layer_x, opts['zdim'], scope='hlast_lin') return res else: mean = ops.linear(opts, layer_x, opts['zdim'], scope='mean_lin') log_sigmas = ops.linear(opts, layer_x, opts['zdim'], scope='log_sigmas_lin') return mean, log_sigmas