TLDR;
Colab Notebook
What will you build?
In this exercise we will create and fine-tune an ‘indoor scene classifier’ using Monk.
We will use Transfer Learning, wherein we pick a ‘Model’ which has already been trained to answer a similar problem and retrain it for our case.
Transfer Learning is a faster workaround than training a model from scratch.
Monk provides a syntax invariant transfer learning framework that supports Keras, Pytorch and Mxnet in the backend. (Read — Documentation).
Setup
We start by setting up Monk and it’s dependencies on colab. For further setup instructions on different platforms check out the DOCS.
$ git clone https://github.com/Tessellate-Imaging/monk_v1
$ cd monk_v1/installation && pip install -r requirements_cu10.txt
$ cd ../..
Dataset
We will utilise the image dataset gathered by these awesome researchers — LINK.
Optional : How to download large files from Google drive
Training
Next we will use Pytorch as our backend to create a new Project and use Resnet101 as our pre-trained model.
ptf = prototype(verbose=1);
ptf.Prototype("indoor_scene", "exp1");
ptf.Default(dataset_path="./Train/",
model_name="resnet101",
freeze_base_network=True, num_epochs=10);
With Monk you can download or upload your workspace onto different machines and resume your work from where you left.
To demo this we will run inference by downloading the workspace to our local machine. To keep the download size small, we will disable saving intermediate models.
ptf.update_save_intermediate_models(False);
ptf.Reload();
ptf.Train();
Inference
Let’s zip our workspace and download it locally for inferencing. Before we can begin doing this we have to setup Monk on our local system. Check the documentation for further instructions on this — SETUP
$ zip -r workspace.zip workspace
After importing ‘Monk’ in our local workspace, we can test our model on a single image and check the predicted label. (DOCS)
ptf.Prototype("indoor_scene", "exp1", eval_infer=True);
img_name = "test.jpg";
predictions = ptf.Infer(img_name=img_name, return_raw=False);print(predictions);
Hyper-Parameter Tuning
Now that we have achieved a baseline validation accuracy of ‘INSERT VALUE’, we can resume fine-tuning the generated model from the previous exercise. To do this we use the ‘copy_from’ feature in Monk.
ptf.Prototype("indoor_scene", "exp2",
copy_from=["indoor_scene", "exp1"]);
We can now update our hyper-parameters,
###########Update hyperparameters################
ptf.optimizer_asgd(0.001, weight_decay=0.00001);ptf.update_num_epochs(10);
################################################
Add randomised cropping
########Update Transforms#####################
ptf.apply_random_resized_crop(224, train=True, val=True);
###############################################
and freeze more layers from our pre-trained model to tune the weights and biases in the last few layers.
###########For freeze layers#####################
ptf.update_freeze_layers(100);
################################################
We can now retrain our model to see if we can churn a better validation accuracy.
NOTE : Don’t forget to reload the experiment after adding updates.
ptf.Reload();
ptf.Train();
Ohh and don’t forget to take a break, stretch your legs. You can always resume training from the last break point using Monk’s resume training feature.
All you have to do is load the experiment with resume state and begin. Read the docs for more information — (DOCS)
Compare
Finally to check if the second experiment was fruitful and if we can do further improvement, we can compare our experiments and visualise losses.
from compare_prototype import compare
ctf = compare(verbose=1);
ctf.Comparison("indoor_scene");
ctf.Add_Experiment("indoor_scene", "exp1");
ctf.Add_Experiment("indoor_scene", "exp2");
ctf.Generate_Statistics()
The final training accuracy plots show that we definitely improved from our baseline model.
We don’t have to stop here. We can utilise augmentation strategies on our dataset and further improve the model performance on newer data.
Happy Coding!
Top comments (0)