[MXNET-423] Gluon Model Zoo Pre Trained Model tutorial #10959
Conversation
BTW where is the entry point for these tutorials? Are they listed on the tutorial page? |
@piiswrong yes the index.md file of the docs/tutorial folder is updated You can find the tutorials listed here: http://mxnet.incubator.apache.org/tutorials/index.html |
* [Linear Regression](http://gluon.mxnet.io/chapter02_supervised-learning/linear-regression-gluon.html) <img src="https://upload.wikimedia.org/wikipedia/commons/6/6a/External_link_font_awesome.svg" alt="External link" height="15px" style="margin: 0px 0px 3px 3px;"/> | ||
* [MNIST Handwritten Digit Classification](/tutorials/gluon/mnist.html) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this tutorial is already listed three lines above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very needed tutorial! Should we talk about freezing parameters during fine-tuning. Or do we want to do that in a different tutorial?
## Loading the model | ||
|
||
The [Gluon Model Zoo](https://mxnet.incubator.apache.org/api/python/gluon/model_zoo.html) provides a collection of off-the-shelf models. You can get the ImageNet pre-trained model by using `pretrained=True`. | ||
If you want to train on your own classification problem from scratch, you can get an untrained network with a specific number of classes using the `classes=10` for example |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using the classes
parameter. For example,
filename = mx.test_utils.download('https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/onnx/images/dog.jpg?raw=true', fname='dog.jpg') | ||
``` | ||
|
||
If you want to use your own image for the test, upload it in the same folder and change the following line |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume user has to copy the image to the same folder as the notebook. 'Upload' usually means uploading to a server. Do you want to change this to something along the lines of "copy the image to the same folder that contains the notebook and change the following line"?
|
||
|
||
Neural network expects input in a specific format. Usually images comes in the `Width x Height x Channels` format. Where channels are the RGB channels. | ||
This network accepts images in the `BatchSize x 3 x 224 x 224`. `224 x 224` is the image resolution, that?s how the model was trained. `3` is the number of channels : Red, Green and Blue (in this order). In this case we use a `BatchSize` of `1` since we are predicting one image at a time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's
top_pred = predictions.topk(k=3)[0].asnumpy() | ||
``` | ||
|
||
And we print the category predicted with the corresponding probability: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
categories predicted with their corresponding probabilities.
@indhub Thanks for the review! I have updated accordingly I have linked to 2 fine-tuning tutorials which cover quite well the subject, especially the straight dope one since it is using a model from the model zoo as well. As for freezing the parameters during fine-tuning, my experience has been that it consistently underperformed compared to full network fine-tuning, even with smaller datasets. Do you have any use-case or datasets where freezing parameters during fine-tuning would be advantageous? In my tests, I thought I would at least see faster convergence when freezing all but the last layer, but it took roughly the same time and got worse accuracy than full network training. |
Regardless of your personal experience, freezing parameters is a technique used in many papers and I'm sure some users would be looking for a way to experiment with that. I think it makes sense to add it to this tutorial. |
@safrooze I am not disputing the fact that freezing parameters is a useful (and used) technique, especially training difficult to converge networks like GANs where you want to freeze parts of your network at times to help convergence. However I think it is out of scope of a tutorial called Using pre-trained models from the Gluon Model Zoo. I would stick to my decision to not include a fully worked out example of fine-tuning, freezing parameters, etc. It should be the subject of a separate tutorial where its usefulness is demonstrable and demonstrated. It is the |
Agreed. |
|
||
# Using pre-trained models in MXNet | ||
|
||
In this tutorial we will see how to use multiple pre-trained models with Apache MXNet. First, let's download three images classification models from the Apache MXNet [Gluon model zoo](https://mxnet.incubator.apache.org/api/python/gluon/model_zoo.html). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: images->image
Also since you're not really downloading any models in this section, maybe reword to: *In this tutorial we will use the following three different image classification models:"
import mxnet as mx | ||
from mxnet import gluon, nd | ||
from mxnet.gluon.model_zoo import vision | ||
import matplotlib.pyplot as plt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8: matplotlib < mxnet
from mxnet.gluon.model_zoo import vision | ||
import matplotlib.pyplot as plt | ||
import numpy as np | ||
import json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PEP8: system imports (json) before other packages followed by a blank line.
resnet18 = vision.resnet18_v1(pretrained=True, ctx=ctx) | ||
``` | ||
|
||
We can look at the description of the MobileNet network for example, which has a relatively simple though deep architecture |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"simple yet deep" or just simple deep?
* Resize the shorter edge of the image 224. | ||
* Crop, using a size of 224x224 from the center of the image. | ||
* Shift the mean and standard deviation of our color channels to match the ones of the dataset the network has been trained on. | ||
* Reshape the array from (Height, Width, 3) to (3, Height, Width). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Transpose, not Reshape.
mean=mx.nd.array([0.485, 0.456, 0.406]), | ||
std=mx.nd.array([0.229, 0.224, 0.225])) | ||
# the network expect batches of the form (N,3,224,224) | ||
flipped_axis = normalized.transpose((2,0,1)) # Flipping from (224, 224, 3) to (3, 224, 224) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a better name than "flipped_axis"? I don't really think of transpose as flipping. You're not reversing that axis to consider it a flipped axis. Maybe swapped_axis? Also perhaps fix the comment as well to match.
``` | ||
|
||
## Testing the different networks | ||
We run the image through each pre-trained network. The models output a *NDArray* holding 1,000 activation values, which we convert to probabilities using the `softmax()` function, corresponding to the 1,000 categories it has been trained on. The *NDArray* has only one line since batch size is equal to 1 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"The NDArray has only one line..." to "The output prediction NDArray has only one row..."
|
||
As you can see, pre-trained networks produce slightly different predictions, and have different run-time. In this case, MobileNet is almost **5 times faster** than DenseNet! | ||
|
||
## Fine-tuning using pre-trained models |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine-tuning pre-trained models (drop using)
## Loading the model | ||
|
||
The [Gluon Model Zoo](https://mxnet.incubator.apache.org/api/python/gluon/model_zoo.html) provides a collection of off-the-shelf models. You can get the ImageNet pre-trained model by using `pretrained=True`. | ||
If you want to train on your own classification problem from scratch, you can get an untrained network with a specific number of classes using the `classes` parameter: for example `classes=10`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a line of code to show how to get the untrained model with different number of classes? Also perhaps emphasise that one cannot change number of classes and user pretrained as the same time.
plt.imshow(image.asnumpy()) | ||
``` | ||
|
||
![png](https://github.com/dmlc/web-data/blob/master/mxnet/doc/tutorials/onnx/images/dog.jpg?raw=true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
notebook-skip-line here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
* adding the pre-trained model tutorial * adding pretrained model tutorial * updating the tutorial * Update pretrained_models.md * Update test_tutorials.py * Update pretrained_models.md * Update pretrained_models.md * updates following indhu's review * Update pretrained_models.md * Trigger build * Trigger build * Trigger build * implement sina feedback * Update pretrained_models.md * Trigger build
* adding the pre-trained model tutorial * adding pretrained model tutorial * updating the tutorial * Update pretrained_models.md * Update test_tutorials.py * Update pretrained_models.md * Update pretrained_models.md * updates following indhu's review * Update pretrained_models.md * Trigger build * Trigger build * Trigger build * implement sina feedback * Update pretrained_models.md * Trigger build
* adding the pre-trained model tutorial * adding pretrained model tutorial * updating the tutorial * Update pretrained_models.md * Update test_tutorials.py * Update pretrained_models.md * Update pretrained_models.md * updates following indhu's review * Update pretrained_models.md * Trigger build * Trigger build * Trigger build * implement sina feedback * Update pretrained_models.md * Trigger build
Description
There has been several instances of beginner mxnet users who haven't been able to use the models from the model zoo as they were lacking a fully integrated example. This puts all parts together. There was a blog post recently posted here: https://aws.amazon.com/fr/blogs/machine-learning/use-pre-trained-models-with-apache-mxnet/ using the module API. This is an attempt to draw people towards the nicer Gluon API for pre-trained model.
Checklist
Essentials
Please feel free to remove inapplicable items for your PR.