Code Generation for Image Classification
此示例显示了如何从MATLAB®功能生成C代码,该函数使用培训的分类模型分类数字图像。此示例演示了替代工作流程数字Classification Using HOG Features(Computer Vision Toolbox)。但是,要支持该示例中的代金宝app码生成,可以遵循此示例中的代码生成步骤。
Automated image classification is an ubiquitous tool. For example, a trained classifier can be deployed to a drone to automatically identify anomalies on land in captured footage, or to a machine that scans handwritten zip codes on letters. In the latter example, after the machine finds the ZIP code and stores individual images of digits, the deployed classifier must guess which digits are in the images to reconstruct the ZIP code.
This example shows how to train and optimize a multiclass error-correcting output codes (ECOC) classification model to classify digits based on pixel intensities in raster images. The ECOC model contains binary support vector machine (SVM) learners. Then, this example shows how to generate C code that uses the trained model to classify new images. The data are synthetic images of warped digits of various fonts, which simulates handwritten digits.
Set Up Your C Compiler
To generate C/C++ code, you must have access to a C/C++ compiler that is configured properly. MATLAB Coder™ locates and uses a supported, installed compiler. You can usemex
-setup
to view and change the default compiler. For more details, seeChange Default Compiler。
Assumptions and Limitations
To generate C code, MATLAB Coder:
Requires a properly configured compiler.
Requires supported functions to be in a MATLAB function that you define. For the basic workflow, seeIntroduction to Code Generation。
Forbids objects as input arguments of the defined function.
Concerning the last limitation, consider that:
Trained classification models are objects
MATLAB Coder supports
predict
to classify observations using trained models, but does not support fitting the model
To work around the code generation limitations for classification, train the classification model using MATLAB, then pass the resulting model object tosaveLearnerForCoder
。这saveLearnerForCoder
function removes some properties that are not required for prediction, and then saves the trained model to disk as a structure array. Like the model, the structure array contains the information used to classify new observations.
After saving the model to disk, load the model in the MATLAB function by usingloadLearnerForCoder
。这loadLearnerForCoder
function loads the saved structure array, and then reconstructs the model object. In the MATLAB function, to classify the observations, you can pass the model and predictor data set, which can be an input argument of the function, topredict
。
Code Generation for Classification Workflow
在将图像分类部署到设备上之前:
Obtain a sufficient amount of labeled images.
Decide which features to extract from the images.
火车并优化分类模型。此步骤包括选择适当的算法和调整超参数,即型号参数在训练期间不适合。
Save the model to disk by using
saveLearnerForCoder
。Define a function for classifying new images. The function must load the model by using
loadLearnerForCoder
, and can return labels, such as classification scores.设置您的C编译器。
Decide the environment in which to execute the generated code.
Generate C code for the function.
Load Data
加载digitimages
data set.
loaddigitimages
images
is a 28-by-28-by-3000 array ofuint16
integers. Each page is a raster image of a digit. Each element is a pixel intensity. Corresponding labels are in the 3000-by-1 numeric vectorY
。For more details, enterDescription
at the command line.
Store the number of observations and number of predictor variables. Create a data partition that specifies to hold out 20% of the data. Extract training and test set indices from the data partition.
rng(1)再现性的百分比n = size(images,3); p = numel(images(:,:,1)); cvp = cvpartition(n,'Holdout',0.20); idxTrn = training(cvp); idxTest = test(cvp);
Display nine random images from the data.
figureforj = 1:9 subplot(3,3,j) selectImage = datasample(images,1,3); imshow(selectImage,[])end
Rescale Data
Because raw pixel intensities vary widely, you should normalize their values before training a classification model. Rescale the pixel intensities so that they range in the interval [0,1]. That is, suppose is pixel intensity within image 。For image , rescale all of its pixel intensities using this formula:
X = double(images);fori = 1:n minX = min(min(X(:,:,i))); maxX = max(max(X(:,:,i))); X(:,:,i) = (X(:,:,i) - minX)/(maxX - minX);end
Alternatively, if you have an Image Processing Toolbox™ license, then you can efficiently rescale pixel intensities of images to [0,1] by usingmat2gray
。For more details, seemat2gray
(Image Processing Toolbox)。
Reshape Data
For code generation, the predictor data for training must be in a table of numeric variables or a numeric matrix.
Reshape the data to a matrix such that predictor variables (pixel intensities) correspond to columns, and images (observations) to rows. Becausereshape
需要元素列,您必须转换结果。
X = reshape(X,[p,n])';
To ensure that preprocessing the data maintains the image, plot the first observation inX
。
figure imshow(reshape(X(1,:),sqrt(p)*[1 1]),[],'InitialMagnification','fit')
Extract Features
Computer Vision Toolbox™ offers several feature-extraction techniques for images. One such technique is the extraction of histogram of oriented gradient (HOG) features. To learn how to train an ECOC model using HOG features, see数字Classification Using HOG Features(Computer Vision Toolbox)。For details on other supported techniques, seeLocal Feature Detection and Extraction(Computer Vision Toolbox)。This example uses the rescaled pixel intensities as predictor variables.
Train and Optimize Classification Model
Linear SVM models are often applied to image data sets for classification. However, SVM are binary classifiers, and there are 10 possible classes in the data set.
您可以使用多种二进制SVM学习者创建多种多组模型fitcecoc
。fitcecoc
combines multiple binary learners using a coding design. By default,fitcecoc
applies the one-versus-one design, which specifies training binary learners based on observations from all combinations of pairs of classes. For example, in a problem with 10 classes,fitcecoc
must train 45 binary SVM models.
In general, when you train a classification model, you should tune the hyperparameters until you achieve a satisfactory generalization error. That is, you should cross-validate models for particular sets of hyperparameters, and then compare the out-of-fold misclassification rates.
You can choose your own sets of hyperparameter values, or you can specify to implement Bayesian optimization. (For general details on Bayesian optimization, seeBayesian Optimization Workflow。) This example performs cross-validation over a chosen grid of values.
要根据培训观察跨越SVM二进制学习者的ECOC模型,使用5倍交叉验证。虽然预测值值具有相同的范围,但是为了避免在训练期间的数值困难,标准化预测器。此外,优化ECOC编码设计和SVM框约束。使用这些值的所有组合:
For the ECOC coding design, use one-versus-one and one-versus-all.
For the SVM box constraint, use three logarithmically-spaced values from 0.1 to 100 each.
For all models, store the 5-fold cross-validated misclassification rates.
coding = {'onevsone''onevsall'}; boxconstraint = logspace(-1,2,3); cvLoss = nan(numel(coding),numel(boxconstraint));预付款的百分比fori = 1:numel(coding)forj = 1:numel(boxconstraint) t = templateSVM('BoxConstraint',boxconstraint(j),'标准化',真的);cvmdl = fitcecoc(x(idxtrn,:),y(idxtrn),'Learners',t,'KFold',5,。。。'Coding',coding{i}); cvLoss(i,j) = kfoldLoss(CVMdl); fprintf('cvloss =模型使用%s编码和框约束=%f \ n',。。。cvloss(i,j),编码{i},boxconstraint(j))endend
cvLoss = 0.052083 for model using onevsone coding and box constraint=0.100000 cvLoss = 0.055000 for model using onevsone coding and box constraint=3.162278 cvLoss = 0.050000 for model using onevsone coding and box constraint=100.000000 cvLoss = 0.116667 for model using onevsall coding and box constraint=0.100000 cvLoss = 0.123750 for model using onevsall coding and box constraint=3.162278 cvLoss = 0.125000 for model using onevsall coding and box constraint=100.000000
确定hyperparameter指数收益率the minimal misclassification rate. Train an ECOC model using the training data. Standardize the training data and supply the observed, optimal hyperparameter combination.
mincvloss = min(cvloss(:))
minCVLoss = 0.0500
linIdx = find(cvLoss == minCVLoss); [bestI,bestJ] = ind2sub(size(cvLoss),linIdx); bestCoding = coding{bestI}
bestCoding = 'onevsone'
bestboxconstraint = boxconstraint(bestj)
BestboxConstraint = 100.
t = templateSVM('BoxConstraint',bestBoxConstraint,'标准化',真的);Mdl = fitcecoc(X(idxTrn,:),Y(idxTrn),'Learners',t,'Coding',bestCoding);
Construct a confusion matrix for the test set images.
testimages = x(iDxtest,:);testLabels =预测(MDL,Testimages);confusionmatrix = confusionchart(y(iDxtest),testlabels);
Diagonal and off-diagonal elements correspond to correctly and incorrectly classified observations, respectively.Mdl
seems to correctly classify most images.
If you are satisfied with the performance ofMdl
, then you can proceed to generate code for prediction. Otherwise, you can continue adjusting hyperparameters. For example, you can try training the SVM learners using different kernel functions.
分类模型保存到磁盘
Mdl
is a predictive classification model, but you must prepare it for code generation. SaveMdl
to your present working directory usingsaveLearnerForCoder
。
saveLearnerForCoder(Mdl,'digitimagesecoc')
saveLearnerForCoder
compactsMdl
, converts it to a structure array, and saves it in the MAT-fileDigitImagesECOC.mat
。
Define Prediction Function for Code Generation
Define an entry-point function namedpredictDigitECOC.m
that does the following:
Include the code generation directive
%#codegen
在功能的某个地方。Accept image data commensurate with
X
。Load
DigitImagesECOC.mat
usingloadLearnerForCoder
。Return predicted labels.
typepredictDigitECOC.m% Display contents of predictDigitECOC.m file
function label = predictDigitECOC(X) %#codegen %PREDICTDIGITECOC Classify digit in image using ECOC Model % PREDICTDIGITECOC classifies the 28-by-28 images in the rows of X using % the compact ECOC model in the file DigitImagesECOC.mat, and then % returns class labels in label. CompactMdl = loadLearnerForCoder('DigitImagesECOC.mat'); label = predict(CompactMdl,X); end
Note:If you click the button located in the upper-right section of this page and open this example in MATLAB, then MATLAB opens the example folder. This folder includes the entry-point function file.
Verify that the prediction function returns the same test set labels aspredict
。
pfLabels = predictDigitECOC(testImages); verifyPF = isequal(pfLabels,testLabels)
verifyPF =logical1
isequal
返回逻辑1(true
), which means all the inputs are equal. ThepredictDigitECOC
yields the expected results.
确定要执行生成的代码的环境
Generated code can run:
Inside the MATLAB environment as a C-MEX file
Outside the MATLAB environment as a standalone executable
在MATLAB环境之外作为与另一个独立可执行文件相关的共享实用程序
This example generates a MEX file to be run in the MATLAB environment. Generating such a MEX file allows you to test the generated code using MATLAB tools before deploying the function outside the MATLAB environment. In the MEX function, you can include code for verification, but not for code generation, by declaring the commands as extrinsic usingcoder.extrinsic
(MATLAB Coder)。Extrinsic commands can include functions that do not have code generation support. All extrinsic commands in the MEX function run in MATLAB, butcodegen
不会为它们生成代码。
If you plan to deploy the code outside the MATLAB environment, then you must generate a standalone executable. One way to specify your compiler choice is by using the-config
option ofcodegen
。For example, to generate a static C executable, specify-config:exe
你打电话的时候codegen
。For more details on setting code generation options, see the-config
option ofcodegen
(MATLAB Coder)。
Compile MATLAB Function to MEX File
CompilepredictDigitECOC.m
使用MEX文件codegen
。Specify these options:
-report
— Generates a compilation report that identifies the original MATLAB code and the associated files thatcodegen
creates during code generation.-args
— MATLAB Coder requires that you specify the properties of all the function input arguments. One way to do this is to providecodegen
with an example of input values. Consequently, MATLAB Coder infers the properties from the example values. Specify the test set images commensurate withX
。
codegenpredictDigitECOC-report-args{testImages}
Code generation successful: View report
codegen
successfully generated the code for the prediction function. You can view the report by clicking theView report
link or by enteringopen('codegen/mex/predictDigitECOC/html/report.mldatx')
在命令窗口中。如果代码生成不成功,那么报告可以帮助您调试。
codegen
creates the directorypwd/codegen/mex/predictDigitECOC
, wherepwd
is your present working directory. In the child directory,codegen
generates, among other things, the MEX-filepredictDigitECOC_mex.mexw64
。
Verify that the MEX file returns the same labels aspredict
。
mexLabels = predictDigitECOC_mex(testImages); verifyMEX = isequal(mexLabels,testLabels)
验证ex =logical1
isequal
返回逻辑1(true
), meaning that the MEX-file yields the expected results.
See Also
saveLearnerForCoder
|loadLearnerForCoder
|predict
|codegen
(MATLAB Coder)