CoolFace
Apppublic

onnx/ShuffleNet-v2

sourceHugging Faceupdated 5y agoView on Hugging Face
0likes
app.py57 linesDownload Raw Back to root
1import onnx2import numpy as np3import onnxruntime as ort4from PIL import Image5import cv26import os7import gradio as gr8 9import mxnet10from torchvision import transforms11 12os.system("wget https://s3.amazonaws.com/onnx-model-zoo/synset.txt")13 14 15with open('synset.txt', 'r') as f:16    labels = [l.rstrip() for l in f]17    18os.system("wget https://github.com/AK391/models/raw/main/vision/classification/shufflenet/model/shufflenet-v2-10.onnx")19 20os.system("wget https://s3.amazonaws.com/model-server/inputs/kitten.jpg")21 22 23 24model_path = 'shufflenet-v2-10.onnx'25model = onnx.load(model_path)26session = ort.InferenceSession(model.SerializeToString())27 28 29    30preprocess = transforms.Compose([31        transforms.Resize(256),32        transforms.CenterCrop(224),33        transforms.ToTensor(),34        transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),35])36        37 38    39 40def predict(img):41    input_tensor = preprocess(img)42    img = input_tensor.unsqueeze(0)43    ort_inputs = {session.get_inputs()[0].name: img.cpu().detach().numpy()}44    preds = session.run(None, ort_inputs)[0]45    preds = np.squeeze(preds)46    a = np.argsort(preds)47    results = {}48    for i in a[0:5]:    49        results[labels[a[i]]] = float(preds[a[i]])50    return results51       52 53title="ShuffleNet-v2"54description="ShuffleNet is a deep convolutional network for image classification. ShuffleNetV2 is an improved architecture that is the state-of-the-art in terms of speed and accuracy tradeoff used for image classification."55 56examples=[['kitten.jpg']]57gr.Interface(predict,gr.inputs.Image(type='pil'),"label",title=title,description=description,examples=examples).launch(enable_queue=True,debug=True)