Main Content

resnet18

(Not recommended) ResNet-18 convolutional neural network

  • ResNet-18 network architecture

resnet18 is not recommended. Use the imagePretrainedNetwork function instead and specify the "resnet18" model. For more information, see Version History.

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.

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.

example

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.

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 2016 IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 770–78. Las Vegas, NV, USA: IEEE, 2016. https://doi.org/10.1109/CVPR.2016.90.

Extended Capabilities

Version History

Introduced in R2018a

collapse all

R2024a: Not Recommended

resnet18 is not recommended. Use the imagePretrainedNetwork function instead and specify "resnet18" as the model.

There are no plans to remove support for the resnet18 function. However, the imagePretrainedNetwork function has additional functionality that helps with transfer learning workflows. For example, you can specify the number of classes in your data using the numClasses option, and the function returns a network that is ready for retraining without the need for modification.

The imagePretrainedNetwork function returns the network as a dlnetwork object, which does not store the class names, To get the class names of the pretrained network, use the second output argument of the imagePretrainedNetwork function.

This table shows some typical usages of the resnet18 function and how to update your code to use the imagePretrainedNetwork function instead.

Not RecommendedRecommended
net = resnet18;[net,classNames] = imagePretrainedNetwork("resnet18");
net = resnet18(Weights="none");net = imagePretrainedNetwork("resnet18",Weights="none");

The imagePretrainedNetwork returns a dlnetwork object, which also has these advantages:

  • dlnetwork objects are a unified data type that supports network building, prediction, built-in training, visualization, compression, verification, and custom training loops.

  • dlnetwork objects support a wider range of network architectures that you can create or import from external platforms.

  • The trainnet function supports dlnetwork objects, which enables you to easily specify loss functions. You can select from built-in loss functions or specify a custom loss function.

  • Training and prediction with dlnetwork objects is typically faster than LayerGraph and trainNetwork workflows.

To train a neural network specified as a dlnetwork object, use the trainnet function.