Lightning-Universe / lightning-flash

Your PyTorch AI Factory - Flash enables you to easily configure and run complex AI recipes for over 15 tasks across 7 data domains
https://lightning-flash.readthedocs.io
Apache License 2.0
1.74k stars 212 forks source link

ModuleNotFoundError with lightning-flash[image] and ImageEmbedder #1532

Open lizziesilver opened 1 year ago

lizziesilver commented 1 year ago

🐛 Bug

After running pip install 'lightning-flash[image]', when I try to create an ImageEmbedder, I get:

ModuleNotFoundError: Required dependencies not available. Please run: pip install 'lightning-flash[image]'

Background:

To Reproduce

1. Create this Dockerfile:

FROM jupyter/base-notebook:python-3.9

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

USER root

RUN pip install 'lightning-flash[image]'

EXPOSE 8888

2. Build the container and save the build logs

docker build --no-cache --progress=plain -t flash:latest . &> build.log

Build log file is attached, looks fine to me. build.log

3. Run the container and create a notebook

I run as root, and mount the notebooks folder to save my work: docker run -it -v $(PWD)/notebooks:/home/jovyan/work --user root -e GRANT_SUDO=yes -p 8888:8888 flash:latest

Navigate to http://127.0.0.1:8888/lab/tree/work/ in the browser and enter the token. Create a notebook.

4. Run the following code from the embeddings tutorial:

import torch
from torchvision.datasets import CIFAR10

import flash
from flash.core.data.utils import download_data
from flash.image import ImageClassificationData, ImageEmbedder

# # 1. Download the data and prepare the datamodule
# datamodule = ImageClassificationData.from_datasets(
#     train_dataset=CIFAR10("./cifar10", download=True),
#     batch_size=8,
# )

# 2. Build the task
embedder = ImageEmbedder(
    backbone="resnet18",
    training_strategy="barlow_twins",
    head="barlow_twins_head",
    pretraining_transform="barlow_twins_transform",
    training_strategy_kwargs={"latent_embedding_dim": 128},
    pretraining_transform_kwargs={"size_crops": [32]},
)

This produces the following error:

/opt/conda/lib/python3.9/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm
/opt/conda/lib/python3.9/site-packages/torchvision/io/image.py:13: UserWarning: Failed to load image Python extension: 
  warn(f"Failed to load image Python extension: {e}")
/opt/conda/lib/python3.9/site-packages/torchvision/models/_utils.py:252: UserWarning: Accessing the model URLs via the internal dictionary of the module is deprecated since 0.13 and may be removed in the future. Please access them via the appropriate Weights Enum instead.
  warnings.warn(
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In [3], line 16
      6 from flash.image import ImageClassificationData, ImageEmbedder
      8 # # 1. Download the data and prepare the datamodule
      9 # datamodule = ImageClassificationData.from_datasets(
     10 #     train_dataset=CIFAR10("./cifar10", download=True),
   (...)
     14 
     15 # 2. Build the task
---> 16 embedder = ImageEmbedder(
     17     backbone="resnet18",
     18     training_strategy="barlow_twins",
     19     head="barlow_twins_head",
     20     pretraining_transform="barlow_twins_transform",
     21     training_strategy_kwargs={"latent_embedding_dim": 128},
     22     pretraining_transform_kwargs={"size_crops": [32]},
     23 )

File /opt/conda/lib/python3.9/site-packages/flash/core/utilities/imports.py:164, in requires.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
    162 @functools.wraps(func)
    163 def wrapper(*args, **kwargs):
--> 164     raise ModuleNotFoundError(
    165         f"Required dependencies not available. Please run: pip install {' '.join(modules)}"
    166     )

ModuleNotFoundError: Required dependencies not available. Please run: pip install 'lightning-flash[image]'

Expected behavior

After installing lightning-flash[image] I should not get an error message telling me to install lightning-flash[image]. I should be able to instantiate an ImageEmbedder.

Environment

Rusteam commented 1 year ago

check this file flash/core/utilities/imports.py:

_IMAGE_AVAILABLE = all(
    [
        _TORCHVISION_AVAILABLE,
        _TIMM_AVAILABLE,
        _PIL_AVAILABLE,
        _ALBUMENTATIONS_AVAILABLE,
        _PYSTICHE_AVAILABLE,
        _SEGMENTATION_MODELS_AVAILABLE,
    ]
)

If any of the above is False, then it throws this error. In my case _PYSTICHE_AVAILABLE was False although it was installed, it couldn't be imported because the version was wrong. Installing a correct version from requirements fixed the issue

lizziesilver commented 1 year ago

Thank you @Rusteam ! It turned out that albumentations was not available because cv2 was not installed. Running pip install opencv-python before installing flash solved that problem.

Unfortunately I still can't import flash because of another issue, but it should be a one-line change. I will fork the repo and see if I can resolve it :)

davidblom603 commented 1 year ago

Also have this problem

davidblom603 commented 1 year ago

It turns out that pystiche requires torch 1.12 and torchvision 0.13. Perhaps consider using poetry to manage the dependencies? It also has a pre-commit hook to export a requirements.txt file on every commit.

Borda commented 1 year ago

It turns out that pystiche requires torch 1.12 and torchvision 0.13. Perhaps consider using poetry to manage the dependencies? It also has a pre-commit hook to export a requirements.txt file on every commit.

I am not sure if I understand what you mean... can you elaborate? in general, have aligned PyTorch and TorchVision versions is quite challenging

davidblom603 commented 1 year ago

thanks for your reply! I guess what would be helpful is when the dependencies are pinned to specific versions.

poetry is a tool which can help with that. https://python-poetry.org/

Borda commented 1 year ago

I guess what would be helpful is when the dependencies are pinned to specific versions.

That would work for end applications or server app, but for frameworks like this one it would restrict what other packages can be used with...

Borda commented 1 year ago

ok, I can reproduce this same issue also with master as well as 0.8.2