edge_detection.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 kornia.core import Module, Tensor
  18. from kornia.core.check import KORNIA_CHECK_SHAPE
  19. from kornia.filters.dexined import DexiNed
  20. class EdgeDetector(Module):
  21. r"""Detect edges in a given image using a CNN.
  22. By default, it uses the method described in :cite:`xsoria2020dexined`.
  23. Return:
  24. A tensor of shape :math:`(B,1,H,W)`.
  25. Example:
  26. >>> img = torch.rand(1, 3, 320, 320)
  27. >>> detect = EdgeDetector()
  28. >>> out = detect(img)
  29. >>> out.shape
  30. torch.Size([1, 1, 320, 320])
  31. """
  32. def __init__(self) -> None:
  33. super().__init__()
  34. self.model = DexiNed(pretrained=True)
  35. def load(self, path_file: str) -> None:
  36. self.model.load_from_file(path_file)
  37. def preprocess(self, image: Tensor) -> Tensor:
  38. return image
  39. def postprocess(self, data: Tensor) -> Tensor:
  40. return data
  41. def forward(self, image: Tensor) -> Tensor:
  42. KORNIA_CHECK_SHAPE(image, ["B", "3", "H", "W"])
  43. img = self.preprocess(image)
  44. out = self.model(img)
  45. return self.postprocess(out)