class TestDataset: def __init__(self, opt, split='test', use_difficult=True): self.opt = opt self.db = VOCDataset(opt.voc_data_dir,split=split) def __getitem__(self, idx): ori_img, bbox, label, difficult = self.db.get_example(idx) img = preprocess(ori_img) return img, ori_img.shape[1:], bbox, label, difficult def __len__(self): return len(self.db)
class Dataset: def __init__(self, opt): self.opt = opt #生成类的实例 self.db = VOCDataset(opt.voc_data_dir) # 生成类的实例 self.tsf = Transform(opt.min_size, opt.max_size) def __getitem__(self, idx): #调用函数来获取一张图片的标注信息 ori_img, bbox, label, difficult = self.db.get_example(idx) # img, bbox, label, scale = self.tsf((ori_img, bbox, label)) return img.copy(), bbox.copy(), label.copy(), scale def __len__(self): return len(self.db)
def main(): lkm = LKM(pretrained=False) lkm.load_params(os.path.join('save', 'LKM', 'weights'), ctx=mx.cpu()) lkm.hybridize() dataset = VOCDataset(cfg.voc_root, 'val') demo(lkm, dataset, 10)
logging.info(args) create_net = lambda num: create_mobilenetv2_ssd_lite(num, width_mult=args.mb2_width_mult) config = ssd_config train_transform = TrainAugmentation(config.image_size, config.image_mean, config.image_std) target_transform = MatchPrior(config.priors, config.center_variance, config.size_variance, 0.5) test_transform = TestTransform(config.image_size, config.image_mean, config.image_std) logging.info("Prepare training datasets.") datasets = [] for dataset_path in args.datasets: if args.dataset_type == 'voc': dataset = VOCDataset(dataset_path, transform=train_transform, target_transform=target_transform) label_file = os.path.join(args.checkpoint_folder, "voc-model-labels.txt") store_labels(label_file, dataset.class_names) num_classes = len(dataset.class_names) else: raise ValueError(f"Dataset type {args.dataset_type} is not supported.") datasets.append(dataset) logging.info(f"Stored labels into file {label_file}.") train_dataset = ConcatDataset(datasets) logging.info("Train dataset size: {}".format(len(train_dataset))) train_loader = DataLoader(train_dataset, args.batch_size, num_workers=args.num_workers, shuffle=True) logging.info("Prepare Validation datasets.") if args.dataset_type == "voc": val_dataset = VOCDataset(args.validation_dataset, transform=test_transform,
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.RandomErasing(p=1, scale=(0.02, 0.33), ratio=(0.1, 0.1)) ]) pair_transform = transforms.Compose([ RandomHorizontalFlip(probability=0), RandomVerticalFlip(probability=1) ]) _IMAGE_SIZE_ = (448, 448) _GRID_SIZE_ = 7 _STRIDE_ = _IMAGE_SIZE_[0] / _GRID_SIZE_ class_names = build_class_names("./voc.names") dataset = VOCDataset(f"./data/val.txt", image_size=_IMAGE_SIZE_, grid_size=_GRID_SIZE_) class_color_mapping = { 0: "red", 1: "blue", 2: "AntiqueWhite", 3: "Aquamarine", 4: "Black", 5: "SeaGreen", 6: "Chartreuse", 7: "Chocolate", 8:"MediumAquaMarine", 9: "DarkGoldenRod", 10: "DarkGreen", 11: "DarkOrchid", 12: "DeepSkyBlue", 13: "DarkSlateGrey", 14: "DarkSalmon", 15: "DimGrey", 16: "SlateBlue", 17: "Fuchsia", 18: "Gold", 19: "IndianRed" } if __name__ == "__main__": model = YOLOv1(class_names, 7) model.load_state_dict( \ # torch.load('./model_checkpoints/yolo_v1_model.pth', map_location=torch.device('cpu')) \ torch.load("./model_checkpoints/yolo_v1_model_80_epoch.pth", map_location=torch.device('cpu')) \ )
def __init__(self, opt, split='test', use_difficult=True): self.opt = opt self.db = VOCDataset(opt.voc_data_dir,split=split)
def __init__(self, opt): self.opt = opt #生成类的实例 self.db = VOCDataset(opt.voc_data_dir) # 生成类的实例 self.tsf = Transform(opt.min_size, opt.max_size)
default=0.3, help='IoU threshold for NMS (default=0.3).') parser.add_argument( '--score_thresh', type=int, default=0.05, help='BBoxes with scores less than this are excluded (default=0.05).') args = parser.parse_args() opt._parse(vars(args)) t.multiprocessing.set_sharing_strategy('file_system') if opt.dataset == 'voc07': n_fg_class = 20 test_data = VOCDataset(opt.data_dir + '/VOCdevkit/VOC2007', 'test', 'test', True) elif opt.dataset == 'coco': n_fg_class = 80 test_data = COCODataset(opt.data_dir + '/COCO', 'val', 'test') else: raise ValueError('Invalid dataset.') test_loader = DataLoader(test_data, 1, False, num_workers=opt.n_workers_test) print('Dataset loaded.') if opt.model == 'frcnn': model = FasterRCNN(n_fg_class).cuda()
def main(): parser = argparse.ArgumentParser( description="SSD Evaluation on VOC Dataset.") parser.add_argument("--trained_model", type=str) parser.add_argument( "--dataset_type", default="voc", type=str, help='Specify dataset type. Currently support voc and open_images.') parser.add_argument( "--dataset", type=str, help="The root directory of the VOC dataset or Open Images dataset.") parser.add_argument("--label_file", type=str, help="The label file path.") parser.add_argument("--use_cuda", type=str2bool, default=True) parser.add_argument("--use_2007_metric", type=str2bool, default=True) parser.add_argument("--nms_method", type=str, default="hard") parser.add_argument("--iou_threshold", type=float, default=0.5, help="The threshold of Intersection over Union.") parser.add_argument("--eval_dir", default="eval_results", type=str, help="The directory to store evaluation results.") parser.add_argument('--mb2_width_mult', default=1.0, type=float, help='Width Multiplifier for MobilenetV2') args = parser.parse_args() DEVICE = torch.device( "cuda:0" if torch.cuda.is_available() and args.use_cuda else "cpu") eval_path = pathlib.Path(args.eval_dir) eval_path.mkdir(exist_ok=True) timer = Timer() class_names = [name.strip() for name in open(args.label_file).readlines()] if args.dataset_type == "voc": dataset = VOCDataset(args.dataset, is_test=True) true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class( dataset) net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=args.mb2_width_mult, is_test=True) timer.start("Load Model") net.load(args.trained_model) net = net.to(DEVICE) print(f'It took {timer.end("Load Model")} seconds to load the model.') predictor = create_mobilenetv2_ssd_lite_predictor( net, nms_method=args.nms_method, device=DEVICE) results = [] for i in range(len(dataset)): print("process image", i) timer.start("Load Image") image = dataset.get_image(i) print("Load Image: {:4f} seconds.".format(timer.end("Load Image"))) timer.start("Predict") boxes, labels, probs = predictor.predict(image) print("Prediction: {:4f} seconds.".format(timer.end("Predict"))) indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i results.append( torch.cat( [ indexes.reshape(-1, 1), labels.reshape(-1, 1).float(), probs.reshape(-1, 1), boxes + 1.0 # matlab's indexes start from 1 ], dim=1)) results = torch.cat(results) for class_index, class_name in enumerate(class_names): if class_index == 0: continue # ignore background prediction_path = eval_path / f"det_test_{class_name}.txt" with open(prediction_path, "w") as f: sub = results[results[:, 1] == class_index, :] for i in range(sub.size(0)): prob_box = sub[i, 2:].numpy() image_id = dataset.ids[int(sub[i, 0])] print(image_id + " " + " ".join([str(v) for v in prob_box]), file=f) aps = [] print("\n\nAverage Precision Per-class:") for class_index, class_name in enumerate(class_names): if class_index == 0: continue prediction_path = eval_path / f"det_test_{class_name}.txt" ap = compute_average_precision_per_class( true_case_stat[class_index], all_gb_boxes[class_index], all_difficult_cases[class_index], prediction_path, args.iou_threshold, args.use_2007_metric) aps.append(ap) print(f"{class_name}: {ap}") print(f"\nAverage Precision Across All Classes:{sum(aps)/len(aps)}")
def get_map(net_para, dataset, label_file): DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") eval_path = pathlib.Path("eval_results") eval_path.mkdir(exist_ok=True) timer = Timer() class_names = [name.strip() for name in open(label_file).readlines()] dataset = VOCDataset(dataset, is_test=True) true_case_stat, all_gb_boxes, all_difficult_cases = group_annotation_by_class( dataset) net = create_mobilenetv2_ssd_lite(len(class_names), width_mult=1.0, is_test=True) timer.start("Load Model") net.load_weight(net_para) net = net.to(DEVICE) predictor = create_mobilenetv2_ssd_lite_predictor(net, nms_method="hard", device=DEVICE) results = [] for i in tqdm(range(len(dataset))): timer.start("Load Image") image = dataset.get_image(i) timer.start("Predict") boxes, labels, probs = predictor.predict(image) indexes = torch.ones(labels.size(0), 1, dtype=torch.float32) * i results.append( torch.cat( [ indexes.reshape(-1, 1), labels.reshape(-1, 1).float(), probs.reshape(-1, 1), boxes + 1.0 # matlab's indexes start from 1 ], dim=1)) results = torch.cat(results) for class_index, class_name in enumerate(class_names): if class_index == 0: continue # ignore background prediction_path = eval_path / f"det_test_{class_name}.txt" with open(prediction_path, "w") as f: sub = results[results[:, 1] == class_index, :] for i in range(sub.size(0)): prob_box = sub[i, 2:].numpy() image_id = dataset.ids[int(sub[i, 0])] print(image_id + " " + " ".join([str(v) for v in prob_box]), file=f) aps = [] print("\n\nAverage Precision Per-class:") for class_index, class_name in enumerate(class_names): if class_index == 0: continue prediction_path = eval_path / f"det_test_{class_name}.txt" ap = compute_average_precision_per_class( true_case_stat[class_index], all_gb_boxes[class_index], all_difficult_cases[class_index], prediction_path, 0.5, True) aps.append(ap) print(f"{class_name}: {ap}") print(f"\nAverage Precision Across All Classes:{sum(aps) / len(aps)}") return sum(aps) / len(aps)
]) #Image detection pair transforms pair_transform = transforms.Compose([ RandomHorizontalFlip(probability=0.5), RandomVerticalFlip(probability=0.3) ]) normalise_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), transforms.RandomErasing(p=0.1, scale=(0.02, 0.12), ratio=(0.1, 1.1)), ]) dataset = VOCDataset(f"./data/train.txt", transform=[image_transform, normalise_transform], pair_transform=pair_transform) image, detections = dataset[random.randint(0, len(dataset))] # image, detections = dataset[100] image = im2PIL(image) true_image = image.copy() for bbox in detections: c = int(bbox[0]) draw_detection(true_image, bbox[1:], class_names[c], "white") true_image.show() #Apply the transform on the image # image, detections = pair_transform((image,detections)) # image = image_transform(image) # image = im2PIL(normalise_transform(image))
help='IoU threshold for NMS (default=0.3).') parser.add_argument( '--score_thresh', type=int, default=0.05, help='BBoxes with scores less than this are excluded (default=0.05).') args = parser.parse_args() opt._parse(vars(args)) t.multiprocessing.set_sharing_strategy('file_system') if opt.dataset == 'voc07': n_fg_class = 20 train_data = [ VOCDataset(opt.data_dir + '/VOCdevkit/VOC2007', 'trainval', 'train') ] test_data = VOCDataset(opt.data_dir + '/VOCdevkit/VOC2007', 'test', 'test', True) elif opt.dataset == 'voc0712': n_fg_class = 20 train_data = [ VOCDataset(opt.data_dir + '/VOCdevkit/VOC2007', 'trainval', 'train'), VOCDataset(opt.data_dir + '/VOCdevkit/VOC2012', 'trainval', 'train') ] test_data = VOCDataset(opt.data_dir + '/VOCdevkit/VOC2007', 'test', 'test', True) elif opt.dataset == 'coco': n_fg_class = 80
]) normalise_transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) erase_transform = transforms.RandomErasing(p=0.2, scale=(0.02, 0.33), ratio=(0.1, 0.1)) dataset = { 'train': VOCDataset( f"./data/train.txt", image_size=_IMAGE_SIZE_, grid_size=_GRID_SIZE_, transform=[image_transform, normalise_transform, erase_transform], pair_transform=pair_transform), 'val': VOCDataset(f"./data/val.txt", transform=[normalise_transform]) } dataloader = { x: utils.data.DataLoader(dataset[x], batch_size=_BATCH_SIZE_, shuffle=True, num_workers=4, collate_fn=batch_collate_fn) for x in ['train', 'val'] }