BloodAxe / pytorch-toolbelt

PyTorch extensions for fast R&D prototyping and Kaggle farming
MIT License
1.51k stars 119 forks source link

Is dependency on `opencv-python` necessary? #71

Closed MikiGrit closed 2 years ago

MikiGrit commented 2 years ago

Depending on opencv-python makes it difficult to use the library in the docker environment since there is typically no gui. Would it be possible to depend on the opencv-python-headless instead?

Thanks.

BloodAxe commented 2 years ago

There is no strict dependency on opencv-python, it would work perfectly with opencv-python-headless and even with opencv-contrib-python[-headless]. Unfortunately picking a correct opencv distro even programmatically inside setup.py is tricky since other libraries may install other versions of opencv. (Seen this many times while maintaining albumentations package).

So as temporary solution you may install toolbelt w/o deps (pip install --no-deps pytorch-toolbelt==0.5). And if know how to do setup-time checks whether any of OpenCV variants is already installed - contributions are welcome!

MikiGrit commented 2 years ago

Thanks for the fast reply and the suggestion. Would something like this be sufficient?

def missing_opencv_requirements():
    """
    Since opencv library is distributed in several independent packages, we first check whether some of them is already installed and if not, the opencv-python-headless version will be in included.
    """
    try:
        import cv2
        return []
    except ImportError:
        return ["opencv-python-headless>=4.1"]
DEPENDENCIES = [
    # We rely on particular activation functions that were added in 1.8.1
    "torch>=1.8.1",
    # We use some pretrained models from torchvision
    "torchvision>=0.9.1",
    # Library uses scipy for linear_sum_assignment for match_bboxes.
    # 1.4.0 is the first release where `maximize` argument gets introduced to this function
    "scipy>=1.4.0",
] + missing_opencv_requirements()
BloodAxe commented 2 years ago

Could you please try this branch to see whether it works on your env: https://github.com/BloodAxe/pytorch-toolbelt/tree/feature/loose-opencv-install

MikiGrit commented 2 years ago

I've tested it and it sadly does not work for us. We are using poetry as a dependency manager and it unfortunately creates dependecy on opencv-python regardless [-headless] version is installed or not.

And the changes in setup.py are not used since poetry creates list of packages to be installed (in poetry.lock file) globally on developer machine.

We will have to solve it some other way, but thanks for you effort anyway 👍 .