Inference Learner

Intermediate tutorial, explains how to create a Learner for inference

Create a Learner for inference

In this tutorial, we’ll see how the same API allows you to create an empty DataBunch for a Learner at inference time (once you have trained your model) and how to call the predict method to get the predictions on a single item.

Note: As usual, this page is generated from a notebook that you can find in the docs_src folder of the fastai repo. We use the saved models from this tutorial to have this notebook run quickly.

Vision

To quickly get acces to all the vision functionality inside fastai, we use the usual import statements.

  1. from fastai.vision import *

A classification problem

Let’s begin with our sample of the MNIST dataset.

  1. mnist = untar_data(URLs.MNIST_TINY)
  2. tfms = get_transforms(do_flip=False)

It’s set up with an imagenet structure so we use it to split our training and validation set, then labelling.

  1. data = (ImageList.from_folder(mnist)
  2. .split_by_folder()
  3. .label_from_folder()
  4. .add_test_folder('test')
  5. .transform(tfms, size=32)
  6. .databunch()
  7. .normalize(imagenet_stats))

Now that our data has been properly set up, we can train a model. We already did in the look at your data tutorial so we’ll just load our saved results here.

  1. learn = cnn_learner(data, models.resnet18).load('mini_train')

Once everything is ready for inference, we just have to call learn.export to save all the information of our Learner object for inference: the stuff we need in the DataBunch (transforms, classes, normalization…), the model with its weights and all the callbacks our Learner was using. Everything will be in a file named export.pkl in the folder learn.path. If you deploy your model on a different machine, this is the file you’ll need to copy.

  1. learn.export()

To create the Learner for inference, you’ll need to use the load_learner function. Note that you don’t have to specify anything: it remembers the classes, the transforms you used or the normalization in the data, the model, its weigths… The only argument needed is the folder where the ‘export.pkl’ file is.

  1. learn = load_learner(mnist)

You can now get the predictions on any image via learn.predict.

  1. img = data.train_ds[0][0]
  2. learn.predict(img)
  1. (Category 3, tensor(0), tensor([0.5275, 0.4725]))

It returns a tuple of three things: the object predicted (with the class in this instance), the underlying data (here the corresponding index) and the raw probabilities. You can also do inference on a larger set of data by adding a test set. This is done by passing an ItemList to load_learner.

  1. learn = load_learner(mnist, test=ImageList.from_folder(mnist/'test'))
  1. preds,y = learn.get_preds(ds_type=DatasetType.Test)
  2. preds[:5]
  1. tensor([[0.9866, 0.0134],
  2. [0.0019, 0.9981],
  3. [0.9721, 0.0279],
  4. [0.0067, 0.9933],
  5. [0.9966, 0.0034]])

A multi-label problem

Now let’s try these on the planet dataset, which is a little bit different in the sense that each image can have multiple tags (and not just one label).

  1. planet = untar_data(URLs.PLANET_TINY)
  2. planet_tfms = get_transforms(flip_vert=True, max_lighting=0.1, max_zoom=1.05, max_warp=0.)

Here each images is labelled in a file named labels.csv. We have to add train as a prefix to the filenames, .jpg as a suffix and indicate that the labels are separated by spaces.

  1. data = (ImageList.from_csv(planet, 'labels.csv', folder='train', suffix='.jpg')
  2. .split_by_rand_pct()
  3. .label_from_df(label_delim=' ')
  4. .transform(planet_tfms, size=128)
  5. .databunch()
  6. .normalize(imagenet_stats))

Again, we load the model we saved in look at your data tutorial.

  1. learn = cnn_learner(data, models.resnet18).load('mini_train')

Then we can export it before loading it for inference.

  1. learn.export()
  1. learn = load_learner(planet)

And we get the predictions on any image via learn.predict.

  1. img = data.train_ds[0][0]
  2. learn.predict(img)
  1. (MultiCategory blooming;clear;habitation;haze;primary,
  2. tensor([0., 0., 0., 1., 1., 0., 0., 1., 1., 0., 1., 0., 0., 0.]),
  3. tensor([0.1906, 0.3646, 0.3146, 0.6931, 0.6352, 0.3982, 0.3114, 0.5396, 0.5245,
  4. 0.3933, 0.6069, 0.1630, 0.4260, 0.2641]))

Here we can specify a particular threshold to consider the predictions to be correct or not. The default is 0.5, but we can change it.

  1. learn.predict(img, thresh=0.3)
  1. (MultiCategory artisinal_mine;bare_ground;blooming;clear;cloudy;cultivation;habitation;haze;partly_cloudy;primary;selective_logging,
  2. tensor([0., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 0., 1., 0.]),
  3. tensor([0.1906, 0.3646, 0.3146, 0.6931, 0.6352, 0.3982, 0.3114, 0.5396, 0.5245,
  4. 0.3933, 0.6069, 0.1630, 0.4260, 0.2641]))

A regression example

For the next example, we are going to use the BIWI head pose dataset. On pictures of persons, we have to find the center of their face. For the fastai docs, we have built a small subsample of the dataset (200 images) and prepared a dictionary for the correspondance filename to center.

  1. biwi = untar_data(URLs.BIWI_SAMPLE)
  2. fn2ctr = pickle.load(open(biwi/'centers.pkl', 'rb'))

To grab our data, we use this dictionary to label our items. We also use the PointsItemList class to have the targets be of type ImagePoints (which will make sure the data augmentation is properly applied to them). When calling transform we make sure to set tfm_y=True.

  1. data = (PointsItemList.from_folder(biwi)
  2. .split_by_rand_pct(seed=42)
  3. .label_from_func(lambda o:fn2ctr[o.name])
  4. .transform(get_transforms(), tfm_y=True, size=(120,160))
  5. .databunch()
  6. .normalize(imagenet_stats))

