Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

alexnet

Pretrained AlexNet convolutional neural network

Syntax

net = alexnet

Description

example

net = alexnet returns a pretrained AlexNet model. This model is trained on a subset of the ImageNet database [1], which is used in ImageNet Large-Scale Visual Recognition Challenge (ILSVRC) [2]. The model is trained on more than a million images and can classify images into 1000 object categories. For example, keyboard, mouse, pencil, and many animals. As a result, the model has learned rich feature representations for a wide range of images.

This function requires Neural Network Toolbox™ Model for AlexNet Network support package. If this support package is not installed, the function provides a download link. To see a list of the latest downloads, see MathWorks Neural Network Toolbox Team.

Examples

collapse all

Download and install Neural Network Toolbox Model for AlexNet Network support package.

Type alexnet at the command line.

alexnet

If Neural Network Toolbox Model for AlexNet Network support package is not installed, then the function provides a link to the required support package in the Add-On Explorer. To install the support package, click the link, and then click Install. Check that the installation is successful by typing alexnet at the command line.

alexnet
ans = 

  SeriesNetwork with properties:

    Layers: [25×1 nnet.cnn.layer.Layer]

If the required support package is installed, then the function returns a SeriesNetwork object.

Load a pretrained AlexNet convolutional neural network and examine the layers and classes.

Load the pretrained AlexNet network using alexnet. The output net is a SeriesNetwork object.

net = alexnet
net = 

  SeriesNetwork with properties:

    Layers: [25×1 nnet.cnn.layer.Layer]

Using the Layers property, view the network architecture. The network comprises of 25 layers. There are 8 layers with learnable weights: 5 convolutional layers, and 3 fully connected layers.

net.Layers
ans = 

  25x1 Layer array with layers:

     1   'data'     Image Input                   227x227x3 images with 'zerocenter' normalization
     2   'conv1'    Convolution                   96 11x11x3 convolutions with stride [4  4] and padding [0  0]
     3   'relu1'    ReLU                          ReLU
     4   'norm1'    Cross Channel Normalization   cross channel normalization with 5 channels per element
     5   'pool1'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0]
     6   'conv2'    Convolution                   256 5x5x48 convolutions with stride [1  1] and padding [2  2]
     7   'relu2'    ReLU                          ReLU
     8   'norm2'    Cross Channel Normalization   cross channel normalization with 5 channels per element
     9   'pool2'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0]
    10   'conv3'    Convolution                   384 3x3x256 convolutions with stride [1  1] and padding [1  1]
    11   'relu3'    ReLU                          ReLU
    12   'conv4'    Convolution                   384 3x3x192 convolutions with stride [1  1] and padding [1  1]
    13   'relu4'    ReLU                          ReLU
    14   'conv5'    Convolution                   256 3x3x192 convolutions with stride [1  1] and padding [1  1]
    15   'relu5'    ReLU                          ReLU
    16   'pool5'    Max Pooling                   3x3 max pooling with stride [2  2] and padding [0  0]
    17   'fc6'      Fully Connected               4096 fully connected layer
    18   'relu6'    ReLU                          ReLU
    19   'drop6'    Dropout                       50% dropout
    20   'fc7'      Fully Connected               4096 fully connected layer
    21   'relu7'    ReLU                          ReLU
    22   'drop7'    Dropout                       50% dropout
    23   'fc8'      Fully Connected               1000 fully connected layer
    24   'prob'     Softmax                       softmax
    25   'output'   Classification Output         crossentropyex with 'tench', 'goldfish', and 998 other classes

You can view the names of the classes learned by the network by viewing the ClassNames property of the classification output layer (the final layer). View the first 10 classes by selecting the first 10 elements.

net.Layers(end).ClassNames(1:10)
ans =

  1×10 cell array

  Columns 1 through 4

    'tench'    'goldfish'    'great white shark'    'tiger shark'

  Columns 5 through 9

    'hammerhead'    'electric ray'    'stingray'    'cock'    'hen'

  Column 10

    'ostrich'

Read, resize, and classify an image using AlexNet. First, load a pretrained AlexNet model.

net = alexnet;

Read the image using imread.

I = imread('peppers.png');
figure
imshow(I)

The pretrained model requires the image size to be the same as the input size of the network. Determine the input size of the network using the InputSize property of the first layer of the network.

sz = net.Layers(1).InputSize
sz =

   227   227     3

Crop the image to the input size of the network. Alternatively, you can resize the image using imresize (Image Processing Toolbox™).

I = I(1:sz(1),1:sz(2),1:sz(3));
figure
imshow(I)

Classify the image using classify.

label = classify(net,I)
label = 

  categorical

     bell pepper 

Show the image and classification result together.

figure
imshow(I)
title(char(label))

Extract learned features from a pretrained network, and use those features to train a classifier. Feature extraction is the easiest and fastest way use the representational power of pretrained deep networks. For example, you can train a support vector machine (SVM) using fitcecoc (Statistics and Machine Learning Toolbox™) on the extracted features.

Load a pretrained AlexNet network.

net = alexnet;

Load the sample images. merchImagesTrain and merchImagesTest are ImageDatastore objects.

[merchImagesTrain,merchImagesTest] = merchData;

In the sample data, there are 60 training images and 15 test images. Display 20 sample images.

figure
for i = 1:20
    subplot(4,5,i)

    I = readimage(merchImagesTrain,i);
    imshow(I)
    drawnow
end

If the training images differ in size to the image input layer, then you must resize or crop the image data. The images in merchImages are the same size as the input size of AlexNet, so you do not need to resize or crop the new image data.

Get the feature representations of the training and test images. Use activations on the last fully connected layer ('fc7') with the image data.

