Chapter 24
From Model to Production
NotebookPython 373 cells
In [ ]python · cell 1
python
#hide
! [ -e /content ] && pip install -Uqq fastbook
import fastbook
fastbook.setup_book()In [ ]python · cell 2
python
#hide
from fastbook import *
from fastai.vision.widgets import *From Model to Production
The Practice of Deep Learning
Starting Your Project
The State of Deep Learning
Computer vision
Text (natural language processing)
Combining text and images
Tabular data
Recommendation systems
Other data types
The Drivetrain Approach
Gathering Data
clean
To download images with Bing Image Search, sign up at Microsoft Azure for a free account. You will be given a key, which you can copy and enter in a cell as follows (replacing 'XXX' with your key and executing it):
In [ ]python · cell 16
python
key = os.environ.get('AZURE_SEARCH_KEY', 'XXX')In [ ]python · cell 17
python
search_images_bingIn [ ]python · cell 18
python
results = search_images_bing(key, 'grizzly bear')
ims = results.attrgot('contentUrl')
len(ims)In [ ]python · cell 19
python
#hide
ims = ['http://3.bp.blogspot.com/-S1scRCkI3vY/UHzV2kucsPI/AAAAAAAAA-k/YQ5UzHEm9Ss/s1600/Grizzly%2BBear%2BWildlife.jpg']In [ ]python · cell 20
python
dest = 'images/grizzly.jpg'
download_url(ims[0], dest)In [ ]python · cell 21
python
im = Image.open(dest)
im.to_thumb(128,128)In [ ]python · cell 22
python
bear_types = 'grizzly','black','teddy'
path = Path('bears')In [ ]python · cell 23
python
if not path.exists():
path.mkdir()
for o in bear_types:
dest = (path/o)
dest.mkdir(exist_ok=True)
results = search_images_bing(key, f'{o} bear')
download_images(dest, urls=results.attrgot('contentUrl'))In [ ]python · cell 24
python
fns = get_image_files(path)
fnsIn [ ]python · cell 25
python
failed = verify_images(fns)
failedIn [ ]python · cell 26
python
failed.map(Path.unlink);Sidebar: Getting Help in Jupyter Notebooks
End sidebar
From Data to DataLoaders
In [ ]python · cell 30
python
bears = DataBlock(
blocks=(ImageBlock, CategoryBlock),
get_items=get_image_files,
splitter=RandomSplitter(valid_pct=0.2, seed=42),
get_y=parent_label,
item_tfms=Resize(128))In [ ]python · cell 31
python
dls = bears.dataloaders(path)In [ ]python · cell 32
python
dls.valid.show_batch(max_n=4, nrows=1)In [ ]python · cell 33
python
bears = bears.new(item_tfms=Resize(128, ResizeMethod.Squish))
dls = bears.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)In [ ]python · cell 34
python
bears = bears.new(item_tfms=Resize(128, ResizeMethod.Pad, pad_mode='zeros'))
dls = bears.dataloaders(path)
dls.valid.show_batch(max_n=4, nrows=1)In [ ]python · cell 35
python
bears = bears.new(item_tfms=RandomResizedCrop(128, min_scale=0.3))
dls = bears.dataloaders(path)
dls.train.show_batch(max_n=4, nrows=1, unique=True)Data Augmentation
In [ ]python · cell 37
python
bears = bears.new(item_tfms=Resize(128), batch_tfms=aug_transforms(mult=2))
dls = bears.dataloaders(path)
dls.train.show_batch(max_n=8, nrows=2, unique=True)Training Your Model, and Using It to Clean Your Data
In [ ]python · cell 39
python
bears = bears.new(
item_tfms=RandomResizedCrop(224, min_scale=0.5),
batch_tfms=aug_transforms())
dls = bears.dataloaders(path)In [ ]python · cell 40
python
learn = vision_learner(dls, resnet18, metrics=error_rate)
learn.fine_tune(4)In [ ]python · cell 41
python
interp = ClassificationInterpretation.from_learner(learn)
interp.plot_confusion_matrix()In [ ]python · cell 42
python
interp.plot_top_losses(5, nrows=1)In [ ]python · cell 43
python
cleaner = ImageClassifierCleaner(learn)
cleanerIn [ ]python · cell 44
python
#hide
# for idx in cleaner.delete(): cleaner.fns[idx].unlink()
# for idx,cat in cleaner.change(): shutil.move(str(cleaner.fns[idx]), path/cat)Turning Your Model into an Online Application
Using the Model for Inference
In [ ]python · cell 47
python
learn.export()In [ ]python · cell 48
python
path = Path()
path.ls(file_exts='.pkl')In [ ]python · cell 49
python
learn_inf = load_learner(path/'export.pkl')In [ ]python · cell 50
python
learn_inf.predict('images/grizzly.jpg')In [ ]python · cell 51
python
learn_inf.dls.vocabCreating a Notebook App from the Model
In [ ]python · cell 53
python
btn_upload = widgets.FileUpload()
btn_uploadIn [ ]python · cell 54
python
#hide
# For the book, we can't actually click an upload button, so we fake it
btn_upload = SimpleNamespace(data = ['images/grizzly.jpg'])In [ ]python · cell 55
python
img = PILImage.create(btn_upload.data[-1])In [ ]python · cell 56
python
out_pl = widgets.Output()
out_pl.clear_output()
with out_pl: display(img.to_thumb(128,128))
out_plIn [ ]python · cell 57
python
pred,pred_idx,probs = learn_inf.predict(img)In [ ]python · cell 58
python
lbl_pred = widgets.Label()
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
lbl_predIn [ ]python · cell 59
python
btn_run = widgets.Button(description='Classify')
btn_runIn [ ]python · cell 60
python
def on_click_classify(change):
img = PILImage.create(btn_upload.data[-1])
out_pl.clear_output()
with out_pl: display(img.to_thumb(128,128))
pred,pred_idx,probs = learn_inf.predict(img)
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
btn_run.on_click(on_click_classify)In [ ]python · cell 61
python
#hide
#Putting back btn_upload to a widget for next cell
btn_upload = widgets.FileUpload()In [ ]python · cell 62
python
VBox([widgets.Label('Select your bear!'),
btn_upload, btn_run, out_pl, lbl_pred])Turning Your Notebook into a Real App
In [ ]python · cell 64
python
#hide
# !pip install voila
# !jupyter serverextension enable --sys-prefix voila Deploying your app
How to Avoid Disaster
Unforeseen Consequences and Feedback Loops
Get Writing!
Questionnaire
- Provide an example of where the bear classification model might work poorly in production, due to structural or style differences in the training data.
- Where do text models currently have a major deficiency?
- What are possible negative societal implications of text generation models?
- In situations where a model might make mistakes, and those mistakes could be harmful, what is a good alternative to automating a process?
- What kind of tabular data is deep learning particularly good at?
- What's a key downside of directly using a deep learning model for recommendation systems?
- What are the steps of the Drivetrain Approach?
- How do the steps of the Drivetrain Approach map to a recommendation system?
- Create an image recognition model using data you curate, and deploy it on the web.
- What is
DataLoaders? - What four things do we need to tell fastai to create
DataLoaders? - What does the
splitterparameter toDataBlockdo? - How do we ensure a random split always gives the same validation set?
- What letters are often used to signify the independent and dependent variables?
- What's the difference between the crop, pad, and squish resize approaches? When might you choose one over the others?
- What is data augmentation? Why is it needed?
- What is the difference between
item_tfmsandbatch_tfms? - What is a confusion matrix?
- What does
exportsave? - What is it called when we use a model for getting predictions, instead of training?
- What are IPython widgets?
- When might you want to use CPU for deployment? When might GPU be better?
- What are the downsides of deploying your app to a server, instead of to a client (or edge) device such as a phone or PC?
- What are three examples of problems that could occur when rolling out a bear warning system in practice?
- What is "out-of-domain data"?
- What is "domain shift"?
- What are the three steps in the deployment process?
Further Research
- Consider how the Drivetrain Approach maps to a project or problem you're interested in.
- When might it be best to avoid certain types of data augmentation?
- For a project you're interested in applying deep learning to, consider the thought experiment "What would happen if it went really, really well?"
- Start a blog, and write your first blog post. For instance, write about what you think deep learning might be useful for in a domain you're interested in.
In [ ]python · cell 73
python
