-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel_eval.py
175 lines (133 loc) · 5.46 KB
/
model_eval.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from sklearn import metrics, decomposition
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sn
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers.experimental import preprocessing
from tensorflow.keras.models import Sequential
from tensorflow.keras.applications import EfficientNetB0
import mlflow
import pickle
import cv2
mlflow.set_tracking_uri("sqlite:///mydb.sqlite")
# n = no. of classes
def get_confusion_matrix(true_labels, pred_labels,n):
cm = metrics.confusion_matrix(true_labels, pred_labels)
df_cm = pd.DataFrame(cm, range(n), range(n))
heatmap = sn.heatmap(df_cm)
figure = heatmap.get_figure()
path = 'run_latest/conf_mat.png'
figure.savefig(path, dpi=400)
return path
def get_classification_report(true_labels, pred_labels, run_id, target_names):
class_report = metrics.classification_report(true_labels,pred_labels,target_names=target_names,output_dict=True)
with mlflow.start_run(run_id=run_id):
for key in target_names:
mlflow.log_metric(key.replace("(","").replace(")","")+" f1-score",class_report[key]['f1-score'])
df = pd.DataFrame(class_report).transpose()
path = 'run_latest/classification_report.csv'
df.to_csv(path)
return path
def get_pca(data, n_components = 2):
pca = decomposition.PCA()
pca.n_components = n_components
pca_data = pca.fit_transform(data)
return pca_data
def plot_representations(data, labels, classes, n_images = None):
if n_images is not None:
data = data[:n_images]
labels = labels[:n_images]
fig = plt.figure(figsize = (10, 10))
ax = fig.add_subplot(111)
scatter = ax.scatter(data[:, 0], data[:, 1], c = labels, cmap = 'tab10')
handles, labels = scatter.legend_elements()
legend = ax.legend(handles = handles, labels = classes)
path = 'run_latest/pca_analysis.png'
fig.savefig(path)
return path
def get_pca_plot(test_dataset_dir, model, classes):
IMG_SIZE=224
NUM_CLASSES=48
test_dataset = tf.keras.preprocessing.image_dataset_from_directory(
test_dataset_dir,
seed=123,
image_size=(IMG_SIZE, IMG_SIZE))
base_model = EfficientNetB0(include_top=False, weights='imagenet')
global_average_layer = tf.keras.layers.GlobalAveragePooling2D()
preprocess_input = tf.keras.applications.efficientnet.preprocess_input
prediction_layer = tf.keras.layers.Dense(NUM_CLASSES,activation='softmax')
inputs = tf.keras.Input(shape=(IMG_SIZE, IMG_SIZE, 3))
#y = rescale(inputs)
x = preprocess_input(inputs)
x = base_model(x, training=False)
outputs = global_average_layer(x)
model1 = tf.keras.Model(inputs, outputs)
model1.set_weights(model.get_weights()[:len(model1.get_weights())])
intermediates = []
labels = []
for x, y in test_dataset:
output = list(model1.predict(x))
intermediates.extend(output)
labels.extend(list(y))
output_pca_data = get_pca(intermediates)
path = plot_representations(output_pca_data, labels, classes)
return path
def model_eval_fns(test_dataset_dir, model_path, run_id):
sign_names = pd.read_csv('signnames_added_classes.csv')
classes = dict(zip(list(sign_names['ClassId']), list(sign_names['SignName'])))
IMG_SIZE=224
NUM_CLASSES=48
model = tf.keras.models.load_model(model_path)
# Show the model architecture
model.summary()
test_dataset = tf.keras.preprocessing.image_dataset_from_directory(
test_dataset_dir,
seed=123,
image_size=(IMG_SIZE, IMG_SIZE))
y1 = np.array([])
y2 = np.array([])
for x, y in test_dataset:
y_true=np.argmax(model.predict(x),axis=1)
y1=np.append(y1,y_true)
y2=np.append(y2,y.numpy())
path1 = get_confusion_matrix(y1, y2, NUM_CLASSES)
path2 = get_classification_report(y1, y2, run_id, target_names=classes.values())
path3 = get_pca_plot(test_dataset_dir, model, classes)
with mlflow.start_run(run_id=run_id):
mlflow.log_artifact(path1)
mlflow.log_artifact(path2)
mlflow.log_artifact(path3)
import heapq
def predict_to_csv(img_path, model_path = "final_model_test.h5"):
model = tf.keras.models.load_model(model_path)
with open('mapping.pickle', 'rb') as handle:
mapping = pickle.load(handle)
sign_names = pd.read_csv('signnames_added_classes.csv')
classes = dict(zip(list(sign_names['ClassId']), list(sign_names['SignName'])))
IMG_SIZE = 224
NUM_CLASSES = 48
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image,(IMG_SIZE,IMG_SIZE))
plt.figure()
plt.imshow(image)
image = np.expand_dims(image, axis=0)
pred = model.predict(image)[0]
top5softmax = np.zeros((5,1))
top5softmax = heapq.nlargest(5, range(NUM_CLASSES), pred.take)
output_dict = {}
path = "predict_backend.csv"
for i in range(5):
prob = pred[top5softmax[i]]
output_dict[prob] = sign_names["SignName"][int(mapping[top5softmax[i]])]
with open(path, 'w') as f:
for key in output_dict.keys():
f.write("%f, %s\n" % (key, output_dict[key]))
return path
# if __name__ == "__main__":
# test_dataset_dir = "/home/lordgrim/Final_interiit/datasets/Test_dataset_48_classes"
# model_path = "/home/lordgrim/Final_interiit/latest_model"
# model_eval_fns(test_dataset_dir, model_path, classes)