def total_loss(self,weight_decay=1e-4): rpn_loss=self._roi_loss() roi_loss=self._roi_loss() if weight_decay>0: weight_loss = [regularize_cost('.*/W', l2_regularizer(weight_decay), name='wd_cost')] else: weight_loss=[] total_loss = tf.add_n([rpn_loss] + [roi_loss] + weight_loss, 'total_cost') return total_loss,rpn_loss,roi_loss,weight_loss
def build_graph(self, *args): costs = [] for i, name in enumerate(self.agent_names): joint_state, action, reward, isOver, comb_mask, joint_fine_mask = args[i * 6:(i + 1) * 6] with tf.variable_scope(name): with conditional(name is None, varreplace.freeze_variables()): state = tf.identity(joint_state[:, 0, :, :, :], name='state') fine_mask = tf.identity(joint_fine_mask[:, 0, :], name='fine_mask') self.predict_value = self.get_DQN_prediction(state, comb_mask, fine_mask) if not get_current_tower_context().is_training: continue # reward = tf.clip_by_value(reward, -1, 1) next_state = tf.identity(joint_state[:, 1, :, :, :], name='next_state') next_fine_mask = tf.identity(joint_fine_mask[:, 1, :], name='next_fine_mask') action_onehot = tf.one_hot(action, self.num_actions, 1.0, 0.0) pred_action_value = tf.reduce_sum(self.predict_value * action_onehot, 1) # N, max_pred_reward = tf.reduce_mean(tf.reduce_max( self.predict_value, 1), name='predict_reward') summary.add_moving_summary(max_pred_reward) with tf.variable_scope('target'), varreplace.freeze_variables(skip_collection=True): # we are alternating between comb and fine states targetQ_predict_value = self.get_DQN_prediction(next_state, tf.logical_not(comb_mask), next_fine_mask) # NxA if self.method != 'Double': # DQN best_v = tf.reduce_max(targetQ_predict_value, 1) # N, else: # Double-DQN next_predict_value = self.get_DQN_prediction(next_state, tf.logical_not(comb_mask), next_fine_mask) self.greedy_choice = tf.argmax(next_predict_value, 1) # N, predict_onehot = tf.one_hot(self.greedy_choice, self.num_actions, 1.0, 0.0) best_v = tf.reduce_sum(targetQ_predict_value * predict_onehot, 1) target = reward + (1.0 - tf.cast(isOver, tf.float32)) * self.gamma * tf.stop_gradient(best_v) # target = tf.Print(target, [target], summarize=100) # tf.assert_greater(target, -100., message='target error') # tf.assert_greater(pred_action_value, -100., message='pred value error') # pred_action_value = tf.Print(pred_action_value, [pred_action_value], summarize=100) l2_loss = tensorpack.regularize_cost(name + '/dqn.*W{1}', l2_regularizer(1e-3)) # cost = tf.losses.mean_squared_error(target, pred_action_value) with tf.control_dependencies([tf.assert_greater(target, -100., message='target error'), tf.assert_greater(pred_action_value, -100., message='pred value error')]): cost = tf.losses.huber_loss( target, pred_action_value, reduction=tf.losses.Reduction.MEAN) summary.add_param_summary((name + '.*/W', ['histogram', 'rms'])) # monitor all W summary.add_moving_summary(cost) costs.append(cost) if not get_current_tower_context().is_training: return return tf.add_n([costs[i] * self.cost_weights[i] for i in range(3)])
def build_graph(self, image: Any, label: Any) -> Any: """ This function builds the model which takes the input variables and returns cost. """ # In tensorflow, inputs to convolution function are assumed to be NHWC. # Add a single channel here. image = tf.expand_dims(image, 3) # Center the pixels values at zero. image = image * 2 - 1 # The context manager `argscope` sets the default option for all the layers under # this context. Here we use 32 channel convolution with shape 3x3. with tp.argscope(tp.Conv2D, kernel_size=3, activation=tf.nn.relu, filters=self.hparams["n_filters"]): logits = (tp.LinearWrap(image).Conv2D("conv0").MaxPooling( "pool0", 2).Conv2D("conv1").MaxPooling("pool1", 2).FullyConnected( "fc0", 512, activation=tf.nn.relu).Dropout( "dropout", rate=0.5).FullyConnected("fc1", 10, activation=tf.identity)()) cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label) cost = tf.reduce_mean( cost, name="cross_entropy_loss") # the average cross-entropy loss correct = tf.cast(tf.nn.in_top_k(predictions=logits, targets=label, k=1), tf.float32, name="correct") accuracy = tf.reduce_mean(correct, name="accuracy") train_error = tf.reduce_mean(1 - correct, name="train_error") tp.summary.add_moving_summary(train_error, accuracy) # Use a regex to find parameters to apply weight decay. # Here we apply a weight decay on all W (weight matrix) of all fc layers. wd_cost = tf.multiply( self.hparams["weight_cost"], tp.regularize_cost("fc.*/W", tf.nn.l2_loss), name="regularize_loss", ) total_cost = tf.add_n([wd_cost, cost], name="loss") return total_cost
def build_graph(self, image, label): image = image / 128.0 assert tf.test.is_gpu_available() with tf.variable_scope(self._name): x = ScaleNormConv2D(image, 16, 3, 1, name="conv_input") # shape = [batchsize, 32, 32, 16] x = CifarResNet.build_group(x, self._n, 16, stride=1, mult_decay=self._mult_decay, name="g1") # shape = [batchsize, 16, 16, 32] x = CifarResNet.build_group(x, self._n, 32, stride=2, mult_decay=self._mult_decay, name="g2") # shape = [batchsize, 8, 8, 64] x = CifarResNet.build_group(x, self._n, 64, stride=2, mult_decay=self._mult_decay, name="g3") # normalise the final output by the accumulated multiplier #x = BatchNorm("bn_last", x, epsilon=EPSILON, center=False, scale=True) x = ActBias(x, name="act_top") # x = GlobalAvgPooling("gap", x) logits = FullyConnected("linear", x, self._n_classes) prob = tf.nn.softmax(logits, name="prob") cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label) cost = tf.reduce_mean(cost, name="cross_entropy_loss") wrong = tf.cast(tf.logical_not(tf.nn.in_top_k(logits, label, 1)), tf.float32, name="wrong_vector") add_moving_summary(tf.reduce_mean(wrong, name="train_error")) wd_w = tf.train.exponential_decay(0.0002, get_global_step_var(), 480000, 0.2, True) wd_cost = tf.multiply(wd_w, regularize_cost('.*/W', tf.nn.l2_loss), name='wd_cost') add_moving_summary(cost, wd_cost) return tf.add_n([cost, wd_cost], name="cost")
def build_graph(self, joint_state, next_mask, action, reward, isOver): state = tf.identity(joint_state[:, 0, ...], name='state') self.predict_value = self.get_DQN_prediction(state) if not get_current_tower_context().is_training: return next_state = tf.identity(joint_state[:, 1, ...], name='next_state') action_onehot = tf.one_hot(action, self.num_actions, 1.0, 0.0) pred_action_value = tf.reduce_sum(self.predict_value * action_onehot, 1) # N, max_pred_reward = tf.reduce_mean(tf.reduce_max(self.predict_value, 1), name='predict_reward') summary.add_moving_summary(max_pred_reward) with tf.variable_scope('target'), varreplace.freeze_variables( skip_collection=True): # we are alternating between comb and fine states targetQ_predict_value = self.get_DQN_prediction(next_state) # NxA if self.method != 'Double': # DQN self.greedy_choice = tf.argmax(targetQ_predict_value + (tf.to_float(next_mask) * 1e4), 1) # N, predict_onehot = tf.one_hot(self.greedy_choice, self.num_actions, 1.0, 0.0) best_v = tf.reduce_sum(targetQ_predict_value * predict_onehot, 1) else: # Double-DQN next_predict_value = self.get_DQN_prediction(next_state) self.greedy_choice = tf.argmax( next_predict_value + (tf.to_float(next_mask) * 1e4), 1) # N, predict_onehot = tf.one_hot(self.greedy_choice, self.num_actions, 1.0, 0.0) best_v = tf.reduce_sum(targetQ_predict_value * predict_onehot, 1) target = reward + (1.0 - tf.cast( isOver, tf.float32)) * self.gamma * tf.stop_gradient(best_v) l2_loss = tensorpack.regularize_cost('.*W{1}', l2_regularizer(1e-3)) # cost = tf.losses.mean_squared_error(target, pred_action_value) cost = tf.losses.huber_loss(target, pred_action_value, reduction=tf.losses.Reduction.MEAN) summary.add_param_summary(('.*/W', ['histogram', 'rms'])) # monitor all W summary.add_moving_summary(cost) return cost
def get_model_cost(self, logits, label): cost = tf.losses.sparse_softmax_cross_entropy( labels=label, logits=logits, scope='cross_entropy_loss') def prediction_incorrect(logits, label, name='incorrect_vector'): with tf.name_scope('prediction_incorrect'): x = tf.logical_not(tf.nn.in_top_k(logits, label, 1)) return tf.cast(x, tf.float32, name=name) wrong = tf.reduce_mean(prediction_incorrect(logits, label), name='train_error') tp.summary.add_moving_summary(wrong) wd_cost = tf.multiply(1e-4, tp.regularize_cost('.*/weights', tf.nn.l2_loss), name='wd_cost') tp.summary.add_moving_summary(cost, wd_cost) tp.summary.add_param_summary(('.*/kernel', ['histogram'])) self.cost = tf.add_n([cost, wd_cost], name='cost')
def build_graph(self, image: Any, label: Any) -> Any: """ This function builds the model which takes the input variables and returns cost. """ # In tensorflow, inputs to convolution function are assumed to be NHWC. # Add a single channel here. image = tf.reshape(image, [-1, self.image_size, self.image_size, 1]) # Center the pixels values at zero. # tf.summary.image("input", (tf.expand_dims(og_image * 2 - 1, 3) + 1.0) * 128.0) image = image * 2 - 1 # The context manager `argscope` sets the default option for all the layers under # this context. Here we use 32 channel convolution with shape 3x3. with tensorpack.argscope( tensorpack.Conv2D, kernel_size=3, activation=tf.nn.relu, filters=self.hparams["n_filters"], ): c0 = tensorpack.Conv2D("conv0", image) p0 = tensorpack.MaxPooling("pool0", c0, 2) c1 = tensorpack.Conv2D("conv1", p0) c2 = tensorpack.Conv2D("conv2", c1) p1 = tensorpack.MaxPooling("pool1", c2, 2) c3 = tensorpack.Conv2D("conv3", p1) fc1 = tensorpack.FullyConnected("fc0", c3, 512, nl=tf.nn.relu) fc1 = tensorpack.Dropout("dropout", fc1, 0.5) logits = tensorpack.FullyConnected("fc1", fc1, out_dim=10, nl=tf.identity) # This line will cause Tensorflow to detect GPU usage. If session is not properly # configured it causes multi-GPU runs to crash. _preprocess_conv2d_input(image, "channels_first") label = tf.reshape(label, [-1]) cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label) cost = tf.reduce_mean( cost, name="cross_entropy_loss") # the average cross-entropy loss correct = tf.cast(tf.nn.in_top_k(predictions=logits, targets=label, k=1), tf.float32, name="correct") accuracy = tf.reduce_mean(correct, name="accuracy") train_error = tf.reduce_mean(1 - correct, name="train_error") tensorpack.summary.add_moving_summary(train_error, accuracy) # Use a regex to find parameters to apply weight decay. # Here we apply a weight decay on all W (weight matrix) of all fc layers. wd_cost = tf.multiply( self.hparams["weight_cost"], tensorpack.regularize_cost("fc.*/W", tf.nn.l2_loss), name="regularize_loss", ) total_cost = tf.add_n([wd_cost, cost], name="total_cost") return total_cost
def build_graph(self, x, image_target): with tf.name_scope("preprocess"): image_target = image_target / 255. def viz(name, images): with tf.name_scope(name): im = tf.concat(images, axis=2) #im = tf.transpose(im, [0, 2, 3, 1]) if self._act_input == tf.tanh: im = (im + 1.0) * 127.5 else: im = im * 255 im = tf.clip_by_value(im, 0, 255) im = tf.round(im) im = tf.cast(im, tf.uint8, name="viz") return im # calculate gram_target _, gram_target = self._build_extractor(image_target, name="ext_target") # inference pre_image_output from pre_image_input and gram_target self.image_outputs = list() self.loss_per_stage = list() x_output = x with tf.variable_scope("syn"): # use data stats in both train and test phases with argscope(BatchNorm, training=True): for s in range(self._top_stage): # get the first (s+1) coefs coefs = OrderedDict() for k in list(SynTexModelDesc.DEFAULT_COEFS.keys())[:s + 1]: coefs[k] = SynTexModelDesc.DEFAULT_COEFS[k] for repeat in range(self._n_stage): x_image, loss_input, _, x_output = \ self.build_stage(x_output, gram_target, coefs, name="stage%d-%d" % (s, repeat)) if repeat == 0: self.image_outputs.append(x_image) self.loss_per_stage.append( tf.reduce_mean(loss_input, name="loss%d" % s)) self.collect_variables("syn") # image_output = self._act_input(x_output, name="output") loss_output, loss_per_layer_output, _ = \ self._build_loss(image_output, gram_target, calc_grad=False) self.image_outputs.append(image_output) self.loss_per_stage.append( tf.reduce_mean(loss_output, name="loss_output")) self.loss_per_layer_output = OrderedDict() with tf.name_scope("loss_per_layer_output"): for layer in loss_per_layer_output: self.loss_per_layer_output[layer] = tf.reduce_mean( loss_per_layer_output[layer], name=layer) # average losses from all stages weights = [1.] for _ in range(len(self.loss_per_stage) - 1): weights.append(weights[-1] * self._loss_scale) # skip the first loss as it is computed from noise self.loss = tf.add_n([weights[i] * loss \ for i, loss in enumerate(reversed(self.loss_per_stage[1:]))], name="loss") wd_cost = regularize_cost(".*/W", tf.nn.l2_loss, name="wd_cost") # summary #with tf.device("/cpu:0"): stages_target = viz("stages-target", self.image_outputs + [image_target]) ctx = get_current_tower_context() if ctx is not None and ctx.is_main_training_tower: tf.summary.image("stages-target", stages_target, max_outputs=10, collections=["image_summaries"]) add_moving_summary(self.loss, wd_cost, *self.loss_per_stage, *self.loss_per_layer_output.values()) add_param_summary(('.*/theta', ['histogram']), collections=["acti_summaries"])
def build_graph(self, image, label): scale_image = 1. / 128.0 image = image * scale_image image_moment2 = CIFAR_TRAIN_PIXEL_MOMENT2 * scale_image * scale_image assert tf.test.is_gpu_available() with tf.variable_scope(self._name): x = NormConv2DScale(image, 16, 3, 1, center=self._center, input_moment2=image_moment2, name="conv_input") add_activation_summary(x, types=["mean", "rms", "histogram"]) # shape = [batchsize, 32, 32, 16] x = CifarResNet.build_group(x, self._n, 16, stride=1, center=self._center, theta_init=self._theta_init, theta_lr_mult=self._theta_lr_mult, name="g1") add_activation_summary(x, types=["mean", "rms", "histogram"]) # shape = [batchsize, 16, 16, 32] x = CifarResNet.build_group(x, self._n, 32, stride=2, center=self._center, theta_init=self._theta_init, theta_lr_mult=self._theta_lr_mult, name="g2") add_activation_summary(x, types=["mean", "rms", "histogram"]) # shape = [batchsize, 8, 8, 64] x = CifarResNet.build_group(x, self._n, 64, stride=2, center=self._center, theta_init=self._theta_init, theta_lr_mult=self._theta_lr_mult, name="g3") add_activation_summary(x, types=["mean", "rms", "histogram"]) x = ActBias(x, name="act_top") # x = GlobalAvgPooling("gap", x) logits = FullyConnected("linear", x, self._n_classes) prob = tf.nn.softmax(logits, name="prob") cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=label) cost = tf.reduce_mean(cost, name="cross_entropy_loss") wrong = tf.cast(tf.logical_not(tf.nn.in_top_k(logits, label, 1)), tf.float32, name="wrong_vector") add_moving_summary(tf.reduce_mean(wrong, name="train_error")) wd_w = tf.train.exponential_decay(0.0002, get_global_step_var(), 480000, 0.2, True) wd_cost = tf.multiply(wd_w, regularize_cost('.*/W', tf.nn.l2_loss), name='wd_cost') add_moving_summary(cost, wd_cost) add_param_summary(('.*/theta', ['histogram'])) add_param_summary(('.*/ma_mu', ['histogram'])) return tf.add_n([cost, wd_cost], name="cost")