Develop, train, and deploy deep learning models in the cloud by using the built-in Notebook or VSCode in Data Science Workshop (DSW). This topic uses MNIST handwritten digit recognition as an example to demonstrate the complete workflow.
MNIST handwritten digit recognition is a classic introductory task in deep learning. The goal is to train a model to recognize the 10 handwritten digits from 0 to 9.
Prerequisites
PAI is activated and a workspace is created in your Alibaba Cloud account. If PAI is not activated, go to the PAI console, select the target region in the upper-left corner of the page, and complete the one-click authorization and activation.
Billing
The DSW instance and the EAS model service in this topic are created in a public resource group and billed on a pay-as-you-go basis. For billing details, see DSW billing and EAS billing.
Create a DSW instance
-
Go to the DSW page.
Go to the PAI console.
Select the target region in the upper-left corner of the page.
Click Workspaces in the left-side navigation pane to go to the target workspace.
In the left-side navigation pane, choose .
-
In the Configure Instance wizard, configure the following parameters and keep the default values for other parameters:
Resource Type: Select Public Resources (pay-as-you-go).
-
Instance Type: Select
ecs.gn7i-c8g1.2xlarge.If this instance type is unavailable, select another GPU instance type.
-
Image config: Select Alibaba Cloud Image, search for and select 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: Stores model development files. This topic uses OSS. Click OSS, click the
icon, select a Bucket, and create a directory (for example, pai_test).If no bucket is available in the current region, follow these steps to create one:
Uri:
oss://**********oss-cn-hangzhou-internal.aliyuncs.com/pai_test/Mount Path:
/mnt/data/
-
Click OK to create the instance.
If the instance fails to start, see Create a DSW instance for troubleshooting.
Develop a model in DSW
Click Open in the instance list to open the DSW development environment. On the launcher, click JupyterLab to create a Notebook development environment.
-
Write the model code. This topic provides the MNIST training code: download mnist.ipynb, and then click the
icon in the upper-left corner of the Notebook to upload the file.
-
Train the model. Open
mnist.ipynb, locate the cell that contains the training code, and click the
icon to run the cell. The code automatically downloads the MNIST dataset to the dataSetdirectory and saves the trained model to theoutputdirectory. Training takes about 10 minutes.

The training output shows the accuracy of the model on the validation set, which reflects its generalization capability. In this example, the validation set accuracy is 98%. Proceed to the next step.
-
View the training curves. Run the following cell and open the TensorBoard URL
http://localhost:6006/.
TensorBoard displays the train_loss (training set loss) and validation_loss (validation set loss) curves.

After you view the curves, click the
icon in the cell to stop TensorBoard. If TensorBoard is not stopped, subsequent cells cannot be run. -
Test the model. Run the following cell to display 20 test images along with their actual labels and the model predictions.

Sample output:

-
Back up the model files. The DSW instance in this topic is created in a public resource group, and files are stored on a free cloud disk. If the instance is stopped for more than 15 days, the content on the cloud disk is automatically cleared. Copy the model files to OSS for later deployment with EAS.

Go to the OSS console to view the copied files:

After the model is developed, you can deploy it as an online service. For more information, see Deploy the model service with EAS.
The DSW instance in this topic is created in a public resource group and billed on a pay-as-you-go basis. When the instance is no longer needed, stop or delete it on the right side of the instance list page to avoid ongoing charges.
Deploy the 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.
-
Write the web interface for the model and copy it to OSS. This topic provides the web interface code and the copy commands. Run the following cell to complete this step.

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

Test the interface. On the launcher page, click Terminal to open a terminal and run
python request_web.py. The following result is returned:{"prediction": 7}NoteAccessing the web service in DSW over the Internet requires a VPC, a NAT gateway, and an Elastic IP (EIP). For more information, see Access services in an instance over the public network.
-
Create the 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 keep the default values for other parameters:
-
Resource Type: Select Public Resources. For Instance Type, select
ecs.gn7i-c8g1.2xlarge.If this instance type is unavailable, select another GPU instance type.
Deployment Method: Image-based Deployment
-
Image Configuration: Select Official Image, search for and select
torcheasyrec:1.2.0-pytorch2.11.0-gpu-py311-cu126-ubuntu22.04.In a production environment, you typically develop and test code in DSW first, and then package the container environment and push it to Container Registry (ACR) for EAS to use during model deployment. For more information, see Create a DSW instance image.
-
Mount storage: The model files and web interface code are copied to OSS. Click OSS and select the corresponding path.
Uri: oss://*/pai_test/
Mount Path: /mnt/data/
Command to Run:
web.pyis mounted to/mnt/data/. The run command ispython /mnt/data/web.pyPort Number: Enter port
9000, which is used byweb.py.-
Third-party Library Settings: Enter
bottle torchvision.The selected official image does not include the bottle or torchvision libraries (as verified by testing). Therefore, enter
bottle torchvisionin the third-party library configuration.
Click Deploy to create the service. It takes about 5 minutes to create the service. When the status changes to Running, the service is deployed.
-
Get the calling information. On the service details page, click View Endpoint Information to obtain the Internet Endpoint and Token.
-
Call the service. Replace the Internet Endpoint and Token in the following code with the 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 5 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 to obtain the binary data img_response = requests.get(image_url, timeout=10) # Check whether the request succeeds based on the status code img_response.raise_for_status() img_bytes = img_response.content # In the header, replace <EAS_TOKEN> with the actual token # In production, we recommend that you set the token as an environment variable to prevent sensitive information leakage # For information about how to configure environment variables, see https://www.alibabacloud.com/help/zh/sdk/developer-reference/configure-the-alibaba-cloud-accesskey-environment-variable-on-linux-macos-and-windows-systems headers = {"Authorization": "Token"} # Send the binary data as the body of the POST request to the model service resp = requests.post('public endpoint/predict_image', data=img_bytes, headers=headers) print(resp.json())The following result is returned:
{"prediction": 7}
The EAS service in this topic is created in a public resource group and billed on a pay-as-you-go basis. When the service is no longer needed, stop or delete it on the right side of the service list page to avoid ongoing charges.
References
For information about troubleshooting DSW startup failures, see Create a DSW instance.
For DSW billing details, see DSW billing.
For the core features of DSW, see DSW overview.
For information about accessing the DSW web service over the Internet, see Access services in an instance over the public network.
For the core features of EAS, see EAS overview.
