Image content risk control solution

更新时间:
复制 MD 格式

PAI provides an image content risk control solution to identify high-risk content in online media. This solution uses preset templates in Machine Learning Designer to quickly build risk control models that are tailored to your business needs. You can then deploy these models as Elastic Algorithm Service (EAS) online services to detect and block high-risk content.

Background information

High-risk content can appear in scenarios such as image comments, short videos, and live streaming because this type of content is often unrestricted. This content must be identified and blocked promptly. Alibaba Cloud PAI provides a solution that uses artificial intelligence (AI) algorithms to help you quickly identify high-risk content:

  • Solution

    1. Use the iTAG platform and PAI Data Management to quickly annotate images and manage samples for your specific scenario.

    2. Use pre-trained models from PAI to fine-tune a model for your image risk control scenario in Machine Learning Designer. This lets you build an image classification or object detection model based on Resnet50.

    3. Deploy the model to EAS to create a complete end-to-end process that automatically detects high-risk content in a production environment.

  • Solution architecture

    The following figure shows the architecture of the image content risk control solution.

    image

Prerequisites

  • You have activated the PAI (Designer, EAS) Pay-as-you-go service. For more information, see Activate and create a default workspace.

  • A general computing resource quota is associated with the workspace. For more information, see Manage workspaces. To create a general computing resource quota, see Add a resource quota.

  • You have created an Object Storage Service (OSS) bucket to store raw data, manifest files, and trained model files. For more information about how to create a bucket, see Create buckets.

Limits

OSS does not support cross-region access. Therefore, the OSS bucket that stores your data and the Machine Learning Designer workflow must be in the same region.

Procedure

The following procedure describes how to build an image content risk control solution using the Alibaba Cloud PAI platform:

  1. Prepare the data

    Store the raw image data in OSS. Then, use PAI Data Management to scan the raw data and generate a manifest file. Finally, use iTAG to annotate the data and obtain an annotated dataset for model training.

    PAI provides a raw dataset that you can use for data preparation. To download the dataset, see Image classification dataset for content risk control or Object detection dataset for content risk control.

  2. Build a model for image content risk control.

    In Machine Learning Designer, you can use a pre-trained model to build either a Resnet50 image classification model or an object detection model for risk detection. Select the appropriate model based on your business scenario:

    • Image Classification Training (torch)

      If your business scenario requires classifying entire images into risk categories, you can build an image classification model.

    • Image Detection Training

      If your business scenario requires detecting and drawing bounding boxes around high-risk entities in images, you can build an object detection model.

  3. Deploy and invoke the model service

    You can use EAS to deploy the trained content risk control model as an online service and invoke it for inference in your production environment.

Prepare the data

This section uses a public PAI dataset as an example. You can follow the same steps to prepare your own dataset.

  1. Divide the raw images into a training dataset and a test dataset, and upload them to the OSS bucket that you created. For more information about how to upload files to OSS, see Upload files.

  2. Use PAI Data Management to scan the raw data and generate a .manifest file. For more information, see Create and manage datasets.

  3. Use the iTAG administrator console to create an annotation task. For more information, see Create an annotation task.

  4. Use the iTAG annotator console to annotate the data. For more information, see Process an annotation task.

  5. On the iTAG page, click the Task Hub tab. Find the completed annotation task and click Get Annotation Result in the Actions column to retrieve the annotated dataset, which is stored in the specified OSS folder.

Build a model

In this solution, you use preset templates in Machine Learning Designer to build an image classification model or an object detection model. Select a model based on your business scenario.

Build an image classification model

  1. Go to the Machine Learning Designer page.

    1. Log on to the PAI console.

    2. In the left-side navigation pane, click Workspaces. On the Workspaces page, click the name of the workspace that you want to manage.

    3. In the left-side navigation pane, choose Model Training > Visualized Modeling (Designer).

  2. Create an image classification workflow from a preset template.

    1. On the Machine Learning Designer page, click the Preset Templates tab.

    2. Click the CV tab.

    3. In the Image Classification area, click Create.

    4. In the New Workflow dialog box, configure the parameters and click OK.

      For Workflow Data Storage, specify an OSS bucket path to store the temporary data and models that are generated when the workflow runs.

  3. Go to the workflow and configure the component parameters.

    1. On the Visualized Modeling (Designer) page, click the Pipelines tab.

    2. Select the pipeline that you created and click Open.

    3. The system automatically builds a workflow based on the preset template. This solution uses the default configurations. If you want to adjust the preset parameters, see the Image Classification Training component documentation.

  4. In the upper-left corner of the canvas, click the Run icon image.png to run the workflow.

