def pre_init_with_sift(trainPhotoTourDataset): patch_size = 65 ON_GPU = True SIFT = SIFTNet(patch_size=patch_size, do_cuda=ON_GPU) SIFT.eval() if ON_GPU: SIFT.cuda() transformed = [] for img in trainPhotoTourDataset.data: transformed.append( np.expand_dims(cv2.resize(img.cpu().numpy(), (65, 65)), axis=0)) phototour_loader = data_utils.DataLoader( PhototourTrainingData(transformed), batch_size=256, shuffle=False) descriptors = [] pbar = tqdm(enumerate(phototour_loader)) for batch_idx, data_a in pbar: if ON_GPU: torch_patches = Variable(data_a.type(torch.FloatTensor).cuda(), volatile=True) else: torch_patches = Variable(data_a.type(torch.FloatTensor), volatile=True) res = SIFT(torch_patches) sift = np.round(512. * res.data.cpu().numpy()).astype(np.float32) descriptors.extend(sift) return np.array(descriptors)
args.cuda = not args.no_cuda and torch.cuda.is_available() if args.cuda: cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) # create loggin directory if not os.path.exists(args.log_dir): os.makedirs(args.log_dir) # set random seeds torch.manual_seed(args.seed) np.random.seed(args.seed) if args.descriptor == 'SIFT': descriptor = SIFTNet(patch_size=32) if not args.no_cuda: descriptor = descriptor.cuda() elif args.descriptor == 'HardNet': descriptor = HardNet() #descriptor = HardNetNarELU(SIFTNet(patch_size=32)) if not args.no_cuda: descriptor = descriptor.cuda() model_weights = 'HardNet++.pth' #model_weights = 'HardNetELU_Narr.pth' hncheckpoint = torch.load(model_weights) descriptor.load_state_dict(hncheckpoint['state_dict']) descriptor.train() else: descriptor = lambda x: x.view(x.size(0),-1)
args.cuda = not args.no_cuda and torch.cuda.is_available() if args.cuda: cudnn.benchmark = True torch.cuda.manual_seed_all(args.seed) # create loggin directory if not os.path.exists(args.log_dir): os.makedirs(args.log_dir) # set random seeds torch.manual_seed(args.seed) np.random.seed(args.seed) random.seed(args.seed) if args.descriptor == 'SIFT': descriptor = SIFTNet(patch_size=PS) if not args.no_cuda: descriptor = descriptor.cuda() elif args.descriptor == 'HardNet': descriptor = HardNet() if not args.no_cuda: descriptor = descriptor.cuda() model_weights = 'HardNet++.pth' hncheckpoint = torch.load(model_weights) descriptor.load_state_dict(hncheckpoint['state_dict']) descriptor.train() elif args.descriptor == 'TFeat': descriptor = HardTFeatNet(sm=SIFTNet(patch_size=32)) if not args.no_cuda: descriptor = descriptor.cuda() model_weights = 'HardTFeat.pth'
return var_image_reshape from HardNet import HardNet, HardTFeatNet, L2Norm from pytorch_sift import SIFTNet import math d1 = lambda x: L2Norm()(x.view(x.size(0), -1) - x.view(x.size(0), -1).mean( dim=1, keepdim=True).expand(x.size(0), x.size(1) * x.size(2) * x.size(3)).detach()) d2 = HardNet() model_weights = '../../HardNet++.pth' hncheckpoint = torch.load(model_weights) d2.load_state_dict(hncheckpoint['state_dict']) d2.eval() d3 = SIFTNet(patch_size=32) model_weights = 'HardTFeat.pth' d4 = HardTFeatNet(sm=SIFTNet(patch_size=32)) checkpoint = torch.load(model_weights) d4.load_state_dict(checkpoint['state_dict']) d4 = nn.Sequential(d4, L2Norm()) desc_list = [d1, d2, d3, d4] desc_names = ['Pixels', 'HardNet', 'SIFT', 'TFeat'] USE_CUDA = False detector = ScaleSpaceAffinePatchExtractor(mrSize=5.12, num_features=200, border=32, num_Baum_iters=0) descriptor = HardNet()
5.0, scale_log = 0.4) if len(geom_loss.size()) == 0: print 'skip' continue print 'A', A1.data.cpu().numpy()[0:1,:,:] #print fro_dists loss = geom_loss.mean() total_feats += geom_loss.size(0) total_loss += loss.data.cpu().numpy()[0] print 'test img', batch_idx, loss.data.cpu().numpy()[0], geom_loss.size(0) print 'Total loss:', total_loss / float(batch_idx+1), 'features', float(total_feats) / float(batch_idx+1) model.num = model_num_feats train_loader, test_loader = create_loaders() HA = ScaleSpaceAffinePatchExtractor( mrSize = 5.192, num_features = 350, border = 5, num_Baum_iters = 1, AffNet = LAFNet()) from pytorch_sift import SIFTNet SIFT = SIFTNet(patch_size = 32 ) if USE_CUDA: HA = HA.cuda() optimizer1 = create_optimizer(HA.AffNet, BASE_LR, 1e-4) #test(test_loader, model, cuda = USE_CUDA) for epoch in range(n_epochs): print 'epoch', epoch train(train_loader, HA, optimizer1, epoch, cuda = USE_CUDA) test(test_loader, HA, cuda = USE_CUDA)
for t in self.itr: im_path = os.path.join(base, t + '.png') im = cv2.imread(im_path, 0) self.N = im.shape[0] / 65 setattr(self, t, np.split(im, self.N)) seqs = glob.glob(sys.argv[1] + '/*') seqs = [os.path.abspath(p) for p in seqs] descr_name_vl = 'pytorch-sift-vlfeat-' + str(OUT_W) descr_name_mp = 'pytorch-sift-mp-' + str(OUT_W) descr_names = [descr_name_vl, descr_name_mp] model_vl = SIFTNet(OUT_W, mask_type='Gauss', sigma_type='vlfeat') model_vl.cuda() print(model_vl) model_mp = SIFTNet(OUT_W, mask_type='CircularGauss', sigma_type='hesamp') model_mp.cuda() print(model_mp) models = [model_vl, model_mp] for seq_path in seqs: seq = hpatches_sequence(seq_path) for descr_id, descr_name in enumerate(descr_names): model = models[descr_id] path = os.path.join(descr_name, seq.name) if not os.path.exists(path):
min_tilt=1.0, max_tilt=1.0, max_rot=1.0, max_shift=0.5, mrSize=1.0, use_cuda=USE_CUDA) def weights_init(m): if isinstance(m, nn.Conv2d): nn.init.xavier_normal(m.weight.data) #SIRNet = SparseImgRepresenter(detector_net = ConvST_net, descriptor_net = hardnet) SIRNet = SparseImgRepresenter(detector_net=ConvST_net, descriptor_net=SIFTNet(patch_size=16, do_cuda=USE_CUDA)) SIRNet.detector.apply(weights_init) #aff_norm_patches, LAFs, descs = SIRNet(var_image_reshape) def distance_matrix_vector(anchor, positive): """Given batch of anchor descriptors and positive descriptors calculate distance matrix""" d1_sq = torch.sum(anchor * anchor, dim=1) d2_sq = torch.sum(positive * positive, dim=1) eps = 1e-6 return torch.sqrt( torch.abs((d1_sq.expand(positive.size(0), anchor.size(0)) + torch.t(d2_sq.expand(anchor.size(0), positive.size(0))) - 2.0 * torch.bmm(positive.unsqueeze(0), torch.t(anchor).unsqueeze(0)).squeeze(0)) +
assert ((height % patch_h == 0) and (width % patch_w == 0)) patch_idxs = [] patches = [] current_patch_idx = start_patch_idx for y in range(0, height, patch_h): patch_idxs.append([]) for x in range(0, width, patch_w): patch = np.array(img.crop( (x, y, x + patch_w, y + patch_h))).astype(np.uint8) patches.append(patch) patch_idxs[-1].append(current_patch_idx) current_patch_idx += 1 return np2torch(np.array(patches)), patch_idxs, patch_idxs[-1][-1] model = SIFTNet(patch_size=41) model.cuda() model.eval() try: input_img_fname = sys.argv[1] output_fname = sys.argv[2] except: print "Wrong input format. Try ./rank_scale_patches.py imgs/ref.png out.txt" sys.exit(1) patches, idxs, max_idx = read_patch_file(input_img_fname, patch_w=73, patch_h=41) bw_patches = patches.float().mean(dim=1, keepdim=True) print bw_patches.shape