I’m getting this error when i’m trying to predict an image. Please do provide me a solution. Here is my code -
%%writefile app.py
import streamlit as st
from fastai.vision 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 = Image.open(uploaded_file)
st.image(image, caption='Uploaded Image', use_column_width=True)
st.write("")
if st.button('Predict'):
st.write("Result...")
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)
learn.load(path+'/polyp-resnet50-fine-tuning_ACC-0.975')
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?
Best,
Randy
This is the error that I got
AttributeError: 'JpegImageFile' object has no attribute 'apply_tfms'
Traceback:
File "/usr/local/lib/python3.7/dist-packages/streamlit/script_runner.py", line 333, in _run_script
exec(code, module.__dict__)
File "/content/app.py", line 48, in <module>
main()
File "/content/app.py", line 41, in main
pred_class, pred_idx, outputs = learn.predict(image)
File "/usr/local/lib/python3.7/dist-packages/fastai/basic_train.py", line 372, in predict
batch = self.data.one_item(item)
File "/usr/local/lib/python3.7/dist-packages/fastai/basic_data.py", 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/basic_data.py", line 169, in one_batch
try: x,y = next(iter(dl))
File "/usr/local/lib/python3.7/dist-packages/fastai/basic_data.py", 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/dataloader.py", line 517, in __next__
data = self._next_data()
File "/usr/local/lib/python3.7/dist-packages/torch/utils/data/dataloader.py", 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/fetch.py", 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/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.7/dist-packages/fastai/data_block.py", 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 = Image.open(uploaded_file)
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 = Image.open(uploaded_file)
x = pil2tensor(image, np.float32)
preds_num = learn.predict(Image(x))[2].numpy()
Hope that helps a little!
Fanilo
1 Like
I tried your code. Now i got this
TypeError: ‘module’ object is not callable
Traceback:
File "/usr/local/lib/python3.7/dist-packages/streamlit/script_runner.py", line 333, in _run_script
exec(code, module.__dict__)File "/content/app.py", line 42, in <module>
pred_class, pred_idx, outputs = learn.predict(Image(x))[2].numpy()