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 |
conda create -n TIONE-test python=3.8conda activate TIONE-test
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 | - | No |
{} indicates optional parameters. Select one parameter and fill it in.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 |
import osimport loggingimport torchfrom typing import Dict, List, Unionimport numpy as npimport cv2cv2.setNumThreads(5)import tiinferimport tiinfer.utilsimport base64import urllibfrom utils.general import non_max_suppression, scale_coordsfrom 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 = Noneself.imgsz=640 # Input image size.self.conf_thres=0.05self.iou_thres=0.45 # for NMSself.max_det = 20self.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-exceptlogging.error("load model failed: %s", e)return Falsereturn 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 conversionimg_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 standardizationc, h, w = img_mat.shapeimg_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 dataexcept 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
/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.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
curl -X POST -H "Content-Type: application/json" http://127.0.0.1:8501/v1/models/m:predict -d @test.json
Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback