Quick start: Interactive modeling with DSW

更新时间:
复制 MD 格式

Use Data Science Workshop (DSW) with its built-in notebook or VS Code to develop, train, and deploy a deep learning model in the cloud. This tutorial uses the MNIST handwritten digit recognition example to demonstrate the end-to-end process.

Note

MNIST handwritten digit recognition is a classic introductory task in deep learning. The goal is to train a model to recognize 10 handwritten digits (0 to 9).

Prerequisites

You have activated PAI and created a workspace with your primary account. If not, go to the PAI console, select a region in the upper-left corner, and follow the on-screen instructions to grant permissions and activate the service.

Billing

This tutorial uses a pay-as-you-go DSW instance and an EAS model service created from public resources. For more information, see DSW billing and EAS billing.

Create a DSW instance

  1. Go to the DSW page.

    1. Go to the PAI console.

    2. In the upper-left corner, select the target region.

    3. In the left-side navigation pane, click Workspaces and select your target workspace.

    4. In the left-side navigation pane, choose Model Training > Interactive Modeling (DSW) > Create an instance..

  2. On the Configure Instance page, configure the following parameters and leave others at their default settings.

    • Resource Type: Select Public Resources (pay-as-you-go).

    • Instance Type: Select ecs.gn7i-c8g1.2xlarge.

      If this instance type is out of stock, you can select another GPU-accelerated instance.
    • Image config: Select Alibaba Cloud Image and search for the image modelscope:1.26.0-pytorch2.6.0-gpu-py311-cu124-ubuntu22.04.

      Use the same image to avoid environment compatibility issues.
    • Mount storage: Persistently store your model development files. This tutorial uses Object Storage Service (OSS). Click OSS, click the image icon, select a Bucket, and create a new directory, such as pai_test.

      If no bucket is available in the current region, follow these steps to create one:

      (Optional) Create an OSS bucket

      1. Activate OSS.

      2. Go to the OSS console, click Create Bucket, enter a Bucket Name, select the same Region as PAI, leave other parameters at their default settings, and click Create.

        image

      • URI: oss://**********oss-cn-hangzhou-internal.aliyuncs.com/pai_test/

      • Mount Path: /mnt/data/

  3. Click OK to create the instance.

    If the instance fails to start, see Create a DSW instance for troubleshooting.

Develop the model in DSW

  1. In the instance list, click Open to enter the DSW development environment. On the Launcher page, click Create Notebook.

  2. Download the provided MNIST training code, mnist.ipynb, and upload it to DSW by clicking the image icon in the upper-left corner.

    image

  3. Train the model. Open mnist.ipynb, find the training code cell, and click the image icon to run it. The code automatically downloads the MNIST dataset to the dataSet directory and saves the trained model to the output directory. Training takes about 10 minutes.

    image

    image

    The model's accuracy on the validation set, which reflects its generalization ability, is displayed during training. The validation accuracy in this example is 98%, which means you can proceed to the next steps.

  4. View the training curves. Run the following cell and click the TensorBoard URL http://localhost:6006/.

    image

    TensorBoard displays the train_loss (training set loss) and validation_loss (validation set loss) curves.

    image

    After viewing the curves, click the image icon in the cell to stop TensorBoard. Subsequent cells cannot run until you stop TensorBoard.

    (Optional) Tune hyperparameters based on the loss curves

    Evaluate the training performance based on the loss curve trends:

    • Underfitting: Both train_loss and validation_loss are still decreasing when training ends.

      Increase num_epochs (training epochs) or increase the learning_rate to improve the model's fit.

    • Overfitting: train_loss continues to decrease, but validation_loss starts to increase before training ends.

      Decrease num_epochs or lower the learning_rate to prevent overfitting.

    • Good fit: Both train_loss and validation_loss stabilize before training ends.

      The model has reached a desired state. You can proceed to the next steps.

    For more information about hyperparameter tuning, see Alibaba Cloud Large Model ACP Course.

  5. Test the model's performance. Run the following cell to display 20 test images with their true labels and the model's predictions.

    image

    Sample output:

    image

  6. The DSW instance created in this tutorial uses a temporary cloud disk for file storage. If the instance remains stopped for more than 15 days, the content on the cloud disk is automatically deleted. To permanently store the model files for subsequent deployment with EAS, copy them to OSS.

    image

    Go to the OSS console to view the copied files.

    image

