Getting the error while creating a ImageDataBunch for inference

Hi i am writing the following code and getting the error.

data = ImageDataBunch.single_from_classes(’’, classes, tfms=get_transforms(), size=196).normalize(imagenet_stats)
learner = create_cnn(data, models.resnet34)
learner.load(‘jeetkarsh.pth’)

AttributeError: type object ‘ImageDataBunch’ has no attribute 'single_from_classes’

2 Likes

You need to pass the same classes that your trained model has. What is the content of the classes object you are passing to that call?

I am passing the same classes in which the model was trained with in a list form.

I also had a problem loading the model for inference and it was related to the fast.ai library breaking in a later release. I had to downgrade to a previous release of the library so I could use inference. However, I retested yesterday with the latest fast.ai release and it worked again. I would suggest to upgrade the library to the latest one and retry.

I am using the latest library don’t know why its breaking. Can you help me out with code snippet that you use fro inferencing.

Have a look at this:
https://github.com/JoseMariaBernad/HandWrittenRecognition/blob/master/3-predict.ipynb

I am running 1.0.29 and its not working can you please check version at your end.

Thanks for all your help.

Version that i am using:

image

Error that i am getting

I am using fast.ai 1.0.19 but as I said, it should work with the latest version. Try instead:
empty_data = ImageDataBunch.single_from_classes(path, classes, tfms=get_transforms())

Its not working by making those changes as well.

It looks like they’ve removed single_from_classes from the ImageDataBunch class in the latest fastai library. I’m not sure what the correct way to do inference on a single image is now.

The official docs notebook still refers to this method.

Also, the zeit.co tarball also calls this method and is throwing errors now.

3 Likes

Yes, the single_from_classes has been removed from the library. You can find it in CHANGES.md.

New way to do it you can find in the doc:
https://docs.fast.ai/tutorial.inference.html

Thanks @dhoa - but from my review of the docs and the source code I can’t find any reference to load_empty in the ImageDataBunch class either unless I’m missing something?

It is in the data_block.py from the LabelList class not ImageDataBunch :smiley:

    @classmethod
    def load_empty(cls, fn:PathOrStr, tfms:TfmList=None, tfm_y:bool=False, **kwargs):
        "Load the sate in `fn` to create an empty `LabelList` for inference."
        state = pickle.load(open(fn, 'rb'))
        x = state['x_cls']([], path=state['path'], processor=state['x_proc'])
        y = state['y_cls']([], path=state['path'], processor=state['y_proc'])
        return cls(x, y, tfms=tfms, tfm_y=tfm_y, **kwargs).process()

Hope that helps,

1 Like

Thanks @dhoa for the helpful code snippet. What confused me was that the load_empty method wasn’t on any of the base classes, but tacked onto the DataBunch class in data_block.py

This new way to do inference assumes that you’re saving a previous DataBunch with the .export() method, correct? This means for an inference scenario in production we’ll need to save both the model and the export.pkl as well?

Is there a supported mechanism to do this without generating an additional file to be loaded at inference time?

Thanks so much for your help.

I followed some threads then know about there are some big changes in the library :smiley: I haven’t tried it yet. Maybe you can do some tests and repost here what you find ?

What does data.export() do? Does it save information to the path?

So I tried making some changes to serve.py in the Zeit tutorial to see if I can get my model to deploy on the most recent version of fastai (I was getting the same error without these changes). It did deploy but it appears to not actually return the prediction. I hit Analyze, the button switches to Analyzing... and that’s it. Here’s my code, and I’d be super grateful if someone wants to look through it and try and pinpoint why this might be happening. Look at lines 14, 15, 33, 34, 35 for the changes.

@dhoa Can you look at this code? It’s not working for me.

data = ImageDataBunch.load_empty(classes, path, tfms=get_transforms(), size=224).normalize(imagenet_stats)

Can you show an example of how it’s supposed to look like now? With the code above I get the error: “TypeError: expected str, bytes or os.PathLike object, not list”

instead of classes, use the path to where the export.pkl file was saved ie

#saves a export.pkl file of the databunch in the path you passed in to create the data object
data.export()

export_path = path/to/export.pkl

empty_data = ImageDataBunch.load_empty(export_path, tfms=get_transforms(), size=224).normalize(imagenet_stats)
3 Likes