def main(): parser = argparse.ArgumentParser(description="PyTorch Object Detection Training") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", type=str, ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--skip-test", dest="skip_test", help="Do not test the final model", action="store_true", ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 args.distributed = num_gpus > 1 if args.distributed: torch.cuda.set_device(args.local_rank) torch.distributed.deprecated.init_process_group( backend="nccl", init_method="env://" ) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() output_dir = cfg.OUTPUT_DIR if output_dir: mkdir(output_dir) logger = setup_logger("maskrcnn_benchmark", output_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(args) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) logger.info("Loaded configuration file {}".format(args.config_file)) with open(args.config_file, "r") as cf: config_str = "\n" + cf.read() logger.info(config_str) logger.info("Running with config:\n{}".format(cfg)) model = train(cfg, args.local_rank, args.distributed) if not args.skip_test: test(cfg, model, args.distributed)
def main(val_path, obj_id): parser = argparse.ArgumentParser( description="PyTorch Object Detection Webcam Demo") parser.add_argument( "--config-file", default="../configs/caffe2/keypoints_R_101_FPN.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--confidence-threshold", type=float, default=0.7, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=224, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument( "--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show", ) parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=args.show_mask_heatmaps, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, ) print("testing ...") # val_path='./../datasets/linemod/ape_train' # val_list=glob.glob(val_path+'/*.png') # val_label=os.path.join(val_path,'val.txt') # metric val_img_list = glob.glob(val_path + '/test_data/*-color.png') # K = np.array([[572.4114, 0., 325.2611], # [0., 573.57043, 242.04899], # [0., 0., 1.]]) add_v = 0 rep_v = 0 length = 0 # obj = {1: 2, # 2: 3, # 3: 4, # 4: 5, # 5: 6, # 6: 7, # 7: 8, # 8: 9, # 9: 10, # 10: 11, # 11: 19, # 12:21, # 13:24, # 14:25, # 16:35, # 16:36, # 17:37, # 18:40, # 19:51, # 20:52, # 21:61} for imgpath in val_img_list[:]: # per_dict = {} img = cv2.imread(imgpath) imgname = imgpath.split('/')[-1] # print(imgname) img = cv2.imread(imgpath) try: labels, box, score, kpts = coco_demo.run_on_opencv_image(img) labels_np = labels.cpu().numpy() np_kpts = kpts ind = np.where(labels_np == obj_id) # print(labels_np ) if len(ind[0]) == 0: continue # print(imgname) # print(len(ind[0])) # print(imgname) obj_kpts = np_kpts[ind[0][0]] # [8,3] add, rep = error_cp(val_path, obj_id, imgname, obj_kpts) except: continue length += 1 # print(length) if add: add_v += 1 if rep: rep_v += 1 print("ADD metric:{}".format(add_v / length)) print("REP metric:{}".format(rep_v / length)) return add_v / length, rep_v / length
import os import cv2 import numpy as np from maskrcnn_benchmark.config import cfg from demo.predictor import ICDARDemo, RRPNDemo from maskrcnn_benchmark.utils.visualize import vis_image, write_result_ICDAR_RRPN2polys, zip_dir from PIL import Image import time config_file = "./configs/rrpn/e2e_rrpn_R_50_C4_1x_ICDAR13_15_17_trial_again_test.yaml" # update the config options with the config file cfg.merge_from_file(config_file) # manual override some options cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) # cfg.freeze() # cfg.MODEL.WEIGHT = 'models/IC-13-15-17-Trial/model_0155000.pth' result_dir = os.path.join('results', config_file.split('/')[-1].split('.')[0], cfg.MODEL.WEIGHT.split('/')[-1].split('.')[0]) if not os.path.isdir(result_dir): os.makedirs(result_dir) coco_demo = RRPNDemo( cfg, min_image_size=1000, confidence_threshold=0.85, )
def main(): # apply_prior prior_mask # 0 - - # 1 Y - # 2 - Y # 3 Y Y parser = argparse.ArgumentParser( description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--ckpt", help= "The path to the checkpoint for test, default is the latest checkpoint.", default=None, ) parser.add_argument( "--dataset_name", help="vcoco_test or vcoco_val_test", default=None, ) parser.add_argument('--num_iteration', dest='num_iteration', help='Specify which weight to load', default=-1, type=int) parser.add_argument('--object_thres', dest='object_thres', help='Object threshold', default=0.1, type=float) # used to be 0.4 or 0.05 parser.add_argument('--human_thres', dest='human_thres', help='Human threshold', default=0.8, type=float) parser.add_argument('--prior_flag', dest='prior_flag', help='whether use prior_flag', default=1, type=int) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 and torch.cuda.is_available() if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() print('prior flag: {}'.format(args.prior_flag)) ROOT_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) # DATA_DIR = os.path.abspath(os.path.join(ROOT_DIR, 'Data')) args.config_file = os.path.join(ROOT_DIR, args.config_file) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = "" logger = setup_logger("DRG", save_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) # model.to(cfg.MODEL.DEVICE) device = torch.device( "cuda") if torch.cuda.is_available() else torch.device("cpu") model.to(device) # Initialize mixed-precision if necessary use_mixed_precision = cfg.DTYPE == 'float16' amp_handle = amp.init(enabled=use_mixed_precision, verbose=cfg.AMP_VERBOSE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) if args.num_iteration != -1: args.ckpt = os.path.join(cfg.OUTPUT_DIR, 'model_%07d.pth' % args.num_iteration) ckpt = cfg.MODEL.WEIGHT if args.ckpt is None else args.ckpt logger.info("Testing checkpoint {}".format(ckpt)) _ = checkpointer.load(ckpt, use_latest=args.ckpt is None) # iou_types = ("bbox",) output_folders = [None] * len(cfg.DATASETS.TEST) # dataset_names = cfg.DATASETS.TEST dataset_names = (args.dataset_name, ) if cfg.OUTPUT_DIR: for idx, dataset_name in enumerate(dataset_names): if args.num_iteration != -1: output_folder = os.path.join(cfg.OUTPUT_DIR, "inference_sp", dataset_name, "model_%07d" % args.num_iteration) else: output_folder = os.path.join(cfg.OUTPUT_DIR, "inference_sp", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder opt = {} # opt['word_dim'] = 300 for output_folder, dataset_name in zip(output_folders, dataset_names): data = DatasetCatalog.get(dataset_name) data_args = data["args"] im_dir = data_args['im_dir'] test_detection = pickle.load(open(data_args['test_detection_file'], "rb"), encoding='latin1') prior_mask = pickle.load(open(data_args['prior_mask'], "rb"), encoding='latin1') action_dic = json.load(open(data_args['action_index'])) action_dic_inv = {y: x for x, y in action_dic.items()} vcoco_test_ids = open(data_args['vcoco_test_ids_file'], 'r') test_image_id_list = [int(line.rstrip()) for line in vcoco_test_ids] vcocoeval = VCOCOeval(data_args['vcoco_test_file'], data_args['ann_file'], data_args['vcoco_test_ids_file']) word_embeddings = pickle.load(open(data_args['word_embedding_file'], "rb"), encoding='latin1') output_file = os.path.join(output_folder, 'detection.pkl') output_dict_file = os.path.join( output_folder, 'detection_human_{}_new.pkl'.format(dataset_name)) logger.info("Output will be saved in {}".format(output_file)) logger.info("Start evaluation on {} dataset({} images).".format( dataset_name, len(test_image_id_list))) run_test(model, dataset_name=dataset_name, im_dir=im_dir, test_detection=test_detection, word_embeddings=word_embeddings, test_image_id_list=test_image_id_list, prior_mask=prior_mask, action_dic_inv=action_dic_inv, output_file=output_file, output_dict_file=output_dict_file, object_thres=args.object_thres, human_thres=args.human_thres, prior_flag=args.prior_flag, device=device, cfg=cfg) synchronize() vcocoeval._do_eval(output_file, ovr_thresh=0.5)
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default= "/private/home/fmassa/github/detectron.pytorch_v2/configs/e2e_faster_rcnn_R_50_C4_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--ckpt", help= "The path to the checkpoint for test, default is the latest checkpoint.", default=None, ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = cfg.OUTPUT_DIR # "" filename = f'test_log_{pathlib.PurePath(args.ckpt) if args.ckpt is not None else "last"}.txt' test_result_filename = os.path.join( save_dir, pathlib.PurePath(filename).stem + '.pickle') logger = setup_logger("maskrcnn_benchmark", save_dir, get_rank(), filename=filename) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) model.to(cfg.MODEL.DEVICE) # Initialize mixed-precision if necessary use_mixed_precision = cfg.DTYPE == 'float16' amp_handle = amp.init(enabled=use_mixed_precision, verbose=cfg.AMP_VERBOSE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) ckpt = cfg.MODEL.WEIGHT if args.ckpt is None else os.path.join( save_dir, args.ckpt) _ = checkpointer.load(ckpt, use_latest=args.ckpt is None) iou_types = () if cfg.MODEL.MASK_ON: iou_types = iou_types + ("segm", ) if cfg.MODEL.KEYPOINT_ON: iou_types = iou_types + ("keypoints", ) output_folders = [None] * len(cfg.DATASETS.TEST) dataset_names = cfg.DATASETS.TEST if cfg.OUTPUT_DIR: for idx, dataset_name in enumerate(dataset_names): output_folder = os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=distributed) for output_folder, dataset_name, data_loader_val in zip( output_folders, dataset_names, data_loaders_val): inference( model, data_loader_val, dataset_name=dataset_name, iou_types=iou_types, box_only=False if cfg.MODEL.RETINANET_ON else cfg.MODEL.RPN_ONLY, bbox_aug=cfg.TEST.BBOX_AUG.ENABLED, device=cfg.MODEL.DEVICE, expected_results=cfg.TEST.EXPECTED_RESULTS, expected_results_sigma_tol=cfg.TEST.EXPECTED_RESULTS_SIGMA_TOL, output_folder=output_folder, test_result_filename=test_result_filename, ) synchronize()
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Inference") parser.add_argument( '--test_path', default=None, help='test model path or a single model weight file, if it is a path, will test all models inside the path' ) parser.add_argument( "--config-file", default="/private/home/fmassa/github/detectron.pytorch_v2/configs/e2e_faster_rcnn_R_50_C4_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) parser.add_argument("--gpuid", default='0', help='set gpu id') args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group( backend="nccl", init_method="env://" ) else: os.environ["CUDA_VISIBLE_DEVICES"] = args.gpuid #pdb.set_trace() if args.test_path is not None: _cfg = _find_yaml_in_dir(args.test_path, os.path.isdir(args.test_path)) model_list = _get_models_in_dir(args.test_path) save_dir = args.test_path if os.path.isdir( args.test_path) else '/'.join((args.test_path).split('/')[:-1]) else: _cfg = args.config_file model_list = [cfg.MODEL.WEIGHT] save_dir = "" #pdb.set_trace() cfg.merge_from_file(_cfg) cfg.merge_from_list(args.opts) cfg.freeze() logger = setup_logger("maskrcnn_benchmark", save_dir, get_rank(), test=True) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) collect_evaluate_results = {} for model_path in model_list: results = test_once(cfg, save_dir, model_path, distributed) # pdb.set_trace() if cfg.DATASETS.MODE_IN_TEST =='test' and \ 'isbi' in args.config_file: sio.savemat(os.path.join(save_dir, 'isbi2015_%s.mat'%model_path.split( '/')[-1].strip('.pth') ), results[0]) continue else: collect_evaluate_results[model_path] = results[0].results['segm'] if cfg.DATASETS.MODE_IN_TEST == 'test' and 'isbi' in args.config_file: return 0 # pdb.set_trace() # pdb.set_trace() # todo: prepare print x_list = [] AJI = [] MAP = [] AP50 = [] AP75 = [] AP85 = [] best_student ={} best_teacher = {} for k,v in collect_evaluate_results.items(): if 't_model' not in k: name = k.split('/')[-1].strip('.pth').strip('model_') if 'fina' in name: continue name = int(name) x_list.append(name) aji = (v['AJI']['nuclei'] + v['AJI']['cytoplasm'])/2 map = v['mAP']['all'] ap50 = v['AP50']['all'] ap75 = v['AP75']['all'] ap85 = v['AP85']['all'] AJI.append(aji) MAP.append(map) AP50.append(ap50) AP75.append(ap75) AP85.append(ap85) plt.plot(x_list, AJI,color='green',label='AJI') plt.plot(x_list,MAP,color='red',label='mAP') plt.plot(x_list,AP50,color = 'skyblue',label='AP50') plt.plot(x_list,AP75,color='blue',label='AP75') plt.plot(x_list, AP85, color='black', label='AP85') # show number anno_xs = [] anno_map = [] anno_aji = [] # pdb.set_trace() idxmap = MAP.index(max(MAP)) idxaji = AJI.index(max(AJI)) anno_xs.append(x_list[idxmap]) anno_map.append(MAP[idxmap]) anno_aji.append(AJI[idxmap]) anno_xs.append(x_list[idxaji]) anno_map.append(MAP[idxaji]) anno_aji.append(AJI[idxaji]) best_student['map_best'] = {x_list[idxmap]: {'map':MAP[idxmap], 'aji':AJI[idxmap]}} best_student['aji_best'] = {x_list[idxaji ]: {'map':MAP[idxaji ], 'aji':AJI[idxaji ]}} for a, b in zip(anno_xs, anno_aji): plt.annotate('(%.0f,%.4f)'%(a,b), (a,b)) for a, b in zip(anno_xs, anno_map): plt.annotate('(%.0f,%.4f)'%(a,b), (a,b)) # for teacher x_list = [] AJI = [] MAP = [] AP50 = [] AP75 = [] AP85 = [] for k, v in collect_evaluate_results.items(): if 't_model' in k: name = k.split('/')[-1].strip('.pth').strip('model_') if 'fina' in name: continue name = int(name) x_list.append(name) aji = (v['AJI']['nuclei'] + v['AJI']['cytoplasm']) / 2 map = v['mAP']['all'] ap50 = v['AP50']['all'] ap75 = v['AP75']['all'] ap85 = v['AP85']['all'] AJI.append(aji) MAP.append(map) AP50.append(ap50) AP75.append(ap75) AP85.append(ap85) if len(AJI)>0: plt.plot(x_list, AJI, '--', color='green', label='AJI') plt.plot(x_list, MAP,'--', color='red', label='mAP') plt.plot(x_list, AP50,'--', color='skyblue', label='AP50') plt.plot(x_list, AP75,'--', color='blue', label='AP75') plt.plot(x_list, AP85, '--',color='black', label='AP85') # show number anno_xs = [] anno_map = [] anno_aji = [] # pdb.set_trace() idxmap = MAP.index(max(MAP)) idxaji = AJI.index(max(AJI)) anno_xs.append(x_list[idxmap]) anno_map.append(MAP[idxmap]) anno_aji.append(AJI[idxmap]) anno_xs.append(x_list[idxaji]) anno_map.append(MAP[idxaji]) anno_aji.append(AJI[idxaji]) best_teacher['map_best'] = {x_list[idxmap]: {'map': MAP[idxmap], 'aji': AJI[idxmap]}} best_teacher['aji_best'] = {x_list[idxaji]: {'map': MAP[idxaji], 'aji': AJI[idxaji]}} for a, b in zip(anno_xs, anno_aji): plt.annotate('(%.0f,%.4f)' % (a, b), (a, b)) for a, b in zip(anno_xs, anno_map): plt.annotate('(%.0f,%.4f)' % (a, b), (a, b)) plt.legend() plt.savefig(os.path.join(save_dir,"result.jpg")) with open(os.path.join(save_dir, 'result.json'), 'w') as f: json.dump(collect_evaluate_results, f) # write best result with open(os.path.join(save_dir, 'best_result.json'), 'w') as f: json.dump({'student':best_student, 'teacher':best_teacher},f)
plt.imshow(img[:, :, [2, 1, 0]]) plt.axis("off") plt.title('Prediction of {name}'.format(name=img_name), fontsize='xx-large') plt.show() if __name__ == '__main__': start = time.time() config_path = '/home/xia/maskrcnn-benchmark/configs/e2e_mask_rcnn_R_101_FPN_1x_predictor.yaml' img_path = '/home/xia/bobbins/data/test/rgb/' pylab.rcParams['figure.figsize'] = (12.0, 9.0) # update the config options with the config file cfg.merge_from_file(config_path) # manual override some options cfg.merge_from_list(["MODEL.DEVICE", "cpu"]) bobbin_demo = BobbinsDemo( cfg, min_image_size=720, confidence_threshold=0.95, ) # load image #imgId = np.random.randint(0,200) #imgId = 400 + imgId imgId = np.random.randint(0, 20) img_name = 'rgb_' + '{:0>4d}'.format(imgId) + '.png' img_file = img_path + img_name image = load(img_file)
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default= "/workspace/object_detection/configs/e2e_mask_rcnn_R_50_FPN_1x.yaml", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--skip-eval", dest="skip_eval", help="Do not eval the predictions", action="store_true", ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = "" logger = setup_logger("maskrcnn_benchmark", save_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) model.to(cfg.MODEL.DEVICE) # Initialize mixed-precision if necessary use_mixed_precision = cfg.DTYPE == 'float16' amp_handle = amp.init(enabled=use_mixed_precision, verbose=cfg.AMP_VERBOSE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) _ = checkpointer.load(cfg.MODEL.WEIGHT) iou_types = ("bbox", ) if cfg.MODEL.MASK_ON: iou_types = iou_types + ("segm", ) output_folders = [None] * len(cfg.DATASETS.TEST) dataset_names = cfg.DATASETS.TEST if cfg.OUTPUT_DIR: for idx, dataset_name in enumerate(dataset_names): output_folder = os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=distributed) for output_folder, dataset_name, data_loader_val in zip( output_folders, dataset_names, data_loaders_val): inference( model, data_loader_val, dataset_name=dataset_name, iou_types=iou_types, box_only=cfg.MODEL.RPN_ONLY, device=cfg.MODEL.DEVICE, expected_results=cfg.TEST.EXPECTED_RESULTS, expected_results_sigma_tol=cfg.TEST.EXPECTED_RESULTS_SIGMA_TOL, output_folder=output_folder, skip_eval=args.skip_eval, ) synchronize()
def main(): # Build a parser for arguments parser = argparse.ArgumentParser(description="Action Prediction Training") parser.add_argument( "--config-file", default= "/home/SelfDriving/maskrcnn/maskrcnn-benchmark/configs/e2e_faster_rcnn_R_50_C4_1x.yaml", metavar="FILE", help="path to maskrcnn_benchmark config file", type=str, ) # Just in order to get outdir parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) parser.add_argument( "--imageroot", type=str, help="Directory to the images", default= "/home/SelfDriving/maskrcnn/maskrcnn-benchmark/datasets/bdd100k/features/val" ) parser.add_argument( "--gtroot", type=str, help="Directory to the groundtruth", default= "/home/SelfDriving/maskrcnn/maskrcnn-benchmark/datasets/bdd100k/annotations/val_gt_action.json" ) parser.add_argument( "--model_root", type=str, help="Directory to the trained model", default="/data6/SRIP19_SelfDriving/Outputs/trained/netFinal_200.pth") parser.add_argument("--imWidth", type=int, help="Crop to width", default=1280) parser.add_argument("--imHeight", type=int, help="Crop to height", default=720) parser.add_argument("--batch_size", type=int, help="Batch Size", default=1) parser.add_argument("--experiment", type=str, help="Give this experiment a name", default=str(datetime.datetime.now())) args = parser.parse_args() print(args) # cfg.OUTPUT_DIR = "/data6/SRIP_SelfDriving/Outputs/" # cfg.MODEL.WEIGHT = "/data6/SRIP_SelfDriving/Outputs/model_final.pth" cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) print(cfg) cfg.freeze() if torch.cuda.is_available(): print("CUDA device is available.") test(cfg, args)
"/mrcnn_integrate/train_tools/pretrained_models/e2e_mask_rcnn_R-50-FPN_1x_no_last.pth", help="path to save the converted model", type=str, ) parser.add_argument( "--cfg", default= "/mrcnn_integrate/config/e2e_mask_rcnn_R_50_FPN_1x_caffe2_stick.yaml", help="path to config file", type=str, ) args = parser.parse_args() # DETECTRON_PATH = os.path.expanduser(args.pretrained_path) print('detectron path: {}'.format(DETECTRON_PATH)) cfg.merge_from_file(args.cfg) _d = load_c2_format(cfg, DETECTRON_PATH) newdict = _d newdict['model'] = removekey(_d['model'], [ 'cls_score.bias', 'cls_score.weight', 'bbox_pred.bias', 'bbox_pred.weight', 'mask_fcn_logits.weight', 'mask_fcn_logits.bias' ]) torch.save(newdict, args.save_path) print('saved to {}.'.format(args.save_path)) if __name__ == '__main__': pass
def main(): # Build a parser for arguments parser = argparse.ArgumentParser(description="Action Prediction Training") parser.add_argument( "--config-file", default= "/home/SelfDriving/maskrcnn/maskrcnn-benchmark/configs/baseline.yaml", metavar="FILE", help="path to maskrcnn_benchmark config file", type=str, ) parser.add_argument( "--weight_decay", default=1e-4, help="Weight decay", ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) parser.add_argument("--initLR", help="Initial learning rate", default=0.0001) parser.add_argument( "--freeze", default=False, help="If freeze faster rcnn", ) parser.add_argument("--imageroot", type=str, help="Directory to the images", default="/data6/SRIP19_SelfDriving/bdd12k/data1/") parser.add_argument( "--gtroot", type=str, help="Directory to the groundtruth", default= "/data6/SRIP19_SelfDriving/bdd12k/annotations/12k_gt_train_5_actions.json" ) parser.add_argument( "--reasonroot", type=str, help="Directory to the explanations", default= "/data6/SRIP19_SelfDriving/bdd12k/annotations/train_reason_img.json") parser.add_argument("--imWidth", type=int, help="Crop to width", default=1280) parser.add_argument("--imHeight", type=int, help="Crop to height", default=720) parser.add_argument("--batch_size", type=int, help="Batch Size", default=1) parser.add_argument("--experiment", type=str, help="Give this experiment a name", default=str(datetime.datetime.now())) parser.add_argument( "--model_root", type=str, help="Directory to the trained model", default= "/data6/SRIP19_SelfDriving/bdd100k/trained_model/Outputs/model_final_apt.pth" ) parser.add_argument("--val", action='store_true', default=False, help='Validation or not') parser.add_argument("--num_epoch", default=20, help="The number of epoch for training", type=int) parser.add_argument("--from_checkpoint", default=False, help="If we need load weights from checkpoint.") parser.add_argument( "--checkpoint", default=".", help="The path to the checkpoint weights.", type=str, ) args = parser.parse_args() print(args) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() if torch.cuda.is_available(): print("CUDA device is available.") # output directory outdir = cfg.OUTPUT_DIR print("Save path:", outdir) if outdir: mkdir(outdir) # logger = setup_logger("training", outdir) train(cfg, args)
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Training") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", type=str, ) parser.add_argument( '--reduce-batch', type=int, help=('Divide IMS_PER_BATCH by this amount. This appropriately ' 'updates the learning rate, number of iterations, and so on.')) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--skip-test", dest="skip_test", help="Do not test the final model", action="store_true", ) parser.add_argument( "--use-tensorboard", dest="use_tensorboard", help="Use tensorboardX logger (Requires tensorboardX installed)", action="store_true", default=False) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # Generate a unique experiment ID for this run. # Note: uuid generation relies on os.urandom, so it is not affected by, # e.g., random.seed. experiment_id = uuid.uuid4() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 args.distributed = num_gpus > 1 if args.distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() cfg.merge_from_file(args.config_file) # We want to get the OUTPUT_DIR from the args immediately, if it exists, so # we can setup logging. We will merge the rest of the config in a few # lines. merge_keys(cfg, args.opts, ['OUTPUT_DIR']) output_dir = cfg.OUTPUT_DIR if output_dir: mkdir(output_dir) if get_rank() == 0: file_logger = common_setup(__file__, output_dir, args) else: file_logger = common_setup(__file__ + '-worker%s' % get_rank(), output_dir, args, log_console_level=logging.CRITICAL, save_git_state=False) # Automatically handle config changes as required by # https://github.com/facebookresearch/maskrcnn-benchmark/tree/327bc29bcc4924e35bd61c59877d5a1d25bb75af#single-gpu-training if args.reduce_batch: # Update using --opts first, then override. merge_keys(cfg, args.opts, [ 'SOLVER.IMS_PER_BATCH', 'SOLVER.BASE_LR', 'SOLVER.MAX_ITER', 'SOLVER.STEPS', 'SOLVER.CHECKPOINT_PERIOD' ]) assert num_gpus in (1, 2, 4) scale = args.reduce_batch logging.info('Updating config for # GPUs = %s', num_gpus) def update_config(key, new_value): key_list = key.split('.') d = cfg for subkey in key_list[:-1]: d = cfg[subkey] subkey = key_list[-1] old_value = d[subkey] logging.info('Updating cfg.%s: %s -> %s', key, old_value, new_value) d[subkey] = new_value update_config('SOLVER.IMS_PER_BATCH', _safe_int(cfg.SOLVER.IMS_PER_BATCH / scale)) update_config('SOLVER.BASE_LR', cfg.SOLVER.BASE_LR / scale) update_config('SOLVER.MAX_ITER', _safe_int(cfg.SOLVER.MAX_ITER * scale)) update_config('SOLVER.CHECKPOINT_PERIOD', _safe_int(cfg.SOLVER.CHECKPOINT_PERIOD * scale)) update_config('SOLVER.STEPS', tuple(_safe_int(x * scale) for x in cfg.SOLVER.STEPS)) logging.info('Updating config from arguments') cfg.merge_from_list(args.opts) cfg.freeze() logger = logging.getLogger("maskrcnn_benchmark") logger.info("Using {} GPUs".format(num_gpus)) logger.info(args) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) logger.info("Loaded configuration file {}".format(args.config_file)) with open(args.config_file, "r") as cf: config_str = "\n" + cf.read() file_logger.info(config_str) file_logger.info("Running with config:\n{}".format(cfg)) if get_rank() == 0: config_output = add_time_to_path(Path(output_dir) / 'config.yaml') with open(config_output, 'w') as f: f.write(cfg.dump()) logging.info('Experiment id: %s', experiment_id) with open(os.path.join(output_dir, 'experiment_id.txt'), 'w') as f: f.write('%s\n' % experiment_id) output_config_path = os.path.join(cfg.OUTPUT_DIR, 'config.yml') logger.info("Saving config into: {}".format(output_config_path)) # save overloaded model config in the output directory save_config(cfg, output_config_path) model = train(cfg, args.local_rank, args.distributed, use_tensorboard=args.use_tensorboard) if not args.skip_test: run_test(cfg, model, args.distributed)
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Webcam Demo") parser.add_argument( "--config-file", default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--predictions-out", default="./test.json", metavar="FILE", help="path to file to output labels", ) parser.add_argument( "--test-image-dir", default="/n/pana/scratch/ravi/bdd/bdd100k/images/100k/val/", metavar="FILE", help="path to test image directory", ) parser.add_argument( "--confidence-threshold", type=float, default=0.5, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=800, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument( "--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show", ) parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=args.show_mask_heatmaps, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, ) image_paths = glob.glob(os.path.join(args.test_image_dir, '*.jpg')) pred_list = [] coco_cat_to_bdd_cat = { "person": "person", "car": "car", "traffic light": "traffic light", "stop sign": "traffic sign", "bus": "bus", "truck": "truck", "bicycle": "bike", "motorcycle": "motor", "train": "train" } for i in tqdm(image_paths): img = cv2.imread(i) image_id = i.split('/')[-1].split('.')[0] start = time.time() predictions = coco_demo.compute_prediction(img) end = time.time() scores = predictions.get_field('scores') #high_conf_idx = scores > args.confidence_threshold #predictions = predictions[high_conf_idx] #scores = predictions.get_field('scores') boxes = predictions.bbox labels = predictions.get_field('labels') labels = [coco_demo.CATEGORIES[l] for l in labels] for b in range(len(labels)): if labels[b] in coco_cat_to_bdd_cat: label = coco_cat_to_bdd_cat[labels[b]] obj_dict = { 'name': image_id, 'bbox': [ float(boxes[b][0]), float(boxes[b][1]), float(boxes[b][2]), float(boxes[b][3]) ], 'category': label, 'score': float(scores[b]) } pred_list.append(obj_dict) with open(args.predictions_out, 'w') as fp: json.dump(pred_list, fp)
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Webcam Demo") parser.add_argument( "--config-file", default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--confidence-threshold", type=float, default=0.7, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=224, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument("--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show") parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=args.show_mask_heatmaps, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, ) video_file_id = "friends-unagi-640x480.mp4" print("Read: ", video_file_id) cam = cv2.VideoCapture(video_file_id) # We need to check if camera # is opened previously or not if (cam.isOpened() == False): print("Error reading video file") while (cam.isOpened()): start_time = time.time() ret_val, img = cam.read() composite = coco_demo.run_on_opencv_image(img) print("Time: {:.2f} s / img".format(time.time() - start_time)) cv2.imshow("COCO detections", composite) if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Training") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", type=str, ) parser.add_argument("--local_rank", type=int, default=os.getenv('LOCAL_RANK', 0)) parser.add_argument("--max_steps", type=int, default=0, help="Override number of training steps in the config") parser.add_argument( "--skip-test", dest="skip_test", help="Do not test the final model", action="store_true", ) parser.add_argument("--fp16", help="Mixed precision training", action="store_true") parser.add_argument("--amp", help="Mixed precision training", action="store_true") parser.add_argument('--skip_checkpoint', default=False, action='store_true', help="Whether to save checkpoints") parser.add_argument( "--json-summary", help="Out file for DLLogger", default="dllogger.out", type=str, ) parser.add_argument("--debug", type=distutils.util.strtobool, default=False, help="debug") parser.add_argument("--eval-loss", action="store_true", default=False, help="evaluate loss(very buggy)") parser.add_argument("--print-freq", type=int, default=100, help="print freq for tensorboard") parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() args.fp16 = args.fp16 or args.amp num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 args.distributed = num_gpus > 1 if args.distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) if args.debug: cfg.DEBUG = args.debug # Redundant option - Override config parameter with command line input if args.max_steps > 0: cfg.SOLVER.MAX_ITER = args.max_steps if args.skip_checkpoint: cfg.SAVE_CHECKPOINT = False cfg.freeze() output_dir = cfg.OUTPUT_DIR if output_dir: mkdir(output_dir) args.log_dir = save_path_formatter(args, cfg) logger = setup_logger("maskrcnn_benchmark", output_dir, get_rank()) if is_main_process(): dllogger.init(backends=[ dllogger.JSONStreamBackend(verbosity=dllogger.Verbosity.VERBOSE, filename=args.json_summary), dllogger.StdOutBackend(verbosity=dllogger.Verbosity.VERBOSE, step_format=format_step) ]) else: dllogger.init(backends=[]) dllogger.log(step="PARAMETER", data={"gpu_count": num_gpus}) # dllogger.log(step="PARAMETER", data={"environment_info": collect_env_info()}) dllogger.log(step="PARAMETER", data={"config_file": args.config_file}) with open(args.config_file, "r") as cf: config_str = "\n" + cf.read() dllogger.log(step="PARAMETER", data={"config": cfg}) if is_main_process(): args.writer = SummaryWriter(args.log_dir, flush_secs=30) if args.fp16: fp16 = True else: fp16 = False model, iters_per_epoch = train(cfg, args.local_rank, args.distributed, fp16, dllogger, args) if not args.skip_test: #if not cfg.PER_EPOCH_EVAL: test_model(cfg, model, args.distributed, iters_per_epoch, dllogger, args)
def image_mask(image): parser = argparse.ArgumentParser( description="PyTorch Object Detection Video") parser.add_argument( "--config-file", default= "lib/maskrcnn-benchmark/configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--confidence-threshold", type=float, default=0.7, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=224, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument( "--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show", ) parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) parser.add_argument( "--show-mask-binary", dest="show_mask_binary", help="show a binary mask image for the top masks-per-dim masks", action="store_true", ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo(cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=False, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, show_mask_binary=True) # cam = cv2.VideoCapture("../../../figure/video.avi") # while True: # start_time = time.time() # ret_val, img = cam.read() # composite = coco_demo.run_on_opencv_image(img) # print("Time: {:.2f} s / img".format(time.time() - start_time)) # cv2.imshow("COCO detections", composite) # if cv2.waitKey(1) == 27: # break # esc to quit # cv2.destroyAllWindows() composite = coco_demo.run_on_opencv_image(image) # cv2.imshow("COCO detections", composite) return composite
def test_net_on_wsi(config_file, ckpt, iteration): #========test on monuseg img_root = '/mnt/DATA_OTHER/moNuSeg/original_testing/tissue_Images/' anno_root = '/mnt/DATA_OTHER/moNuSeg/original_testing/tissue_Images/' # img_root = '/mnt/DATA_OTHER/moNuSeg/original_training/tissue_Images/' # anno_root = '/mnt/DATA_OTHER/moNuSeg/original_training/tissue_Images/' test_data = monuseg(img_root, anno_root) # ###========test on monuseg # img_root = '/mnt/DATA_OTHER/digestPath/Signet_ring_cell_dataset/sig-train-pos/validation/' # test_data = ringcell(img_root, anno_root) _, img_list, _, _ = test_data.walk_root_dir() # config_file = args.config_file # config_file = '/home/ys309/Dropbox/coding/maskrcnn-benchmark/configs/monuseg/rc_retinanet_R-50-FPN_1x.yaml' # update the config options with the config file cfg.merge_from_file(config_file) # manual override some options cfg.merge_from_list(["MODEL.DEVICE", "cuda:0"]) cfg.MODEL.WEIGHT = ckpt # cfg.MODEL.WEIGHT = '/mnt/DATA_OTHER/moNuSeg/results/rc_retinanet_R-50-FPN_1x/model_0020000.pth' histo_demo = HistoDemo( cfg, min_image_size=600, confidence_threshold=0.5, ) # ### confidence_threshold=0.5 # # plot bounding boxes # from maskrcnn_benchmark.data.datasets.evaluation.histo import eval_detection_monuseg # model_size = 600 # overlap = 200 # # l_scores=[] # for i, fimg in enumerate(img_list): # if i<90: # print('%d/%d'%(i+1,len(img_list))) # img = Image.open(fimg) # boxlist=histo_demo.sliding_window_wsi(img, nms_thresh = 0.25) # # l_scores.append(boxlist.get_field('scores')) # boxes=boxlist.bbox.cpu().numpy() # gt = test_data.get_groundtruth(i) # gt_boxes = gt.bbox.cpu().numpy() # result = eval_detection_monuseg([boxlist], [gt], iou_thresh=0.25,) # print (result) # else: # break # # # # R-50-2500 ### confidence_threshold=0.5 # all testing data # plot bounding boxes model_size = 600 overlap = 200 # l_scores=[] predictions = [] gts = [] for i, fimg in enumerate(img_list): if i<90: print('%d/%d'%(i+1,len(img_list))) img = Image.open(fimg) boxlist=histo_demo.sliding_window_wsi(img, nms_thresh = 0.3) predictions.append(boxlist) gt = test_data.get_groundtruth(i) gts.append(gt) else: break output_folder = os.path.join(cfg.OUTPUT_DIR, cfg.OUTPUT_FOLDER) result = eval_detection_monuseg(predictions, gts, iou_thresh=0.3, iteration=iteration, output_folder=output_folder) print (result) del histo_demo return result
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Training") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", type=str, ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--skip-test", dest="skip_test", help="Do not test the final model", action="store_true", ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 args.distributed = num_gpus > 1 if args.distributed: # os.environ['MASTER_ADDR'] = '127.0.0.1' # os.environ['MASTER_PORT'] = '29500' torch.cuda.set_device(args.local_rank) torch.distributed.deprecated.init_process_group(backend="nccl", init_method="env://") cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() output_dir = cfg.OUTPUT_DIR run_name = get_run_name() + '_step' output_dir = get_output_dir(output_dir, args, run_name) if output_dir: mkdir(output_dir) logger = setup_logger("maskrcnn_benchmark", output_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(args) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) logger.info("Loaded configuration file {}".format(args.config_file)) with open(args.config_file, "r") as cf: config_str = "\n" + cf.read() logger.info(config_str) logger.info("Running with config:\n{}".format(cfg)) model = train(cfg, args.local_rank, args.distributed, output_dir) if not args.skip_test: test(cfg, model, args.distributed)
def main(): # Build a parser for arguments parser = argparse.ArgumentParser(description="Action Prediction Training") parser.add_argument( "--config-file", # default="/home/SelfDriving/maskrcnn/maskrcnn-benchmark/configs/e2e_faster_rcnn_R_101_FPN_1x.yaml", default="/home/SelfDriving/maskrcnn/maskrcnn-benchmark/configs/e2e_faster_rcnn_R_50_C4_1x.yaml", metavar="FILE", help="path to maskrcnn_benchmark config file", type=str, ) parser.add_argument( "--skip-test", dest="skip_test", help="Do not test the final model", action="store_true", default=False ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) parser.add_argument( "--MaxIteration", help="the iteration to end training", type=int, default=90000, ) parser.add_argument( "--initLR", help="Initial learning rate", default=0.001 ) parser.add_argument( "--imageroot", type=str, help="Directory to the images", default="/home/SelfDriving/maskrcnn/maskrcnn-benchmark/datasets/bdd100k/images/100k/train" ) parser.add_argument( "--gtroot", type=str, help="Directory to the groundtruth", default="/home/SelfDriving/maskrcnn/maskrcnn-benchmark/datasets/bdd100k/annotations/train_gt_action.json" ) parser.add_argument( "--imWidth", type=int, help="Crop to width", default=1280 ) parser.add_argument( "--imHeight", type=int, help="Crop to height", default=720 ) parser.add_argument( "--batch_size", type=int, help="Batch Size", default=1 ) parser.add_argument( "--experiment", type=str, help="Give this experiment a name", default=str(datetime.datetime.now()) ) args = parser.parse_args() print(args) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() if torch.cuda.is_available(): print("CUDA device is available.") # output directory outdir = cfg.OUTPUT_DIR print("Save path:", outdir) if outdir: mkdir(outdir) # logger = setup_logger("training", outdir) train(cfg, args) # if validate if not args.skip_test: run_test()
import torchvision.models as models from flashtorch.utils import apply_transforms, load_image from flashtorch.saliency import Backprop from flashtorch.activmax import GradientAscent import matplotlib.pyplot as plt import torch from maskrcnn_benchmark.modeling.detector import build_detection_model from maskrcnn_benchmark.config import cfg import os from tqdm import tqdm cfg.merge_from_file('./maskrcnn-benchmark/configs/e2e_faster_rcnn_R_50_FPN_1x.yaml') model=build_detection_model(cfg) # loaded from the checkpoint model model=model.backbone path='/home/wangfa/Workspace/jupiter/maskrcnn-benchmark/datasets/coco/train2014/' for root,dirs,files in os.walk(path): for file in tqdm(files): backprop = Backprop(model) image = load_image(os.path.join(root,file)) input_ = apply_transforms(image) target_class = 0 backprop.visualize(input_,target_class,guided=True) # model = models.vgg16(pretrained=True) # g_ascent= GradientAscent(model.features) # conv5_1=model.features[24] # conv5_1_filters= [45,271,363,409]
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Webcam Demo") parser.add_argument( "--config-file", default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml", # default="../configs/e2e_mask_rcnn_R_50_FPN_1x.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--confidence-threshold", type=float, default=0.7, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=224, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument( "--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show", ) parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=args.show_mask_heatmaps, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, ) # cam = cv2.VideoCapture('/dev/C922') cam = cv2.VideoCapture(0) while True: start_time = time.time() ret_val, img = cam.read() if ret_val: composite = coco_demo.run_on_opencv_image(img) print("Time: {:.2f} s / img".format(time.time() - start_time)) print("FPS: ", 1.0 / (time.time() - start_time)) # FPS = 1 / time to process loop cv2.imshow("COCO detections", composite) else: break k = cv2.waitKey(1) if k == 27 or k == ord('q'): print('Exit with code 0') break # esc to quit cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser() parser.add_argument('config_file') parser.add_argument('ckpt_file') parser.add_argument('image_dir') parser.add_argument('name_file') parser.add_argument('bbox_file') parser.add_argument('output_dir') parser.add_argument('--layer_name', default='fc7') parser.add_argument('--start_id', type=int, default=0) parser.add_argument('--end_id', type=int, default=None) opts = parser.parse_args() bbox_data = json.load(open(opts.bbox_file)) if not os.path.exists(opts.output_dir): os.makedirs(opts.output_dir) ########### build model ############# # update the config options with the config file cfg.merge_from_file(opts.config_file) # manual override some options cfg.merge_from_list(['MODEL.DEVICE', 'cuda:0']) cfg.freeze() device = torch.device(cfg.MODEL.DEVICE) cpu_device = torch.device("cpu") model = build_detection_model(cfg) model.to(device) model.eval() checkpointer = DetectronCheckpointer(cfg, model) _ = checkpointer.load(f=opts.ckpt_file, use_latest=False) transform_fn = build_transform(cfg) ########### extract feature ############# names = np.load(opts.name_file) if opts.end_id is None: opts.end_id = len(names) total_images = opts.end_id - opts.start_id for i, name in enumerate(names): if i < opts.start_id or i >= opts.end_id: continue outname = name.replace('/', '_') outfile = os.path.join(opts.output_dir, '%s.hdf5'%outname) if os.path.exists(outfile): continue img_file = os.path.join(opts.image_dir, name) # apply pre-processing to image original_image = cv2.imread(img_file) height, width = original_image.shape[:-1] image = transform_fn(original_image) nheight, nwidth = image.size(1), image.size(2) # convert to an ImageList, padded so that it is divisible by # cfg.DATALOADER.SIZE_DIVISIBILITY image_list = to_image_list(image, cfg.DATALOADER.SIZE_DIVISIBILITY) image_list = image_list.to(device) # compute predictions: one image one mini-batch with torch.no_grad(): # features: tuples in FPN (batch, dim_ft: 256, h, w) features = model.backbone(image_list.tensors) if name in bbox_data: cpu_boxes = bbox_data[name] boxes = torch.FloatTensor(cpu_boxes).to(device) cand_proposals = BoxList(boxes, (width, height), mode='xyxy') cand_proposals = cand_proposals.resize((nwidth, nheight)) bbox_fts, _, _ = model.roi_heads.extract_features(features, [cand_proposals]) bbox_fts = bbox_fts[opts.layer_name].cpu() # save to file with h5py.File(outfile, 'w') as outf: outf.create_dataset(outname, bbox_fts.size(), dtype='float', compression='gzip') outf[outname][...] = bbox_fts.data.numpy() outf[outname].attrs['image_w'] = width outf[outname].attrs['image_h'] = height outf[outname].attrs['boxes'] = np.array(cpu_boxes).astype(np.int32) if i % 1000 == 0: print('name %s shape %s, processing %d/%d (%.2f%% done)'%(name, bbox_fts.shape, i-opts.start_id, total_images, (i-opts.start_id)*100/total_images))
def main(dir_path=None, config_file=None, model_file=None, save_dir=None): dir_path = norm_path(dir_path) if dir_path else None config_file = norm_path(config_file) if config_file else None model_file = norm_path(model_file) if model_file else None save_dir = norm_path(save_dir, makedirs=True) if save_dir else None save_crop_dir = norm_path(os.path.join(save_dir, 'crop'), makedirs=True) if save_dir else None save_mask_dir = norm_path(os.path.join(save_dir, 'mask'), makedirs=True) if save_dir else None print('paths', save_dir, save_crop_dir, save_mask_dir) # this makes our figures bigger pylab.rcParams['figure.figsize'] = 20, 12 # update the config options with the config file cfg.merge_from_file(config_file) # manual override some options cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) cfg.merge_from_list(["MODEL.WEIGHT", model_file]) kidney_demo = KidneyDemo( cfg, min_image_size=512, confidence_threshold=0.3, ) class_names = ['', 'AKI', 'CKD', 'normal'] acc_meter = Acc_meter() for filePath in image_list(dir_path): print('file_path', filePath) img = cv2.imread(filePath, cv2.IMREAD_COLOR) # resize image for input size of model img, resize_info = resize_aspect_ratio(img, (512, 512)) #find label high score result, crops, masks, labels = kidney_demo.detection(img) top_label = labels[0] #find diagnosis in directory, it will be used as an correct answer path = os.path.split(filePath)[0] diagnosis, accno = path.split('/')[-2:] #convert diagnosis to int(because labels is integer) diag_class_no = class_names.index(diagnosis) # print(diagnosis) # print(top_label) # evaluate(diagnosis,top_label) acc_meter.update(diag_class_no, top_label) # restore size of image to original size result = resize_restore(result, resize_info) # save result image if save_dir: save_file = os.path.join(save_dir, fileName(filePath)) cv2.imwrite(save_file, result) else: imshow(result) # if found object, make corp and mask image if len(labels) > 0: for crop, mask, label in zip(crops, masks, labels): if save_crop_dir: save_file = os.path.join(save_crop_dir, fileName(filePath)) crop = resize_restore(crop, resize_info) cv2.imwrite(save_file, crop) if save_mask_dir: save_file = os.path.join(save_mask_dir, fileName(filePath)) mask = resize_restore(mask, resize_info) cv2.imwrite(save_file, mask) print('acc:{:3.2f}'.format(acc_meter.get_acc())) for class_num, class_acc in acc_meter.get_class_acc(): print('class {} acc {:3.2f}'.format(class_num, class_acc * 100))
def main(): mlperf_log.ROOT_DIR_MASKRCNN = os.path.dirname(os.path.abspath(__file__)) parser = argparse.ArgumentParser(description="PyTorch Object Detection Training") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", type=str, ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 args.distributed = num_gpus > 1 if is_main_process: # Setting logging file parameters for compliance logging os.environ["COMPLIANCE_FILE"] = '/MASKRCNN_complVv0.5.0_' + str(datetime.datetime.now()) mlperf_log.LOG_FILE = os.getenv("COMPLIANCE_FILE") mlperf_log._FILE_HANDLER = logging.FileHandler(mlperf_log.LOG_FILE) mlperf_log._FILE_HANDLER.setLevel(logging.DEBUG) mlperf_log.LOGGER.addHandler(mlperf_log._FILE_HANDLER) if args.distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group( backend="nccl", init_method="env://" ) synchronize() print_mlperf(key=mlperf_log.RUN_START) # setting seeds - needs to be timed, so after RUN_START if is_main_process(): master_seed = random.SystemRandom().randint(0, 2 ** 32 - 1) seed_tensor = torch.tensor(master_seed, dtype=torch.float32, device=torch.device("cuda")) else: seed_tensor = torch.tensor(0, dtype=torch.float32, device=torch.device("cuda")) torch.distributed.broadcast(seed_tensor, 0) master_seed = int(seed_tensor.item()) else: print_mlperf(key=mlperf_log.RUN_START) # random master seed, random.SystemRandom() uses /dev/urandom on Unix master_seed = random.SystemRandom().randint(0, 2 ** 32 - 1) # actually use the random seed args.seed = master_seed # random number generator with seed set to master_seed random_number_generator = random.Random(master_seed) print_mlperf(key=mlperf_log.RUN_SET_RANDOM_SEED, value=master_seed) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() output_dir = cfg.OUTPUT_DIR if output_dir: mkdir(output_dir) logger = setup_logger("maskrcnn_benchmark", output_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(args) # generate worker seeds, one seed for every distributed worker worker_seeds = generate_seeds(random_number_generator, torch.distributed.get_world_size() if torch.distributed.is_initialized() else 1) # todo sharath what if CPU # broadcast seeds from rank=0 to other workers worker_seeds = broadcast_seeds(worker_seeds, device='cuda') # Setting worker seeds logger.info("Worker {}: Setting seed {}".format(args.local_rank, worker_seeds[args.local_rank])) torch.manual_seed(worker_seeds[args.local_rank]) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) logger.info("Loaded configuration file {}".format(args.config_file)) with open(args.config_file, "r") as cf: config_str = "\n" + cf.read() logger.info(config_str) logger.info("Running with config:\n{}".format(cfg)) model = train(cfg, args.local_rank, args.distributed) print_mlperf(key=mlperf_log.RUN_FINAL)
masks = prediction.get_field('mask').squeeze(1).numpy() if show: res = draw_masks_maskrcnn(image, boxes, scores, labels, masks, human_label_list=CATEGORIES) # print('fps: {}'.format(1/(time.time() - tic))) return boxes, scores, labels, masks, res else: return boxes, scores, labels, masks, _ if __name__ == "__main__": cfg.merge_from_file( '../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml') cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) cfg.freeze() mask_rcnn_demo = MaskRCNNDemo(cfg) v_f = sys.argv[1] cap = cv2.VideoCapture(v_f) fps = cap.get(cv2.CAP_PROP_FPS) size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))) video_writer = cv2.VideoWriter('result.mp4', cv2.VideoWriter_fourcc(*'DIVX'), fps, size) while True: start_time = time.time()
def main(): parser = argparse.ArgumentParser(description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default="configs/visual_genome_vqa/e2e_faster_rcnn_X-101-64x4d-FPN_1x_MLP_2048_FPN_512_vqa_single.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--save_path", default="/checkpoint/meetshah/features/vqa/pytorch/resnext101_64x4d_mlp_2048_fpn_512/", ) parser.add_argument( "--feat_name", default="fc6", ) parser.add_argument( "--n_groups", default=0, ) parser.add_argument( "--group_id", default=1, ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() if not os.path.exists(args.save_path): os.makedirs(args.save_path) num_gpus = int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = "" logger = setup_logger("maskrcnn_benchmark", save_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) model.to(cfg.MODEL.DEVICE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) _ = checkpointer.load(cfg.MODEL.WEIGHT) iou_types = ("bbox",) if cfg.MODEL.MASK_ON: iou_types = iou_types + ("segm",) output_folders = [None] * len(cfg.DATASETS.TEST) dataset_names = cfg.DATASETS.TEST if cfg.OUTPUT_DIR: for idx, dataset_name in enumerate(dataset_names): output_folder = os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=distributed) for output_folder, dataset_name, data_loader_val in zip( output_folders, dataset_names, data_loaders_val ): inference( model, data_loader_val, dataset_name=dataset_name, iou_types=iou_types, box_only=cfg.MODEL.RPN_ONLY, device=cfg.MODEL.DEVICE, expected_results=cfg.TEST.EXPECTED_RESULTS, expected_results_sigma_tol=cfg.TEST.EXPECTED_RESULTS_SIGMA_TOL, output_folder=output_folder, save_path=args.save_path, feat_name=args.feat_name, group_id=args.group_id, n_groups=args.n_groups, ) synchronize()
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default= "/private/home/fmassa/github/detectron.pytorch_v2/configs/e2e_faster_rcnn_R_50_C4_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--ckpt", help= "The path to the checkpoint for test, default is the latest checkpoint.", default=None, ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = "" logger = setup_logger("maskrcnn_benchmark", save_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) model.to(cfg.MODEL.DEVICE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) ckpt = cfg.MODEL.WEIGHT if args.ckpt is None else args.ckpt _ = checkpointer.load(ckpt, use_latest=args.ckpt is None) """ # Initialize mixed-precision if necessary use_mixed_precision = cfg.DTYPE == 'float16' amp_handle = amp.init(enabled=use_mixed_precision, verbose=cfg.AMP_VERBOSE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) ckpt = cfg.MODEL.WEIGHT if args.ckpt is None else args.ckpt _ = checkpointer.load(ckpt, use_latest=args.ckpt is None) iou_types = ("bbox",) if cfg.MODEL.MASK_ON: iou_types = iou_types + ("segm",) if cfg.MODEL.KEYPOINT_ON: iou_types = iou_types + ("keypoints",) output_folders = [None] * len(cfg.DATASETS.TEST) dataset_names = cfg.DATASETS.TEST if cfg.OUTPUT_DIR: for idx, dataset_name in enumerate(dataset_names): output_folder = os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=distributed) for output_folder, dataset_name, data_loader_val in zip(output_folders, dataset_names, data_loaders_val): inference( model, data_loader_val, dataset_name=dataset_name, iou_types=iou_types, box_only=False if cfg.MODEL.RETINANET_ON else cfg.MODEL.RPN_ONLY, device=cfg.MODEL.DEVICE, expected_results=cfg.TEST.EXPECTED_RESULTS, expected_results_sigma_tol=cfg.TEST.EXPECTED_RESULTS_SIGMA_TOL, output_folder=output_folder, ) synchronize() """ from maskrcnn_benchmark.data.transforms.build import build_transforms from PIL import Image import torchvision.transforms.functional as F transform = build_transforms(cfg, is_train=False) img_dir = "demo_imgs" res_dir = "demo_res" model.eval() imgs = os.listdir(img_dir) for img in imgs: img_path = os.path.join(img_dir, img) img_pil = Image.open(img_path) # for i in range( 360 ): original_img = img_pil # original_img = F.rotate( img_pil, 45, expand=True ) origin_w, origin_h = original_img.size img, target = transform(original_img, None) print(img.shape) img = img.view((1, img.shape[0], img.shape[1], img.shape[2])) h, w = img.shape[2:] if h % 32 != 0: new_h = (h // 32 + 1) * 32 else: new_h = h if w % 32 != 0: new_w = (w // 32 + 1) * 32 else: new_w = w ratio_w = 1. * new_w / w ratio_h = 1. * new_h / h padded_img = torch.zeros((1, 3, new_h, new_w)).float() padded_img[:, :, :h, :w] = img prediction = model(padded_img.cuda())[0] prediction = prediction.resize( (origin_w * ratio_w, origin_h * ratio_h)) hboxes = prediction.bbox.cpu() rboxes = prediction.get_field("rboxes").cpu() ratios = prediction.get_field("ratios").cpu() scores = prediction.get_field("scores").cpu() # labels = prediction.get_field( "labels" ).cpu() for rbox, ratio, score in zip(rboxes, ratios, scores): print(rbox) print(ratio, score) h_idx = ratios > 0.8 # print(hboxes) h = hboxes[h_idx] hboxes_vtx = torch.stack([ h[:, 0], h[:, 1], h[:, 2], h[:, 1], h[:, 2], h[:, 3], h[:, 0], h[:, 3] ]).permute(1, 0) rboxes[h_idx] = hboxes_vtx # rboxes = rboxes.data.numpy().astype( np.int32 ) rboxes = rboxes.data.numpy() keep = poly_nms( np.hstack([rboxes, scores.cpu().data.numpy()[:, np.newaxis] ]).astype(np.double), 0.1) rboxes = rboxes[keep].astype(np.int32) scores = scores[keep] hboxes = hboxes[keep] keep = np.where(scores > 0.6) rboxes = rboxes[keep] scores = scores[keep].tolist() hboxes = hboxes[keep] # rboxes = list( map( minAreaRect, rboxes ) ) if len(rboxes) > 0: rboxes = np.vstack(rboxes) else: rboxes = np.array(rboxes) # vis( img_info["file_name"], rboxes ) # img = cv2.imread( original_img ) img = np.array(original_img.convert("RGB"))[:, :, ::-1].copy() cv2.polylines(img, rboxes.reshape(-1, 4, 2).astype(np.int32), True, (0, 255, 255), thickness=2, lineType=cv2.LINE_AA) filename = img_path.split("/")[-1] cv2.imwrite("{}/{}".format(res_dir, filename), img)
def main(): parser = argparse.ArgumentParser( description="PyTorch Relation Detection Training") parser.add_argument( "--config-file", default="", metavar="FILE", help="path to config file", type=str, ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "--skip-test", dest="skip_test", help="Do not test the final model", action="store_true", ) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 cfg.NUM_GPUS = num_gpus args.distributed = num_gpus > 1 if args.distributed: torch.cuda.set_device(args.local_rank) torch.distributed.init_process_group(backend="nccl", init_method="env://") synchronize() cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() output_dir = cfg.OUTPUT_DIR if output_dir: mkdir(output_dir) listener_dir = cfg.LISTENER_DIR if listener_dir: mkdir(listener_dir) logger = setup_logger("maskrcnn_benchmark", output_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(args) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) logger.info("Loaded configuration file {}".format(args.config_file)) with open(args.config_file, "r") as cf: config_str = "\n" + cf.read() logger.info(config_str) logger.info("Running with config:\n{}".format(cfg)) """ output_config_path = os.path.join(cfg.OUTPUT_DIR, 'config.yml') logger.info("Saving config into: {}".format(output_config_path)) # save overloaded model config in the output directory save_config(cfg, output_config_path) """ listener_config_path = os.path.join(cfg.LISTENER_DIR, 'config.yml') logger.info("Saving config into: {}".format(listener_config_path)) # save overloaded model config in the output directory save_config(cfg, listener_config_path) listener = train(cfg, args.local_rank, args.distributed, logger) if not args.skip_test: run_test(cfg, model, listener, args.distributed, logger)
def main(): parser = argparse.ArgumentParser(description="DPNet Demo") parser.add_argument( "--config-file", default= "configs/e2e_faster_rcnn_R_101_FPN_1x_rpc_syn_render_density_map.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--images_dir", required=True, type=str, help="path to images file", ) parser.add_argument( "--save_dir", default='rpc_results', type=str, help="path to images file", ) parser.add_argument( "--confidence-threshold", type=float, default=0.7, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=800, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument( "--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show", ) parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=args.show_mask_heatmaps, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, ) if os.path.exists(args.save_dir): shutil.rmtree(args.save_dir) os.mkdir(args.save_dir) with open('/data7/lufficc/rpc/instances_test2019.json') as fid: data = json.load(fid) images = {} for x in data['images']: images[x['id']] = x annotations = defaultdict(list) for x in data['annotations']: annotations[images[x['image_id']]['file_name']].append(x) annotations = dict(annotations) counter = { 'easy': 0, 'medium': 0, 'hard': 0, } data_images = data['images'].copy() random.shuffle(data_images) FONT = ImageFont.truetype('/data7/lufficc/projects/DPNet/demo/arial.ttf', 8) for image_ann in data_images: if counter[image_ann['level']] >= 20: continue image_path = os.path.join(args.images_dir, image_ann['file_name']) img = cv2.imread(image_path) annotation = annotations[image_ann['file_name']] prediction = coco_demo.run_on_opencv_image(img) new_size = (400, 400) img = cv2.resize(img, new_size) prediction = prediction.resize(new_size) boxes = prediction.bbox.numpy() labels = prediction.get_field('labels').numpy() scores = prediction.get_field('scores').numpy() img = draw_boxes(img, boxes, labels, scores, COCODemo.CATEGORIES, width=2, font=FONT, alpha=0.4) gt_labels = sorted([ann['category_id'] for ann in annotation]) if gt_labels == sorted(labels.tolist()): print('Get {}.'.format(image_ann['level'])) cv2.imwrite( os.path.join( args.save_dir, image_ann['level'] + '_' + os.path.basename(image_path)), img) counter[image_ann['level']] += 1
def main(): parser = argparse.ArgumentParser( description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default= "/private/home/fmassa/github/detectron.pytorch_v2/configs/e2e_faster_rcnn_R_50_C4_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int( os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.deprecated.init_process_group(backend="nccl", init_method="env://") cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = "" logger = setup_logger("maskrcnn_benchmark", save_dir, args.local_rank) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) model.to(cfg.MODEL.DEVICE) checkpointer = DetectronCheckpointer(cfg, model) _ = checkpointer.load(cfg.MODEL.WEIGHT) iou_types = ("bbox", ) if cfg.MODEL.MASK_ON: iou_types = iou_types + ("segm", ) output_folders = [None] * len(cfg.DATASETS.TEST) if cfg.OUTPUT_DIR: dataset_names = cfg.DATASETS.TEST for idx, dataset_name in enumerate(dataset_names): output_folder = os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=distributed) for output_folder, data_loader_val in zip(output_folders, data_loaders_val): inference( model, data_loader_val, iou_types=iou_types, box_only=cfg.MODEL.RPN_ONLY, device=cfg.MODEL.DEVICE, expected_results=cfg.TEST.EXPECTED_RESULTS, expected_results_sigma_tol=cfg.TEST.EXPECTED_RESULTS_SIGMA_TOL, output_folder=output_folder, ) synchronize()
def main(): parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo") parser.add_argument( "--config-file", default="../configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument( "--confidence-threshold", type=float, default=0.7, help="Minimum score for the prediction to be shown", ) parser.add_argument( "--min-image-size", type=int, default=224, help="Smallest size of the image to feed to the model. " "Model was trained with 800, which gives best results", ) parser.add_argument( "--show-mask-heatmaps", dest="show_mask_heatmaps", help="Show a heatmap probability for the top masks-per-dim masks", action="store_true", ) parser.add_argument( "--masks-per-dim", type=int, default=2, help="Number of heatmaps per dimension to show", ) parser.add_argument( "opts", help="Modify model config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() # load config from file and command-line arguments cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() # prepare object that handles inference plus adds predictions on top of image coco_demo = COCODemo( cfg, confidence_threshold=args.confidence_threshold, show_mask_heatmaps=args.show_mask_heatmaps, masks_per_dim=args.masks_per_dim, min_image_size=args.min_image_size, ) cam = cv2.VideoCapture(0) while True: start_time = time.time() ret_val, img = cam.read() composite = coco_demo.run_on_opencv_image(img) print("Time: {:.2f} s / img".format(time.time() - start_time)) cv2.imshow("COCO detections", composite) if cv2.waitKey(1) == 27: break # esc to quit cv2.destroyAllWindows()
def main(): parser = argparse.ArgumentParser(description="PyTorch Object Detection Inference") parser.add_argument( "--config-file", default="/private/home/fmassa/github/detectron.pytorch_v2/configs/e2e_faster_rcnn_R_50_C4_1x_caffe2.yaml", metavar="FILE", help="path to config file", ) parser.add_argument("--local_rank", type=int, default=0) parser.add_argument( "opts", help="Modify config options using the command-line", default=None, nargs=argparse.REMAINDER, ) args = parser.parse_args() num_gpus = int(os.environ["WORLD_SIZE"]) if "WORLD_SIZE" in os.environ else 1 distributed = num_gpus > 1 if distributed: torch.cuda.set_device(args.local_rank) torch.distributed.deprecated.init_process_group( backend="nccl", init_method="env://" ) cfg.merge_from_file(args.config_file) cfg.merge_from_list(args.opts) cfg.freeze() save_dir = "" logger = setup_logger("maskrcnn_benchmark", save_dir, get_rank()) logger.info("Using {} GPUs".format(num_gpus)) logger.info(cfg) logger.info("Collecting env info (might take some time)") logger.info("\n" + collect_env_info()) model = build_detection_model(cfg) model.to(cfg.MODEL.DEVICE) output_dir = cfg.OUTPUT_DIR checkpointer = DetectronCheckpointer(cfg, model, save_dir=output_dir) _ = checkpointer.load(cfg.MODEL.WEIGHT) iou_types = ("bbox",) if cfg.MODEL.MASK_ON: iou_types = iou_types + ("segm",) output_folders = [None] * len(cfg.DATASETS.TEST) dataset_names = cfg.DATASETS.TEST if cfg.OUTPUT_DIR: for idx, dataset_name in enumerate(dataset_names): output_folder = os.path.join(cfg.OUTPUT_DIR, "inference", dataset_name) mkdir(output_folder) output_folders[idx] = output_folder data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=distributed) for output_folder, dataset_name, data_loader_val in zip(output_folders, dataset_names, data_loaders_val): inference( model, data_loader_val, dataset_name=dataset_name, iou_types=iou_types, box_only=cfg.MODEL.RPN_ONLY, device=cfg.MODEL.DEVICE, expected_results=cfg.TEST.EXPECTED_RESULTS, expected_results_sigma_tol=cfg.TEST.EXPECTED_RESULTS_SIGMA_TOL, output_folder=output_folder, ) synchronize()
from maskrcnn_benchmark.data.datasets.evaluation import evaluate from maskrcnn_benchmark.data.datasets.cityscapes import CityscapesDataset import torch from maskrcnn_benchmark.config import cfg from maskrcnn_benchmark.data import make_data_loader output_folder = '/home/brianyao/Documents/maskrcnn-benchmark/inference/cityscapes_fine_instanceonly_seg_val_cocostyle' anno_file = '/media/DATA/Cityscapes/annotations/instancesonly_filtered_gtFine_val.json' root = '/media/DATA/Cityscapes/leftImg8bit/val' dataset = CityscapesDataset(anno_file, root, True) cfg.merge_from_file('../configs/cityscapes/mask_rcnn_coco_eval.yaml') cfg.merge_from_list([]) cfg.freeze() data_loaders_val = make_data_loader(cfg, is_train=False, is_distributed=False) data_loader = data_loaders_val[0] extra_args = dict( box_only=False, iou_types=("bbox", "segm"), expected_results=[], expected_results_sigma_tol=4, ) predictions = torch.load( '../inference/cityscapes_fine_instanceonly_seg_val_cocostyle/predictions.pth' ) evaluate(data_loader.dataset, predictions, output_folder, **extra_args)