Inception_v2 / app.py
akhaliq's picture
akhaliq HF Staff
Update app.py
c12d225
import onnx
import numpy as np
import onnxruntime as ort
from PIL import Image
import cv2
import os
import gradio as gr
os.system("wget https://s3.amazonaws.com/onnx-model-zoo/synset.txt")
with open('synset.txt', 'r') as f:
labels = [l.rstrip() for l in f]
os.system("wget https://github.com/AK391/models/raw/main/vision/classification/inception_and_googlenet/inception_v2/model/inception-v2-9.onnx")
os.system("wget https://s3.amazonaws.com/model-server/inputs/kitten.jpg")
model_path = 'inception-v2-9.onnx'
model = onnx.load(model_path)
session = ort.InferenceSession(model.SerializeToString())
def get_image(path):
with Image.open(path) as img:
img = np.array(img.convert('RGB'))
return img
def preprocess(img):
'''
Preprocessing required on the images for inference with mxnet gluon
The function takes loaded image and returns processed tensor
'''
img = np.array(Image.fromarray(img).resize((224, 224))).astype(np.float32)
img[:, :, 0] -= 123.68
img[:, :, 1] -= 116.779
img[:, :, 2] -= 103.939
img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
img = img.transpose((2, 0, 1))
img = np.expand_dims(img, axis=0)
return img
def predict(path):
img = get_image(path)
img = preprocess(img)
ort_inputs = {session.get_inputs()[0].name: img}
preds = session.run(None, ort_inputs)[0]
preds = np.squeeze(preds)
a = np.argsort(preds)[::-1]
results = {}
for i in a[0:5]:
results[labels[a[i]]] = float(preds[a[i]])
return results
title="Inception v2"
description="Inception v2 is a deep convolutional networks for classification."
examples=[['kitten.jpg']]
gr.Interface(predict,gr.inputs.Image(type='filepath'),"label",title=title,description=description,examples=examples).launch(enable_queue=True,debug=True)