def main(): # argument parse and create log args = parse_args() # restrict one gpu (not support distributed learning) cfg.set_args(args.gpu) cudnn.benchmark = True cfg.batch_size = 1 # set tester tester = Tester() tester.build_dataloader() tester.load_model() for data in tqdm(tester.dataloader): results = tester.model(data) tester.save_results(results) # visualize input image if cfg.visualize: visualize_input_image(data[0]['raw_image'], data[0]['raw_gt_data']['bboxs'], './outputs/input_image.jpg') visualize_result(data[0]['raw_image'], results, data[0]['raw_gt_data']['bboxs'], './outputs/result_image.jpg') tester.save_jsons() tester.evaluate()
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True if cfg.dataset == 'InterHand2.6M': assert args.test_set, 'Test set is required. Select one of test/val' else: args.test_set = 'test' tester = Tester(args.test_epoch) tester._make_batch_generator(args.test_set) tester._make_model() preds = {'joint_coord': [], 'rel_root_depth': [], 'hand_type': [], 'inv_trans': []} with torch.no_grad(): for itr, (inputs, targets, meta_info) in enumerate(tqdm(tester.batch_generator)): # forward out = tester.model(inputs, targets, meta_info, 'test') joint_coord_out = out['joint_coord'].cpu().numpy() rel_root_depth_out = out['rel_root_depth'].cpu().numpy() hand_type_out = out['hand_type'].cpu().numpy() inv_trans = out['inv_trans'].cpu().numpy() preds['joint_coord'].append(joint_coord_out) preds['rel_root_depth'].append(rel_root_depth_out) preds['hand_type'].append(hand_type_out) preds['inv_trans'].append(inv_trans) # evaluate preds = {k: np.concatenate(v) for k,v in preds.items()} tester._evaluate(preds)
def func(id): cfg.set_args(args.gpu_ids.split(',')[id]) tester = Tester(Network(), cfg) tester.load_weights(test_model) range = [ranges[id], ranges[id + 1]] print('Range: ', range) return test_net(tester, logger, dets, range)
def main(): args = parse_args() cfg.set_args(args.gpu_ids, args.mode) cudnn.benchmark = True tester = Tester(args.test_epoch) tester._make_batch_generator() tester._make_model() outs = [] with torch.no_grad(): for itr, (inputs, targets, meta_info) in enumerate(tqdm(tester.batch_generator)): # forward out = tester.model(inputs, targets, meta_info, 'test') # save output out = {k: v.cpu().numpy() for k, v in out.items()} for k, v in out.items(): batch_size = out[k].shape[0] out = [{k: v[bid] for k, v in out.items()} for bid in range(batch_size)] outs += out # evaluate tester._evaluate(outs)
def func(id): cfg.set_args(args.gpu_ids.split(',')[id]) cap = cv2.VideoCapture(video_path) if out_path is not None: video_width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) video_height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter(out_path, fourcc, 20.0, (int(video_width), int(video_height))) # out_cpn_input_path = "/home/docker/Desktop/cpn_in_backextension.avi" out_cpn_input = None if out_cpn_input_path is not None: out_cpn_input = cv2.VideoWriter( out_cpn_input_path, fourcc, 20.0, (int(cfg.data_shape[1]), int(cfg.data_shape[0]))) tester = Tester(Network(), cfg) tester.load_weights(test_model) count = 0 while True: ret, frame = cap.read() if not ret: break test_net_t(tester, logger, dets_list[count], frame, out, out_cpn_input) count += 1
def main(): args = parse_args() cfg.set_args(args.gpu_ids, args.stage) cudnn.benchmark = True print('Stage: ' + args.stage) tester = Tester(args.test_epoch) tester._make_batch_generator() tester._make_model() eval_result = {} cur_sample_idx = 0 for itr, (inputs, targets, meta_info) in enumerate(tqdm(tester.batch_generator)): # forward with torch.no_grad(): out = tester.model(inputs, targets, meta_info, 'test') # save output out = {k: v.cpu().numpy() for k, v in out.items()} for k, v in out.items(): batch_size = out[k].shape[0] out = [{k: v[bid] for k, v in out.items()} for bid in range(batch_size)] # evaluate cur_eval_result = tester._evaluate(out, cur_sample_idx) for k, v in cur_eval_result.items(): if k in eval_result: eval_result[k] += v else: eval_result[k] = v cur_sample_idx += len(out) tester._print_eval_result(eval_result)
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True tester = Tester(args.test_epoch) tester._make_batch_generator() tester._make_model() with torch.no_grad(): for itr, (inputs, targets, meta_info) in enumerate(tqdm(tester.batch_generator)): # forward out = tester.model(inputs, targets, meta_info, 'test') vis = True if vis: filename = str(itr) for bid in range(len(out['geo_out_refined'])): tester.mesh.save_obj( out['mesh_refined'][bid].cpu().numpy(), None, filename + '_template_refined.obj') tester.mesh.save_obj( out['geo_out_refined'][bid].cpu().numpy(), None, filename + '_refined.obj')
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.fastest = True cudnn.benchmark = True cudnn.deterministic = False cudnn.enabled = True tester = Tester(args.backbone) tester._make_batch_generator() for epoch in range(args.model_epoch[0], args.model_epoch[1]): tester._make_model(epoch) preds = [] with torch.no_grad(): for itr, input_img in enumerate(tqdm(tester.batch_generator)): # forward coord_out = tester.model(input_img) if cfg.flip_test: flipped_input_img = flip(input_img, dims=3) flipped_coord_out = tester.model(flipped_input_img) flipped_coord_out[:, :, 0] = cfg.output_shape[ 1] - flipped_coord_out[:, :, 0] - 1 for pair in tester.flip_pairs: flipped_coord_out[:, pair[ 0], :], flipped_coord_out[:, pair[ 1], :] = flipped_coord_out[:, pair[1], :].clone( ), flipped_coord_out[:, pair[0], :].clone() coord_out = (coord_out + flipped_coord_out) / 2. vis = False if vis: filename = str(itr) tmpimg = input_img[0].cpu().numpy() tmpimg = tmpimg * np.array(cfg.pixel_std).reshape( 3, 1, 1) + np.array(cfg.pixel_mean).reshape(3, 1, 1) tmpimg = tmpimg.astype(np.uint8) tmpimg = tmpimg[::-1, :, :] tmpimg = np.transpose(tmpimg, (1, 2, 0)).copy() tmpkps = np.zeros((3, tester.joint_num)) tmpkps[:2, :] = coord_out[ 0, :, :2].cpu().numpy().transpose( 1, 0) / cfg.output_shape[0] * cfg.input_shape[0] tmpkps[2, :] = 1 tmpimg = vis_keypoints(tmpimg, tmpkps, tester.skeleton) cv2.imwrite(filename + '_output.jpg', tmpimg) coord_out = coord_out.cpu().numpy() preds.append(coord_out) # evaluate preds = np.concatenate(preds, axis=0) tester._evaluate(preds, cfg.result_dir)
def main(): # argument parse and create log args = parse_args() cfg.set_args(args.gpu_ids, args.mode, args.continue_train) cudnn.benchmark = True trainer = Trainer() trainer._make_batch_generator() trainer._make_model() # train for epoch in range(trainer.start_epoch, cfg.end_epoch): trainer.set_lr(epoch) trainer.tot_timer.tic() trainer.read_timer.tic() for itr, (inputs, targets, meta_info) in enumerate(trainer.batch_generator): trainer.read_timer.toc() trainer.gpu_timer.tic() # forward trainer.optimizer.zero_grad() loss = trainer.model(inputs, targets, meta_info, 'train') loss = {k: loss[k].mean() for k in loss} # backward sum(loss[k] for k in loss).backward() trainer.optimizer.step() trainer.gpu_timer.toc() screen = [ 'Epoch %d/%d itr %d/%d:' % (epoch, cfg.end_epoch, itr, trainer.itr_per_epoch), 'lr: %g' % (trainer.get_lr()), 'speed: %.2f(%.2fs r%.2f)s/itr' % (trainer.tot_timer.average_time, trainer.gpu_timer.average_time, trainer.read_timer.average_time), '%.2fh/epoch' % (trainer.tot_timer.average_time / 3600. * trainer.itr_per_epoch), ] screen += [ '%s: %.4f' % ('loss_' + k, v.detach()) for k, v in loss.items() ] trainer.logger.info(' '.join(screen)) trainer.tot_timer.toc() trainer.tot_timer.tic() trainer.read_timer.tic() trainer.save_model( { 'epoch': epoch, 'network': trainer.model.state_dict(), 'optimizer': trainer.optimizer.state_dict(), }, epoch)
def main(): # argument parse and create log args = parse_args() cfg.set_args(args.gpu_ids, args.continue_train) cudnn.fastest = True cudnn.benchmark = True trainer = Trainer() trainer._make_batch_generator() trainer._make_model() # train for epoch in range(trainer.start_epoch, cfg.end_epoch): trainer.set_lr(epoch) trainer.tot_timer.tic() trainer.read_timer.tic() for itr, (input_img, k_value, root_img, root_vis, joints_have_depth) in enumerate(trainer.batch_generator): trainer.read_timer.toc() trainer.gpu_timer.tic() # forward trainer.optimizer.zero_grad() target = {'coord': root_img, 'vis': root_vis, 'have_depth': joints_have_depth} loss_coord = trainer.model(input_img, k_value, target) loss_coord = loss_coord.mean(); # backward loss = loss_coord loss.backward() trainer.optimizer.step() trainer.gpu_timer.toc() screen = [ 'Epoch %d/%d itr %d/%d:' % (epoch, cfg.end_epoch, itr, trainer.itr_per_epoch), 'lr: %g' % (trainer.get_lr()), 'speed: %.2f(%.2fs r%.2f)s/itr' % ( trainer.tot_timer.average_time, trainer.gpu_timer.average_time, trainer.read_timer.average_time), '%.2fh/epoch' % (trainer.tot_timer.average_time / 3600. * trainer.itr_per_epoch), '%s: %.4f' % ('loss_coord', loss_coord.detach()), ] trainer.logger.info(' '.join(screen)) trainer.tot_timer.toc() trainer.tot_timer.tic() trainer.read_timer.tic() trainer.save_model({ 'epoch': epoch, 'network': trainer.model.state_dict(), 'optimizer': trainer.optimizer.state_dict(), }, epoch)
def main(): # argument parse and create log args = parse_args() # restrict one gpu : not support distributed learning cfg.set_args(args.gpu) cudnn.benchmark = True # set trainer trainer = Trainer() trainer.build_dataloader() trainer.build_model() trainer.set_optimizer() trainer.set_scheduler() start_epoch = 0 # load model if cfg.load_checkpoint: start_epoch = trainer.load_model() # logger logger = Logger() # train model for epoch in range(start_epoch, cfg.epoch): for i, data in enumerate(trainer.dataloader): trainer.optimizer.zero_grad() proposal_loss, detection_loss = trainer.model(data) loss = proposal_loss[ 0] + proposal_loss[1] * cfg.pro_loc_lambda + detection_loss[ 0] + detection_loss[1] * cfg.det_loc_lambda loss.backward() trainer.optimizer.step() logger.log(proposal_loss, detection_loss, epoch, i, epoch * len(trainer.dataloader) + i) if cfg.visualize_switch: if i % 500 == 0: cfg.visualize = True else: cfg.visualize = False trainer.scheduler.step() if cfg.save_checkpoint: trainer.save_model(epoch)
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.fastest = True cudnn.benchmark = True tester = Tester(args.test_epoch) tester._make_batch_generator() tester._make_model() preds = [] with torch.no_grad(): for itr, (input_img, cam_param) in enumerate(tqdm(tester.batch_generator)): coord_out = tester.model(input_img, cam_param) coord_out = coord_out.cpu().numpy() preds.append(coord_out) # evaluate preds = np.concatenate(preds, axis=0) tester._evaluate(preds, cfg.result_dir)
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.fastest = True cudnn.benchmark = True tester = Tester(args.test_epoch) print(args) # tester._make_batch_generator() tester._make_model() preds = [] with torch.no_grad(): # for itr, (input_img, cam_param) in enumerate(tqdm(tester.batch_generator)): bbox = get_bbox(int(args.image_id)) #bbox[0]-=20 path = "../data/MSCOCO/images/val2017/{:012d}.jpg".format(int(args.image_id)) input_img, cam_param, bbox, c = get_item(path, bbox) # print("input image : ", input_img.size()) # print(type(input_img), type(cam_param)) input_img = input_img.reshape(-1, 3, 256, 256) cam_param = torch.tensor(cam_param).reshape(-1, 1) # print("input image : ", input_img.size()) # print(type(input_img), type(cam_param)) coord_out = tester.model(input_img, cam_param) coord_out = coord_out.cpu().numpy() print(coord_out.shape) preds.append(coord_out) # evaluate preds = np.concatenate(preds, axis=0) print("pred: ", preds, "bbox : ", bbox) print(preds.shape) bbox, pred_root = evaluate(preds, bbox, c) print("pred_root: ", pred_root, "bbox: ", bbox)
def func(id): cfg.set_args(args.gpu_ids.split(',')[id]) tester = Tester(Network(), cfg) tester.load_weights(test_model) img = cv2.imread(os.path.join(image_path)) return test_net_t(tester, logger, dets, img)
def custom_main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True if cfg.dataset == 'InterHand2.6M': assert args.test_set, 'Test set is required. Select one of test/val' assert args.annot_subset, "Please set proper annotation subset. Select one of all, human_annot, machine_annot" else: args.test_set = 'test' args.annot_subset = 'all' tester = Tester(args.test_epoch) tester._make_batch_generator(args.test_set, args.annot_subset) tester._make_model() test_loader = CustomLoader(transforms.ToTensor(), 'my_hand', 1) with torch.no_grad(): for filename in test_loader.filenames: inputs = {} preds = {'joint_coord': [], 'rel_root_depth': [], 'hand_type': []} img, img_path, inv_trans = test_loader.get_batch_from_txt_files_() inputs['img'] = torch.reshape(img, (1, 3, 256, 256)) # forward out = tester.model(inputs) joint_coord_out = out['joint_coord'].cpu().numpy() rel_root_depth_out = out['rel_root_depth'].cpu().numpy() hand_type_out = out['hand_type'].cpu().numpy() preds['joint_coord'].append(joint_coord_out) preds['rel_root_depth'].append(rel_root_depth_out) preds['hand_type'].append(hand_type_out) # evaluate preds = {k: np.concatenate(v) for k, v in preds.items()} test_loader.visualize(preds, filename, img_path, inv_trans) def run_webcam(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True if cfg.dataset == 'InterHand2.6M': assert args.test_set, 'Test set is required. Select one of test/val' assert args.annot_subset, "Please set proper annotation subset. Select one of all, human_annot, machine_annot" else: args.test_set = 'test' args.annot_subset = 'all' tester = Tester(args.test_epoch) tester._make_batch_generator(args.test_set, args.annot_subset) tester._make_model() detector = YOLO_AKASH("best.pt", save_img=True) test_loader = CustomLoader(transforms.ToTensor(), 'my_hand', 1) cap = cv2.VideoCapture(0) with torch.no_grad(): while (True): ret, frame = cap.read() inputs = {} preds = { 'joint_coord': [], 'rel_root_depth': [], 'hand_type': [] } img = test_loader.process_frame(frame) inputs['img'] = torch.reshape(img, (1, 3, 256, 256)) boxes, confs, labels = detector.detect_image(img) img, inv_trans = augmentation(img, np.array(bbox), None, None, None, 'test', None) # forward out = tester.model(inputs) joint_coord_out = out['joint_coord'].cpu().numpy() rel_root_depth_out = out['rel_root_depth'].cpu().numpy() hand_type_out = out['hand_type'].cpu().numpy() preds['joint_coord'].append(joint_coord_out) preds['rel_root_depth'].append(rel_root_depth_out) preds['hand_type'].append(hand_type_out) # evaluate preds = {k: np.concatenate(v) for k, v in preds.items()} ##Changes need to be done from here onwards !!!!!!!!!!!!!!!!!!!!!!!! keypoint_frame = test_loader.visualize(preds, filename, img_path, inv_trans) cv2.imshow('webcam', keypoint_frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows()
def main(): # argument parse and create log args = parse_args() cfg.set_args(args.gpu_ids, args.continue_train) cudnn.fastest = True cudnn.benchmark = True cudnn.deterministic = False cudnn.enabled = True trainer = Trainer(cfg) trainer._make_batch_generator() trainer._make_model() # train for epoch in range(trainer.start_epoch, cfg.end_epoch): trainer.scheduler.step() trainer.tot_timer.tic() trainer.read_timer.tic() for itr, (input_img, joint_img, joint_vis, joints_have_depth) in enumerate(trainer.batch_generator): trainer.read_timer.toc() trainer.gpu_timer.tic() trainer.optimizer.zero_grad() input_img = input_img.cuda() joint_img = joint_img.cuda() joint_vis = joint_vis.cuda() joints_have_depth = joints_have_depth.cuda() # forward heatmap_out = trainer.model(input_img) # backward JointLocationLoss = trainer.JointLocationLoss( heatmap_out, joint_img, joint_vis, joints_have_depth) loss = JointLocationLoss loss.backward() trainer.optimizer.step() trainer.gpu_timer.toc() screen = [ 'Epoch %d/%d itr %d/%d:' % (epoch, cfg.end_epoch, itr, trainer.itr_per_epoch), 'lr: %g' % (trainer.scheduler.get_lr()[0]), 'speed: %.2f(%.2fs r%.2f)s/itr' % (trainer.tot_timer.average_time, trainer.gpu_timer.average_time, trainer.read_timer.average_time), '%.2fh/epoch' % (trainer.tot_timer.average_time / 3600. * trainer.itr_per_epoch), '%s: %.4f' % ('loss_loc', JointLocationLoss.detach()), ] trainer.logger.info(' '.join(screen)) trainer.tot_timer.toc() trainer.tot_timer.tic() trainer.read_timer.tic() trainer.save_model( { 'epoch': epoch, 'network': trainer.model.state_dict(), 'optimizer': trainer.optimizer.state_dict(), 'scheduler': trainer.scheduler.state_dict(), }, epoch)
def main(): # argument parse and create log args = parse_args() cfg.set_args(args.gpu_ids, args.continue_train) cudnn.fastest = True cudnn.benchmark = True trainer = Trainer() trainer._make_batch_generator() trainer._make_model() # train for epoch in range(trainer.start_epoch, cfg.end_epoch): trainer.set_lr(epoch) trainer.tot_timer.tic() trainer.read_timer.tic() for itr in range(trainer.itr_per_epoch): input_img_list, joint_img_list, joint_vis_list, joints_have_depth_list = [], [], [], [] for i in range(len(cfg.trainset)): try: input_img, joint_img, joint_vis, joints_have_depth = next(trainer.iterator[i]) except StopIteration: trainer.iterator[i] = iter(trainer.batch_generator[i]) input_img, joint_img, joint_vis, joints_have_depth = next(trainer.iterator[i]) input_img_list.append(input_img) joint_img_list.append(joint_img) joint_vis_list.append(joint_vis) joints_have_depth_list.append(joints_have_depth) # aggregate items from different datasets into one single batch input_img = torch.cat(input_img_list,dim=0) joint_img = torch.cat(joint_img_list,dim=0) joint_vis = torch.cat(joint_vis_list,dim=0) joints_have_depth = torch.cat(joints_have_depth_list,dim=0) # shuffle items from different datasets rand_idx = [] for i in range(len(cfg.trainset)): rand_idx.append(torch.arange(i,input_img.shape[0],len(cfg.trainset))) rand_idx = torch.cat(rand_idx,dim=0) rand_idx = rand_idx[torch.randperm(input_img.shape[0])] input_img = input_img[rand_idx]; joint_img = joint_img[rand_idx]; joint_vis = joint_vis[rand_idx]; joints_have_depth = joints_have_depth[rand_idx]; target = {'coord': joint_img, 'vis': joint_vis, 'have_depth': joints_have_depth} trainer.read_timer.toc() trainer.gpu_timer.tic() trainer.optimizer.zero_grad() # forward loss_coord = trainer.model(input_img, target) loss_coord = loss_coord.mean() # backward loss = loss_coord loss.backward() trainer.optimizer.step() trainer.gpu_timer.toc() screen = [ 'Epoch %d/%d itr %d/%d:' % (epoch, cfg.end_epoch, itr, trainer.itr_per_epoch), 'lr: %g' % (trainer.get_lr()), 'speed: %.2f(%.2fs r%.2f)s/itr' % ( trainer.tot_timer.average_time, trainer.gpu_timer.average_time, trainer.read_timer.average_time), '%.2fh/epoch' % (trainer.tot_timer.average_time / 3600. * trainer.itr_per_epoch), '%s: %.4f' % ('loss_coord', loss_coord.detach()), ] trainer.logger.info(' '.join(screen)) trainer.tot_timer.toc() trainer.tot_timer.tic() trainer.read_timer.tic() trainer.save_model({ 'epoch': epoch, 'network': trainer.model.state_dict(), 'optimizer': trainer.optimizer.state_dict(), }, epoch)
args.gpu_ids = str(np.argmin(mem_info())) if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = 0 if not gpus[0].isdigit() else int(gpus[0]) gpus[1] = len( mem_info()) if not gpus[1].isdigit() else int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) if args.test_epochs and not ',' in args.test_epochs: args.test_epochs = '%d,%d' % (int( args.test_epochs), int(args.test_epochs) + 1) assert args.test_epochs or args.test_model, 'Test epoch or model to test is required.' return args global args args = parse_args() cfg.set_args(args.gpu_ids, False, args.batch_size, args.epoch_size, 1) if args.test_model: logger = colorlogger( cfg.output_dir, 'test_model_{}'.format( args.test_model.split('/')[-1].split('.')[0])) test(args.test_model, logger) else: for i in range(*eval(args.test_epochs)): log_name = 'test_epoch_{}.logs'.format(i) logger = colorlogger(cfg.output_dir, log_name) test(i, logger)
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.fastest = True cudnn.benchmark = True cudnn.deterministic = False cudnn.enabled = True time_0 = time.time() tester = Tester(24) ##loading 3D pose estimation model tester._make_model() time_1 = time.time() print('loading integral pose model elapse:', round(time_1 - time_0, 2), 's') ##loading yolo detector detector = YOLOv3( model_def= "D:\\Robot Dance\\AI-Project-Portfolio\\3DMPPE_POSENET_RELEASE\\common\\detectors\\yolo\\config\\yolov3.cfg", class_path= "D:\\Robot Dance\\AI-Project-Portfolio\\3DMPPE_POSENET_RELEASE\\common\\detectors\\yolo\\data\\coco.names", weights_path= "D:\\Robot Dance\\AI-Project-Portfolio\\3DMPPE_POSENET_RELEASE\\common\\detectors\\yolo\\weights\\yolov3.weights", classes=('person', ), max_batch_size=16, device=torch.device('cuda:{}'.format(cfg.gpu_ids[0]))) print('loading yolo elapse:', round(time.time() - time_1, 2), 's') skeleton = ((0, 7), (7, 8), (8, 9), (9, 10), (8, 11), (11, 12), (12, 13), (8, 14), (14, 15), (15, 16), (0, 1), (1, 2), (2, 3), (0, 4), (4, 5), (5, 6)) fig = plt.figure(figsize=(10, 10)) transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=cfg.pixel_mean, std=cfg.pixel_std) ]) ##load model preds = [] if args.demo == 'image': image_path = 'D:\\Robot Dance\\AI-Project-Portfolio\\3DMPPE_POSENET_RELEASE\\image\\test_sample_20200711.jpg' raw_image = cv2.imread( image_path, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) input_img = raw_image.copy() ##using yolo to get human bounding box detections = detector.predict_single(input_img) # if not detections.cpu().numpy(): # detections = (0,0,input_img.shape[1],input_img.shape[0],1,1) for i, (x1, y1, x2, y2, conf, cls_conf, cls_pred) in enumerate(detections): x1 = int(round(x1.item())) x2 = int(round(x2.item())) y1 = int(round(y1.item())) y2 = int(round(y2.item())) img_patch = (input_img[y1:y2, x1:x2, ::-1]).copy().astype(np.float32) input_patch = cv2.resize(img_patch, (cfg.input_shape)) with torch.no_grad(): # forward input_patch = transform(input_patch).unsqueeze(0) coord_out = tester.model(input_patch) vis = True itr = 3 if vis: filename = str(itr) tmpimg = input_patch[0].cpu().numpy() tmpimg = tmpimg * np.array(cfg.pixel_std).reshape( 3, 1, 1) + np.array(cfg.pixel_mean).reshape(3, 1, 1) tmpimg = (tmpimg).astype(np.uint8) tmpimg = tmpimg[::-1, :, :] tmpimg = np.transpose(tmpimg, (1, 2, 0)).copy() tmpkps = np.zeros((3, 18)) tmpkps[:2, :] = coord_out[0, :, :2].cpu().numpy().transpose( 1, 0) / cfg.output_shape[0] * cfg.input_shape[0] tmpkps[2, :] = 1 tmpimg = vis_keypoints(tmpimg, tmpkps, skeleton) tmpimg = cv2.resize(tmpimg, (img_patch.shape[1], img_patch.shape[0])) cv2.imwrite(filename + '_output.jpg', tmpimg) # print(tmpkps) coord_out = coord_out.cpu().numpy() coord_out = coord_out * np.array([ img_patch.shape[1] / cfg.input_shape[1], img_patch.shape[0] / cfg.input_shape[0], 1 ]) #coord_out = (coord_out - np.mean(coord_out, axis = 1))/np.std(coord_out, axis = 1) coord_out = coord_out[:, :-1, :] print(coord_out) print(coord_out.shape) np.save('coord_out_0711.npy', coord_out) vis_3d = True if vis_3d: pred = coord_out.squeeze() #remove first batch dimension plot_3dkeypoints(pred, skeleton) motion_frame = {} for idx in range(coord_out.shape[1] - 1): motion_frame[idx] = (coord_out[0][idx][0].item(), coord_out[0][idx][2].item(), coord_out[0][idx][1].item()) with open('motionFrame.json', 'w') as f: json.dump(motion_frame, f) elif args.demo == 'video': video = cv2.VideoCapture( 'D:\\Robot Dance\\AI-Project-Portfolio\\dance_videos\\test_sample.mp4' ) ret_val, image = video.read() motion = {} frame = 0 with torch.no_grad(): while True: time_start = time.time() ret_val, raw_image = video.read() if not ret_val: break input_img = raw_image.copy() ##using yolo to get human bounding box detections = detector.predict_single(input_img) # if not detections.cpu().numpy().all(): # detections = (0,0,input_img.shape[1],input_img.shape[0],1,1) # print('not detected') if detections is None: detections = np.array([[ 0, 0, input_img.shape[1], input_img.shape[0], 1, 1, 1 ]]) print('not detected') elif detections.size()[0] == 0: detections = np.array([[ 0, 0, input_img.shape[1], input_img.shape[0], 1, 1, 1 ]]) print('not detected') for i, (x1, y1, x2, y2, conf, cls_conf, cls_pred) in enumerate(detections): x1 = int(round(x1.item())) x2 = int(round(x2.item())) y1 = int(round(y1.item())) y2 = int(round(y2.item())) img_patch = (input_img[y1:y2, x1:x2, ::-1]).copy().astype(np.float32) input_patch = cv2.resize(img_patch, (cfg.input_shape)) input_patch = transform(input_patch).unsqueeze(0) coord_out = tester.model(input_patch) print('Running model time:', round(time.time() - time_start, 2), 's') motion[frame] = {} for idx in range(coord_out.shape[1] - 1): motion[frame][idx] = (coord_out[0][idx][0].item(), coord_out[0][idx][2].item(), coord_out[0][idx][1].item()) vis = True vis_3d = False if vis: tmpimg = input_patch[0].cpu().numpy() tmpimg = tmpimg * np.array(cfg.pixel_std).reshape( 3, 1, 1) + np.array(cfg.pixel_mean).reshape(3, 1, 1) tmpimg = (tmpimg).astype(np.uint8) tmpimg = tmpimg[::-1, :, :] tmpimg = np.transpose(tmpimg, (1, 2, 0)).copy() tmpkps = np.zeros((3, 18)) tmpkps[:2, :] = coord_out[ 0, :, :2].cpu().numpy().transpose( 1, 0) / cfg.output_shape[0] * cfg.input_shape[0] tmpkps[2, :] = 1 tmpimg = vis_keypoints(tmpimg, tmpkps, skeleton) tmpimg = cv2.resize( tmpimg, (img_patch.shape[1], img_patch.shape[0])) file_name = '../demo_result/{0}.png'.format( str(frame).zfill(4)) cv2.imwrite(file_name, tmpimg) frame += 1 if vis_3d: coord_out = coord_out.cpu().numpy() coord_out = coord_out * np.array([ img_patch.shape[1] / cfg.input_shape[1], img_patch.shape[0] / cfg.input_shape[0], 1 ]) pred = coord_out.squeeze() #remove first batch dimension ax = plt.subplot('121', projection='3d') plt.axis('off') show3D_pose(pred, ax, skeleton, radius=40) # plot_3dkeypoints(pred,skeleton) file_name = '../test_sample_result/{0}.png'.format( str(frame).zfill(4)) plt.savefig(file_name) # cv2.imwrite(file_name, tmpimg) frame += 1 print('Processing Frame:', round(time.time() - time_start, 2), 's') with open('motionTest.json', 'w') as f: json.dump(motion, f)
def set_rootnet_config(): rootnet_cfg.set_args(gpu_ids='0') cudnn.fastest = True cudnn.benchmark = True cudnn.deterministic = False cudnn.enabled = True
if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = int(gpus[0]) gpus[1] = int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) if not args.stage: assert 0, "Please set training stage among [lixel, param]" assert args.test_epoch, 'Test epoch is required.' return args # argument parsing args = parse_args() cfg.set_args(args.gpu_ids, args.stage) cudnn.benchmark = True # SMPL joint set joint_num = 29 # original: 24. manually add nose, L/R eye, L/R ear joints_name = ('Pelvis', 'L_Hip', 'R_Hip', 'Torso', 'L_Knee', 'R_Knee', 'Spine', 'L_Ankle', 'R_Ankle', 'Chest', 'L_Toe', 'R_Toe', 'Neck', 'L_Thorax', 'R_Thorax', 'Head', 'L_Shoulder', 'R_Shoulder', 'L_Elbow', 'R_Elbow', 'L_Wrist', 'R_Wrist', 'L_Hand', 'R_Hand', 'Nose', 'L_Eye', 'R_Eye', 'L_Ear', 'R_Ear') flip_pairs = ( (1,2), (4,5), (7,8), (10,11), (13,14), (16,17), (18,19), (20,21), (22,23) , (25,26), (27,28) ) skeleton = ( (0,1), (1,4), (4,7), (7,10), (0,2), (2,5), (5,8), (8,11), (0,3), (3,6), (6,9), (9,14), (14,17), (17,19), (19, 21), (21,23), (9,13), (13,16), (16,18), (18,20), (20,22), (9,12), (12,24), (24,15), (24,25), (24,26), (25,27), (26,28) ) # SMPl mesh vertex_num = 6890 smpl_layer = SMPL_Layer(gender='neutral', model_root=cfg.smpl_path + '/smplpytorch/native/models') face = smpl_layer.th_faces.numpy() joint_regressor = smpl_layer.th_J_regressor.numpy() root_joint_idx = 0
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.fastest = True cudnn.benchmark = True cudnn.deterministic = False cudnn.enabled = True tester = Tester(args.test_epoch) tester._make_batch_generator() tester._make_model() preds = [] tmpp = 0 with torch.no_grad(): for itr, input_img in enumerate(tqdm(tester.batch_generator)): # forward coord_out = tester.model(input_img) if cfg.flip_test: flipped_input_img = flip(input_img, dims=3) flipped_coord_out = tester.model(flipped_input_img) flipped_coord_out[:, :, 0] = cfg.output_shape[ 1] - flipped_coord_out[:, :, 0] - 1 for pair in tester.flip_pairs: flipped_coord_out[:, pair[0], :], flipped_coord_out[:, pair[ 1], :] = flipped_coord_out[:, pair[1], :].clone( ), flipped_coord_out[:, pair[0], :].clone() coord_out = (coord_out + flipped_coord_out) / 2. vis = True if vis: filename = str(itr) tmpimg = input_img[0].cpu().numpy() tmpimg = tmpimg * np.array(cfg.pixel_std).reshape( 3, 1, 1) + np.array(cfg.pixel_mean).reshape(3, 1, 1) tmpimg = tmpimg.astype(np.uint8) tmpimg = tmpimg[::-1, :, :] tmpimg = np.transpose(tmpimg, (1, 2, 0)).copy() tmpkps = np.zeros((3, tester.joint_num)) tmpkps[:2, :] = coord_out[0, :, :2].cpu().numpy().transpose( 1, 0) / cfg.output_shape[0] * cfg.input_shape[0] tmpkps[2, :] = 1 tmpimg = vis_keypoints(tmpimg, tmpkps, tester.skeleton) # print(tmpkps) cv2.imwrite(filename + '_output.jpg', tmpimg) coord_out = coord_out.cpu().numpy() preds.append(coord_out) tmpp += 1 if tmpp == 5: break # evaluate preds = np.concatenate(preds, axis=0) # print(preds.shape) # It takes testest from common/base.py # It then calls evaluate of that dataset # from data/MSCOCO/MSCOCO.py call visualise keypoints # from common/utils/vis.py the plts are plotted tester._evaluate(preds, cfg.result_dir)
def test(test_model,vis_kps,run_nr,make_stats,gpu_id): cfg.set_args(gpu_id) if make_stats: base_log_dir = cfg.log_dir # due to cocoeval format for keypoints index = np.asarray([0,1,2,5,6,7,8,9,10],dtype=np.int32) header = ["AP@[IoU=0.5:0.95]", "AP@[IoU=0.5]", "AP@[IoU=0.75]", "AR@[IoU=0.5:0.95]", "AR@[IoU=0.5]", "AR@[IoU=0.75]", "AP@[IoU=0.5:0.95,easy]", "AP@[IoU=0.5:0.95,medium]", "AP@[IoU=0.5:0.95,hard]"] available_runs = glob(os.path.join(cfg.model_dump_dir,"run_*")) if not all([os.path.isdir(run) for run in available_runs]): print("Not all subdirs are valid dirs, exit.") exit(0) print("Found {} runs. Appply testing on all.".format(len(available_runs))) results = np.zeros([len(available_runs),index.shape[0]]) for nr,run in enumerate(available_runs): eval_dir = osp.join(cfg.result_dir, "run_{}".format(nr+1)) if not osp.isdir(eval_dir): os.makedirs(eval_dir) snapshots = glob(osp.join(run,"snapshot_*.ckpt.*")) nr_snapshots = len(snapshots) // 3 stats = np.zeros([nr_snapshots,index.shape[0]]) sn_index = ["@ckpt#{}".format(n+1) for n in range(nr_snapshots)] for sn_nr in range(nr_snapshots): # annotation load d = Dataset(train=False) annot = d.load_annot(cfg.testset) gt_img_id = d.load_imgid(annot) cfg.log_dir = osp.join(base_log_dir,"eval","run_{}".format(nr+1),"sn_{}".format(sn_nr+1)) if not osp.isdir(cfg.log_dir): os.makedirs(cfg.log_dir) # human bbox load if cfg.useGTbbox and cfg.testset in ['train', 'val']: if cfg.testset == 'train': dets = d.load_test_data(score=True) else: dets = d.load_val_data_with_annot() dets.sort(key=lambda x: (x['image_id'])) else: with open(cfg.human_det_path, 'r') as f: dets = json.load(f) dets = [i for i in dets if i['image_id'] in gt_img_id] dets = [i for i in dets if i['category_id'] == 1] dets = [i for i in dets if i['score'] > 0] dets.sort(key=lambda x: (x['image_id'], x['score']), reverse=True) img_id = [] for i in dets: img_id.append(i['image_id']) imgname = d.imgid_to_imgname(annot, img_id, cfg.testset) for i in range(len(dets)): dets[i]['imgpath'] = imgname[i] det_range = [0, len(dets)] tester = Tester(Model(), cfg) tester.load_weights(sn_nr+1, model_dump_dir=run) result = test_net(tester, dets, det_range, int(gpu_id), d.sigmas, vis_kps) tester.sess.close() tf.reset_default_graph() del tester # result = func(gpu_id) # evaluation cocoeval = d.evaluation_stats(result, annot, eval_dir) stats[sn_nr,:] = cocoeval.stats[index] print("Finished! Actual stats are:") print(cocoeval.stats[index]) del d # get best result with respect to ap 0.5:0.95 best_id = np.argmax(stats[:,0]) results[nr,:] = stats[best_id,:] # store actual results to csv df = pd.DataFrame(data=stats,columns=header,index=sn_index) df.to_csv(osp.join(eval_dir,"stats_all.csv"),index=True) # add f1-score computation header +=["F1@[IoU=0.5:0.95]"] #compute f1-scores for every run f1_scores = 2*(results[:,0]*results[:,3])/(results[:,0]+results[:,3]) f1_scores = np.expand_dims(f1_scores,axis=1) results = np.concatenate([results,f1_scores],axis=1) # compute stats and store to csv file mean = np.mean(results,axis=0) if len(available_runs)<2: std = np.zeros_like(mean,dtype=np.float) else: # compute unbiased variant of std, as the number of samples is not large std = np.std(results,axis=0,ddof=1) best = results[np.argmax(results[:,0]),:] out = np.stack([best,mean,std],axis=1) out=out.transpose() out_df = pd.DataFrame(data=out,columns=header,index=["best","mean","stddev"]) out_df.to_csv(osp.join(cfg.result_dir,"results_final.csv"),index=True) #store all results run_idx = ["run_{}".format(r+1) for r in range(len(available_runs))] all_df = pd.DataFrame(data=results,columns=header,index=run_idx) all_df.to_csv(osp.join(cfg.result_dir,"results_list.csv"),index=True) # store final results header.append(["mean",""]) else: # annotation load d = Dataset(train=False) annot = d.load_annot(cfg.testset) gt_img_id = d.load_imgid(annot) # human bbox load if cfg.useGTbbox and cfg.testset in ['train', 'val']: if cfg.testset == 'train': dets = d.load_test_data(score=True) else: dets = d.load_val_data_with_annot() dets.sort(key=lambda x: (x['image_id'])) else: with open(cfg.human_det_path, 'r') as f: dets = json.load(f) dets = [i for i in dets if i['image_id'] in gt_img_id] dets = [i for i in dets if i['category_id'] == 1] dets = [i for i in dets if i['score'] > 0] dets.sort(key=lambda x: (x['image_id'], x['score']), reverse=True) img_id = [] for i in dets: img_id.append(i['image_id']) imgname = d.imgid_to_imgname(annot, img_id, cfg.testset) for i in range(len(dets)): dets[i]['imgpath'] = imgname[i] det_range = [0, len(dets)] model_dump_dir = osp.join(cfg.model_dump_dir, "run_{}".format(run_nr)) assert osp.isdir(model_dump_dir) # evaluation cfg.set_args(gpu_id) tester = Tester(Model(), cfg) tester.load_weights(test_model,model_dump_dir=model_dump_dir) result = test_net(tester, dets, det_range, int(gpu_id),d.sigmas,vis_kps) eval_dir = osp.join(cfg.result_dir,"run_{}".format(run_nr)) if not osp.isdir(eval_dir): os.makedirs(eval_dir) d.evaluation(result, annot, eval_dir, cfg.testset)
def main(): args = parse_args() cfg.set_args(args.gpu_ids) cudnn.fastest = True cudnn.benchmark = True cudnn.deterministic = False cudnn.enabled = True tester = Tester(24) # The pretrained model used is assumed to be Human36M keypoints and skeleton. Change if a different model is used. tester.joint_num = 18 tester.skeleton = ( (0, 7), (7, 8), (8, 9), (9, 10), (8, 11), (11, 12), (12, 13), (8, 14), (14, 15), (15, 16), (0, 1), (1, 2), (2, 3), (0, 4), (4, 5), (5, 6)) #tester._make_batch_generator() ##load 3D pose estimation model tester._make_model() ##loading yolo detector """detector = YOLOv3( model_def="/data1/cx/project/3DMPPE_POSENET_RELEASE/common/detectors/yolo/config/yolov3.cfg", class_path="/data1/cx/project/3DMPPE_POSENET_RELEASE/common/detectors/yolo/data/coco.names", weights_path="/data1/cx/project/3DMPPE_POSENET_RELEASE/common/detectors/yolo/weights/yolov3.weights", classes=('person',), max_batch_size=16, device=torch.device('cuda:{}'.format(cfg.gpu_ids[0])))""" preds = [] with torch.no_grad(): itr = 1 # iteration imgpath = 'D:\\Robot Dance\\AI-Project-Portfolio\\3DMPPE_POSENET_RELEASE\\image\\test.jpg' # imgpath = '/data1/cx/project/3DMPPE_POSENET_RELEASE/image/test.jpg' bbox = (0, 0, 256, 256) input_img = prepare_input(imgpath, bbox) # forward coord_out = tester.model(input_img) print('coord out shape:', coord_out.shape) vis = True if vis: filename = str(itr) tmpimg = input_img[0].cpu().numpy() tmpimg = tmpimg * np.array(cfg.pixel_std).reshape(3,1,1) + np.array(cfg.pixel_mean).reshape(3,1,1) tmpimg = tmpimg.astype(np.uint8) tmpimg = tmpimg[::-1, :, :] tmpimg = np.transpose(tmpimg,(1,2,0)).copy() tmpkps = np.zeros((3,tester.joint_num)) tmpkps[:2,:] = coord_out[0,:,:2].cpu().numpy().transpose(1,0) / cfg.output_shape[0] * cfg.input_shape[0] tmpkps[2,:] = 1 tmpimg = vis_keypoints(tmpimg, tmpkps, tester.skeleton) cv2.imwrite(filename + '_output.jpg', tmpimg) # cv2.imshow('img', tmpimg) # cv2.waitKey(0) # cv2.destroyAllWindows() coord_out = coord_out.cpu().numpy() vis_3d=True if vis_3d: pred=coord_out.squeeze() #remove first batch dimension plot_3dkeypoints(pred, tester.skeleton) preds.append(coord_out) # evaluate preds = np.concatenate(preds, axis=0)
self.add_tower_summary('loss', total_loss) self.set_loss(total_loss) else: self.set_outputs(refine_out) if __name__ == '__main__': def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-d', type=str, dest='gpu_ids') parser.add_argument('--continue', '-c', dest='continue_train', action='store_true') parser.add_argument('--type', '-t', type=str, dest='cloth_type') parser.add_argument('--debug', dest='debug', action='store_true') args = parser.parse_args() if not args.gpu_ids: args.gpu_ids = str(np.argmin(mem_info())) if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = 0 if not gpus[0].isdigit() else int(gpus[0]) gpus[1] = len(mem_info()) if not gpus[1].isdigit() else int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) return args args = parse_args() cfg.set_args(args.gpu_ids, args.continue_train, args.cloth_type) trainer = Trainer(Network(), cfg) trainer.train()
if __name__ == '__main__': def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-d', type=str, dest='gpu_ids') parser.add_argument('--continue', '-c', dest='continue_train', action='store_true') parser.add_argument('--debug', dest='debug', action='store_true') args = parser.parse_args() if not args.gpu_ids: args.gpu_ids = str(np.argmin(mem_info())) if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = 0 if not gpus[0].isdigit() else int(gpus[0]) gpus[1] = len( mem_info()) if not gpus[1].isdigit() else int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) return args args = parse_args() cfg.set_args(args.gpu_ids, args.continue_train) trainer = Trainer(Network(), cfg) trainer.train()
parser = argparse.ArgumentParser() parser.add_argument('--gpu', '-d', type=str, dest='gpu_ids') parser.add_argument('--continue', '-c', dest='continue_train', action='store_true') parser.add_argument('--debug', dest='debug', action='store_true') parser.add_argument('--batch_size', type=int, dest='batch_size') parser.add_argument('--epoch_size', type=int, dest='epoch_size') parser.add_argument('--num_epochs', type=int, dest='num_epochs') args = parser.parse_args() if not args.gpu_ids: args.gpu_ids = str(np.argmin(mem_info())) if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = 0 if not gpus[0].isdigit() else int(gpus[0]) gpus[1] = len( mem_info()) if not gpus[1].isdigit() else int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) return args args = parse_args() cfg.set_args(args.gpu_ids, args.continue_train, args.batch_size, args.epoch_size, args.num_epochs) trainer = Trainer(Network(), cfg) trainer.train()
assert 0, print("Please set proper gpu ids") if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = 0 if not gpus[0].isdigit() else int(gpus[0]) gpus[1] = len( mem_info()) if not gpus[1].isdigit() else int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) assert args.test_epoch, 'Test epoch is required.' return args # argument parsing args = parse_args() cfg.set_args(args.gpu_ids) cudnn.benchmark = True # MuCo joint set joint_num = 21 joints_name = ('Head_top', 'Thorax', 'R_Shoulder', 'R_Elbow', 'R_Wrist', 'L_Shoulder', 'L_Elbow', 'L_Wrist', 'R_Hip', 'R_Knee', 'R_Ankle', 'L_Hip', 'L_Knee', 'L_Ankle', 'Pelvis', 'Spine', 'Head', 'R_Hand', 'L_Hand', 'R_Toe', 'L_Toe') flip_pairs = ((2, 5), (3, 6), (4, 7), (8, 11), (9, 12), (10, 13), (17, 18), (19, 20)) skeleton = ((0, 16), (16, 1), (1, 15), (15, 14), (14, 8), (14, 11), (8, 9), (9, 10), (10, 19), (11, 12), (12, 13), (13, 20), (1, 2), (2, 3), (3, 4), (4, 17), (1, 5), (5, 6), (6, 7), (7, 18)) # snapshot load
def func(gpu_id): cfg.set_args(args.gpu_ids.split(',')[gpu_id]) tester = Tester(Model(), cfg) tester.load_weights(test_model) range = [ranges[gpu_id], ranges[gpu_id + 1]] return test_net(tester, dets, range, gpu_id)
def parse_args(): parser = argparse.ArgumentParser() parser.add_argument('--gpu', type=str, dest='gpu_ids') parser.add_argument('--continue', dest='continue_train', action='store_true') parser.add_argument("--cnt_val_it", type=int, dest="cnt_val_itr", default=-1) args = parser.parse_args() if not args.gpu_ids: args.gpu_ids = str(np.argmin(mem_info())) if '-' in args.gpu_ids: gpus = args.gpu_ids.split('-') gpus[0] = 0 if not gpus[0].isdigit() else int(gpus[0]) gpus[1] = len( mem_info()) if not gpus[1].isdigit() else int(gpus[1]) + 1 args.gpu_ids = ','.join(map(lambda x: str(x), list(range(*gpus)))) return args args = parse_args() cfg.set_args(args.gpu_ids, args.cnt_val_itr, args.continue_train) trainer = Trainer(Model(), cfg) trainer.train()