Problems at running ImageDataBunch in Deepnote

450 views Asked by At

I'm having trouble running this line of code in Deepnote, does anyone know why?

data = ImageDataBunch.from_folder(path, train="train", valid ="test",ds_tfms=get_transforms(), size=(256,256), bs=32, num_workers=4).normalize()

The error says:

NameError: name 'ImageDataBunch' is not defined

And previously, I have imported the Fastai library. So I don't get it!

1

There are 1 answers

0
Jakub Žitný On

The FastAI setup in Deepnote is not that straightforward. It's best to use a custom environment where you set stuff up in a Dockerfile and everything works afterwards in the notebook. I am not sure if the ImageDataBunch or whatever you're trying to do works the same way in FastAI v1 and v2, but here are the details for v1.

This is a Dockerfile which sets up the FastAI environment via conda:

# This is Dockerfile

FROM deepnote/python:3.9


RUN wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda.sh
RUN bash ~/miniconda.sh -b -p $HOME/miniconda
ENV PATH $HOME/miniconda/bin:$PATH
ENV PYTONPATH $HOME/miniconda

RUN $HOME/miniconda/bin/conda install python=3.9 ipykernel -y
RUN $HOME/miniconda/bin/conda install -c fastai -c pytorch fastai -y
RUN $HOME/miniconda/bin/python -m ipykernel install --user --name=conda
ENV DEFAULT_KERNEL_NAME "conda"

After that, you can test the fastai imports in the notebook:

import fastai
from fastai.vision import *

print(fastai.__version__)

ImageDataBunch

And if you download and unpack this sample MNIST dataset, you should be able to load the data like you suggested:

data = ImageDataBunch.from_folder(path, train="train", valid ="test",ds_tfms=get_transforms(), size=(256,256), bs=32, num_workers=4).normalize()

Feel free to check out or clone my Deepnote project to continue working on this.