Deep Learning: Transfer Learning in 10 Lines of MATLAB Code


hi my name is Joe Hicklin I’m a senior developer at the math works I’m going to show you how to do transfer learning transfer learning can be a very practical way to apply deep learning to your problems with transfer learning you take a pre-existing neural net modify it slightly and then retrain it on your images this can produce excellent results and it’s far far easier than designing a network from scratch and training it yourself in my work I need to be able to distinguish hamburgers from hot dogs and cupcakes and apple pie and and ice cream as far as I know there’s no network that will do that for me so I’m going to start with a pre-existing Network Alex met Alex that’s been trained to classify a thousand different kinds of images and it’s been trained on over a million images already so here I am I’m going to start out loading Alex net and I’m going to get the layers out of it so I can see the parts if you look down here you can see that Alex now has 25 layers most of the layers are doing useful image processing things that will work for my system as well as for Alex net I’m going to leave those alone but the 23rd layer has a thousand neurons in it because Alex net classifieds a thousand different images I’m only going to do a 5 different kinds of images so I’m going to replace that with a network that only has five images finally I’m going to replace the output layer as well the last layer of Alex that has learned Alex Nets classifications those thousand different classes I don’t want that I’m going to replace it with an empty layer that’s going to learn mine so now I’ve got my network set up it’s time to deal with the data you don’t need a million images like Alex net was trained on but you do need a thousand of them to get good results I’ve made a folder with five subfolders in it one for each of my classes so there’s one called apple-pie one called cupcakes and so on and inside each of these folders are thousand images of the appropriate topic I’ve sized these images to be the size Alex and that expects to 27 by 227 and you’ll have to do that too if you arrange your data like this you can use matlab’s image data store object because it understands that structure and it will load all the images and label them appropriately for you so that’s what I’m doing here as soon as I’ve got my images I need to separate them into two sets most of them are used for training but I’ll save a few of them out to test for accuracy later so let’s do that and now I’m all set to train my network I’ve got to set up a few Network parameters here I’ve chosen parameters are going to work well you can change these if you like and see what happens and then I’m ready to train the network that started that’s going to take five or six minutes to do its job I have a fairly powerful GPU in my computer so it’s pretty quick your mileage may vary all right the network’s done training the first thing we’re going to do now is see how accurate it is we’re going to ask the network to classify the test images the images we left out of our training set and then we’re just going to see what fraction of those it gets right we were 84% accurate pretty good for five minutes of work let’s try it now at the webcam on some real food I just happen to have some food than my desk numbers hamburgers apple pie hot dogs ice cream so overall it works pretty good it’s fairly robust for a lot of these for an angles and stuff so there we go that works better than I expected really I simplified this demo as much as I could but in the download will include a second file that will have a lot more comments and it will have some more code to handle some situations that might arise I’ve showed you how to do classification with transfer learning but if you need real numbers out you can also do regression with transfer learning well I hope I’ve shown you enough to get you interested in transfer learning so grab some snacks and give it a go you

24 Comments

  1. Girish Nair

    March 5, 2017 at 10:30 am

    Good work. Is there an example of regression being used in transfer learning ?

  2. Waleed Raza

    April 26, 2017 at 2:43 pm

    How do i configure it to work on CPU?

  3. wikkerfly

    June 9, 2017 at 4:46 pm

    Wonderful video, truly helps

  4. Ji Yuan

    June 13, 2017 at 6:01 am

    Where is the function classifyVideo? Did you write it yourself?

  5. Devanand T ദേവാനന്ദ്

    August 2, 2017 at 5:54 am

    Where can I get image dataset for this experiment?

  6. mitsokampolis

    August 16, 2017 at 2:57 pm

    Well the video is amazing i understand everything. It helps that i am not a beginner in deep learning as i am doing my MSc dissertation on this topic. I 've got a serious issue though. It appears that alexnet and imageDatastore are not available in MATLAB R2015a, which i use…
    Serious question: Is there another way to use deep learning and alexnet in my version? I can't afford to buy the 2017 one 🙁

  7. Imran Adib

    September 23, 2017 at 4:07 am

    Hi, can I know where or what apps that I need to use for resize the image/dataset in a large number? because I have thousand of images but not in the same size. Please help me.

  8. Sundar B

    January 7, 2018 at 3:49 pm

    Thank you a lot!!

  9. devyani parate

    January 13, 2018 at 3:30 pm

    How do we classify the real time images? In the video, the video input is given. Instead, I want to give a real time image input through webcam.

  10. Amr Ezz El-Din Rashed

    February 19, 2018 at 7:49 am

    How to download alexnet?and How to import it in matlab 2016?

  11. Reisimben

    March 27, 2018 at 11:29 pm

    He cometed on my video!!!!

  12. Vrushal Sakharkar

    March 31, 2018 at 4:43 am

    Can i use alexnet for classification of different phases of moons?

  13. Siluveru Kiran Kumar

    April 2, 2018 at 4:21 pm

    Can I use this with inbuilt cam of laptop

  14. Ahmad Maher

    April 2, 2018 at 6:37 pm

    We need object detection examples, not just classification stuff !!

  15. Rikesh Oli

    April 3, 2018 at 5:00 pm

    What will be the code for keyframe extraction from video instead of images?

  16. Taha Khan

    April 30, 2018 at 7:13 pm

    training is too much slow ? sir help me how to fast my training ??

  17. Imran Shikkari

    May 6, 2018 at 6:16 am

    sir i have tested it on the images of the objects other than the objects for which it is trained. it give wrong output. sir is there any way to recognize certain object and when it will be tested on other than that specific objects it must give no results…. please help me i will be very thankful to you

  18. Michael Ballack

    June 16, 2018 at 8:49 pm

    How do you resize the folders correctly?

  19. budapesteBR2012

    September 2, 2018 at 4:27 am

    You are a monster. How you do that. incredible. So much possibilities

  20. паша зайцев

    December 5, 2018 at 10:02 pm

    I have NN which has been trained to classify a ten symbols. Then i got 11th symbol in my task. How can i use this principle for adding "knowledge" of 11th symbol w/o full-training again?

  21. Srinivasa Satya

    January 19, 2019 at 3:10 pm

    sir is it mandatory that all the images should be of same format like only jpg or only png ?

  22. Enes Veliu

    February 6, 2019 at 11:55 pm

    Why did not you test you code with the items on the table? In the case one can receive a better flavour of your code!

  23. alliana marie dela fuente

    February 17, 2019 at 3:45 pm

    Thanks.. Where Can I Figure Out the Color of Image how can i do that?

  24. cik sity

    March 17, 2019 at 4:25 pm

    so what about transfer learning using googlenet ? there is any example ?

Leave a Reply