def forward(self, img_id, img_shape, inputs): outputs = [] boxes = [] scores = [] downsample = 32 feats = self.backbone(inputs) route = None for idx, feat in enumerate(feats[::-1]): if idx > 0: feat = paddle.concat(x=[route, feat], axis=1) route, tip = self.yolo_blocks[idx](feat) block_out = self.block_outputs[idx](tip) outputs.append(block_out) if idx < 2: route = self.route_blocks[idx](route) route = F.resize_nearest(route, scale=2) if self.model_mode != 'train': anchor_mask = self.anchor_masks[idx] mask_anchors = [] for m in anchor_mask: mask_anchors.append(self.anchors[2 * m]) mask_anchors.append(self.anchors[2 * m + 1]) b, s = F.yolo_box(x=block_out, img_size=img_shape, anchors=mask_anchors, class_num=self.num_classes, conf_thresh=self.valid_thresh, downsample_ratio=downsample) boxes.append(b) scores.append(paddle.transpose(s, perm=[0, 2, 1])) downsample //= 2 if self.model_mode == 'train': return outputs preds = [ img_id, F.multiclass_nms(bboxes=paddle.concat(boxes, axis=1), scores=paddle.concat(scores, axis=2), score_threshold=self.valid_thresh, nms_top_k=self.nms_topk, keep_top_k=self.nms_posk, nms_threshold=self.nms_thresh, background_label=-1) ] if self.model_mode == 'test': return preds # model_mode == "eval" return outputs + preds
def predict(self, imgpath: str, filelist: str, visualization: bool = True, save_path: str = 'result'): ''' Detect images Args: imgpath(str): Image path . filelist(str): Path to get label name. visualization(bool): Whether to save result image. save_path(str) : Path to save detected images. Returns: boxes(np.ndarray): Predict box information. scores(np.ndarray): Predict score. labels(np.ndarray): Predict labels. ''' self.eval() boxes = [] scores = [] self.downsample = 32 im = self.transform(imgpath) h, w, c = utils.img_shape(imgpath) im_shape = paddle.to_tensor(np.array([[h, w]]).astype('int32')) label_names = utils.get_label_infos(filelist) img_data = paddle.to_tensor(np.array([im]).astype('float32')) outputs = self(img_data) for i, out in enumerate(outputs): anchor_mask = self.anchor_masks[i] mask_anchors = [] for m in anchor_mask: mask_anchors.append((self.anchors[2 * m])) mask_anchors.append(self.anchors[2 * m + 1]) box, score = F.yolo_box(x=out, img_size=im_shape, anchors=mask_anchors, class_num=self.class_num, conf_thresh=self.valid_thresh, downsample_ratio=self.downsample, name="yolo_box" + str(i)) boxes.append(box) scores.append(paddle.transpose(score, perm=[0, 2, 1])) self.downsample //= 2 yolo_boxes = paddle.concat(boxes, axis=1) yolo_scores = paddle.concat(scores, axis=2) pred = F.multiclass_nms(bboxes=yolo_boxes, scores=yolo_scores, score_threshold=self.valid_thresh, nms_top_k=self.nms_topk, keep_top_k=self.nms_posk, nms_threshold=self.nms_thresh, background_label=-1) bboxes = pred.numpy() labels = bboxes[:, 0].astype('int32') scores = bboxes[:, 1].astype('float32') boxes = bboxes[:, 2:].astype('float32') if visualization: if not os.path.exists(save_path): os.mkdir(save_path) utils.draw_boxes_on_image(imgpath, boxes, scores, labels, label_names, 0.5, save_path) return boxes, scores, labels