def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) dataset = WAD_CVPR2018(args.dataset_dir) cfg.MODEL.NUM_CLASSES = len( dataset.eval_class) + 1 # with a background class print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) cfg.RESNETS.IMAGENET_PRETRAINED = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() imglist = misc_utils.get_imagelist_from_dir(dataset.test_image_dir) num_images = len(imglist) output_dir = os.path.join(('/').join(args.load_ckpt.split('/')[:-2]), 'Images') if not os.path.exists(output_dir): os.makedirs(output_dir) for i in tqdm(xrange(num_images)): im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) args.current_im_name = im_name cls_boxes, cls_segms, prediction_row = im_detect_all(args, maskRCNN, im, dataset, timers=timers) thefile = open(os.path.join(output_dir, im_name + '.txt'), 'w') for item in prediction_row: thefile.write("%s\n" % item)
def main(): args = parse_args() print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.nms_soft: cfg.TEST.SOFT_NMS.ENABLED = True else: cfg.TEST.NMS = args.nms if args.nms_soft: output_dir = os.path.join( ('/').join(args.load_ckpt.split('/')[:-2]), 'Images_' + str(cfg.TEST.SCALE) + '_SOFT_NMS') elif args.nms: output_dir = os.path.join( ('/').join(args.load_ckpt.split('/')[:-2]), 'Images_' + str(cfg.TEST.SCALE) + '_NMS_%.2f' % args.nms) else: output_dir = os.path.join(('/').join(args.load_ckpt.split('/')[:-2]), 'Images_' + str(cfg.TEST.SCALE)) if cfg.TEST.BBOX_AUG.ENABLED: output_dir += '_TEST_AUG' # if args.cls_boxes_confident_threshold < 0.5: output_dir += '_cls_boxes_confident_threshold_%.1f' % args.cls_boxes_confident_threshold if not os.path.exists(output_dir): os.makedirs(output_dir) args.output_vis_dir = os.path.join(output_dir, 'Image_Vis') if not os.path.exists(args.output_vis_dir): os.makedirs(args.output_vis_dir) args.output_img_dir = os.path.join(output_dir, 'Image_Masks') if not os.path.exists(args.output_img_dir): os.makedirs(args.output_img_dir) args.output_list_dir = os.path.join(output_dir, 'List_Masks') if not os.path.exists(args.output_list_dir): os.makedirs(args.output_list_dir) print('Called with args:') print(args) if args.range is None: args.test_net_file, _ = os.path.splitext(__file__) dataset = WAD_CVPR2018(args.dataset_dir) img_produced = os.listdir(args.output_list_dir) imglist_all = misc_utils.get_imagelist_from_dir(dataset.test_image_dir) imglist = [ x for x in imglist_all if x.split('/')[-1][:-4] + '.txt' not in img_produced ] num_images = len(imglist) multi_gpu_test_net_on_dataset(args, num_images) else: test_net_on_dataset_multigpu(args)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) dataset = datasets.get_hospital_dataset() cfg.MODEL.NUM_CLASSES = 20 # with bg num_class = cfg.MODEL.NUM_CLASSES sents = dataset.sents th_cls = dataset.th_cls cls2eng = dataset.cls2eng eng2type = dataset.eng2type print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): # for each image print('img', i) im = cv2.imread(imglist[i]) assert im is not None # segmentation # d = segment(im) # pdb.set_trace() timers = defaultdict(Timer) # detection cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) # first we collect boxes from all classes dets_total = np.empty([0, 6], dtype=np.float32) for cls in range(1, num_class): # for each cls dets = cls_boxes[cls] if dets.shape[0] == 0: continue dets_extend = np.pad( dets, ((0, 0), (0, 1)), # add 0 rows above, below and left, but 1 row right mode='constant', constant_values=cls) # append cls to dets dets_total = np.vstack((dets_total, dets_extend)) # then use a loose NMS to make each region has only one symptom keep = box_utils.nms(dets_total, 0.7) nms_dets = dets_total[keep, :] # iterate through remained boxes report, healthy = '', True have_sym_of_cls = [False for _ in range(num_class)] n = nms_dets.shape[0] final_results = [] # return to the web for idx in range(n): # for each region th, cls = nms_dets[idx, -2], int(nms_dets[idx, -1]) if th > th_cls[cls]: # diagnosed to have the sym report += sents[cls][1] have_sym_of_cls[cls] = True healthy = False ename = cls2eng[int(cls)] _type = eng2type[ename] final_results.append({ 'name': ename, 'type': _type, 'box': list(nms_dets[idx, 0:4]) }) for cls in range(1, num_class): # for each cls if not have_sym_of_cls[cls]: # if have no sym of this cls report += sents[cls][0] if healthy: report = sents[0][0] print(report) pdb.set_trace() # healthy = True # flag indicating healthy or not # for cls in range(1, num_class): # for each cls # dets = cls_boxes[cls] # if dets.shape[0] == 0: # report += sents[cls][0] # continue # n = dets.shape[0] # flag = False # indicates if the sym exists # for k in range(n): # for each region # if dets[k, -1] > th_cls[cls]: # above threshold for this cls, means have this cls of symptom # report += sents[cls][1] # flag = True # healthy = False # if not flag: # don't have this symptom # report += sents[cls][0] # # if healthy: # use the report for healthy people # report = sents[0][0] im_name, _ = os.path.splitext(os.path.basename(imglist[i])) # vis_utils.vis_one_image( # im[:, :, ::-1], # BGR -> RGB for visualization # im_name, # args.output_dir, # cls_boxes, # cls_segms, # cls_keyps, # dataset=dataset, # box_alpha=0.3, # show_class=True, # thresh=0.05, # kp_thresh=2 # ) if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def main(): """main function""" os.environ['CUDA_VISIBLE_DEVICES'] = '1' if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() params = list(maskRCNN.parameters()) k = 0 for i in params: l = 1 for j in i.size(): l *= j k = k + l print('zonghe:' + str(k)) if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) start = time.time() cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) class_result_boxes = [] for index, class_boxes in enumerate(cls_boxes): if len(class_boxes) != 0: class_boxes = class_boxes.tolist() results_oneclass = threeD_detect(imglist[i], class_boxes, index) class_result_boxes.append(results_oneclass) save_image = im color_class = { 'Car': [0, 255, 255], 'Cyclist': [255, 0, 0], 'Pedestrian': [0, 0, 255] } for result_boxes in class_result_boxes: for box in result_boxes: cv2.rectangle(save_image, (box[0], box[1]), (box[2], box[3]), color_class[box[-1]], 2) height = round(box[-2][0], 2) width = round(box[-2][1], 2) length = round(box[-2][2], 2) threeD_info = str(height) + ' ' + str(width) + ' ' + str( length) cv2.putText(save_image, threeD_info, (box[0], box[1] - 20), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2) _, imagename = os.path.split(imglist[i]) imagename2 = imagename.split('.')[0] cv2.imwrite('../output1/%s.png' % imagename2, save_image) end = time.time() print(end - start) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) prefix_path = args.output_dir + '_results' if os.path.exists(prefix_path): shutil.rmtree(prefix_path) os.mkdir(prefix_path) else: os.mkdir(prefix_path) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) for i in tqdm(range(num_images)): im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) boxes, _, _, classes = convert_from_cls_format(cls_boxes, cls_segms, cls_keyps) if classes == []: continue voc_boxes = np.zeros_like(boxes) voc_boxes[:, 0:1] = boxes[:, 4:5] voc_boxes[:, 1:3] = boxes[:, 0:2] + 1 voc_boxes[:, 3:5] = boxes[:, 2:4] + 1 for instance_idx, cls_idx in enumerate(classes): cls_name = dataset.classes[cls_idx] if cls_name == 'motorcycle': cls_name = 'motorbike' f = open(os.path.join(prefix_path, cls_name + ".txt"), "a+") f.write("%s " % im_name) for item in voc_boxes[instance_idx]: f.write("%f " % item) f.write("\n") f.close()
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i, im_name in enumerate(imglist): out_name = os.path.join( args.output_dir, '{}'.format(os.path.basename(im_name) + '.png')) print('Processing {} -> {}'.format(im_name, out_name)) im = cv2.imread(im_name) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im = vis_utils.vis_one_image_opencv(im, cls_boxes, cls_segms, cls_keyps, thresh=0.5, show_box=True, show_class=True) cv2.imwrite(out_name, im) if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def test_net_on_dataset_multigpu(args): dataset = WAD_CVPR2018(args.dataset_dir) cfg.MODEL.NUM_CLASSES = len( dataset.eval_class) + 1 # with a background class print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) cfg.RESNETS.IMAGENET_PRETRAINED = False # Don't need to load imagenet pretrained weights if args.nms_soft: cfg.TEST.SOFT_NMS.ENABLED = True else: cfg.TEST.NMS = args.nms assert_and_infer_cfg() maskRCNN = Generalized_RCNN() maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() #imglist = misc_utils.get_imagelist_from_dir(dataset.test_image_dir) img_produced = os.listdir(args.output_list_dir) imglist_all = misc_utils.get_imagelist_from_dir(dataset.test_image_dir) imglist = [ x for x in imglist_all if x.split('/')[-1][:-4] + '.txt' not in img_produced ] #for i in tqdm(xrange(args.range[0], args.range[1])): start_idx = len(imglist_all) - args.range[0] end_idx = len(imglist_all) - args.range[0] + args.range[1] for i in tqdm(xrange(start_idx, end_idx)): im = cv2.imread(imglist_all[i]) assert im is not None timers = defaultdict(Timer) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) args.current_im_name = im_name cls_boxes, cls_segms, prediction_row = im_detect_all(args, maskRCNN, im, dataset, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) print(im_name) if args.vis: vis_utils.vis_one_image_cvpr2018_wad( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_vis_dir, cls_boxes, cls_segms, None, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.5, kp_thresh=2) thefile = open(os.path.join(args.output_list_dir, im_name + '.txt'), 'w') for item in prediction_row: thefile.write("%s\n" % item)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2 ) if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset == "miotcd": dataset = datasets.get_miotcd_dataset() cfg.MODEL.NUM_CLASSES = 12 elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset.startswith("bogota"): dataset = datasets.get_bogota_dataset() cfg.MODEL.NUM_CLASSES = 12 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) dataset_result = {} for i in xrange(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None try: timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) boxes_, segme_ , keyps_ ,clasies= convert_from_cls_format(cls_boxes,cls_segms,cls_keyps) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) dataset_result[im_name] = localize_obj_in_image(im_name,boxes_,clasies) except(e): import pdb pdb.set_trace() np.save('dictionary_answer.npy', dataset_result) tmp = args.image_dir.split('/') txt = str(tmp[-2:])+'_'+str(tmp[-1:])+'.csv' save_localization_result(dataset_result,txt)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset.startswith("keypoints_carfusion"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) assert bool(args.load_ckpt_car) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' print('load cfg from file: {}'.format(args.cfg_file_person)) cfg_from_file(args.cfg_file_person) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) cfg.RESNETS.IMAGENET_PRETRAINED = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN_person = Generalized_RCNN() #print('load cfg from file: {}'.format(args.cfg_file_person)) #cfg_from_file(args.cfg_file_person) #assert_and_infer_cfg() #maskRCNN_person = Generalized_RCNN() if args.visualize: save_image = True if args.cuda: maskRCNN_person.cuda() if args.load_ckpt_person: load_name = args.load_ckpt_person print("loading checkpoint for person %s" % (load_name)) checkpoint_person = torch.load( load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN_person, checkpoint_person['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN_car, args.load_detectron) maskRCNN_person = mynn.DataParallel( maskRCNN_person, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN_person.eval() print('load cfg from file: {}'.format(args.cfg_file_car)) cfg_from_file(args.cfg_file_car) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) #cfg.RESNETS.IMAGENET_PRETRAINED = False # Don't need to load imagenet pretrained weights #assert_and_infer_cfg() maskRCNN_car = Generalized_RCNN() if args.cuda: maskRCNN_car.cuda() if args.load_ckpt_car: load_name = args.load_ckpt_car print("loading checkpoint for car %s" % (load_name)) checkpoint_car = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN_car, checkpoint_car['model']) maskRCNN_car = mynn.DataParallel(maskRCNN_car, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN_car.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) #imglist.sort(key=lambda f: int(filter(str.isdigit, f))) l = imglist try: imglist = sorted( l, key=lambda x: int(os.path.splitext(x)[0].split('/')[-1])) except: print('images couldnot be sorted') for i in xrange(num_images): print('img', i, ' out of ', num_images, ' filename: ', imglist[i].split('/')[-1], ' in camera', imglist[i].split('/')[-2]) im = cv2.imread(imglist[i]) try: assert im is not None except: continue timers = defaultdict(Timer) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) #print(im_name) output_name = os.path.basename(im_name) + '.txt' output_file = os.path.join(args.output_dir, '{}'.format(output_name)) text_file = open(output_file, "w") cfg_from_file(args.cfg_file_car) cls_boxes_car, cls_segms_car, cls_keyps_car, features_car = im_detect_all( maskRCNN_car, im, timers=timers) if len(cls_boxes_car[1]) > 0: features_car = features_car.data.cpu().numpy() #print(loop,loop2) #print(distance_matrix) #plt.figure() #plot_confusion_matrix(distance_matrix, classes=[0,1,2,3,4,5,6], #title='Confusion matrix, without normalization') #fig = plt.figure() #ax = fig.add_subplot(1,1,1) #ax.set_aspect('equal') #plt.imshow(distance_matrix, interpolation='nearest', cmap=plt.cm.ocean) #plt.colorbar() #plt.show() #fig.savefig('1.png') count = 0 filename = 'finalized_model.txt' loaded_model = pickle.load(open(filename, 'rb')) for ind, bb in enumerate(cls_boxes_car[1]): string = str(count) keyps = [k for klist in cls_keyps_car for k in klist] bb_new = [bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1]] features = features_car[ind, :, :, :].flatten() pca_feature = [] pca_feature.append(np.transpose(features.astype(np.float))) #print(pca_feature) features = loaded_model.transform(pca_feature) features = features[0] #loaded_model.transform(pca_feature) if bb[4] < 0.5: continue #for bb_ind,val in enumerate(bb_new): # string = string + ',' + str(val) for kp_ind, kp in enumerate(keyps[ind][0]): string = string + ',' + str(kp) + ',' + str( keyps[ind][1][kp_ind]) + ',' + str( int(keyps[ind][2][kp_ind])) for feature_ind, feature in enumerate(features): string = string + ',' + str(feature) string = string + ',car' text_file.write(string) text_file.write('\n') #print(string) count = count + 1 cfg_from_file(args.cfg_file_person) cls_boxes_person, cls_segms_person, cls_keyps_person, features_person = im_detect_all( maskRCNN_person, im, timers=timers) if len(cls_boxes_person[1]) > 0: features_person = features_person.data.cpu().numpy() for ind, bb in enumerate(cls_boxes_person[1]): string = str(count) keyps = [k for klist in cls_keyps_person for k in klist] bb_new = [bb[0], bb[1], bb[2] - bb[0], bb[3] - bb[1]] features = features_person[ind, :, :, :].flatten() pca_feature = [] pca_feature.append(np.transpose(features.astype(np.float))) #print(pca_feature) features = loaded_model.transform(pca_feature) features = features[0] #loaded_model.transform(pca_feature) #features = loaded_model.transform(np.transpose(features.astype(np.float))) # print(features) if bb[4] < 0.5: continue for bb_ind, val in enumerate(bb_new): string = string + ',' + str(val) for kp_ind, kp in enumerate(keyps[ind][0]): string = string + ',' + str(kp) + ',' + str( keyps[ind][1][kp_ind]) + ',' + str( int(keyps[ind][2][kp_ind])) for feature_ind, feature in enumerate(features): string = string + ',' + str(feature) string = string + ',person' text_file.write(string) text_file.write('\n') #print(string) count = count + 1 if save_image == True: im_name, _ = os.path.splitext(os.path.basename(imglist[i])) image_car = vis_utils.vis_one_image_car( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes_car, cls_segms_car, cls_keyps_car, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.5, kp_thresh=0.1) output_name = os.path.basename(im_name) + '.png' im = cv2.imread( os.path.join(args.output_dir, '{}'.format(output_name))) if im is None: continue continue vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes_person, cls_segms_person, cls_keyps_person, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.5, kp_thresh=10) if args.merge_pdfs and num_images > 1 and save_image == True: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def test_net_on_dataset(args): dataset = WAD_CVPR2018(args.dataset_dir) cfg.MODEL.NUM_CLASSES = len( dataset.eval_class) + 1 # with a background class print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.nms_soft: cfg.TEST.SOFT_NMS.ENABLED = True else: cfg.TEST.NMS = args.nms cfg.RESNETS.IMAGENET_PRETRAINED = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() imglist_all = misc_utils.get_imagelist_from_dir(dataset.test_image_dir) if args.nms_soft: output_dir = os.path.join( ('/').join(args.load_ckpt.split('/')[:-2]), 'Images_' + str(cfg.TEST.SCALE) + '_SOFT_NMS') elif args.nms: output_dir = os.path.join( ('/').join(args.load_ckpt.split('/')[:-2]), 'Images_' + str(cfg.TEST.SCALE) + '_NMS_%.2f' % args.nms) else: output_dir = os.path.join(('/').join(args.load_ckpt.split('/')[:-2]), 'Images_' + str(cfg.TEST.SCALE)) if cfg.TEST.BBOX_AUG.ENABLED: output_dir += '_TEST_AUG' #if args.cls_boxes_confident_threshold < 0.5: output_dir += '_cls_boxes_confident_threshold_%.1f' % args.cls_boxes_confident_threshold if not os.path.exists(output_dir): os.makedirs(output_dir) output_vis_dir = os.path.join(output_dir, 'Image_Vis') if not os.path.exists(output_vis_dir): os.makedirs(output_vis_dir) args.output_img_dir = os.path.join(output_dir, 'Image_Masks') if not os.path.exists(args.output_img_dir): os.makedirs(args.output_img_dir) output_list_dir = os.path.join(output_dir, 'List_Masks') if not os.path.exists(output_list_dir): os.makedirs(output_list_dir) # A break point # img_produced = os.listdir(output_list_dir) # imglist = [x for x in imglist_all if x.split('/')[-1][:-4]+'.txt' not in img_produced] imglist = imglist_all if False: args.vis = True imglist = [ os.path.join(args.dataset_dir, 'test', '4f38c1d630209cfb777a3dcbb613ba56.jpg'), os.path.join(args.dataset_dir, 'test', '3b08ff3b969200982d0283aedbcaae20.jpg') ] if not args.range: start = 0 end = len(imglist) else: start = args.range[0] end = args.range[1] for i in tqdm(xrange(start, end)): im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) args.current_im_name = im_name cls_boxes, cls_segms, prediction_row = im_detect_all(args, maskRCNN, im, dataset, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) print(im_name) if args.vis: vis_utils.vis_one_image_cvpr2018_wad( im[:, :, ::-1], # BGR -> RGB for visualization im_name, output_vis_dir, cls_boxes, cls_segms, None, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.5, kp_thresh=2) thefile = open(os.path.join(output_list_dir, im_name + '.txt'), 'w') for item in prediction_row: thefile.write("%s\n" % item)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset == "rsna2018": dataset = datasets.get_rsna_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) submission_dict = {} vis_dir = os.path.join(args.output_dir, 'vis') for i in xrange(num_images): print('img', i) predictionstring = '' im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) boxes = [] # vis_utils.vis_one_image( # im[:, :, ::-1], # BGR -> RGB for visualization # im_name, # vis_dir, # cls_boxes, # cls_segms, # cls_keyps, # dataset=dataset, # box_alpha=0.3, # show_class=True, # thresh=args.submit_threshold, # kp_thresh=2, # ext='png' # ) box_list = [b for b in cls_boxes if len(b) > 0] if len(box_list) > 0: boxes = np.concatenate(box_list) for box in boxes: bbox = box[:4] score = box[-1] if score < args.submit_threshold: continue x = bbox[0] y = bbox[1] height = bbox[3] - bbox[1] width = bbox[2] - bbox[0] predictionstring += str(score) + ' ' + str(x) + ' ' + str( y) + ' ' + str(width) + ' ' + str(height) + ' ' filename = imglist[i].split('.')[0] filename = filename.split('/')[-1] submission_dict[filename] = predictionstring assert len(submission_dict) <= num_images sub = pd.DataFrame.from_dict(submission_dict, orient='index') sub.index.name = 'patientId' sub.columns = ['PredictionString'] sub.to_csv(os.path.join(args.output_dir, "submission.csv"))
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset.startswith("maze"): dataset = datasets.get_maze_dataset() cfg.MODEL.NUM_CLASSES = 4 elif args.dataset.startswith("apollo"): dataset = datasets.get_apollo_dataset() cfg.MODEL.NUM_CLASSES = 12 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.1, kp_thresh=2) for i in range(3): pred_boxes = boxes_pred[i] for image in pred_boxes.keys(): filename = image.split('.')[0] + '.txt' file1 = open(pred_path + filename, 'a+') boxes = pred_boxes[image]['boxes'] scores = pred_boxes[image]['scores'] for score, box in zip(scores, boxes): file1.write( str(classes[i]) + " " + str(score) + " " + str(box[0]) + " " + str(box[1]) + " " + str(box[2]) + " " + str(box[3]) + "\n") file1.close()
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): ''' this datasets is a dummy one, with only N+1 labels including background the classes of the dataset obj is an extended one of dict, the structure is <K, V> = <index from 0 to N, class name>, quite simple ''' dataset = datasets.get_coco_dataset() '''num_classes is not in yaml, but declared in config.py, set to -1 by default''' cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset == "triangle_2017": cfg.TRAIN.DATASETS = ('triangle_2017_train', ) cfg.MODEL.NUM_CLASSES = 81 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) ''' this method is imported from core.config, which holds all params with correct hierachy from yaml file. Remarkably, the weight in yaml is not reloaded because reloat_pretrained_weight is set to false explicitly ''' cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() ''' Generalized_RCNN is a class from modeling.model_builder extending Module.nn, complicated attributes will explore mentioned-oriented ''' if cfg.RETINANET.RETINANET_ON: model = RetinaNet() else: model = Generalized_RCNN() if args.cuda: model.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) '''This should be a .pth file''' checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) ''' load the params into model's state dictionary we have step, train_size, batch_size, model, optimizer keys in the dict ''' net_utils.load_ckpt(model, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(model, args.load_detectron) model = mynn.DataParallel(model, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU '''nn method meaning evalutation, Sets the module in evaluation mode''' model.eval() if args.image_dir: '''from utils.misc, grab all images in the image_dir into a list''' imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: '''so it can also be a list of images that pass in''' imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): im = cv2.imread(imglist[i]) assert im is not None ''''a dictionary of timer object from utils''' timers = defaultdict(Timer) '''This is from core.test''' cls_boxes, cls_segms, cls_keyps = im_detect_all(model, im, timers=timers) '''eliminate suffix .jpg''' im_name, _ = os.path.splitext(os.path.basename(imglist[i])) '''this method from utils.vis visualize the image with bbox''' vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) #FIXME: this part not working somehow, but not important, will fix it later on if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset == "pascal_parts_heads": dataset = datasets.get_head_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset == "scuthead_a": dataset = datasets.get_head_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) outputfile = os.path.join(args.output_dir, im_name) head_boxes = cls_boxes[1] print('img :', i, ' num_heads :', len(head_boxes), ' img_path :', imglist[i]) np.save(outputfile, head_boxes)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("mapillary"): dataset = datasets.get_mapillary_dataset() cfg.MODEL.NUM_CLASSES = 38 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) im_list = [] for i in range(num_images): im_name, _ = os.path.splitext(os.path.basename(imglist[i])) im_list.append(im_name) processed_save_path = "/nfs/project/libo_i/mask-rcnn.pytorch/vis_mid_result.json" submit_json_path = "/nfs/project/libo_i/mask-rcnn.pytorch/submit_json/map_trainval_29999/segmentations_coco_2017_test_multi_results.json" if os.path.exists(processed_save_path) is False: config_path = "/nfs/project/libo_i/mask-rcnn.pytorch/config.json" with open(config_path) as f: config_json = json.load(f) cls_id_map = {} cnt = 1 for id, item in enumerate(config_json['labels']): if item['instances']: cls_id_map.update({id + 1: cnt}) cnt += 1 with open(submit_json_path) as f: segms_json = json.load(f) # processed_segms_json使用im_name索引具体的cls_segms processed_segms_list = process_input_json(segms_json, im_list, cls_id_map) with open(processed_save_path, 'w') as f: json.dump(processed_segms_list, f) else: with open(processed_save_path) as f: processed_segms_list = json.load(f) score_path = "/nfs/project/libo_i/mask-rcnn.pytorch/vis_mid_score.json" if os.path.exists(score_path) is False: config_path = "/nfs/project/libo_i/mask-rcnn.pytorch/config.json" with open(config_path) as f: config_json = json.load(f) cls_id_map = {} cnt = 1 for id, item in enumerate(config_json['labels']): if item['instances']: cls_id_map.update({id + 1: cnt}) cnt += 1 with open(submit_json_path) as f: segms_json = json.load(f) # processed_segms_json使用im_name索引具体的cls_segms score_list = process_score_json(segms_json, im_list, cls_id_map) with open(score_path, 'w') as f: json.dump(score_list, f) else: with open(score_path) as f: score_list = json.load(f) # assert len(processed_segms_list) == num_images for i in range(num_images): print('img {} \ {}'.format(i, num_images)) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) # cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) cls_boxes = None cls_keyps = None im_name, _ = os.path.splitext(os.path.basename(imglist[i])) cls_segms = processed_segms_list[im_name] score = score_list[im_name] # tolist_box = [[v.tolist() for v in i] for i in cls_boxes] # all_segms[im_name].update({'boxes': tolist_box}) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, score, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
from __future__ import absolute_import
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) for i in xrange(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, args.output_dir, cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.3, show_class=True, thresh=0.7, kp_thresh=2) if args.merge_pdfs and num_images > 1: merge_out_path = '{}/results.pdf'.format(args.output_dir) if os.path.exists(merge_out_path): os.remove(merge_out_path) command = "pdfunite {}/*.pdf {}".format(args.output_dir, merge_out_path) subprocess.call(command, shell=True)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) prefix_path = args.output_dir os.makedirs(prefix_path, exist_ok=True) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) writen_results = [] # validate demo_im = cv2.imread(imglist[0]) print(np.shape(demo_im)) h, w, _ = np.shape(demo_im) #print(h) #print(args.height) assert h == args.height assert w == args.width h_scale = 720 / args.height w_scale = 1280 / args.width for i in tqdm(range(num_images)): im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) # boxs = [[x1, y1, x2, y2, cls], ...] boxes, _, _, classes = convert_from_cls_format(cls_boxes, cls_segms, cls_keyps) if boxes is None: continue # scale boxes[:, 0] = boxes[:, 0] * w_scale boxes[:, 2] = boxes[:, 2] * w_scale boxes[:, 1] = boxes[:, 1] * h_scale boxes[:, 3] = boxes[:, 3] * h_scale if classes == []: continue for instance_idx, cls_idx in enumerate(classes): cls_name = dataset.classes[cls_idx] if cls_name == 'motorcycle': cls_name = 'motor' elif cls_name == 'stop sign': cls_name = 'traffic sign' elif cls_name == 'bicycle': cls_name = 'bike' if cls_name not in bdd_category: continue writen_results.append({ "name": imglist[i].split('/')[-1], "timestamp": 1000, "category": cls_name, "bbox": boxes[instance_idx, :4], "score": boxes[instance_idx, -1] }) with open(os.path.join(prefix_path, args.name + '.json'), 'w') as outputfile: json.dump(writen_results, outputfile, cls=MyEncoder)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) if args.dataset.startswith("coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) elif args.dataset.startswith("keypoints_coco"): dataset = datasets.get_coco_dataset() cfg.MODEL.NUM_CLASSES = 2 elif args.dataset.startswith("gangjin"): dataset = datasets.get_gangjin_dataset() cfg.MODEL.NUM_CLASSES = len(dataset.classes) else: raise ValueError('Unexpected dataset name: {}'.format(args.dataset)) print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) img_ids = [] rects = [] for i in range(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) boxes, segms, keypoints, classes = vis_utils.convert_from_cls_format( cls_boxes, cls_segms, cls_keyps) if boxes is not None: for j in range(len(boxes)): # print(boxes[j][-1]) if float(boxes[j][-1]) < 0.99: # 阀值 continue xmin = float(boxes[j, 0]) xmax = float(boxes[j, 2]) ymin = float(boxes[j, 1]) ymax = float(boxes[j, 3]) img_ids.append(os.path.basename(imglist[i])) rects.append( str(xmin) + " " + str(ymin) + " " + str(xmax) + " " + str(ymax)) # im_name, _ = os.path.splitext(os.path.basename(imglist[i])) # vis_utils.vis_one_image( # im[:, :, ::-1], # BGR -> RGB for visualization # im_name, # args.output_dir, # cls_boxes, # cls_segms, # cls_keyps, # dataset=dataset, # box_alpha=0.3, # show_class=False, # thresh=0.99, # kp_thresh=2, # ext="jpg" # ) result_dict = {"ID": img_ids, "rects": rects} import pandas as pd result = pd.DataFrame.from_dict(result_dict) result.to_csv('submit/submit1.csv', header=None, index=False)
def main(): """main function""" if not torch.cuda.is_available(): sys.exit("Need a CUDA device to run the code.") args = parse_args() print('Called with args:') print(args) assert args.image_dir or args.images assert bool(args.image_dir) ^ bool(args.images) # Pedestrian or Background class Dataset: def __init__(self): self.classes = {0: 'background', 1: 'pedestrian'} dataset = Dataset() cfg.MODEL.NUM_CLASSES = 2 print('load cfg from file: {}'.format(args.cfg_file)) cfg_from_file(args.cfg_file) if args.set_cfgs is not None: cfg_from_list(args.set_cfgs) assert bool(args.load_ckpt) ^ bool(args.load_detectron), \ 'Exactly one of --load_ckpt and --load_detectron should be specified.' cfg.MODEL.LOAD_IMAGENET_PRETRAINED_WEIGHTS = False # Don't need to load imagenet pretrained weights assert_and_infer_cfg() maskRCNN = Generalized_RCNN() if args.cuda: maskRCNN.cuda() if args.load_ckpt: load_name = args.load_ckpt print("loading checkpoint %s" % (load_name)) checkpoint = torch.load(load_name, map_location=lambda storage, loc: storage) net_utils.load_ckpt(maskRCNN, checkpoint['model']) if args.load_detectron: print("loading detectron weights %s" % args.load_detectron) load_detectron_weight(maskRCNN, args.load_detectron) maskRCNN = mynn.DataParallel(maskRCNN, cpu_keywords=['im_info', 'roidb'], minibatch=True, device_ids=[0]) # only support single GPU maskRCNN.eval() if args.image_dir: imglist = misc_utils.get_imagelist_from_dir(args.image_dir) else: imglist = args.images num_images = len(imglist) if not os.path.exists(args.output_dir): os.makedirs(args.output_dir) if not os.path.exists(osp.join(args.output_dir, args.model_name)): os.makedirs(osp.join(args.output_dir, args.model_name)) for i in xrange(num_images): print('img', i) im = cv2.imread(imglist[i]) assert im is not None timers = defaultdict(Timer) cls_boxes, cls_segms, cls_keyps = im_detect_all(maskRCNN, im, timers=timers) im_name, _ = os.path.splitext(os.path.basename(imglist[i])) vis_utils.vis_one_image( im[:, :, ::-1], # BGR -> RGB for visualization im_name, osp.join(args.output_dir, args.model_name), cls_boxes, cls_segms, cls_keyps, dataset=dataset, box_alpha=0.5, show_class=True, thresh=0.9, kp_thresh=2, ext='png')