The deep learning environment was up. It was time to get the hands dirty and experiment with a CNN pipeline.
This post shows how to use a pretrained CNN model for image recognition. Using pretrained models allows one to jump straight to predicting without the effort of training a model. Training deep learning models is computationally expensive so pretrained models can really speed up the process.
The model in this case is VGG_CNN_S, which is trained on the ILSVRC-2012 dataset. The dataset includes 150,000 hand labeled photos. The photos are classified into 1,000 categories. The model achieves 13.1% top-5 error with validation data. It is originally trained on Caffe, but it has also been “translated” into Lasagne.
What does a CNN pipeline look like?
The code is based on Lasagne’s deep learning pipeline tutorial. One can divide the pipeline into these steps:
1) Import everything
2) Define CNN (matching with the pretrained model)
3) Load parameters to the CNN (~400MB)
4) Get urls for the photos
5) Preprocess the photos to match VGG_CNN_S’s input format
6) Feed the photos through the CNN and get predicted labels. Enjoy.
I would like to highlight the importance of appropriate preprocessing. Preprocessing on predicted photos has to be the same as the preprocessing on the trained photos. Wrong preprocessing makes the model more or less useless. Pictures fed into VGG_CNN_S should go through these steps:
1) Resize smallest dimension to 256
2) Central crop to 224×224
3) Shuffle axes
4) Switch RGB to BRG if necessary (be careful with this one, BRG != RGB)
5) Substract the MEAN_IMAGE from the photo.
Image recognition results
Below you can see some example results from the model. You can find the photo behind the link. Predicted labels are below the link.
Predict this photo.
- lion, king of beasts, Panthera leo
- lynx, catamount
- cougar, puma, catamount, mountain lion, painter, panther, Felis concolor
- cheetah, chetah, Acinonyx jubatus
- red wolf, maned wolf, Canis rufus, Canis niger
And this one.
- bighorn, bighorn sheep, cimarron, Rocky Mountain bighorn, Rocky Mountain sheep, Ovis canadensis
- ram, tup
- ibex, Capra ibex
Seems like the model performs better than I do! 😀
That’s all this time.