Skip to content

Commit

Permalink
Fix ModuleNotFoundError: No module named 'skimage' when importing tor…
Browse files Browse the repository at this point in the history
…cheval (#205)

Summary:
Prevent ModuleNotFoundError when skimage is not installed

Pull Request resolved: #205

Test Plan:
None

Fixes #204

Reviewed By: diego-urgell

Differential Revision: D62450136

Pulled By: bobakfb

fbshipit-source-id: cdc4507019d000f2352f7cd5ba25d71d12ab749c
  • Loading branch information
fleonce authored and facebook-github-bot committed Sep 11, 2024
1 parent de4d85e commit e972bf3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions image-requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
torchvision
skimage
16 changes: 15 additions & 1 deletion torcheval/metrics/image/ssim.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,32 @@
# pyre-ignore-all-errors[16]: Undefined attribute of metric states.

import warnings
from importlib.util import find_spec
from typing import Iterable, Optional, TypeVar

import torch

from skimage.metrics import structural_similarity
if find_spec("skimage") is not None:
from skimage.metrics import structural_similarity

_SKIMAGE_AVAILABLE = True
else:
_SKIMAGE_AVAILABLE = False


from torcheval.metrics.metric import Metric


TStructuralSimilarity = TypeVar("TStructuralSimilarity")


def _validate_torchvision_available() -> None:
if not _SKIMAGE_AVAILABLE:
raise RuntimeError(
"You must have skimage installed to use SSIM, please install torcheval[image]"
)


class StructuralSimilarity(Metric[torch.Tensor]):
"""
Compute the structural similarity index (SSIM) between two sets of images.
Expand Down

0 comments on commit e972bf3

Please sign in to comment.