suko commited on
Commit
ec8aa40
1 Parent(s): a4ef6cc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +104 -0
app.py ADDED
@@ -0,0 +1,104 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import json
4
+ import os
5
+ from PIL import Image
6
+ import onnxruntime as rt
7
+ class ONNXModel:
8
+ def __init__(self, dir_path) -> None:
9
+ """Method to get name of model file. Assumes model is in the parent directory for script."""
10
+ model_dir = os.path.dirname(dir_path)
11
+ with open(os.path.join(model_dir, "signature.json"), "r") as f:
12
+ self.signature = json.load(f)
13
+ self.model_file = os.path.join(model_dir, self.signature.get("filename"))
14
+ if not os.path.isfile(self.model_file):
15
+ raise FileNotFoundError(f"Model file does not exist")
16
+ # get the signature for model inputs and outputs
17
+ self.signature_inputs = self.signature.get("inputs")
18
+ self.signature_outputs = self.signature.get("outputs")
19
+ self.session = None
20
+ if "Image" not in self.signature_inputs:
21
+ raise ValueError("ONNX model doesn't have 'Image' input! Check signature.json, and please report issue to Lobe.")
22
+ # Look for the version in signature file.
23
+ # If it's not found or the doesn't match expected, print a message
24
+ version = self.signature.get("export_model_version")
25
+ if version is None or version != EXPORT_MODEL_VERSION:
26
+ print(
27
+ f"There has been a change to the model format. Please use a model with a signature 'export_model_version' that matches {EXPORT_MODEL_VERSION}."
28
+ )
29
+
30
+ def load(self) -> None:
31
+ """Load the model from path to model file"""
32
+ # Load ONNX model as session.
33
+ self.session = rt.InferenceSession(path_or_bytes=self.model_file)
34
+
35
+ def predict(self, image: Image.Image) -> dict:
36
+ """
37
+ Predict with the ONNX session!
38
+ """
39
+ # process image to be compatible with the model
40
+ img = self.process_image(image, self.signature_inputs.get("Image").get("shape"))
41
+ # run the model!
42
+ fetches = [(key, value.get("name")) for key, value in self.signature_outputs.items()]
43
+ # make the image a batch of 1
44
+ feed = {self.signature_inputs.get("Image").get("name"): [img]}
45
+ outputs = self.session.run(output_names=[name for (_, name) in fetches], input_feed=feed)
46
+ return self.process_output(fetches, outputs)
47
+
48
+ def process_image(self, image: Image.Image, input_shape: list) -> np.ndarray:
49
+ """
50
+ Given a PIL Image, center square crop and resize to fit the expected model input, and convert from [0,255] to [0,1] values.
51
+ """
52
+ width, height = image.size
53
+ # ensure image type is compatible with model and convert if not
54
+ if image.mode != "RGB":
55
+ image = image.convert("RGB")
56
+ # center crop image (you can substitute any other method to make a square image, such as just resizing or padding edges with 0)
57
+ if width != height:
58
+ square_size = min(width, height)
59
+ left = (width - square_size) / 2
60
+ top = (height - square_size) / 2
61
+ right = (width + square_size) / 2
62
+ bottom = (height + square_size) / 2
63
+ # Crop the center of the image
64
+ image = image.crop((left, top, right, bottom))
65
+ # now the image is square, resize it to be the right shape for the model input
66
+ input_width, input_height = input_shape[1:3]
67
+ if image.width != input_width or image.height != input_height:
68
+ image = image.resize((input_width, input_height))
69
+
70
+ # make 0-1 float instead of 0-255 int (that PIL Image loads by default)
71
+ image = np.asarray(image) / 255.0
72
+ # format input as model expects
73
+ return image.astype(np.float32)
74
+
75
+ def process_output(self, fetches: dict, outputs: dict) -> dict:
76
+ # un-batch since we ran an image with batch size of 1,
77
+ # convert to normal python types with tolist(), and convert any byte strings to normal strings with .decode()
78
+ out_keys = ["label", "confidence"]
79
+ results = {}
80
+ for i, (key, _) in enumerate(fetches):
81
+ val = outputs[i].tolist()[0]
82
+ if isinstance(val, bytes):
83
+ val = val.decode()
84
+ results[key] = val
85
+ confs = results["Confidences"]
86
+ labels = self.signature.get("classes").get("Label")
87
+ output = [dict(zip(out_keys, group)) for group in zip(labels, confs)]
88
+ sorted_output = {"predictions": sorted(output, key=lambda k: k["confidence"], reverse=True)}
89
+ return sorted_output
90
+ EXPORT_MODEL_VERSION=1
91
+ model = ONNXModel(dir_path="../model.onnx")
92
+ model.load()
93
+
94
+ def predict(image):
95
+ image = Image.fromarray(np.uint8(image), 'RGB')
96
+ prediction = model.predict(image)
97
+ for output in prediction["predictions"]:
98
+ output["confidence"] = round(output["confidence"], 2)
99
+ return prediction
100
+
101
+ inputs = gr.inputs.Image(type="pil")
102
+ outputs = gr.outputs.JSON()
103
+
104
+ gr.Interface(fn=predict, inputs=inputs, outputs=outputs).launch()