Error in inference tutorial (fastai==1.0.36.post1)

Hi!

When following along the tabular data inference tutorial (https://docs.fast.ai/tutorial.inference.html) I found an error. I am using fastai==1.0.36.post1.

Code that generates the error:

from fastai import *
from fastai.tabular import *

adult = untar_data(URLs.ADULT_SAMPLE)
df = pd.read_csv(adult/'adult.csv')
dep_var = '>=50k'
cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race', 'sex', 'native-country']
cont_names = ['education-num', 'hours-per-week', 'age', 'capital-loss', 'fnlwgt', 'capital-gain']
procs = [FillMissing, Categorify, Normalize]

data = (TabularList.from_df(df, path=adult, cat_names=cat_names, cont_names=cont_names, procs=procs)
                           .split_by_idx(valid_idx=range(800,1000))
                           .label_from_df(cols=dep_var)
                           .databunch())

learn = tabular_learner(data, layers=[20,10], metrics=accuracy)
learn.fit(1, 1e-2)
learn.save('mini_train')

data = TabularDataBunch.load_empty(adult)
learn = tabular_learner(data, layers=[20,10])
learn.load('mini_train');

I get the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-664f9a90ba78> in <module>
     18 learn.save('mini_train')
     19 
---> 20 data = TabularDataBunch.load_empty(adult)
     21 learn = tabular_learner(data, layers=[20,10])
     22 learn.load('mini_train');

/private/tmp/fastai-inference/venv/lib/python3.7/site-packages/fastai/data_block.py in _databunch_load_empty(cls, path, fname, tfms, tfm_y, **kwargs)
    548 def _databunch_load_empty(cls, path, fname:str='export.pkl', tfms:TfmList=None, tfm_y:bool=False, **kwargs):
    549     "Load an empty `DataBunch` from the exported file in `path/fname` with optional `tfms`."
--> 550     sd = LabelLists.load_empty(path/fname, tfms=tfms, tfm_y=tfm_y, **kwargs)
    551     return sd.databunch()
    552 

/private/tmp/fastai-inference/venv/lib/python3.7/site-packages/fastai/data_block.py in load_empty(cls, fn, tfms, tfm_y, **kwargs)
    446     @classmethod
    447     def load_empty(cls, fn:PathOrStr, tfms:TfmList=None, tfm_y:bool=False, **kwargs):
--> 448         train_ds = LabelList.load_empty(fn, tfms=tfms[0], tfm_y=tfm_y, **kwargs)
    449         valid_ds = LabelList.load_empty(fn, tfms=tfms[1], tfm_y=tfm_y, **kwargs)
    450         return LabelLists(valid_ds.path, train=train_ds, valid=valid_ds)

TypeError: 'NoneType' object is not subscriptable

The tutorial runs with master, that’s why you have an error.

Ah, I see. Thanks! Keep up the good work!

I am trying to do something similar to this on Google Cloud (fastai.version = ‘1.0.36’). Do we need to update the fastai version to get loading trained model/inference to work? Or is there another approach that will work?

Can you explain what this means?

master is the fastai repo. You need a dev install until we make a new release.

hey.
I followed the following steps for dev install.

git clone https://github.com/fastai/fastai
cd fastai
tools/run-after-git-clone
pip install -e “.[dev]”

But still the error persists

It looks like when we export the model, the classes are not saved in the model state.

This is how I managed to get around it (using the current conda installation):

  1. Serialize your classes someway, in my case I just used a list classlist = ['cat1', 'cat2', 'cat3']
  2. after the learn = load_learner part of the tutorial, I add learn.single_ds.y.classes = classlist

This solves the problem for me. I am not sure whether the problem is in the learner.export method or in the load_learner function

1 Like

I have this similar problem with 1.0.58.dev0 when trying to run load_empty().

@sgugger can you check this? Why I’m still getting the error?
I have 1.0.58.dev0

from fastai.collab import collab_learner, CollabDataBunch
data = CollabDataBunch.load_empty('path')

Which tutorial are you talking about? There is no data = CollabDataBunch.load_empty('path') in the inference tutorial.

It’s not tutorial but I wanted to later load the data. But I figured it out by just using pickle. Thanks