{"id":8432,"date":"2017-03-03T08:00:14","date_gmt":"2017-03-03T13:00:14","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8432"},"modified":"2018-09-14T06:55:10","modified_gmt":"2018-09-14T10:55:10","slug":"deep-learning-in-11-lines-of-matlab-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2017\/03\/03\/deep-learning-in-11-lines-of-matlab-code\/","title":{"rendered":"Deep Learning in 11 Lines of MATLAB Code"},"content":{"rendered":"<h2><\/h2>\n<p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/4291457-avi-nehemiah\">Avi&#8217;s <\/a>pick of the week is <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/60659-deep-learning-in-11-lines-of-matlab-code\"><span style=\"text-decoration: underline;\">Deep Learning in 11 Lines of MATLAB Code<\/span><\/a> by the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/?term=authorid%3A1211826\">MathWorks Deep Learning Toolbox Team<\/a>. This post is follow up to <a href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/01\/13\/deep-learning-in-11-lines\/\">this post<\/a> by Jiro and provides a more detailed explanation.<\/p>\n<p>If you are \u00a0interested in learning more about deep learning or trying out some of latest deep learning research in MATLAB this blog post will walk you through the first steps to getting started. The entry on File Exchange provides everything you need to download one of the most popular deep neural networks and use it to classify images using\u00a0live video from a webcam. Before you read through the rest of this post, I would highly recommend you watch <span style=\"text-decoration: underline;\"><strong><a href=\"https:\/\/www.mathworks.com\/videos\/deep-learning-in-11-lines-of-matlab-code-1481229977318.html\">this video<\/a><\/strong> <\/span>by Joe Hicklin (pictured below) that illustrates what I will explain in more detail.<\/p>\n<p class=\"codeinput\"><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/Joe.png\"><img decoding=\"async\" loading=\"lazy\" width=\"747\" height=\"422\" class=\"aligncenter size-full wp-image-8433\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/Joe.png\" alt=\"\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>This post covers\u00a0how to download a pre-trained deep convolutional neural network and use it to classify images in a live video stream.<\/p>\n<h2 id=\"3\">Load Pre-Trained Network<\/h2>\n<p>The pre-trained model I&#8217;ll use in this post is known as AlexNet. This model was\u00a0trained to recognize 1000 different categories of objects in images, and was trained using over a million images. The <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/59133\">AlexNet model<\/a> is available as a support package in MATLAB, you can learn more about the AlexNet support package from <a href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/04\/deep-learning-for-image-classification\">this blog post<\/a>. Now let&#8217;s load the pre-trained network into MATLAB.<\/p>\n<pre class=\"codeinput\">nnet = alexnet; <span class=\"comment\">% Will prompt support package install if unavaliable<\/span>\r\n<\/pre>\n<h2 id=\"4\">View Network Structure<\/h2>\n<p>Now let us take a look at the structure of AlexNet model. Notice that the first layer requires a 227&#215;227 RGB image, and the last layer classifies 1000 different objects.<\/p>\n<pre class=\"codeinput\">nnet.Layers\r\n<\/pre>\n<pre class=\"codeoutput\">ans = \r\n\r\n  25x1 Layer array with layers:\r\n\r\n     1   'data'     Image Input                   227x227x3 images with 'zerocenter' normalization\r\n     2   'conv1'    Convolution                   96 11x11x3 convolutions with stride [4  4] and padding [0  0]\r\n     3   'relu1'    ReLU                          ReLU\r\n     4   'norm1'    Cross Channel Normalization   cross channel normalization with 5 channels per element\r\n     5   'pool1'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0]\r\n     6   'conv2'    Convolution                   256 5x5x48 convolutions with stride [1  1] and padding [2  2]\r\n     7   'relu2'    ReLU                          ReLU\r\n     8   'norm2'    Cross Channel Normalization   cross channel normalization with 5 channels per element\r\n     9   'pool2'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0]\r\n    10   'conv3'    Convolution                   384 3x3x256 convolutions with stride [1  1] and padding [1  1]\r\n    11   'relu3'    ReLU                          ReLU\r\n    12   'conv4'    Convolution                   384 3x3x192 convolutions with stride [1  1] and padding [1  1]\r\n    13   'relu4'    ReLU                          ReLU\r\n    14   'conv5'    Convolution                   256 3x3x192 convolutions with stride [1  1] and padding [1  1]\r\n    15   'relu5'    ReLU                          ReLU\r\n    16   'pool5'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0]\r\n    17   'fc6'      Fully Connected               4096 fully connected layer\r\n    18   'relu6'    ReLU                          ReLU\r\n    19   'drop6'    Dropout                       50% dropout\r\n    20   'fc7'      Fully Connected               4096 fully connected layer\r\n    21   'relu7'    ReLU                          ReLU\r\n    22   'drop7'    Dropout                       50% dropout\r\n    23   'fc8'      Fully Connected               1000 fully connected layer\r\n    24   'prob'     Softmax                       softmax\r\n    25   'output'   Classification Output         crossentropyex with 'tench', 'goldfish', and 998 other classes\r\n<\/pre>\n<h2 id=\"5\">Classify Test Images<\/h2>\n<p>Before we connect a webcam and try classifying images on a live stream, lets try classifying a single test image.<\/p>\n<pre class=\"codeinput\">I = imread(<span class=\"string\">'peppers.png'<\/span>);\r\nimshow(I)\r\ntitle(<span class=\"string\">'Input Image'<\/span>)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/DeepLearningIn11LinesPost_01.png\"><img decoding=\"async\" loading=\"lazy\" width=\"684\" height=\"479\" class=\"aligncenter size-full wp-image-8434\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/DeepLearningIn11LinesPost_01.png\" alt=\"\" \/><\/a><\/p>\n<p>To classify the input image we use the &#8216;classify&#8217; function.\u00a0<span class=\"comment\">Since AlexNet expects the input image to be 227&#215;227 let&#8217;s resize the<\/span> <span class=\"comment\">input image.<\/span><\/p>\n<pre class=\"codeinput\">Iin = imresize(I,[227 227]);\r\nfigure\r\nlabel = classify(nnet, Iin);        <span class=\"comment\">% Classify the picture<\/span>\r\nimshow(Iin)     <span class=\"comment\">% Show the picture<\/span>\r\ntitle(char(label)) <span class=\"comment\">% Show the label<\/span>\r\n<\/pre>\n<p><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/DeepLearningIn11LinesPost.png\"><br \/>\n<\/a> <a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/DeepLearningIn11LinesPost_02.png\"><img decoding=\"async\" loading=\"lazy\" width=\"399\" height=\"322\" class=\"aligncenter size-full wp-image-8441\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/DeepLearningIn11LinesPost_02.png\" alt=\"\" \/><\/a><\/p>\n<p class=\"codeinput\"><span class=\"comment\">You&#8217;ll see that the network accurately classifies the image as a bell <\/span><span class=\"comment\">pepper. You can also see the prediction score or confidence of the <\/span><span class=\"comment\">prediction for each of the 1000 classes by outputing the score from the <\/span><span class=\"comment\">classify method.<\/span><\/p>\n<h2 id=\"4\"><\/h2>\n<h2 id=\"6\">Try on Live Video<\/h2>\n<p>Now that we&#8217;ve tried the deep learning image classifier on a single image lets try doing the same with a live video stream. We connect to a webcam using the webcam object in MATLAB.<\/p>\n<pre class=\"codeinput\">camera = webcam;              <span class=\"comment\">% Connect to camera<\/span>\r\npicture = snapshot(camera);   <span class=\"comment\">% Take picture<\/span>\r\nimshow(picture)               <span class=\"comment\">% Show picture<\/span>\r\n\r\n<span class=\"comment\">% Run live video capture and classification in loop<\/span>\r\n<span class=\"keyword\">for<\/span> n = 1:100\r\n    picture = snapshot(camera);             <span class=\"comment\">% Take picture<\/span>\r\n    picture = imresize(picture,[227,227]);  <span class=\"comment\">% Resize picture<\/span>\r\n    label = classify(nnet, picture);        <span class=\"comment\">% Classify picture<\/span>\r\n    imshow(picture);                        <span class=\"comment\">% Show picture<\/span>\r\n    title(char(label))                      <span class=\"comment\">% Show label<\/span>\r\n    drawnow\r\n<span class=\"keyword\">end<\/span><\/pre>\n<p><img decoding=\"async\" loading=\"lazy\" width=\"812\" height=\"575\" class=\"aligncenter size-full wp-image-8445\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/DeepLearningIn11LinesPost_03.png\" alt=\"\" \/><\/p>\n<p class=\"footer\">To learn more about deep learning in MATLAB take a look at <a title=\"https:\/\/www.mathworks.com\/help\/nnet\/deep-networks.html (link no longer works)\">this page.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/Joe.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>\nAvi&#8217;s pick of the week is Deep Learning in 11 Lines of MATLAB Code by the MathWorks Deep Learning Toolbox Team. This post is follow up to this post by Jiro and provides a more detailed&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2017\/03\/03\/deep-learning-in-11-lines-of-matlab-code\/\">read more >><\/a><\/p>\n","protected":false},"author":132,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8432"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/132"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=8432"}],"version-history":[{"count":26,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8432\/revisions"}],"predecessor-version":[{"id":10133,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8432\/revisions\/10133"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}