def predict(ig,pred_loc, pred_confs, vbs,cfg): #priors = utils.get_prio_box(cfg=cfg) #priors = np_utils.get_prio_box_new(cfg=cfg) priors = gen_chors.gen_ssd_anchors() print(priors.shape) box = utils.decode_box(prios=priors, pred_loc=pred_loc[0]) props = slim.nn.softmax(pred_confs[0]) pp = props[:,1:] cls = tf.argmax(pp, axis=1) pp = tf.reduce_max(pp,axis=1) ix = tf.where(tf.greater(pp,0.1))[:,0] score = tf.gather(pp,ix) box = tf.gather(box,ix) cls = tf.gather(cls, ix) box = tf.clip_by_value(box,clip_value_min=0.0,clip_value_max=1.0) keep = tf.image.non_max_suppression( scores=score, boxes=box, iou_threshold=0.4, max_output_size=50 ) return tf.gather(box,keep),tf.gather(score,keep),tf.gather(cls,keep)
def get_loc_conf(true_box, true_label, batch_size=4, cfg=config.voc_vgg_300): #pri = get_prio_box_new(cfg = cfg) pri = gen_chors.gen_ssd_anchors() num_priors = pri.shape[0] loc_t = np.zeros([batch_size, num_priors, 4]) conf_t = np.zeros([batch_size, num_priors]) for s in range(batch_size): true_box_tm = true_box[s] labels = true_label[s] ix = np.sum(true_box_tm, axis=1) true_box_tm = true_box_tm[np.where(ix > 0)] labels = labels[np.where(ix > 0)] ops = over_laps(true_box_tm, pt_from(pri)) best_true = np.max(ops, axis=0) best_true_idx = np.argmax(ops, axis=0) best_prior = np.max(ops, axis=1) best_prior_idx = np.argmax(ops, axis=1) for j in range(best_prior_idx.shape[0]): best_true_idx[best_prior_idx[j]] = j matches = true_box_tm[best_true_idx] conf = labels[best_true_idx] + 1 conf[best_true < 0.5] = 0 loc = encode(matches, pri, variances=[0.1, 0.2]) loc_t[s] = loc conf_t[s] = conf return loc_t, conf_t
def predict(pred_loc, pred_confs, mask_fp, cfg): priors = gen_chors.gen_ssd_anchors() box = utils.decode_box(prios=priors, pred_loc=pred_loc[0]) props = slim.nn.softmax(pred_confs[0]) pp = props[:, 1:] cls = tf.argmax(pp, axis=1) pp = tf.reduce_max(pp, axis=1) ix = tf.where(tf.greater(pp, 0.1))[:, 0] score = tf.gather(pp, ix) box = tf.gather(box, ix) cls = tf.gather(cls, ix) cls = tf.cast(cls, tf.int32) box = tf.clip_by_value(box, clip_value_min=0.0, clip_value_max=1.0) x1, y1, x2, y2 = tf.split(box, 4, axis=1) crop_box = tf.concat([y1, x1, y2, x2], axis=1) keep = tf.image.non_max_suppression(scores=score, boxes=crop_box, iou_threshold=0.3, max_output_size=50) cls = tf.gather(cls, keep) box = tf.gather(crop_box, keep) masks = build_fpn_mask_graph(box, mask_fp, cfg) masks = tf.transpose(masks, [0, 3, 1, 2]) ix = tf.concat([ tf.reshape(tf.range(tf.shape(masks)[0]), [-1, 1]), tf.reshape(cls, [-1, 1]) + 1 ], axis=1) pred_masks = tf.gather_nd(masks, ix) y1, x1, y2, x2 = tf.split(box, 4, axis=1) crop_box = tf.concat([x1, y1, x2, y2], axis=1) return crop_box, tf.gather(score, keep), cls, pred_masks
def get_loss(conf_t, loc_t, pred_loc, pred_confs, target_mask, mask_fp, cfg): anchor = gen_chors.gen_ssd_anchors() #anchor = tf.tile(anchor,multiples=[cfg.batch_size,1]) # conf_t, loc_t = utils.batch_slice(inputs=[truth_box, gt_lables], graph_fn=loss_clc, batch_size=batch_size) crop_boxs = [] target_class_ids = [] for b in range(cfg.batch_size): tmp_conf_t = conf_t[b] #tmp_ped_loc = pred_loc[b] tmp_ped_loc = loc_t[b] tmp_conf_index = tf.where(tmp_conf_t > 0)[:, 0] tmp_conf_t = tf.gather(tmp_conf_t, tmp_conf_index) tmp_ped_loc = tf.gather(tmp_ped_loc, tmp_conf_index) live_anchor = tf.gather(anchor, tmp_conf_index) live_anchor = tf.cast(live_anchor, tf.float32) decode_box = utils.decode_box(live_anchor, tmp_ped_loc) x1, y1, x2, y2 = tf.split(decode_box, 4, axis=1) crop_box = tf.concat([y1, x1, y2, x2], axis=1) crop_box = tf.clip_by_value(crop_box, 0.0, 1.0) crop_boxs.append(crop_box) tmp_conf_t = tf.expand_dims(tmp_conf_t, 0) target_class_ids.append(tmp_conf_t) crop_boxs = tf.concat(crop_boxs, axis=0) target_class_ids = tf.squeeze(tf.concat(target_class_ids, axis=1), axis=0) sam_box = tf.random_shuffle(tf.range( tf.shape(crop_boxs)[0]))[:cfg.mask_train] crop_boxs = tf.gather(crop_boxs, sam_box) target_class_ids = tf.gather(target_class_ids, sam_box) target_mask = tf.gather(target_mask, sam_box) pred_mask = build_fpn_mask_graph(crop_boxs, mask_fp, cfg) mask_loss = mrcnn_mask_loss(target_mask, pred_mask, target_class_ids) mask_loss = mask_loss * cfg.mask_weight_loss conf_t = tf.reshape(conf_t, shape=(-1, )) loc_t = tf.reshape(loc_t, shape=(-1, 4)) positive_roi_ix = tf.where(conf_t > 0)[:, 0] positive_roi_class_ids = tf.cast(tf.gather(conf_t, positive_roi_ix), tf.int64) indices = tf.stack([positive_roi_ix, positive_roi_class_ids], axis=1) pred_loc = tf.reshape(pred_loc, shape=(-1, 4)) # Gather the deltas (predicted and true) that contribute to loss target_bbox = tf.gather(loc_t, positive_roi_ix) pred_bbox = tf.gather(pred_loc, positive_roi_ix) target_bbox = tf.cast(target_bbox, tf.float32) loss = tf.keras.backend.switch( tf.cast(tf.size(target_bbox) > 0, tf.bool), smooth_l1_loss(y_true=target_bbox, y_pred=pred_bbox), tf.constant(0.0, dtype=tf.float32)) loss_l = tf.reduce_sum(loss) pred_conf = tf.reshape(pred_confs, shape=(-1, cfg.Config['num_classes'])) conf_t_tm = tf.cast(conf_t, tf.int32) conf_t_tm = tf.reshape(conf_t_tm, shape=(-1, )) index = tf.stack([tf.range(tf.shape(conf_t_tm)[0]), conf_t_tm], axis=1) loss_c = log_sum(pred_conf) - tf.expand_dims( tf.gather_nd(pred_conf, index), -1) loss_c = tf.reshape(loss_c, shape=(cfg.batch_size, -1)) conf_t = tf.reshape(conf_t, shape=(cfg.batch_size, -1)) zeros = tf.zeros(shape=tf.shape(loss_c), dtype=tf.float32) loss_c = tf.where(tf.greater(conf_t, 0), zeros, loss_c) pos_num = tf.reduce_sum(tf.cast(tf.greater(conf_t, 0), dtype=tf.int32), axis=1) ne_num = pos_num * 3 los = [] for s in range(cfg.batch_size): loss_tt = loss_c[s, :] value, ne_index = tf.nn.top_k(loss_tt, k=ne_num[s]) pos_ix = tf.where(conf_t[s, :] > 0)[:, 0] pos_ix = tf.cast(pos_ix, tf.int32) ix = tf.concat([pos_ix, ne_index], axis=0) label = tf.gather(conf_t[s, :], ix) label = tf.cast(label, tf.int32) label = tf.one_hot(label, depth=cfg.Config['num_classes']) logits = tf.gather(pred_confs[s, :], ix) ls = tf.keras.backend.switch( tf.cast(tf.size(label) > 0, tf.bool), tf.nn.softmax_cross_entropy_with_logits(labels=label, logits=logits), tf.constant(0.0)) ls = tf.reduce_sum(ls) los.append(ls) num = tf.reduce_sum(pos_num) num = tf.cast(num, dtype=tf.float32) final_loss_c = tf.keras.backend.switch(num > 0, tf.reduce_sum(los) / num, tf.constant(0.0)) final_loss_l = tf.keras.backend.switch(num > 0, loss_l / num, tf.constant(0.0)) final_loss_c = final_loss_c tf.losses.add_loss(final_loss_c) tf.losses.add_loss(final_loss_l) tf.losses.add_loss(mask_loss) total_loss = tf.losses.get_losses() tf.summary.scalar(name='class_loss', tensor=final_loss_c) tf.summary.scalar(name='loc_loss', tensor=final_loss_l) tf.summary.scalar(name='mask_loss', tensor=mask_loss) train_tensors = tf.identity(total_loss, 'ss') return train_tensors