Deploying with @fastdotai isn't always learn = load_learner(), learn.predict. There are numerous scenarios when you might only want some, part, or none of both the API and the library as a whole. In this thread we will be exploring your options, how they work, and what to do: 1/n
Ideally we have the following context:

DataBlock -> DataLoaders -> Model -> Learner -> Train

This can then stem off to a few things:

1. learn.export() -> Model and DataLoaders (which are now blank) ...
In this scenario, we need to ensure that ALL functions which were used in relation to the data are imported before loading in the learner. This can run into issues when using fastAPI and other platforms when loading in the Learner is done in a multi-process fashion 3/
2. We have my #fastinference library:

learn.to_onnx() -> Model is run through ONNX ort and DataLoaders (which are the same blank DataLoaders from 1) get exported.

Still requires that all related functions are in the namespace still 4/
3. We have just saving and loading in the raw @PyTorch models and @fastdotai blank DataLoaders (depicted below): 5/
Now if we want to REMOVE @fastdotai all together, here's some basic to-knows:

1. fastai's test_dl/predict/etc are based on the validation transforms
2. As a result, cropping is center cropped and at most your transform pipeline is: 6/
When deploying we can then have at most 3 situations:

1. Full @fastdotai Learner, need to export any custom functions into a file and import it
2. fastai DataLoaders and raw @PyTorch model
3. Nothing exported from @fastdotai, just raw torch model 7/
Obviously for 1, we just use the raw @fastdotai API:

But what about 2 and 3?
For 2 we have:
And for 3, we look at the transforms that were used (I'm assuming image context here, for tabular and text I'd just recommend 2) and do the following with a VERY important note:

ALWAYS set split_idx to 1, this will ensure that your transforms mimic the validation set

10/
You can replace `learn.model` `net` for any model representation, however if you're using ONNX, you should also do the following as ONNX wants raw Numpy:

11/
Finally, if you don't want any fastai WHATSOEVER (this includes DataLoaders), then make sure to mimic what happens on the validation Dataloaders. For images this involves center cropping, and Normalization.

12/
Anything branded as "augmentation" is almost never done on the validation set (during training) and shouldn't be used in production.

Hope this helps folks! I'll link to a few useful libraries I've made in regards to @fastdotai inference below:

13/
fastinference: Can speed up fastai inference and makes some aspects of it a bit more "readable" WITHOUT any changes to the overall API: muellerzr.github.io/fastinference/…

14/
fastinference with ONNX: ONNX wrapper for fastai which operates with situation 2 (fastai DataLoaders, ONNX model) muellerzr.github.io/fastinference/…

15/
fastai_minima: Minimal version of the fastai library. Mostly designed for training but has minimal dependancies and has all of Learner's capabilities: muellerzr.github.io/fastai_minima/

Thanks for reading!

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Zach Mueller

Zach Mueller Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @TheZachMueller

8 Apr
A list of the best (and partially biased) @fastdotai resources, in no particular order, with descriptions and how I utilize them 🙂

(A thread)
docs.fast.ai paired with their source notebooks in github.com/fastai/fastai/….

Looking from documentation to examples right away, and letting yourself get your hands dirty. The most common notebooks I go back to ...
nb50, DataBlock: docs.fast.ai/tutorial.datab…

Shows a variety of ways to create @fastdotai DataBlock and DataLoaders with the midlevel API

Callback (Core): docs.fast.ai/callback.core.…

Remembering the event trigger names, how they work, and quick links to other examples

...
Read 13 tweets
7 Apr
Quiz Time!

Can anyone tell me the difference between this @fastdotai code? How does it behave differently:
Answer: There isn't!

One is more "verbose" in my opinion (get_x and get_y), but neither lose any flexibility as you use it.

2/
But what if I have three "blocks"? Don't I need to use `getters` for that?

Not necessarily. Since we can declare an `n_inp` (as seen below), we can simply load in our `get_x` or `get_y` with multiple functions to be utilized instead for those types:
Read 5 tweets
6 Apr
lib2nbdev has been on my mind for months now, and finally it exists! What's the biggest struggle with @fastdotai's #nbdev? Using it on existing projects. This tool aims to fix that.

But how does it work?

novetta.github.io/lib2nbdev/
With a simple bash command: `convert_lib`.

You are then guided through the process of setting up nbdev's `setting.ini` file and afterwards all of your existing code will be converted directly into fully functional notebooks! Image
But wait, don't you just throw it all into one big cell?

NO! Instead lib2nbdev will determine what should be private or public, what's an import, and what particular cell tag it should be generated with, such as below which has both private and public tags: Image
Read 8 tweets
27 Feb
Thanks everyone for joining me on this much more brief stream, in this thread I'll try and summarize all we discussed including:

- x is not implemented for y
- LossMetrics
- TensorBase and tensor metadata

And here is the stream:

1/
.@fastdotai had utilized their own tensor subclassing system in v2, however originally there weren't many issues as fastai just "let" you do things with these classes. Then @PyTorch came along and introduced them in 1.7. Suddenly, people were getting this! Why?
Pytorch became much more explicit in what subclasses can interact with oneanother. As a result @fastdotai had to make the TensorBase class which allows for any tensor-types to interact with each other. A quick function to convert and an example are below 3/
Read 11 tweets
20 Feb
Thank you all SO much for joining me tonight, it was a ton of fun to be back in the streaming/teaching wheelhouse again! We had almost 300 people chime in! For those that missed out, the video link is here:
1/
The notebooks are also available here, and eventually I'll have them fully integrated into walkwithfastai.com, just need to figure out a good way first! 2/ github.com/walkwithfastai…
From what I gathered from the feedback, it seems this stream really did help open up everyone's eyes to the power of @fastdotai's Callback system, and how it's training loop works. It's not just a marketing phrase when they say you can modify ~everything~ in the training loop! 3/
Read 5 tweets
18 Feb
Have you wanted to know just what goes on in @fastdotai's training loop and just what those pesky Callbacks are up to? In the latest release of Walk with @fastdotai this is now possible!

walkwithfastai.com/basics.trainin…

1/ Image
What we've done here is extended fastai's existing `show_training_loop` functionality to include some information (from a provided doc string) of what exactly is occurring during each event. This then lets you get more familiar with what fastai's training loop, ... 2/
Understand when things are called, and what happens at each step. The old version (which is still there if you pass `verbose=False`) simply just showed what Callbacks were triggered (pictured below) Image
Read 5 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!