def run_demo(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names else: raise NotImplementedError('Not implemented now.') device = torch.device(cfg.MODEL.DEVICE) cpu_device = torch.device("cpu") model = build_detection_model(cfg) model = model.to(cpu_device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = glob.glob(os.path.join(images_dir, '*.jpg')) mkdir(output_dir) transforms = build_transforms(cfg, is_train=False) model.eval() for i, image_path in enumerate(image_paths): start = time.time() image_name = os.path.basename(image_path) image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start start = time.time() result = model(images.to(cpu_device))[0] inference_time = time.time() - start result = result.resize((width, height)).numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] indices = scores > score_threshold boxes = boxes[indices] labels = labels[indices] scores = scores[indices] meters = ' | '.join([ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ]) print('({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) for i in range(len(labels)): text = str(label_name[labels[i]]) + str(round(scores[i], 2)) cv2.rectangle(image, tuple(boxes[i][:2]), tuple(boxes[i][2:]), color, 3) image = Image.fromarray(image) draw = ImageDraw.Draw(image) draw.text(tuple([boxes[i][0], boxes[i][1] - 40]), text, color, font=fontStyle) image = np.asarray(image) cv2.imshow('drawn_image', image) # drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) Image.fromarray(image).save(os.path.join(output_dir, image_name))
def run_demo(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names else: raise NotImplementedError('Not implemented now.') device = torch.device(cfg.MODEL.DEVICE) model = build_detection_model(cfg) model = model.to(device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = glob.glob(os.path.join(images_dir, '*.jpg')) mkdir(output_dir) cpu_device = torch.device("cpu") transforms = build_transforms(cfg, is_train=False) model.eval() _t = {'im_detect': Timer()} timer = Timer() timer.tic() inference_time_list=[] load_time_list = [] for image_path in image_paths: start = time.time() image_name = os.path.basename(image_path) image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start load_time_list.append(1000*load_time) _t['im_detect'].tic() #start = time.time() #print('1') result = model(images.to(device))[0] #print('2') result = result.resize((width, height)).to(cpu_device).numpy() boxes, labels, scores = result['boxes'], result['labels'], result['scores'] indices = scores > score_threshold boxes = boxes[indices] labels = labels[indices] scores = scores[indices] #inference_time = time.time() - start inference_time = _t['im_detect'].toc() #print(1000*(inference_time)) inference_time_list.append(1000*inference_time) meters = ' | '.join( [ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ] ) # print('({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) #drawn_image = draw_boxes(image, boxes, labels, scores, class_names).astype(np.uint8) #Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name)) _t['im_detect'].clear() N = len(inference_time_list)//2 total_time_list = np.array(inference_time_list) + np.array(load_time_list) total_time_list.sort() inference_time_list.sort() det_time = np.mean(total_time_list[:N])#/BATCH_SIZE best_det_time = np.min(total_time_list)#/BATCH_SIZE print("Total test time: %.2f s" % (timer.toc())) print("\nTotal detection speed: %.1f FPS" % (len(inference_time_list)/timer.toc())) print("\nAvg detection speed: %.1f FPS" % (1000./det_time)) print("Best detection speed: %.1f FPS" % (1000./best_det_time))
def run_demo(cfg, ckpt, score_threshold, images_dir, output_dir, dataset_type, gen_heatmap): if dataset_type == "voc": class_names = VOCDataset.class_names elif dataset_type == 'coco': class_names = COCODataset.class_names else: raise NotImplementedError('Not implemented now.') if torch.cuda.is_available(): device = torch.device(cfg.MODEL.DEVICE) else: device = torch.device("cpu") model = build_detection_model(cfg) model = model.to(device) checkpointer = CheckPointer(model, save_dir=cfg.OUTPUT_DIR) checkpointer.load(ckpt, use_latest=ckpt is None) weight_file = ckpt if ckpt else checkpointer.get_checkpoint_file() print('Loaded weights from {}'.format(weight_file)) image_paths = glob.glob(os.path.join(images_dir, '*.jpg')) mkdir(output_dir) cpu_device = torch.device("cpu") transforms = build_transforms(cfg, is_train=False) model.eval() dist_regr_model = DistanceRegrNet(2) dist_regr_model = load_model_weight(dist_regr_model, device) # load weights dist_regr_model.eval() X_scaler = load_standardizer(Standardizer()) person_label_idx = class_names.index('person') for i, image_path in enumerate(image_paths): start = time.time() image_name = os.path.basename(image_path) image = np.array(Image.open(image_path).convert("RGB")) height, width = image.shape[:2] images = transforms(image)[0].unsqueeze(0) load_time = time.time() - start start = time.time() result = model(images.to(device))[0] inference_time = time.time() - start result = result.resize((width, height)).to(cpu_device).numpy() boxes, labels, scores = result['boxes'], result['labels'], result[ 'scores'] # remove all non person class detections indices = np.logical_and(scores > score_threshold, labels == person_label_idx) boxes = boxes[indices] labels = labels[indices] scores = scores[indices] distances = None # create gaussian mixture models and kde plots only if centers detected if len(boxes) != 0: centers = np.apply_along_axis(get_mid_point, 1, boxes) image = draw_points(image, centers) # draw center points on image # reset center point ranges to a min of 0 and max of 100 _x = centers[:, 0] _y = centers[:, 1] centers[:, 0] = reset_range(max(_x), min(_x), 100, 0, _x) centers[:, 1] = reset_range(max(_y), min(_y), 100, 0, _y) # DBSCAN Clustering start = time.time() dbscan_center = DBSCAN(eps=18) dbscan_center.fit(centers) # print("dbscan clusters", dbscan_center._labels) # print("Unique number of clusters", len(set(dbscan_center._labels))) print( f"DBSCAN clustering time {round((time.time() - start) * 1000, 3)}ms" ) # Distance Regression start_time = time.time() # As boxes is in (xmin, ymin, xmax, ymax) format # X should always have width, height format width = boxes[:, 2] - boxes[:, 0] height = boxes[:, 3] - boxes[:, 1] X = np.column_stack((width, height)) X_scaled = X_scaler.transform(X) distances = dist_regr_model(torch.Tensor(X_scaled).to(device)) print( f"Distance Regr Inference time {round(time.time() - start_time, 4) * 1000}ms" ) if gen_heatmap: generate_sns_kde_heatmap(centers[:, 0], centers[:, 1], i, image_name) generate_sk_gaussian_mixture(centers, dbscan_center._labels, i, image_name, len(set(dbscan_center._labels)), covariance_type='diag') generate_cv2_heatmap(centers, dbscan_center._labels, i, image_name, len(set(dbscan_center._labels)), covariance_type='diag') meters = ' | '.join([ 'objects {:02d}'.format(len(boxes)), 'load {:03d}ms'.format(round(load_time * 1000)), 'inference {:03d}ms'.format(round(inference_time * 1000)), 'FPS {}'.format(round(1.0 / inference_time)) ]) print('({:04d}/{:04d}) {}: {}'.format(i + 1, len(image_paths), image_name, meters)) drawn_image = draw_boxes(image, boxes, labels, scores, distances, class_names).astype(np.uint8) Image.fromarray(drawn_image).save(os.path.join(output_dir, image_name))