def test_pretrained(self): kwargs = { 'n_fg_class': self.n_fg_class, 'pretrained_model': self.pretrained_model, } if self.pretrained_model == 'voc0712': valid = self.n_fg_class in {None, 20} if valid: YOLOv2(**kwargs) else: with self.assertRaises(ValueError): YOLOv2(**kwargs)
def runYOLO(): '''parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('yolo_v2', 'yolo_v3'), default='yolo_v2') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='voc0712') parser.add_argument('image') args = parser.parse_args()''' aimage = 'sample7.jpg' amodel = 'yolo_v2' apretrained_model = 'voc0712' agpu = -1 if amodel == 'yolo_v2': model = YOLOv2(n_fg_class=len(voc_bbox_label_names), pretrained_model=apretrained_model) elif amodel == 'yolo_v3': model = YOLOv3(n_fg_class=len(voc_bbox_label_names), pretrained_model=apretrained_model) if agpu >= 0: chainer.cuda.get_device_from_id(agpu).use() model.to_gpu() img = utils.read_image(aimage, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names)
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('yolo_v2', 'yolo_v2_tiny', 'yolo_v3'), default='yolo_v2') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='voc0712') parser.add_argument('image') args = parser.parse_args() if args.model == 'yolo_v2': model = YOLOv2( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) elif args.model == 'yolo_v2_tiny': model = YOLOv2Tiny( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) elif args.model == 'yolo_v3': model = YOLOv3( n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() img = utils.read_image(args.image, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox( img, bbox, label, score, label_names=voc_bbox_label_names) plt.show()
def main(): dataset = VOCBboxDataset(year='2007', split='test') \ .slice[[29, 301, 189, 229], 'img'] models = [ ('Faster R-CNN', FasterRCNNVGG16(pretrained_model='voc07')), ('SSD300', SSD300(pretrained_model='voc0712')), ('SSD512', SSD512(pretrained_model='voc0712')), ('YOLOv2', YOLOv2(pretrained_model='voc0712')), ('YOLOv3', YOLOv3(pretrained_model='voc0712')), ] fig = plt.figure(figsize=(30, 20)) for i, img in enumerate(dataset): for j, (name, model) in enumerate(models): bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] ax = fig.add_subplot( len(dataset), len(models), i * len(models) + j + 1) vis_bbox( img, bbox, label, score, label_names=voc_bbox_label_names, ax=ax ) # Set MatplotLib parameters ax.set_aspect('equal') if i == 0: font = FontProperties() font.set_family('serif') font.set_size(35) ax.set_title(name, y=1.03, fontproperties=font) plt.axis('off') plt.tight_layout() plt.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument( '--model', choices=('yolo_v2', 'yolo_v3'), default='yolo_v2') parser.add_argument('--n_fg_class', type=int, default=80) parser.add_argument('darknetmodel') parser.add_argument('output') args = parser.parse_args() if args.model == 'yolo_v2': model = YOLOv2(n_fg_class=args.n_fg_class) elif args.model == 'yolo_v3': model = YOLOv3(n_fg_class=args.n_fg_class) with chainer.using_config('train', False): model(np.empty((1, 3, model.insize, model.insize), dtype=np.float32)) with open(args.darknetmodel, mode='rb') as f: major = np.fromfile(f, dtype=np.int32, count=1) minor = np.fromfile(f, dtype=np.int32, count=1) np.fromfile(f, dtype=np.int32, count=1) # revision assert(major * 10 + minor >= 2 and major < 1000 and minor < 1000) np.fromfile(f, dtype=np.int64, count=1) # seen if args.model == 'yolo_v2': load_yolo_v2(f, model) elif args.model == 'yolo_v3': load_yolo_v3(f, model) serializers.save_npz(args.output, model)
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--pretrained-model', default='voc0712') parser.add_argument('--out', '-o', type=str, default='yolo_dump') parser.add_argument('--verbose', default=False, action='store_true', help='Verbose mode') args = parser.parse_args() model = YOLOv2(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) x = chainer.Variable(np.random.random((1, 3, 416, 416)).astype(np.float32)) if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() x.to_gpu() retriever = chainer_trt.ModelRetriever(args.out, verbose=args.verbose) retriever.register_inputs(x, 'x') with chainer.using_config('train', False), chainer_trt.RetainHook(): locs, objs, confs = model(x) retriever(locs, name='locs') retriever(objs, name='objs') retriever(confs, name='confs') retriever.save()
def __init__(self, opts): self.pretrained_models = ('voc0712', ) assert opts[ 'pretrained_model'] in self.pretrained_models, "{} pretrained model doesn't exist".format( opts.pretrained_model) self.model = YOLOv2(n_fg_class=opts['n_classes'], pretrained_model=opts['pretrained_model'])
def __init__(self, gpu, model, nms_thresh= 0.45, score_thresh=0.6): self.gpu = gpu if model == 'yolo_v2_tiny': self.model = YOLOv2Tiny( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') elif model == 'yolo_v3': self.model = YOLOv3( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') elif model == 'ssd300': self.model = SSD300( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') elif model == 'ssd512': self.model = SSD512( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') elif model == 'fasterrcnnvgg16': self.model = FasterRCNNVGG16( n_fg_class=len(coco_bbox_label_names), pretrained_model='voc0712') elif model == 'fasterrcnnfpnresnet50': self.model = FasterRCNNFPNResNet50( n_fg_class=len(coco_bbox_label_names), pretrained_model='coco') elif model == 'fasterrcnnfpnresnet101': self.model = FasterRCNNFPNResNet101( n_fg_class=len(coco_bbox_label_names), pretrained_model='coco') else: self.model = YOLOv2( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') #self.model.nms_thresh = nms_thresh #self.model.score_thresh = score_thresh if self.gpu >= 0: chainer.cuda.get_device_from_id(self.gpu).use() self.model.to_gpu()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=int, default=0) parser.add_argument('--engine', required=False, help='TensorRT engine file. If None, chainer-based ' 'inference will run') parser.add_argument('--n-try', '-n', type=int, default=1000) parser.add_argument('--chainer', action='store_true') parser.add_argument('image') args = parser.parse_args() model = YOLOv2(n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') img = utils.read_image(args.image, color=True) chainer.cuda.get_device_from_id(args.gpu).use() if args.engine is not None: # Key idea: # `predict` method applies some pre-processings and preparations, # runs forward-pass and some post-processings. # We want to re-use all of them without copy-and-pasting the code, # while only forward-pass runs with TensorRT. # solution: replace __call__ and let `predict` call it, # as if it is the original python based forward pass def run_infer(self, x): return tuple(chainer.Variable(t) for t in infer(x)) infer = chainer_trt.Infer(args.engine) # NOQA type(model).__call__ = run_infer print("Loaded TensorRT inference engine {}".format(args.engine)) else: model.to_gpu() print("Run Chainer based inference") # Run inference once and get detections # (1st execution tends to be slow in CUDA) bboxes, labels, scores = model.predict([img]) before = time.time() for _ in range(args.n_try): model.predict([img]) after = time.time() print('{:.3f}ms/img'.format(1000 * (after - before) / args.n_try)) bbox, label, score = bboxes[0], labels[0], scores[0] vis_bbox(img, bbox, label, score, label_names=voc_bbox_label_names) plt.show()
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', choices=('yolo_v2', 'yolo_v2_tiny', 'yolo_v3'), default='yolo_v3') parser.add_argument('--n-fg-class', type=int, default=7) parser.add_argument('--darknetmodel', default='yolo-obj_6000.weights') parser.add_argument('--output', default='accident_KitDashV_6000.npz') args = parser.parse_args() if args.model == 'yolo_v2': model = YOLOv2(n_fg_class=args.n_fg_class) # elif args.model == 'yolo_v2_tiny': # model = YOLOv2Tiny(n_fg_class=args.n_fg_class) elif args.model == 'yolo_v3': model = YOLOv3(n_fg_class=args.n_fg_class) with chainer.using_config('train', False): model(np.empty((1, 3, model.insize, model.insize), dtype=np.float32)) with open(args.darknetmodel, mode='rb') as f: major = np.fromfile(f, dtype=np.int32, count=1) minor = np.fromfile(f, dtype=np.int32, count=1) np.fromfile(f, dtype=np.int32, count=1) # revision if major * 10 + minor >= 2 and major < 1000 and minor < 1000: np.fromfile(f, dtype=np.int64, count=1) # seen else: np.fromfile(f, dtype=np.int32, count=1) # seen if args.model == 'yolo_v2': load_yolo_v2(f, model) elif args.model == 'yolo_v2_tiny': load_yolo_v2(f, model) elif args.model == 'yolo_v3': load_yolo_v3(f, model) serializers.save_npz(args.output, model)
def setUp(self): self.link = YOLOv2(n_fg_class=self.n_fg_class) self.insize = 416 self.n_bbox = 13 * 13 * 5
def main(): parser = argparse.ArgumentParser() parser.add_argument('--model', choices=('faster_rcnn', 'ssd300', 'ssd512', 'yolo_v2', 'yolo_v2_tiny', 'yolo_v3'), default='ssd300') parser.add_argument('--pretrained-model') parser.add_argument('--gpu', type=int, default=-1) parser.add_argument('--batchsize', type=int, default=32) args = parser.parse_args() if args.model == 'faster_rcnn': if args.pretrained_model: model = FasterRCNNVGG16(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) else: model = FasterRCNNVGG16(pretrained_model='voc07') elif args.model == 'ssd300': if args.pretrained_model: model = SSD300(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) else: model = SSD300(pretrained_model='voc0712') elif args.model == 'ssd512': if args.pretrained_model: model = SSD512(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) else: model = SSD512(pretrained_model='voc0712') elif args.model == 'yolo_v2': if args.pretrained_model: model = YOLOv2(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) else: model = YOLOv2(pretrained_model='voc0712') elif args.model == 'yolo_v2_tiny': if args.pretrained_model: model = YOLOv2Tiny(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) else: model = YOLOv2(pretrained_model='voc0712') elif args.model == 'yolo_v3': if args.pretrained_model: model = YOLOv3(n_fg_class=len(voc_bbox_label_names), pretrained_model=args.pretrained_model) else: model = YOLOv3(pretrained_model='voc0712') if args.gpu >= 0: chainer.cuda.get_device_from_id(args.gpu).use() model.to_gpu() model.use_preset('evaluate') dataset = VOCBboxDataset(year='2007', split='test', use_difficult=True, return_difficult=True) iterator = iterators.SerialIterator(dataset, args.batchsize, repeat=False, shuffle=False) in_values, out_values, rest_values = apply_to_iterator(model.predict, iterator, hook=ProgressHook( len(dataset))) # delete unused iterators explicitly del in_values pred_bboxes, pred_labels, pred_scores = out_values gt_bboxes, gt_labels, gt_difficults = rest_values result = eval_detection_voc(pred_bboxes, pred_labels, pred_scores, gt_bboxes, gt_labels, gt_difficults, use_07_metric=True) print() print('mAP: {:f}'.format(result['map'])) for l, name in enumerate(voc_bbox_label_names): if result['ap'][l]: print('{:s}: {:f}'.format(name, result['ap'][l])) else: print('{:s}: -'.format(name))