def apply_deltas(self, deltas, boxes, img_size=None): ''' :param deltas: [batch_size,N,4]/[N,4] :param boxes: [batch_size,N,4]/[N.4] :return: ''' B0 = boxes.get_shape().as_list()[0] B1 = deltas.get_shape().as_list()[0] assert len(deltas.get_shape()) == len( boxes.get_shape()), "deltas and boxes's dims must be equal." if len(deltas.get_shape()) == 2: return self.decode_boxes( boxes, deltas, prio_scaling=[1 / x for x in self.weights]) if B0 == B1: return wmlt.static_or_dynamic_map_fn(lambda x: self.decode_boxes( x[0], x[1], prio_scaling=[1 / x for x in self.weights]), elems=[boxes, deltas], dtype=tf.float32, back_prop=False) elif B0 == 1 and B1 > 1: boxes = tf.squeeze(boxes, axis=0) return wmlt.static_or_dynamic_map_fn(lambda x: self.decode_boxes( boxes, x, prio_scaling=[1 / x for x in self.weights]), elems=deltas, dtype=tf.float32, back_prop=False) else: raise Exception("Error batch size")
def subsample_labels_by_negative_loss(labels, num_samples, probability, num_classes, positive_fraction): ''' 大于0为正样本,等于零为背景,小于0为忽略 SSD选择样本的方法,最指定百分比的正样本,负样本按损失大小选 :param probability: [batch_size,X]必须为相应的概率,不然计算不准确 :param labels:[batch_size,X] :param num_samples: :param positive_fraction: :return: [batch_size,num_samples],[batch_size,num_samples] ''' with tf.variable_scope("subsample_labels_negative_loss"): selected_pmask, selected_nmask = wmlt.static_or_dynamic_map_fn( lambda x: subsample_labels_by_negative_loss_on_single_img( x[0], num_samples, x[1], num_classes, positive_fraction), elems=[labels, probability], back_prop=True, ) selected_nmask = tf.reshape(selected_nmask, [-1]) selected_pmask = tf.reshape(selected_pmask, [-1]) if global_cfg.GLOBAL.SUMMARY_LEVEL <= SummaryLevel.DEBUG: tf.summary.scalar("psize", tf.reduce_sum(tf.cast(selected_pmask, tf.int32))) tf.summary.scalar("nsize", tf.reduce_sum(tf.cast(selected_nmask, tf.int32))) return selected_pmask, selected_nmask
def batch_nms_wrapper(bboxes, classes, lens, confidence=None, nms=None, k=200, sort=False): with tf.name_scope("batch_nms_wrapper"): if confidence is None: confidence = tf.ones_like(classes, dtype=tf.float32) def do_nms(sboxes, sclasses, sconfidence, lens): if sort: r_index = tf.range(lens) sconfidence, t_index = tf.nn.top_k(sconfidence[:lens], k=lens) sboxes = tf.gather(sboxes, t_index) sclasses = tf.gather(sclasses, t_index) r_index = tf.gather(r_index, t_index) boxes, labels, nms_indexs = nms(bboxes=sboxes[:lens], classes=sclasses[:lens], confidence=sconfidence[:lens]) r_len = tf.shape(nms_indexs)[0] boxes = tf.pad(boxes, [[0, k - r_len], [0, 0]]) labels = tf.pad(labels, [[0, k - r_len]]) if sort: nms_indexs = tf.gather(r_index, nms_indexs) nms_indexs = tf.pad(nms_indexs, [[0, k - r_len]]) return [boxes, labels, nms_indexs, r_len] boxes, labels, nms_indexs, lens = wmlt.static_or_dynamic_map_fn( lambda x: do_nms(x[0], x[1], x[2], x[3]), elems=[bboxes, classes, confidence, lens], dtype=[tf.float32, tf.int32, tf.int32, tf.int32]) return boxes, labels, nms_indexs, lens
def inference(self, inputs, box_cls, box_regression, center_ness, nms=None, pad=True): """ Arguments: inputs: same as FCOS.forward's batched_inputs box_cls: list of Tensor, Tensor's shape is [B,H,W,A*num_classes] box_delta: list of Tensor, Tensor's shape is [B,H,W,A*4] Returns: results: RD_BOXES: [B,N,4] RD_LABELS: [B,N] RD_PROBABILITY:[ B,N] RD_LENGTH:[B] """ assert len(box_cls[0].get_shape()) == 4, "error box cls dims" assert len(box_regression[0].get_shape()) == 4, "error box delta dims" B, _, _, _ = wmlt.combined_static_and_dynamic_shape(box_regression[0]) fm_sizes = [tf.shape(x)[1:3] for x in box_regression] box_cls = [reshape_to_N_HWA_K(x, self.num_classes) for x in box_cls] box_regression = [reshape_to_N_HWA_K(x, 4) for x in box_regression] center_ness = [tf.reshape(x, [B, -1]) for x in center_ness] box_cls = tf.concat(box_cls, axis=1) box_regression = tf.concat(box_regression, axis=1) center_ness = tf.concat(center_ness, axis=1) results = wmlt.static_or_dynamic_map_fn( lambda x: self.inference_single_image( x[0], x[1], x[2], fm_sizes, nms=nms, pad=pad), elems=[box_cls, box_regression, center_ness], dtype=[tf.float32, tf.int32, tf.float32, tf.int32], back_prop=False) outdata = { RD_BOXES: results[0], RD_LABELS: results[1], RD_PROBABILITY: results[2], RD_LENGTH: results[3] } if global_cfg.GLOBAL.SUMMARY_LEVEL <= SummaryLevel.DEBUG: wsummary.detection_image_summary( images=inputs[IMAGE], boxes=outdata[RD_BOXES], classes=outdata[RD_LABELS], lengths=outdata[RD_LENGTH], scores=outdata[RD_PROBABILITY], name="FCOSGIou_result", category_index=DataLoader.category_index) return outdata
def inference(self, inputs, box_cls, box_delta, anchors, output_fix_nr=0): """ Arguments: inputs: same as RetinaNet.forward's batched_inputs box_cls: list of Tensor, Tensor's shape is [B,H,W,A*num_classes] box_delta: list of Tensor, Tensor's shape is [B,H,W,A*4] anchors: list of Tensor, Tensor's shape is [X,4]( X=H*W*A) Returns: results: RD_BOXES: [B,N,4] RD_LABELS: [B,N] RD_PROBABILITY:[ B,N] RD_LENGTH:[B] """ assert len(anchors[0].get_shape()) == 2, "error anchors dims" assert len(box_cls[0].get_shape()) == 4, "error box cls dims" assert len(box_delta[0].get_shape()) == 4, "error box delta dims" anchors_size = [tf.shape(x)[0] for x in anchors] anchors = tf.concat(anchors, axis=0) box_cls = [reshape_to_N_HWA_K(x, self.num_classes) for x in box_cls] box_delta = [reshape_to_N_HWA_K(x, 4) for x in box_delta] box_cls = tf.concat(box_cls, axis=1) box_delta = tf.concat(box_delta, axis=1) results = wmlt.static_or_dynamic_map_fn( lambda x: self.inference_single_image(x[0], x[1], anchors, anchors_size, output_fix_nr), elems=[box_cls, box_delta], dtype=[tf.float32, tf.int32, tf.float32, tf.int32, tf.int32], back_prop=False) outdata = { RD_BOXES: results[0], RD_LABELS: results[1], RD_PROBABILITY: results[2], RD_LENGTH: results[4], RD_INDICES: results[3] } if global_cfg.GLOBAL.SUMMARY_LEVEL <= SummaryLevel.DEBUG: wsummary.detection_image_summary( images=inputs[IMAGE], boxes=outdata[RD_BOXES], classes=outdata[RD_LABELS], lengths=outdata[RD_LENGTH], scores=outdata[RD_PROBABILITY], name="RetinaNet_result", category_index=DataLoader.category_index) return outdata
def inference(self, inputs, box_cls, box_delta, anchors): """ Arguments: box_cls, box_delta: Same as the output of :meth:`RetinaNetHead.forward` anchors (list[list[Boxes]]): a list of #images elements. Each is a list of #feature level Boxes. The Boxes contain anchors of this image on the specific feature level. Returns: results (List[Instances]): a list of #images elements. """ assert len(anchors[0].get_shape()) == 2, "error anchors dims" anchors_size = [tf.shape(x)[0] for x in anchors] anchors = tf.concat(anchors, axis=0) box_cls = [ reshape_to_N_HWA_K(x, self.num_classes + 1) for x in box_cls ] box_delta = [reshape_to_N_HWA_K(x, 4) for x in box_delta] box_cls = tf.concat(box_cls, axis=1) box_delta = tf.concat(box_delta, axis=1) results = wmlt.static_or_dynamic_map_fn( lambda x: self.inference_single_image(x[0], x[1], anchors, anchors_size), elems=[box_cls, box_delta], dtype=(tf.float32, tf.int32, tf.float32, tf.int32), back_prop=False) outdata = { RD_BOXES: results[0], RD_LABELS: results[1], RD_PROBABILITY: results[2], RD_LENGTH: results[3] } if self.cfg.GLOBAL.SUMMARY_LEVEL <= SummaryLevel.DEBUG: wsummary.detection_image_summary( images=inputs[IMAGE], boxes=outdata[RD_BOXES], classes=outdata[RD_LABELS], scores=outdata[RD_PROBABILITY], lengths=outdata[RD_LENGTH], name="SSD_result", category_index=DataLoader.category_index) return outdata
def inference(self, inputs, box_cls, box_delta, anchors): """ Arguments: inputs: same as RetinaNet.forward's batched_inputs box_cls: list of Tensor, Tensor's shape is [B,H,W,A*num_classes] box_delta: list of Tensor, Tensor's shape is [B,H,W,A*4] anchors: list of Tensor, Tensor's shape is [X,4]( X=H*W*A) Returns: results: RD_BOXES: [B,N,4] RD_LABELS: [B,N] RD_PROBABILITY:[ B,N] RD_LENGTH:[B] """ assert len(anchors[0].get_shape()) == 2, "error anchors dims" assert len(box_cls[0].get_shape()) == 4, "error box cls dims" assert len(box_delta[0].get_shape()) == 4, "error box delta dims" anchors_size = [tf.shape(x)[0] for x in anchors] anchors = tf.concat(anchors, axis=0) box_cls = [reshape_to_N_HWA_K(x, self.num_classes) for x in box_cls] box_delta = [reshape_to_N_HWA_K(x, 4) for x in box_delta] box_cls = tf.concat(box_cls, axis=1) box_delta = tf.concat(box_delta, axis=1) pred_coeff = general_to_N_HWA_K_and_concat( self.head_outputs[COEFFICIENT], K=self.coefficient_nr) results = wmlt.static_or_dynamic_map_fn( lambda x: self.inference_single_image(x[0], x[1], x[2], x[3], anchors, anchors_size), elems=[ box_cls, box_delta, pred_coeff, self.head_outputs["protos"] ], dtype=[tf.float32, tf.int32, tf.float32, tf.int32, tf.float32], back_prop=False) outdata = { RD_BOXES: results[0], RD_LABELS: results[1], RD_PROBABILITY: results[2], RD_MASKS: results[3], RD_LENGTH: results[4] } if global_cfg.GLOBAL.SUMMARY_LEVEL <= SummaryLevel.DEBUG: wsummary.detection_image_summary_with_croped_mask( images=inputs[IMAGE], boxes=outdata[RD_BOXES], classes=outdata[RD_LABELS], lengths=outdata[RD_LENGTH], instance_masks=outdata[RD_MASKS], scores=outdata[RD_PROBABILITY], name="YOLACT_result", category_index=DataLoader.category_index) wsummary.detection_image_summary_with_croped_mask( images=tf.zeros_like(inputs[IMAGE]), boxes=outdata[RD_BOXES], classes=outdata[RD_LABELS], lengths=outdata[RD_LENGTH], instance_masks=outdata[RD_MASKS], scores=outdata[RD_PROBABILITY], name="YOLACT_only_mask_result", category_index=DataLoader.category_index) return outdata