akhaliq HF Staff commited on
Commit
3d83121
·
1 Parent(s): c615a1a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
app.py CHANGED
@@ -7,6 +7,32 @@ from mxnet.contrib.onnx.onnx2mx.import_model import import_model
7
  import os
8
  import gradio as gr
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  mx.test_utils.download('https://s3.amazonaws.com/model-server/inputs/kitten.jpg')
11
 
12
  mx.test_utils.download('https://s3.amazonaws.com/onnx-model-zoo/synset.txt')
@@ -20,28 +46,10 @@ os.system("wget https://github.com/onnx/models/raw/main/vision/classification/in
20
  sym, arg_params, aux_params = import_model('googlenet-9.onnx')
21
 
22
  Batch = namedtuple('Batch', ['data'])
23
- def get_image(path, show=False):
24
- img = mx.image.imread(path)
25
- if img is None:
26
- return None
27
- if show:
28
- plt.imshow(img.asnumpy())
29
- plt.axis('off')
30
- return img
31
-
32
- def preprocess(img):
33
- transform_fn = transforms.Compose([
34
- transforms.Resize(256),
35
- transforms.CenterCrop(224),
36
- transforms.ToTensor(),
37
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
38
- ])
39
- img = transform_fn(img)
40
- img = img.expand_dims(axis=0)
41
- return img
42
 
43
  def predict(path):
44
- img = get_image(path, show=True)
45
  img = preprocess(img)
46
  mod.forward(Batch([img]))
47
  # Take softmax to generate probabilities
 
7
  import os
8
  import gradio as gr
9
 
10
+ from PIL import Image
11
+ import imageio
12
+
13
+ def get_image(path):
14
+ '''
15
+ Using path to image, return the RGB load image
16
+ '''
17
+ img = imageio.imread(path, pilmode='RGB')
18
+ return img
19
+
20
+ # Pre-processing function for ImageNet models using numpy
21
+ def preprocess(img):
22
+ '''
23
+ Preprocessing required on the images for inference with mxnet gluon
24
+ The function takes loaded image and returns processed tensor
25
+ '''
26
+ img = np.array(Image.fromarray(img).resize((224, 224))).astype(np.float32)
27
+ img[:, :, 0] -= 123.68
28
+ img[:, :, 1] -= 116.779
29
+ img[:, :, 2] -= 103.939
30
+ img[:,:,[0,1,2]] = img[:,:,[2,1,0]]
31
+ img = img.transpose((2, 0, 1))
32
+ img = np.expand_dims(img, axis=0)
33
+
34
+ return img
35
+
36
  mx.test_utils.download('https://s3.amazonaws.com/model-server/inputs/kitten.jpg')
37
 
38
  mx.test_utils.download('https://s3.amazonaws.com/onnx-model-zoo/synset.txt')
 
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