×
Community Blog Creating and Deploying Custom Models on Alibaba Cloud

Creating and Deploying Custom Models on Alibaba Cloud

This article introduces how to create and deploy a basic generative AI model (an autoencoder) on Alibaba Cloud.

By Farah Abdou

Step 1: Set Up Your Alibaba Cloud Account

  1. Go to the Alibaba Cloud website.
  2. Click on Free Account and follow the registration process.
  3. Once registered, log in to the Alibaba Cloud console.

Step 2: Prepare Your Development Environment

1.  Install Python (version 3.7 or later) on your local machine.

2.  Install the Alibaba Cloud SDK:

pip install aliyun-python-sdk-core

3.  Set up your credentials:

  • In the console, go to AccessKey Management and create an AccessKey.

1

  • Store your AccessKey ID and Secret securely.

2

Step 3: Choose and Set Up Your AI Framework

For this guide, we'll use TensorFlow with Keras.

1.  Install TensorFlow:

   pip install tensorflow

2.  Create a new Python file for your project, e.g., generative_model.py.

Step 4: Prepare Your Dataset

  1. For this example, we'll use the MNIST dataset (built into Keras).
  2. Add the following code to your Python file:

    import tensorflow as tf
    from tensorflow.keras.datasets import mnist
    
    (x_train, _), (x_test, _) = mnist.load_data()
    x_train = x_train.reshape(x_train.shape[0], 28, 28, 1).astype('float32')
    x_test = x_test.reshape(x_test.shape[0], 28, 28, 1).astype('float32')
    x_train /= 255.
    x_test /= 255.

Step 5: Design Your Model Architecture

We'll create a simple autoencoder as our generative model:

from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D
from tensorflow.keras.models import Model

input_img = Input(shape=(28, 28, 1))
x = Conv2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

x = Conv2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)

autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

Step 6: Train Your Model Locally

Before deploying, let's train the model locally:

autoencoder.fit(x_train, x_train,
                epochs=50,
                batch_size=128,
                shuffle=True,
                validation_data=(x_test, x_test))

Step 7: Save Your Model

Save the trained model:

autoencoder.save('my_autoencoder.h5')

Step 8: Set Up Alibaba Cloud Platform for AI

  1. In the Alibaba Cloud console, navigate to Platform for AI.
  2. Create a new project.

Step 9: Upload Your Model to Alibaba Cloud

1.  In your project, go to Model Management and click Create Model.

3

2.  Choose TensorFlow as the framework.

4

3.  Upload your my_autoencoder.h5 file.

Step 10: Deploy Your Model

  1. In Model Management, select your model and click Deploy.
  2. Choose Online Service as the deployment method.
  3. Configure the instance (start with a small instance for testing).
  4. Click Deploy and wait for the process to complete.

Step 11: Test Your Deployed Model

  1. Once deployed, you'll get an endpoint URL.
  2. Use the Alibaba Cloud SDK to send requests to your model:
   from aliyunsdkcore.client import AcsClient
   from aliyunsdkcore.acs_exception.exceptions import ClientException
   from aliyunsdkcore.acs_exception.exceptions import ServerException
   from aliyunsdkcore.auth.credentials import AccessKeyCredential
   from aliyunsdkcore.auth.credentials import StsTokenCredential
   from aliyunsdkcore.request import CommonRequest

   # Replace with your AccessKey ID and Secret
   credentials = AccessKeyCredential('<your-access-key-id>', '<your-access-key-secret>')
   client = AcsClient(region_id='<your-region-id>', credential=credentials)

   request = CommonRequest()
   request.set_domain('pai-eas.cn-hangzhou.aliyuncs.com')
   request.set_version('2019-09-25')
   request.set_action_name('PredictModel')
   request.add_query_param('RegionId', '<your-region-id>')
   request.add_query_param('ServiceName', '<your-service-name>')
   request.add_body_params('Body', '<your-input-data>')

   response = client.do_action_with_exception(request)
   print(response)

Conclusion

You've now created and deployed a basic generative AI model (an autoencoder) on Alibaba Cloud. This is just the beginning!

Remember to monitor your usage and costs in the Alibaba Cloud console, and don't forget to shut down resources you're not using to avoid unnecessary charges.


Disclaimer: The views expressed herein are for reference only and don't necessarily represent the official views of Alibaba Cloud.

0 1 0
Share on

Farah Abdou

2 posts | 0 followers

You may also like

Comments

Farah Abdou

2 posts | 0 followers

Related Products