layer = 'fc7';
trainingFeatures = activations(net,merchImagesTrain,layer);
testFeatures = activations(net,merchImagesTest,layer);

Extract the class labels from the training and test data.

trainingLabels = merchImagesTrain.Labels;
testLabels = merchImagesTest.Labels;

Fit a multiclass support vector machine (SVM) using fitcecoc (Statistics and Machine Learning Toolbox).

classifier = fitcecoc(trainingFeatures,trainingLabels);

Classify the test images using predict.

predictedLabels = predict(classifier,testFeatures);

Display four sample test images with their predicted labels.

idx = [1 4 7 10];
figure
for i = 1:numel(idx)
    subplot(2,2,i)

    I = readimage(merchImagesTest,idx(i));
    label = predictedLabels(idx(i));

    imshow(I)
    title(char(label))
    drawnow
end

Calculate the classification accuracy.

accuracy = sum(predictedLabels==testLabels)/numel(predictedLabels)
accuracy =

    0.9333

This example SVM has high accuracy. If the accuracy is not high enough using feature extraction, the try transfer learning instead.

Fine-tune a pretrained convolutional neural network to learn the features on a new collection of images.

Transfer learning is commonly used in deep learning applications. You can take a pretrained network and use it as a starting point to learn a new task. Fine-tuning a network with transfer learning is much faster and easier than training from scratch. You can quickly transfer learning to a new task using a smaller number of training images.

Load the sample images as ImageDatastore objects.

[merchImagesTrain,merchImagesTest] = merchData;

In the sample data, there are 60 training images and 15 test images. Display 20 sample images.

figure
for i = 1:20
    subplot(4,5,i)

    I = readimage(merchImagesTrain,i);
    imshow(I)
    drawnow
end

Load a pretrained AlexNet network.

net = alexnet;

The last three layers of the pretrained network net are configured for 1000 classes. These three layers must be fine-tuned for the new classification problem. Extract all the layers except the last three from the pretrained network, net.

layersTransfer = net.Layers(1:end-3);

Transfer the layers to the new task by replacing the last three layers with a fully connected layer, a softmax layer, and a classification output layer. Specify the options of the new fully connected layer according to the new data. Set the fully connected layer to be of the same size as the number of classes in the new data. To speed up training, also increase 'WeightLearnRateFactor' and 'BiasLearnRateFactor' values in the fully connected layer.

Determine the number of classes from the training data.

numClasses = numel(categories(merchImagesTrain.Labels))
numClasses =

     5

Create the layer array by combining the transferred layers with the new layers.

layers = [...
    layersTransfer
    fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
    softmaxLayer
    classificationLayer];

If the training images differ in size from the image input layer, then you must resize or crop the image data. The images in merchImages are the same size as the input size of AlexNet, so you do not need to resize or crop the new image data.

Create the training options. For transfer learning, you want to keep the features from the early layers of the pretrained network (the transferred layer weights). Set 'InitialLearnRate' to a low value. This low initial learn rate slows down learning on the transferred layers. In the previous step, you set the learn rate factors for the fully connected layer higher to speed up learning on the new final layers. This combination results in fast learning only on the new layers, while keeping the other layers fixed. When performing transfer learning, you do not need to train for as many epochs. To speed up training, you can reduce the value of the 'MaxEpochs' name-value pair argument in the call to trainingOptions. To reduce memory usage, reduce 'MiniBatchSize'.

options = trainingOptions('sgdm',...
    'MiniBatchSize',5,...
    'MaxEpochs',10,...
    'InitialLearnRate',0.0001);

Fine-tune the network using trainNetwork on the new layer array.

netTransfer = trainNetwork(merchImagesTrain,layers,options);
Training on single GPU.
Initializing image normalization.
|=========================================================================================|
|     Epoch    |   Iteration  | Time Elapsed |  Mini-batch  |  Mini-batch  | Base Learning|
|              |              |  (seconds)   |     Loss     |   Accuracy   |     Rate     |
|=========================================================================================|
|            1 |            1 |         0.55 |       2.4120 |       20.00% |       0.0001 |
|            5 |           50 |        18.28 |       0.0002 |      100.00% |       0.0001 |
|            9 |          100 |        34.82 |      -0.0000 |      100.00% |       0.0001 |
|           10 |          120 |        41.36 |       0.0001 |      100.00% |       0.0001 |
|=========================================================================================|

Classify the test images using classify.

predictedLabels = classify(netTransfer,merchImagesTest);

Display four sample test images with their predicted labels.

idx = [1 4 7 10];
figure
for i = 1:numel(idx)
    subplot(2,2,i)

    I = readimage(merchImagesTest,idx(i));
    label = predictedLabels(idx(i));

    imshow(I)
    title(char(label))
    drawnow
end

Calculate the classification accuracy.

testLabels = merchImagesTest.Labels;

accuracy = sum(predictedLabels==testLabels)/numel(predictedLabels)
accuracy =

    0.8667

This example has high accuracy. If the accuracy is not high enough using transfer learning, try feature extraction instead.

Output Arguments

collapse all

Pretrained AlexNet convolutional neural network returned as a SeriesNetwork object.

References

[1] ImageNet. http://www.image-net.org

[2] Russakovsky, O., Deng, J., Su, H., et al. "ImageNet Large Scale Visual Recognition Challenge." International Journal of Computer Vision (IJCV). Vol 115, Issue 3, 2015, pp. 211–252

[3] Krizhevsky, Alex, Ilya Sutskever, and Geoffrey E. Hinton. "ImageNet Classification with Deep Convolutional Neural Networks." Advances in neural information processing systems. 2012.

[4] BVLC AlexNet Model. https://github.com/BVLC/caffe/tree/master/models/bvlc_alexnet

Introduced in R2017a

Was this topic helpful?