参考:
https://blog.csdn.net/Deaohst/article/details/126864267
转onnx
别直接转onnx。
先转PB:
import tensorflow as tfmodel_path = './models/model.h5' # 模型文件
model = tf.keras.models.load_model(model_path)
model.save('tfmodel', save_format='tf')
再转onnx:
python -m tf2onnx.convert --saved-model ./tfmodel/ --output ./models/model.onnx --opset 12 --verbose
转化成功:
将原结果和onnx推理结果比对:
原结果:
{‘drawings’: 0.00619311910122633, ‘hentai’: 0.00011550176714081317, ‘neutral’: 0.992009162902832, ‘porn’: 0.0008918801322579384, ‘sexy’: 0.0007902580546215177}}
onnx推理代码和推理结果:
import cv2
import numpy as np
import onnxruntimeIMAGE_DIM = 299 # required/default image dimensionalitydef load_single_image(image_path, image_size, verbose=True):try:if verbose:print(image_path, "size:", image_size)# Load image using OpenCVimage = cv2.imread(image_path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # Convert to RGBimage = cv2.resize(image, (image_size, image_size))# Preprocess the imageimage = image.astype(np.float32) / 255.0return np.expand_dims(image, axis=0), image_pathexcept Exception as ex:print("Image Load Failure: ", image_path, ex)return None, None# Load ONNX model
onnx_model_path = './models/model.onnx'
ort_session = onnxruntime.InferenceSession(onnx_model_path)# Example usage:
image_path_to_load = "images/20230903000800.jpg"
loaded_image, loaded_image_path = load_single_image(image_path_to_load, IMAGE_DIM)if loaded_image is not None:# Perform inferenceinput_name = ort_session.get_inputs()[0].nameoutput_name = ort_session.get_outputs()[0].nameinput_data = loaded_image# Run the ONNX modelresult = ort_session.run([output_name], {input_name: input_data})print(result[0].tolist())
images/20230903000800.jpg size: 299
[[0.004163397941738367, 0.00018479839491192251, 0.9918997287750244, 0.0020591376814991236, 0.0016930525889620185]]
结果不是很吻合,但也大差不差了。
转fp16 onnx
安装:
pip install onnxmltools
执行脚本:
import onnxmltools
# 加载float16_converter转换器
from onnxmltools.utils.float16_converter import convert_float_to_float16
# 使用onnxmltools.load_model()函数来加载现有的onnx模型
# 但是请确保这个模型是一个fp32的原始模型
onnx_model = onnxmltools.load_model('./models/model.onnx')
# 使用convert_float_to_float16()函数将fp32模型转换成半精度fp16
onnx_model_fp16 = convert_float_to_float16(onnx_model)
# 使用onnx.utils.save_model()函数来保存,
onnxmltools.utils.save_model(onnx_model_fp16, './models/model_fp16.onnx')
推理结果:
images/20230903000800.jpg size: 299
[[0.004119873046875, 0.00018489360809326172, 0.99169921875, 0.002071380615234375, 0.001697540283203125]]