| | import onnxruntime as ort
|
| | from PIL import Image
|
| | import numpy as np
|
| | from torchvision import transforms
|
| | import gradio as gr
|
| |
|
| |
|
| | session = ort.InferenceSession("mnist_resnet18.onnx")
|
| |
|
| |
|
| | transform = transforms.Compose([
|
| | transforms.Grayscale(num_output_channels=3),
|
| | transforms.Resize((224, 224)),
|
| | transforms.ToTensor(),
|
| | transforms.Normalize([0.485, 0.456, 0.406],
|
| | [0.229, 0.224, 0.225])
|
| | ])
|
| |
|
| |
|
| | def predict(image):
|
| | """
|
| | image: PIL.Image
|
| | zwraca: przewidziana cyfra (0-9)
|
| | """
|
| |
|
| | img_t = transform(image).unsqueeze(0).numpy()
|
| |
|
| |
|
| | outputs = session.run(None, {"input": img_t})
|
| |
|
| |
|
| | pred = int(np.argmax(outputs[0]))
|
| | return pred
|
| |
|
| |
|
| | iface = gr.Interface(
|
| | fn=predict,
|
| | inputs=gr.Image(type="pil"),
|
| | outputs="number",
|
| | title="MNIST ResNet18 ONNX API",
|
| | description="Prze艣lij obraz cyfry 0-9, model ResNet18 (ONNX) zwr贸ci predykcj臋."
|
| | )
|
| |
|
| |
|
| | iface.launch()
|
| |
|