Project management with Monk: Creating an indoor-scene classifier
Tags: deeplearning, prototyping, projectmanagement
Published: 2019-12-03 11:21:09

---
title: Project management with Monk: Creating an indoor-scene classifier
published: true
description: In this exercise we will create and fine-tune an ‘indoor scene classifier’ using Monk.
tags: #deeplearning, #prototyping, #projectmanagement, #opensource
series: Deep Learning using Monk.
cover_image: https://thepracticaldev.s3.amazonaws.com/i/5zn59dmbuv33of6cla3v.gif
---

TLDR;
[Colab Notebook](https://drive.google.com/file/d/1Qd6XRWIsIfge5nbl0jT_RM8_a-iacEuH/view?usp=sharing&source=post_page-----6cec33fd6252----------------------)

&nbsp;
&nbsp;
&nbsp;
###What will you build?
![](https://tessellate-pytorch-serverless.s3.amazonaws.com/indoor_classification.gif)


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](https://clever-noyce-f9d43f.netlify.com/)).


&nbsp;
&nbsp;
&nbsp;
###Setup

We start by setting up Monk and it’s dependencies on colab. For further setup instructions on different platforms check out the [DOCS](https://clever-noyce-f9d43f.netlify.com/#/setup/setup).

```
$ git clone https://github.com/Tessellate-Imaging/monk_v1
$ cd monk_v1/installation && pip install -r requirements_cu10.txt
$ cd ../..
```

&nbsp;
&nbsp;
&nbsp;
###Dataset

We will utilise the image dataset gathered by these awesome researchers — [LINK](https://omidpoursaeed.github.io/publication/vision-based-real-estate-price-estimation/).

Optional : [How to download large files from Google drive](https://medium.com/@paudelanjanchandra/download-google-drive-files-using-wget-3c2c025a8b99)



&nbsp;
&nbsp;
&nbsp;
###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();
```

&nbsp;
&nbsp;
&nbsp;
###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](https://clever-noyce-f9d43f.netlify.com/#/setup/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](https://clever-noyce-f9d43f.netlify.com/#/quick_mode/quickmode_pytorch))

```
ptf.Prototype("indoor_scene", "exp1", eval_infer=True);
img_name = "test.jpg";
predictions = ptf.Infer(img_name=img_name, return_raw=False);print(predictions);
```




&nbsp;
&nbsp;
&nbsp;
###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](https://clever-noyce-f9d43f.netlify.com/))



&nbsp;
&nbsp;
&nbsp;
###Compare

Finally to check if the second experiment was fruitful and if we can do further improvement, we can [compare our experiments](https://clever-noyce-f9d43f.netlify.com/#/compare_experiment) 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.

![](https://miro.medium.com/max/1440/1*M2RwUD8pzu6cq81AdqeZCw.png)*Training Accuracies*


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!