Deeplabv3+昇腾适配推理验证任务心得
验证过程
明确目的后开始熟悉项目,Deeplabv3+的github网址:https://github.com/qubvel-org/segmentation_models.pytorch
segmentation_models.pytorch的环境配置
1 资源清单
产品名称 |
NPU架构 |
CPU架构 |
操作系统 |
云堡垒机 |
Ascend 910B3 |
鲲鹏计算 Kunpeng-920 |
Huawei Cloud EulerOS 2.0 (aarch64) |
2 验证流程
1、安装segmentation_models.pytorch依赖包
可在requirements文件夹require.txt下修改依赖包版本
efficientnet-pytorch==0.7.1
huggingface_hub==0.26.3
numpy==2.1.3
pillow==11.0.0
pretrainedmodels==0.7.4
six==1.16.0
timm==1.0.12
torch==2.5.1
torchvision==0.20.1
tqdm==4.67.1
默认没有安装opencv,需要安装python-opencv
2、安装SMP依赖项
Install SMP
make install_dev # create .venv, install SMP in dev mode
Run tests and code checks
make fixup # Ruff for formatting and lint checks
Update table with encoders
make table # generate a table with encoders and print to stdout
3、安装onnxruntime
4、生成onnx模型文件并验证有效性
model = smp.DeepLabV3Plus("resnet34", encoder_weights="imagenet", classes=1)
model = model.eval()
# dynamic_axes is used to specify the variable length axes. it can be just batch size
dynamic_axes = {0: "batch_size", 2: "height", 3: "width"}
onnx_model_name = "DeepLabV3Plus_resnet34.onnx"
onnx_model = torch.onnx.export(
model, # model being run
torch.randn(1, 3, 224, 224), # model input
onnx_model_name, # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
opset_version=12, # the ONNX version to export
do_constant_folding=True, # whether to execute constant folding for optimization
input_names=["input"], # the model's input names
output_names=["output"], # the model's output names
dynamic_axes={ # variable length axes
"input": dynamic_axes,
"output": dynamic_axes,
},
)
onnx_model = onnx.load(onnx_model_name)
onnx.checker.check_model(onnx_model)
check_model没有报错日志即验证成功
"onnx test is ok!"
5、加载onnx验证CPU推理结果
sample = torch.randn(1,3, 512, 512)
img=np.expand_dims(sample)
img=np.array(img,dtype=np.float32)
batch,height,width,channel=img.shape
img=np.reshape(img,(batch,channel,height,width))
ort_session = onnxruntime.InferenceSession(
onnx_model_name, providers=["CPUExecutionProvider"]
)
# compute ONNX Runtime output prediction
# ort_inputs = {"input": sample.numpy()}
ort_inputs = {"input": sample}
ort_outputs = ort_session.run(output_names=None, input_feed=ort_inputs)
ort_outputs
tensor([[[[-0.1246, -0.8691, 1.2468, ..., 1.5282, -1.2732, 0.9794],
[ 1.3723, -0.0419, 2.8774, ..., 2.5053, 0.2462, 1.1938],
[ 0.7045, -1.1840, -0.5846, ..., 0.7469, 0.3499, 0.8009],
...,
[-0.8097, 0.1798, 0.3107, ..., -1.2473, 1.5366, -0.3955],
[-0.1176, -0.0104, 0.4736, ..., 0.8608, -0.8950, 0.6830],
[-1.6339, -1.4623, 0.6656, ..., -0.6432, -2.3936, -0.6228]],
6、onnx转om模型推理
安装cann后利用如下代码进行转换
atc --model=deeplabv3_plus.onnx --framework=5 --output=deeplabv3_plus --soc_version=Ascend310 -- --input_shape="input:1,3,512,512"
转换成功后可以在服务器上进行om模型推理
加载模型设置大小为(1,3,512,512)维度的随机图像tensor进行推理
# 从文件加载离线模型数据
model_path = './model/deeplabv3_plus.om'
model = acllite_model(model_path)
# 根据构建好的模型输入数据进行模型推理
img=torch.randn(1,3,1024,1024)
result_list = model.execute([img])
#得到推理结果并且reshape成512*512,并且转换成uint8类型
result_img = result_list[0].reshape(512, 512)
result_img = result_img.astype('uint8')
result_img.shape
print(result_img)
打印result_img输出结果完成推理
tensor([[[[ 0.1919, -0.7563, -1.1512, ..., 0.2721, -0.1220, 1.4461],
[ 0.9982, 0.6150, 0.0191, ..., -2.4493, -0.8513, -0.6102],
[ 0.7011, -0.2566, 1.3187, ..., -0.4520, -1.4002, -0.1832],
...,
[-0.6632, -0.7704, 0.8694, ..., 0.9940, 0.7905, -0.2253],
[-0.0981, 0.1034, -0.3128, ..., -0.1130, -0.0628, -0.8793],
[-0.9770, -0.6159, -0.6841, ..., -0.2381, 1.0263, -0.2224]],
[[ 0.1441, 1.7850, 0.7764, ..., 0.2099, -0.3699, 0.8952],
[ 0.3328, 0.8359, 2.3847, ..., -2.0905, 0.2405, -0.2678],
[ 0.4478, 0.4810, -0.3092, ..., 0.8745, -1.2963, 0.6621],
...,
[ 0.1475, 0.2986, -0.8419, ..., 0.0538, 0.0745, 0.5989],
[ 1.1479, 0.3992, -1.0586, ..., -1.3081, 1.4740, -1.3389],
[-1.0331, -2.2473, 1.1847, ..., -0.7303, 2.7132, 0.0633]],
- 点赞
- 收藏
- 关注作者
评论(0)