Example of Dynamic Resolution Inference (ResNet-50)
Sample Code
import numpy as np
from mindx.sdk import base
from mindx.sdk.base import Tensor
import cv2 as cv
try:
# Set the path of the image to be inferred.
jpg_path = "./test.jpg"
# Read the original image.
img = cv.imread(jpg_path)
# Resize the image to 224 x 224. The output size format is (width, height).
img_test1 = cv.resize(img, (224, 224))
# Perform CSC: BGR > YUV420
input_image = cv.cvtColor(img_test1, cv.COLOR_BGR2YUV_I420)
device_id = 1 # Device ID
base.mx_init() # Perform global initialization and allocate device and log resources.
input_image1 = np.reshape(input_image,((1,) + input_image.shape[:2] + (1,)))
i imgTensor = [Tensor(input_image1)] # Convert the Image class to the Tensor class.
model_path = "./resnet50_dynamic_image.om" # Model path
model_ = base.model(model_path, deviceId=device_id) # Create a model object.
outputs = model_.infer(imgTensor) # Use a model to perform inference on tensor objects.
label_path = "./resnet50_clsidx_to_labels.names" # Classification label file path
output0 = outputs[0] # Obtain the inference result.
output0.to_host() # Transfer the inference result tensor to the host.
result0_array = np.array(output0) # Convert the inference result tensor into a NumPy array for post-processing.
max_idx = result0_array.argmax(axis=1)[0] # Obtain the label subscript index with the highest confidence score.
print('label_index:', max_idx) # Output the label subscript index with the highest confidence score.
with open(label_path, 'r', encoding='utf-8') as f: # Read classification label files.
file = f.read()
label_list = file.split('\n')[1:-1] # Obtain valid classification labels.
infer_result = label_list[max_idx] # Obtain label names.
print('infer result:{}'.format(infer_result)) # Print the inference result.
except Exception as e:
print(e)
Example
For details about the inference process when a dynamic resolution model uses the DVPP class for data pre-processing, see End-to-End Inference Processing Example (ResNet-50). This sample shows the data pre-processing and inference process using OpenCV.
The example is for reference only. For details about the commands and configurations for model building, see the CANN ATC Instructions.
If AIPP CSC is configured when the ATC tool is used, ensure that the CSC corresponds to the OM model input when the OpenCV reads images.
- Example of the dynamic resolution conversion command used for model building:
atc --model=./resnet50_v1.5.pb --framework=3 --output=resnet50_dynamic_image --output_type=FP32 --soc_version=Ascend310 --input_shape=input_tensor:1,-1,-1,3 --insert_op_conf=./aipp_resnet50_dynamic_image.aippconfig --dynamic_image_size="224,224;448,448"
- Example of dynamic resolution AIPP configuration:
aipp_op { aipp_mode: static input_format : YUV420SP_U8 csc_switch : true rbuv_swap_switch : false src_image_size_w : 0 src_image_size_h : 0 matrix_r0c0 : 256 matrix_r0c1 : 0 matrix_r0c2 : 359 matrix_r1c0 : 256 matrix_r1c1 : -88 matrix_r1c2 : -183 matrix_r2c0 : 256 matrix_r2c1 : 454 matrix_r2c2 : 0 input_bias_0 : 0 input_bias_1 : 128 input_bias_2 : 128 mean_chn_0 : 124 mean_chn_1 : 117 mean_chn_2 : 104 }
Parent topic: Samples