Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor' #768

Open
rahulmahmud opened this issue Mar 17, 2024 · 10 comments

Comments

@rahulmahmud
Copy link

Please solve step by step
Traceback (most recent call last):
File "/content/Real-ESRGAN/inference_realesrgan_video.py", line 10, in
from basicsr.archs.rrdbnet_arch import RRDBNet
File "/usr/local/lib/python3.10/dist-packages/basicsr/init.py", line 4, in
from .data import *
File "/usr/local/lib/python3.10/dist-packages/basicsr/data/init.py", line 22, in
_dataset_modules = [importlib.import_module(f'basicsr.data.{file_name}') for file_name in dataset_filenames]
File "/usr/local/lib/python3.10/dist-packages/basicsr/data/init.py", line 22, in
_dataset_modules = [importlib.import_module(f'basicsr.data.{file_name}') for file_name in dataset_filenames]
File "/usr/lib/python3.10/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/usr/local/lib/python3.10/dist-packages/basicsr/data/realesrgan_dataset.py", line 11, in
from basicsr.data.degradations import circular_lowpass_kernel, random_mixed_kernels
File "/usr/local/lib/python3.10/dist-packages/basicsr/data/degradations.py", line 8, in
from torchvision.transforms.functional_tensor import rgb_to_grayscale
ModuleNotFoundError: No module named 'torchvision.transforms.functional_tensor'

@jundao17
Copy link

The torchvision.transforms.functional_tensor module is deprecated in 0.15 and will be removed in 0.17. Please don't rely on it. You probably just need to use APIs in torchvision.transforms.functional or in torchvision.transforms.v2.functional.

@JossCamp
Copy link

JossCamp commented Mar 20, 2024

you need to install a specific version of pytorch

python -m pip install torch==2.0.1 torchvision==0.15.2 --extra-index-url https://download.pytorch.org/whl/cu118

After that, it should work correctly.

@cncloudli
Copy link

This might be caused by the modification of this module name. I opened the same issue in BasicSR project. In some specific versions of pytorch, for this module name there is a "_" in front of "functional". That‘s why changing the version of pytorch could solve this issue.

@KanTakahiro
Copy link

Just because BasicSR doesn't get ready for torchvision >= 0.17
I found this and edited the basicsr/data/degradations.py file, then the issue solved.

@m4ra7h0n
Copy link

m4ra7h0n commented Apr 4, 2024

you need to install a specific version of pytorch

python -m pip install torch==2.0.1 torchvision==0.15.2 --extra-index-url https://download.pytorch.org/whl/cu118

After that, it should work correctly.

ERROR: Could not find a version that satisfies the requirement torch==2.0.1 (from versions: 2.2.0, 2.2.1, 2.2.2)
ERROR: No matching distribution found for torch==2.0.1

@DongKat
Copy link

DongKat commented Apr 5, 2024

@m4ra7h0n you should try older version of python.

@PhielAi
Copy link

PhielAi commented Apr 9, 2024

C:\Users\ user-name \anaconda3\envs\ envs-name \Lib\site-packages\basicsr\data\degradations.py

line 8:
from torchvision.transforms.functional_tensor import rgb_to_grayscale
to:
from torchvision.transforms._functional_tensor import rgb_to_grayscale

@Anyeos
Copy link

Anyeos commented Apr 20, 2024

It need to be updated because the AI world is continiously evolving and there are always new versions and solutions for everything. Now xformers requires torch==2.2.2 and it cannot be installed with torch==2.0.1
So the outdated implied library is the real problem.

@moustaphachadly
Copy link

C:\Users\ user-name \anaconda3\envs\ envs-name \Lib\site-packages\basicsr\data\degradations.py

line 8: from torchvision.transforms.functional_tensor import rgb_to_grayscale to: from torchvision.transforms._functional_tensor import rgb_to_grayscale

This worked for me, but with miniconda: /Users/[user-name]/miniconda3/lib/python3.12/site-packages/basicsr/data/degradations.py.

@wladradchenko
Copy link

Hi! Modifying the library code, as suggested in the answer by @PhielAi on Apr 9, is not ideal, and locking to specific versions can also introduce limitations. Instead, you can handle the missing function without changing the library code directly.

Here’s an approach that dynamically adds the rgb_to_grayscale function into the torchvision.transforms.functional_tensor module:

import sys
import types
from torchvision.transforms.functional import rgb_to_grayscale

# Create a module for `torchvision.transforms.functional_tensor`
functional_tensor = types.ModuleType("torchvision.transforms.functional_tensor")
functional_tensor.rgb_to_grayscale = rgb_to_grayscale

# Add this module to sys.modules so other imports can access it
sys.modules["torchvision.transforms.functional_tensor"] = functional_tensor

This approach makes the missing function available where it’s expected, without requiring any modifications to the library code itself. It's especially useful for introducing back functions that may have been removed in recent library updates, providing a clean and maintainable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests