Introduction
A trained model sitting in a Jupyter notebook helps nobody. To be useful, it has to run somewhere else: on a web server answering thousands of requests, inside a mobile app, in a factory's camera, or in a C++ program. That environment might not have Python, might not have PyTorch, or might need the model to run much faster than in development.
Exporting converts your model from flexible Python code into a self-contained, optimized form. Think of a chef's recipe notes, full of crossings-out and "add salt to taste". To open a chain of restaurants, the recipe must be turned into a precise, standard procedure any kitchen can follow without the original chef. Exporting does that for models.
Before exporting, always:
- put the model in evaluation mode (
model.eval()), so dropout is off and batch norm uses its running statistics; - decide on the input format (shape, data type, preprocessing) the deployed model will receive;
- keep a set of test inputs and expected outputs, to check the exported model gives the same answers.