ONNX分割模型推理

可根据具体需要修改

import glob
import os
import shutil
import time
import cv2

import onnxruntime
import numpy as np
import tqdm

# Load ONNX model
onnx_file_name = "./save_weights/best_model.onnx"
session = onnxruntime.InferenceSession(onnx_file_name)
start_time = time.time()
paths = glob.glob("./UNet/Dataset/test/*.*")

result_path = './onnx'

if os.path.exists(result_path):
    shutil.rmtree(result_path)
os.mkdir(result_path)

image_size = 224
# Load golden images
for path in tqdm.tqdm(paths):
    if 'image' not in path and 'implant' in path:
        continue
    # Compute ONNX model output
    origin_image = cv2.imread(path)
    original_height, original_width, i = origin_image.shape
    image = cv2.cvtColor(origin_image, cv2.COLOR_BGR2RGB)
    image = cv2.resize(image, (image_size, image_size), interpolation=cv2.INTER_CUBIC)
    image = np.array(image, np.float32)
    image = image / 127.5 - 1

    image = np.expand_dims(image, 0)
    input_name = session.get_inputs()[0].name
    output_name = session.get_outputs()[0].name
    result = session.run([output_name], {input_name: image})

    # Compare ONNX model output with golden image
    output_image = result[0].argmax(3).squeeze(0)
    output_image[output_image == 1] = 129
    output_image[output_image == 2] = 192
    output_image[output_image == 3] = 255
    prediction = output_image.astype(np.uint8)

    predict_result = cv2.resize(prediction, (original_width, original_height), interpolation=cv2.INTER_NEAREST)
    cv2.imwrite(os.path.join(result_path, os.path.splitext(os.path.basename(path))[0] + "_origin.png"), origin_image)
    cv2.imwrite(os.path.join(result_path, os.path.splitext(os.path.basename(path))[0] + "_predict.png"), predict_result)

total_time = time.time() - start_time
print("time {}s, fps {}".format(total_time, 100 / total_time))

  • 9
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值