This example requires the Deep Learning Toolbox™ Verification Library support package. If this support package is not installed, use the Add-On Explorer. To open the Add-On Explorer, go to the MATLAB® Toolstrip and click Add-Ons > Get Add-Ons.
Load a trained dlnetwork
object.
Load in-distribution training data.
Create a distribution discriminator. You can use the discriminator to classify observations as in-distribution (ID) or out-of-distribution (OOD). OOD data refers to data that is sufficiently different from the data you use to train the network, which can cause the network to behave unexpectedly.
discriminator =
BaselineDistributionDiscriminator with properties:
Method: "baseline"
Network: [1×1 dlnetwork]
Threshold: 0.9269
Save the discriminator.
Create an entry-point function discriminatorEntryPoint
that accepts formatted dlarray
data. The entry-point function performs these operations:
Define a persistent variable called discriminator
. The persistent variable prevents reconstructing and reloading the object during subsequent calls to the discriminatorEntryPoint
function.
Load the discriminator from the file discriminator.mat
file into the discriminator
variable using the coder.loadNetworkDistributionDiscriminator
function.
Calculate a distribution score using the distributionScores
function.
Return the distribution score as an output.
An entry-point function is provided with this example as a supporting file. To access the supporting file, open this example as a live script. Show the contents of the entry-point function.
function score = discriminatorEntryPoint(X)
persistent discriminator;
if isempty(discriminator)
discriminator = coder.loadNetworkDistributionDiscriminator("discriminator.mat");
end
score = distributionScores(discriminator,X);
end
To configure the build settings, create a coder configuration object. To create the object, use the coder.gpuConfig
function and specify the output as a MEX file that calls generated CUDA code.
Create an array of example values that define the size and class of the inputs to the generated function.
Generate the MEX file using the configuration object, the example values, and the entry-point function discriminatorEntryPoint
. The generated discriminatorEntryPoint_mex
function returns the distribution score for an input observation. Code generation requires a supported compiler. To view a list of supported compilers, see Supported and Compatible Compilers.
Code generation successful: View report
You can view the resulting code generation report by clicking View Report in the MATLAB® Command Window. The report is displayed in the Report Viewer window. If the code generator detects errors or warnings during code generation, then the report describes the issues and provides links to the problematic MATLAB code.
Test the generated MEX function by comparing the distribution confidence score for an ID image and an OOD image. To create out an OOD image, modify an image from the ID training data set.
Calculate the distribution score for the ID image and the OOD image.