AttributeError: 'JpegImageFile' object has no attribute 'apply_tfms'

I’m getting this error when i’m trying to predict an image. Please do provide me a solution. Here is my code -


import streamlit as st
from import *
from fastai.metrics import error_rate
import torchvision.transforms as T
from pathlib import Path
import pickle as pkl
import builtins 
from PIL import Image

uploaded_file = st.file_uploader("Choose an image file", type=["jpg", "png"])

if uploaded_file is not None:

        image =
        st.image(image, caption='Uploaded Image', use_column_width=True)

        if st.button('Predict'):
                classes= ['non_polyps', 'polyps']
                path = "/content/drive/MyDrive/CVC-ClinicDB/data_polyps"
                data = ImageDataBunch.single_from_classes(path, classes, size=299).normalize(imagenet_stats)
                learn = cnn_learner(data, models.resnet50)
                pred_class, pred_idx, outputs = learn.predict(image)
                st.write("Decision: ",pred_class)
                st.write("Accuracy: ",output*100)

Hi @Dhipikha, welcome to the Streamlit community!

Can you post the actual stack trace here with the error, so we can get a better idea of what line of code the app has a problem with?


This is the error that I got

AttributeError: 'JpegImageFile' object has no attribute 'apply_tfms'
File "/usr/local/lib/python3.7/dist-packages/streamlit/", line 333, in _run_script
    exec(code, module.__dict__)
File "/content/", line 48, in <module>
File "/content/", line 41, in main
    pred_class, pred_idx, outputs = learn.predict(image)
File "/usr/local/lib/python3.7/dist-packages/fastai/", line 372, in predict
    batch =
File "/usr/local/lib/python3.7/dist-packages/fastai/", line 182, in one_item
    return self.one_batch(ds_type=DatasetType.Single, detach=detach, denorm=denorm, cpu=cpu)
File "/usr/local/lib/python3.7/dist-packages/fastai/", line 169, in one_batch
    try:     x,y = next(iter(dl))
File "/usr/local/lib/python3.7/dist-packages/fastai/", line 75, in __iter__
    for b in self.dl: yield self.proc_batch(b)
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/", line 517, in __next__
    data = self._next_data()
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/", line 557, in _next_data
    data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/_utils/", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.7/dist-packages/fastai/", line 658, in __getitem__
    x = x.apply_tfms(self.tfms, **self.tfmargs)

Unfortunately, not sure what to suggest. This appears to be an issue with fastai, a package that I’ve never used.

Hey @Dhipikha,

Haven’t used fastai later but it does seem to come from the package.

Your image = is returning a PIL object and I’m going to assume learn.predict(image) takes a numpy array as argument, not a PIL object. From this answer I’m expecting you should convert your image to a tensor using the pil2tensor method in fastai? The following is not tested, just a guess…

image =
x = pil2tensor(image, np.float32)
preds_num = learn.predict(Image(x))[2].numpy()

Hope that helps a little!

1 Like

I tried your code. Now i got this

TypeError: ‘module’ object is not callable


File "/usr/local/lib/python3.7/dist-packages/streamlit/", line 333, in _run_script
    exec(code, module.__dict__)File "/content/", line 42, in <module>
    pred_class, pred_idx, outputs = learn.predict(Image(x))[2].numpy()