Exemple #1
0
def AffNetHardNet_describeFromKeys(img_np, KPlist):
    img = torch.autograd.Variable(torch.from_numpy(img_np.astype(np.float32)),
                                  volatile=True)
    img = img.view(1, 1, img.size(0), img.size(1))
    HessianAffine = ScaleSpaceAffinePatchExtractor(mrSize=5.192,
                                                   num_features=0,
                                                   border=0,
                                                   num_Baum_iters=0)
    if USE_CUDA:
        HessianAffine = HessianAffine.cuda()
        img = img.cuda()
    with torch.no_grad():
        HessianAffine.createScaleSpace(
            img)  # to generate scale pyramids and stuff
    descriptors = []
    Alist = []
    n = 0
    # for patch_np in patches:
    for kp in KPlist:
        x, y = np.float32(kp.pt)
        LAFs = normalizeLAFs(
            torch.tensor([[AffNetPix.PS / 2, 0, x], [0, AffNetPix.PS / 2,
                                                     y]]).reshape(1, 2, 3),
            img.size(3), img.size(2))
        with torch.no_grad():
            patch = HessianAffine.extract_patches_from_pyr(denormalizeLAFs(
                LAFs, img.size(3), img.size(2)),
                                                           PS=AffNetPix.PS)
        if WRITE_IMGS_DEBUG:
            SaveImageWithKeys(patch.detach().cpu().numpy().reshape([32, 32]),
                              [], 'p2/' + str(n) + '.png')
        if USE_CUDA:
            # or ---> A = AffNetPix(subpatches.cuda()).cpu()
            with torch.no_grad():
                A = batched_forward(AffNetPix, patch.cuda(), 256).cpu()
        else:
            with torch.no_grad():
                A = AffNetPix(patch)
        new_LAFs = torch.cat([torch.bmm(A, LAFs[:, :, 0:2]), LAFs[:, :, 2:]],
                             dim=2)
        dLAFs = denormalizeLAFs(new_LAFs, img.size(3), img.size(2))
        with torch.no_grad():
            patchaff = HessianAffine.extract_patches_from_pyr(dLAFs, PS=32)
            if WRITE_IMGS_DEBUG:
                SaveImageWithKeys(
                    patchaff.detach().cpu().numpy().reshape([32, 32]), [],
                    'p1/' + str(n) + '.png')
                SaveImageWithKeys(img_np, [kp], 'im1/' + str(n) + '.png')
            descriptors.append(
                HardNetDescriptor(patchaff).cpu().numpy().astype(np.float32))
            Alist.append(
                convertLAFs_to_A23format(LAFs.detach().cpu().numpy().astype(
                    np.float32)))
    n = n + 1
    return descriptors, Alist
 def forward(self,x, random_Baum = False, random_resp = False, return_patches = False):
     ### Detection
     num_features_prefilter = self.num
     #if self.num_Baum_iters > 0:
     #    num_features_prefilter = 2 * self.num;
     if random_resp:
         num_features_prefilter *= 4
     responses, LAFs, final_pyr_idxs, final_level_idxs, scale_pyr = self.multiScaleDetector(x,num_features_prefilter)
     if random_resp:
         if self.num < responses.size(0):
             ridxs = torch.randperm(responses.size(0))[:self.num]
             if x.is_cuda:
                 ridxs = ridxs.cuda() 
             responses = responses[ridxs]
             LAFs = LAFs[ridxs ,:,:]
             final_pyr_idxs = final_pyr_idxs[ridxs]
             final_level_idxs = final_level_idxs[ridxs]
     LAFs[:,0:2,0:2] =   self.mrSize * LAFs[:,:,0:2]
     n_iters = self.num_Baum_iters;
     if random_Baum and (n_iters > 1):
         n_iters = int(np.random.randint(1,n_iters + 1)) 
     if n_iters > 0:
         responses, LAFs, final_pyr_idxs, final_level_idxs  = self.getAffineShape(scale_pyr, responses, LAFs,
                                                                                  final_pyr_idxs, final_level_idxs, self.num, n_iters = n_iters)
     #LAFs = self.getOrientation(scale_pyr, LAFs, final_pyr_idxs, final_level_idxs)
     #if return_patches:
     #    pyr_inv_idxs = get_inverted_pyr_index(scale_pyr, final_pyr_idxs, final_level_idxs)
     #    patches = extract_patches_from_pyramid_with_inv_index(scale_pyr, pyr_inv_idxs, LAFs, PS = self.PS)
     if return_patches:
         patches = extract_patches(x, LAFs, PS = self.PS)
     else:
         patches = None
     return denormalizeLAFs(LAFs, x.size(3), x.size(2)), patches, responses#, scale_pyr
