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 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())
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.
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
@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()
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?
I followed some threads then know about there are some big changes in the library I haven’t tried it yet. Maybe you can do some tests and repost here what you find ?
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)