img += mean return img CanChinese = False for i, sample in enumerate(previewed): # if i > 30: # break utils.progress_bar(i, len(previewed), 'Handling...') if opt.debug: ipdb.set_trace() image = sample['input'][0].detach().cpu().numpy().transpose([1, 2, 0]) # image = (image.copy() image = (denormalize(image, max_pixel_value=1.0) * 255).astype( np.uint8).copy() label = sample['label'][0].item() if CanChinese: name = names[str(label)] image = cv2ImgAddText(image, name, 7, 3, (255, 0, 0), textSize=24) else: cv2.putText(image, 'label: ' + str(label), (10, 30), 0, 1, (255, 0, 0), 2) write_image(writer, f'preview_{opt.dataset}/{i}', '0_input', image, 0, 'HWC') writer.flush()
def eval_mAP(self, dataloader, epoch, writer, logger, data_name='val'): # eval_yolo(self.detector, dataloader, epoch, writer, logger, dataname=data_name) pred_bboxes = [] pred_labels = [] pred_scores = [] gt_bboxes = [] gt_labels = [] gt_difficults = [] with torch.no_grad(): for i, sample in enumerate(dataloader): utils.progress_bar(i, len(dataloader), 'Eva... ') image = sample['image'].to(opt.device) gt_bbox = sample['bboxes'] labels = sample['labels'] paths = sample['path'] batch_bboxes, batch_labels, batch_scores = self.forward(image) pred_bboxes.extend(batch_bboxes) pred_labels.extend(batch_labels) pred_scores.extend(batch_scores) for b in range(len(gt_bbox)): gt_bboxes.append(gt_bbox[b].detach().cpu().numpy()) gt_labels.append(labels[b].int().detach().cpu().numpy()) gt_difficults.append(np.array([False] * len(gt_bbox[b]))) if opt.vis: # 可视化预测结果 img = tensor2im(image).copy() # for x1, y1, x2, y2 in gt_bbox[0]: # cv2.rectangle(img, (x1,y1), (x2,y2), (0, 255, 0), 2) # 绿色的是gt num = len(batch_scores[0]) visualize_boxes(image=img, boxes=batch_bboxes[0], labels=batch_labels[0].astype(np.int32), probs=batch_scores[0], class_labels=opt.class_names) write_image(writer, f'{data_name}/{i}', 'image', img, epoch, 'HWC') result = [] for iou_thresh in [0.5, 0.55, 0.6, 0.65, 0.7, 0.75]: AP = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults=None, iou_thresh=iou_thresh, use_07_metric=False) APs = AP['ap'] mAP = AP['map'] result.append(mAP) logger.info( f'Eva({data_name}) epoch {epoch}, IoU: {iou_thresh}, APs: {str(APs[:opt.num_classes])}, mAP: {mAP}' ) write_loss(writer, f'val/{data_name}', 'mAP', mAP, epoch) logger.info( f'Eva({data_name}) epoch {epoch}, mean of (AP50-AP75): {sum(result)/len(result)}' )
# class_names = config.DATA.CLASS_NAMES class_names = config.DATA.CLASS_NAMES preview = train_dataloader # train_dataloader, val_dataloader for i, sample in enumerate(preview): # if i > 30: # break utils.progress_bar(i, len(preview), 'Handling...') if opt.debug: ipdb.set_trace() image = sample['image'][0].detach().cpu().numpy().transpose([1, 2, 0]) image = (image.copy() * 255).astype(np.uint8) bboxes = sample['bboxes'][0].cpu().numpy() labels = sample['labels'][0].cpu().numpy().astype(np.int32) visualize_boxes(image=image, boxes=bboxes, labels=labels, probs=np.array( np.random.randint(100, 101, size=[len(bboxes)]) / 100), class_labels=class_names) write_image(writer, f'preview_{config.DATA.DATASET}/{i}', 'image', image, 0, 'HWC') writer.flush()