Build an object detection model

  1. Go to the Machine Learning Designer page.

    1. Log on to the PAI console.

    2. In the left-side navigation pane, click Workspaces. On the Workspaces page, click the name of the workspace that you want to manage.

    3. In the left-side navigation pane, choose Model Training > Visualized Modeling (Designer).

  2. Create an object detection workflow from a preset template.

    1. On the Machine Learning Designer page, click the Preset Templates tab.

    2. Click the CV tab.

    3. In the Image Object Detection area, click Create.

    4. In the New Workflow dialog box, configure the parameters and click OK.

      For Workflow Data Storage, specify an OSS bucket path to store the temporary data and models that are generated when the workflow runs.

  3. Go to the workflow and configure the component parameters.

    1. On the Machine Learning Designer page, click the Workflow List tab.

    2. Double-click the workflow that you just created to open it.

    3. The system automatically builds the workflow based on the preset template. This solution uses the default configurations. If you want to adjust the preset parameters, see the Image Detection Training component documentation.

  4. In the upper-left corner of the canvas, click the Run icon image.png to run the workflow.

Deploy and invoke the model service

You can use EAS to deploy the trained image classification or object detection model as an online service and invoke it for inference in your production environment.

  1. Go to the Elastic Algorithm Service (EAS) page.

    1. Log on to the PAI console.

    2. In the left-side navigation pane, click Workspaces. On the Workspaces page, click the name of the workspace that you want to manage.

    3. In the left-side navigation pane, choose Model Deployment > Elastic Algorithm Service (EAS).

  2. Deploy the model service.

    1. On the Elastic Algorithm Service (EAS) page, click Deploy Service. In the dialog box that appears, select Custom Deployment and click OK.

    2. On the Custom Deployment page, configure the following parameters and click Deploy. This topic describes only the core parameters. For information about other parameters, see Custom Deployment.

      Parameter

      Description

      Service Name

      Enter a custom name for the model. We recommend that you name it based on your business to distinguish it from other model services.

      Deployment Method

      Select Deploy with Processor.

      Model Configuration

      The models trained in this example are stored in OSS. Therefore, set the configuration type to Object Storage Service (OSS). In the workflow data storage directory that you configured in the Build a model step, select the OSS path of the epoch_xx_export.pt model file.

      Processor Type

      Select EasyCV.

      Model Type

      This solution uses Image Classification.

      • If you are deploying an image classification model, select Image Classification.

      • If you are deploying an object detection model, select Object Detection.

      Deployment Resources

      This solution uses the ecs.gn7i-c16g1.4xlarge resource specification.

  3. View the public endpoint and access token of the model service.

    1. On the Elastic Algorithm Service (EAS) page, click the name of the target service. In the Basic Information area, click View Endpoint Information.

    2. In the Endpoint Information panel, view the endpoint and token for public network access.

  4. Use a script for batch invocation.

    1. Create a Python script locally to invoke the model service.

      • Python script for image classification (cv_risk_cls.py):

        import requests
        import base64
        import sys
        import json
        resp = requests.get('<image_url>')
        ENCODING = 'utf-8'
        datas = json.dumps( {
                    "image": base64.b64encode(resp.content).decode(ENCODING),
                    })
        head = {
           "Authorization": "YjdhYWRhYWZhYzNjZTFlMDZlNjAxxxxxxxxxxxxxxxxxxxx" # Replace the service access token with your actual token.
        }
        for x in range(0,1):
            # Replace the public endpoint of the service with your actual endpoint.
            resp = requests.post("<service_url>", data=datas, headers=head)
            print(resp.text)
        print("test endding")

        Replace <image_url> with the URL of the image, such as https://xxxx/1.jpg. Set Authorization to the service token that you obtained. Replace <service_url> with the service endpoint that you obtained.

      • Python script for object detection (cv_risk_det.py):

        import requests
        import base64
        import sys
        import json
        img_file = './xxx.jpg'
        ENCODING = 'utf-8'
        datas = json.dumps( {
                    "image": base64.b64encode(open(img_file, 'rb').read()).decode(ENCODING),
                    })
        head = {
           "Authorization": "NGVkMTVmZjNlNzA3ZGVlNWIzxxxxxxxxxxxxxx" # Replace the service access token with your actual token.
        }
        # Replace the public endpoint of the service with your actual endpoint.
        r = requests.post("<service_url>", data=datas, headers=head)
        print(r.text)

        Set img_file to the path of the image file. Set Authorization to the service token that you obtained. Replace <service_url> with the service endpoint that you obtained.

    2. Upload the Python script for image classification or object detection to an environment. In the directory where you uploaded the script, run the following command.

      $ python3 <cv_risk_xxx.py>

      Replace <cv_risk_xxx.py> with the actual name of your Python script.

  5. Monitor service metrics.

    After you invoke the model service, you can view the invocation metrics, such as queries per second (QPS), response time (RT), CPU usage, GPU usage, and memory usage.

    1. On the Elastic Algorithm Service (EAS) page, click the monitoring icon image in the Invocation/Logs/Monitoring column of the service that you invoked.

    2. On the Monitoring tab, you can view the model invocation metrics.

References