Main Content

resnet18

ResNet-18 convolutional neural network

  • ResNet-18 network architecture

Description

ResNet-18 is a convolutional neural network that is 18 layers deep. You can load a pretrained version of the network trained on more than a million images from the ImageNet database [1]. The pretrained network can classify images into 1000 object categories, such as keyboard, mouse, pencil, and many animals. As a result, the network has learned rich feature representations for a wide range of images. The network has an image input size of 224-by-224. For more pretrained networks in MATLAB®, see Pretrained Deep Neural Networks.

You can use classify to classify new images using the ResNet-18 model. Follow the steps of Classify Image Using GoogLeNet and replace GoogLeNet with ResNet-18.

To retrain the network on a new classification task, follow the steps of Train Deep Learning Network to Classify New Images and load ResNet-18 instead of GoogLeNet.

Tip

To create an untrained residual network suitable for image classification tasks, use resnetLayers.

example

net = resnet18 returns a ResNet-18 network trained on the ImageNet data set.

This function requires the Deep Learning Toolbox™ Model for ResNet-18 Network support package. If this support package is not installed, then the function provides a download link.

net = resnet18('Weights','imagenet') returns a ResNet-18 network trained on the ImageNet data set. This syntax is equivalent to net = resnet18.

lgraph = resnet18('Weights','none') returns the untrained ResNet-18 network architecture. The untrained model does not require the support package.

Examples

collapse all

Download and install the Deep Learning Toolbox Model for ResNet-18 Network support package.

Type resnet18 at the command line.

resnet18

If the Deep Learning Toolbox Model for ResNet-18 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 resnet18 at the command line. If the required support package is installed, then the function returns a DAGNetwork object.

resnet18
ans = 

  DAGNetwork with properties:

         Layers: [72×1 nnet.cnn.layer.Layer]
    Connections: [79×2 table]

Visualize the network using Deep Network Designer.

deepNetworkDesigner(resnet18)

Explore other pretrained neural networks in Deep Network Designer by clicking New.

Deep Network Designer start page showing available pretrained neural networks

If you need to download a neural network, pause on the desired neural network and click Install to open the Add-On Explorer.

Load a pretrained ResNet-18 model.

net = resnet18;

Read a test image using imread.

I = imread("peppers.png");
imshow(I)

Figure contains an axes object. The axes object contains an object of type image.

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 = 1×3

   224   224     3

Resize the image to the input size of the network.

I = imresize(I,sz(1:2));

Classify the image using classify.

label = classify(net,I)
label = categorical
     bell pepper 

Show the image and classification result together.

imshow(I)
title(label)

Figure contains an axes object. The axes object with title bell pepper contains an object of type image.

In the workspace, extract the MathWorks Merch data set.

unzip("MerchData.zip");

Open Deep Network Designer to see available pretrained networks.

deepNetworkDesigner

Select ResNet-18 and click Open. If Deep Learning Toolbox™ Model for ResNet-18 Network is not installed, then the software opens the Add-On Explorer.

To load the data into Deep Network Designer, on the Data tab, click Import Data > Import Image Classification Data. In the Data source list, select Folder. Click Browse and select the extracted MerchData folder.

Divide the data into training and validation data sets. Use 70% of the images for training and 30% for validation. Select Randomize to randomly assign the specified proportion of images to the validation and training sets.

Click Import to import the data into Deep Network Designer.

The app shows a summary of the imported data.

To retrain a pretrained network to classify new images, adapt the last learnable layer and the final classification layer to the new data set.

In the Designer tab, click the final fully connected layer. At the bottom of the Properties pane, click Unlock Layer. In the warning dialog that appears, click Unlock Anyway. This unlocks the layer properties so that you can adapt them to your new task.

Before R2023b: To edit the layer properties, you must replace the layers instead of unlocking them.

Set the OutputSize to the number of classes in the new data, in this example, 5. Edit learning rates to learn faster in the new layers than in the transferred layers. Set WeightLearnRateFactor and BiasLearnRateFactor to 10.

Select the classification layer and click Unlock Layer and then click Unlock Anyway. For the unlocked output layer, you do not need to set the OutputSize. At training time, Deep Network Designer automatically sets the output classes of the layer from the data.

