ONNX 运行模型

import onnx
import onnxruntime as ort
import numpy as np
import cv2

import sys


def preprocess(img_data):
    mean_vec = np.array([0.485, 0.456, 0.406])
    stddev_vec = np.array([0.229, 0.224, 0.225])
    norm_img_data = np.zeros(img_data.shape).astype('float32')
    for i in range(img_data.shape[0]):
         # for each pixel in each channel, divide the value by 255 to get value between [0, 1] and then normalize
        norm_img_data[i,:,:] = (img_data[i,:,:]/255 - mean_vec[i]) / stddev_vec[i]
    return norm_img_data


if __name__ == "__main__":
	if len(sys.argv) < 3:
        print("usage program model image")
        exit()
        
    model_name = sys.argv[1]
    img_name = sys.argv[2]
		
    img = cv2.imread(img_name)
    img = cv2.resize(img, (512,512), interpolation=cv2.INTER_AREA)
    #img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    input_data = np.transpose(img, (2, 0, 1))
    input_data = preprocess(input_data)
    input_data = input_data.reshape([1, 3, 512, 512])

    sess = ort.InferenceSession(model_name)
    input_name = sess.get_inputs()[0].name
    result = sess.run([], {input_name: input_data})
    result = np.reshape(result, [1, -1])
    index = np.argmax(result)
    print("max index:", index)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值