def log_pdf(self, sample): log_pdfs_all = None for obs_type in ['flat', 'image']: if sample[obs_type] is not None: split = helper.split_tensor_tf( sample[obs_type], -1, [e['size'][-1] for e in self.sample_properties[obs_type]]) split_batched = [ tf.reshape(e, [-1, *e.get_shape().as_list()[2:]]) for e in split ] log_pdfs = [] for i, e in enumerate(self.dist_list[obs_type]): split_batched_flat = tf.reshape(split_batched[i], [ -1, np.prod(split_batched[i].get_shape().as_list()[1:]) ]) log_pdfs.append( tf.reshape( e.log_pdf(split_batched_flat), [-1, sample[obs_type].get_shape().as_list()[1], 1 ])) if log_pdfs_all is None: log_pdfs_all = log_pdfs else: log_pdfs_all = log_pdfs_all + log_pdfs return helper.list_sum(log_pdfs_all)
def entropy(self): entropies_all = None for obs_type in ['flat', 'image']: if self.params[obs_type] is not None: entropies = [] for i, e in enumerate(self.dist_list[obs_type]): entropies.append(tf.reshape(e.entropy(), [-1, self.params[obs_type][i].get_shape().as_list()[1], 1])) if entropies_all is None: entropies_all = entropies else: entropies_all = entropies_all + entropies return helper.list_sum(entropies_all)
def forward(self, x, noise=None, name=''): with tf.compat.v1.variable_scope("Encoder", reuse=self.constructed): if len(self.config['data_properties']['flat']) > 0: n_output_size = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['flat'] ]) x_batched_inp_flat = tf.reshape( x['flat'], [-1, *x['flat'].get_shape().as_list()[2:]]) lay1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=self.config['n_flat'], activation=self.activation_function) lay2_flat = helper.FCResnetLayer( lay1_flat, units=self.config['n_flat'], activation=self.activation_function) latent_flat = tf.layers.dense(inputs=lay2_flat, units=self.config['n_latent'], activation=None) z_flat = tf.reshape(latent_flat, [ -1, x['flat'].get_shape().as_list()[1], self.config['n_latent'] ]) z = z_flat if len(self.config['data_properties']['image']) > 0: image_shape = ( self.config['data_properties']['image'][0]['size'][-3:-1]) n_image_size = np.prod(image_shape) n_output_channels = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['image'] ]) image_input = tf.reshape( x['image'], [-1, *x['image'].get_shape().as_list()[2:]]) reduce_units = self.config['n_filter'] # # # 28x28xn_channels if image_shape == (28, 28): ## works with 512 lay1_image = tf.layers.conv2d( inputs=image_input, filters=int(self.config['n_filter'] / 8), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay1_image = helper.conv_layer_norm_layer( lay1_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay1_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN1', [0, 1, 2], lay1_image) lay1_image = self.activation_function(lay1_image) lay2_image = tf.layers.conv2d( inputs=lay1_image, filters=int(self.config['n_filter'] / 8), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay2_image = helper.conv_layer_norm_layer( lay2_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay2_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN2', [0, 1, 2], lay2_image) lay2_image = self.activation_function(lay2_image) lay3_image = tf.layers.conv2d( inputs=lay2_image, filters=int(self.config['n_filter'] / 4), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay3_image = helper.conv_layer_norm_layer( lay3_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay3_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN3', [0, 1, 2], lay3_image) lay3_image = self.activation_function(lay3_image) lay4_image = tf.layers.conv2d( inputs=lay3_image, filters=int(self.config['n_filter'] / 4), kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay4_image = helper.conv_layer_norm_layer( lay4_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay4_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN4', [0, 1, 2], lay4_image) lay4_image = self.activation_function(lay4_image) latent_image = tf.layers.conv2d( inputs=lay4_image, filters=self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=self.activation_function) latent_image_flat = tf.reshape( latent_image, [-1, np.prod(latent_image.get_shape().as_list()[1:])]) # # # 32x32xn_channels 'n_filter': 512, 28 sec, 3.4 in 20 epochs if image_shape == (32, 32): ## works with 512 lay1_image = tf.layers.conv2d( inputs=image_input, filters=int(self.config['n_filter'] / 4), kernel_size=[4, 4], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay1_image = helper.conv_layer_norm_layer( lay1_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay1_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN1', [0, 1, 2], lay1_image) lay1_image = self.activation_function(lay1_image) lay2_image = tf.layers.conv2d( inputs=lay1_image, filters=int(self.config['n_filter'] / 2), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay2_image = helper.conv_layer_norm_layer( lay2_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay2_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN2', [0, 1, 2], lay2_image) lay2_image = self.activation_function(lay2_image) lay3_image = tf.layers.conv2d( inputs=lay2_image, filters=int(self.config['n_filter'] / 2), kernel_size=[3, 3], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay3_image = helper.conv_layer_norm_layer( lay3_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay3_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN3', [0, 1, 2], lay3_image) lay3_image = self.activation_function(lay3_image) latent_image = tf.layers.conv2d( inputs=lay3_image, filters=self.config['n_filter'], kernel_size=[3, 3], strides=[1, 1], padding="valid", use_bias=True, activation=self.activation_function) latent_image_flat = tf.reshape( latent_image, [-1, np.prod(latent_image.get_shape().as_list()[1:])]) # # # 64x64xn_channels if image_shape == (64, 64): lay1_image = tf.layers.conv2d( inputs=image_input, filters=int(self.config['n_filter'] / 32), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay1_image = helper.conv_layer_norm_layer( lay1_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay1_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN1', [0, 1, 2], lay1_image) lay1_image = self.activation_function(lay1_image) lay2_image = tf.layers.conv2d( inputs=lay1_image, filters=int(self.config['n_filter'] / 16), kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay2_image = helper.conv_layer_norm_layer( lay2_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay2_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN2', [0, 1, 2], lay2_image) lay2_image = self.activation_function(lay2_image) lay3_image = tf.layers.conv2d( inputs=lay2_image, filters=int(self.config['n_filter'] / 16), kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay3_image = helper.conv_layer_norm_layer( lay3_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay3_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN3', [0, 1, 2], lay3_image) lay3_image = self.activation_function(lay3_image) lay4_image = tf.layers.conv2d( inputs=lay3_image, filters=int(self.config['n_filter'] / 4), kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay4_image = helper.conv_layer_norm_layer( lay4_image, channel_index=3) elif self.normalization_mode == 'Batch Norm': lay4_image = lib.ops.batchnorm.Batchnorm( 'Encoder.BN4', [0, 1, 2], lay4_image) lay4_image = self.activation_function(lay4_image) latent_image = tf.layers.conv2d( inputs=lay4_image, filters=self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=self.activation_function) latent_image_flat = tf.reshape( latent_image, [-1, np.prod(latent_image.get_shape().as_list()[1:])]) if self.config['encoder_mode'] == 'Deterministic': latent_flat = tf.layers.dense( inputs=latent_image_flat, units=self.config['n_latent'], use_bias=True, activation=None) if self.config['encoder_mode'] == 'Gaussian': latent_mu = tf.layers.dense(inputs=latent_image_flat, units=self.config['n_latent'], use_bias=True, activation=None) latent_pre_scale = tf.layers.dense( inputs=latent_image_flat, units=self.config['n_latent'], use_bias=True, activation=None) latent_flat = latent_mu + tf.nn.softplus( latent_pre_scale) * noise z_flat = tf.reshape(latent_flat, [ -1, x['image'].get_shape().as_list()[1], self.config['n_latent'] ]) self.constructed = True return z_flat
def forward(self, x, name=''): with tf.compat.v1.variable_scope("Generator", reuse=self.constructed): out_dict = {'flat': None, 'image': None} if len(self.config['data_properties']['flat']) > 0: n_output_size = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['flat'] ]) x_batched_inp_flat = tf.reshape( x, [-1, x.get_shape().as_list()[-1]]) lay1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=self.config['n_flat'], activation=self.activation_function) lay2_flat = tf.layers.dense( inputs=lay1_flat, units=self.config['n_flat'], activation=self.activation_function) lay3_flat = tf.layers.dense( inputs=lay2_flat, units=self.config['n_flat'], activation=self.activation_function) flat_param = tf.layers.dense(inputs=lay3_flat, units=n_output_size, activation=None) out_dict['flat'] = tf.reshape( flat_param, [-1, x.get_shape().as_list()[1], n_output_size]) if len(self.config['data_properties']['image']) > 0: image_shape = ( self.config['data_properties']['image'][0]['size'][-3:-1]) n_image_size = np.prod(image_shape) n_output_channels = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['image'] ]) x_batched_inp_flat = tf.reshape( x, [-1, x.get_shape().as_list()[-1]]) # list_of_results = [] # for i # batch x 2 = tf.reduce_sum(self.weight_matrix[:i,:]*h) # list_of_results.append(batch x 2) # tf.concat(list_of_results, axis=-1) batch x D # out_dict['image'] = tf.reshape(batch x D, batch x 1 x 28 x 28 x 2*3) if image_shape == (28, 28): layer_1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=8 * 8 * self.config['n_filter'], activation=self.activation_function, use_bias=True) layer_1 = tf.reshape(layer_1_flat, [-1, 8, 8, self.config['n_filter']]) layer_2 = tf.layers.conv2d_transpose( inputs=layer_1, filters=int(self.config['n_filter'] / 4), kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_2 = helper.conv_layer_norm_layer(layer_2, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_2 = lib.ops.batchnorm.Batchnorm( 'Generator.BN2', [0, 1, 2], layer_2) layer_2 = self.activation_function(layer_2) layer_3 = tf.layers.conv2d_transpose( inputs=layer_2, filters=int(self.config['n_filter'] / 4), kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_3 = helper.conv_layer_norm_layer(layer_3, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_3 = lib.ops.batchnorm.Batchnorm( 'Generator.BN3', [0, 1, 2], layer_3) layer_3 = self.activation_function(layer_3) layer_4 = tf.layers.conv2d_transpose( inputs=layer_3, filters=int(self.config['n_filter'] / 8), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_4 = helper.conv_layer_norm_layer(layer_4, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_4 = lib.ops.batchnorm.Batchnorm( 'Generator.BN4', [0, 1, 2], layer_4) layer_4 = self.activation_function(layer_4) layer_5 = tf.layers.conv2d_transpose( inputs=layer_4, filters=int(self.config['n_filter'] / 8), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_5 = helper.conv_layer_norm_layer(layer_5, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_5 = lib.ops.batchnorm.Batchnorm( 'Generator.BN5', [0, 1, 2], layer_5) layer_5 = self.activation_function(layer_5) output = tf.layers.conv2d_transpose( inputs=layer_5, filters=n_output_channels, kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) image_param = helper.tf_center_crop_image( output, resize_ratios=[28, 28]) # # # 32x32xn_channels 'n_filter': 512, 28 sec, 3.4 in 20 epochs if image_shape == (32, 32): ## works with 128 ? layer_1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=8 * 8 * self.config['n_filter'], activation=self.activation_function, use_bias=True) layer_1 = tf.reshape(layer_1_flat, [-1, 8, 8, self.config['n_filter']]) layer_2 = tf.layers.conv2d_transpose( inputs=layer_1, filters=int(self.config['n_filter'] / 2), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_2 = helper.conv_layer_norm_layer(layer_2, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_2 = lib.ops.batchnorm.Batchnorm( 'Generator.BN2', [0, 1, 2], layer_2) layer_2 = self.activation_function(layer_2) layer_3 = tf.layers.conv2d_transpose( inputs=layer_2, filters=int(self.config['n_filter'] / 4), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_3 = helper.conv_layer_norm_layer(layer_3, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_3 = lib.ops.batchnorm.Batchnorm( 'Generator.BN3', [0, 1, 2], layer_3) layer_3 = self.activation_function(layer_3) layer_4 = tf.layers.conv2d_transpose( inputs=layer_3, filters=int(self.config['n_filter'] / 4), kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_4 = helper.conv_layer_norm_layer(layer_4, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_4 = lib.ops.batchnorm.Batchnorm( 'Generator.BN4', [0, 1, 2], layer_4) layer_4 = self.activation_function(layer_4) output = tf.layers.conv2d_transpose( inputs=layer_4, filters=n_output_channels, kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) image_param = helper.tf_center_crop_image( output, resize_ratios=[32, 32]) # # # 64x64xn_channels if image_shape == (64, 64): layer_1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=11 * 11 * self.config['n_filter'], activation=self.activation_function, use_bias=True) layer_1 = tf.reshape(layer_1_flat, [-1, 11, 11, self.config['n_filter']]) layer_2 = tf.layers.conv2d_transpose( inputs=layer_1, filters=int(self.config['n_filter'] / 4), kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_2 = helper.conv_layer_norm_layer(layer_2, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_2 = lib.ops.batchnorm.Batchnorm( 'Generator.BN2', [0, 1, 2], layer_2) layer_2 = self.activation_function(layer_2) layer_3 = tf.layers.conv2d_transpose( inputs=layer_2, filters=int(self.config['n_filter'] / 16), kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_3 = helper.conv_layer_norm_layer(layer_3, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_3 = lib.ops.batchnorm.Batchnorm( 'Generator.BN2', [0, 1, 2], layer_3) layer_3 = self.activation_function(layer_3) layer_4 = tf.layers.conv2d_transpose( inputs=layer_3, filters=int(self.config['n_filter'] / 16), kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_4 = helper.conv_layer_norm_layer(layer_4, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_4 = lib.ops.batchnorm.Batchnorm( 'Generator.BN3', [0, 1, 2], layer_4) layer_4 = self.activation_function(layer_4) layer_5 = tf.layers.conv2d_transpose( inputs=layer_4, filters=int(self.config['n_filter'] / 32), kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': layer_5 = helper.conv_layer_norm_layer(layer_5, channel_index=3) elif self.normalization_mode == 'Batch Norm': layer_5 = lib.ops.batchnorm.Batchnorm( 'Generator.BN5', [0, 1, 2], layer_5) layer_5 = self.activation_function(layer_5) output = tf.layers.conv2d_transpose( inputs=layer_5, filters=n_output_channels, kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) image_param = output out_dict['image'] = tf.reshape(image_param, [ -1, x.get_shape().as_list()[1], *image_shape, n_output_channels ]) self.constructed = True return out_dict
def forward(self, x, name=''): with tf.variable_scope("Generator", reuse=self.constructed): out_dict = {'flat': None, 'image': None} if len(self.config['data_properties']['flat']) > 0: n_output_size = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['flat'] ]) x_batched_inp_flat = tf.reshape( x, [-1, x.get_shape().as_list()[-1]]) lay1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=self.config['n_flat'], activation=self.activation_function) lay2_flat = tf.layers.dense( inputs=lay1_flat, units=self.config['n_flat'], activation=self.activation_function) lay3_flat = tf.layers.dense( inputs=lay2_flat, units=self.config['n_flat'], activation=self.activation_function) flat_param = tf.layers.dense(inputs=lay3_flat, units=n_output_size, activation=None) out_dict['flat'] = tf.reshape( flat_param, [-1, x.get_shape().as_list()[1], n_output_size]) if len(self.config['data_properties']['image']) > 0: image_shape = ( self.config['data_properties']['image'][0]['size'][-3:-1]) n_image_size = np.prod(image_shape) n_output_channels = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['image'] ]) x_batched_inp_flat = tf.reshape( x, [-1, x.get_shape().as_list()[-1]]) noise = x_batched_inp_flat n_latents = self.config['n_latent'] DIM = self.config['n_filter'] output = lib.ops.linear.Linear('Generator.Input', n_latents, 4 * 4 * 4 * DIM, noise) output = lib.ops.batchnorm.Batchnorm('Generator.BN1', [0], output) output = tf.nn.relu(output) output = tf.reshape(output, [-1, 4 * DIM, 4, 4]) output = lib.ops.deconv2d.Deconv2D('Generator.2', 4 * DIM, 2 * DIM, 5, output) output = lib.ops.batchnorm.Batchnorm('Generator.BN2', [0, 2, 3], output) output = tf.nn.relu(output) output = lib.ops.deconv2d.Deconv2D('Generator.3', 2 * DIM, DIM, 5, output) output = lib.ops.batchnorm.Batchnorm('Generator.BN3', [0, 2, 3], output) output = tf.nn.relu(output) output = lib.ops.deconv2d.Deconv2D('Generator.5', DIM, 3, 5, output) output = tf.nn.sigmoid(output) image_param = tf.transpose(output, perm=[0, 2, 3, 1]) out_dict['image'] = tf.reshape(image_param, [ -1, x.get_shape().as_list()[1], *image_shape, n_output_channels ]) self.constructed = True return out_dict
def forward(self, x, noise=None, name=''): with tf.variable_scope("Encoder", reuse=self.constructed): if len(self.config['data_properties']['flat']) > 0: n_output_size = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['flat'] ]) x_batched_inp_flat = tf.reshape( x['flat'], [-1, *x['flat'].get_shape().as_list()[2:]]) lay1_flat = tf.layers.dense( inputs=x_batched_inp_flat, units=self.config['n_flat'], activation=self.activation_function) lay2_flat = tf.layers.dense( inputs=lay1_flat, units=self.config['n_flat'], activation=self.activation_function) latent_flat = tf.layers.dense(inputs=lay2_flat, units=self.n_output, activation=None) z_flat = tf.reshape( latent_flat, [-1, x['flat'].get_shape().as_list()[1], self.n_output]) z = z_flat if len(self.config['data_properties']['image']) > 0: image_shape = ( self.config['data_properties']['image'][0]['size'][-3:-1]) n_image_size = np.prod(image_shape) n_output_channels = helper.list_sum([ distributions.DistributionsAKA[e['dist']].num_params( e['size'][-1]) for e in self.config['data_properties']['image'] ]) x_batched_inp_image = tf.reshape( x['image'], [-1, *x['image'].get_shape().as_list()[2:]]) if self.config[ 'encoder_mode'] == 'Deterministic' or self.config[ 'encoder_mode'] == 'Gaussian' or self.config[ 'encoder_mode'] == 'UnivApproxNoSpatial': image_input = x_batched_inp_image if self.config['encoder_mode'] == 'UnivApprox' or self.config[ 'encoder_mode'] == 'UnivApproxSine': noise_spatial = tf.tile( noise[:, np.newaxis, np.newaxis, :], [ 1, *x_batched_inp_image.get_shape().as_list()[1:3], 1 ]) x_and_noise_image = tf.concat( [x_batched_inp_image, noise_spatial], axis=-1) image_input = x_and_noise_image # # 28x28xn_channels if image_shape == (28, 28): lay1_image = tf.layers.conv2d( inputs=image_input, filters=self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay2_image = self.activation_function( helper.conv_layer_norm_layer(lay1_image)) elif self.normalization_mode == 'Batch Norm': lay2_image = self.activation_function( helper.batch_norm()(lay1_image)) else: lay2_image = self.activation_function(lay1_image) lay3_image = tf.layers.conv2d(inputs=lay2_image, filters=1 * self.config['n_filter'], kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay4_image = self.activation_function( helper.conv_layer_norm_layer(lay3_image)) elif self.normalization_mode == 'Batch Norm': lay4_image = self.activation_function( helper.batch_norm()(lay3_image)) else: lay4_image = self.activation_function(lay3_image) lay5_image = tf.layers.conv2d(inputs=lay4_image, filters=2 * self.config['n_filter'], kernel_size=[4, 4], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay6_image = self.activation_function( helper.conv_layer_norm_layer(lay5_image)) elif self.normalization_mode == 'Batch Norm': lay6_image = self.activation_function( helper.batch_norm()(lay5_image)) else: lay6_image = self.activation_function(lay5_image) latent_image = tf.layers.conv2d( inputs=lay6_image, filters=2 * self.config['n_filter'], kernel_size=[3, 3], strides=[1, 1], padding="valid", use_bias=True, activation=self.activation_function) latent_image_flat = tf.reshape( latent_image, [-1, np.prod(latent_image.get_shape().as_list()[1:])]) # # 32x32xn_channels if image_shape == (32, 32): lay1_image = tf.layers.conv2d( inputs=image_input, filters=self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay2_image = self.activation_function( helper.conv_layer_norm_layer(lay1_image)) elif self.normalization_mode == 'Batch Norm': lay2_image = self.activation_function( helper.batch_norm()(lay1_image)) else: lay2_image = self.activation_function(lay1_image) lay3_image = tf.layers.conv2d(inputs=lay2_image, filters=1 * self.config['n_filter'], kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay4_image = self.activation_function( helper.conv_layer_norm_layer(lay3_image)) elif self.normalization_mode == 'Batch Norm': lay4_image = self.activation_function( helper.batch_norm()(lay3_image)) else: lay4_image = self.activation_function(lay3_image) lay5_image = tf.layers.conv2d(inputs=lay4_image, filters=2 * self.config['n_filter'], kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay6_image = self.activation_function( helper.conv_layer_norm_layer(lay5_image)) elif self.normalization_mode == 'Batch Norm': lay6_image = self.activation_function( helper.batch_norm()(lay5_image)) else: lay6_image = self.activation_function(lay5_image) latent_image = tf.layers.conv2d( inputs=lay6_image, filters=2 * self.config['n_filter'], kernel_size=[3, 3], strides=[1, 1], padding="valid", use_bias=True, activation=self.activation_function) latent_image_flat = tf.reshape( latent_image, [-1, np.prod(latent_image.get_shape().as_list()[1:])]) # 64x64xn_channels if image_shape == (64, 64): lay1_image = tf.layers.conv2d( inputs=image_input, filters=self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay2_image = self.activation_function( helper.conv_layer_norm_layer(lay1_image)) elif self.normalization_mode == 'Batch Norm': lay2_image = self.activation_function( helper.batch_norm()(lay1_image)) else: lay2_image = self.activation_function(lay1_image) lay3_image = tf.layers.conv2d( inputs=lay2_image, filters=self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay4_image = self.activation_function( helper.conv_layer_norm_layer(lay3_image)) elif self.normalization_mode == 'Batch Norm': lay4_image = self.activation_function( helper.batch_norm()(lay3_image)) else: lay4_image = self.activation_function(lay3_image) lay5_image = tf.layers.conv2d(inputs=lay4_image, filters=2 * self.config['n_filter'], kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay6_image = self.activation_function( helper.conv_layer_norm_layer(lay5_image)) elif self.normalization_mode == 'Batch Norm': lay6_image = self.activation_function( helper.batch_norm()(lay5_image)) else: lay6_image = self.activation_function(lay5_image) lay7_image = tf.layers.conv2d(inputs=lay6_image, filters=3 * self.config['n_filter'], kernel_size=[5, 5], strides=[1, 1], padding="valid", use_bias=True, activation=None) if self.normalization_mode == 'Layer Norm': lay8_image = self.activation_function( helper.conv_layer_norm_layer(lay7_image)) elif self.normalization_mode == 'Batch Norm': lay8_image = self.activation_function( helper.batch_norm()(lay7_image)) else: lay8_image = self.activation_function(lay7_image) latent_image = tf.layers.conv2d( inputs=lay8_image, filters=3 * self.config['n_filter'], kernel_size=[3, 3], strides=[1, 1], padding="valid", use_bias=True, activation=self.activation_function) latent_image_flat = tf.reshape( latent_image, [-1, np.prod(latent_image.get_shape().as_list()[1:])]) if self.config['encoder_mode'] == 'Deterministic': lay1_flat = tf.layers.dense( inputs=latent_image_flat, units=self.config['n_flat'], use_bias=True, activation=self.activation_function) latent_flat = tf.layers.dense(inputs=lay1_flat, units=self.n_output, use_bias=True, activation=None) if self.config['encoder_mode'] == 'Gaussian': lay1_flat = tf.layers.dense( inputs=latent_image_flat, units=self.config['n_flat'], use_bias=True, activation=self.activation_function) latent_mu = tf.layers.dense(inputs=lay1_flat, units=self.n_output, use_bias=True, activation=None) latent_log_sig = tf.layers.dense(inputs=lay1_flat, units=self.n_output, use_bias=True, activation=None) latent_flat = latent_mu + tf.nn.softplus( latent_log_sig) * noise if self.config['encoder_mode'] == 'UnivApprox' or self.config[ 'encoder_mode'] == 'UnivApproxNoSpatial': lay1_concat = tf.layers.dense( inputs=tf.concat([latent_image_flat, noise], axis=-1), units=self.config['n_flat'], use_bias=True, activation=self.activation_function) latent_flat = tf.layers.dense(inputs=lay1_concat, units=self.n_output, use_bias=True, activation=None) if self.config['encoder_mode'] == 'UnivApproxSine': lay1_concat = tf.layers.dense( inputs=tf.concat([latent_image_flat, noise], axis=-1), units=self.config['n_flat'], use_bias=True, activation=self.activation_function) latent_correction = tf.layers.dense(inputs=lay1_concat, units=self.n_output, use_bias=True, activation=None) latent_output = tf.layers.dense(inputs=lay1_concat, units=self.n_output, use_bias=True, activation=None) latent_flat = latent_output + tf.sin( self.config['enc_sine_freq'] * noise) - latent_correction z_flat = tf.reshape( latent_flat, [-1, x['image'].get_shape().as_list()[1], self.n_output]) self.constructed = True return z_flat
def forward(self, x, name = ''): with tf.variable_scope("Generator", reuse=self.constructed): out_dict = {'flat': None, 'image': None} if len(self.config['data_properties']['flat']) > 0: n_output_size = helper.list_sum([distributions.DistributionsAKA[e['dist']].num_params(e['size'][-1]) for e in self.config['data_properties']['flat']]) x_batched_inp_flat = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) lay1_flat = tf.layers.dense(inputs = x_batched_inp_flat, units = self.config['n_flat'], activation = self.activation_function) lay2_flat = tf.layers.dense(inputs = lay1_flat, units = self.config['n_flat'], activation = self.activation_function) lay3_flat = tf.layers.dense(inputs = lay2_flat, units = self.config['n_flat'], activation = self.activation_function) flat_param = tf.layers.dense(inputs = lay3_flat, units = n_output_size, activation = None) out_dict['flat'] = tf.reshape(flat_param, [-1, x.get_shape().as_list()[1], n_output_size]) if len(self.config['data_properties']['image']) > 0: image_shape = (self.config['data_properties']['image'][0]['size'][-3:-1]) n_image_size = np.prod(image_shape) n_output_channels = helper.list_sum([distributions.DistributionsAKA[e['dist']].num_params(e['size'][-1]) for e in self.config['data_properties']['image']]) x_batched_inp_flat = tf.reshape(x, [-1, x.get_shape().as_list()[-1]]) # # 28x28xn_channels if image_shape == (28, 28): print('CHECK ME I AM FULLY CONNECTED~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') lay1_image = tf.layers.dense(inputs = x_batched_inp_flat, units = self.config['n_flat'], activation = self.activation_function) lay2_image = tf.layers.dense(inputs = lay1_image, units = self.config['n_flat'], activation = self.activation_function) lay3_image = tf.layers.dense(inputs = lay2_image, units = n_image_size*n_output_channels, activation = self.activation_function) lay4_image = tf.nn.sigmoid(lay3_image) image_param = tf.reshape(lay4_image, [-1, *image_shape, n_output_channels]) # lay1_image = tf.layers.dense(inputs = x_batched_inp_flat, units = 8*self.config['n_filter']*4*4, activation = None) # lay2_image = tf.reshape(lay1_image, [-1, 4, 4, 8*self.config['n_filter']]) # if self.normalization_mode == 'Layer Norm': # lay3_image = self.activation_function(helper.conv_layer_norm_layer(lay2_image)) # elif self.normalization_mode == 'Batch Norm': # lay3_image = self.activation_function(helper.batch_norm()(lay2_image)) # else: lay3_image = self.activation_function(lay2_image) #h0 # lay4_image = tf.layers.conv2d_transpose(inputs=lay3_image, filters=4*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) # lay5_image = helper.tf_center_crop_image(lay4_image, resize_ratios=[8,8]) # if self.normalization_mode == 'Layer Norm': # lay6_image = self.activation_function(helper.conv_layer_norm_layer(lay5_image)) # elif self.normalization_mode == 'Batch Norm': # lay6_image = self.activation_function(helper.batch_norm()(lay5_image)) # else: lay6_image = self.activation_function(lay5_image) #h1 # lay7_image = tf.layers.conv2d_transpose(inputs=lay6_image, filters=2*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) # lay8_image = helper.tf_center_crop_image(lay7_image, resize_ratios=[16,16]) # if self.normalization_mode == 'Layer Norm': # lay9_image = self.activation_function(helper.conv_layer_norm_layer(lay8_image)) # elif self.normalization_mode == 'Batch Norm': # lay9_image = self.activation_function(helper.batch_norm()(lay8_image)) # else: lay9_image = self.activation_function(lay8_image) #h2 # lay10_image = tf.layers.conv2d_transpose(inputs=lay9_image, filters=1*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) # lay11_image = helper.tf_center_crop_image(lay10_image, resize_ratios=[28,28]) # if self.normalization_mode == 'Layer Norm': # lay12_image = self.activation_function(helper.conv_layer_norm_layer(lay11_image)) # elif self.normalization_mode == 'Batch Norm': # lay12_image = self.activation_function(helper.batch_norm()(lay11_image)) # else: lay12_image = self.activation_function(lay11_image) #h3 # lay13_image = tf.layers.conv2d_transpose(inputs=lay12_image, filters=n_output_channels, kernel_size=[3, 3], strides=[1, 1], activation=tf.nn.sigmoid) # image_param = helper.tf_center_crop_image(lay13_image, resize_ratios=[28,28]) # # 32x32xn_channels if image_shape == (32, 32): lay1_image = tf.layers.dense(inputs = x_batched_inp_flat, units = 8*self.config['n_filter']*4*4, activation = None) lay2_image = tf.reshape(lay1_image, [-1, 4, 4, 8*self.config['n_filter']]) if self.normalization_mode == 'Layer Norm': lay3_image = self.activation_function(helper.conv_layer_norm_layer(lay2_image)) elif self.normalization_mode == 'Batch Norm': lay3_image = self.activation_function(helper.batch_norm()(lay2_image)) else: lay3_image = self.activation_function(lay2_image) #h0 lay4_image = tf.layers.conv2d_transpose(inputs=lay3_image, filters=4*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay5_image = helper.tf_center_crop_image(lay4_image, resize_ratios=[8,8]) if self.normalization_mode == 'Layer Norm': lay6_image = self.activation_function(helper.conv_layer_norm_layer(lay5_image)) elif self.normalization_mode == 'Batch Norm': lay6_image = self.activation_function(helper.batch_norm()(lay5_image)) else: lay6_image = self.activation_function(lay5_image) #h1 lay7_image = tf.layers.conv2d_transpose(inputs=lay6_image, filters=2*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay8_image = helper.tf_center_crop_image(lay7_image, resize_ratios=[16,16]) if self.normalization_mode == 'Layer Norm': lay9_image = self.activation_function(helper.conv_layer_norm_layer(lay8_image)) elif self.normalization_mode == 'Batch Norm': lay9_image = self.activation_function(helper.batch_norm()(lay8_image)) else: lay9_image = self.activation_function(lay8_image) #h2 lay10_image = tf.layers.conv2d_transpose(inputs=lay9_image, filters=1*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay11_image = helper.tf_center_crop_image(lay10_image, resize_ratios=[32,32]) if self.normalization_mode == 'Layer Norm': lay12_image = self.activation_function(helper.conv_layer_norm_layer(lay11_image)) elif self.normalization_mode == 'Batch Norm': lay12_image = self.activation_function(helper.batch_norm()(lay11_image)) else: lay12_image = self.activation_function(lay11_image) #h3 lay13_image = tf.layers.conv2d_transpose(inputs=lay12_image, filters=n_output_channels, kernel_size=[3, 3], strides=[1, 1], activation=tf.nn.sigmoid) image_param = helper.tf_center_crop_image(lay13_image, resize_ratios=[32,32]) # 64x64xn_channels if image_shape == (64, 64): lay1_image = tf.layers.dense(inputs = x_batched_inp_flat, units = 8*self.config['n_filter']*4*4, activation = None) lay2_image = tf.reshape(lay1_image, [-1, 4, 4, 8*self.config['n_filter']]) if self.normalization_mode == 'Layer Norm': lay3_image = self.activation_function(helper.conv_layer_norm_layer(lay2_image)) elif self.normalization_mode == 'Batch Norm': lay3_image = self.activation_function(helper.batch_norm()(lay2_image)) else: lay3_image = self.activation_function(lay2_image) #h0 lay4_image = tf.layers.conv2d_transpose(inputs=lay3_image, filters=4*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay5_image = helper.tf_center_crop_image(lay4_image, resize_ratios=[8,8]) if self.normalization_mode == 'Layer Norm': lay6_image = self.activation_function(helper.conv_layer_norm_layer(lay5_image)) elif self.normalization_mode == 'Batch Norm': lay6_image = self.activation_function(helper.batch_norm()(lay5_image)) else: lay6_image = self.activation_function(lay5_image) #h1 lay7_image = tf.layers.conv2d_transpose(inputs=lay6_image, filters=2*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay8_image = helper.tf_center_crop_image(lay7_image, resize_ratios=[16,16]) if self.normalization_mode == 'Layer Norm': lay9_image = self.activation_function(helper.conv_layer_norm_layer(lay8_image)) elif self.normalization_mode == 'Batch Norm': lay9_image = self.activation_function(helper.batch_norm()(lay8_image)) else: lay9_image = self.activation_function(lay8_image) #h2 lay10_image = tf.layers.conv2d_transpose(inputs=lay9_image, filters=1*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay11_image = helper.tf_center_crop_image(lay10_image, resize_ratios=[32,32]) if self.normalization_mode == 'Layer Norm': lay12_image = self.activation_function(helper.conv_layer_norm_layer(lay11_image)) elif self.normalization_mode == 'Batch Norm': lay12_image = self.activation_function(helper.batch_norm()(lay11_image)) else: lay12_image = self.activation_function(lay11_image) #h3 lay13_image = tf.layers.conv2d_transpose(inputs=lay12_image, filters=n_output_channels, kernel_size=[5, 5], strides=[2, 2], activation=tf.nn.sigmoid) image_param = helper.tf_center_crop_image(lay13_image, resize_ratios=[64,64]) # 128x128xn_channels if image_shape == (128, 128): lay1_image = tf.layers.dense(inputs = x_batched_inp_flat, units = 8*self.config['n_filter']*4*4, activation = None) lay2_image = tf.reshape(lay1_image, [-1, 4, 4, 8*self.config['n_filter']]) if self.normalization_mode == 'Layer Norm': lay3_image = self.activation_function(helper.conv_layer_norm_layer(lay2_image)) elif self.normalization_mode == 'Batch Norm': lay3_image = self.activation_function(helper.batch_norm()(lay2_image)) else: lay3_image = self.activation_function(lay2_image) #h0 lay4_image = tf.layers.conv2d_transpose(inputs=lay3_image, filters=4*self.config['n_filter'], kernel_size=[5, 5], strides=[1, 1], activation=None) if self.normalization_mode == 'Layer Norm': lay5_image = self.activation_function(helper.conv_layer_norm_layer(lay4_image)) elif self.normalization_mode == 'Batch Norm': lay5_image = self.activation_function(helper.batch_norm()(lay4_image)) else: lay5_image = self.activation_function(lay4_image) #h1 lay6_image = tf.layers.conv2d_transpose(inputs=lay5_image, filters=2*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay7_image = helper.tf_center_crop_image(lay6_image, resize_ratios=[16,16]) if self.normalization_mode == 'Layer Norm': lay8_image = self.activation_function(helper.conv_layer_norm_layer(lay7_image)) elif self.normalization_mode == 'Batch Norm': lay8_image = self.activation_function(helper.batch_norm()(lay7_image)) else: lay8_image = self.activation_function(lay7_image) #h2 lay9_image = tf.layers.conv2d_transpose(inputs=lay8_image, filters=2*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay10_image = helper.tf_center_crop_image(lay9_image, resize_ratios=[32,32]) if self.normalization_mode == 'Layer Norm': lay11_image = self.activation_function(helper.conv_layer_norm_layer(lay10_image)) elif self.normalization_mode == 'Batch Norm': lay11_image = self.activation_function(helper.batch_norm()(lay10_image)) else: lay11_image = self.activation_function(lay10_image) #h3 lay12_image = tf.layers.conv2d_transpose(inputs=lay11_image, filters=1*self.config['n_filter'], kernel_size=[5, 5], strides=[2, 2], activation=None) lay13_image = helper.tf_center_crop_image(lay12_image, resize_ratios=[64,64]) if self.normalization_mode == 'Layer Norm': lay14_image = self.activation_function(helper.conv_layer_norm_layer(lay13_image)) elif self.normalization_mode == 'Batch Norm': lay14_image = self.activation_function(helper.batch_norm()(lay13_image)) else: lay14_image = self.activation_function(lay13_image) #h3 lay15_image = tf.layers.conv2d_transpose(inputs=lay14_image, filters=n_output_channels, kernel_size=[5, 5], strides=[2, 2], activation=tf.nn.sigmoid) image_param = helper.tf_center_crop_image(lay15_image, resize_ratios=[128,128]) out_dict['image'] = tf.reshape(image_param, [-1, x.get_shape().as_list()[1], *image_shape, n_output_channels]) self.constructed = True return out_dict