def convert_to_coco_api(ds): coco_ds = COCO() # annotation IDs need to start at 1, not 0, see torchvision issue #1530 ann_id = 1 dataset = {'images': [], 'categories': [], 'annotations': []} categories = set() for img_idx in range(len(ds)): # find better way to get target # targets = ds.get_annotations(img_idx) img, targets = ds[img_idx] image_id = targets["image_id"].item() img_dict = {} img_dict['id'] = image_id img_dict['height'] = targets["img_size"][0] img_dict['width'] = targets["img_size"][1] dataset['images'].append(img_dict) bboxes = targets["boxes"] bboxes = helper.convert2_abs_xyxy(bboxes, targets["img_size"]) bboxes[:, 2:] -= bboxes[:, :2] bboxes = bboxes.tolist() labels = (targets['labels'] + 1).tolist() areas = (targets['area'] * targets["img_size"][0] * targets["img_size"][1]).tolist() iscrowd = targets['iscrowd'].tolist() if 'masks' in targets: masks = targets['masks'] # make masks Fortran contiguous for coco_mask masks = masks.permute(0, 2, 1).contiguous().permute(0, 2, 1) if 'keypoints' in targets: keypoints = targets['keypoints'] keypoints = keypoints.reshape(keypoints.shape[0], -1).tolist() num_objs = len(bboxes) for i in range(num_objs): ann = {} ann['image_id'] = image_id ann['bbox'] = bboxes[i] ann['category_id'] = labels[i] categories.add(labels[i]) ann['area'] = areas[i] ann['iscrowd'] = iscrowd[i] ann['id'] = ann_id if 'masks' in targets: ann["segmentation"] = coco_mask.encode(masks[i].numpy()) if 'keypoints' in targets: ann['keypoints'] = keypoints[i] ann['num_keypoints'] = sum(k != 0 for k in keypoints[i][2::3]) dataset['annotations'].append(ann) ann_id += 1 # print(ann) dataset['categories'] = [{'id': i} for i in sorted(categories)] coco_ds.dataset = dataset coco_ds.createIndex() return coco_ds
def __getitem__(self, idx): img_path = os.path.join(self.my_image_path, self.pointers.iloc[idx, 0]) label_path = os.path.join(self.my_label_path, self.pointers.iloc[idx, 1]) with open(label_path ) as box: #opens txt file and puts bbox into a dataframe box = box.read() box = pd.DataFrame( [x.split() for x in box.rstrip('\n').split('\n')], columns=['class', 'xc', 'yc', 'w', 'h']) img = cv2.imread(img_path, 1)[:, :, ::-1] img_size = img.shape b = box.values.astype(np.float32) b[:, 1:] = helper.convert2_abs_xyxy(b[:, 1:], img_size) sample = { 'image': img, 'boxes': b[:, 1:], 'area': None, 'labels': b[:, 0], 'img_size': img_size } if self.transform: sample = self.transform(sample) targets = { 'boxes': sample['boxes'], 'labels': sample['labels'], 'image_id': torch.tensor(int( self.pointers.iloc[idx, 1].split('/')[-1].split('.')[0]), dtype=torch.int64), 'area': sample['area'], 'iscrowd': torch.zeros(sample['boxes'].shape[0], dtype=torch.uint8), 'img_size': img_size } return sample['image'], targets
def evaluate(model, device, coco_version, confidence=0.01, iou_threshold=0.5, subset=1): # FIXME remove this and make paste_masks_in_image run on the GPU n_threads = torch.get_num_threads() # FIXME remove this and make paste_masks_in_image run on the GPU torch.set_num_threads(1) cpu_device = torch.device("cpu") device = device model.eval() # metric_logger = utils.MetricLogger(delimiter=" ") if type(model) is nn.DataParallel: inp_dim = model.module.inp_dim pw_ph = model.module.pw_ph cx_cy = model.module.cx_cy stride = model.module.stride else: inp_dim = model.inp_dim pw_ph = model.pw_ph cx_cy = model.cx_cy stride = model.stride pw_ph = pw_ph.to(device) cx_cy = cx_cy.to(device) stride = stride.to(device) subset = subset transformed_dataset = Coco(partition='val', coco_version=coco_version, subset=subset, transform=transforms.Compose( [ResizeToTensor(inp_dim)])) dataloader = DataLoader(transformed_dataset, batch_size=8, shuffle=False, collate_fn=helper.collate_fn, num_workers=4) coco = coco_utils.get_coco_api_from_dataset(transformed_dataset) iou_types = ["bbox"] coco_evaluator = coco_eval.CocoEvaluator(coco, iou_types) for images, targets in dataloader: images = images.to(device) targets2 = [] for t in targets: dd = {} for k, v in t.items(): if (k != 'img_size'): dd[k] = v.to(device) else: dd[k] = v targets2.append(dd) # targets = [{k: v.to(device) for k, v in t.items()} for t in targets] targets = targets2 with torch.no_grad(): raw_pred = model(images, device) true_pred = util.transform(raw_pred.clone().detach(), pw_ph, cx_cy, stride) sorted_pred = torch.sort(true_pred[:, :, 4] * (true_pred[:, :, 5:].max(axis=2)[0]), descending=True) pred_mask = sorted_pred[0] > confidence indices = [(sorted_pred[1][e, :][pred_mask[e, :]]) for e in range(pred_mask.shape[0])] pred_final = [true_pred[i, indices[i], :] for i in range(len(indices))] pred_final_coord = [ util.get_abs_coord(pred_final[i].unsqueeze(-2)) for i in range(len(pred_final)) ] indices = [ nms_box.nms(pred_final_coord[i][0], pred_final[i][:, 4], iou_threshold) for i in range(len(pred_final)) ] pred_final = [ pred_final[i][indices[i], :] for i in range(len(pred_final)) ] abs_pred_final = [ helper.convert2_abs_xyxy(pred_final[i], targets[i]['img_size'], inp_dim) for i in range(len(pred_final)) ] outputs = [dict() for i in range(len((abs_pred_final)))] for i, atrbs in enumerate(abs_pred_final): outputs[i]['boxes'] = atrbs[:, :4] outputs[i]['scores'] = pred_final[i][:, 4] try: outputs[i]['labels'] = pred_final[i][:, 5:].max( axis=1)[1] + 1 #could be empty except: outputs[i]['labels'] = torch.tensor([]) outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs] res = { target["image_id"].item(): output for target, output in zip(targets, outputs) } coco_evaluator.update(res) # print('det is ',res) # gather the stats from all processes sys.stdout = open(os.devnull, 'w') #wrapper to disable hardcoded printing coco_evaluator.synchronize_between_processes() # accumulate predictions from all images coco_evaluator.accumulate() coco_evaluator.summarize() torch.set_num_threads(n_threads) mAP = coco_evaluator.get_stats()[0] sys.stdout = sys.__stdout__ #wrapper to enable hardcoded printing (return to normal mode) return mAP
def test(testloader, model, epoch, device): # FIXME remove this and make paste_masks_in_image run on the GPU cpu_device = torch.device("cpu") device = device batch_time = AverageMeter() data_time = AverageMeter() hyperparameters = model.hp confidence = hyperparameters['inf_confidence'] iou_threshold = hyperparameters['inf_iou_threshold'] if type(model) is nn.DataParallel: inp_dim = model.module.inp_dim pw_ph = model.module.pw_ph cx_cy = model.module.cx_cy stride = model.module.stride else: inp_dim = model.inp_dim pw_ph = model.pw_ph cx_cy = model.cx_cy stride = model.stride pw_ph = pw_ph.to(device) cx_cy = cx_cy.to(device) stride = stride.to(device) sys.stdout = open(os.devnull, 'w') #wrapper to disable hardcoded printing coco = coco_utils.get_coco_api_from_dataset(testloader.dataset) iou_types = ["bbox"] coco_evaluator = coco_eval.CocoEvaluator(coco, iou_types) sys.stdout = sys.__stdout__ #wrapper to enable hardcoded printing (return to normal mode) # switch to evaluate mode model.eval() end = time.time() with torch.no_grad(): for batch_idx, (images, targets) in enumerate(testloader): # measure data loading time data_time.update(time.time() - end) images = images.to(device) targets2 = [] for t in targets: dd = {} for k, v in t.items(): if (k != 'img_size'): dd[k] = v.to(device) else: dd[k] = v targets2.append(dd) # targets = [{k: v.to(device) for k, v in t.items()} for t in targets] targets = targets2 raw_pred = model(images, device) true_pred = util.transform(raw_pred.clone().detach(), pw_ph, cx_cy, stride) sorted_pred = torch.sort(true_pred[:, :, 4] * (true_pred[:, :, 5:].max(axis=2)[0]), descending=True) pred_mask = sorted_pred[0] > confidence indices = [(sorted_pred[1][e, :][pred_mask[e, :]]) for e in range(pred_mask.shape[0])] pred_final = [ true_pred[i, indices[i], :] for i in range(len(indices)) ] pred_final_coord = [ util.get_abs_coord(pred_final[i].unsqueeze(-2)) for i in range(len(pred_final)) ] indices = [ nms_box.nms(pred_final_coord[i][0], pred_final[i][:, 4], iou_threshold) for i in range(len(pred_final)) ] pred_final = [ pred_final[i][indices[i], :] for i in range(len(pred_final)) ] abs_pred_final = [ helper.convert2_abs_xyxy(pred_final[i], targets[i]['img_size'], inp_dim) for i in range(len(pred_final)) ] outputs = [dict() for i in range(len((abs_pred_final)))] for i, atrbs in enumerate(abs_pred_final): outputs[i]['boxes'] = atrbs[:, :4] outputs[i]['scores'] = pred_final[i][:, 4] try: outputs[i]['labels'] = pred_final[i][:, 5:].max( axis=1)[1] + 1 #could be empty except: outputs[i]['labels'] = torch.tensor([]) outputs = [{k: v.to(cpu_device) for k, v in t.items()} for t in outputs] res = { target["image_id"].item(): output for target, output in zip(targets, outputs) } coco_evaluator.update(res) # measure elapsed time batch_time.update(time.time() - end) end = time.time() sys.stdout = open(os.devnull, 'w') #wrapper to disable hardcoded printing coco_evaluator.synchronize_between_processes() # accumulate predictions from all images coco_evaluator.accumulate() coco_evaluator.summarize() metrics = coco_evaluator.get_stats() sys.stdout = sys.__stdout__ #wrapper to enable hardcoded printing (return to normal mode) coco_stats = { 'map_all': metrics[0], '[email protected]': metrics[1], '[email protected]': metrics[2], 'map_small': metrics[3], 'map_med': metrics[4], 'map_large': metrics[5], 'recall@1': metrics[6], 'recall@10': metrics[7], 'recall@100': metrics[8], 'recall@small': metrics[9], 'recall@medium': metrics[10], 'recall@large': metrics[11] } track.metric(iteration=0, epoch=epoch, coco_stats=coco_stats) return (metrics[0])