ID photo of Ciro Santilli taken in 2013 right eyeCiro Santilli OurBigBook logoOurBigBook.com  Sponsor 中国独裁统治 China Dictatorship 新疆改造中心、六四事件、法轮功、郝海东、709大抓捕、2015巴拿马文件 邓家贵、低端人口、西藏骚乱
lenet/infer.py
#!/usr/bin/env python

# https://cirosantilli.com/activatedgeek-lenet-5-use-onnx-for-inference

import os
import sys

import numpy as np
np.set_printoptions(threshold=sys.maxsize)
np.set_printoptions(linewidth=np.inf)

import onnxruntime
from PIL import Image

paths = sys.argv[1:]
session = onnxruntime.InferenceSession(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lenet.onnx'),
    None
)
input_name = session.get_inputs()[0].name
output_name = session.get_outputs()[0].name
for path in paths:
    # The final input to onnx is a 1x1x32x32 float32 numpy array
    # with values between 0.0 and 255.0
    img = Image.open(path)
    img = img.resize((32, 32), Image.LANCZOS)
    img = np.array([[img]])
    # The resolution is so small that we can actually visualize
    # it really well on the terminal.
    #print(img)
    img = img.astype('float32')

    result = session.run([output_name], { input_name: img })
    prediction=np.argmax(np.array(result).squeeze(), axis=0)
    print(f'{path} {prediction}')