tfeat.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from typing import Dict
  18. import torch
  19. from torch import nn
  20. from kornia.core.check import KORNIA_CHECK_SHAPE
  21. urls: Dict[str, str] = {}
  22. urls["liberty"] = "https://github.com/vbalnt/tfeat/raw/master/pretrained-models/tfeat-liberty.params" # pylint: disable
  23. urls["notredame"] = "https://github.com/vbalnt/tfeat/raw/master/pretrained-models/tfeat-notredame.params" # pylint: disable
  24. urls["yosemite"] = "https://github.com/vbalnt/tfeat/raw/master/pretrained-models/tfeat-yosemite.params" # pylint: disable
  25. class TFeat(nn.Module):
  26. r"""Module, which computes TFeat descriptors of given grayscale patches of 32x32.
  27. This is based on the original code from paper "Learning local feature descriptors
  28. with triplets and shallow convolutional neural networks".
  29. See :cite:`TFeat2016` for more details
  30. Args:
  31. pretrained: Download and set pretrained weights to the model.
  32. Returns:
  33. torch.Tensor: TFeat descriptor of the patches.
  34. Shape:
  35. - Input: :math:`(B, 1, 32, 32)`
  36. - Output: :math:`(B, 128)`
  37. Examples:
  38. >>> input = torch.rand(16, 1, 32, 32)
  39. >>> tfeat = TFeat()
  40. >>> descs = tfeat(input) # 16x128
  41. """
  42. patch_size = 32
  43. def __init__(self, pretrained: bool = False) -> None:
  44. super().__init__()
  45. self.features = nn.Sequential(
  46. nn.InstanceNorm2d(1, affine=False),
  47. nn.Conv2d(1, 32, kernel_size=7),
  48. nn.Tanh(),
  49. nn.MaxPool2d(kernel_size=2, stride=2),
  50. nn.Conv2d(32, 64, kernel_size=6),
  51. nn.Tanh(),
  52. )
  53. self.descr = nn.Sequential(nn.Linear(64 * 8 * 8, 128), nn.Tanh())
  54. # use torch.hub to load pretrained model
  55. if pretrained:
  56. pretrained_dict = torch.hub.load_state_dict_from_url(urls["liberty"], map_location=torch.device("cpu"))
  57. self.load_state_dict(pretrained_dict, strict=True)
  58. self.eval()
  59. def forward(self, input: torch.Tensor) -> torch.Tensor:
  60. KORNIA_CHECK_SHAPE(input, ["B", "1", "32", "32"])
  61. x = self.features(input)
  62. x = x.view(x.size(0), -1)
  63. x = self.descr(x)
  64. return x