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: YOLOv3(**kwargs) else: with self.assertRaises(ValueError): YOLOv3(**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_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(): 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_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 __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 = YOLOv3(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 recog(file, outdir): model = YOLOv3( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') model.to_cpu() image = file img = utils.read_image(image, color=True) bboxes, labels, scores = model.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] base_out_path = outdir + '/' + Path(file).stem for (b, l, s) in zip(bbox, label, score): dog_label_number = 11 count = 1 if l == dog_label_number: crop_image(str(file), base_out_path + '_' + str(count) + '.jpg', int(b[1]), int(b[0]), int(b[3]), int(b[2])) count += 1
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 main(): parser = argparse.ArgumentParser() parser.add_argument('--model', choices=('faster_rcnn', 'ssd300', 'ssd512', '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_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))
method = 'POST' headers = {'Content-Type': 'application/json'} dic = {} response_body = {} process_time = {} yolo_input_size = 416 period_list = [ 'cap_read', 'cv2_resize', 'image_encode', 'image_post', 'object_detection', 'bbox_draw', 'textbox_draw', 'text_put', 'cv2_imshow' ] period_dict = {period: 0 for period in period_list} period_dict['environment'] = 'local' if args.local_cpu else 'remote' # https://github.com/chainer/chainercv/blob/master/chainercv/links/model/yolo/yolo_v3.py # input image size = 416 x 416 pix model = YOLOv3(pretrained_model='voc0712') if args.local_cpu else None if args.local_gpu: period_dict['environment'] = 'local_gpu' model = YOLOv3(pretrained_model='voc0712') chainer.backends.cuda.get_device_from_id(0) model.to_gpu() # capture video from web camera cap = cv2.VideoCapture(0) while (True): # Capture frame-by-frame event_number = int(time.time() * 1000000) start_time = time.time() ret, orig_frame = cap.read() # orig_frame.shape = (720, 1280, 3) period_dict['cap_read'] = time.time() - start_time
def setUp(self): self.link = YOLOv3(n_fg_class=self.n_fg_class) self.insize = 416 self.n_bbox = (13 * 13 + 26 * 26 + 52 * 52) * 3
def test_pretrained_wrong_n_fg_class(self): with self.assertRaises(ValueError): YOLOv3(n_fg_class=10, pretrained_model='voc0712')
def predict_bbox(img): chainer_img = trans_img_chainer(img) model = YOLOv3(pretrained_model="voc0712") bboxes, labels, scores = model.predict([chainer_img]) return bboxes, labels, scores
path = input(">>") while not os.path.exists(path2): print("Please type Output FilePath \n Default is C:\\DriveRecorder\\test") path2 = input(">>") if path2 == '': path2 = r'C:\DriveRecorder\test' break flist = glob.glob(path + r"/*.JPG") # model = FasterRCNNVGG16(n_fg_class=len(voc_bbox_label_names), # pretrained_model="voc07") model = YOLOv3( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') chainer.cuda.get_device_from_id(0).use() print("Runing on GPU") model.to_gpu() for f in tqdm(flist): # start=time.time() img = imread(f) result = img.copy() if img is None: raise FileNotFoundError('FileNotFound::' + f) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
def main(): model_phase1 = YOLOv3(n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712') #pretrained_model='voc07') chainer.cuda.get_device_from_id(0).use() model_phase1.to_gpu() accum_time = 0 curr_fps = 0 fps = "FPS: ??" prev_time = timer() frame_count = 1 while True: fframes = pipeline.wait_for_frames() color_frame = fframes.get_color_frame() depth_frame = fframes.get_depth_frame() frame = np.asanyarray(color_frame.get_data()) depth_image = np.asanyarray(depth_frame.get_data()) depth_colormap = cv2.applyColorMap( cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET) # BGR -> RGB rgb = cv2.cvtColor( cv2.resize(frame, (int(frame.shape[1] / LIGHTWEIGHT_COEF), int(frame.shape[0] / LIGHTWEIGHT_COEF))), cv2.COLOR_BGR2RGB) result = frame.copy() img = np.asarray(rgb, dtype=np.float32).transpose((2, 0, 1)) bboxes, labels, scores = model_phase1.predict([img]) bbox, label, score = bboxes[0], labels[0], scores[0] if len(bbox) != 0: for i, bb in enumerate(bbox): lb = label[i] if lb != 14: continue conf = score[i].tolist() ymin = int(bb[0] * LIGHTWEIGHT_COEF) xmin = int(bb[1] * LIGHTWEIGHT_COEF) ymax = int(bb[2] * LIGHTWEIGHT_COEF) xmax = int(bb[3] * LIGHTWEIGHT_COEF) class_num = int(lb) cv2.rectangle( result, (xmin, ymin), (xmax, ymax), voc_semantic_segmentation_label_colors[class_num], 5) #cv2.rectangle(depth_colormap, (int(xmin * 2 / 3), int(ymin * 2 / 3)), (int(xmax * 2 / 3), int(ymax * 2 / 3)), voc_semantic_segmentation_label_colors[class_num], 5) text = "person" ftext = text text_top = (xmin, ymin - 10) text_bot = (xmin + 80, ymin + 5) text_pos = (xmin + 5, ymin) text_top1 = (int(text_top[0] * 2 / 3), int(text_top[1] * 2 / 3)) text_bot1 = (int(text_bot[0] * 2 / 3), int(text_bot[1] * 2 / 3)) text_pos1 = (int(text_pos[0] * 2 / 3), int(text_pos[1] * 2 / 3)) # Draw label 1 cv2.rectangle( result, text_top, text_bot, voc_semantic_segmentation_label_colors[class_num], -1) cv2.putText(result, ftext, text_pos, cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0), 1) #cv2.rectangle(depth_colormap, text_top1, text_bot1, # voc_semantic_segmentation_label_colors[class_num], -1) #cv2.putText(depth_colormap, ftext, text_pos1, # cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 0), 1) curr_time = timer() exec_time = curr_time - prev_time prev_time = curr_time accum_time = accum_time + exec_time curr_fps = curr_fps + 1 if accum_time > 1: accum_time = accum_time - 1 fps = "FPS:" + str(curr_fps) curr_fps = 0 #Draw FPS in top right corner cv2.rectangle(result, (590, 0), (640, 17), (0, 0, 0), -1) cv2.putText(result, fps, (595, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Draw Frame Number cv2.rectangle(result, (0, 0), (50, 17), (0, 0, 0), -1) cv2.putText(result, str(frame_count), (0, 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) #cv2.rectangle(depth_colormap, (590, 0), (640, 17), (0, 0, 0), -1) #cv2.putText(depth_colormap, fps, (595, 10), # cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Draw Frame Number #cv2.rectangle(depth_colormap, (0, 0), (50, 17), (0, 0, 0), -1) #cv2.putText(depth_colormap, str(frame_count), (0, 10), # cv2.FONT_HERSHEY_SIMPLEX, 0.35, (255, 255, 255), 1) # Output Result cv2.imshow("Yolo Result", result) #cv2.imshow("Realsense", depth_colormap) # Stop Processing if cv2.waitKey(1) & 0xFF == ord('q'): break frame_count += 1 cv2.destroyAllWindows()
def _set_model(self): self.model = YOLOv3( n_fg_class=len(voc_bbox_label_names), pretrained_model='voc0712' )
args = parser.parse_args() period_list = ['image_decode', 'image_transform', 'object_detection', 'pack_result'] period_dict = {period: 0 for period in period_list} #period_dict['environment'] = 'remote' #dbfile = sqlite3.connect('speed_server.db') #c = dbfile.cursor() # Flaskクラスのインスタンスを作成 # __name__は現在のファイルのモジュール名 api = Flask(__name__) # https://github.com/chainer/chainercv/blob/master/chainercv/links/model/yolo/yolo_v3.py # input image size = 416 x 416 pix model = YOLOv3(pretrained_model='voc0712') if args.gpu: chainer.backends.cuda.get_device_from_id(0) model.to_gpu() # POSTの実装 @api.route('/api/detection', methods=['POST']) def post(): """ model.predict([img])の出力例 bboxes: [array([[ 99.97439 , 4.5523834, 192.06816 , 134.67502 ], [199.90709 , 143.38562 , 352.9582 , 195.82318 ], [ 75.87911 , 204.2211 , 360.09158 , 426.09985 ], [ 11.633713 , 264.68134 , 225.85754 , 356.1368 ], [126.243256 , 429.72067 , 178.3947 , 447.91504 ]], dtype=float32)]
def test_pretrained(self): YOLOv3(pretrained_model='voc0712')
def test_pretrained_n_fg_class(self): YOLOv3(n_fg_class=20, pretrained_model='voc0712')