def model_setup(self): """ This function sets up the model to train. self.input_a/self.input_b -> Set of training images. self.fake_a/self.fake_b -> Generated images by corresponding generator of input_a and input_b self.lr -> Learning rate variable self.cyc_a/ self.cyc_b -> Images generated after feeding self.fake_a/self.fake_b to corresponding generator. This is use to calculate cyclic loss """ self.input_a = tf.placeholder(tf.float32, [ utils.BATCH_SIZE, utils.IMG_WIDTH, utils.IMG_HEIGHT, utils.IMG_CHANNEL ], name="input_a") self.input_b = tf.placeholder(tf.float32, [ utils.BATCH_SIZE, utils.IMG_WIDTH, utils.IMG_HEIGHT, utils.IMG_CHANNEL ], name="input_b") self.fake_pool_a = tf.placeholder( tf.float32, [None, utils.IMG_WIDTH, utils.IMG_HEIGHT, utils.IMG_CHANNEL], name="fake_pool_a") self.fake_pool_b = tf.placeholder( tf.float32, [None, utils.IMG_WIDTH, utils.IMG_HEIGHT, utils.IMG_CHANNEL], name="fake_pool_b") # self.global_step = tf.contrib.slim.get_or_create_global_step() self.global_step = tf.train.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_a, 'fake_pool_b': self.fake_pool_b, } outputs = model.get_outputs(inputs, skip=self._skip) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real']
def model_setup(self, mode): if mode == 'train': factor = 1 if mode == 'test': factor = 1 self.input = tf.placeholder(tf.float32, [ model.BATCH_SIZE, model.L_IMG_HEIGHT // factor, model.L_IMG_WIDTH // factor, model.IMG_CHANNELS ], name="input") self.gt = tf.placeholder(tf.float32, [ model.BATCH_SIZE, model.H_IMG_HEIGHT // factor, model.H_IMG_WIDTH // factor, model.IMG_CHANNELS ], name="gt") self.edges = tf.placeholder(tf.float32, [ model.BATCH_SIZE, model.G_IMG_HEIGHT // factor, model.G_IMG_WIDTH // factor, 6 ], name="edges") self.global_step = slim.get_or_create_global_step() self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") inputs = { 'image_in': self.input, 'image_gt': self.gt, 'image_edges': self.edges # 'image_re': self.resized } outputs = model.get_outputs(inputs) self.output = outputs['output']
def model_setup(self): """ This function sets up the model to train. self.input_A/self.input_B -> Set of training images. self.fake_A/self.fake_B -> Generated images by corresponding generator of input_A and input_B self.lr -> Learning rate variable self.cyc_A/ self.cyc_B -> Images generated after feeding self.fake_A/self.fake_B to corresponding generator. This is use to calculate cyclic loss """ self.input_a = tf.placeholder( tf.float32, [1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="input_A") self.input_b = tf.placeholder( tf.float32, [1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="fake_pool_B") self.global_step = slim.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, } outputs = model.get_outputs(inputs, network=self._network_version, skip=self._skip) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real']
def model_setup(self): self.input_a = tf.placeholder( tf.float32, [ self.batch_size, model.IMG_WIDTH, model.IMG_HEIGHT, 1 ], name="input_A") self.input_b = tf.placeholder( tf.float32, [ self.batch_size, model.IMG_WIDTH, model.IMG_HEIGHT, 1 ], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [ None, model.IMG_WIDTH, model.IMG_HEIGHT, 1 ], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [ None, model.IMG_WIDTH, model.IMG_HEIGHT, 1 ], name="fake_pool_B") self.gt_a = tf.placeholder( tf.float32, [ self.batch_size, model.IMG_WIDTH, model.IMG_HEIGHT, self._num_cls ], name="gt_A") self.gt_b = tf.placeholder( tf.float32, [ self.batch_size, model.IMG_WIDTH, model.IMG_HEIGHT, self._num_cls ], name="gt_B") inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, } outputs = model.get_outputs(inputs, skip=self._skip, is_training=self.is_training, keep_rate=self.keep_rate) self.pred_mask_b = outputs['pred_mask_b'] self.predicter_b = tf.nn.softmax(self.pred_mask_b) self.compact_pred_b = tf.argmax(self.predicter_b, 3) self.compact_y_b = tf.argmax(self.gt_b, 3)
def save_images(self, nets, epoch, curr_tr, images_i, images_j): """ Saves input and output images. """ nets = utils.set_mode(nets, "eval") exists_or_mkdir(self._images_dir) if curr_tr > 0: donorm = False else: donorm = True names = [ 'inputA_', 'inputB_', 'fakeA_', 'fakeB_', 'cycA_', 'cycB_', 'mask_a', 'mask_b' ] with open( os.path.join(self._output_dir, 'epoch_' + str(epoch) + '.html'), 'w') as v_html: input_iter = minibatches(images_i, images_j, batch_size=1, shuffle=True) for i in range(0, self._num_imgs_to_save): #pdb.set_trace() print("Saving image {}/{}...".format(i, self._num_imgs_to_save)) self.image_a, self.image_b = next(input_iter) tmp_imgA = self.get_fake_image_pool(self.num_fake_inputs, self.fake_images_A) self.fake_pool_A_mask = tmp_imgA["mask"] self.fake_pool_A = tmp_imgA["im"] tmp_imgB = self.get_fake_image_pool(self.num_fake_inputs, self.fake_images_B) self.fake_pool_B_mask = tmp_imgB["mask"] self.fake_pool_B = tmp_imgB["im"] self.transition_rate = np.array([curr_tr], dtype=np.float32) self.donorm = np.array([donorm], dtype=np.float32) self.output_converter( model.get_outputs(self.input_converter(), nets)) figures_to_save = [ self.image_a, self.image_b, self.fake_images_b, self.fake_images_a, self.cycle_images_a, self.cycle_images_b, self.masks[0], self.masks[1] ] self.figure_writer(figures_to_save, names, v_html, epoch, i, html_mode=0)
def model_setup(self): self.input_a = tf.placeholder( tf.float32, [ 1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS + 1 ], name="input_A") self.input_b = tf.placeholder( tf.float32, [ 1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS ], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [ None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS + 1 ], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [ None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS ], name="fake_pool_B") self.global_step = slim.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, } outputs = model.get_outputs( inputs, network=self._network_version) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real']
def test_evaluate_g(sess): x_val = np.ones_like(np.random.randn(1, 16, 16, 3)).astype(np.float32) for i in range(16): for j in range(16): for k in range(3): x_val[0][i][j][k] = ((i + j + k) % 2) / 2.0 inputs = { 'images_a': tf.stack(x_val), 'images_b': tf.stack(x_val), 'fake_pool_a': tf.zeros([1, 16, 16, 3]), 'fake_pool_b': tf.zeros([1, 16, 16, 3]), } outputs = model.get_outputs(inputs) sess.run(tf.global_variables_initializer()) assert sess.run(outputs['fake_images_a'][0][5][7][0]) == 5
def test_output_sizes(): images_size = [ utils.BATCH_SIZE, utils.IMG_HEIGHT, utils.IMG_WIDTH, utils.IMG_CHANNEL, ] pool_size = [ utils.POOL_SIZE, utils.IMG_HEIGHT, utils.IMG_WIDTH, utils.IMG_CHANNEL, ] inputs = { 'images_a': tf.ones(images_size), 'images_b': tf.ones(images_size), 'fake_pool_a': tf.ones(pool_size), 'fake_pool_b': tf.ones(pool_size), } outputs = model.get_outputs(inputs) assert outputs['prob_real_a_is_real'].get_shape().as_list() == [ utils.BATCH_SIZE, 32, 32, 1, ] assert outputs['prob_real_b_is_real'].get_shape().as_list() == [ utils.BATCH_SIZE, 32, 32, 1, ] assert outputs['prob_fake_a_is_real'].get_shape().as_list() == [ utils.BATCH_SIZE, 32, 32, 1, ] assert outputs['prob_fake_b_is_real'].get_shape().as_list() == [ utils.BATCH_SIZE, 32, 32, 1, ] assert outputs['prob_fake_pool_a_is_real'].get_shape().as_list() == [ utils.POOL_SIZE, 32, 32, 1, ] assert outputs['prob_fake_pool_b_is_real'].get_shape().as_list() == [ utils.POOL_SIZE, 32, 32, 1, ] assert outputs['cycle_images_a'].get_shape().as_list() == images_size assert outputs['cycle_images_b'].get_shape().as_list() == images_size
def model_setup(self): self.input_a = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 3], name="input_A") self.input_b = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 3], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="fake_pool_B") self.gt_a = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 4], name="gt_A") self.keep_rate = tf.placeholder(tf.float32, shape=()) self.is_training = tf.placeholder(tf.bool, shape=()) self.global_step = tf.train.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate_gan = tf.placeholder(tf.float32, shape=[], name="lr_gan") self.learning_rate_seg = tf.placeholder(tf.float32, shape=[], name="lr_seg") self.lr_gan_summ = tf.summary.scalar("lr_gan", self.learning_rate_gan) self.lr_seg_summ = tf.summary.scalar("lr_seg", self.learning_rate_seg) inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, } outputs = model.get_outputs(inputs, skip=self._skip, is_training=self.is_training, keep_rate=None) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real'] self.pred_mask_b = outputs['pred_mask_b'] self.pred_mask_fake_b = outputs['pred_mask_fake_b'] self.prob_fea_fake_b_is_real = outputs['prob_fea_fake_b_is_real'] self.prob_fea_b_is_real = outputs['prob_fea_b_is_real'] self.prob_real_a_aux = outputs['prob_real_a_aux'] self.prob_fake_a_aux_is_real = outputs['prob_fake_a_aux_is_real'] self.prob_fake_pool_a_aux_is_real = outputs[ 'prob_fake_pool_a_aux_is_real'] self.prob_cycle_a_is_real = outputs['prob_cycle_a_is_real'] self.prob_cycle_a_aux_is_real = outputs['prob_cycle_a_aux_is_real']
def model_setup(self): """Set up the model to train.""" self.input_a = tf.placeholder(tf.float32, [ 1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS, ], name="input_A") self.input_b = tf.placeholder(tf.float32, [ 1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS, ], name="input_B") self.fake_images_A = np.zeros((self._pool_size, 1, model.IMG_HEIGHT, model.IMG_WIDTH, model.IMG_CHANNELS), ) self.fake_images_B = np.zeros((self._pool_size, 1, model.IMG_HEIGHT, model.IMG_WIDTH, model.IMG_CHANNELS), ) self.fake_pool_A = tf.placeholder(tf.float32, [ None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS, ], name="fake_pool_A") self.fake_pool_B = tf.placeholder(tf.float32, [ None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS, ], name="fake_pool_B") self.global_step = slim.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, } outputs = model.get_outputs( inputs, variable_scope='img2img', num_separate_layers=self._num_separate_layers_g, num_separate_layers_d=self._num_separate_layers_d, num_no_skip_layers=self._num_no_skip_layers, network_structure=self._network_structure) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real'] self.ae_images_a = outputs['ae_images_a'] self.ae_images_b = outputs['ae_images_b']
def train(self): """ Training Function. We use batch size = 1 for training """ # Build the network and compute losses nets = self.model_setup() summary_writer = tf.summary.create_file_writer( os.path.join(self._output_dir, "log")) summary_writer.set_as_default() max_images = cyclegan_datasets.DATASET_TO_SIZES[self._dataset_name] half_training_ep = int(self._max_step / 2) # Restore the model to run the model from last checkpoint print("Loading the latest checkpoint...") if self._to_restore: checkpoint_name = os.path.join(self._checkpoint_dir, self._checkpoint_name) utils.load(checkpoint_name, nets=nets) self.global_step = int(checkpoint_name[-2:]) else: self.global_step = 0 exists_or_mkdir(self._output_dir) self.upd_fake_image_pool(self.num_fake_inputs, self.fake_pool_A, self.fake_pool_A_mask, self.fake_images_A) self.upd_fake_image_pool(self.num_fake_inputs, self.fake_pool_B, self.fake_pool_B_mask, self.fake_images_B) self.num_fake_inputs += 1 # Training Loop for epoch in range(self.global_step, self._max_step): print("In the epoch ", epoch) print("Saving the latest checkpoint...") utils.save(nets, os.path.join(self._output_dir, "AGGAN_%02d" % epoch)) # Setting lr curr_lr = self._base_lr if epoch >= half_training_ep: curr_lr -= self._base_lr * ( epoch - half_training_ep) / half_training_ep self.g_A_trainer.learning_rate = curr_lr self.g_B_trainer.learning_rate = curr_lr self.g_A_trainer_bis.learning_rate = curr_lr self.g_B_trainer_bis.learning_rate = curr_lr self.d_A_trainer.learning_rate = curr_lr self.d_B_trainer.learning_rate = curr_lr if epoch < self._switch: curr_tr = 0 donorm = True to_train_A = self.g_A_trainer to_train_B = self.g_B_trainer to_train_A_vars = self.g_A_vars + self.g_Ae_vars to_train_B_vars = self.g_B_vars + self.g_Be_vars else: curr_tr = self._threshold_fg donorm = False to_train_A = self.g_A_trainer_bis to_train_B = self.g_B_trainer_bis to_train_A_vars = self.g_A_vars to_train_B_vars = self.g_B_vars print("Loading data...") tot_inputs = data_loader.load_data(self._dataset_name, self._size_before_crop, False, self._do_flipping) self.inputs_img_i = tot_inputs['images_i'] self.inputs_img_j = tot_inputs['images_j'] assert (len(self.inputs_img_i) == len(self.inputs_img_j) and max_images == len(self.inputs_img_i)) self.save_images(nets, epoch, curr_tr, self.inputs_img_i, self.inputs_img_j) nets = utils.set_mode(nets, "train") input_iter = minibatches(self.inputs_img_i, self.inputs_img_j, batch_size=1, shuffle=True) for i in range(max_images): print("Processing batch {}/{} in {}th epoch".format( i, max_images, epoch)) self.image_a, self.image_b = next(input_iter) tmp_imgA = self.get_fake_image_pool(self.num_fake_inputs, self.fake_images_A) self.fake_pool_A_mask = tmp_imgA["mask"] self.fake_pool_A = tmp_imgA["im"] tmp_imgB = self.get_fake_image_pool(self.num_fake_inputs, self.fake_images_B) self.fake_pool_B_mask = tmp_imgB["mask"] self.fake_pool_B = tmp_imgB["im"] self.transition_rate = np.array([curr_tr], dtype=np.float32) self.donorm = np.array([donorm], dtype=np.float32) with tf.GradientTape(persistent=True) as tape: self.output_converter( model.get_outputs(self.input_converter(), nets)) self.upd_fake_image_pool(self.num_fake_inputs, self.fake_images_b, self.masks[0], self.fake_images_B) self.upd_fake_image_pool(self.num_fake_inputs, self.fake_images_a, self.masks[1], self.fake_images_A) self.compute_losses() #pdb.set_trace() grad = tape.gradient(self.d_B_loss, self.d_B_vars) self.d_B_trainer.apply_gradients(zip(grad, self.d_B_vars)) grad = tape.gradient(self.d_A_loss, self.d_A_vars) self.d_A_trainer.apply_gradients(zip(grad, self.d_A_vars)) grad = tape.gradient(self.g_A_loss, to_train_A_vars) to_train_A.apply_gradients(zip(grad, to_train_A_vars)) grad = tape.gradient(self.g_B_loss, to_train_B_vars) to_train_B.apply_gradients(zip(grad, to_train_B_vars)) tot_loss = self.g_A_loss + self.g_B_loss + self.d_A_loss + self.d_B_loss print("[training_info] g_A_loss = {}, g_B_loss = {}, d_A_loss = {}, d_B_loss = {}, \ tot_loss = {}, lr={}, curr_tr={}" .format(self.g_A_loss, self.g_B_loss, self.d_A_loss, \ self.d_B_loss, tot_loss, curr_lr, curr_tr)) tf.summary.scalar('g_A_loss', self.g_A_loss, step=self.global_step * max_images + i) tf.summary.scalar('g_B_loss', self.g_B_loss, step=self.global_step * max_images + i) tf.summary.scalar('d_A_loss', self.d_A_loss, step=self.global_step * max_images + i) tf.summary.scalar('d_B_loss', self.d_B_loss, step=self.global_step * max_images + i) tf.summary.scalar('learning_rate', to_train_A.learning_rate, step=self.global_step * max_images + i) tf.summary.scalar('total_loss', tot_loss, step=self.global_step * max_images + i) self.num_fake_inputs += 1 self.global_step = epoch + 1 summary_writer.flush()
def save_images_bis(self, nets, epoch, images_i, images_j): """ Saves input and output images. """ names = [ 'input_A_', 'mask_A_', 'masked_inputA_', 'fakeB_', 'input_B_', 'mask_B_', 'masked_inputB_', 'fakeA_' ] space = '                        ' \ '                        ' \ '         ' nets = utils.set_mode(nets, "eval") #pdb.set_trace() exists_or_mkdir(self._images_dir) with open( os.path.join(self._output_dir, 'results_' + str(epoch) + '.html'), 'w') as v_html: v_html.write("<b>Inputs" + space + "Masks" + space + "Masked_images" + space + "Generated_images</b>") v_html.write("<br>") input_iter = minibatches(images_i, images_j, batch_size=1, shuffle=True) for i in range(0, self._num_imgs_to_save): print("Saving image {}/{}...".format(i, self._num_imgs_to_save)) #pdb.set_trace() self.image_a, self.image_b = next(input_iter) tmp_imgA = self.get_fake_image_pool(self.num_fake_inputs, self.fake_images_A) self.fake_pool_A_mask = tmp_imgA["mask"] self.fake_pool_A = tmp_imgA["im"] tmp_imgB = self.get_fake_image_pool(self.num_fake_inputs, self.fake_images_B) self.fake_pool_B_mask = tmp_imgB["mask"] self.fake_pool_B = tmp_imgB["im"] self.transition_rate = np.array([0.1], dtype=np.float32) self.donorm = np.array([True], dtype=np.float32) self.output_converter( model.get_outputs(self.input_converter(), nets)) figures_to_save = [ self.image_a, self.masks[0], self.masked_ims[0], self.fake_images_b, self.image_b, self.masks[1], self.masked_ims[1], self.fake_images_a ] self.figure_writer(figures_to_save, names, v_html, epoch, i, html_mode=1)
def model_setup(self): self.input_a = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 3], name="input_A") self.input_b = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 3], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="fake_pool_B") self.gt_a = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 5], name="gt_A") self.gt_b = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 5], name="gt_B") self.global_step = tf.train.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") self.learning_rate_seg = tf.placeholder(tf.float32, shape=[], name="lr_seg") self.lr_gan_summ = tf.summary.scalar("lr_gan", self.learning_rate) self.lr_seg_summ = tf.summary.scalar("lr_seg", self.learning_rate_seg) inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, 'gt_a': self.gt_a, } outputs = model.get_outputs(inputs, skip=self._skip, is_training=self.is_training, keep_rate=self.keep_rate) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real'] self.pred_mask_a = outputs['pred_mask_a'] self.pred_mask_b = outputs['pred_mask_b'] self.pred_mask_fake_a = outputs['pred_mask_fake_a'] self.pred_mask_fake_b = outputs['pred_mask_fake_b'] self.prob_fea_fake_b_is_real = outputs['prob_fea_fake_b_is_real'] self.prob_fea_b_is_real = outputs['prob_fea_b_is_real'] self.prob_real_a_aux = outputs['prob_real_a_aux'] self.prob_fake_a_aux_is_real = outputs['prob_fake_a_aux_is_real'] self.prob_fake_pool_a_aux_is_real = outputs[ 'prob_fake_pool_a_aux_is_real'] self.prob_cycle_a_is_real = outputs['prob_cycle_a_is_real'] self.prob_cycle_a_aux_is_real = outputs['prob_cycle_a_aux_is_real'] self.predicter_fake_b = pixel_wise_softmax_2(self.pred_mask_fake_b) self.compact_pred_fake_b = tf.argmax(self.predicter_fake_b, 3) self.compact_y_fake_b = tf.argmax(self.gt_a, 3) self.confusion_matrix_fake_b = tf.confusion_matrix( tf.reshape(self.compact_y_fake_b, [-1]), tf.reshape(self.compact_pred_fake_b, [-1]), num_classes=self._num_cls) self.predicter_b = pixel_wise_softmax_2(self.pred_mask_b) self.compact_pred_b = tf.argmax(self.predicter_b, 3) self.compact_y_b = tf.argmax(self.gt_b, 3) self.confusion_matrix_b = tf.confusion_matrix( tf.reshape(self.compact_y_b, [-1]), tf.reshape(self.compact_pred_b, [-1]), num_classes=self._num_cls)
def model_setup(self): self.input_a = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="input_A") self.input_b = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="fake_pool_B") self.gt_a = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, self._num_cls], name="gt_A") self.gt_b = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, self._num_cls], name="gt_B") # for validation only, not used during training self.keep_rate = tf.placeholder(tf.float32, shape=()) self.is_training = tf.placeholder(tf.bool, shape=()) self.num_fake_inputs = 0 self.learning_rate_gan = tf.placeholder(tf.float32, shape=[], name="lr_gan") self.learning_rate_seg = tf.placeholder(tf.float32, shape=[], name="lr_seg") self.lr_gan_summ = tf.summary.scalar("lr_gan", self.learning_rate_gan) self.lr_seg_summ = tf.summary.scalar("lr_seg", self.learning_rate_seg) inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, } outputs = model.get_outputs(inputs, skip=self._skip, is_training=self.is_training, keep_rate=self.keep_rate) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real'] self.pred_mask_a = outputs['pred_mask_a'] self.pred_mask_b = outputs['pred_mask_b'] self.pred_mask_b_ll = outputs['pred_mask_b_ll'] self.pred_mask_fake_a = outputs['pred_mask_fake_a'] self.pred_mask_fake_b = outputs['pred_mask_fake_b'] self.pred_mask_fake_b_ll = outputs['pred_mask_fake_b_ll'] self.prob_pred_mask_fake_b_is_real = outputs[ 'prob_pred_mask_fake_b_is_real'] self.prob_pred_mask_b_is_real = outputs['prob_pred_mask_b_is_real'] self.prob_pred_mask_fake_b_ll_is_real = outputs[ 'prob_pred_mask_fake_b_ll_is_real'] self.prob_pred_mask_b_ll_is_real = outputs[ 'prob_pred_mask_b_ll_is_real'] self.prob_fake_a_aux_is_real = outputs['prob_fake_a_aux_is_real'] self.prob_fake_pool_a_aux_is_real = outputs[ 'prob_fake_pool_a_aux_is_real'] self.prob_cycle_a_aux_is_real = outputs['prob_cycle_a_aux_is_real'] self.predicter_fake_b = tf.nn.softmax(self.pred_mask_fake_b) self.compact_pred_fake_b = tf.argmax(self.predicter_fake_b, 3) self.compact_y_fake_b = tf.argmax(self.gt_a, 3) self.predicter_b = tf.nn.softmax(self.pred_mask_b) self.compact_pred_b = tf.argmax(self.predicter_b, 3) self.compact_y_b = tf.argmax(self.gt_b, 3) self.dice_fake_b_arr = dice_eval(self.compact_pred_fake_b, self.gt_a, self._num_cls) self.dice_fake_b_mean = tf.reduce_mean(self.dice_fake_b_arr) self.dice_fake_b_mean_summ = tf.summary.scalar("dice_fake_b", self.dice_fake_b_mean) self.dice_b_arr = dice_eval(self.compact_pred_b, self.gt_b, self._num_cls) self.dice_b_mean = tf.reduce_mean(self.dice_b_arr) self.dice_b_mean_summ = tf.summary.scalar("dice_b", self.dice_b_mean)
def model_setup(self): """ This function sets up the model to train. self.input_A/self.input_B -> Set of training images. self.fake_A/self.fake_B -> Generated images by corresponding generator of input_A and input_B self.lr -> Learning rate variable self.cyc_A/ self.cyc_B -> Images generated after feeding self.fake_A/self.fake_B to corresponding generator. This is use to calculate cyclic loss """ self.input_a = tf.placeholder( tf.float32, [1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="input_A") self.input_b = tf.placeholder( tf.float32, [1, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="input_B") self.fake_pool_A = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="fake_pool_A") self.fake_pool_B = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="fake_pool_B") self.fake_pool_A_mask = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="fake_pool_A_mask") self.fake_pool_B_mask = tf.placeholder( tf.float32, [None, model.IMG_WIDTH, model.IMG_HEIGHT, model.IMG_CHANNELS], name="fake_pool_B_mask") ''' Real Mask placeholder ''' self.input_a_mask = tf.placeholder( tf.float32, [1, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="input_A_mask") self.input_b_mask = tf.placeholder( tf.float32, [1, model.IMG_WIDTH, model.IMG_HEIGHT, 1], name="input_B_mask") self.global_step = slim.get_or_create_global_step() self.num_fake_inputs = 0 self.learning_rate = tf.placeholder(tf.float32, shape=[], name="lr") self.transition_rate = tf.placeholder(tf.float32, shape=[], name="tr") self.donorm = tf.placeholder(tf.bool, shape=[], name="donorm") inputs = { 'images_a': self.input_a, 'images_b': self.input_b, 'fake_pool_a': self.fake_pool_A, 'fake_pool_b': self.fake_pool_B, 'fake_pool_a_mask': self.fake_pool_A_mask, 'fake_pool_b_mask': self.fake_pool_B_mask, 'images_a_mask': self.input_a_mask, 'images_b_mask': self.input_b_mask, 'transition_rate': self.transition_rate, 'donorm': self.donorm, } outputs = model.get_outputs(inputs, skip=self._skip) self.prob_real_a_is_real = outputs['prob_real_a_is_real'] self.prob_real_b_is_real = outputs['prob_real_b_is_real'] self.fake_images_a = outputs['fake_images_a'] self.fake_images_b = outputs['fake_images_b'] self.prob_fake_a_is_real = outputs['prob_fake_a_is_real'] self.prob_fake_b_is_real = outputs['prob_fake_b_is_real'] self.cycle_images_a = outputs['cycle_images_a'] self.cycle_images_b = outputs['cycle_images_b'] self.prob_fake_pool_a_is_real = outputs['prob_fake_pool_a_is_real'] self.prob_fake_pool_b_is_real = outputs['prob_fake_pool_b_is_real'] self.masks = outputs['masks'] self.masked_gen_ims = outputs['masked_gen_ims'] self.masked_ims = outputs['masked_ims'] self.masks_ = outputs['mask_tmp']