|
import gradio as gr |
|
import numpy as np |
|
import json |
|
import os |
|
from PIL import Image |
|
import onnxruntime as rt |
|
class ONNXModel: |
|
def __init__(self, dir_path) -> None: |
|
"""Method to get name of model file. Assumes model is in the parent directory for script.""" |
|
model_dir = os.path.dirname(dir_path) |
|
with open(os.path.join(model_dir, "signature.json"), "r") as f: |
|
self.signature = json.load(f) |
|
self.model_file = os.path.join(model_dir, self.signature.get("filename")) |
|
if not os.path.isfile(self.model_file): |
|
raise FileNotFoundError(f"Model file does not exist") |
|
|
|
self.signature_inputs = self.signature.get("inputs") |
|
self.signature_outputs = self.signature.get("outputs") |
|
self.session = None |
|
if "Image" not in self.signature_inputs: |
|
raise ValueError("ONNX model doesn't have 'Image' input! Check signature.json, and please report issue to Lobe.") |
|
|
|
|
|
version = self.signature.get("export_model_version") |
|
if version is None or version != EXPORT_MODEL_VERSION: |
|
print( |
|
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}." |
|
) |
|
|
|
def load(self) -> None: |
|
"""Load the model from path to model file""" |
|
|
|
self.session = rt.InferenceSession(path_or_bytes=self.model_file) |
|
|
|
def predict(self, image: Image.Image) -> dict: |
|
""" |
|
Predict with the ONNX session! |
|
""" |
|
|
|
img = self.process_image(image, self.signature_inputs.get("Image").get("shape")) |
|
|
|
fetches = [(key, value.get("name")) for key, value in self.signature_outputs.items()] |
|
|
|
feed = {self.signature_inputs.get("Image").get("name"): [img]} |
|
outputs = self.session.run(output_names=[name for (_, name) in fetches], input_feed=feed) |
|
return self.process_output(fetches, outputs) |
|
|
|
def process_image(self, image: Image.Image, input_shape: list) -> np.ndarray: |
|
""" |
|
Given a PIL Image, center square crop and resize to fit the expected model input, and convert from [0,255] to [0,1] values. |
|
""" |
|
width, height = image.size |
|
|
|
if image.mode != "RGB": |
|
image = image.convert("RGB") |
|
|
|
if width != height: |
|
square_size = min(width, height) |
|
left = (width - square_size) / 2 |
|
top = (height - square_size) / 2 |
|
right = (width + square_size) / 2 |
|
bottom = (height + square_size) / 2 |
|
|
|
image = image.crop((left, top, right, bottom)) |
|
|
|
input_width, input_height = input_shape[1:3] |
|
if image.width != input_width or image.height != input_height: |
|
image = image.resize((input_width, input_height)) |
|
|
|
|
|
image = np.asarray(image) / 255.0 |
|
|
|
return image.astype(np.float32) |
|
|
|
def process_output(self, fetches: dict, outputs: dict) -> dict: |
|
|
|
|
|
out_keys = ["label", "confidence"] |
|
results = {} |
|
for i, (key, _) in enumerate(fetches): |
|
val = outputs[i].tolist()[0] |
|
if isinstance(val, bytes): |
|
val = val.decode() |
|
results[key] = val |
|
confs = results["Confidences"] |
|
labels = self.signature.get("classes").get("Label") |
|
output = [dict(zip(out_keys, group)) for group in zip(labels, confs)] |
|
sorted_output = {"predictions": sorted(output, key=lambda k: k["confidence"], reverse=True)} |
|
return sorted_output |
|
EXPORT_MODEL_VERSION=1 |
|
model = ONNXModel(dir_path="model.onnx") |
|
model.load() |
|
|
|
def predict(image): |
|
image = Image.fromarray(np.uint8(image), 'RGB') |
|
prediction = model.predict(image) |
|
for output in prediction["predictions"]: |
|
output["confidence"] = round(output["confidence"], 4) |
|
return prediction |
|
|
|
inputs = gr.inputs.Image(type="pil") |
|
outputs = gr.outputs.JSON() |
|
|
|
runtime=gr.Interface(title="Naked Detector",fn=predict, inputs=inputs, outputs=outputs) |
|
runtime.launch() |