def learning_rate(self): # the learning rate is a function for the global step, so we could # define is entirely in tf ops, instead of a placeholder and feeding. with tf.device('/cpu:0'): lr = tf.placeholder(tf.float32, shape=[], name='learning_rate') self.summaries.append(scalar_summary('lr', lr)) return lr
def build_model(self): if self.y_dim: self.y = tf.placeholder( tf.float32, [self.batch_size, self.y_dim], name='y') else: self.y = None if self.crop: image_dims = [self.output_height, self.output_width, self.c_dim] else: image_dims = [self.input_height, self.input_width, self.c_dim] self.inputs = tf.placeholder( tf.float32, [self.batch_size] + image_dims, name='real_images') inputs = self.inputs self.z = tf.placeholder( tf.float32, [None, self.z_dim], name='z') self.z_sum = histogram_summary("z", self.z) self.G = self.generator(self.z, self.y) self.D, self.D_logits = self.discriminator(inputs, self.y, reuse=False) self.sampler = self.sampler(self.z, self.y) self.D_, self.D_logits_ = self.discriminator( self.G, self.y, reuse=True) self.d_sum = histogram_summary("d", self.D) self.d__sum = histogram_summary("d_", self.D_) self.G_sum = image_summary("G", self.G) def sigmoid_cross_entropy_with_logits(x, y): try: return tf.nn.sigmoid_cross_entropy_with_logits( logits=x, labels=y) except: return tf.nn.sigmoid_cross_entropy_with_logits( logits=x, targets=y) self.d_loss_real = tf.reduce_mean( sigmoid_cross_entropy_with_logits( self.D_logits, tf.ones_like(self.D))) self.d_loss_fake = tf.reduce_mean( sigmoid_cross_entropy_with_logits( self.D_logits_, tf.zeros_like(self.D_))) self.g_loss = tf.reduce_mean( sigmoid_cross_entropy_with_logits( self.D_logits_, tf.ones_like(self.D_))) self.d_loss_real_sum = scalar_summary("d_loss_real", self.d_loss_real) self.d_loss_fake_sum = scalar_summary("d_loss_fake", self.d_loss_fake) self.d_loss = self.d_loss_real + self.d_loss_fake self.g_loss_sum = scalar_summary("g_loss", self.g_loss) self.d_loss_sum = scalar_summary("d_loss", self.d_loss) t_vars = tf.trainable_variables() self.d_vars = [var for var in t_vars if 'd_' in var.name] self.g_vars = [var for var in t_vars if 'g_' in var.name] self.saver = tf.train.Saver()
def build_model(self): if self.y_dim: self.y = tf.placeholder(tf.float32, [self.batch_size, self.y_dim], name='y') if self.crop: image_dims = [self.output_height, self.output_width, self.c_dim] else: image_dims = [self.input_height, self.input_width, self.c_dim] self.inputs = tf.placeholder(tf.float32, [self.batch_size] + image_dims, name='real_images') #输入discriminator真实图像placeholder inputs = self.inputs #输入generator噪声值placeholder self.z = tf.placeholder(tf.float32, [None, self.z_dim], name='z') self.z_sum = ops.histogram_summary('z', self.z) if self.y_dim: self.G = self.generator(self.z, self.y) self.D, self.D_logits = self.discriminator(inputs, self.y, reuse=False) self.sampler = self.sampler(self.z, self.y) self.D_, self.D_logits_ = self.discriminator(self.G, self.y, reuse=True) else: self.G = self.generator(self.z) self.D, self.D_logits = self.discriminator(inputs) self.sampler = self.sampler(self.z) self.D_, self.D_logits_ = self.discriminator(self.G, reuse=True) self.d_sum = ops.histogram_summary('d', self.D) self.d__sum = ops.histogram_summary('d_', self.D_) self.G_sum = ops.image_summary('G', self.G) def sigmoid_cross_entropy_with_logits(x, y): try: return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, labels=y) except: return tf.nn.sigmoid_cross_entropy_with_logits(logits=x, targets=y) #输入真实图像时,discriminator 损失值(label都为 1 ) self.d_loss_real = tf.reduce_sum( sigmoid_cross_entropy_with_logits(self.D_logits, tf.ones_like(self.D))) #输入generator结果时,discriminator损失值(label都为0) self.d_loss_fake = tf.reduce_mean( sigmoid_cross_entropy_with_logits(self.D_logits_, tf.zeros_like(self.D_))) #以discriminator输出计算generator损失值,要求discriminator都判别为真(label为1) self.g_loss = tf.reduce_mean( sigmoid_cross_entropy_with_logits(self.D_logits_, tf.ones_like(self.D_))) self.d_loss_real_sum = ops.scalar_summary('d_loss_real', self.d_loss_real) self.d_loss_fake_sum = ops.scalar_summary('d_loss_fake', self.d_loss_fake) #discriminator总损失值 self.d_loss = self.d_loss_real + self.d_loss_fake self.g_loss_sum = ops.scalar_summary('g_loss', self.g_loss) self.d_loss_sum = ops.scalar_summary('d_loss', self.d_loss) t_vars = tf.trainable_variables() self.d_vars = [var for var in t_vars if 'd_' in var.name] self.g_vars = [var for var in t_vars if 'g_' in var.name] self.saver = tf.train.Saver()
def create_model(self, obj_UserModel, stage_scope, batch_x=None): logging.debug('Stage: {}'.format(stage_scope)) # get batch data if batch_x is None: self.init_dataloader() batch_x = self.dataloader.batch_x if self.stage != utils.STAGE_INF: batch_y = self.dataloader.batch_y else: assert self.stage == utils.STAGE_INF batch_x = batch_x logging.debug('batch_x shape={}'.format(batch_x.get_shape().as_list())) if self.stage != utils.STAGE_INF: logging.debug('batch_y shape={}'.format( batch_y.get_shape().as_list())) # get avilable gpu list available_devices = utils.get_available_gpus() logging.debug('GPUs {}'.format(available_devices)) if not available_devices: available_devices.append('/cpu:0') # available_devices = ['/gpu:0', '/gpu:1'] # DEVELOPMENT: virtual multi-gpu # Split the batch over the batch dimension over the number of available gpu's if len(available_devices) == 1: batch_x_split = [batch_x] if self.stage != utils.STAGE_INF: # Has no labels batch_y_split = [batch_y] else: with tf.name_scope('parallelize'): # Split them up batch_x_split = tf.split(batch_x, len(available_devices), 0, name='split_batch_x') if self.stage != utils.STAGE_INF: # Has no labels batch_y_split = tf.split(batch_y, len(available_devices), 0, name='split_batch_y') # Run the user model through the build_model function that should be filled in # collect all type of lossess and all gpus grad_towers = [] #with tf.variable_scope(tf.get_variable_scope()): for dev_i, dev_name in enumerate(available_devices): with tf.device(dev_name): current_scope = stage_scope if len( available_devices) == 1 else ('tower_{}'.format(dev_i)) with tf.name_scope(current_scope) as scope_tower: with tf.variable_scope(utils.GraphKeys.MODEL, reuse=dev_i > 0 or self._reuse): if self.stage != utils.STAGE_INF: tower_model = self.add_tower( obj_tower=obj_UserModel, x=batch_x_split[dev_i], y=batch_y_split[dev_i]) else: tower_model = self.add_tower( obj_tower=obj_UserModel, x=batch_x_split[dev_i], y=None) tower_model.inference # touch to initialize # Reuse the variables int this scope for the next tower/device tf.get_variable_scope().reuse_variables() if self.stage == utils.STAGE_INF: # For inferencing we will only use the inference part of the graph continue with tf.name_scope(utils.GraphKeys.LOSS): for loss in self.get_tower_losses(tower_model): tf.add_to_collection(utils.GraphKeys.LOSSES, loss['loss']) # Assemble all made within this scope so far. The user can add custom # losses to the utils.GraphKeys.LOSSES collection losses = tf.get_collection(utils.GraphKeys.LOSSES, scope=scope_tower) #logging.debug('get_collection: graykeys.LOSSES : {}'.format(losses)) #logging.debug('get_collection: graykeys.REGULARIZATION_LOSSES : {}'.format(ops.get_collection(ops.GraphKeys.REGULARIZATION_LOSSES, scope=None))) losses += ops.get_collection( ops.GraphKeys.REGULARIZATION_LOSSES, scope=None) tower_loss = tf.add_n(losses, name='loss') self.summaries.append( scalar_summary(tower_loss.op.name, tower_loss)) if self.stage == utils.STAGE_TRAIN: # collect all type of losses on the gpu grad_tower_losses = [] # for each type loss for loss in self.get_tower_losses(tower_model): # compute gradients of this gpu grad_tower_loss = self.optimizer.compute_gradients( loss['loss'], loss['vars']) grad_tower_loss = tower_model.gradientUpdate( grad_tower_loss) grad_tower_losses.append(grad_tower_loss) grad_towers.append(grad_tower_losses) # Assemble and average the gradients from all towers if self.stage == utils.STAGE_TRAIN: n_gpus = len(available_devices) if n_gpus == 1: grad_averages = grad_towers[0] else: with tf.device(available_devices[0]): n_losses = len(grad_towers[0]) grad_averages = [] # for each loss, averages loss on all gpus for loss in xrange(n_losses): grad_averages.append( average_gradients([ grad_towers[gpu][loss] for gpu in xrange(n_gpus) ])) apply_gradient_ops = [] for grad_avg in grad_averages: # apply average gradients apply_gradient_ops.append( self.optimizer.apply_gradients( grad_avg, global_step=self.global_step)) # train op, list self._train = apply_gradient_ops
def build_graph_with_losses(self, real, config, training=True, summary=False, reuse=False): real = real / 127.5 - 1. mask = random_mask(config, name='mask_input') x = real * (1. - mask) x1, x2, offset_flow = self.build_generator(x, mask, config, reuse=reuse, training=training) fake = x2 losses = {} # apply mask and reconstruct fake_patched = fake * mask + x * (1. - mask) coarse_alpha = config.COARSE_ALPHA losses['l1_loss'] = coarse_alpha * tf.reduce_mean( tf.abs(real - x1) * mask) losses['l1_loss'] += tf.reduce_mean(tf.abs(real - x2) * mask) losses['ae_loss'] = coarse_alpha * tf.reduce_mean( tf.abs(real - x1) * (1. - mask)) losses['ae_loss'] += tf.reduce_mean(tf.abs(real - x2) * (1. - mask)) losses['ae_loss'] /= tf.reduce_mean(1. - mask) if summary: viz_img = [real, x, x1, x2, fake_patched] if offset_flow is not None: viz_img.append( resize(offset_flow, to_shape=config.IMG_SHAPE[0:2], func=tf.image.resize_nearest_neighbor)) images_summary(tf.concat(viz_img, axis=2), 'train_real_x_x1_x2_result_flow', config.VIZ_MAX_OUT) # gan real_fake = tf.concat([real, fake_patched], axis=0) if config.GAN_WITH_MASK: real_fake = tf.concat( [real_fake, tf.tile(mask, [config.BATCH_SIZE * 2, 1, 1, 1])], axis=3) # gan loss D_real_fake = self.build_discriminator(real_fake, training=training, reuse=reuse, nc=config.DIS_NC) D_real, D_fake = tf.split(D_real_fake, 2) g_loss, d_loss = gan_wgan_loss(D_real, D_fake, name='gan_loss') losses['g_loss'] = g_loss losses['d_loss'] = d_loss # gp interps = random_interpolates(real, fake_patched) D_interps = self.build_discriminator(interps, reuse=True, nc=config.DIS_NC) # apply gp gp_loss = gradients_penalty(interps, D_interps, mask=mask) losses['gp_loss'] = config.WGAN_GP_LAMBDA * gp_loss losses['d_loss'] = losses['d_loss'] + losses['gp_loss'] if summary: gradients_summary(g_loss, fake, name='g_loss_to_fake') scalar_summary('d_loss_with_gp', losses['d_loss']) scalar_summary('d_loss', d_loss) scalar_summary('g_loss', g_loss) scalar_summary('d_loss', d_loss) scalar_summary('l1', losses['l1_loss']) scalar_summary('ae', losses['ae_loss']) losses['g_loss'] = config.GAN_LOSS_ALPHA * losses['g_loss'] losses['g_loss'] += config.L1_LOSS_ALPHA * losses['l1_loss'] losses['g_loss'] += config.AE_LOSS_ALPHA * losses['ae_loss'] g_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'generator') d_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, 'discriminator') return g_vars, d_vars, losses