def compute_loss(self): self.cls_loss = loss.focal_loss(self.pred_center, self.center_gt) self.size_loss = loss._reg_l1loss(self.pred_size, self.size_gt, self.mask_gt) self.offset_loss = loss._reg_l1loss(self.pred_offset, self.offset_gt, self.mask_gt) # self.regular_loss=cfg.weight_decay * tf.add_n([tf.nn.l2_loss(var) for var in tf.trainable_variables()]) self.total_loss = self.cls_loss + cfg.lambda_size * self.size_loss + cfg.lambda_offset * self.offset_loss #+self.regular_loss
def loss(self, class_pred, box_pred, class_label, box_label, pos_indices, bg_indices): reg_loss = huber_loss(box_pred, box_label, pos_indices) cls_loss = focal_loss(class_pred, class_label, pos_indices | bg_indices) normalizer = tf.maximum( tf.reduce_sum(tf.to_float(pos_indices), axis=-2), 1.0) normalized_reg_loss = tf.multiply(reg_loss, 1.0 / 4.0 / normalizer) normalized_cls_loss = tf.multiply(cls_loss, 1.0 / normalizer) return normalized_cls_loss, normalized_reg_loss
def build(self, data, reuse=None): with tf.variable_scope(self.name, reuse=reuse): wave = data["wave"] mu_wav = mu_law(wave) conditions = tf.cast(mu_wav + self.central_class, dtype=tf.int32) inputs = tf.to_int64(data["labels"]) # 1st. build the initial causal layer. with tf.variable_scope("init_causal_layer"): # 1st. right shift inputs to get labels. labels = tf.pad(inputs[:, :-1], [[0, 0], [1, 0]], constant_values=self.label_pad_value) # 2nd. to one-hot representation conditions_one_hot = tf.one_hot(conditions, depth=self.sample_classes, dtype=tf.float32) inputs_one_hot = tf.one_hot(inputs, depth=self.label_classes, dtype=tf.float32) labels_one_hot = tf.one_hot(labels, depth=self.label_classes, dtype=tf.float32) # 3rd. calculate init_causal_out = dilated_causal_conv1d(inputs=inputs_one_hot, dilation_rate=1, filters=self.residual_units, width=self.conv_width) # 2nd. build the dilated causal blocks. with tf.variable_scope("dilated_causal_blocks"): resi_out = init_causal_out skip_out = 0. for idx in range(self.blks): with tf.variable_scope("blk_{}".format(idx)): for idy in range(self.layers_per_blk): with tf.variable_scope("layer_{}".format(idy)): conv_out = dilated_causal_conv1d(inputs=resi_out, dilation_rate=self.dilation_base ** idy, filters=2*self.residual_units, width=self.conv_width) conditions_conv = tf.layers.dense(inputs=conditions_one_hot, units=2*self.residual_units, activation=None, name="conditions_conv") acti_out = self_gated_layer(inputs=tf.add(conv_out, conditions_conv)) resi_out += tf.layers.dense(inputs=acti_out, units=self.residual_units, activation=None, name="residual_out") skip_out += tf.layers.dense(inputs=acti_out, units=self.residual_units, activation=None, name="skip_out") # 3rd. calculate probabilities, and get loss. with tf.variable_scope("softmax"): # 1st. relu skip_out = tf.nn.relu(skip_out) # 2nd. dense -> relu skip_out = tf.layers.dense(inputs=skip_out, units=self.hidden_out, activation=tf.nn.relu) # 3rd. dense -> softmax logits = tf.layers.dense(inputs=skip_out, units=self.label_classes, activation=None) # 4th. get modes modes = tf.argmax(logits, axis=-1) # 5th. get focal loss. loss = focal_loss(labels=labels_one_hot, logits=logits) return {"wave": wave, "output": tf.to_float(modes), "labels": tf.to_float(labels), "loss": loss}
def _create_losses(input_queue, num_classes, train_config): """Creates loss function for a DetectionModel. Args: input_queue: BatchQueue object holding enqueued tensor_dicts. num_classes: num of classes, integer Returns: Average sum of loss of given input batch samples with shape """ (images, groundtruth_boxes_list, groundtruth_classes_list, anchors_list) = _get_inputs(input_queue, num_classes, batch_size=train_config.batch_size) images = [ preprocess(image, im_height=train_config.im_height, im_width=train_config.im_width, preprocess_options=train_config.data_augmentation_ops) for image in images ] images = tf.concat(images, 0) net = RetinaNet() loc_preds, cls_preds = net(images, num_classes + 1, anchors=9) # get num of anchor overlapped with ground truth box cls_gt = [anchor.get_field("gt_labels") for anchor in anchors_list] loc_gt = [anchor.get_field("gt_encoded_boxes") for anchor in anchors_list] # pos anchor count for each image gt_anchor_nums = tf.map_fn( lambda x: tf.reduce_sum(tf.cast(tf.greater(x, 0), tf.int32)), cls_gt) # get valid anchor indices valid_anchor_indices = tf.squeeze(tf.where(tf.greater_equal(cls_gt, 0))) # skip ignored anchors (iou belong to 0.4 to 0.5) [valid_cls_preds, valid_cls_gt] = map(lambda x: tf.gather(x, valid_anchor_indices, axis=1), [cls_preds, cls_gt]) # classification loss: convert to onehot code cls_loss = tf.multiply(focal_loss(valid_cls_gt, valid_cls_preds), 1. / tf.to_float(gt_anchor_nums)) # location regression loss valid_cls_indices = tf.squeeze(tf.where(tf.greater(cls_gt, 0))) # skip negative and ignored anchors [valid_loc_preds, valid_loc_gt] = map(lambda x: tf.gather(x, valid_cls_indices, axis=1), [loc_preds, loc_gt]) loc_loss = regression_loss(valid_loc_preds, valid_loc_gt, weights=tf.expand_dims( 1. / tf.to_float(gt_anchor_nums), 1)) loss = (tf.reduce_sum(loc_loss) + tf.reduce_sum(cls_loss)) / tf.size( gt_anchor_nums, out_type=tf.float32) return loss
def _compute_loss(self, truth, predictions, **params): self.cls_loss_out = None if 'focal_loss' in params and params['focal_loss']: temp = focal_loss(predictions, truth, weights=None, alpha=0.25, gamma=2) return temp else: temp = tf.nn.sigmoid_cross_entropy_with_logits(labels=truth, logits=predictions) temp = tf.reduce_mean(temp) return temp
def main(): parser = argparse.ArgumentParser() arg = parser.add_argument arg('--root', default='../input/df314/training', help='The root of train data folder') arg('--log-dir', default='log', help='the train output folder') arg('--last-model-name', default='keras.model', help='the name of model for loading') arg('--save-model-name', default='keras.model', help='the name of model to save') arg('--batch-size', type=int, default=8) arg('--epochs', type=int, default=100) arg('--lr', type=float, default=0.01) args = parser.parse_args() lr = args.lr saved_model = args.save_model_name last_model = args.last_model_name num_classes = 8 train_loader = DF314(args.root, batch_size=args.batch_size, num_classes=num_classes, fold=True) model = create_model(64, 4000, 1, num_classes=num_classes) if os.path.exists(last_model): model.load_weights(last_model) print('Last trained weight loaded') # add callback model_checkpoint = ModelCheckpoint(saved_model, monitor='val_iou_metric_func', mode='max', save_best_only=True, verbose=1, save_weights_only=True) reduce_lr = ReduceLROnPlateau(monitor='val_iou_metric_func', factor=0.5, patience=8, min_lr=0.0001, verbose=1) optimizer = SGD(lr=lr) loss_weight = [0, 2., 2., 1.2, 1.2, 2., 3., 2.] loss_func = focal_loss(num_classes=num_classes, loss_weight=loss_weight) valid_func = iou_metric(num_classes=num_classes) model.compile(loss=loss_func, optimizer=optimizer, metrics=[valid_func]) model.fit_generator(train_loader, epochs=50, max_queue_size=10, workers=0, verbose=1, validation_data=train_loader, validation_steps=train_loader.get_validation_step(), callbacks=[model_checkpoint, reduce_lr])
def loss(self, prediction_dict, gt_boxes_list, gt_labels_list): """ Compute loss between prediction tensor and gt Args: prediction_dict: dict of following items box_encodings: a [batch_size, num_anchors, 4] containing predicted boxes cls_pred_with_bg: a [batch_size, num_anchors, num_classes+1] containing predicted classes gt_boxes_list: a list of 2D gt box tensor with shape [num_boxes, 4] gt_labels_list: a list of 2-D gt one-hot class tensor with shape [num_boxes, num_classes] Returns: a dictionary with localization_loss and classification_loss """ with tf.name_scope(None, 'Loss', prediction_dict.values()): (batch_cls_targets, batch_cls_weights, batch_reg_targets, batch_reg_weights, match_list) = self._assign_targets(gt_boxes_list, gt_labels_list) # num_positives = [tf.reduce_sum(tf.cast(tf.not_equal(matches.match_results, -1), tf.float32)) # for matches in match_list] self._summarize_target_assignment(gt_boxes_list, match_list) reg_loss = regression_loss(prediction_dict["box_pred"], batch_reg_targets, batch_reg_weights) cls_loss = focal_loss(prediction_dict["cls_pred"], batch_cls_targets, batch_cls_weights) # normalize loss by num of matches # num_pos_anchors = [tf.reduce_sum(tf.cast(tf.not_equal(match.match_results, -1), tf.float32)) # for match in match_list] normalizer = tf.maximum( tf.to_float(tf.reduce_sum(batch_reg_weights)), 1.0) # normalize reg loss by box codesize (here is 4) reg_normalizer = normalizer * 4 normalized_reg_loss = tf.multiply(reg_loss, 1.0 / reg_normalizer, name="regression_loss") normalized_cls_loss = tf.multiply(cls_loss, 1.0 / normalizer, name="classification_loss") return normalized_reg_loss, normalized_cls_loss, batch_reg_weights, batch_cls_weights
batch_size=batch_size, shuffle=True, num_workers=4) for epoch in range(MAX_epoch): slosses_insideepoch = [] for i_data, (image, label, s_in, c_in) in enumerate(dataloader): st = time.time() image = image.float().to(device) label = label.float().unsqueeze(dim=1).to(device) s_in = s_in.float().unsqueeze(dim=1).to(device) c_in = c_in.float().unsqueeze(dim=1).to(device) b, _, h, w = image.shape image_in = torch.cat((s_in, c_in), dim=1) print(image_in.shape) sout = model(image, image_in) loss = focal_loss(sout, label) + dice_weight * dice_loss_perimg( F.sigmoid(sout), label) optimizer.zero_grad() loss.backward() optimizer.step() slosses_insideepoch.append(loss.item()) et = time.time() print( 'epoch {0: d}, batch {1:d} ; sloss {3:.3f}; used {2:.6f} s'.format( epoch, i_data, et - st, loss)) # 保存模型 torch.save(model.state_dict(), f"model_epoch_{epoch}.pth")
def compute_loss(self, true_hm, true_wh, true_reg, reg_mask, ind, true_cls): hm_loss = loss.focal_loss(self.pred_hm, true_hm) * cfgs.HM_LOSS_WEIGHT wh_loss = loss.reg_l1_loss(self.pred_wh, true_wh, ind, reg_mask) * cfgs.WH_LOSS_WEIGHT reg_loss = loss.reg_l1_loss(self.pred_reg, true_reg, ind, reg_mask) * cfgs.REG_LOSS_WEIGHT cls_loss = loss.cross_entropy_loss(self.pred_cls, true_cls, reg_mask) * cfgs.CLS_LOSS_WEIGHT return hm_loss, wh_loss, reg_loss, cls_loss
def compute_loss(self, true_hm, true_wh, true_reg, reg_mask, ind): hm_loss = loss.focal_loss(self.pred_hm, true_hm) wg_loss = 0.05*loss.reg_l1_loss(self.pred_wh, true_wh, ind, reg_mask) reg_loss = loss.reg_l1_loss(self.pred_reg, true_reg, ind, reg_mask) return hm_loss, wg_loss, reg_loss
def compute_loss(self): self.cls_loss = loss.focal_loss(self.pred_cls, self.cls_gt) self.size_loss = loss.reg_l1_loss(self.pred_size, self.size_gt) self.total_loss = self.cls_loss + 0.1 * self.size_loss
def model_fn(self, features, labels, mode, params): s1, s2, pos1, pos2, len1, len2, labels = get_input_tensors( features, labels) # s1, s2, len1, len2, labels = get_input_tensors(features, labels) with tf.name_scope("Input_embedding"): embeddings = tf.constant(self.W, dtype=tf.float32, name="fastText") embedded_s1 = tf.nn.embedding_lookup(embeddings, s1) embedded_s2 = tf.nn.embedding_lookup(embeddings, s2) # pos_embeddings = tf.get_variable("POS_embeddings", shape=[56], initializer=tf.initializers.ones) # embedded_pos1 = tf.nn.embedding_lookup(pos_embeddings, pos1) # embedded_pos2 = tf.nn.embedding_lookup(pos_embeddings, pos2) embedded_pos1 = tf.one_hot(pos1, 56, name="POS_embedding_1") embedded_pos2 = tf.one_hot(pos2, 56, name="POS_embedding_2") embedded_s1 = tf.reshape(embedded_s1, [params.batch_size, -1, 300]) embedded_s2 = tf.reshape(embedded_s2, [params.batch_size, -1, 300]) embedded_pos1 = tf.reshape(embedded_pos1, [params.batch_size, -1, 56]) embedded_pos2 = tf.reshape(embedded_pos2, [params.batch_size, -1, 56]) embedded_s1 = tf.concat([embedded_s1, embedded_pos1], axis=-1, name="s1_concat_pos1") embedded_s2 = tf.concat([embedded_s2, embedded_pos2], axis=-1, name="s2_concat_pos2") # logits = inference_v2(embedded_s1, embedded_s2, embedded_pos1, embedded_pos2, len1, len2, mode, params) logits = inference_v2(embedded_s1, embedded_s2, len1, len2, mode, params) predictions = tf.argmax(logits, axis=1) if mode == tf.estimator.ModeKeys.PREDICT: return tf.estimator.EstimatorSpec(mode=mode, predictions={ "logits": tf.nn.softmax(logits), "predictions": predictions }) cross_entropy = focal_loss(logits, labels, return_mean=False) train_op = tf.contrib.layers.optimize_loss( loss=cross_entropy, global_step=tf.train.get_global_step(), learning_rate=params.learning_rate, optimizer="Adam", clip_gradients=params.gradient_clipping_norm, summaries=["learning_rate", "loss", "gradients", "gradient_norm"]) precision = tf.metrics.precision(labels, predictions) recall = tf.metrics.recall(labels, predictions) return tf.estimator.EstimatorSpec(mode=mode, predictions={ "logits": logits, "predictions": predictions }, loss=cross_entropy, train_op=train_op, eval_metric_ops={ "accuracy": tf.metrics.accuracy( labels, predictions), "precision": precision, "recall": recall, "f1-score": f1_score(precision, recall) })
from __future__ import print_function import numpy as np from loss import focal_loss import tensorflow as tf Y_true = np.array([[0, 1, 0, 1, 0], [1, 0, 1, 0, 1]]) Y_pred = np.array([[0.1, 0.2, 0.3, 0.4, 0.5], [0.6, 0.7, 0.8, 0.9, 1]]) loss = focal_loss(Y_true, Y_pred) with tf.Session() as sess: print(sess.run(loss))