Recipes¶
Recipes are a way to provide off the shelf algorithms that ce be either used directly from the command line or easily imported in a python script if more flexibility is needed.
A recipe should, as much a possible, be agnostic of the data and the underlying model so that it can be used as a way to quickly try an algorithm on new data or be easily experimented on by changing the model
-
class
torchelie.recipes.recipebase.
Recipe
(call_fun, loader)¶ Basic recipe that iterates mutiple epochs over a dataset. That loop is instrumented through several configurable callbacks. Callbacks can handle events before and after each batch, before and after each epoch. Each batch is treated with a user supplied function that manipulates it and returns a dict that returns a state usable by the callbacks.
A recipe can be saved by calling its
state_dict()
member. All its hyper parameters and state will be saved so that it can restart, but requires the exact same setting of callbacks.You can register multiple objects to a Recipe with
register()
. If it has astate_dict()
member, its state will be saved into the recipe’s when calling the recipe’sstate_dict()
function. If it has a memberto()
, moving the recipe to another device will also move those objects.Parameters: - call_fun (Callable) – A function that takes a batch as an argument and returns a dict of value to feed the state
- loader (Iterable) – any iterable (most likely a DataLoader)
-
cpu
()¶ Move a recipe and all its movable registered objects to cpu
-
cuda
()¶ Move a recipe and all its movable registered objects to cuda
-
load_state_dict
(state_dict)¶ Restore a recipe
-
modules
()¶ Iterate over all nn.Modules registered in the recipe
-
register
(name, value)¶ Register an object into the recipe as a member. Calling
recipe.register('foo', bar)
registers bar, and makes it usable throughrecipe.foo
.Parameters: - name (str) – member’s name
- value – the object to register
-
run
(epochs)¶ Run the recipe for
epochs
epochs.Parameters: epochs (int) – number of epochs Returns: The state
-
state_dict
()¶ Returns: A state dict
-
to
(device)¶ Move a recipe and all its movable registered objects to a device
Parameters: device – a torch device
-
torchelie.recipes.
TrainAndCall
(model, train_fun, test_fun, train_loader, test_every=100, visdom_env='main', checkpoint='model', log_every=10)¶ Train a model and evaluate it with a custom function. The model is automatically checkpointed, VisdomLogger and StdoutLogger callbacks are also already provided. The gradients are disabled and the model is automatically set to evaluation mode for the evaluation procedure.
Parameters: - model (nn.Model) – a model
- train_fun (Callabble) – a function that takes a batch as a single argument, performs a training step and return a dict of values to populate the recipe’s state.
- test_fun (Callable) – a function taking no argument that performs something to evaluate your model and returns a dict to populate the state.
- train_loader (DataLoader) – Training set dataloader
- test_every (int) – testing frequency, in number of iterations (default: 100)
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- checkpoint (str) – checkpointing path or None for no checkpointing
- log_every (int) – logging frequency, in number of iterations (default: 10)
Returns: a configured Recipe
-
torchelie.recipes.
TrainAndTest
(model, train_fun, test_fun, train_loader, test_loader, test_every=100, visdom_env='main', log_every=10, checkpoint='model')¶ Perform training and testing on datasets. The model is automatically checkpointed, VisdomLogger and StdoutLogger callbacks are also already provided. The gradients are disabled and the model is automatically set to evaluation mode for the evaluation procedure.
Parameters: - model (nn.Model) – a model
- train_fun (Callabble) – a function that takes a batch as a single argument, performs a training step and return a dict of values to populate the recipe’s state.
- test_fun (Callable) – a function taking a batch as a single argument then performs something to evaluate your model and returns a dict to populate the state.
- train_loader (DataLoader) – Training set dataloader
- test_loader (DataLoader) – Testing set dataloader
- test_every (int) – testing frequency, in number of iterations (default: 100)
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- log_every (int) – logging frequency, in number of iterations (default: 100)
- checkpoint (str) – checkpointing path or None for no checkpointing
-
torchelie.recipes.
Classification
(model, train_fun, test_fun, train_loader, test_loader, classes, visdom_env=None, test_every=1000, log_every=100)¶ Classification training and testing loop. Displays Loss, accuracy, gradient based visualization of features, a classification report on worst samples, best samples and confusing samples, a confusion matrix, VisdomLogger, StdLogger, MetricsTable.
Parameters: - model (nn.Model) – a model
- train_fun (Callabble) – a function that takes a batch as a single argument, performs a training forward pass and return a dict of values to populate the recipe’s state. It expects the logits predictions under key “preds”, and this batch’s loss under key “loss”.
- test_fun (Callable) – a function taking a batch as a single argument then performs something to evaluate your model and returns a dict to populate the state. It expects the logits predictions under key “preds”, and this batch’s loss under key “loss”.
- train_loader (DataLoader) – Training set dataloader
- test_loader (DataLoader) – Testing set dataloader
- classes (list of str) – classes name, in order
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- test_every (int) – testing frequency, in number of iterations (default: 1000)
- log_every (int) – logging frequency, in number of iterations (default: 100)
-
torchelie.recipes.
CrossEntropyClassification
(model, train_loader, test_loader, classes, lr=0.003, beta1=0.9, wd=0.01, visdom_env='main', test_every=1000, log_every=100)¶ A Classification recipe with a default froward training / testing pass using cross entropy, and extended with RAdamW and ReduceLROnPlateau.
Parameters: - model (nn.Module) – a model learnable with cross entropy
- train_loader (DataLoader) – Training set dataloader
- test_loader (DataLoader) – Testing set dataloader
- classes (list of str) – classes name, in order
- lr (float) – the learning rate
- beta1 (float) – RAdamW’s beta1
- wd (float) – weight decay
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- test_every (int) – testing frequency, in number of iterations (default: 1000)
- log_every (int) – logging frequency, in number of iterations (default: 1000)
-
torchelie.recipes.gan.
GANRecipe
(G, D, G_fun, D_fun, loader, visdom_env='main', log_every=10)¶
(unstable)
Model Training¶
-
torchelie.recipes.trainandtest.
TrainAndTest
(model, train_fun, test_fun, train_loader, test_loader, test_every=100, visdom_env='main', log_every=10, checkpoint='model')¶ Perform training and testing on datasets. The model is automatically checkpointed, VisdomLogger and StdoutLogger callbacks are also already provided. The gradients are disabled and the model is automatically set to evaluation mode for the evaluation procedure.
Parameters: - model (nn.Model) – a model
- train_fun (Callabble) – a function that takes a batch as a single argument, performs a training step and return a dict of values to populate the recipe’s state.
- test_fun (Callable) – a function taking a batch as a single argument then performs something to evaluate your model and returns a dict to populate the state.
- train_loader (DataLoader) – Training set dataloader
- test_loader (DataLoader) – Testing set dataloader
- test_every (int) – testing frequency, in number of iterations (default: 100)
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- log_every (int) – logging frequency, in number of iterations (default: 100)
- checkpoint (str) – checkpointing path or None for no checkpointing
-
torchelie.recipes.classification.
Classification
(model, train_fun, test_fun, train_loader, test_loader, classes, visdom_env=None, test_every=1000, log_every=100)¶ Classification training and testing loop. Displays Loss, accuracy, gradient based visualization of features, a classification report on worst samples, best samples and confusing samples, a confusion matrix, VisdomLogger, StdLogger, MetricsTable.
Parameters: - model (nn.Model) – a model
- train_fun (Callabble) – a function that takes a batch as a single argument, performs a training forward pass and return a dict of values to populate the recipe’s state. It expects the logits predictions under key “preds”, and this batch’s loss under key “loss”.
- test_fun (Callable) – a function taking a batch as a single argument then performs something to evaluate your model and returns a dict to populate the state. It expects the logits predictions under key “preds”, and this batch’s loss under key “loss”.
- train_loader (DataLoader) – Training set dataloader
- test_loader (DataLoader) – Testing set dataloader
- classes (list of str) – classes name, in order
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- test_every (int) – testing frequency, in number of iterations (default: 1000)
- log_every (int) – logging frequency, in number of iterations (default: 100)
-
torchelie.recipes.classification.
CrossEntropyClassification
(model, train_loader, test_loader, classes, lr=0.003, beta1=0.9, wd=0.01, visdom_env='main', test_every=1000, log_every=100)¶ A Classification recipe with a default froward training / testing pass using cross entropy, and extended with RAdamW and ReduceLROnPlateau.
Parameters: - model (nn.Module) – a model learnable with cross entropy
- train_loader (DataLoader) – Training set dataloader
- test_loader (DataLoader) – Testing set dataloader
- classes (list of str) – classes name, in order
- lr (float) – the learning rate
- beta1 (float) – RAdamW’s beta1
- wd (float) – weight decay
- visdom_env (str) – name of the visdom environment to use, or None for not using Visdom (default: None)
- test_every (int) – testing frequency, in number of iterations (default: 1000)
- log_every (int) – logging frequency, in number of iterations (default: 1000)
Deep Dream¶
Deep Dream recipe.
Performs the algorithm described in https://ai.googleblog.com/2015/06/inceptionism-going-deeper-into-neural.html
This implementation differs from the original one: the image is optimized in Fourier space, for greater details and colors, the model and layers are customizable.
A commandline interface is provided through python3 -m torchelie.recipes.deepdream, and a DeepDreamRecipe is provided.
-
class
torchelie.recipes.deepdream.
DeepDream
(model, dream_layer)¶ Deep Dream recipe
First instantiate the recipe then call recipe(n_iter, img)
Parameters: - model (nn.Module) – the trained model to use
- dream_layer (str) – the layer to use on which activations will be maximized
-
fit
(ref, iters, lr=0.0003, device='cpu', visdom_env='deepdream')¶ Parameters: - lr (float, optional) – the learning rate
- visdom_env (str or None) – the name of the visdom env to use, or None to disable Visdom
Feature visualization¶
Feature visualization
This optimizes an image to maximize some neuron in order to visualize the features it captures
A commandline is provided with python3 -m torchelie.recipes.feature_vis
-
class
torchelie.recipes.feature_vis.
FeatureVis
(model, layer, input_size, lr=0.001, device='cpu', visdom_env='feature_vis')¶ Feature viz
First instantiate the recipe then call recipe(n_iter, img)
Parameters: - model (nn.Module) – the trained model to use
- layer (str) – the layer to use on which activations will be maximized
- input_size (int) – the size of the square image the model accepts as input
- lr (float, optional) – the learning rate
- device (device) – where to run the computation
- visdom_env (str or None) – the name of the visdom env to use, or None to disable Visdom
-
fit
(n_iters, neuron)¶ Run the recipe
Parameters: - n_iters (int) – number of iterations to run
- neuron (int) – the feature map to maximize
Returns: the optimized image
Neural Style¶
Neural Style from Leon Gatys
A commandline interface is provided through python3 -m torchelie.recipes.neural_style
-
class
torchelie.recipes.neural_style.
NeuralStyle
(lr=0.01, device='cpu', visdom_env='style')¶ Neural Style Recipe
First instantiate the recipe then call recipe(n_iter, img)
Parameters: - lr (float, optional) – the learning rate
- device (device) – where to run the computation
- visdom_env (str or None) – the name of the visdom env to use, or None to disable Visdom
-
fit
(iters, content_img, style_img, style_ratio, content_layers=None)¶ Run the recipe
Parameters: - n_iters (int) – number of iterations to run
- content (PIL.Image) – content image
- style (PIL.Image) – style image
- ratio (float) – weight of style loss
- content_layers (list of str) – layers on which to reconstruct content
Deep Image Prior¶
Deep Image Prior as a command line tool is in this recipe, it provides super-resolution and denoising.
python3 -m torchelie.recipes.image_prior
for the usage message