FastAI Lesson 2: Deployment

Practical Deep Learning for Coders: Lesson 2
AI
Engineering
FastAI
Author

Gurpreet Johl

Published

August 30, 2023

ML Deployment

These are notes from lesson 2 of Fast AI Practical Deep Learning for Coders.

Homework Task
Note

A website for quizzes based on the book: AI quizzes

1. Data Pre-processing

It can be useful to train a model on the data BEFORE you clean it

  • Counterintuitive!
  • The confusion matrix output of the learner gives you a good intuition about which classifications are hard
  • plot_top_losses shows which examples were hardest for the model to classify. This can find (1) when the model is correct but not confident, and (2) when the model was confident but incorrect
  • ImageClassifierCleaner shows the examples in the training and validation set ordered by loss, so we can choose to keep, reclassify or remove them

For image resizing, random resize crop can often be more effective.

  • Squishing can result in weird, unrealistic images.
  • Padding or mirroring can add false information that the model will erroneously learn.
  • Random crops give different sections of the image which acts as a form of data augmentation.
  • aug_transforms can be use for more sophisticated data augmentation like warping and recoloring images.

2. Saving a model

Once you are happy with the model you’ve trained, you can pickle the learner object and save it.

learn.export('model.pkl')

Then you can add the saved model to the hugging face space. To use the model to make predictions, any external functions you used to create the model will need to be instantiated too.

learn = load_learner('model.pkl')
learn.predict(image)

3. Hosting a model

Hugging face spaces hosts models with a choice of pre-canned interfaces to quickly deploy a model to the public. Gradio is used in the example in the lecture. Streamlit is an alternative to Gradio that is more flexible.

Gradio requires a dict of classes as keys and probabilities (as floats not tensors) as the values. To go from the Gradio prototype to a production app, you can view the Gradio API from the huggingface space which will show you the API. The API exposes an endpoint which you can then hit from your own frontend app.

Github pages is a free and simple way to host a public website. See this fastai repo as an example of a minimal example html website which issues GET requests to the Gradio API

4. Using nbdev

To convert a notebook to a python script, you can add #|export to the top of any cells to include in the script, then use:

from nbdev.export import notebook2script
notebook2script('name_of_output_file.py')

Use #|default_exp app in the first cell of the notebook to set the default name of the exported python file.

5. Training the Model (and when to stop)

How do we choose the number of epochs to train for? Whenever it is “good enough” for your use case.

If you need to train for longer, you may need to use data augmentation to prevent overfitting. Keep an eye on the validation error to check overfitting.

References

Back to top