def main(): args = parse_args() update_config(cfg, args) check_config(cfg) logger, final_output_dir, tb_log_dir = create_logger( cfg, args.cfg, "valid") logger.info(pprint.pformat(args)) logger.info(cfg) # cudnn related setting cudnn.benchmark = cfg.CUDNN.BENCHMARK torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED _, dataset = make_test_dataloader(cfg) total_size = len(dataset) pred_queue = Queue(100) workers = [] for i in range(args.world_size): indices = list(range(i, total_size, args.world_size)) p = Process(target=worker, args=(i, dataset, indices, cfg, logger, final_output_dir, pred_queue)) p.start() workers.append(p) logger.info("==>" + " Worker {} Started, responsible for {} images".format( i, len(indices))) all_preds = [] for idx in range(args.world_size): all_preds += pred_queue.get() for p in workers: p.join() res_folder = os.path.join(final_output_dir, "results") if not os.path.exists(res_folder): os.makedirs(res_folder) res_file = os.path.join(res_folder, "keypoints_%s_results.json" % dataset.dataset) json.dump(all_preds, open(res_file, 'w')) info_str = dataset._do_python_keypoint_eval(res_file, res_folder) name_values = OrderedDict(info_str) if isinstance(name_values, list): for name_value in name_values: _print_name_value(logger, name_value, cfg.MODEL.NAME) else: _print_name_value(logger, name_values, cfg.MODEL.NAME)
def main(): args = get_args() # get student config student_cfg = get_student_cfg(cfg, args.student_file) student_cfg.LOG_DIR = args.log student_cfg.PRINT_FREQ = int(args.print_freq) if args.mode == 'test': student_cfg.DATASET.TEST = 'test2017' logger, final_output_dir, tb_log_dir = create_logger( student_cfg, args.student_file, 'valid') logger.info(pprint.pformat(args)) logger.info(student_cfg) # cudnn related setting cudnn.benchmark = student_cfg.CUDNN.BENCHMARK torch.backends.cudnn.deterministic = student_cfg.CUDNN.DETERMINISTIC torch.backends.cudnn.enabled = student_cfg.CUDNN.ENABLED dev = 'cuda' if torch.cuda.is_available() else 'cpu' model = PoseHigherResolutionNet(student_cfg) model.load_state_dict(torch.load(args.model_file)) dump_input = torch.rand( (1, 3, student_cfg.DATASET.INPUT_SIZE, student_cfg.DATASET.INPUT_SIZE)) logger.info( get_model_summary(model, dump_input, verbose=student_cfg.VERBOSE)) model = torch.nn.DataParallel(model, device_ids=student_cfg.GPUS).cuda() model.eval() data_loader, test_dataset = make_test_dataloader(student_cfg) transforms = torchvision.transforms.Compose([ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) parser = HeatmapParser(student_cfg) all_preds = [] all_scores = [] pbar = tqdm( total=len(test_dataset)) if student_cfg.TEST.LOG_PROGRESS else None for i, (images, annos) in enumerate(data_loader): assert 1 == images.size(0), 'Test batch size should be 1' image = images[0].cpu().numpy() # size at scale 1.0 base_size, center, scale = get_multi_scale_size( image, student_cfg.DATASET.INPUT_SIZE, 1.0, min(student_cfg.TEST.SCALE_FACTOR)) with torch.no_grad(): final_heatmaps = None tags_list = [] for idx, s in enumerate( sorted(student_cfg.TEST.SCALE_FACTOR, reverse=True)): input_size = student_cfg.DATASET.INPUT_SIZE image_resized, center, scale = resize_align_multi_scale( image, input_size, s, min(student_cfg.TEST.SCALE_FACTOR)) image_resized = transforms(image_resized) image_resized = image_resized.unsqueeze(0).cuda() outputs, heatmaps, tags = get_multi_stage_outputs( student_cfg, model, image_resized, student_cfg.TEST.FLIP_TEST, student_cfg.TEST.PROJECT2IMAGE, base_size) final_heatmaps, tags_list = aggregate_results( student_cfg, s, final_heatmaps, tags_list, heatmaps, tags) final_heatmaps = final_heatmaps / float( len(student_cfg.TEST.SCALE_FACTOR)) tags = torch.cat(tags_list, dim=4) grouped, scores = parser.parse(final_heatmaps, tags, student_cfg.TEST.ADJUST, student_cfg.TEST.REFINE) final_results = get_final_preds( grouped, center, scale, [final_heatmaps.size(3), final_heatmaps.size(2)]) if student_cfg.TEST.LOG_PROGRESS: pbar.update() if i % student_cfg.PRINT_FREQ == 0: prefix = '{}_{}'.format( os.path.join(final_output_dir, 'result_valid'), i) # logger.info('=> write {}'.format(prefix)) save_valid_image(image, final_results, '{}.jpg'.format(prefix), dataset=test_dataset.name) # save_debug_images(cfg, image_resized, None, None, outputs, prefix) all_preds.append(final_results) all_scores.append(scores) if student_cfg.TEST.LOG_PROGRESS: pbar.close() name_values, _ = test_dataset.evaluate(cfg, all_preds, all_scores, final_output_dir) if isinstance(name_values, list): for name_value in name_values: _print_name_value(logger, name_value, cfg.MODEL.NAME) else: _print_name_value(logger, name_values, cfg.MODEL.NAME)
def main(): args = parse_args() update_config(cfg, args) check_config(cfg) logger, final_output_dir, tb_log_dir = create_logger( cfg, args.cfg, 'valid') logger.info(pprint.pformat(args)) logger.info(cfg) # cudnn related setting cudnn.benchmark = cfg.CUDNN.BENCHMARK torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED model = eval('models.' + cfg.MODEL.NAME + '.get_pose_net')(cfg, is_train=False) dump_input = torch.rand( (1, 3, cfg.DATASET.INPUT_SIZE, cfg.DATASET.INPUT_SIZE)) logger.info(get_model_summary(model, dump_input, verbose=cfg.VERBOSE)) if cfg.FP16.ENABLED: model = network_to_half(model) if cfg.TEST.MODEL_FILE: logger.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE)) model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=True) else: model_state_file = os.path.join(final_output_dir, 'model_best.pth.tar') logger.info('=> loading model from {}'.format(model_state_file)) model.load_state_dict(torch.load(model_state_file)) model = torch.nn.DataParallel(model, device_ids=cfg.GPUS).cuda() model.eval() data_loader, test_dataset = make_test_dataloader(cfg) if cfg.MODEL.NAME == 'pose_hourglass': transforms = torchvision.transforms.Compose([ torchvision.transforms.ToTensor(), ]) else: transforms = torchvision.transforms.Compose([ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) parser = HeatmapParser(cfg) all_preds = [] all_scores = [] # pbar = tqdm(total=len(test_dataset)) if cfg.TEST.LOG_PROGRESS else None pbar = tqdm(total=len(test_dataset)) for i, (images, annos) in enumerate(data_loader): assert 1 == images.size(0), 'Test batch size should be 1' image = images[0].cpu().numpy() # size at scale 1.0 base_size, center, scale = get_multi_scale_size( image, cfg.DATASET.INPUT_SIZE, 1.0, min(cfg.TEST.SCALE_FACTOR)) with torch.no_grad(): final_heatmaps = None tags_list = [] for idx, s in enumerate(sorted(cfg.TEST.SCALE_FACTOR, reverse=True)): input_size = cfg.DATASET.INPUT_SIZE image_resized, center, scale = resize_align_multi_scale( image, input_size, s, min(cfg.TEST.SCALE_FACTOR)) image_resized = transforms(image_resized) image_resized = image_resized.unsqueeze(0).cuda() outputs, heatmaps, tags = get_multi_stage_outputs( cfg, model, image_resized, cfg.TEST.FLIP_TEST, cfg.TEST.PROJECT2IMAGE, base_size) final_heatmaps, tags_list = aggregate_results( cfg, s, final_heatmaps, tags_list, heatmaps, tags) final_heatmaps = final_heatmaps / float(len(cfg.TEST.SCALE_FACTOR)) tags = torch.cat(tags_list, dim=4) grouped, scores = parser.parse(final_heatmaps, tags, cfg.TEST.ADJUST, cfg.TEST.REFINE) final_results = get_final_preds( grouped, center, scale, [final_heatmaps.size(3), final_heatmaps.size(2)]) if cfg.RESCORE.USE: try: scores = rescore_valid(cfg, final_results, scores) except: print("got one.") # if cfg.TEST.LOG_PROGRESS: # pbar.update() pbar.update() if i % cfg.PRINT_FREQ == 0: prefix = '{}_{}'.format( os.path.join(final_output_dir, 'result_valid'), i) # logger.info('=> write {}'.format(prefix)) save_valid_image(image, final_results, '{}.jpg'.format(prefix), dataset=test_dataset.name) # for scale_idx in range(len(outputs)): # prefix_scale = prefix + '_output_{}'.format( # # cfg.DATASET.OUTPUT_SIZE[scale_idx] # scale_idx # ) # save_debug_images( # cfg, images, None, None, # outputs[scale_idx], prefix_scale # ) all_preds.append(final_results) all_scores.append(scores) if cfg.TEST.LOG_PROGRESS: pbar.close() name_values, _ = test_dataset.evaluate(cfg, all_preds, all_scores, final_output_dir) if isinstance(name_values, list): for name_value in name_values: _print_name_value(logger, name_value, cfg.MODEL.NAME) else: _print_name_value(logger, name_values, cfg.MODEL.NAME)
def main(): args = parse_args() update_config(cfg, args) check_config(cfg) logger, final_output_dir, tb_log_dir = create_logger( cfg, args.cfg, 'valid' ) logger.info(pprint.pformat(args)) logger.info(cfg) # cudnn related setting cudnn.benchmark = cfg.CUDNN.BENCHMARK torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED model = eval('models.'+cfg.MODEL.NAME+'.get_pose_net')( cfg, is_train=False ) if cfg.TEST.MODEL_FILE: logger.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE)) model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=True) else: model_state_file = os.path.join( final_output_dir, 'model_best.pth.tar' ) logger.info('=> loading model from {}'.format(model_state_file)) model.load_state_dict(torch.load(model_state_file)) #dump_input = torch.rand( # (1, 3, cfg.DATASET.INPUT_SIZE, cfg.DATASET.INPUT_SIZE) #) #logger.info(get_model_summary(model, dump_input, verbose=cfg.VERBOSE)) model = torch.nn.DataParallel(model, device_ids=cfg.GPUS).cuda() model.eval() data_loader, test_dataset = make_test_dataloader(cfg) if cfg.MODEL.NAME == 'pose_hourglass': transforms = torchvision.transforms.Compose( [ torchvision.transforms.ToTensor(), ] ) else: transforms = torchvision.transforms.Compose( [ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) ] ) parser = HeatmapRegParser(cfg) # for only kpts all_reg_preds = [] all_reg_scores = [] # for pred kpts and pred heat all_preds = [] all_scores = [] pbar = tqdm(total=len(test_dataset)) if cfg.TEST.LOG_PROGRESS else None for i, (images, joints, masks, areas) in enumerate(data_loader): assert 1 == images.size(0), 'Test batch size should be 1' image = images[0].cpu().numpy() joints = joints[0].cpu().numpy() mask = masks[0].cpu().numpy() area = areas[0].cpu().numpy() # size at scale 1.0 base_size, center, scale = get_multi_scale_size( image, cfg.DATASET.INPUT_SIZE, 1.0, 1.0 ) with torch.no_grad(): heatmap_fuse = 0 final_heatmaps = None final_kpts = None input_size = cfg.DATASET.INPUT_SIZE for idx, s in enumerate(sorted(cfg.TEST.SCALE_FACTOR, reverse=True)): image_resized, joints_resized, _, center, scale = resize_align_multi_scale( image, joints, mask, input_size, s, 1.0 ) image_resized = transforms(image_resized) image_resized = image_resized.unsqueeze(0).cuda() outputs, heatmaps, kpts = get_multi_stage_outputs( cfg, model, image_resized, cfg.TEST.FLIP_TEST ) final_heatmaps, final_kpts = aggregate_results( cfg, final_heatmaps, final_kpts, heatmaps, kpts ) for heatmap in final_heatmaps: heatmap_fuse += up_interpolate( heatmap, size=(base_size[1], base_size[0]), mode='bilinear' ) heatmap_fuse = heatmap_fuse/float(len(final_heatmaps)) # for only pred kpts grouped, scores = parser.parse( final_heatmaps, final_kpts, heatmap_fuse[0], use_heatmap=False ) if len(scores) == 0: all_reg_preds.append([]) all_reg_scores.append([]) else: final_results = get_final_preds( grouped, center, scale, [heatmap_fuse.size(-1),heatmap_fuse.size(-2)] ) if cfg.RESCORE.USE: scores = rescore_valid(cfg, final_results, scores) all_reg_preds.append(final_results) all_reg_scores.append(scores) # for pred kpts and pred heatmaps grouped, scores = parser.parse( final_heatmaps, final_kpts, heatmap_fuse[0], use_heatmap=True ) if len(scores) == 0: all_preds.append([]) all_scores.append([]) if cfg.TEST.LOG_PROGRESS: pbar.update() continue final_results = get_final_preds( grouped, center, scale, [heatmap_fuse.size(-1),heatmap_fuse.size(-2)] ) if cfg.RESCORE.USE: scores = rescore_valid(cfg, final_results, scores) all_preds.append(final_results) all_scores.append(scores) if cfg.TEST.LOG_PROGRESS: pbar.update() sv_all_preds = [all_reg_preds, all_preds] sv_all_scores = [all_reg_scores, all_scores] sv_all_name = ['regression', 'final'] if cfg.TEST.LOG_PROGRESS: pbar.close() for i in range(len(sv_all_preds)): print('Testing '+sv_all_name[i]) preds = sv_all_preds[i] scores = sv_all_scores[i] name_values, _ = test_dataset.evaluate( cfg, preds, scores, final_output_dir, sv_all_name[i] ) if isinstance(name_values, list): for name_value in name_values: _print_name_value(logger, name_value, cfg.MODEL.NAME) else: _print_name_value(logger, name_values, cfg.MODEL.NAME)
nargs=argparse.REMAINDER) parser.add_argument('--gpu', help='gpu id for multiprocessing training', type=str) parser.add_argument('--world-size', default=1, type=int, help='number of nodes for distributed training') args = parser.parse_args() update_config(cfg, args) X = [] dataloader, _ = make_test_dataloader(cfg) for i, (images, heatmaps, joints, img_path) in enumerate(dataloader): rel_joints = joints[0][:, 0:2] - joints[0][0, 0:2] X.append(rel_joints.numpy().ravel()) X = np.stack(X) print(X.shape) tsne = TSNE(n_components=n_components) X_embedded = tsne.fit_transform(X) print(X_embedded.shape) fig = plt.figure() ax = Axes3D(fig) ax.scatter(X_embedded[:, 0], X_embedded[:, 1], X_embedded[:, 2]) # sns.scatterplot(x=X_embedded[:,0], y=X_embedded[:,1], legend='full', palette=palette) plt.show()
def main(): args = parse_args() update_config(cfg, args) check_config(cfg) logger, final_output_dir, tb_log_dir = create_logger( cfg, args.cfg, 'valid' ) logger.info(pprint.pformat(args)) # cudnn related setting cudnn.benchmark = cfg.CUDNN.BENCHMARK torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED model = eval('models.'+cfg.MODEL.NAME+'.get_pose_net')( cfg, is_train=False ) if cfg.FP16.ENABLED: model = network_to_half(model) if cfg.TEST.MODEL_FILE: logger.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE)) model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=True) else: model_state_file = os.path.join( final_output_dir, 'model_best.pth.tar' ) logger.info('=> loading model from {}'.format(model_state_file)) model.load_state_dict(torch.load(model_state_file)) model = torch.nn.DataParallel(model, device_ids=cfg.GPUS).cuda() model.eval() data_loader, test_dataset = make_test_dataloader(cfg) if cfg.MODEL.NAME == 'pose_hourglass': transforms = torchvision.transforms.Compose( [ torchvision.transforms.ToTensor(), ] ) else: transforms = torchvision.transforms.Compose( [ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225] ) ] ) parser = HeatmapParser(cfg) vid_file = 0 # Or video file path print("Opening Camera " + str(vid_file)) cap = cv2.VideoCapture(vid_file) while True: ret, image = cap.read() a = datetime.datetime.now() # size at scale 1.0 base_size, center, scale = get_multi_scale_size( image, cfg.DATASET.INPUT_SIZE, 1.0, min(cfg.TEST.SCALE_FACTOR) ) with torch.no_grad(): final_heatmaps = None tags_list = [] for idx, s in enumerate(sorted(cfg.TEST.SCALE_FACTOR, reverse=True)): input_size = cfg.DATASET.INPUT_SIZE image_resized, center, scale = resize_align_multi_scale( image, input_size, s, min(cfg.TEST.SCALE_FACTOR) ) image_resized = transforms(image_resized) image_resized = image_resized.unsqueeze(0).cuda() outputs, heatmaps, tags = get_multi_stage_outputs( cfg, model, image_resized, cfg.TEST.FLIP_TEST, cfg.TEST.PROJECT2IMAGE, base_size ) final_heatmaps, tags_list = aggregate_results( cfg, s, final_heatmaps, tags_list, heatmaps, tags ) final_heatmaps = final_heatmaps / float(len(cfg.TEST.SCALE_FACTOR)) tags = torch.cat(tags_list, dim=4) grouped, scores = parser.parse( final_heatmaps, tags, cfg.TEST.ADJUST, cfg.TEST.REFINE ) final_results = get_final_preds( grouped, center, scale, [final_heatmaps.size(3), final_heatmaps.size(2)] ) b = datetime.datetime.now() inf_time = (b - a).total_seconds()*1000 print("Inf time {} ms".format(inf_time)) # Display the resulting frame for person in final_results: color = np.random.randint(0, 255, size=3) color = [int(i) for i in color] add_joints(image, person, color, test_dataset.name, cfg.TEST.DETECTION_THRESHOLD) image = cv2.putText(image, "{:.2f} ms / frame".format(inf_time), (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA) cv2.imshow('frame', image) if cv2.waitKey(1) & 0xFF == ord('q'): break
def main(): args = parse_args() update_config(cfg, args) logger, final_output_dir, _ = create_logger(cfg, args.cfg, 'valid') logger.info(pprint.pformat(args)) logger.info(cfg) # cudnn related setting cudnn.benchmark = cfg.CUDNN.BENCHMARK torch.backends.cudnn.deterministic = cfg.CUDNN.DETERMINISTIC torch.backends.cudnn.enabled = cfg.CUDNN.ENABLED model = eval('models.' + cfg.MODEL.NAME + '.get_pose_net')(cfg, is_train=False) if cfg.TEST.MODEL_FILE: logger.info('=> loading model from {}'.format(cfg.TEST.MODEL_FILE)) model.load_state_dict(torch.load(cfg.TEST.MODEL_FILE), strict=True) else: model_state_file = os.path.join(final_output_dir, 'model_best.pth.tar') logger.info('=> loading model from {}'.format(model_state_file)) model.load_state_dict(torch.load(model_state_file)) model = torch.nn.DataParallel(model, device_ids=cfg.GPUS).cuda() model.eval() data_loader, test_dataset = make_test_dataloader(cfg) transforms = torchvision.transforms.Compose([ torchvision.transforms.ToTensor(), torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) all_reg_preds = [] all_reg_scores = [] pbar = tqdm(total=len(test_dataset)) if cfg.TEST.LOG_PROGRESS else None for i, images in enumerate(data_loader): assert 1 == images.size(0), 'Test batch size should be 1' image = images[0].cpu().numpy() # size at scale 1.0 base_size, center, scale = get_multi_scale_size( image, cfg.DATASET.INPUT_SIZE, 1.0, 1.0) with torch.no_grad(): heatmap_sum = 0 poses = [] for scale in sorted(cfg.TEST.SCALE_FACTOR, reverse=True): image_resized, center, scale_resized = resize_align_multi_scale( image, cfg.DATASET.INPUT_SIZE, scale, 1.0) image_resized = transforms(image_resized) image_resized = image_resized.unsqueeze(0).cuda() heatmap, posemap = get_multi_stage_outputs( cfg, model, image_resized, cfg.TEST.FLIP_TEST) heatmap_sum, poses = aggregate_results(cfg, heatmap_sum, poses, heatmap, posemap, scale) heatmap_avg = heatmap_sum / len(cfg.TEST.SCALE_FACTOR) poses, scores = pose_nms(cfg, heatmap_avg, poses) if len(scores) == 0: all_reg_preds.append([]) all_reg_scores.append([]) else: if cfg.TEST.MATCH_HMP: poses = match_pose_to_heatmap(cfg, poses, heatmap_avg) final_poses = get_final_preds(poses, center, scale_resized, base_size) if cfg.RESCORE.VALID: scores = rescore_valid(cfg, final_poses, scores) all_reg_preds.append(final_poses) all_reg_scores.append(scores) if cfg.TEST.LOG_PROGRESS: pbar.update() sv_all_preds = [all_reg_preds] sv_all_scores = [all_reg_scores] sv_all_name = [cfg.NAME] if cfg.TEST.LOG_PROGRESS: pbar.close() for i in range(len(sv_all_preds)): print('Testing ' + sv_all_name[i]) preds = sv_all_preds[i] scores = sv_all_scores[i] if cfg.RESCORE.GET_DATA: test_dataset.evaluate(cfg, preds, scores, final_output_dir, sv_all_name[i]) print('Generating dataset for rescorenet successfully') else: name_values, _ = test_dataset.evaluate(cfg, preds, scores, final_output_dir, sv_all_name[i]) if isinstance(name_values, list): for name_value in name_values: _print_name_value(logger, name_value, cfg.MODEL.NAME) else: _print_name_value(logger, name_values, cfg.MODEL.NAME)