HF space failing to build using fast.ai because of Resampling on module PIL.Image

I keep getting the below error when I try to access my model on hugging face spaces. I am building my model in a Kaggle notebook, then downloading to a pkl file to my spaces repo and git pushing to HF spaces. Below is my ImageDataLoaders class that I am using, as I suspect the error is coming from here.

dls = ImageDataLoaders.from_folder(path, valid_pct = 0.2, item_tfms=Resize(460), batch_tfms=aug_transforms(size=224, min_scale=0.75))

Here is the error I am getting.

Custom classes or functions exported with your `Learner` not available in namespace.\Re-declare/import before loading:
	Can't get attribute 'Resampling' on <module 'PIL.Image'

Here is my fill app.py code.

from fastai.vision.all import *
import gradio as gr
import skimage

learn = load_learner('new_model.pkl')

categories = ('deer', 'elk', 'moose')

def classify_image(img):
    img = PILImage.create(img)
    pred, idx, probs = learn.predict(img)
    return dict(zip(categories, map(float,probs)))

image = gr.inputs.Image(type='pil', shape=(192,192))
label = gr.outputs.Label()

intf = gr.Interface(fn=classify_image, inputs=image, outputs=label)
intf.launch(inline=False)
1 Like

tldr: these package versions fixed this error for me
!pip install Pillow==9.2.0
!pip install matplotlib==3.5.2

I had the same error and found a fix. The error was caused by having incompatible package versions in the deployment environment (specifically matplotlib, pillow).

I used pip freeze in the training environment (kaggle for you) to get the pack versions and installed them in the deployment env e.g. include a requirements.txt in the root directory of your spaces repo.