Deploy Wafer Map Defect Classifier as Microservice Using Docker
This example shows how to create a microservice Docker® image from the wafer map defect classifier described in Classify Defects on Wafer Maps Using Deep Learning (Image Processing Toolbox). The microservice image created by MATLAB® Compiler SDK™ provides an HTTP/HTTPS endpoint to access MATLAB code.
You package a MATLAB function into a deployable archive, and then create a Docker image that contains the archive and a minimal MATLAB Runtime package. You can then run the image in Docker and make calls to the service using any of the MATLAB Production Server™ client APIs.
Required Products
Type ver
at the MATLAB command prompt to verify whether the following products are installed:
MATLAB
Deep Learning Toolbox™
MATLAB Compiler™
MATLAB Compiler SDK
Prerequisites
Verify that
CNN-WM811K.mat
, the convolutional neural network trained or downloaded in the example Classify Defects on Wafer Maps Using Deep Learning (Image Processing Toolbox), is on your MATLAB path.Verify that you have MATLAB Compiler SDK installed on the development machine.
Verify that you have Docker installed and configured on the development machine by typing
[~,msg] = system('docker version')
in a MATLAB command window. Note: If you are using WSL, use the command[~,msg] = system('wsl docker version')
instead. If you do not have Docker installed, follow the instructions on the Docker website to install and set up Docker.docs.docker.com/engine/install/
To build microservice images on Windows®, you must install either Docker Desktop or Docker on Windows Subsystem for Linux v2 (WSL2). To install Docker Desktop, see
https:/
/
docs.
docker.
com/
desktop/
install/
windows-install/
.For instructions on how to install Docker on WSL2, see https://www.mathworks.com/matlabcentral/answers/1758410-how-do-i-install-docker-on-wsl2.
Verify that you have the MATLAB Runtime installer for Linux® on your machine. You can download the installer from the MathWorks® website: https://www.mathworks.com/products/compiler/matlab-runtime.html
Create an Inference Function
First, create a function in a separate file called waferdefect.m
that performs the inference.
function prediction = waferdefect(image) load("CNN-WM811K.mat","preTrainedNetwork"); %classify the wafer images in the batch and return the results prediction = string(classify(preTrainedNetwork,imresize(imread(image), [48,48]))); disp(string(prediction)) end
Test the function from the MATLAB command line using the included image, shown below.
[im, cmap] = imread("waferimage.jpg"); imshow(im, cmap) %% Detect objects in image prediction = waferdefect(im)
prediction = "Edge-Loc"
Note: For this example, the waferimage.jpg
file for testing the classifier will be included under the AdditionalFiles
argument. In practice, the images would likely be captured by a camera periodically.
Create Deployable Archive
Next, package the waferdefect
function into a deployable archive using the compiler.build.productionServerArchive
function.
You can specify additional options in the compiler.build
command by using name-value arguments. For details, see compiler.build.productionServerArchive
.
deployableArchive = compiler.build.productionServerArchive('waferdefect.m',... ArchiveName='waferdefectclassifier', AdditionalFiles=["CNN-WM811K.mat", "waferimage.jpg"], Verbose=true);
Once the build is complete, the function creates a folder named waferdefectproductionServerArchive
in your current directory to store the deployable archive.
Package Archive into Microservice Docker Image
Build the microservice Docker image using the deployableArchive
object that you created.
You can specify additional options in the compiler.build
command by using name-value arguments. For details, see compiler.package.microserviceDockerImage
.
compiler.package.microserviceDockerImage(deployableArchive,'ImageName','waferdefectclassifier');
This function creates a Docker container image in your local container repository called waferdefectclassifier
. You can verify that the Docker container image was created by opening a command window and typing in the following command:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE waferdefectclassifier latest 809ec52ec3f9 About a minute ago 6.96GB matlabruntime/r2023a/release/update3/f08140002000000010 latest e46173b11d3c 2 days ago
In addition, the function generates a MicroserviceDockerContext
folder in your current directory with the following files:
applicationFilesForMATLABCompiler/waferdefect.ctf
— Deployable archive file.Dockerfile
— Docker file that specifies Docker run-time options.GettingStarted.txt
— Text file that contains deployment information.
The GettingStarted.txt
file contains the commands you will use to start the microservice locally for testing
docker run --rm -p 9900:9910 waferdefectclassifier -l trace &
Port 9910 is the default port exposed by the microservice within the Docker container. You can map it to any available port on your host machine. For this example, it is mapped to port 9900.
You can specify additional options in the Docker command. For a complete list of options, see Microservice Command Arguments.
Once the microservice container is running in Docker, you can check the status of the service by going to the following URL in a web browser:
http://localhost:9900/api/health
If the service is ready to receive requests, you see the following message:
"status: ok"
Test the running service. In the terminal, use the curl
command to send a JSON query to the service through port 9900. For more information on constructing JSON requests, see JSON Representation of MATLAB Data Types (MATLAB Production Server).
Linux and macOS Terminal
curl -v -H Content-Type:application/json \ -d '{"nargout":1,"rhs":["https://<path_to_the_image_file>"]}' \ "http://localhost:9900/waferdefectclassifier/waferdefect" | jq -c
Windows PowerShell
Invoke-RestMethod -Uri 'http://localhost:9900/waferdefectclassifier/waferdefect' -Method Post -ContentType 'application/json' -Body '{"nargout":1,"rhs":["https://uk.mathworks.com/help/examples/images_deeplearning/win64/ClassifyWaferMapDefectsUsingDeepLearningExample_08.png"]}' | ConvertTo-Json
The output is:
{"lhs":[{"mwdata": "Edge-Loc", "mwsize": "1 1", "mwtype": "string"}]}
Share Docker Image
You can share your Docker image in various ways.
Push your image to the Docker central registry DockerHub, or to your private registry. This is the most common workflow.
Save your image as a tar archive and share it with others. This workflow is suitable for immediate testing.
For details about pushing your image to DockerHub or your private registry, consult the Docker documentation.
Save Docker Image as Tar Archive
To save your Docker image as a tar archive, open a system command window, navigate to the Docker context folder, and type the following.
docker save waferdefectclassifier -o waferdefectclassifier.tar
This command creates a file named waferdefectclassifier.tar
in the current folder. Set the appropriate permissions (for example, using chmod
) prior to sharing the tarball with other users.
Load Docker Image from Tar Archive
Load the image contained in the tarball on the end user machine.
docker load --input waferdefectclassifier.tar
Verify that the image is loaded.
docker images
Run Docker Image
docker run --rm -p 9900:9910 waferdefectclassifier
See Also
matlabshared.supportpkg.getInstalled
| compiler.build.productionServerArchive
| compiler.package.microserviceDockerImage
| compiler.runtime.createInstallerDockerImage