kornia / kornia

Geometric Computer Vision Library for Spatial AI
https://kornia.readthedocs.io
Apache License 2.0
9.83k stars 959 forks source link

[Bug] in LafOrienter #469

Closed connorlee77 closed 4 years ago

connorlee77 commented 4 years ago
#Now lets define local deature detector and descriptor

device = torch.device('cpu')
#device = torch.device('cuda:0')

PS = 41

sift = kornia.feature.SIFTDescriptor(PS, rootsift=True).to(device)
descriptor = sift

resp = BlobHessian()
scale_pyr = kornia.geometry.ScalePyramid(3, 1.6, PS, double_image=True)

nms = kornia.geometry.ConvQuadInterp3d(10)

n_features = 4000
detector = ScaleSpaceDetector(n_features,
                              resp_module=resp,
                              nms_module=nms,
                              scale_pyr_module=scale_pyr,
                              ori_module=kornia.feature.LAFOrienter(19),
                              mr_size=6.0).to(device)

with torch.no_grad():
    lafs, resps = detector(timg_gray)
    patches =  kornia.feature.extract_patches_from_pyramid(timg_gray, lafs, PS)
    B, N, CH, H, W = patches.size()
    # Descriptor accepts standard tensor [B, CH, H, W], while patches are [B, N, CH, H, W] shape
    # So we need to reshape a bit :) 
    descs = descriptor(patches.view(B * N, CH, H, W)).view(B, N, -1)
    matches, scores, dists = match_snn(descs[0], descs[1], 0.9)

# Now RANSAC
src_pts = lafs[0,matches[:,0], :, 2].data.cpu().numpy()
dst_pts = lafs[1,matches[:,1], :, 2].data.cpu().numpy()

H, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 1.0, 0.999, 10000)

inliers = matches[torch.from_numpy(mask).bool().squeeze(), :]

for i in range(2):
    visualize_LAF(timg_gray, lafs[:,inliers[:,i]], i)
print (len(inliers), 'inliers')

returns this error message

RuntimeError Traceback (most recent call last)

in 23 24 with torch.no_grad(): ---> 25 lafs, resps = detector(timg_gray) 26 patches = kornia.feature.extract_patches_from_pyramid(timg_gray, lafs, PS) 27 B, N, CH, H, W = patches.size() ~/anaconda3/envs/pt-gpu/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 548 result = self._slow_forward(*input, **kwargs) 549 else: --> 550 result = self.forward(*input, **kwargs) 551 for hook in self._forward_hooks.values(): 552 hook_result = hook(self, input, result) ~/anaconda3/envs/pt-gpu/lib/python3.7/site-packages/kornia/feature/scale_space_detector.py in forward(self, img, mask) 203 responses, lafs = self.detect(img, self.num_features, mask) 204 lafs = self.aff(lafs, img) --> 205 lafs = self.ori(lafs, img) 206 return lafs, responses ~/anaconda3/envs/pt-gpu/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 548 result = self._slow_forward(*input, **kwargs) 549 else: --> 550 result = self.forward(*input, **kwargs) 551 for hook in self._forward_hooks.values(): 552 hook_result = hook(self, input, result) ~/anaconda3/envs/pt-gpu/lib/python3.7/site-packages/kornia/feature/orientation.py in forward(self, laf, img) 149 self.patch_size, 150 self.patch_size) --> 151 angles_radians: torch.Tensor = self.angle_detector(patches).view(B, N) 152 rotmat: torch.Tensor = angle_to_rotation_matrix(rad2deg(angles_radians)).view(B * N, 2, 2) 153 ~/anaconda3/envs/pt-gpu/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs) 548 result = self._slow_forward(*input, **kwargs) 549 else: --> 550 result = self.forward(*input, **kwargs) 551 for hook in self._forward_hooks.values(): 552 hook_result = hook(self, input, result) ~/anaconda3/envs/pt-gpu/lib/python3.7/site-packages/kornia/feature/orientation.py in forward(self, patch) 96 ang_bins = torch.cat(ang_bins, 1).view(-1, 1, self.num_ang_bins) # type: ignore 97 ang_bins = self.angular_smooth(ang_bins) # type: ignore ---> 98 values, indices = ang_bins.view(-1, self.num_ang_bins).max(1) # type: ignore 99 angle = -((2. * pi * indices.to(patch.dtype) / float(self.num_ang_bins)) - pi) # type: ignore 100 return angle RuntimeError: shape '[-1, 36]' is invalid for input of size 304000
edgarriba commented 4 years ago

/cc @ducha-aiki

ducha-aiki commented 4 years ago

@connorlee77 which version of kornia and pytorch do you use? I cannot reproduce your error and the notebook https://github.com/kornia/kornia-examples/blob/master/image-matching-example.ipynb works just fine for me.

connorlee77 commented 4 years ago

I've been using the following:

kornia: 0.2.0 pytorch: 1.5.0.dev20200304 (the most recent nightly build)

connorlee77 commented 4 years ago

@ducha-aiki is this an easy fix? which versions are you using?

ducha-aiki commented 4 years ago

I cannot reproduce your error, unfortunately :( are you running the notebook from kornia-examples and not your own images? One thing I could think of is that you could forget to make images grayscale

connorlee77 commented 4 years ago

I'm running it right as is, right after I clone the repo. What versions of pytorch and kornia are you using?

Running on the example images.

connorlee77 commented 4 years ago

Hey, so the issue seems to be the padding in the angular_smooth function of PatchDominantGradientOrientation. Instead of self.angular_smooth = nn.Conv1d(1, 1, kernel_size=3, padding=2, bias=False, padding_mode="circular")

it should be

self.angular_smooth = nn.Conv1d(1, 1, kernel_size=3, padding=1, bias=False, padding_mode="circular")

ducha-aiki commented 4 years ago

You are right. It is working in pytorch 1.4.0, but not in nightly, as I have checked right now. Feel free to submit a PR with fix, or switch to 1.4.0

connorlee77 commented 4 years ago

Will do!

connorlee77 commented 4 years ago

PR here: https://github.com/kornia/kornia/pull/478