As before, the road to inference is pretty straightforward: load the model we trained before, export the Learner then load it for production.

  1. learn = cnn_learner(data, models.resnet18, lin_ftrs=[100], ps=0.05).load('mini_train');
  1. learn.export()
  1. learn = load_learner(biwi)

And now we can a prediction on an image.

  1. img = data.valid_ds[0][0]
  2. learn.predict(img)
  1. (ImagePoints (120, 160),
  2. tensor([[ 2.2184, -0.3693]]),
  3. tensor([ 2.2184, -0.3693]))

To visualize the predictions, we can use the Image.show method.

  1. img.show(y=learn.predict(img)[0])

Inference Learner - 图1

A segmentation example

Now we are going to look at the camvid dataset (at least a small sample of it), where we have to predict the class of each pixel in an image. Each image in the ‘images’ subfolder has an equivalent in ‘labels’ that is its segmentations mask.

  1. camvid = untar_data(URLs.CAMVID_TINY)
  2. path_lbl = camvid/'labels'
  3. path_img = camvid/'images'

We read the classes in ‘codes.txt’ and the function maps each image filename with its corresponding mask filename.

  1. codes = np.loadtxt(camvid/'codes.txt', dtype=str)
  2. get_y_fn = lambda x: path_lbl/f'{x.stem}_P{x.suffix}'

The data block API allows us to quickly get everything in a DataBunch and then we can have a look with show_batch.

  1. data = (SegmentationItemList.from_folder(path_img)
  2. .split_by_rand_pct()
  3. .label_from_func(get_y_fn, classes=codes)
  4. .transform(get_transforms(), tfm_y=True, size=128)
  5. .databunch(bs=16, path=camvid)
  6. .normalize(imagenet_stats))

As before, we load our model, export the Learner then create a new one with load_learner.

  1. learn = unet_learner(data, models.resnet18).load('mini_train');
  2. learn.export()
  1. learn = load_learner(camvid)

And now we can a prediction on an image.

  1. img = data.train_ds[0][0]
  2. learn.predict(img);

To visualize the predictions, we can use the Image.show method.

  1. img.show(y=learn.predict(img)[0])

Inference Learner - 图2

Text

Next application is text, so let’s start by importing everything we’ll need.

  1. from fastai.text import *

Language modelling

First let’s look a how to get a language model ready for inference. Since we’ll load the model trained in the visualize data tutorial, we load the DataBunch used there.

  1. imdb = untar_data(URLs.IMDB_SAMPLE)
  1. data_lm = load_data(imdb)

Like in vision, we just have to type learn.export() after loading our pretrained model to save all the information inside the Learner we’ll need. In this case, this includes all the vocabulary we created. The only difference is that we will specify a filename, since we have several model in the same path (language model and classifier).

  1. learn = language_model_learner(data_lm, AWD_LSTM, pretrained=False).load('mini_train_lm', with_opt=False);
  1. learn.export(file = 'export_lm.pkl')

Now let’s define our inference learner.

  1. learn = load_learner(imdb, file = 'export_lm.pkl')

Then we can predict with the usual method, here we can specify how many words we want the model to predict.

  1. learn.predict('This is a simple test of', n_words=20)
  1. 'This is a simple test of the critique made out of the concerns on the consequences of it and the called Sub Cooper comparisons'

You can also use beam search to generate text.

  1. learn.beam_search('This is a simple test of', n_words=20, beam_sz=200)
  1. 'This is a simple test of This is a simple test of the quality of the English version of the English version . n n The English version is'

Classification

Now let’s see a classification example. We have to use the same vocabulary as for the language model if we want to be able to use the encoder we saved.

  1. data_clas = (TextList.from_csv(imdb, 'texts.csv', cols='text', vocab=data_lm.vocab)
  2. .split_from_df(col='is_valid')
  3. .label_from_df(cols='label')
  4. .databunch(bs=42))

Again we export the Learner where we load our pretrained model.

  1. learn = text_classifier_learner(data_clas, AWD_LSTM, pretrained=False).load('mini_train_clas', with_opt=False);
  2. learn.export(file = 'export_clas.pkl')

Now let’s use load_learner.

  1. learn = load_learner(imdb, file = 'export_clas.pkl')

Then we can predict with the usual method.

  1. learn.predict('I really loved that movie!')
  1. (Category negative, tensor(0), tensor([0.8878, 0.1122]))

If we’re performing inference on a larger dataset, we can add it as a test set. This will batch up the items and make inference much faster.

  1. learn.data.add_test(["That movie was terrible!",
  2. "I'm a big fan of all movies with Hal 3000."])
  3. preds,y = learn.get_preds(ds_type=DatasetType.Test)
  4. preds

Tabular

Last application brings us to tabular data. First let’s import everything we’ll need.

  1. from fastai.tabular import *

We’ll use a sample of the adult dataset here. Once we read the csv file, we’ll need to specify the dependant variable, the categorical variables, the continuous variables and the processors we want to use.

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

Then we can use the data block API to grab everything together.

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

We define a Learner object that we fit and then save the model.

  1. learn = tabular_learner(data, layers=[200,100], metrics=accuracy)
  2. learn.fit(1, 1e-2)
  3. learn.save('mini_train')
epochtrain_lossvalid_lossaccuracytime
00.3374450.3604880.84500000:04

As in the other applications, we just have to type learn.export() to save everything we’ll need for inference (here it includes the inner state of each processor).

  1. learn.export()

Then we create a Learner for inference like before.

  1. learn = load_learner(adult)

And we can predict on a row of dataframe that has the right cat_names and cont_names.

  1. learn.predict(df.iloc[0])
  1. (Category >=50k, tensor(1), tensor([0.1976, 0.8024]))

Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Jan 5, 2021