Specify training options. Select the Training tab and click Training Options. Set Solver to sgdm, InitialLearnRate to 0.0001, MiniBatchSize to 11, MaxEpochs to 8, and ValidationFrequency to 5.

To train the network with the specified training options, click OK and then click Train.

To export the network architecture with the trained weights, on the Training tab, select Export > Export Trained Network and Results. Deep Network Designer exports the trained network as the variable trainedNetwork_1.

Load a new image to classify using the trained network.

I = imread("MerchDataTest.jpg");

Deep Network Designer resizes the images during training to match the network input size. To view the network input size, go to the Designer pane and select the imageInputLayer (first layer). This network has an input size of 224-by-224.

Resize the test image to match the network input size.

I = imresize(I,[224 224]);

Classify the test image using the trained network.

YPred = classify(trainedNetwork_1,I);
imshow(I)
title("Predicted Class: " + string(YPred));

Load the pretrained ResNet-18 neural network and find the input size.

net = resnet18;
inputSize = net.Layers(1).InputSize;

Extract the MathWorks Merch data set. Load the new images as an image datastore.

unzip("MerchData.zip");
imds = imageDatastore("MerchData", ...
    IncludeSubfolders=true, ...
    LabelSource="foldernames");

numClasses = numel(categories(imds.Labels));

Divide the data into training and validation data sets. Specify "randomized" to randomly assign the specified proportion of images to the validation and training sets.

[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,"randomized");

Use an augmented image datastore to automatically resize the training images to match the input size of the network.

augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);

To retrain the pretrained network to classify new images, replace the last fully connected layer and the final classification layer with new layers adapted to the new data set. First, extract the layer graph from the trained network.

lgraph = layerGraph(net); 

Replace the fully connected layer with a new fully connected layer that has number of outputs equal to the number of classes. To make learning faster in the new layers than in the transferred layers, increase the WeightLearnRateFactor and BiasLearnRateFactor values of the fully connected layer.

newLearnableLayer = fullyConnectedLayer(numClasses, ...
    WeightLearnRateFactor=10, ...
    BiasLearnRateFactor=10);
    
lgraph = replaceLayer(lgraph,"fc1000",newLearnableLayer);

The classification layer specifies the output classes of the network. Replace the classification layer with a new layer without class labels. trainNetwork automatically sets the output classes of the layer at training time.

newClassLayer = classificationLayer;
lgraph = replaceLayer(lgraph,"ClassificationLayer_predictions",newClassLayer);

Specify the training options. To slow down learning in the transferred layers, set the initial learning rate to a small value.

options = trainingOptions("sgdm", ...
    InitialLearnRate=0.0001, ...
    MiniBatchSize=11, ...
    MaxEpochs=8, ...
    ValidationData=augimdsValidation, ...
    ValidationFrequency=5, ...
    Verbose=false, ...
    Plots="training-progress");

Train the network.

trainedNetwork = trainNetwork(augimdsTrain,lgraph,options);

Figure Training Progress (19-Aug-2023 11:40:06) contains 2 axes objects and another object of type uigridlayout. Axes object 1 with xlabel Iteration, ylabel Loss contains 15 objects of type patch, text, line. Axes object 2 with xlabel Iteration, ylabel Accuracy (%) contains 15 objects of type patch, text, line.

Load a new image to classify using the trained network.

I = imread("MerchDataTest.jpg");
I = imresize(I,inputSize(1:2));

Classify the test image.

YPred = classify(trainedNetwork,I);
imshow(I)
title("Predicted class: " + string(YPred));

Figure contains an axes object. The axes object with title Predicted class: MathWorks Cube contains an object of type image.

Output Arguments

collapse all

Pretrained ResNet-18 convolutional neural network, returned as a DAGNetwork object.

Untrained ResNet-18 convolutional neural network architecture, returned as a LayerGraph object.

References

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

[2] He, Kaiming, Xiangyu Zhang, Shaoqing Ren, and Jian Sun. "Deep residual learning for image recognition." In Proceedings of the IEEE conference on computer vision and pattern recognition, pp. 770-778. 2016.

Extended Capabilities

Version History

Introduced in R2018a