def __init__(
        self,
        backbone=None,
        neck=None,
        bbox_head=None,
        train_cfg=None,
        test_cfg=None,
        pretrained=None,
        distill=None,
    ):
        super(Distilling_Single,
              self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg,
                             pretrained)
        from mmdet.apis.inference import init_detector

        self.device = torch.cuda.current_device()
        # breakpoint()
        self.teacher = init_detector(distill.teacher_cfg, \
                        distill.teacher_model_path, self.device)
        self.stu_feature_adap = build_neck(distill.stu_feature_adap)

        self.distill_feat_weight = distill.get("distill_feat_weight", 0)
        self.distill_cls_weight = distill.get("distill_cls_weight", 0)
        self.distill_bbox_weight = distill.get("distill_bbox_weight", 0)

        for m in self.teacher.modules():
            for param in m.parameters():
                param.requires_grad = False
        self.distill_warm_step = distill.distill_warm_step
        self.debug = distill.get("debug", False)

        self.bbox_head.loss_cls.register_forward_hook(
            hook=self.get_target_hook)
        self.target_list = []
Beispiel #2
0
def main():
    check_args()
    model = init_detector(args.config, args.ckpt, args.gpu)
    img_name_list = os.listdir(args.images)
    img_name_list.sort()
    tot_imgs = len(img_name_list)
    print('Total {} images to inference'.format(tot_imgs))
    progbar = mmcv.ProgressBar(len(img_name_list))
    all_sub_result = []
    for img_name in img_name_list:
        sub_result = [[] for i in range(10)]
        img_path = osp.join(args.images, img_name)
        result = inference_detector(model, img_path)
        for i, cont in enumerate(result):
            cont = cont.tolist()
            #print("**********************************")
            #for tmp in cont:
            #print(tmp)
            cate_name = train_cate[i]
            sub_index = submit_cate[cate_name]
            #print('train_cate_id:{},sub_index:{}'.format(i,sub_index))
            sub_result[sub_index] = cont
        all_sub_result.append(sub_result)
        progbar.update()
    json.dump(all_sub_result, open(args.out, 'w'))
    print("finish!")
Beispiel #3
0
def main():
    parser = ArgumentParser()
    parser.add_argument('img', help='Image file')
    parser.add_argument('config', help='Config file')
    parser.add_argument('checkpoint', help='Checkpoint file stage 1')
    parser.add_argument('--model_stage_2', help='Checkpoint file stage 2')
    parser.add_argument('--device',
                        default='cuda:0',
                        help='Device used for inference')
    parser.add_argument('--score-thr',
                        type=float,
                        default=0.3,
                        help='bbox score threshold')
    args = parser.parse_args()

    # build the model from a config file and a checkpoint file
    model = init_detector(args.config, args.checkpoint, device=args.device)

    # test a single image
    result = inference_detector(model, args.img)

    # segmentation_refine
    if args.model_stage_2 is not None:
        result = segmentation_refine(result, args.model_stage_2, args.img)

    result_img = show_result_pyplot(model,
                                    args.img,
                                    result,
                                    score_thr=args.score_thr)
Beispiel #4
0
def main():
    args = parse_args()
    cfg = Config.fromfile(args.config_file)
    model = init_detector(args.config_file, args.ckpt_file, device='cuda:0')
    result, uncertainty = inference_detector(model, args.img_file)
    # uncertainty = calculate_uncertainty_single(cfg, model, args.img_file, return_box=False)
    model.show_result(args.img_file, result, out_file=args.out_file)
    print('Image uncertainty is: ' + str(uncertainty.cpu().numpy()))
def main():
  # args = parse_args()
  # config_file = args.config
  # checkpoint_file = args.checkpoint
  # p = Pool(4)
  config_file = "./configs/rscup/htc_res50.py"
  checkpoint_file = "./work_dirs/htc_res50/epoch_12.pth"
  model = init_detector(config_file, checkpoint_file)
  print(model.CLASSES)
  img = './result/demo/7.jpg'
  results = inference_detector(model, img)
Beispiel #6
0
def main():
    m = torch.load(
        '../../../work_dirs/fabric/defectnet_inverse_cascade_rcnn_r50_fpn_1x/epoch_12.pth'
    )
    print(get_parameter_number(m['state_dict']))

    model = init_detector(
        '../../../configs/fabric/defectnet_inverse_cascade_rcnn_r50_fpn_1x.py',
        '../../../work_dirs/fabric/defectnet_inverse_cascade_rcnn_r50_fpn_1x/epoch_12.pth'
    )

    # count_params(model, (3, 800, 1333))
    stat(model, fake_data(model, '../../../demo/normal_demo.jpg'))
    stat(model, fake_data(model, '../../../demo/defective_demo.jpg'))
