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.

      • 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.

  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.

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    import torch.optim as optim
    from torch.utils.data import DataLoader
    from torchvision import datasets, transforms
    from torch.utils.tensorboard import SummaryWriter
    
    # Hyperparameters
    batch_size = 64  # Batch size for training
    learning_rate = 0.01  # Learning rate
    num_epochs = 5  # Number of training epochs
    
    # Check if GPU is available
    device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
    Train Epoch: 3 [57600/60000 (96%)]      Loss: 0.116840
    Validation Loss: 0.0753, Accuracy: 9750/10000 (98%)
    Model saved with accuracy: 97.50%
    Train Epoch: 4 [0/60000 (0%)]    Loss: 0.155105
    Train Epoch: 4 [6400/60000 (11%)]       Loss: 0.025345
    Train Epoch: 4 [12800/60000 (21%)]      Loss: 0.063321
    Train Epoch: 4 [19200/60000 (32%)]      Loss: 0.129752
    Train Epoch: 4 [25600/60000 (43%)]      Loss: 0.021768
    Train Epoch: 4 [32000/60000 (53%)]      Loss: 0.127890
    Train Epoch: 4 [38400/60000 (64%)]      Loss: 0.049701
    Train Epoch: 4 [44800/60000 (75%)]      Loss: 0.025785
    Train Epoch: 4 [51200/60000 (85%)]      Loss: 0.042838
    Train Epoch: 4 [57600/60000 (96%)]      Loss: 0.064462
    Validation Loss: 0.0666, Accuracy: 9767/10000 (98%)
    Model saved with accuracy: 97.67%
    Training complete. writer.close()

    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/.

    !tensorboard --logdir=/mnt/workspace/output/runs
    
    TensorFlow installation not found - running with reduced feature set.
    
    NOTE: Using experimental fast data loading logic. To disable, pass
        "--load_fast=false" and report issues on GitHub. More details:
        https://github.com/tensorflow/tensorboard/issues/4784
    
    Serving TensorBoard on localhost; to expose to the network, use a proxy or pass --bind_all
    TensorBoard 2.19.0 at http://localhost:6006/ (Press CTRL+C to quit)
    ^C

    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.

    Click the Run button on the left to execute the following code:

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from torchvision import datasets, transforms
    import matplotlib.pyplot as plt
    
    # Define the same model structure as used during training
    class SimpleCNN(nn.Module):
        def __init__(self):
            super(SimpleCNN, self).__init__()
            self.conv1 = nn.Conv2d(1, 10, kernel_size=5)

    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.

    !cp  -r /mnt/workspace/output/  /mnt/data/

    Go to the OSS console to view the copied files.

    The OSS storage path pai_test/output/ contains the runs/ folder and the best_model.pth model file.

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

EAS (Elastic Algorithm Service) allows you to deploy trained models as an online inference service or an AI web application. EAS supports heterogeneous resources and combines features like auto scaling, one-click stress testing, canary release, and real-time monitoring to ensure service stability in high-concurrency scenarios at a lower cost.

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

    # Download the web API code web.py for model prediction
    !curl -f -o web.py "http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/web.py"
    
    # Download the test code for the model service
    !curl -f -o request_web.py "http://aliyun-document-review.oss-cn-beijing.aliyuncs.com/dsw_files/request_web.py"
    
    # Copy web.py to OSS
    !cp /mnt/workspace/web.py  /mnt/data/web.py
  2. (Optional) Verify the web API in DSW. Run the following cell to install dependencies and start the service.

    # Install the bottle package
    !pip install bottle
    
    # Start the web.py service
    !python web.py
    
    Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
    Requirement already satisfied: bottle in /usr/local/lib/python3.11/site-packages (0.13.4)
    WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
    
    [notice] A new release of pip is available: 23.3.2 -> 25.1.1
    [notice] To update

    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.

    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

    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.

      On the Instance List page of DSW, find the target instance and locate the Image Version column. Click the copy icon next to the image address to copy it.

    • 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