Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModuleNotFoundError: No module named 'fastai.vision'

I am trying to use ImageDataBunch from fastai, and it worked fine, but recently when I ran my code, it showed this error ModuleNotFoundError: No module named 'fastai.vision'

Then, I upgraded my fastai version pip install fastai --upgrade. This error got cleared but landed in NameError: name 'ImageDataBunch' is not defined

Here's my code:

import warnings
import numpy as np
from fastai.vision import *
warnings.filterwarnings("ignore", category=UserWarning, module="torch.nn.functional")
np.random.seed(42)
data = ImageDataBunch.from_folder(path, train='.', valid_pct=0.2, 
                                  ds_tfms=get_transforms(), size=224, num_workers=4, no_check=True).normalize(imagenet_stats)

How can I fix this?

like image 516
i_am_lak Avatar asked Sep 24 '20 04:09

i_am_lak


People also ask

Why is my fastai not installed in my environment?

ModuleNotFoundError: No module named ‘fastai.vision’ If you have multiple environments, it’s very possible that you installed fastai into one environment, but then are trying to use it from another, where it’s not installed.

Is it possible to install fastai on a Conda?

In general it is the best to create a new dedicated conda environment for fastai and not install anything there, other than fastai and its requirements, and keep it that way. If you do that, you will not have any such conflicts in the future.

How do I upgrade to the latest version of fastai?

you are probably using the older version of fastai. fastai now refers to fastai v2. You’ll need to upgrade your library Type pip install fastai --upgrade in the command line. You should be good to go! You’ll also need to install fastcore, if you haven’t done it before!

Can I run fastai code on Colab?

I already run fastai code on Colab and Colab supports fastai very well, thanks! Sorry, something went wrong. Can we do the installation of the specific library for inference in fastai.


2 Answers

I actually ran into this same issue when I started using Colab, but haven't been able to reproduce it. Here was the thread describing what I and another developer did to troubleshoot: https://forums.fast.ai/t/no-module-named-fastai-data-in-google-colab/78164/4

I would recommend trying to factory reset your runtime ( "Runtime" -> "Factory Reset Runtime")

Then you can check which version of fastai you have (you have to restart the runtime to use the new version if you've already imported it)

import fastai
fastai.__version__

I'm able to run fastai.vision import * on fastai version 1.0.61 and 2.0.13

like image 70
dnishiyama Avatar answered Oct 23 '22 03:10

dnishiyama


In Google Colab:

Upgrade fastai on colab:

! [ -e /content ] && pip install -Uqq fastai 

Import necessary libraries:

from fastai.vision.all import *
from fastai.text.all import *
from fastai.collab import *
from fastai.tabular.all import * 

Get the images and annotations:

path = untar_data(URLs.PETS)
path_anno = path/'annotations'
path_img = path/'images'
print( path_img.ls() )             # print all images
fnames = get_image_files(path_img) # -->> 7390 images
print(fnames[:5])                  # print first 5 images 
like image 25
Färid Alijani Avatar answered Oct 23 '22 03:10

Färid Alijani