主要内容

Extract Image Features Using Pretrained Network

This example shows how to extract learned image features from a pretrained convolutional neural network and use those features to train an image classifier. Feature extraction is the easiest and fastest way to use the representational power of pretrained deep networks. For example, you can train a support vector machine (SVM) usingfitcecoc(Statistics and Machine Learning Toolbox™) on the extracted features. Because feature extraction only requires a single pass through the data, it is a good starting point if you do not have a GPU to accelerate network training with.

Load Data

Unzip and load the sample images as an image datastore.成像automatically labels the images based on folder names and stores the data as anImageDatastoreobject. An image datastore lets you store large image data, including data that does not fit in memory. Split the data into 70% training and 30% test data.

unzip('merchdata.zip');imds = imagedatastore('MerchData',“包括橡皮folders”,true,“ Labelsource”,'foldernames');[imdsTrain,imdsTest] = splitEachLabel(imds,0.7,“随机”);

现在,在这个很小的数据集中,现在有55张培训图像和20个验证图像。显示一些示例图像。

numTrainImages = numel(imdstrain.labels);idx = randperm(numTrainimages,16);数字fori = 1:16 subplot(4,4,i) I = readimage(imdsTrain,idx(i)); imshow(I)end

图包含16个轴对象。轴对象1包含类型图像的对象。轴对象2包含类型图像的对象。轴对象3包含类型图像的对象。轴对象4包含类型图像的对象。轴对象5包含类型图像的对象。轴对象6包含类型图像的对象。轴对象7包含类型图像的对象。轴对象8包含类型图像的对象。轴对象9包含类型图像的对象。 Axes object 10 contains an object of type image. Axes object 11 contains an object of type image. Axes object 12 contains an object of type image. Axes object 13 contains an object of type image. Axes object 14 contains an object of type image. Axes object 15 contains an object of type image. Axes object 16 contains an object of type image.

负载预估计的网络

加载预处理的RESNET-18网络。如果深度学习工具箱模型for ResNet-18 Network金宝app支持软件包未安装,然后该软件提供了下载链接。RESNET-18经过一百万张图像的培训,可以将图像分类为1000个对象类别,例如键盘,鼠标,铅笔和许多动物。结果,该模型已经学习了广泛图像的丰富特征表示。

net = resnet18
net =带有属性的dagnetwork:layers:[71x1 nnet.cnn.layer.layer] connections:[78x2 table] inputNames:{'data'} outputnames:{'classification layer_predictions'}

分析网络体系结构。第一层是图像输入层,需要大小224 by-224 by-3的输入图像,其中3是颜色通道的数量。

inputsize = net.layers(1).inputsize;分析(NET)

Extract Image Features

The network requires input images of size 224-by-224-by-3, but the images in the image datastores have different sizes. To automatically resize the training and test images before they are input to the network, create augmented image datastores, specify the desired image size, and use these datastores as input arguments to激活

augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain); augimdsTest = augmentedImageDatastore(inputSize(1:2),imdsTest);

网络结构层次representation of input images. Deeper layers contain higher-level features, constructed using the lower-level features of earlier layers. To get the feature representations of the training and test images, use激活在全球合并层上,'pool5',at the end of the network. The global pooling layer pools the input features over all spatial locations, giving 512 features in total.

layer ='pool5'; featuresTrain = activations(net,augimdsTrain,layer,'outputas','rows');featuresTest = activations(net,augimdsTest,layer,'outputas','rows');whos特征
Name Size Bytes Class Attributes featuresTrain 55x512 112640 single

Extract the class labels from the training and test data.

YTrain = imdsTrain.Labels; YTest = imdsTest.Labels;

Fit Image Classifier

Use the features extracted from the training images as predictor variables and fit a multiclass support vector machine (SVM) usingfitcecoc(统计和机器学习工具箱)。

classifier = fitcecoc(featurantain,ytrain);

分类测试图像

Classify the test images using the trained SVM model using the features extracted from the test images.

ypred =预测(分类器,特征最佳);

显示四个带有预测标签的样本测试图像。

IDX = [1 5 10 15];数字fori = 1:numel(idx) subplot(2,2,i) I = readimage(imdsTest,idx(i)); label = YPred(idx(i)); imshow(I) title(char(label))end

图包含4个轴对象。带有标题的轴对象1 Mathworks Cap包含类型图像的对象。轴对象2带标题Mathworks Cube包含类型图像的对象。轴对象3带有标题数学工程纸牌包含类型图像的对象。轴对象4带有标题的Mathworks螺丝刀包含类型图像的对象。

计算测试集上的分类精度。精度是网络正确预测的标签的比例。

精度=意味着(YPred = =次)
accuracy = 1

Train Classifier on Shallower Features

You can also extract features from an earlier layer in the network and train a classifier on those features. Earlier layers typically extract fewer, shallower features, have higher spatial resolution, and a larger total number of activations. Extract the features from the'res3b_relu'层。这是输出128个功能的最后一层,并且激活的空间大小为28 x-28。

layer ='res3b_relu'; featuresTrain = activations(net,augimdsTrain,layer); featuresTest = activations(net,augimdsTest,layer); whos特征
名称大小字节类属性featurantain 28x28x128x55 22077440单

本示例第一部分中使用的提取功能通过全局池层在所有空间位置汇总。为了在早期层中提取特征时达到相同的结果,请在所有空间位置手动平均激活。要在表单上获取功能N-经过-C, whereN是观察的数量和Cis the number of features, remove the singleton dimensions and transpose.

特征= squeeze(mean(featuresTrain,[1 2]))'; featuresTest = squeeze(mean(featuresTest,[1 2]))'; whos特征
名称大小字节类属性特征术55x128 28160单

在较浅的功能上训练SVM分类器。计算测试精度。

classifier = fitcecoc(featurantain,ytrain);ypred =预测(分类器,特征最佳);精度=意味着(YPred = =次)
精度= 0.9500

Both trained SVMs have high accuracies. If the accuracy is not high enough using feature extraction, then try transfer learning instead. For an example, see训练深度学习网络以对新图像进行分类。有关验证网络的列表和比较,请参见预处理的深神经网络

See Also

(Statistics and Machine Learning Toolbox)|

Related Topics