code generation for image classification -凯发k8网页登录
this example shows how to generate c code from a matlab® function that classifies images of digits using a trained classification model. this example demonstrates an alternative workflow to (computer vision toolbox). however, to support code generation in that example, you can follow the code generation steps in this example.
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 use mex
-setup
to view and change the default compiler. for more details, see change 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, see introduction 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 to savelearnerforcoder
. the 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 using loadlearnerforcoder
. the 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, to predict
.
code generation for classification workflow
before deploying an image classifier onto a device:
obtain a sufficient amount of labeled images.
decide which features to extract from the images.
train and optimize a classification model. this step includes choosing an appropriate algorithm and tuning hyperparameters, that is, model parameters not fit during training.
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.set up your c compiler.
decide the environment in which to execute the generated code.
generate c code for the function.
load data
load the digitimages
data set.
load digitimages
images
is a 28-by-28-by-3000 array of uint16
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 vector y
. for more details, enter description
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) % for reproducibility 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.
figure for j = 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); for i = 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 using mat2gray
. for more details, see (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. because reshape
takes elements column-wise, you must transpose its result.
x = reshape(x,[p,n])';
to ensure that preprocessing the data maintains the image, plot the first observation in x
.
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 (computer vision toolbox). for details on other supported techniques, see local 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.
you can create a multiclass model of multiple binary svm learners using 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, see bayesian optimization workflow.) this example performs cross-validation over a chosen grid of values.
to cross-validate an ecoc model of svm binary learners based on the training observations, use 5-fold cross-validation. although the predictor values have the same range, to avoid numerical difficulties during training, standardize the predictors. also, optimize the ecoc coding design and the svm box constraint. use all combinations of these values:
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)); % for preallocation for i = 1:numel(coding) for j = 1:numel(boxconstraint) t = templatesvm('boxconstraint',boxconstraint(j),'standardize',true); cvmdl = fitcecoc(x(idxtrn,:),y(idxtrn),'learners',t,'kfold',5,... 'coding',coding{i}); cvloss(i,j) = kfoldloss(cvmdl); fprintf('cvloss = %f for model using %s coding and box constraint=%f\n',... cvloss(i,j),coding{i},boxconstraint(j)) end end
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
determine the hyperparameter indices that yield 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,'standardize',true); mdl = fitcecoc(x(idxtrn,:),y(idxtrn),'learners',t,'coding',bestcoding);
construct a confusion matrix for the test set images.
testimages = x(idxtest,:); testlabels = predict(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 of mdl
, 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.
save classification model to disk
mdl
is a predictive classification model, but you must prepare it for code generation. save mdl
to your present working directory using savelearnerforcoder
.
savelearnerforcoder(mdl,'digitimagesecoc')
savelearnerforcoder
compacts mdl
, converts it to a structure array, and saves it in the mat-file digitimagesecoc.mat
.
define prediction function for code generation
define an entry-point function named predictdigitecoc.m
that does the following:
include the code generation directive
%#codegen
somewhere in the function.accept image data commensurate with
x
.load
digitimagesecoc.mat
usingloadlearnerforcoder
.return predicted labels.
type predictdigitecoc.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 as predict
.
pflabels = predictdigitecoc(testimages); verifypf = isequal(pflabels,testlabels)
verifypf = logical
1
isequal
returns logical 1 (true
), which means all the inputs are equal. the predictdigitecoc
yields the expected results.
decide which environment to execute generated code
generated code can run:
inside the matlab environment as a c-mex file
outside the matlab environment as a standalone executable
outside the matlab environment as a shared utility linked to another standalone executable
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 using (matlab coder). extrinsic commands can include functions that do not have code generation support. all extrinsic commands in the mex function run in matlab, but codegen
does not generate code for them.
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 of codegen
. for example, to generate a static c executable, specify -config:exe
when you call codegen
. for more details on setting code generation options, see the -config
option of codegen
(matlab coder).
compile matlab function to mex file
compile predictdigitecoc.m
to a mex file using 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
.
codegen predictdigitecoc -report -args {testimages}
code generation successful: view report
codegen
successfully generated the code for the prediction function. you can view the report by clicking the view report
link or by entering open('codegen/mex/predictdigitecoc/html/report.mldatx')
in the command window. if code generation is unsuccessful, then the report can help you debug.
codegen
creates the directory pwd/codegen/mex/predictdigitecoc
, where pwd
is your present working directory. in the child directory, codegen
generates, among other things, the mex-file predictdigitecoc_mex.mexw64
.
verify that the mex file returns the same labels as predict
.
mexlabels = predictdigitecoc_mex(testimages); verifymex = isequal(mexlabels,testlabels)
verifymex = logical
1
isequal
returns logical 1 (true
), meaning that the mex-file yields the expected results.
see also
savelearnerforcoder
| loadlearnerforcoder
| predict
| codegen
(matlab coder)