-
Notifications
You must be signed in to change notification settings - Fork 1
/
predictions.py
51 lines (39 loc) · 1.87 KB
/
predictions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import tensorflow as tf
from transformers import AutoFeatureExtractor
from pandas import DataFrame
import numpy as np
import onnxruntime as rt
import onnx
from transformers.onnx.features import FeaturesManager
categories = ['daisy', 'rose', 'tulip', 'dandelion', 'sunflower']
def vit_predict(image):
# ViT Preprocessing
image_processor = AutoFeatureExtractor.from_pretrained("google/vit-base-patch16-224")
inputs = image_processor(image, return_tensors="np")
providers = ['CPUExecutionProvider']
m = rt.InferenceSession("./saved_model/tf-vit-model.onnx", providers=providers)
input_name = m.get_inputs()[0].name
output_names = [n.name for n in m.get_outputs()]
pred = m.run(output_names, {input_name: inputs['pixel_values']})
class_prediction = np.argmax(pred[0])
probabilities = np.exp(pred[0])/np.sum(np.exp(pred[0]))
d = DataFrame([categories, probabilities.reshape(-1,1)]).T
d.columns = ["Flower", "Confidence"]
d.sort_values(by='Confidence', inplace=True, ascending=False)
d["Confidence"] = d["Confidence"].apply(lambda row: f"{row[0] * 100:.1f}%")
return d.reset_index(drop=True)
def cnn_predict(image):
# CNN Preprocessing
image = image.resize((224,224))
arr = np.expand_dims(tf.keras.preprocessing.image.img_to_array(image), axis=0)
providers = ['CPUExecutionProvider']
m = rt.InferenceSession("./saved_model/model.onnx", providers=providers)
input_name = m.get_inputs()[0].name
output_names = [n.name for n in m.get_outputs()]
pred = m.run(output_names, {input_name: arr})
class_prediction = np.argmax(pred[0])
d = DataFrame([categories, pred[0].reshape(-1,1)]).T
d.columns = ["Flower", "Confidence"]
d.sort_values(by='Confidence', inplace=True, ascending=False)
d["Confidence"] = d["Confidence"].apply(lambda row: f"{row[0] * 100:.1f}%")
return d.reset_index(drop=True)