Exemple #3
0
def getAffmaps_from_Affnet(patches_np):
    sp1, sp2 = np.shape(patches_np[0])
    subpatches = torch.autograd.Variable(
        torch.zeros([len(patches_np), 1, 32, 32], dtype=torch.float32),
        volatile=True).view(len(patches_np), 1, 32, 32)
    for k in range(0, len(patches_np)):
        subpatch = patches_np[k][int(sp1 / 2) - 16:int(sp2 / 2) + 16,
                                 int(sp1 / 2) - 16:int(sp2 / 2) + 16].reshape(
                                     1, 1, 32, 32)
        subpatches[k, :, :, :] = torch.from_numpy(subpatch.astype(
            np.float32))  #=subpatch

    x, y = subpatches.shape[3] / 2.0 + 2, subpatches.shape[2] / 2.0 + 2
    LAFs = normalizeLAFs(
        torch.tensor([[AffNetPix.PS / 2, 0, x], [0, AffNetPix.PS / 2,
                                                 y]]).reshape(1, 2, 3),
        subpatches.shape[3], subpatches.shape[2])
    baseLAFs = torch.zeros([subpatches.shape[0], 2, 3], dtype=torch.float32)
    for m in range(subpatches.shape[0]):
        baseLAFs[m, :, :] = LAFs

    if USE_CUDA:
        # or ---> A = AffNetPix(subpatches.cuda()).cpu()
        with torch.no_grad():
            A = batched_forward(AffNetPix, subpatches.cuda(), 256).cpu()
    else:
        with torch.no_grad():
            A = AffNetPix(subpatches)
    LAFs = torch.cat([torch.bmm(A, baseLAFs[:, :, 0:2]), baseLAFs[:, :, 2:]],
                     dim=2)
    dLAFs = denormalizeLAFs(LAFs, subpatches.shape[3], subpatches.shape[2])
    Alist = convertLAFs_to_A23format(dLAFs.detach().cpu().numpy().astype(
        np.float32))
    return Alist
Exemple #4
0
 def forward(self, x, do_ori=False, verb=False):
     ### Detection
     t = time.time()
     num_features_prefilter = self.num
     if self.num_Baum_iters > 0:
         num_features_prefilter = int(1.5 * self.num)
     responses, LAFs, final_pyr_idxs, final_level_idxs = self.multiScaleDetector(
         x, num_features_prefilter)
     if verb:
         print((time.time() - t, 'detection multiscale'))
     t = time.time()
     LAFs[:, 0:2, 0:2] = self.mrSize * LAFs[:, :, 0:2]
     if self.num_Baum_iters > 0:
         responses, LAFs, final_pyr_idxs, final_level_idxs = self.getAffineShape(
             responses, LAFs, final_pyr_idxs, final_level_idxs, self.num)
     if verb:
         print((time.time() - t, 'affine shape iters'))
     t = time.time()
     if do_ori:
         LAFs = self.getOrientation(LAFs, final_pyr_idxs, final_level_idxs)
         #pyr_inv_idxs = get_inverted_pyr_index(self.scale_pyr, final_pyr_idxs, final_level_idxs)
     #patches = extract_patches_from_pyramid_with_inv_index(scale_pyr, pyr_inv_idxs, LAFs, PS = self.PS)
     #patches = extract_patches(x, LAFs, PS = self.PS)
     #print time.time() - t, len(LAFs), ' patches extraction'
     return denormalizeLAFs(LAFs, x.size(3), x.size(2)), responses