def mmdet_inference(config, ckpt, model_name):
    train_cate = train_id_cate()
    model = init_detector(config, ckpt)
    img_name_list = os.listdir(args.images)
    img_name_list.sort()
    tot_imgs = len(img_name_list)
    print("model:{},total {} images to inference".format(config, tot_imgs))

    probar = mmcv.ProgressBar(tot_imgs)
    all_img_bboxs_res = []

    for img_name in img_name_list:
        img_bbox_res = {}
        img_path = osp.join(args.images, img_name)
        img = cv2.imread(img_path, -1)
        img_w, img_h = img.shape[1], img.shape[0]
        result = inference_detector(model, img_path)
        bboxs_list = []
        scores_list = []
        labels_list = []
        for i, cont in enumerate(result):
            cont = cont.tolist()
            if cont:
                for data in cont:
                    label = i
                    submit_label = submit_cate[train_cate[label]]
                    data[0] = data[0] / img_w
                    data[2] = data[2] / img_w
                    data[1] = data[1] / img_h
                    data[3] = data[3] / img_h
                    bboxs_list.append(data[:4])
                    scores_list.append(data[-1])
                    labels_list.append(submit_label)
        img_bbox_res['img'] = img_name
        img_bbox_res['bboxs'] = bboxs_list
        img_bbox_res['scores'] = scores_list
        img_bbox_res['labels'] = labels_list
        all_img_bboxs_res.append(img_bbox_res)
        probar.update()
    json.dump(all_img_bboxs_res, open('./fusion_' + model_name + '.json', 'w'))
    return all_img_bboxs_res
Beispiel #8
0
def main():
    #detection debug
    if debug is True:
        print(load_result())
        return
    path = sys.path[0]
    # config = path + '/../configs/faster_rcnn_r50_fpn_1x.py'
    # checkpoint = path + '/../work_dirs/faster_rcnn_r50_fpn_1x/latest.pth'
    config = path + '/../work_dirs/backplane_voc_20200520_rcnn_r50_fpn_1x_multiscale_kmeans_scorethr0.05_2/faster_rcnn_r50_fpn_1x_20200614_173610.py'
    checkpoint = path + '/../work_dirs/backplane_voc_20200520_rcnn_r50_fpn_1x_multiscale_kmeans_scorethr0.05_2/latest.pth'
    model = init_detector(config, checkpoint)
    my_config.set('classes', model.CLASSES)
    # print(model)
    img = path + '/test.jpg'
    if (len(sys.argv) > 1):
        img = sys.argv[1]
    print(img)
    result = inference_detector(model, img)
    out_file = osp.splitext(img)[0] + "_result.png"
    # show_result_pyplot(img, result, model.CLASSES, score_thr=0.05)
    show_result(img, result, model.CLASSES, out_file=out_file)
    print("out_file:%s" % out_file)
Beispiel #9
0
    test_pipeline = Compose(test_pipeline)
    data = dict(img=img)
    data = test_pipeline(data)
    data = scatter(collate([data], samples_per_gpu=1), [device])[0]
    with torch.no_grad():
        result = model(return_loss=False, rescale=True, **data)
    return result


if __name__ == "__main__":
	# Define
	threshold = 0.3
	config = "configs/effdet/atss_effdet_d0.py"
	checkpoint = "work_dirs/atss_effdet_d0/latest.pth"

	out_dir = "demo/atss_effdet_d0"
	img_files = sorted(glob("demo/images/*.jpg"))
	# img_files = sorted(glob("datasets/coco/images/val2017/*.jpg"))[:10]

	# Setup
	model = init_detector(config, checkpoint=checkpoint, device='cuda')
	CLASSES = list(DATASET.CLASSES)
	os.makedirs(out_dir, exist_ok=True)

	# Inference
	for img_file in img_files:
		result = inference_detector(model, img_file)
		out_file = os.path.join(out_dir, '{}.jpg'.format(os.path.basename(img_file).split('.')[0]))
		show_result(img_file, result, CLASSES, score_thr=threshold, show=False, out_file=out_file)
		print("Result is saved at {}".format(out_file))
Beispiel #10
0
torch.cuda.manual_seed(args.seed)
np.random.seed(args.seed)
os.makedirs(args.save, exist_ok=True)

pre = transforms.Compose([transforms.ToTensor()])
nor = transforms.Normalize([123.675/255., 116.28/255., 103.53/255.],[58.395/255., 57.12/255., 57.375/255.])

model1 = Yolov4(yolov4conv137weight=None, n_classes=80, inference=True)
pretrained_dict = torch.load('checkpoints/yolov4.pth', map_location=torch.device('cuda'))
model1.load_state_dict(pretrained_dict)
model1.eval().cuda()

config = './mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint = './checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
meta = [{'filename': '../images/6.png', 'ori_filename': '../images/6.png', 'ori_shape': (500, 500, 3), 'img_shape': (800, 800, 3), 'pad_shape': (800, 800, 3), 'scale_factor': np.array([1.6, 1.6, 1.6, 1.6], dtype=np.float32), 'flip': False, 'flip_direction': None, 'img_norm_cfg': {'mean': np.array([123.675, 116.28 , 103.53 ], dtype=np.float32), 'std': np.array([58.395, 57.12 , 57.375], dtype=np.float32), 'to_rgb': True}}]
model2 = init_detector(config, checkpoint, device='cuda:0')

cfg = model2.cfg
device = next(model2.parameters()).device  # model device
# build the data pipeline
test_pipeline = [LoadImage()] + cfg.data.test.pipeline[1:]
test_pipeline = Compose(test_pipeline)
#print(test_pipeline)

def get_mask(image, meta, pixels):
    mask = torch.zeros((1,3,500,500)).cuda()
    bbox, label = model2(return_loss=False, rescale=True, img=image, img_metas=meta)
    bbox = bbox[bbox[:,4]>0.3]
    num = bbox.shape[0]
    if num > 10: num = 10
    if num == 0: return mask.float().cuda()