Skip to content

Commit

Permalink
Make opencv an optional dependency in VISSL (facebookresearch#202)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebookresearch#202

Making opencv optional as it complicates the conda packaging for vissl. we use OpenCV in only 1 transform for colorization and ask user to install opencv following `pip install opencv-python` manually. When user installs VISSL from source, the instruction is already included as part of the instruction.

Reviewed By: bottler

Differential Revision: D26724383

fbshipit-source-id: c44968eeb76aa8cd61061b7f541a14d4314ea868
  • Loading branch information
prigoyal authored and facebook-github-bot committed Mar 1, 2021
1 parent ce785b7 commit 701172a
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ At a high level, project requires following system dependencies.
- PyTorch>=1.4
- torchvision (matching PyTorch install)
- CUDA (must be a version supported by the pytorch version)
- OpenCV
- OpenCV (optional)

## Installing VISSL from pre-built binaries

Expand Down Expand Up @@ -124,7 +124,7 @@ pip install classy-vision@https://github.com/facebookresearch/ClassyVision/tarba
# install vissl dev mode (e stands for editable)
pip install -e .[dev]
# verify installation
python -c 'import vissl, apex, cv2'
python -c 'import vissl, apex'
```

### Install from source in Conda environment
Expand Down
1 change: 0 additions & 1 deletion dev/packaging/vissl_conda/vissl/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ requirements:
- apex
- torchvision>=0.5
- tensorboard>=1.15
- opencv
- scipy
- scikit-learn
- parameterized
Expand Down
2 changes: 1 addition & 1 deletion dev/packaging/vissl_pip/test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ conda install -y -c pytorch pytorch=1.5.1 cudatoolkit=10.1 torchvision
pip install apex -f https://dl.fbaipublicfiles.com/vissl/packaging/apexwheels/py37_cu101_pyt151/download.html
#pip install vissl --no-index -f https://dl.fbaipublicfiles.com/vissl/packaging/visslwheels/download.html
pip install vissl
python -c "import vissl, apex, cv2"
python -c "import vissl, apex"
cd loc1
python -m unittest discover -v -s tests
dev/run_quick_tests.sh
2 changes: 1 addition & 1 deletion docs/source/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ At a high level, project requires following system dependencies.
- PyTorch>=1.4
- torchvision (matching PyTorch install)
- CUDA (must be a version supported by the pytorch version)
- OpenCV
- OpenCV (optional)

Installing VISSL from pre-built binaries
-------------------------------------------
Expand Down
8 changes: 7 additions & 1 deletion vissl/data/ssl_transforms/img_pil_to_lab_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

from typing import Any, Dict

import cv2
import numpy as np
import torch
from classy_vision.dataset.transforms import register_transform
from classy_vision.dataset.transforms.classy_transform import ClassyTransform
from vissl.utils.misc import is_opencv_available


@register_transform("ImgPil2LabTensor")
Expand Down Expand Up @@ -39,6 +39,12 @@ def __call__(self, image):
return img_lab_tensor

def _convertbgr2lab(self, img):
# opencv is not a hard dependency for VISSL so we do the import locally
assert (
is_opencv_available()
), "Please install OpenCV using: pip install opencv-python"
import cv2

# img is [0, 255] , HWC, BGR format, uint8 type
assert len(img.shape) == 3, "Image should have dim H x W x 3"
assert img.shape[2] == 3, "Image should have dim H x W x 3"
Expand Down
16 changes: 16 additions & 0 deletions vissl/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ def is_fairscale_sharded_available():
return fairscale_sharded_available


def is_opencv_available():
"""
Check if opencv is available with simple python imports.
To install opencv, simply do: `pip install opencv-python`
regardless of whether using conda or pip environment.
"""
try:
import cv2 # NOQA

opencv_available = True
except ImportError:
opencv_available = False
return opencv_available


def is_apex_available():
"""
Check if apex is available with simple python imports.
Expand Down

0 comments on commit 701172a

Please sign in to comment.