def __init__(self, do_cuda=True): self.lock = RLock() self.opts = rfnetOptions(do_cuda) print(self.opts) print('rfnet init') #print(f"{gct()} model init") print("model init") det = RFDetSO( cfg.TRAIN.score_com_strength, cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, cfg.TRAIN.TOPK, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO( det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, cfg.TRAIN.TOPK ) device = torch.device("cuda") model = model.to(device) #resume = args.resume #resume = "/home/cviss3/PycharmProjects/gensynth_dev_env/pyslam/thirdparty/rfnet/runs/10_24_09_25/model/e121_NN_0.480_NNT_0.655_NNDR_0.813_MeanMS_0.649.pth.tar" resume = "/content/RFnetpyslam/thirdparty/rfnet/runs/10_24_09_25/model/e121_NN_0.480_NNT_0.655_NNDR_0.813_MeanMS_0.649.pth.tar" print('==> Loading pre-trained network.') checkpoint = torch.load(resume) model.load_state_dict(checkpoint["state_dict"]) self.fe = model print('==> Successfully loaded pre-trained network.') self.device = device self.pts = [] self.kps = [] self.des = [] self.img = [] self.heatmap = [] self.frame = None self.frameFloat = None self.keypoint_size = 20 # just a representative size for visualization and in order to convert extracted points to cv2.KeyPoint
print(f"{gct()} : Build the model") det = RFDetSO( cfg.TRAIN.score_com_strength, cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, cfg.TRAIN.TOPK, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO(det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, cfg.TRAIN.TOPK) if mgpu: model = torch.nn.DataParallel(model) model = model.to(device=device) ############################################################################### # Load train data ############################################################################### PPT = [cfg.PROJ.TRAIN_PPT, (cfg.PROJ.TRAIN_PPT + cfg.PROJ.EVAL_PPT)] print(f"{gct()} : Loading traning data") train_data = DataLoader( HpatchDataset( data_type="train", PPT=PPT, use_all=cfg.PROJ.TRAIN_ALL,
model_file = "./model/e121_NN_0.480_NNT_0.655_NNDR_0.813_MeanMS_0.649.pth.tar" det = RFDetSO( cfg.TRAIN.score_com_strength, cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, cfg.TRAIN.TOPK, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO(det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, cfg.TRAIN.TOPK) model = model.to(device) checkpoint = torch.load(model_file) model.load_state_dict(checkpoint["state_dict"]) model.eval() def distance_matrix_vector(anchor, positive): eps = 1e-8 FeatSimi_Mat = 2 - 2 * torch.mm(anchor, positive.t()) # [0, 4] FeatSimi_Mat = FeatSimi_Mat.clamp(min=eps, max=4.0) FeatSimi_Mat = torch.sqrt(FeatSimi_Mat) # euc [0, 2] return FeatSimi_Mat
#输入模型参数 det = RFDetSO( cfg.TRAIN.score_com_strength, cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, cfg.TRAIN.TOPK, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO(det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, cfg.TRAIN.TOPK) print(f"{gct()} : to device") device = torch.device("cuda") model = model.to(device) resume = args.resume print(f"{gct()} : in {resume}") checkpoint = torch.load(resume) model.load_state_dict(checkpoint["state_dict"]) ############################################################################### # detect and compute ############################################################################### #split表示通过分隔符对字符串进行切割 img1_path, img2_path = args.imgpath.split("@") kp1, des1, img1 = model.detectAndCompute(img1_path, device, (240, 320))
cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, args.k, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO( det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, args.k, ) model = model.to(device) checkpoint = torch.load(model_file) model.load_state_dict(checkpoint["state_dict"]) model.eval() random.seed(cfg.PROJ.SEED) torch.manual_seed(cfg.PROJ.SEED) np.random.seed(cfg.PROJ.SEED) root_dir = '/home/wang/workspace/RFSLAM_offline/RFNET/data/' csv_file = None seq = None
cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, 1024, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO( det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, 512, ) model = model.to(device) flop, para = profile(det, (torch.randn((1, 1, 640, 480), device=device), )) ''' flop 6829056000.0 para Out[4]: 21890.0 ''' flop2, para2 = profile(des, (torch.randn((1000, 1, 32, 32), device=device), )) ''' flop2
model_file = "/home/wang/workspace/CVToolbox/benchmark/evaluationMMA/model/e121_NN_0.480_NNT_0.655_NNDR_0.813_MeanMS_0.649.pth.tar" det = RFDetSO( cfg.TRAIN.score_com_strength, cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, cfg.TRAIN.TOPK, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO(det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, cfg.TRAIN.TOPK) model = model.to(device) checkpoint = torch.load(model_file) model.load_state_dict(checkpoint["state_dict"]) model.eval() img1_path = "./material/4_1.png" img2_path = "./material/4_2.png" img1 = cv2.imread(img1_path) img2 = cv2.imread(img2_path) width = img1.shape[1] kp1, des1, _ = model.detectAndCompute(img1_path, device, (img1.shape[0], img1.shape[1])) kp2, des2, _ = model.detectAndCompute(img2_path, device, (img2.shape[0], img2.shape[1]))
print(f"{gct()} : model init") det = RFDetSO( cfg.TRAIN.score_com_strength, cfg.TRAIN.scale_com_strength, cfg.TRAIN.NMS_THRESH, cfg.TRAIN.NMS_KSIZE, cfg.TRAIN.TOPK, cfg.MODEL.GAUSSIAN_KSIZE, cfg.MODEL.GAUSSIAN_SIGMA, cfg.MODEL.KSIZE, cfg.MODEL.padding, cfg.MODEL.dilation, cfg.MODEL.scale_list, ) des = HardNetNeiMask(cfg.HARDNET.MARGIN, cfg.MODEL.COO_THRSH) model = RFNetSO(det, des, cfg.LOSS.SCORE, cfg.LOSS.PAIR, cfg.PATCH.SIZE, cfg.TRAIN.TOPK) print(f"{gct()} : to device") device = torch.device("cuda") model = model.to(device) resume = '/home/wang/workspace/Faster-net2/runs/10_24_09_25/model/e121_NN_0.480_NNT_0.655_NNDR_0.813_MeanMS_0.649.pth.tar' print(f"{gct()} : in {resume}") checkpoint = torch.load(resume) model.load_state_dict(checkpoint["state_dict"]) def to_cv2_kp(kp, scale, angle): # kp is like [batch_idx, y, x, channel] return cv2.KeyPoint(kp[2], kp[1], scale, angle)