def __call__(self, input_x, input_y): with tf.variable_scope(self.name, reuse=self.reuse): x = conv_layer(input_x, filter=2 * self.filters, kernel=[7, 7], stride=2, layer_name='conv0') x = Max_Pooling(x, pool_size=[3, 3], stride=2) for i in range(self.nb_blocks): x = self.dense_block(input_x=x, nb_layers=4, layer_name='dense_' + str(i)) x = self.transition_layer(x, scope='trans_' + str(i)) x = self.dense_block(input_x=x, nb_layers=32, layer_name='dense_final') x = Batch_Normalization(x, training=self.training, scope='linear_batch') x = Relu(x) x = Global_Average_Pooling(x) x = flatten(x) logits = Linear(x, self.class_num) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=input_y, logits=logits)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) count_parameters(self.variables, name="classifier_parameter_num") return cost, accuracy
def __call__(self, inputs, input_y, classes, training): # with tf.variable_scope('resnet_v1_50', reuse = self.reuse): if inputs.get_shape()[3] < 3: inputs_three_channel = tf.concat([inputs, inputs, inputs], axis=3) else: inputs_three_channel = inputs with slim.arg_scope(resnet_v1.resnet_arg_scope(weight_decay=0.00001)): logits, _ = resnet_v1_50(inputs_three_channel, num_classes=classes, is_training=True) logits = tf.squeeze(logits) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=input_y, logits=logits)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # feature = tf.squeeze(feature, axis=[1, 2]) # logits = slim.fully_connected(feature, num_outputs=classes, activation_fn=None, scope='Predict') # cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=input_y, logits=logits)) # correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) self.variables = tf.contrib.framework.get_variables( "resnet_v1_50/logits") + tf.contrib.framework.get_variables( "resnet_v1_50/AuxLogits") # self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, self.name) count_parameters(self.variables, name="classifier_parameter_num") return cost, accuracy
def __call__(self, input_tensor_batch, input_y, n, classes, training): layers = [] filter_depth = 16 # filters = [16, 16, 32, 64] with tf.variable_scope(self.name, reuse=self.reuse): # [-1,w,h,3] with tf.variable_scope('conv0', reuse=self.reuse): if input_tensor_batch.get_shape()[3] == 3: conv0 = conv_bn_relu_layer(input_tensor_batch, [3, 3, 3, filter_depth], 1, training) else: conv0 = conv_bn_relu_layer(input_tensor_batch, [3, 3, 1, filter_depth], 1, training) activation_summary(conv0) layers.append(conv0) # [-1,w/2,h/2,features=filter_depth] for i in range(n): with tf.variable_scope('conv1_%d' % i, reuse=self.reuse): if i == 0: conv1 = residual_block(layers[-1], filter_depth, training, first_block=True) else: conv1 = residual_block(layers[-1], filter_depth, training) activation_summary(conv1) layers.append(conv1) # [-1,w/2,h/2,features=filter_depth] for i in range(n): with tf.variable_scope('conv2_%d' % i, reuse=self.reuse): conv2 = residual_block(layers[-1], filter_depth * 2, training) activation_summary(conv2) layers.append(conv2) # [-1,w/4,h/4,features=filter_depth*2] for i in range(n): with tf.variable_scope('conv3_%d' % i, reuse=self.reuse): conv3 = residual_block(layers[-1], filter_depth * 4, training) layers.append(conv3) # assert conv3.get_shape().as_list()[1:] == [8, 8, 64] # [-1,w/8,h/8,features=filter_depth*4] with tf.variable_scope('fc', reuse=self.reuse): in_channel = layers[-1].get_shape().as_list()[-1] bn_layer = batch_normalization_layer(layers[-1], in_channel, training) # relu_layer = tf.nn.relu(bn_layer) global_pool = tf.reduce_mean(bn_layer, [1, 2]) # assert global_pool.get_shape().as_list()[-1:] == [filter_depth*4] output = output_layer(global_pool, 1024) layers.append(output) logits = tf.layers.dense(layers[-1], units=classes) cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=input_y, logits=logits)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) self.conv_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) self.fc_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='fc') self.variables = self.conv_variables + self.fc_variables count_parameters(self.variables, name="classifier_parameter_num") return cost, accuracy
def __call__(self, conditional_input, generated_input, training=False, dropout_rate=0.0): """ :param conditional_input: A batch of conditional inputs (x_i) of size [batch_size, height, width, channel] :param generated_input: A batch of generated inputs (x_g) of size [batch_size, height, width, channel] :param training: Placeholder for training or a boolean indicating training or validation :param dropout_rate: A float placeholder for dropout rate or a float indicating the dropout rate :param name: Network name :return: """ conditional_input = tf.convert_to_tensor(conditional_input) generated_input = tf.convert_to_tensor(generated_input) with tf.variable_scope(self.name, reuse=self.reuse): concat_images = tf.concat([conditional_input, generated_input], axis=3) outputs = concat_images self.current_layers.append(outputs) with slim.arg_scope([slim.conv2d, slim.conv2d_transpose], num_outputs = 64, padding = 'SAME', kernel_size = [3,3], stride = (1,1), activation_fn = tf.nn.leaky_relu, normalizer_fn=slim.batch_norm, normalizer_params= self.batch_norm_params): #with tf.variable_scope('first_conv', reuse=self.reuse): conv = slim.conv2d(outputs, stride=(2,2)) self.current_layers.append(conv) input_projection = slim.conv2d(outputs, num_outputs=outputs.get_shape()[3], kernel_size = [3,3], stride=(2,2), activation_fn= None, normalizer_fn= None) conv1 = tf.concat([conv, input_projection], axis=3) en1 = self.encoder_block(conv1, 1) en2 = self.encoder_block(en1, 2) en3 = self.encoder_block(en2, 3) #with tf.variable_scope('decoder_block', reuse=self.reuse): feature_level_flatten = tf.reduce_mean(en3, axis=[1, 2]) location_level_flatten = tf.layers.flatten(en3) feature_level_dense = tf.layers.dense(feature_level_flatten, units=1024, activation=tf.nn.leaky_relu) combo_level_flatten = tf.concat([feature_level_dense, location_level_flatten], axis=1) #with tf.variable_scope('discriminator_out_block', reuse=self.reuse): outputs = tf.layers.dense(combo_level_flatten, 1) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES) if self.build: print("discr layers", self.conv_layer_num) count_parameters(self.variables, name="discriminator_parameter_num") self.build = False return outputs, self.current_layers
def __call__(self, image_input, training=False, dropout_rate=0.0): """ Runs the CNN producing the predictions and the gradients. :param image_input: Image input to produce embeddings for. e.g. for EMNIST [batch_size, 28, 28, 1] :param training: A flag indicating training or evaluation :param dropout_rate: A tf placeholder of type tf.float32 indicating the amount of dropout applied :return: Embeddings of size [batch_size, self.num_classes] """ with tf.variable_scope(self.name, reuse=self.reuse): layer_features = [] with tf.variable_scope('FCCLayerNet'): outputs = image_input for i in range(len(self.layer_stage_sizes)): with tf.variable_scope('conv_stage_{}'.format(i)): for j in range(self.inner_layer_depth): with tf.variable_scope('conv_{}_{}'.format(i, j)): outputs = tf.layers.dense( outputs, units=self.layer_stage_sizes[i]) outputs = leaky_relu( outputs, name="leaky_relu{}".format(i)) layer_features.append(outputs) if self.batch_norm_use: outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=False) outputs = tf.layers.dropout(outputs, rate=dropout_rate, training=training) # apply dropout only at dimensionality # reducing steps, i.e. the last layer in # every group c_conv_encoder = outputs c_conv_encoder = tf.contrib.layers.flatten(c_conv_encoder) c_conv_encoder = tf.layers.dense(c_conv_encoder, units=self.num_classes) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if not self.build_completed: self.build_completed = True count_parameters(self.variables, "FCCLayerNet") return c_conv_encoder, layer_features
def __call__(self, input_tensor_batch, input_y, training=True): with tf.variable_scope(self.name, reuse=self.reuse): input_tensor_batch_3_channel = tf.concat( [input_tensor_batch, input_tensor_batch, input_tensor_batch], axis=3) x = self.conv1(input_tensor_batch_3_channel) x = self.bn_conv1(x, training=training) x = tf.nn.relu(x) x = self.max_pool(x) x = self.l2a(x, training=training) x = self.l2b(x, training=training) x = self.l2c(x, training=training) x = self.l3a(x, training=training) x = self.l3b(x, training=training) x = self.l3c(x, training=training) x = self.l3d(x, training=training) x = self.l4a(x, training=training) x = self.l4b(x, training=training) x = self.l4c(x, training=training) x = self.l4d(x, training=training) x = self.l4e(x, training=training) x = self.l4f(x, training=training) x = self.l5a(x, training=training) x = self.l5b(x, training=training) x = self.l5c(x, training=training) # x = self.avg_pool(x) if self.include_top: logits = self.fc1000(self.flatten(x)) cost = tf.losses.softmax_cross_entropy(onehot_labels=input_y, logits=logits) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float32)) self.variables = tf.get_collection( tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) count_parameters(self.variables, name="classifier_parameter_num") return cost, accuracy elif self.global_pooling: return self.global_pooling(x) else: return x
def __call__(self, conditional_input, generated_input, training=False, dropout_rate=0.0): """ :param conditional_input: A batch of conditional inputs (x_i) of size [batch_size, height, width, channel] :param generated_input: A batch of generated inputs (x_g) of size [batch_size, height, width, channel] :param training: Placeholder for training or a boolean indicating training or validation :param dropout_rate: A float placeholder for dropout rate or a float indicating the dropout rate :param name: Network name :return: """ conditional_input = tf.convert_to_tensor(conditional_input) generated_input = tf.convert_to_tensor(generated_input) with tf.variable_scope(self.name, reuse=self.reuse): concat_images = tf.concat([conditional_input, generated_input], axis=3) outputs = concat_images encoder_layers = [] current_layers = [outputs] with tf.variable_scope('conv_layers'): for i, layer_size in enumerate(self.layer_sizes): encoder_inner_layers = [outputs] with tf.variable_scope('g_conv{}'.format(i)): if i == 0: outputs = self.conv_layer(outputs, num_filters=64, filter_size=(3, 3), strides=(2, 2)) outputs = leaky_relu(features=outputs) outputs = layer_norm(inputs=outputs, center=True, scale=True) current_layers.append(outputs) else: for j in range(self.inner_layers[i]): outputs = self.add_encoder_layer( input=outputs, name="encoder_inner_conv_{}_{}".format( i, j), training=training, layer_to_skip_connect=current_layers[-2], num_features=self.layer_sizes[i], dropout_rate=dropout_rate, dim_reduce=False, local_inner_layers=encoder_inner_layers) current_layers.append(outputs) outputs = self.add_encoder_layer( input=outputs, name="encoder_outer_conv_{}".format(i), training=training, layer_to_skip_connect=current_layers[-2], local_inner_layers=encoder_inner_layers, num_features=self.layer_sizes[i], dropout_rate=dropout_rate, dim_reduce=True) current_layers.append(outputs) encoder_layers.append(outputs) flatten = tf.contrib.layers.flatten(encoder_layers[-1]) dense = tf.layers.dense(flatten, units=1024, activation=leaky_relu) with tf.variable_scope('discriminator_out'): outputs = tf.layers.dense(dense, 1, name='outputs') self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) #view_names_of_variables(self.variables) if self.build: print("discr layers", self.conv_layer_num) count_parameters(self.variables, name="discriminator_parameter_num") self.build = False return outputs, current_layers
def __call__(self, z_inputs, conditional_input, training=False, dropout_rate=0.0): """ Apply network on data. :param z_inputs: Random noise to inject [batch_size, z_dim] :param conditional_input: A batch of images to use as conditionals [batch_size, height, width, channels] :param training: Training placeholder or boolean :param dropout_rate: Dropout rate placeholder or float :return: Returns x_g (generated images), encoder_layers(encoder features), decoder_layers(decoder features) """ conditional_input = tf.convert_to_tensor(conditional_input) with tf.variable_scope(self.name, reuse=self.reuse): # reshape from inputs outputs = conditional_input encoder_layers = [] current_layers = [outputs] with tf.variable_scope('conv_layers'): for i, layer_size in enumerate(self.layer_sizes): encoder_inner_layers = [outputs] with tf.variable_scope('g_conv{}'.format(i)): if i == 0: #first layer is a single conv layer instead of MultiLayer for best results outputs = self.conv_layer(outputs, num_filters=64, filter_size=(3, 3), strides=(2, 2)) outputs = leaky_relu(features=outputs) outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=True) current_layers.append(outputs) encoder_inner_layers.append(outputs) else: for j in range( self.inner_layers[i] ): #Build the inner Layers of the MultiLayer outputs = self.add_encoder_layer( input=outputs, training=training, name="encoder_layer_{}_{}".format(i, j), layer_to_skip_connect=current_layers, num_features=self.layer_sizes[i], dim_reduce=False, local_inner_layers=encoder_inner_layers, dropout_rate=dropout_rate) encoder_inner_layers.append(outputs) current_layers.append(outputs) #add final dim reducing conv layer for this MultiLayer outputs = self.add_encoder_layer( input=outputs, name="encoder_layer_{}".format(i), training=training, layer_to_skip_connect=current_layers, local_inner_layers=encoder_inner_layers, num_features=self.layer_sizes[i], dim_reduce=True, dropout_rate=dropout_rate) current_layers.append(outputs) encoder_layers.append(outputs) g_conv_encoder = outputs with tf.variable_scope( "vector_expansion" ): # Used for expanding the z injected noise to match the # dimensionality of the various decoder MultiLayers, injecting # noise into multiple decoder layers in a skip-connection way # improves quality of results. We inject in the first 3 decode # multi layers num_filters = 8 concat_shape = tuple(encoder_layers[-1].get_shape()) concat_shape = [int(i) for i in concat_shape] z_dense_0 = tf.layers.dense(z_inputs, concat_shape[1] * concat_shape[2] * num_filters, name='input_dense_0') z_reshape_0 = tf.reshape(z_dense_0, [ self.batch_size, concat_shape[1], concat_shape[2], num_filters ], name='z_reshape_0') concat_shape = tuple(encoder_layers[-2].get_shape()) concat_shape = [int(i) for i in concat_shape] z_dense_1 = tf.layers.dense(z_inputs, concat_shape[1] * concat_shape[2] * num_filters, name='input_dense_1') z_reshape_1 = tf.reshape(z_dense_1, [ self.batch_size, concat_shape[1], concat_shape[2], num_filters ], name='z_reshape_1') num_filters = int(num_filters / 2) concat_shape = tuple(encoder_layers[-3].get_shape()) concat_shape = [int(i) for i in concat_shape] z_dense_2 = tf.layers.dense(z_inputs, concat_shape[1] * concat_shape[2] * num_filters, name='input_dense_2') z_reshape_2 = tf.reshape(z_dense_2, [ self.batch_size, concat_shape[1], concat_shape[2], num_filters ], name='z_reshape_2') num_filters = int(num_filters / 2) concat_shape = tuple(encoder_layers[-4].get_shape()) concat_shape = [int(i) for i in concat_shape] z_dense_3 = tf.layers.dense(z_inputs, concat_shape[1] * concat_shape[2] * num_filters, name='input_dense_3') z_reshape_3 = tf.reshape(z_dense_3, [ self.batch_size, concat_shape[1], concat_shape[2], num_filters ], name='z_reshape_3') z_layers = [z_reshape_0, z_reshape_1, z_reshape_2, z_reshape_3] outputs = g_conv_encoder decoder_layers = [] current_layers = [outputs] with tf.variable_scope('g_deconv_layers'): for i in range(len(self.layer_sizes) + 1): if i < 3: #Pass the injected noise to the first 3 decoder layers for sharper results outputs = tf.concat([z_layers[i], outputs], axis=3) current_layers[-1] = outputs idx = len(self.layer_sizes) - 1 - i num_features = self.layer_sizes[idx] inner_layers = self.inner_layers[idx] upscale_shape = encoder_layers[idx].get_shape().as_list() if idx < 0: num_features = self.layer_sizes[0] inner_layers = self.inner_layers[0] outputs = tf.concat([outputs, conditional_input], axis=3) upscale_shape = conditional_input.get_shape().as_list() with tf.variable_scope('g_deconv{}'.format(i)): decoder_inner_layers = [outputs] for j in range(inner_layers): if i == 0 and j == 0: outputs = self.add_decoder_layer( input=outputs, name="decoder_inner_conv_{}_{}".format( i, j), training=training, layer_to_skip_connect=current_layers, num_features=num_features, dim_upscale=False, local_inner_layers=decoder_inner_layers, dropout_rate=dropout_rate) decoder_inner_layers.append(outputs) else: outputs = self.add_decoder_layer( input=outputs, name="decoder_inner_conv_{}_{}".format( i, j), training=training, layer_to_skip_connect=current_layers, num_features=num_features, dim_upscale=False, local_inner_layers=decoder_inner_layers, w_size=upscale_shape[1], h_size=upscale_shape[2], dropout_rate=dropout_rate) decoder_inner_layers.append(outputs) current_layers.append(outputs) decoder_layers.append(outputs) if idx >= 0: upscale_shape = encoder_layers[ idx - 1].get_shape().as_list() if idx == 0: upscale_shape = conditional_input.get_shape( ).as_list() outputs = self.add_decoder_layer( input=outputs, name="decoder_outer_conv_{}".format(i), training=training, layer_to_skip_connect=current_layers, num_features=num_features, dim_upscale=True, local_inner_layers=decoder_inner_layers, w_size=upscale_shape[1], h_size=upscale_shape[2], dropout_rate=dropout_rate) current_layers.append(outputs) if (idx - 1) >= 0: outputs = tf.concat( [outputs, encoder_layers[idx - 1]], axis=3) current_layers[-1] = outputs high_res_layers = [] for p in range(2): outputs = self.conv_layer(outputs, self.layer_sizes[0], [3, 3], strides=(1, 1), transpose=False) outputs = leaky_relu(features=outputs) outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=True) high_res_layers.append(outputs) outputs = self.conv_layer(outputs, self.num_channels, [3, 3], strides=(1, 1), transpose=False) # output images with tf.variable_scope('g_tanh'): gan_decoder = tf.tanh(outputs, name='outputs') self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='g') if self.build: print("generator_total_layers", self.conv_layer_num) count_parameters(self.variables, name="generator_parameter_num") self.build = False return gan_decoder, encoder_layers, decoder_layers
def __call__(self, inputs, input_y, classes, training=False, dropout_rate=0.0): inputs = tf.convert_to_tensor(inputs) with tf.variable_scope(self.name, reuse=self.reuse): outputs = inputs encoder_layers = [] current_layers = [outputs] with tf.variable_scope('conv_layers'): for i, layer_size in enumerate(self.layer_sizes): encoder_inner_layers = [outputs] with tf.variable_scope('g_conv{}'.format(i)): if i == 0: outputs = self.conv_layer(outputs, num_filters=64, filter_size=(3, 3), strides=(2, 2)) outputs = leaky_relu(features=outputs) outputs = layer_norm(inputs=outputs, center=True, scale=True) # outputs = tf.nn.relu(outputs) # outputs = tf.layers.batch_normalization(outputs, training=training, momentum=0.9) current_layers.append(outputs) else: for j in range(self.inner_layers[i]): outputs = self.add_encoder_layer( input=outputs, name="encoder_inner_conv_{}_{}".format( i, j), training=training, layer_to_skip_connect=current_layers[-2], num_features=self.layer_sizes[i], dropout_rate=dropout_rate, dim_reduce=False, local_inner_layers=encoder_inner_layers) current_layers.append(outputs) encoder_inner_layers.append(outputs) outputs = self.add_encoder_layer( input=outputs, name="encoder_outer_conv_{}".format(i), training=training, layer_to_skip_connect=current_layers[-2], local_inner_layers=encoder_inner_layers, num_features=self.layer_sizes[i], dropout_rate=dropout_rate, dim_reduce=True) current_layers.append(outputs) encoder_layers.append(outputs) with tf.variable_scope('classifier_dense_block'): if self.use_wide_connections: mean_encoder_layers = [] concat_encoder_layers = [] for layer in encoder_layers: mean_encoder_layers.append( tf.reduce_mean(layer, axis=[1, 2])) concat_encoder_layers.append(tf.layers.flatten(layer)) feature_level_flatten = tf.concat(mean_encoder_layers, axis=1) location_level_flatten = tf.concat(concat_encoder_layers, axis=1) else: feature_level_flatten = tf.reduce_mean(encoder_layers[-1], axis=[1, 2]) location_level_flatten = tf.layers.flatten( encoder_layers[-1]) feature_level_dense = tf.layers.dense(feature_level_flatten, units=1024, activation=leaky_relu) # feature_level_dense = tf.layers.dense(feature_level_flatten, units=1024) combo_level_flatten = tf.concat( [feature_level_dense, location_level_flatten], axis=1) with tf.variable_scope('classifier_out_block'): logits = tf.layers.dense(combo_level_flatten, units=classes) cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(labels=input_y, logits=logits)) correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) accuracy = tf.reduce_mean( tf.cast(correct_prediction, tf.float32)) # cost = tf.losses.softmax_cross_entropy(onehot_labels=input_y, logits=logits) # correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # cost = tf.losses.softmax_cross_entropy(onehot_labels=input_y, logits=logits) # correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(input_y, 1)) # # correct_prediction=tf.Print(correct_prediction,[correct_prediction],'correct_prediction') # accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if self.build: print("classification layers", self.conv_layer_num) count_parameters(self.variables, name="classifier_parameter_num") self.build = False return cost, accuracy
def __call__(self, image_input, training=False, dropout_rate=0.0): """ Runs the CNN producing the predictions and the gradients. :param image_input: Image input to produce embeddings for. e.g. for EMNIST [batch_size, 28, 28, 1] :param training: A flag indicating training or evaluation :param dropout_rate: A tf placeholder of type tf.float32 indicating the amount of dropout applied :return: Embeddings of size [batch_size, self.num_classes] """ regularizer = tf.contrib.layers.l2_regularizer(scale=0.1) stride = 1 self.former_dim = self.layer_stage_sizes[0] self.current_dim = self.layer_stage_sizes[0] with tf.variable_scope(self.name, reuse=self.reuse): layer_features = [] with tf.variable_scope('Pyramid'): outputs = image_input #outputs = tf.layers.dropout(outputs, rate=0.2, training=training) outputs = tf.layers.conv2d(outputs, self.layer_stage_sizes[0], [3, 3], strides=(stride, stride), padding='SAME', activation=None) if self.batch_norm_use: outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=False) #outputs = relu(outputs, name="relu") outputs = 1.125 * outputs * tf.nn.sigmoid(outputs, name="swish") #outputs = tf.layers.conv2d(outputs, self.layer_stage_sizes[0], [3, 3], strides=(stride, stride), padding='SAME', activation=None, kernel_regularizer=regularizer) for i in range(3): with tf.variable_scope('conv_stage_{}'.format(i)): for j in range(self.inner_layer_depth): with tf.variable_scope('conv_{}_{}'.format(i, j)): if j == 0 and i != 0: stride = 2 res = tf.layers.average_pooling2d( outputs, pool_size=(2, 2), strides=2) else: stride = 1 res = outputs self.former_dim = self.current_dim self.current_dim = self.layer_stage_sizes[ i * self.inner_layer_depth + j] res = tf.concat([ res, tf.zeros([ res.shape[0], res.shape[1], res.shape[2], self.current_dim - self.former_dim ]) ], axis=3) outputs = tf.layers.conv2d(outputs, self.current_dim, [3, 3], strides=(stride, stride), padding='SAME', activation=None) if self.batch_norm_use: outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=False) #outputs = relu(outputs, name="relu{}".format(i)) outputs = 1.125 * outputs * tf.nn.sigmoid( outputs, name="swish{}".format(i)) # outputs = tf.layers.conv2d(outputs, self.layer_stage_sizes[i], [3, 3], strides=(stride, stride), padding='SAME', activation=None, kernel_regularizer=regularizer) layer_features.append(outputs) stride = 1 outputs = tf.layers.conv2d(outputs, self.current_dim, [3, 3], strides=(stride, stride), padding='SAME', activation=None) #outputs =outputs*tf.nn.sigmoid(outputs, name="swish{}".format(i)) # outputs = tf.layers.conv2d(outputs, self.layer_stage_sizes[i], [3, 3], strides=(stride, stride), padding='SAME', activation=None, kernel_regularizer=regularizer) if self.batch_norm_use: outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=False) outputs = outputs + res #outputs = relu(outputs, name="relu{}".format(i)) outputs = 1.125 * outputs * tf.nn.sigmoid( outputs, name="swish{}".format(i)) layer_features.append(outputs) #outputs = tf.layers.dropout(outputs, rate=0.5, training=training) #outputs = tf.layers.average_pooling2d(outputs, pool_size=(2, 2), strides=1) c_conv_encoder = outputs c_conv_encoder = tf.reduce_mean(c_conv_encoder, [1, 2]) # c_conv_encoder = tf.contrib.layers.flatten(c_conv_encoder) c_conv_encoder = tf.layers.dense(c_conv_encoder, units=self.num_classes) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if not self.build_completed: self.build_completed = True count_parameters(self.variables, "Pyramid") return c_conv_encoder, layer_features
def __call__(self, text_input, training=False, dropout_rate=0.0): """ Runs the CNN producing the predictions and the gradients. :param text_input: Text input to produce embeddings for. e.g. for text data [batch_size, 300] :param training: A flag indicating training or evaluation :param dropout_rate: A tf placeholder of type tf.float32 indicating the amount of dropout applied :return: Embeddings of size [batch_size, self.num_classes] """ with tf.variable_scope(self.name, reuse=self.reuse): layer_features = [] with tf.variable_scope('VGGNet'): outputs = image_input for i in range(len(self.layer_stage_sizes)): with tf.variable_scope('conv_stage_{}'.format(i)): for j in range(self.inner_layer_depth): with tf.variable_scope('conv_{}_{}'.format(i, j)): if (j == self.inner_layer_depth - 1) and self.strided_dim_reduction: stride = 2 else: stride = 1 outputs = tf.layers.conv2d( outputs, self.layer_stage_sizes[i], [3, 3], strides=(stride, stride), padding='SAME', activation=None) outputs = leaky_relu( outputs, name="leaky_relu{}".format(i)) layer_features.append(outputs) if self.batch_norm_use: outputs = batch_norm(outputs, decay=0.99, scale=True, center=True, is_training=training, renorm=False) if self.strided_dim_reduction == False: outputs = tf.layers.max_pooling2d(outputs, pool_size=(2, 2), strides=2) outputs = tf.layers.dropout(outputs, rate=dropout_rate, training=training) # apply dropout only at dimensionality # reducing steps, i.e. the last layer in # every group c_conv_encoder = outputs c_conv_encoder = tf.contrib.layers.flatten(c_conv_encoder) c_conv_encoder = tf.layers.dense(c_conv_encoder, units=self.num_classes) self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if not self.build_completed: self.build_completed = True count_parameters(self.variables, "VGGNet") return c_conv_encoder, layer_features
def __call__(self, text_input, training=False, dropout_rate=0.0): with tf.variable_scope(self.name, reuse=self.reuse): layer_features = [] with tf.variable_scope('VGGNet'): if self.embeddings == None: with tf.name_scope("embedding"): W = tf.Variable(tf.random_uniform( [self.vocab_size, self.embedding_dim], -1.0, 1.0), name="W") else: W = self.embeddings embedded_chars = tf.nn.embedding_lookup(W, text_input) if self.cell == 'bidlstm': lstm_fw_cell = rnn.BasicLSTMCell( self.hidden_unit) #forward direction cell lstm_bw_cell = rnn.BasicLSTMCell( self.hidden_unit) #backward direction cell if dropout_rate is not None: lstm_fw_cell = rnn.DropoutWrapper(lstm_fw_cell, output_keep_prob=1 - dropout_rate) lstm_bw_cell = rnn.DropoutWrapper(lstm_bw_cell, output_keep_prob=1 - dropout_rate) outputs, _ = tf.nn.bidirectional_dynamic_rnn( lstm_fw_cell, lstm_bw_cell, embedded_chars, dtype=tf.float32) output_rnn = tf.concat( outputs, axis=2) #[batch_size,sequence_length,hidden_size*2] output_rnn_last = tf.reduce_mean( output_rnn, axis=1 ) #[batch_size,hidden_size*2] #output_rnn_last=output_rnn[:,-1,:] ##[batch_size,hidden_size*2] #TODO elif self.cell == 'lstm': lstm_cell = rnn.BasicLSTMCell(self.hidden_unit) if dropout_rate is not None: lstm_cell = rnn.DropoutWrapper(lstm_cell, output_keep_prob=1 - dropout_rate) outputs, _ = tf.nn.dynamic_rnn(lstm_cell, embedded_chars, dtype=tf.float32) output_rnn = tf.concat(outputs, axis=2) output_rnn_last = tf.reduce_mean(output_rnn, axis=1) elif self.cell == 'gru': gru_cell = rnn.GRUCell(self.hidden_unit) if dropout_rate is not None: gru_cell = rnn.DropoutWrapper(gru_cell, output_keep_prob=1 - dropout_rate) outputs, _ = tf.nn.dynamic_rnn(gru_cell, embedded_chars, dtype=tf.float32) output_rnn = tf.concat(outputs, axis=2) output_rnn_last = tf.reduce_mean(output_rnn, axis=1) c_conv_encoder = output_rnn_last dense = tf.layers.dense(inputs=c_conv_encoder, units=self.num_units, activation=tf.nn.relu) # Logits Layer scores = tf.layers.dense(inputs=dense, units=self.num_classes, activation=tf.sigmoid) #self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if not self.build_completed: self.build_completed = True count_parameters(self.variables, "VGGNet") return scores, layer_features
def __call__(self, text_input, training=False, dropout_rate=0.0): """ Runs the CNN producing the predictions and the gradients. :param text_input: Text input to produce embeddings for. e.g. for text data [batch_size, sequence_length] :param training: A flag indicating training or evaluation :param dropout_rate: A tf placeholder of type tf.float32 indicating the amount of dropout applied :return: Embeddings of size [batch_size, self.num_classes] """ with tf.variable_scope(self.name, reuse=self.reuse): layer_features = [] with tf.variable_scope('VGGNet'): if self.embeddings == None: with tf.device('/cpu:0'), tf.name_scope("embedding"): W = tf.Variable(tf.random_uniform( [self.vocab_size, self.embedding_dim], -1.0, 1.0), name="W") else: W = self.embeddings embedded_chars = tf.nn.embedding_lookup(W, text_input) inputs = embedded_chars_expanded = tf.expand_dims( embedded_chars, -1) pooled_outputs = [] for i, filter_size in enumerate(self.filter_sizes): with tf.name_scope("conv-maxpool-%s" % filter_size): # Convolution Layer filter_shape = [ filter_size, self.embedding_dim, 1, self.num_filters ] W = tf.Variable(tf.truncated_normal(filter_shape, stddev=0.1), name="W") b = tf.Variable(tf.constant(0.1, shape=[self.num_filters]), name="b") conv = tf.nn.conv2d(inputs, W, strides=[1, 1, 1, 1], padding="VALID", name="conv") # Apply nonlinearity if self.activation == 'relu': h = tf.nn.relu(tf.nn.bias_add(conv, b), name="relu") elif self.activation == 'sigmoid': h = tf.sigmoid(tf.nn.bias_add(conv, b), name="sigmoid") elif self.activation == 'tanh': h = tf.tanh(tf.nn.bias_add(conv, b), name="tanh") layer_features.append(h) # Maxpooling over the outputs pooled = tf.nn.max_pool(h, ksize=[ 1, self.max_sent_length - filter_size + 1, 1, 1 ], strides=[1, 1, 1, 1], padding='VALID', name="pool") pooled_outputs.append(pooled) # Combine all the pooled features num_filters_total = self.num_filters * len(self.filter_sizes) h_pool = tf.concat(pooled_outputs, 3) h_pool_flat = tf.reshape(h_pool, [-1, num_filters_total]) if self.l2_norm != 0: h_pool_flat = self.l2_norm * tf.divide( h_pool_flat, tf.norm(h_pool_flat, ord='euclidean')) # Add dropout with tf.name_scope("dropout"): h_drop = tf.layers.dropout(h_pool_flat, rate=dropout_rate, training=training) c_conv_encoder = h_drop c_conv_encoder = tf.contrib.layers.flatten(c_conv_encoder) dense = tf.layers.dense(inputs=c_conv_encoder, units=self.num_units, activation=tf.nn.relu) # Logits Layer scores = tf.layers.dense(inputs=dense, units=self.num_classes) #self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if not self.build_completed: self.build_completed = True count_parameters(self.variables, "VGGNet") return scores, layer_features
def __call__(self, z_inputs, conditional_input, training=False, dropout_rate=0.0): """ Apply network on data. :param z_inputs: Random noise to inject [batch_size, z_dim] :param conditional_input: A batch of images to use as conditionals [batch_size, height, width, channels] :param training: Training placeholder or boolean :param dropout_rate: Dropout rate placeholder or float :return: Returns x_g (generated images), encoder_layers(encoder features), decoder_layers(decoder features) """ z_layer=[] h, w, n = 2, 2, 8 for i in range(4): z_dence = tf.layers.dense(z_inputs, h*w*n) z_noise = tf.reshape(z_dence, [16,h,w,n]) z_layer.append(z_noise) h = h*2 w = w*2 n = np.int(n/2) with tf.variable_scope(self.name, reuse=self.reuse): # reshape from inputs conditional_input = tf.convert_to_tensor(conditional_input) with slim.arg_scope([slim.conv2d, slim.conv2d_transpose], num_outputs = 64, padding = 'SAME', kernel_size = [3,3], stride = (1,1), activation_fn = None): with tf.variable_scope(self.name + 'first_en_conv'): conv = slim.conv2d(conditional_input, stride=(2,2)) self.encoder_layers.append(conv) input_projection = slim.conv2d(conditional_input, num_outputs=conditional_input.get_shape()[3], stride=(2,2)) output1 = tf.concat([conv, input_projection], axis=3) en1 = self.encoder_block(output1, 1) #[B,7, 7, 64] en2 = self.encoder_block(en1, 2) #[B,4,4,64] en3 = self.encoder_block(en2, 3) #[b,2,2,64] #end encoder with tf.variable_scope(self.name + '/First_de_conv'): self.decoder_layers.append(en3) #input_noise = self.z_noise_concat(en3, z_inputs, 2, 2) #[b,2,2,72] input_noise = tf.concat([en3, z_layer[0]], axis=3) with tf.variable_scope(self.name + '/First_de_block'): de_conv1 = self.decoder_block(input_noise) #[b, 4, 4, 64] de_conv1 = tf.concat([de_conv1, self.encoder_layers[2]], axis=3) #de_conv1_noise = self.z_noise_concat(de_conv1, z_inputs, 4, 4) de_conv1_noise = tf.concat([de_conv1, z_layer[1]], axis=3) with tf.variable_scope(self.name + '/Second_de_block'): de_conv2 = self.decoder_block(de_conv1_noise) #[b, 8, 8, 64] #de_conv2_noise = self.z_noise_concat(de_conv2, z_inputs, 8, 8) de_conv2_noise = tf.concat([de_conv2, z_layer[2]], axis=3) de_conv2 = self.upscale(de_conv2_noise, 7, 7) de_conv2 = tf.concat([de_conv2, self.encoder_layers[1]], axis=3) with tf.variable_scope(self.name + '/Third_de_block'): de_conv3 = self.decoder_block(de_conv2) #[b, 14, 14 ,64] de_conv3 = tf.concat([de_conv3, self.encoder_layers[0]], axis=3) with tf.variable_scope(self.name + '/Forth_de_block'): de_conv4 = self.decoder_block(de_conv3) #[b, 28, 28 ,64] de_conv4 = tf.concat([de_conv4, conditional_input], axis=3) with tf.variable_scope(self.name + '/Last_de_block'): de_conv5_1 = slim.conv2d(de_conv4) de_conv5_1 = tf.concat([de_conv5_1, de_conv4], axis=3) de_conv5_2 = slim.conv2d(de_conv5_1) de_conv5_2 = tf.concat([de_conv5_2, de_conv5_1], axis=3) with tf.variable_scope(self.name + '/P_process'): de_conv = slim.conv2d(de_conv5_2) de_conv = slim.conv2d(de_conv, num_outputs = 3) with tf.variable_scope('g_tanh'): gan_decoder = tf.tanh(de_conv, name='outputs') self.reuse = True self.variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope=self.name) if self.build: count_parameters(self.variables, 'generator_parameter_num') self.build = False return gan_decoder, self.encoder_layers, self.decoder_layers