{"id":8007,"date":"2016-11-04T09:00:17","date_gmt":"2016-11-04T13:00:17","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/?p=8007"},"modified":"2018-09-14T06:43:23","modified_gmt":"2018-09-14T10:43:23","slug":"deep-learning-for-image-classification","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/04\/deep-learning-for-image-classification\/","title":{"rendered":"Deep Learning for Image Classification"},"content":{"rendered":"<h1>Deep Learning for Image Classification<\/h1>\n<p>Avi&#8217;s pick of the week is the <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/59133\">Deep Learning Toolbox Model for AlexNet Network<\/a>, by <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/8743315\">The Deep Learning Toolbox Team<\/a>. AlexNet is a pre-trained 1000-class image classifier using deep learning more specifically a convolutional neural networks (CNN). The support package provides easy access to this powerful model to help quickly get started with deep learning in MATLAB.<\/p>\n<h2>Contents<\/h2>\n<div>\n<ul>\n<li><a href=\"#1\">Access the pre-trained model in MATLAB<\/a><\/li>\n<li><a href=\"#2\">View network architecture<\/a><\/li>\n<li><a href=\"#3\">Classify image<\/a><\/li>\n<\/ul>\n<\/div>\n<h2 id=\"1\">Access the pre-trained model in MATLAB<\/h2>\n<p>Once you have downloaded and installed the support package, you can load the pre-trained model into MATLAB.<\/p>\n<pre class=\"codeinput\">net = alexnet\r\n<\/pre>\n<pre class=\"codeoutput\">net = \r\n\r\n  SeriesNetwork with properties:\r\n\r\n    Layers: [25\u00d71 nnet.cnn.layer.Layer]\r\n\r\n<\/pre>\n<h2 id=\"2\">View network architecture<\/h2>\n<p>Now let&#8217;s take a quick look at the structure of the deep neural network layers.<\/p>\n<pre class=\"codeinput\">net.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         cross-entropy with 'tench', 'goldfish', and 998 other classes\r\n<\/pre>\n<h2 id=\"3\">Classify image<\/h2>\n<p>Now lets try and classify an image using deep learning. We need to resize the input images to match the input of the network that is 227 x 227 pixels before classifying the image.<\/p>\n<pre class=\"codeinput\">I = imread(<span class=\"string\">'Keyboard.jpg'<\/span>);\r\n\r\n<span class=\"comment\">% Adjust size of the image<\/span>\r\nsz = net.Layers(1).InputSize\r\nI = imresize(I,[sz(1) sz(2)]);\r\n\r\n<span class=\"comment\">% Classify the image using AlexNet<\/span>\r\nlabel = classify(net, I)\r\n\r\n<span class=\"comment\">% Show the image and the classification results<\/span>\r\nfigure\r\nimshow(I)\r\ntext(10,20,char(label),<span class=\"string\">'Color'<\/span>,<span class=\"string\">'white'<\/span>)\r\n<\/pre>\n<pre class=\"codeoutput\">sz =\r\n\r\n   227   227     3\r\n\r\n\r\nlabel = \r\n\r\n     typewriter keyboard \r\n\r\n<\/pre>\n<p><a href=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_01-1.png\"><img decoding=\"async\" loading=\"lazy\" width=\"399\" height=\"322\" class=\"size-full wp-image-8009 alignleft\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_01-1.png\" alt=\"potw_post_01\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>More Information on Deep Learning\u00a0<\/strong><\/p>\n<p>To learn more about deep learning and how to re-train the AlexNet model for a different task, take a look at the example in <a href=\"https:\/\/www.mathworks.com\/videos\/object-recognition-deep-learning-and-machine-learning-for-computer-vision-121144.html\">this webinar<\/a> that uses AlexNet to distinguish between different kinds of tools.<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/pick\/files\/POTW_Post_01-1.png\" onError=\"this.style.display ='none';\" \/><\/div>\n<p>Deep Learning for Image Classification<br \/>\nAvi&#8217;s pick of the week is the Deep Learning Toolbox Model for AlexNet Network, by The Deep Learning Toolbox Team. AlexNet is a pre-trained 1000-class&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2016\/11\/04\/deep-learning-for-image-classification\/\">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\/8007"}],"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=8007"}],"version-history":[{"count":8,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8007\/revisions"}],"predecessor-version":[{"id":10121,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/8007\/revisions\/10121"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=8007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=8007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=8007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}