PAI provides an industrial quality inspection solution that improves defect detection efficiency in manufacturing. This solution uses artificial intelligence algorithms to help you quickly build an end-to-end quality inspection service. This topic describes how to use this solution.
Background information
Defect detection is an essential step in manufacturing. It is widely used in various industrial fields, such as 3C electronics, automotive, chemical, and light industry. Although detection technologies such as artificial intelligence and computer vision are maturing, manual visual inspection remains common. This leads to low production automation, low efficiency, and difficulties in achieving lean manufacturing. To address these issues, Alibaba Cloud PAI offers a solution that uses artificial intelligence algorithms.
Solution
You can use the iTAG platform and PAI Data Management to quickly annotate data and manage samples for your target scenario.
You can use the models and algorithms provided by PAI to fine-tune a model with your own data on the Data Science Workshop (DSW) platform. This builds a quality inspection model for your specific scenario.
You can deploy the model online using Elastic Algorithm Service (EAS) to create a complete end-to-end process. You can then access the service to automatically detect defects in images.
Workflow
The workflow for building an industrial quality inspection solution using Alibaba Cloud PAI is as follows.
You can use the sample iTAG data provided by PAI or annotate raw data using iTAG. Then, you can export the annotation results and upload them to OSS for model training.
Build a quality inspection model using DSW
On the DSW platform, you can build a model by following the example provided in the DSW Gallery.
Deploy and invoke the model service
You can use EAS to deploy the trained model as an online service and invoke it for inference in a production environment.
Prerequisites
Before you begin, complete the following preparations.
PAI is activated, and the pay-as-you-go billing method is enabled for DSW and EAS. A default workspace is created. For more information, see Activate PAI and create a default workspace.
An OSS bucket is created to store raw images and trained model files. For more information about how to create a bucket, see Create a bucket in the console.
A DSW instance is created. When you create the instance, select the modelscope:tf1.15torch1.11-gpu-py37-cu113-ubuntu20.04 image, a P100 or V100 GPU, and more than 32 GB of memory. For more information, see Create a DSW instance.
Prepare data
You can use the sample iTAG data provided by PAI in the DSW Gallery or use a custom dataset annotated with iTAG. If your data is not annotated, you can use iTAG to annotate the raw data. The iTAG annotation procedure is as follows. For more information, see the iTAG documentation.
Upload the images that you want to annotate to OSS. For more information, see Upload files in the console.
Create a dataset from the image files that you uploaded to OSS. For more information, see Create a dataset.
Create and complete an annotation task.
In the iTAG administrator console, create an annotation task. For more information, see Create an annotation task.
In the iTAG annotator console, annotate the data. For more information, see Annotate images (annotate, quality check, and accept). The following figures show examples of annotations.




After the annotation is complete, export the annotation results to a .manifest file. For more information, see Export annotation results.
Upload the annotation result file, the label file, and the raw image files to the DSW Notebook. The files must be organized in the following folder structure. For more information, see Upload and download files.
itag_det_example |--imgs |--class_list.txt |--pet_det.manifestWhere:
imgs: This folder stores the raw image files.
class_list.txt: The label file. Enter data labels line by line. For example:
Category One Category Twopet_det.manifest: The annotation result file that you obtained in the previous step.
Build a quality inspection model using DSW
Go to the Notebook Gallery page. For more information, see Notebook Gallery.
On the Notebook Gallery page, build the target quality inspection model.
In the search box, enter YOLOX and press Enter. In the Image Detection Example Based on YOLOX Model and iTAG Annotated Data section, click Open in DSW and follow the instructions in the console to build the YOLOX object detection model.If you are using a custom dataset annotated with iTAG, replace the default dataset folder with the custom dataset folder described in the Prepare data section.

Upload the trained model file, which has the _export.pt suffix, to OSS. For more information, see Upload files in the console.
Deploy and invoke the model service
The procedure to deploy the model service is as follows.
-
Go to the Elastic Algorithm Service (EAS) page.
-
Log on to the PAI console.
-
In the left-side navigation pane, click Workspaces. On the Workspaces page, click the name of the workspace that you want to manage.
-
In the left-side navigation pane, choose .
-
Deploy the model service.
On the Elastic Algorithm Service (EAS) page, click Deploy Service. In the Deploy Custom Model section, click Deploy with JSON.
On the Deploy with JSON page, configure the parameters and click Deploy.
Paste the following JSON content into the JSON editor.
{ "name": "easycv_yolox_example", "processor": "easycv", "model_path": "oss://examplebucket/epoch_10_export.pt", "model_config": {"type":"YoloXPredictor"}, "metadata": { "instance": 1 }, "cloud": { "computing": { "instance_type": "ecs.gn5i-c4g1.xlarge" } } }Where:
name: Replace the value with your service name.
processor: Set this parameter to easycv. The EasyCV processor provided by EAS can load deep learning models that are trained using the EasyCV framework. For more information about the EasyCV processor, see Built-in processors.
model_config: When you deploy a YOLOX object detection model, set this parameter to YoloXPredictor.
View the public endpoint and access token of the model service.
On the Elastic Algorithm Service (EAS) page, click the name of the target service. In the Basic Information section, click View Endpoint Information.
In the Endpoint Information panel, view the public endpoint and token.
Use a script to make batch calls.
Create a Python script named eas_predict.py to invoke the model service.
import requests import base64 import json resp = requests.get('http://examplebucket.oss-cn-zhangjiakou.aliyuncs.com/images/000000123213.jpg') ENCODING = 'utf-8' datas = json.dumps( { "image": base64.b64encode(resp.content).decode(ENCODING) }) head = { "Authorization": "NTFmNDJlM2E4OTRjMzc3OWY0NzI3MTg5MzZmNGQ5Yj***" } for x in range(0,10): resp = requests.post("http://150231884461***.cn-hangzhou.pai-eas.aliyuncs.com/api/predict/test_easycv_yolox_example", data=datas, headers=head) print(resp.text)The image data must be Base64-encoded.
Upload the eas_predict.py script to any environment. In the directory where you uploaded the script, run the following command to invoke the service:
python <eas_predict.py>Replace <eas_predict.py> with the name of your Python script.
Monitor service metrics. After you invoke the model service, you can view metrics related to the model calls, such as queries per second (QPS), response time (RT), CPU usage, GPU usage, and memory usage.
On the Elastic Algorithm Service (EAS) page, click the
icon in the Invoke/Log/Monitoring column for the service that you invoked.On the Monitoring tab, view the metrics for the model calls.

icon in the Invoke/Log/Monitoring column for the service that you invoked.