tencent cloud

DocumentationTencent Cloud TI PlatformOperation GuideOnline ServicesGuide on Publishing Online Services Using Platform Built-In Images

Guide on Publishing Online Services Using Platform Built-In Images

PDF
Focus Mode
Font Size
Last updated: 2025-05-09 15:13:10
The platform is pre-set with inference images compatible with mainstream deep learning and machine learning frameworks, including TensorFlow, PyTorch, and PySpark. Users can directly use these preset inference images to deploy models, which helps save the development costs for online services.

Inference Image List

Image Name
Compatible Frameworks
Compatible Chips
tensorflow1.15-py37
TensorFlow-1.15.0,LIGHT-3.0
GPU(CUDA-10.0),CPU
tensorflow2.4-py38
TensorFlow-2.4.0,LIGHT-3.0
GPU(CUDA-11.0),CPU
pytorch1.9.0-py38
PyTorch-1.9.0,LIGHT-3.0
GPU(CUDA-11.1),CPU
pytorch1.12.1-py38
PyTorch-1.12.1,LIGHT-3.0
GPU(CUDA-11.1),CPU
detectron2-py38
PyTorch-1.9.0,LIGHT-3.0
GPU(CUDA-11.1)
mmdetection1.4.8-py38
PyTorch-1.9.0,LIGHT-3.0
GPU(CUDA-11.1)
onnx1.11.1-py38
onnx-runtime-1.11.1
GPU(CUDA-11.1),CPU
jpmml-py38
PySpark-2.4.5
CPU

Operation Guide

1. Preparing the Development Environment

a. Installing Anaconda and Preparing the Python Environment

Since the TI-ONE inference framework is only supported on Linux Kernel Systems, users need to continue subsequent operations under a Linux Kernel System.
To use a GPU, please install cuda locally. See Table 1 Inference Image List for the corresponding version.
Before starting the development of the model package, install Anaconda and set up the Python virtual environment. Anaconda Download Address.
After installation, use Anaconda to create a Python virtual environment. Assume that the name of the virtual environment created this time is TIONE-test. Please specify the Python version as 3.7 or 3.8 as needed. Currently, you can only install environments in these two versions.
conda create -n TIONE-test python=3.8
conda activate TIONE-test

b. Installing Related Dependencies for the Inference Framework

TI-ONE provides an installation script for the inference framework and related dependencies: Download the Script.
The main features of the script include installing the tiinfer inference framework, installing basic Python function libraries, installing environment libraries including TensorFlow and Torch, downloading platform demos and model files, and other features. You can use python init_infer_env.py -h to view the method of use. For the meanings of specific parameters, see the table below. For "script dependencies" and "existing Python environments", you are advised to install cuda on the local dev machine.
Parameter Name/Abbreviation
Description
Parameter
Required or Not
--ti-infer-version/-v
tiinfer version
1.0
No
--model-format/-m
Model Format
TorchScript,Detectron2,ONNX,FrozenGraph,SavedModel,MMDetection,PMML,HuggingFace
Yes
--framework/-f
Inference framework, Torch or TensorFlow version, corresponding to the model format.
torch1.9.0,torch1.12.1,onnx1.11.0,tf1.15.0,tf2.4.0,jpmml0.6.2
Yes
--model-scene/-s
Model use case, defaulting to detect
detect,classify,nlp,ocr,recommend
No
--demo-dir/-d
Download path, defaulting to do not download
-
No
--index-url/-i
Image source address, defaulting to http://mirrors.tencent.com/pypi/simple
-
No
Details of the script installation environment and corresponding installation commands are shown in the following table. {} indicates optional parameters. Select one parameter and fill it in.
Do not install different image environments in the same non-isolated Python environment. This may cause problems in the correlation between dependency packages. To use multiple environments, it is recommended to isolate multiple Python environments for different image names before installing them separately.
Model Format
Corresponding Image Name
Dependency Installation
Installation Command
PYTORCH TorchScript
pytorch1.9.0-py38
pytorch==1.9.0 opencv-contrib-python==4.6.0.66 opencv-python==4.6.0.66 setuptools==59.5.0 mmcv-full==1.4.8 transformers==4.19.4 torchvision==0.10.0 easyocr==1.6.2
python init_infer_env.py --framework torch1.9.0 --model-format TorchScript --model-scene {detect, classify, ocr, nlp} --demo-dir ./
Or
python init_infer_env.py --framework torch1.9.0 --model-format HuggingFace --model-scene nlp --demo-dir ./
pytorch1.12.1-py38
pytorch==1.12.1 opencv-contrib-python==4.6.0.66 opencv-python==4.6.0.66 mmcv-full==1.4.8 transformers==4.23.0 torchvision==0.13.1 easyocr==1.6.2
python init_infer_env.py --framework torch1.12.1 --model-format TorchScript --model-scene {detect, classify, ocr, nlp} --demo-dir ./
DETECTRON2
detectron2-py38
pytorch==1.9.0 opencv-contrib-python==4.6.0.66 setuptools==59.5.0 detectron2 torchvision==0.10.0
python init_infer_env.py --framework torch1.9.0 --model-format Detectron2 --model-scene detect --demo-dir ./
MMDETECTION
mmdetection1.4.8-py38
pytorch==1.9.0 opencv-contrib-python==4.6.0.66 opencv-python==4.6.0.66 setuptools==59.5.0 mmcv-full==1.4.8 transformers==4.19.4
python init_infer_env.py --framework torch1.9.0 --model-format MMDetection --model-scene detect --demo-dir ./
ONNX
onnx1.11.0-py38
torch==1.9.0 onnx==1.11.1 onnxruntime-gpu==1.11.1 opencv-contrib-python==4.6.0.66 setuptools==59.5.0
python init_infer_env.py --framework onnx1.11.0 --model-format ONNX --model-scene detect --demo-dir ./
TENSORFLOW
tensorflow2.4-py38
ensorflow==2.4.0 opencv-contrib-python==4.6.0.66 setuptools==59.5.0
python init_infer_env.py --framework tf2.4.0 --model-format FrozenGraph --model-scene nlp --demo-dir ./
Or
python init_infer_env.py --framework tf2.4.0 --model-format SavedModel --model-scene {nlp, recommend} --demo-dir ./
tensorflow1.15-py37
tensorflow==1.15.0 opencv-contrib-python==4.6.0.66 setuptools==59.5.0
python init_infer_env.py --framework tf1.15.0 --model-format FrozenGraph --model-scene nlp --demo-dir ./
PMML
jpmml-py38
jpmml_evaluator==0.6.2. This environment depends on OpenJDK 1.8, and you are advised to install it locally.
python init_infer_env.py --framework jpmml0.6.2 --model-format PMML

