def __call__(self, D): with tf.variable_scope(self.name, reuse=self.reuse): D_norm = D G_conv1 = utils.conv2d_basic(D_norm, self.Param['G_W1'], self.Param['G_b1']) G_relu1 = tf.nn.relu(G_conv1, name="G_relu1") G_conv2 = utils.conv2d_basic(G_relu1, self.Param['G_W2'], self.Param['G_b2']) G_relu2 = tf.nn.relu(G_conv2, name="G_relu2") G_pool1 = utils.max_pool_2x2(G_relu2) G_conv3 = utils.conv2d_basic(G_pool1, self.Param['G_W3'], self.Param['G_b3']) G_relu3 = tf.nn.relu(G_conv3, name="G_relu3") G_conv4 = utils.conv2d_basic(G_relu3, self.Param['G_W4'], self.Param['G_b4']) G_relu4 = tf.nn.relu(G_conv4, name="G_relu4") G_pool2 = utils.max_pool_2x2(G_relu4) G_conv5 = utils.conv2d_basic(G_pool2, self.Param['G_W5'], self.Param['G_b5']) G_relu5 = tf.nn.relu(G_conv5, name="G_relu5") output_shape = G_relu5.get_shape().as_list() output_shape[1] *= 2 output_shape[2] *= 2 output_shape[3] = self.Param['G_W6'].get_shape().as_list()[2] G_rs6 = tf.image.resize_images(G_relu5, output_shape[1:3], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) G_conv6 = utils.conv2d_basic(G_rs6, self.Param['G_W6'], self.Param['G_b6']) G_relu6 = tf.nn.relu(G_conv6, name="G_rs6") output_shape = G_relu6.get_shape().as_list() output_shape[1] *= 2 output_shape[2] *= 2 output_shape[3] = self.Param['G_W7'].get_shape().as_list()[2] G_rs7 = tf.image.resize_images(G_relu6, output_shape[1:3], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) G_conv7 = utils.conv2d_basic(G_rs7, self.Param['G_W7'], self.Param['G_b7']) G_relu7 = tf.nn.relu(G_conv7, name="G_rs7") G_conv8 = utils.conv2d_basic(G_relu7, self.Param['G_W8'], self.Param['G_b8']) G_relu8 = tf.nn.relu(G_conv8, name="G_relu8") G_conv9 = utils.conv2d_basic(G_relu8, self.Param['G_W9'], self.Param['G_b9']) Gama = tf.nn.softmax(G_conv9, name="G_latent_softmax") return Gama
def build_encoding(self, x): """ Builds graph to create encoding from input x :param x: input image :return: flat layer containing the encoding """ def getVars(name, w_shape): """ Helper function to resuse variables in order to create a siamese net. :param name: Name of the variable we want :param w_shape: Shape of the variable we want :return: Variable with given name if exists, otherwise new variable with given shape """ w = tf.get_variable("W" + name, w_shape, initializer=tf.random_normal_initializer(mean=0.0, stddev=0.1), regularizer=tf.contrib.layers.l2_regularizer(0.01)) b = tf.get_variable("b" + name, [w_shape[-1]], initializer=tf.constant_initializer(0.1), regularizer=tf.contrib.layers.l2_regularizer(0.01)) return w, b prev_layer = x img_size = self.shape[2] for ind in range(len(self.conv_layer_size)): """ Iterate through conv_layers and apply convolution, rele and max-pool """ if ind == 0: w_shape = [self.conv_dim[ind], self.conv_dim[ind], self.shape[3], self.conv_layer_size[ind]] else: w_shape = [self.conv_dim[ind], self.conv_dim[ind], self.conv_layer_size[ind - 1], self.conv_layer_size[ind]] w, b = getVars("enc%s" % ind, w_shape) prev_layer = ops.max_pool_2x2(tf.nn.relu(ops.conv_2d(prev_layer, w, b))) self.enc_weights.append(w) self.enc_weights.append(b) # Reshape for fully connected layers next_size = self.conv_layer_size[-1] * img_size / (2 ** len(self.conv_layer_size)) * img_size / ( 2 ** len(self.conv_layer_size)) flat_layer = tf.reshape(prev_layer, [-1, next_size]) for ind in range(len(self.fcl_layer_size)): """ Iterate through fully connected layers and apply matmul and sigmoid """ if ind == 0: w_shape = [next_size, self.fcl_layer_size[0]] else: w_shape = [self.fcl_layer_size[ind - 1], self.fcl_layer_size[ind]] w, b = getVars("enc_fcl%s" % ind, w_shape) flat_layer = tf.nn.sigmoid(tf.matmul(flat_layer, w) + b) self.enc_weights.append(w) self.enc_weights.append(b) return flat_layer
def make_convnet(self): n_ffnet_inputs = self.n_ffnet_input n_ffnet_outputs = self.n_ffnet_output print("COVNET: Inputs: ", n_ffnet_inputs, " outputs: ", n_ffnet_outputs) with tf.name_scope('reshape'): x_image = tf.reshape(self.covnet_input, [-1, self.resolution[0], self.resolution[1], 1]) with tf.name_scope('conv1'): W_conv1 = my_ops.weight_variable([5, 5, 1, 32]) b_conv1 = my_ops.bias_variable([32]) h_conv1 = tf.nn.relu(my_ops.conv2d(x_image, W_conv1) + b_conv1) with tf.name_scope('pool1'): h_pool1 = my_ops.max_pool_2x2(h_conv1) with tf.name_scope('conv2'): W_conv2 = my_ops.weight_variable([5, 5, 32, 64]) b_conv2 = my_ops.bias_variable([64]) h_conv2 = tf.nn.relu(my_ops.conv2d(h_pool1, W_conv2) + b_conv2) with tf.name_scope('pool2'): h_pool2 = my_ops.max_pool_2x2(h_conv2) with tf.name_scope('fc1'): W_fc1 = my_ops.weight_variable([int(self.resolution[0]/4) * int(self.resolution[1]/4) * 64, 64]) b_fc1 = my_ops.bias_variable([64]) h_pool2_flat = tf.reshape(h_pool2, [-1, int(self.resolution[0]/4) * int(self.resolution[1]/4) * 64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # single output: with tf.name_scope('fc2'): W_fc2 = my_ops.weight_variable([64, 1]) b_fc2 = my_ops.bias_variable([1]) self.y_conv = tf.tanh(tf.matmul(h_fc1, W_fc2) + b_fc2) self.covloss = tf.squared_difference(self.y_conv, self.covnet_target) self.covnet_train_step = tf.train.AdamOptimizer(self.learning_rate).minimize(self.covloss) self.covaccuracy = tf.reduce_mean(self.covloss)
def __call__(self, D): with tf.variable_scope(self.name, reuse=self.reuse): D_norm = D G_conv1 = utils.conv2d_basic(D_norm, self.Param['G_W1'], self.Param['G_b1']) G_relu1 = tf.nn.relu(G_conv1, name="G_relu1") G_conv2 = utils.conv2d_basic(G_relu1, self.Param['G_W2'], self.Param['G_b2']) G_relu2 = tf.nn.relu(G_conv2, name="G_relu2") G_pool1 = utils.max_pool_2x2(G_relu2) G_conv3 = utils.conv2d_basic(G_pool1, self.Param['G_W3'], self.Param['G_b3']) G_relu3 = tf.nn.relu(G_conv3, name="G_relu3") G_conv4 = utils.conv2d_basic(G_relu3, self.Param['G_W4'], self.Param['G_b4']) G_relu4 = tf.nn.relu(G_conv4, name="G_relu4") G_pool2 = utils.max_pool_2x2(G_relu4) G_conv5 = utils.conv2d_basic(G_pool2, self.Param['G_W5'], self.Param['G_b5']) G_relu5 = tf.nn.relu(G_conv5, name="G_relu5") G_conv6 = utils.conv2d_basic(G_relu5, self.Param['G_W6'], self.Param['G_b6']) G_relu6 = tf.nn.relu(G_conv6, name="G_relu6") G_conv7 = utils.conv2d_basic(G_relu6, self.Param['G_W7'], self.Param['G_b7']) G_relu7 = tf.nn.relu(G_conv7, name="G_relu7") G_pool3 = utils.max_pool_2x2(G_relu7) G_conv8 = utils.conv2d_basic(G_pool3, self.Param['G_W8'], self.Param['G_b8']) G_relu8 = tf.nn.relu(G_conv8, name="G_relu8") G_conv9 = utils.conv2d_basic(G_relu8, self.Param['G_W9'], self.Param['G_b9']) G_relu9 = tf.nn.relu(G_conv9, name="G_relu9") G_conv10 = utils.conv2d_basic(G_relu9, self.Param['G_W10'], self.Param['G_b10']) G_relu10 = tf.nn.relu(G_conv10, name="G_relu10") G_pool4 = utils.max_pool_2x2(G_relu10) G_conv11 = utils.conv2d_basic(G_pool4, self.Param['G_W11'], self.Param['G_b11']) G_relu11 = tf.nn.relu(G_conv11, name="G_relu11") G_conv12 = utils.conv2d_basic(G_relu11, self.Param['G_W12'], self.Param['G_b12']) G_relu12 = tf.nn.relu(G_conv12, name="G_relu12") G_conv13 = utils.conv2d_basic(G_relu12, self.Param['G_W13'], self.Param['G_b13']) G_relu13 = tf.nn.relu(G_conv13, name="G_relu13") G_rs14 = tf.image.resize_images( G_relu13, [63, 63], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #G_rs14 = tf.image.resize_images(G_relu13, [64, 64], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) G_cc14 = tf.concat([G_rs14, G_relu10], 3, name="G_cc14") G_conv14 = utils.conv2d_basic(G_cc14, self.Param['G_W14'], self.Param['G_b14']) G_relu14 = tf.nn.relu(G_conv14, name="G_rs14") G_rs15 = tf.image.resize_images( G_relu14, [125, 125], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #G_rs15 = tf.image.resize_images(G_relu14, [128, 128], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) G_cc15 = tf.concat([G_rs15, G_relu7], 3, name="G_cc15") G_conv15 = utils.conv2d_basic(G_cc15, self.Param['G_W15'], self.Param['G_b15']) G_relu15 = tf.nn.relu(G_conv15, name="G_rs15") G_rs16 = tf.image.resize_images( G_relu15, [250, 250], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #G_rs16 = tf.image.resize_images(G_relu15, [256, 256], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) G_cc16 = tf.concat([G_rs16, G_relu4], 3, name="G_cc16") G_conv16 = utils.conv2d_basic(G_cc16, self.Param['G_W16'], self.Param['G_b16']) G_relu16 = tf.nn.relu(G_conv16, name="G_rs16") G_rs17 = tf.image.resize_images( G_relu16, [500, 500], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) #G_rs17 = tf.image.resize_images(G_relu16, [512, 512], method=tf.image.ResizeMethod.NEAREST_NEIGHBOR) G_cc17 = tf.concat([G_rs17, G_relu2], 3, name="G_cc17") G_conv17 = utils.conv2d_basic(G_cc17, self.Param['G_W17'], self.Param['G_b17']) G_relu17 = tf.nn.relu(G_conv17, name="G_rs17") G_conv18 = utils.conv2d_basic(G_relu17, self.Param['G_W18'], self.Param['G_b18']) G_relu18 = tf.nn.relu(G_conv18, name="G_relu18") G_conv19 = utils.conv2d_basic(G_relu18, self.Param['G_W19'], self.Param['G_b19']) Gama = tf.nn.softmax(G_conv19, name="G_latent_softmax") return Gama
def make_convnet(self): # # self.resolution is [width, height] # x_image = tf.reshape(self.covnet_input, [-1, self.resolution[0], self.resolution[1], 1]) # # n_features_maps = 20 # filter_width = int(2) # filter_height = int(2) # # W_conv1 = my_ops.weight_variable([filter_width, filter_height, 1, n_features_maps], 0.001) # b_conv1 = my_ops.bias_variable([n_features_maps]) # h_conv1 = tf.nn.tanh(my_ops.conv2d(x_image, W_conv1) + b_conv1) # # h_pool1 = my_ops.max_pool_2x2(h_conv1) # # h_pool1 = h_conv1 # # W_fc1 = my_ops.weight_variable([int(self.resolution[0]/2) * int(self.resolution[1]/2) * n_features_maps, 20], 0.001) # b_fc1 = my_ops.bias_variable([20]) # # h_pool1_flat = tf.reshape(h_pool1, [-1, int(self.resolution[0]/2) * int(self.resolution[1]/2) * n_features_maps]) # h_fc1 = tf.nn.tanh(tf.matmul(h_pool1_flat, W_fc1) + b_fc1) # # # single output: # W_fc2 = my_ops.weight_variable([20, 1], 0.01) # b_fc2 = my_ops.bias_variable([1]) # # self.y_conv = tf.tanh(tf.matmul(h_fc1, W_fc2) + b_fc2) # # self.covloss = tf.squared_difference(self.y_conv, self.covnet_target) # self.covnet_train_step = tf.train.AdamOptimizer(self.learning_rate).minimize(self.covloss) # self.covaccuracy = tf.reduce_mean(self.covloss) with tf.name_scope('reshape'): x_image = tf.reshape( self.covnet_input, [-1, self.resolution[0], self.resolution[1], 1]) # First convolutional layer - maps one grayscale image to 32 feature maps. with tf.name_scope('conv1'): W_conv1 = my_ops.weight_variable([5, 5, 1, 8], 0.1) b_conv1 = my_ops.bias_variable([8]) h_conv1 = tf.nn.relu(my_ops.conv2d(x_image, W_conv1) + b_conv1) # Pooling layer - downsamples by 2X. with tf.name_scope('pool1'): h_pool1 = my_ops.max_pool_2x2(h_conv1) # Second convolutional layer -- maps 32 feature maps to 64. with tf.name_scope('conv2'): W_conv2 = my_ops.weight_variable([5, 5, 8, 8], 0.1) b_conv2 = my_ops.bias_variable([8]) h_conv2 = tf.nn.relu(my_ops.conv2d(h_pool1, W_conv2) + b_conv2) # Second pooling layer. with tf.name_scope('pool2'): h_pool2 = my_ops.max_pool_2x2(h_conv2) # Fully connected layer 1 -- after 2 round of downsampling, our 28x28 image # is down to 7x7x64 feature maps -- maps this to 1024 features. with tf.name_scope('fc1'): W_fc1 = my_ops.weight_variable([ int(self.resolution[0] / 4) * int(self.resolution[1] / 4) * 8, 10 ], 0.001) b_fc1 = my_ops.bias_variable([10]) h_pool2_flat = tf.reshape(h_pool2, [ -1, int(self.resolution[0] / 4) * int(self.resolution[1] / 4) * 8 ]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # Map the 1024 features to 10 classes, one for each digit with tf.name_scope('fc2'): W_fc2 = my_ops.weight_variable([10, 1], 0.1) b_fc2 = my_ops.bias_variable([1]) self.y_conv = tf.tanh(tf.matmul(h_fc1, W_fc2) + b_fc2) self.covloss = tf.squared_difference(self.y_conv, self.covnet_target) self.covnet_train_step = tf.train.AdamOptimizer( self.learning_rate).minimize(self.covloss) self.covaccuracy = tf.reduce_mean(self.covloss)