Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pickle import HIGHEST_PROTOCOL
|
2 |
+
# Import necessary libraries
|
3 |
+
import numpy as np
|
4 |
+
import math
|
5 |
+
import matplotlib.pyplot as plt
|
6 |
+
import cv2
|
7 |
+
import json
|
8 |
+
import gradio as gr
|
9 |
+
from huggingface_hub import hf_hub_download
|
10 |
+
from onnx import hub
|
11 |
+
import onnxruntime as ort
|
12 |
+
import tempfile
|
13 |
+
import onnx
|
14 |
+
|
15 |
+
# Load the ONNX model from ONNX Model Zoo
|
16 |
+
model = hub.load("efficientnet-lite4")
|
17 |
+
|
18 |
+
# Save the ModelProto object to a temporary file
|
19 |
+
with tempfile.NamedTemporaryFile(suffix=".onnx", delete=False) as temp_file:
|
20 |
+
onnx.save(model, temp_file.name)
|
21 |
+
model_path = temp_file.name
|
22 |
+
|
23 |
+
# Load the labels from a text file
|
24 |
+
labels = json.load(open("/content/drive/MyDrive/labels_map.txt", "r"))
|
25 |
+
|
26 |
+
# Define a function to preprocess the image for the EfficientNet-Lite4 model
|
27 |
+
def pre_process_edgetpu(img, dims):
|
28 |
+
# Unpack the dimensions
|
29 |
+
output_height, output_width, _ = dims
|
30 |
+
# Resize the image while maintaining aspect ratio
|
31 |
+
img = resize_with_aspectratio(
|
32 |
+
img,
|
33 |
+
output_height,
|
34 |
+
output_width,
|
35 |
+
inter_pol=cv2.INTER_LINEAR
|
36 |
+
)
|
37 |
+
# Crop the image from the center
|
38 |
+
img = center_crop(img, output_height, output_width)
|
39 |
+
# Convert image to float32 numpy array
|
40 |
+
img = np.asarray(img, dtype='float32')
|
41 |
+
# Normalize pixel values from [0-255] to [-1.0, 1.0]
|
42 |
+
img -= [127.0, 127.0, 127.0]
|
43 |
+
img /= [128.0, 128.0, 128.0]
|
44 |
+
return img
|
45 |
+
|
46 |
+
# Define a function to resize the image while maintaining aspect ratio
|
47 |
+
def resize_with_aspectratio(
|
48 |
+
img,
|
49 |
+
out_height,
|
50 |
+
out_width,
|
51 |
+
scale=87.5,
|
52 |
+
inter_pol=cv2.INTER_LINEAR):
|
53 |
+
# Get original image dimensions
|
54 |
+
height, width, _ = img.shape
|
55 |
+
# Calculate new dimensions
|
56 |
+
new_height = int(100. * out_height / scale)
|
57 |
+
new_width = int(100. * out_width / scale)
|
58 |
+
# Determine which dimension to scale based on aspect ratio
|
59 |
+
if height > width:
|
60 |
+
w = new_width
|
61 |
+
h = int(new_height * height / width)
|
62 |
+
else:
|
63 |
+
h = new_height
|
64 |
+
w = int(new_width * width / height)
|
65 |
+
# Resize the image
|
66 |
+
img = cv2.resize(img, (w, h), interpolation=inter_pol)
|
67 |
+
return img
|
68 |
+
|
69 |
+
# Define a function to crop the image from the center
|
70 |
+
def center_crop(img, out_height, out_width):
|
71 |
+
# Get image dimensions
|
72 |
+
height, width, _ = img.shape
|
73 |
+
# Calculate crop coordinates
|
74 |
+
left = int((width - out_width) / 2)
|
75 |
+
right = int((width + out_width) / 2)
|
76 |
+
top = int((height - out_height) / 2)
|
77 |
+
bottom = int((height + out_height) / 2)
|
78 |
+
# Crop the image
|
79 |
+
img = img[top:bottom, left:right]
|
80 |
+
return img
|
81 |
+
|
82 |
+
# Create an ONNX Runtime inference session
|
83 |
+
sess = ort.InferenceSession(model_path)
|
84 |
+
|
85 |
+
# Define the main inference function
|
86 |
+
def inference(img):
|
87 |
+
# Read the image file
|
88 |
+
img = cv2.imread(img)
|
89 |
+
# Convert BGR to RGB color space
|
90 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
91 |
+
|
92 |
+
# Preprocess the image
|
93 |
+
img = pre_process_edgetpu(img, (224, 224, 3))
|
94 |
+
|
95 |
+
# Add batch dimension to the image
|
96 |
+
img_batch = np.expand_dims(img, axis=0)
|
97 |
+
|
98 |
+
# Run inference using the ONNX model
|
99 |
+
results = sess.run(["Softmax:0"], {"images:0": img_batch})[0]
|
100 |
+
# Get the top 5 predictions
|
101 |
+
result = reversed(results[0].argsort()[-5:])
|
102 |
+
# Create a dictionary to store results
|
103 |
+
resultdic = {}
|
104 |
+
for r in result:
|
105 |
+
resultdic[labels[str(r)]] = float(results[0][r])
|
106 |
+
return resultdic
|
107 |
+
|
108 |
+
# Set up the Gradio interface
|
109 |
+
title = "EfficientNet-Lite4"
|
110 |
+
description = """EfficientNet-Lite 4 is the largest variant and most accurate of the set of
|
111 |
+
EfficientNet-Lite model. It is an integer-only quantized model that produces the HIGHEST_PROTOCOL
|
112 |
+
accuracy of all of the EfficientNet models. It achieves 80.4% ImageNet top-1 accuracy, while
|
113 |
+
still running in real-time (e.g. 30ms/image) on a Pixel 4 CPU."""
|
114 |
+
|
115 |
+
examples = [['catonnx.jpg']]
|
116 |
+
|
117 |
+
# Launch the Gradio interface
|
118 |
+
gr.Interface(
|
119 |
+
inference,
|
120 |
+
gr.Image(type="filepath"),
|
121 |
+
"label",
|
122 |
+
title=title,
|
123 |
+
description=description,
|
124 |
+
examples=examples).launch()
|
125 |
+
|
126 |
+
|