Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

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

Open
lizziesilver opened this issue Mar 10, 2023 · 8 comments
Open
Labels
bug / fix Something isn't working help wanted Extra attention is needed

Comments

@lizziesilver
Copy link

馃悰 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:

  • I am trying to create an image embedding model.
  • I want to follow the embeddings tutorial to get started.
  • I want to run the tutorial interactively in a notebook so I can play with it easily.
  • I have installed lightning-flash[image] into a docker container built on the jupyter base-notebook container, to minimise what else is installed.
  • Using python 3.9 because I see lightning-flash is tagged as supporting python 3.7, 3.8, 3.9.

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

  • OS (e.g., Linux): linux/arm64/v8
  • Python version: 3.9.13
  • PyTorch/Lightning/Flash Version (e.g., 1.10/1.5/0.7):
    • Torch: 1.13.1
    • Flash: 0.8.1.post0
  • GPU models and configuration: N/A (cpu)
  • Any other relevant information: see above
@lizziesilver lizziesilver added bug / fix Something isn't working help wanted Extra attention is needed labels Mar 10, 2023
@Rusteam
Copy link

Rusteam commented Mar 19, 2023

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
Copy link
Author

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
Copy link

Also have this problem

@davidblom603
Copy link

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
Copy link
Member

Borda commented May 11, 2023

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
Copy link

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
Copy link
Member

Borda commented May 18, 2023

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
Copy link
Member

Borda commented Aug 9, 2023

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug / fix Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

4 participants