Exemple #5
0
 def forward(self,x, do_ori = True):
     ### Detection
     t = time.time()
     num_features_prefilter = self.num
     responses, LAFs, final_pyr_idxs, final_level_idxs = self.multiScaleDetectorAff(x,num_features_prefilter)
     print time.time() - t, 'detection multiscale'
     t = time.time()
     LAFs[:,0:2,0:2] =   self.mrSize * LAFs[:,:,0:2]
     if do_ori:
         LAFs = self.getOrientation(LAFs, final_pyr_idxs, final_level_idxs)
     #pyr_inv_idxs = get_inverted_pyr_index(scale_pyr, final_pyr_idxs, final_level_idxs)
     #patches = extract_patches_from_pyramid_with_inv_index(scale_pyr, pyr_inv_idxs, LAFs, PS = self.PS)
     #patches = extract_patches(x, LAFs, PS = self.PS)
     #print time.time() - t, len(LAFs), ' patches extraction'
     return denormalizeLAFs(LAFs, x.size(3), x.size(2)), responses
 def forward(self,
             x,
             random_Baum=False,
             random_resp=False,
             return_patches=False):
     ### Detection
     num_features_prefilter = self.num
     #if self.num_Baum_iters > 0:
     #    num_features_prefilter = 2 * self.num;
     if random_resp:
         num_features_prefilter *= 4
     responses, LAFs, final_pyr_idxs, final_level_idxs, scale_pyr = self.multiScaleDetector(
         x, num_features_prefilter)
     if random_resp:
         if self.num < responses.size(0):
             ridxs = torch.randperm(responses.size(0))[:self.num]
             if x.is_cuda:
                 ridxs = ridxs.cuda()
             responses = responses[ridxs]
             LAFs = LAFs[ridxs, :, :]
             final_pyr_idxs = final_pyr_idxs[ridxs]
             final_level_idxs = final_level_idxs[ridxs]
     LAFs[:, 0:2, 0:2] = self.mrSize * LAFs[:, :, 0:2]
     n_iters = self.num_Baum_iters
     if random_Baum and (n_iters > 1):
         n_iters = int(np.random.randint(1, n_iters + 1))
     if n_iters > 0:
         responses, LAFs, final_pyr_idxs, final_level_idxs, dets, A = self.ImproveLAFsEstimation(
             scale_pyr,
             responses,
             LAFs,
             final_pyr_idxs,
             final_level_idxs,
             self.num,
             n_iters=n_iters)
     if return_patches:
         patches = extract_patches(x, LAFs, PS=self.PS)
     else:
         patches = None
     return denormalizeLAFs(
         LAFs, x.size(3),
         x.size(2)), patches, responses, dets, A  #, scale_pyr