After model development is complete, you can deploy the model as an online service. For more information, see Deploy the model as a service by using EAS.

Important

The DSW instance in this tutorial uses public resources and is billed on a pay-as-you-go basis. To avoid ongoing charges, stop or delete the instance on the instance list page when you no longer need it.

Deploy a model service with EAS

Elastic Algorithm Service (EAS) deploys trained models as online inference services or AI web applications. It combines heterogeneous resource support, automatic scaling, one-click stress testing, canary releases, and real-time monitoring to maintain service stability under high concurrency at lower cost.

  1. This tutorial provides a web API for the model. Run the following cell to copy the API code to OSS.

    image

  2. (Optional) Verify the web API in DSW. Run the following cell to install dependencies and start the service.

    image

    Test the endpoint. At the top of the page, click WebIDE. In the left-side pane, click request_web.py, and then click the image icon to run the code.

    image

    Returned result:

    {"prediction": 7}
    Note

    To access the web service in DSW from the internet, you must configure a VPC, a NAT gateway, and an EIP. For more information, see Access a service in an instance over the internet.

  3. Create an EAS service. In the left-side navigation pane of the PAI console, click Elastic Algorithm Service (EAS) > Deploy Service > Custom Deployment.

    Configure the following parameters and leave others at their default settings:

    • Deployment Method: Image-based Deployment

    • Image Configuration: Select Image Address and paste the image address used by DSW.

      To avoid environment-related issues, use the same image as in DSW, which has already been verified to run the model code.

      image

    • Mount storage: The model files and web API code are already copied to OSS. Click OSS and select the corresponding path.

      • URI: oss://*****/pai_test/

      • Mount Path: /mnt/data/

    • Command to Run: Because the web.py file is mounted to /mnt/data/, the run command is python /mnt/data/web.py.

    • Port Number: Enter the port number used by web.py, which is 9000.

    • Third-party Library Settings: The selected image is missing the bottle library. Add bottle here.

    • Resource Type: Select Public Resources. For Instance Type, select ecs.gn7i-c8g1.2xlarge.

    • Configure a system disk: Set the size to 20 GB.

      The image is large. An insufficient system disk size may cause the instance to fail to start. Set the size to 20 GB or more.

    Click Deploy to create the service. The service takes about 5 minutes to create. When the status changes to Running, the deployment is successful.

  4. Get the service endpoint and token. On the service details page, click View Endpoint Information to get the Internet Endpoint and Token.

  5. Call the service. Replace the Internet Endpoint and Token in the following code with your actual values, and then run the code.

    import requests
    
    """
    Test image URLs:
    Label is 7
    http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_7_No_0.jpg
    Label is 2
    http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_2_No_1.jpg
    Label is 1
    http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_1_No_2.jpg
    Label is 0
    http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_0_No_3.jpg
    Label is 4
    http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_4_No_4.jpg
    Label is 9
    http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_9_No_5.jpg
    """
    
    image_url = 'http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/mnist_label_7_No_0.jpg'
    
    # The client downloads the image and obtains the binary data.
    img_response = requests.get(image_url, timeout=10)
    # Automatically check if the request was successful based on the status code.
    img_response.raise_for_status()
    img_bytes = img_response.content
    
    # Header information. Replace <your_token> with your actual token.
    # In production environments, set the token as an environment variable to prevent sensitive information leaks.
    # For more information about how to configure environment variables, see https://help.aliyun.com/en/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems.
    headers = {"Authorization": "<your_token>"}
    # Send the binary data as the body of a POST request to the model service.
    resp = requests.post('<your_internet_endpoint>/predict_image', data=img_bytes, headers=headers)
    print(resp.json())

    Returned result:

    {"prediction": 7}
Important

The EAS service in this tutorial uses public resources and is billed on a pay-as-you-go basis. To avoid ongoing charges, stop or delete the service on the service list page when you no longer need it.

Related documents