Import the squeezenet
convolution neural network as a function and fine-tune the pretrained network with transfer learning to perform classification on a new collection of images.
This example uses several helper functions. To view the code for these functions, see Helper Functions.
Unzip and load the new images as an image datastore. imageDatastore
automatically labels the images based on folder names and stores the data as an ImageDatastore
object. An image datastore enables you to store large image data, including data that does not fit in memory, and efficiently read batches of images during training of a convolutional neural network. Specify the mini-batch size.
This data set is small, containing 75 training images. Display some sample images.
Extract the training set and one-hot encode the categorical classification labels.
Determine the number of classes in the data.
squeezenet
is a convolutional neural network that is trained on more than a million images from the ImageNet database. As a result, the network has learned rich feature representations for a wide range of images. The network can classify images into 1000 object categories, such as keyboard, mouse, pencil, and many animals.
Import the pretrained squeezenet
network as a function.
A function containing the imported ONNX network has been saved to the file squeezenetFcn.m.
To learn how to use this function, type: help squeezenetFcn.
params =
ONNXParameters with properties:
Learnables: [1×1 struct]
Nonlearnables: [1×1 struct]
State: [1×1 struct]
NumDimensions: [1×1 struct]
NetworkFunctionName: 'squeezenetFcn'
params
is an ONNXParameters
object that contains the network parameters. squeezenetFcn
is a model function that contains the network architecture. importONNXFunction
saves squeezenetFcn
in the current folder.
Calculate the classification accuracy of the pretrained network on the new training set.
0.01 accuracy before transfer learning
The accuracy is very low.
Display the learnable parameters of the network by typing params.Learnables
. These parameters, such as the weights (W
) and bias (B
) of convolution and fully connected layers, are updated by the network during training. Nonlearnable parameters remain constant during training.
The last two learnable parameters of the pretrained network are configured for 1000 classes.
conv10_W: [1×1×512×1000 dlarray]
conv10_B: [1000×1 dlarray]
The parameters conv10_W
and conv10_B
must be fine-tuned for the new classification problem. Transfer the parameters to classify five classes by initializing the parameters.
Freeze all the parameters of the network to convert them to nonlearnable parameters. Because you do not need to compute the gradients of the frozen layers, freezing the weights of many initial layers can significantly speed up network training.
Unfreeze the last two parameters of the network to convert them to learnable parameters.
Now the network is ready for training. Initialize the training progress plot.
Specify the training options.
Train the network.
Calculate the classification accuracy of the network after fine-tuning.
1.00 accuracy after transfer learning
Helper Functions
This section provides the code of the helper functions used in this example.
The getNetworkAccuracy
function evaluates the network performance by calculating the classification accuracy.
The modelGradients
function calculates the loss and gradients.
The squeezenetONNX
function generates an ONNX model of the squeezenet
network.