Load a pretrained fcddAnomalyDetector
object from a MAT file. This detector was trained on grayscale images of size 28-by-28.
Create an entry-point function myFCDD
that accepts an image as input. The entry-point function performs these operations:
Define a persistent variable called myDetector
. The persistent variable prevents reconstructing and reloading the network object during subsequent calls to the myFCDD
function.
Load the detector in the file digit8AnomalyDetector.mat
into the myDetector
variable using the vision.loadFCDDAnomalyDetector
function.
Perform operations, such as prediction and classification, on the input image using the detector.
Show the contents of the entry-point function. This function is attached to the example as a supporting file.
function [score,label] = myFCDD(in)
persistent myDetector;
if isempty(myDetector)
myDetector = vision.loadFCDDAnomalyDetector("digit8AnomalyDetector.mat");
end
score = predict(myDetector,in);
if score < myDetector.Threshold
label = "normal";
else
label = "anomaly";
end
end
Specify the size of the training images as the size of the input to the entry-point function.
Create a coder.config
configuration object for MEX code generation and set the target language to C. On the configuration object, set the DeepLearningConfig
property with no target library. The codegen
function must determine the size, class, and complexity of MATLAB function inputs. Use the -args
option to specify the size of the input to the entry-point function. Use the -config
option to pass the code configuration object.
Code generation successful: To view the report, open('codegen/mex/myFCDD/html/report.mldatx')
The codegen
command places all the generated files in the codegen
folder. The folder contains the C code for the entry-point function myFCDD.c
, header and source files containing the C class definitions for the convoluted neural network (CNN), weight, and bias files.
Load the test data. The test images must be the same size as the training images.
Select a test image. Convert the image to data type single
, as expected by the generated code.
Predict the anomaly score and label of the test image by calling the generated code.
predScore = single
0.0107