Exemple #7
0
def AffNetHardNet_describe(patches):
    descriptors = np.zeros(shape=[patches.shape[0], 128], dtype=np.float32)
    HessianAffine = []
    subpatches = torch.autograd.Variable(torch.zeros([len(patches), 1, 32, 32],
                                                     dtype=torch.float32),
                                         volatile=True).view(
                                             len(patches), 1, 32, 32)
    baseLAFs = torch.zeros([len(patches), 2, 3], dtype=torch.float32)
    for m in range(patches.shape[0]):
        patch_np = patches[m, :, :, 0].reshape(np.shape(patches)[1:3])
        HessianAffine.append(
            ScaleSpaceAffinePatchExtractor(mrSize=5.192,
                                           num_features=0,
                                           border=0,
                                           num_Baum_iters=0))
        with torch.no_grad():
            var_image = torch.autograd.Variable(torch.from_numpy(
                patch_np.astype(np.float32)),
                                                volatile=True)
            patch = var_image.view(1, 1, var_image.size(0), var_image.size(1))
        with torch.no_grad():
            HessianAffine[m].createScaleSpace(
                patch)  # to generate scale pyramids and stuff
        x, y = patch.size(3) / 2.0 + 2, patch.size(2) / 2.0 + 2
        LAFs = normalizeLAFs(
            torch.tensor([[AffNetPix.PS / 2, 0, x], [0, AffNetPix.PS / 2,
                                                     y]]).reshape(1, 2, 3),
            patch.size(3), patch.size(2))
        baseLAFs[m, :, :] = LAFs
        with torch.no_grad():
            subpatch = HessianAffine[m].extract_patches_from_pyr(
                denormalizeLAFs(LAFs, patch.size(3), patch.size(2)),
                PS=AffNetPix.PS)
            if WRITE_IMGS_DEBUG:
                SaveImageWithKeys(
                    subpatch.detach().cpu().numpy().reshape([32, 32]), [],
                    'p1/' + str(n) + '.png')
            # This subpatch has been blured by extract_patches _from_pyr...
            # let't us crop it manually to obtain fair results agains other methods
            subpatch = patch_np[16:48, 16:48].reshape(1, 1, 32, 32)
            #var_image = torch.autograd.Variable(torch.from_numpy(subpatch.astype(np.float32)), volatile = True)
            #subpatch = var_image.view(1, 1, 32,32)
            subpatches[m, :, :, :] = torch.from_numpy(
                subpatch.astype(np.float32))  #=subpatch
            if WRITE_IMGS_DEBUG:
                SaveImageWithKeys(
                    subpatch.detach().cpu().numpy().reshape([32, 32]), [],
                    'p2/' + str(n) + '.png')
    if USE_CUDA:
        # or ---> A = AffNetPix(subpatches.cuda()).cpu()
        with torch.no_grad():
            A = batched_forward(AffNetPix, subpatches.cuda(), 256).cpu()
    else:
        with torch.no_grad():
            A = AffNetPix(subpatches)
    LAFs = torch.cat([torch.bmm(A, baseLAFs[:, :, 0:2]), baseLAFs[:, :, 2:]],
                     dim=2)
    dLAFs = denormalizeLAFs(LAFs, patch.size(3), patch.size(2))
    Alist = convertLAFs_to_A23format(dLAFs.detach().cpu().numpy().astype(
        np.float32))
    for m in range(patches.shape[0]):
        with torch.no_grad():
            patchaff = HessianAffine[m].extract_patches_from_pyr(
                dLAFs[m, :, :].reshape(1, 2, 3), PS=32)
            if WRITE_IMGS_DEBUG:
                SaveImageWithKeys(
                    patchaff.detach().cpu().numpy().reshape([32, 32]), [],
                    'im1/' + str(n) + '.png')
                SaveImageWithKeys(patch_np, [], 'im2/' + str(n) + '.png')
            subpatches[m, :, :, :] = patchaff
    if USE_CUDA:
        with torch.no_grad():
            # descriptors = HardNetDescriptor(subpatches.cuda()).detach().cpu().numpy().astype(np.float32)
            descriptors = batched_forward(HardNetDescriptor, subpatches.cuda(),
                                          256).cpu().numpy().astype(np.float32)
    else:
        with torch.no_grad():
            descriptors = HardNetDescriptor(
                subpatches).detach().cpu().numpy().astype(np.float32)
    return descriptors, Alist
        abc = self.features(self.input_norm(input))
        return abc2A(abc[:, 0, :, :].contiguous() + 1.,
                     abc[:, 1, :, :].contiguous(),
                     abc[:, 2, :, :].contiguous() + 1.)


def weights_init(m):
    if isinstance(m, nn.Conv2d):
        nn.init.orthogonal(m.weight.data, gain=1.0)
        try:
            nn.init.constant(m.bias.data, 0.01)
        except:
            pass
    return


HA = ScaleSpaceAffinePatchExtractor(mrSize=5.0,
                                    num_features=3000,
                                    border=1,
                                    num_Baum_iters=5,
                                    AffNet=BaumNet())

LAFs, patches, resp, pyr = HA(var_image_reshape / 255.)
LAFs = denormalizeLAFs(LAFs, img.shape[1], img.shape[0],
                       use_cuda=USE_CUDA).data.cpu().numpy()

ells = LAFs2ell(LAFs)

np.savetxt(output_fname, ells, delimiter=' ', fmt='%10.10f')
#line_prepender(output_fname, str(len(ells)))
#line_prepender(output_fname, '1.0')