akhaliq HF staff commited on
Commit
f56da98
1 Parent(s): c51520c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -29
app.py CHANGED
@@ -3,12 +3,12 @@ import matplotlib.pyplot as plt
3
  import numpy as np
4
  from collections import namedtuple
5
  from mxnet.gluon.data.vision import transforms
6
- from mxnet.contrib.onnx.onnx2mx.import_model import import_model
7
  import os
8
  import gradio as gr
9
 
10
  from PIL import Image
11
  import imageio
 
12
 
13
  def get_image(path):
14
  '''
@@ -39,41 +39,28 @@ mx.test_utils.download('https://s3.amazonaws.com/onnx-model-zoo/synset.txt')
39
  with open('synset.txt', 'r') as f:
40
  labels = [l.rstrip() for l in f]
41
 
42
- os.system("wget https://github.com/onnx/models/raw/main/vision/classification/inception_and_googlenet/googlenet/model/googlenet-9.onnx")
43
 
44
- # Enter path to the ONNX model file
45
-
46
- sym, arg_params, aux_params = import_model('googlenet-9.onnx')
47
-
48
- Batch = namedtuple('Batch', ['data'])
49
 
50
 
51
  def predict(path):
52
- img = get_image(path)
53
- img = preprocess(img)
54
- mod.forward(Batch([img]))
55
- # Take softmax to generate probabilities
56
- scores = mx.ndarray.softmax(mod.get_outputs()[0]).asnumpy()
57
- # print the top-5 inferences class
58
- scores = np.squeeze(scores)
59
- a = np.argsort(scores)[::-1]
60
  results = {}
61
  for i in a[0:5]:
62
- results[labels[i]] = float(scores[i])
63
  return results
64
-
65
- # Determine and set context
66
- if len(mx.test_utils.list_gpus())==0:
67
- ctx = mx.cpu()
68
- else:
69
- ctx = mx.gpu(0)
70
- # Load module
71
- mod = mx.mod.Module(symbol=sym, context=ctx, data_names=['data_0'], label_names=None)
72
- mod.bind(for_training=False, data_shapes=[('data_0', (1,3,224,224))],label_shapes=mod._label_shapes)
73
- mod.set_params(arg_params, aux_params, allow_missing=True, allow_extra=True)
74
 
75
- title="GoogleNet"
76
- description="GoogLeNet is the name of a convolutional neural network for classification, which competed in the ImageNet Large Scale Visual Recognition Challenge in 2014."
77
 
78
  examples=[['catonnx.jpg']]
79
- gr.Interface(predict,gr.inputs.Image(type='filepath'),"label",title=title,description=description,examples=examples).launch(enable_queue=True)
 
3
  import numpy as np
4
  from collections import namedtuple
5
  from mxnet.gluon.data.vision import transforms
 
6
  import os
7
  import gradio as gr
8
 
9
  from PIL import Image
10
  import imageio
11
+ import onnxruntime as ort
12
 
13
  def get_image(path):
14
  '''
 
39
  with open('synset.txt', 'r') as f:
40
  labels = [l.rstrip() for l in f]
41
 
42
+ os.system("wget https://github.com/AK391/models/raw/main/vision/classification/alexnet/model/bvlcalexnet-7.onnx")
43
 
44
+ ort_session = ort.InferenceSession("bvlcalexnet-7.onnx")
 
 
 
 
45
 
46
 
47
  def predict(path):
48
+ img_batch = preprocess(get_image(path))
49
+
50
+ outputs = ort_session.run(
51
+ None,
52
+ {"data_0": img_batch.astype(np.float32)},
53
+ )
54
+
55
+ a = np.argsort(-outputs[0].flatten())
56
  results = {}
57
  for i in a[0:5]:
58
+ results[labels[i]]=float(outputs[0][0][i])
59
  return results
60
+
 
 
 
 
 
 
 
 
 
61
 
62
+ title="AlexNet"
63
+ description="AlexNet is the name of a convolutional neural network for classification, which competed in the ImageNet Large Scale Visual Recognition Challenge in 2012."
64
 
65
  examples=[['catonnx.jpg']]
66
+ gr.Interface(predict,gr.inputs.Image(type='filepath'),"label",title=title,description=description,examples=examples).launch(enable_queue=True,debug=True)