2. Local Development and Debugging of Models

Users need to self-develop the model_service.py file for the platform's inference service. For details of the development method, see Introduction and Samples of Model Inference Files. Taking the TorchScript model format as an example, the following explains the model package directory standard and the development method of model_service.py.
model_service.py needs to implement the functions corresponding to the tiinfer.Model class, including init (initialization), load (model loading), preprocess (data pre-processing), predict (model inference), and postprocess functions.
In the initialization function init, settings are required for the script and model parameters.
In the load function, the model is loaded and class information is set. The class information is mainly used to provide data during output. You can select whether the function is needed based on your needs.
In the preprocess function, the detected image of the request input is read and standardized to the input size specified by the model.
The predict function performs the inference process.
The postprocess function processes and packages the inference results.

PyTorch Implementation Example for Detection Model Inference Script:

import os
import logging
import torch
from typing import Dict, List, Union
import numpy as np
import cv2
cv2.setNumThreads(5)
import tiinfer
import tiinfer.utils
import base64
import urllib

from utils.general import non_max_suppression, scale_coords
from torchvision import transforms

# If acceleration is used, add a reference to the acceleration library: tiacc_inference.
# import tiacc_inference

# Model path when acceleration is not used.
PYTORCH_FILE = "model/yolov5s_ts.pt"
# Model path after acceleration.
# TIACC_FILE = "model/tiacc.pt"


class YoloV5DetectModel(tiinfer.Model):
def __init__(self, model_dir: str):
super().__init__(model_dir)
# workers: a platform parameter, used to set the number of processes. The parameter is optional and defaults to 1.
self.workers = 1
# The following parameters can be customized and defined based on model needs.
self.model = None
self.imgsz=640 # Input image size.
self.conf_thres=0.05
self.iou_thres=0.45 # for NMS
self.max_det = 20
self.use_gpu = torch.cuda.is_available()
self.device = torch.device('cuda:0' if self.use_gpu else 'cpu')
self.resize = transforms.Resize((self.imgsz, self.imgsz))
self.normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])

