Skip to content

Commit

Permalink
Make faiss an optional dependency of VISSL and switch to faiss-gpu (f…
Browse files Browse the repository at this point in the history
…acebookresearch#203)

Summary:
Pull Request resolved: facebookresearch#203

- making faiss an optional dependency. faiss is used only in 1 place in VISSL for clusterfit.
- When user uses clusterfit, we check if faiss is installed or not. we give instructions to user at runtime to how to install faiss if not already done
- we shift to using faiss-gpu since that's official dependency to use for faiss

Reviewed By: bottler

Differential Revision: D26725011

fbshipit-source-id: eac86595da9c998230aa74a4a00c7a894b6200f7
  • Loading branch information
prigoyal authored and facebook-github-bot committed Mar 1, 2021
1 parent 701172a commit 44bb572
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dev/packaging/vissl_conda/vissl/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ requirements:
- scikit-learn
- parameterized
- numpy >=1.11
- faiss
- faiss-gpu
- fvcore
- iopath
- importlib_resources
Expand Down
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mock
opencv-python
tensorboard==1.15.0
hydra-core>=1.0
faiss>=1.5.3
faiss-gpu
cython
scikit-learn
parameterized==0.7.4
Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
tensorboard==1.15.0
hydra-core>=1.0
faiss>=1.5.3
cython
scikit-learn
parameterized==0.7.4
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def get_version():
"pre-commit",
"nbconvert",
"bs4",
"faiss-gpu",
]
},
)
10 changes: 8 additions & 2 deletions tools/cluster_features_and_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from argparse import Namespace
from typing import Any, List

import faiss
import numpy as np
from hydra.experimental import compose, initialize_config_module
from vissl.data import build_dataset
Expand All @@ -21,7 +20,7 @@
from vissl.utils.hydra_config import AttrDict, convert_to_attrdict, is_hydra_available
from vissl.utils.io import save_file
from vissl.utils.logger import setup_logging, shutdown_logging
from vissl.utils.misc import merge_features, set_seeds
from vissl.utils.misc import merge_features, set_seeds, is_faiss_available


def get_data_features_and_images(cfg: AttrDict):
Expand All @@ -45,6 +44,13 @@ def get_data_features_and_images(cfg: AttrDict):


def cluster_features_and_label(args: Namespace, cfg: AttrDict):
# faiss is an optional dependency for VISSL.
assert is_faiss_available(), (
"Please install faiss using conda install faiss-gpu -c pytorch "
"if using conda or pip install faiss-gpu"
)
import faiss

cluster_backend = cfg.CLUSTERFIT.CLUSTER_BACKEND
num_clusters = cfg.CLUSTERFIT.NUM_CLUSTERS
data_split = cfg.CLUSTERFIT.FEATURES.DATA_PARTITION
Expand Down
1 change: 1 addition & 0 deletions vissl/data/datasets/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ def get_valid_objs(entry, objs):


def get_coco_imgs_labels_info(split, data_source_dir, args):
# pycocotools is an optional dependency for VISSL
from pycocotools.coco import COCO

json_file = f"{data_source_dir}/annotations/instances_{split}2014.json"
Expand Down
17 changes: 17 additions & 0 deletions vissl/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ def is_fairscale_sharded_available():
return fairscale_sharded_available


def is_faiss_available():
"""
Check if faiss is available with simple python imports.
To install faiss, simply do:
If using PIP env: `pip install faiss-gpu`
If using conda env: `conda install faiss-gpu -c pytorch`
"""
try:
import faiss # NOQA

faiss_available = True
except ImportError:
faiss_available = False
return faiss_available


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

0 comments on commit 44bb572

Please sign in to comment.