# Load model.
def load(self) -> bool:
try:
# Assemble model path. self.model_dir: current working directory. Replace 'PYTORCH_FILE' with 'TIACC_FILE' for an acceleration model.
model_file = os.path.join(self.model_dir, PYTORCH_FILE)
logging.info("model_file %s", model_file)
# Load model.
self.model = torch.jit.load(model_file, map_location=self.device)
# Set class information.
self.det_obj_names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign', 'parking meter', 'bench',
'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe',
'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard',
'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl',
'banana', 'apple', 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut',
'cake', 'chair', 'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop',
'mouse', 'remote', 'keyboard', 'cell phone', 'microwave', 'oven', 'toaster', 'sink',
'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush']
logging.info("load model to device %s" % (self.device) )
except Exception as e: # pylint: disable=broad-except
logging.error("load model failed: %s", e)
return False
return True

# Preprocess the request body.
def preprocess(self, request: Dict) -> Dict:
try:
img_data = request["image"]
# image_to_cv2_mat can correctly handle local images, image URLs, and base64-encoded image content, converting them into matrices in OpenCV 2.
img_mat = tiinfer.utils.image_to_cv2_mat(img_data)
# Latitude conversion
img_mat = torch.Tensor(img_mat).permute((2, 0, 1)).to(self.device)

# The image size should be within the tensor range of the acceleration configurations in an acceleration scenario. The platform provides interfaces for proportionally scaling and padding images: tiinfer.scale_fill_image.
# For example: The range of tensor settings for the acceleration model is [720, 2080], and the range of proportional scaling of images is [1080, 1999]. If the minimum side after proportional scaling is not within the tensor range, edge padding is required. For example, the minimum side below needs to be padded to 800.
# min_size=1080; # Minimum side value range for image scaling.
# max_size=1999; # Maximum side value range for image scaling.
# padding_limit=800; # Upper limit of the short side during padding.
# Specific use guide for image, self.scaleratio = tiinfer.scale_fill_image(image, min_size, max_size, padding_limit, Horizontal.RIGHT,Vertical.LOWER).
# Data standardization
c, h, w = img_mat.shape
img_tensor = self.normalize(self.resize(img_mat).type(torch.float32)/255)
return {"img_tensor": img_tensor.unsqueeze(0), "original_dims": [w, h]}
except Exception as e:
logging.error("Preprocess failed: %s" % (e))
return {"error": "preprocess failed"}

# Perform inference. The request parameter is the returned result of the preprocess method.
def predict(self, request: Dict) -> Dict:
with torch.no_grad():
try:
# Get the forecast results.
out = self.model(request["img_tensor"])
# Optional. Executed according to actual conditions.
# Perform non-maximum suppression on the forecast result.
preds, nms_t = non_max_suppression(out, self.conf_thres, self.iou_thres, max_det=self.max_det)
data = {
"pred_out": preds[0].to('cpu'),
"original_dims": request["original_dims"]
}
return data
except Exception as e:
logging.error("Failed to predict" % (e))
request["predict_err"] = str(e)
return request

# Post-processing. The request parameter is the returned result of the forecast method.
def postprocess(self, request: Dict) -> Dict:
try:
# Optional. Perform post-processing on the forecast results based on the model output and actual needs.
pred_out = request["pred_out"]
original_dim = request["original_dims"]
# Adjust the image shape.
pred_bbox = scale_coords([self.imgsz, self.imgsz], pred_out[:, :4], original_dim).round()
# Output parameters, defined based on actual network circumstances.
labels = []
confs = []
label_indx = []
boxes = []
for i in range(pred_out.shape[0]):
obj = pred_out[i, :].tolist()
boxes.append(pred_bbox[i, :].tolist())
confs.append(obj[4])
label_indx.append(int(obj[5]))
labels.append(self.det_obj_names[int(obj[5])])
image_result = {
"det_boxes": boxes,
"det_labels": labels,
"det_labels_idx": label_indx,
"det_scores": confs
}
return {
"result": {
"det_objs": image_result
}
}
except Exception as e:
logging.error("Postprocess failed: %s" % (e))
request.pop("image", '')
request.pop("instances", '')
request.pop("predictions", '')
request["post_process_err"] = str(e)
return request


3. Testing Before Deploying Online Services on TI-ONE

After the above environment is installed, users can test the model on their local dev machines. The default model package path of the TI-ONE inference framework is /data/model/. Before starting the test, please confirm that the model package is placed under this path. If needed, you can also add export TI_MODEL_DIR=/xxx in the system environment variable file /etc/profile to customize the model package path. Please point the default model package path to the folder where model_service.py is located.
After the model package is developed, enter the path where model_service.py is located. Taking the default framework path as an example, please run the following code in the /data/model/ path to start the service:
python3 -m tiinfer --timeout 30000
Upon a successful service startup, the service can be accessed via port 8501. You can use test cases for local invocation to verify service availability.
curl -X POST -H "Content-Type: application/json" http://127.0.0.1:8501/v1/models/m:predict -d @test.json












Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback