-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccuracy_images_script.py
More file actions
287 lines (225 loc) · 9.74 KB
/
accuracy_images_script.py
File metadata and controls
287 lines (225 loc) · 9.74 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#Import
import pathlib
import os
import sys
import json
import argparse
import pandas as pd
from sklearn.utils import shuffle
import json
import tensorflow as tf
from tensorflow import keras
from keras import utils, layers, metrics
from keras.models import Sequential
import pickle
import pandas as pd
import cv2
import numpy as np
import os
from sklearn import preprocessing
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Load model libraries
import sklearn
from sklearn.ensemble import AdaBoostClassifier
from sklearn import datasets
# Import train_test_split function
from sklearn.model_selection import train_test_split
#Import scikit-learn metrics module for accuracy calculation
from sklearn import metrics
# Import Support Vector Classifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
import numpy as np
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score,confusion_matrix
#models
from sklearn.neighbors import KNeighborsClassifier
#DEFINE SOME CONSTANTS
BATCH_SIZE = 32
IMG_HEIGHT = 128
IMG_WIDTH = 128
class accuracy_calculator:
def __init__(self, images_path, model_path, scale) -> None:
self.path = images_path
self.model_path = model_path #this is the path of the directory with all the model
self.scale = scale #percentage of the data to get
def load_data_for_NN(self):
data_dir = pathlib.Path(self.path)
self.val_ds = tf.keras.utils.image_dataset_from_directory(data_dir,seed=123,image_size=(IMG_HEIGHT, IMG_WIDTH),batch_size=BATCH_SIZE)
#set class number
self.class_names = self.val_ds.class_names
self.class_number = len(self.class_names)
def load_data_for_other_analysis(self):
images = []
labels = []
flag = cv2.IMREAD_GRAYSCALE
for class_dir in os.listdir(self.path): #for each class name dir
for filename in os.listdir(os.path.join(self.path, class_dir)): # for each images
img = cv2.imread(os.path.join(self.path, class_dir,filename), flag) #load in greyscale
new_array=cv2.resize(img,(IMG_HEIGHT,IMG_WIDTH))
if img is not None:
images.append(new_array)
labels.append(class_dir)
print(f"{class_dir} loaded")
#get only a percentage of the data
images = images[0:int(len(images) * self.scale)]
labels = labels[0:int(len(labels) * self.scale)]
#split train and validation set
self.X_val, self.y_val = shuffle(images, labels)
#print the shapes
print(f"\nLen X: {len(self.X_val)}")
print(f"Len y: {len(self.y_val)}")
def adaboost_accuracy(self, model_name):
#define the parameter
model_category = "ADABOOST"
adaboost_model_path = f"{self.model_path}/{model_category}/{model_name}.sav"
# convert dataset in order to be used for AdaBoost
X_val_AB = []
for i in range(len(self.X_val)):
X_val_AB.append(self.X_val[i].flatten())
#define the model
svc=SVC(probability=True, kernel='linear')
model = AdaBoostClassifier(n_estimators=20, base_estimator=svc,learning_rate=1, random_state=101)
#load the weight
print(F"Loading {model_category} model --")
exists = os.path.isfile(adaboost_model_path)
if exists:
with open(adaboost_model_path, "rb") as sav_file:
model = pickle.load(sav_file)
else:
raise Exception("{model_category} model unavailable, check the path")
print(F"{model_category} model loaded --\nStart Prediction")
#predict the results
y_pred = model.predict(X_val_AB)
print(f"{model_category} model Accuracy:", "%.2f" % ( metrics.accuracy_score(self.y_val, y_pred) * 100))
def svm_accuracy(self, model_name):
#define the parameter
model_category = "SVM"
svm_model_path = f"{self.model_path}/{model_category}/{model_name}.sav"
# convert dataset in order to be used for AdaBoost
X_val_SVM = []
for i in range(len(self.X_val)):
X_val_SVM.append(self.X_val[i].flatten().astype("float32")/255)
y_val_SVM = [self.class_names.index(i) for i in self.y_val]
#convert the type
y_val_SVM = np.array(y_val_SVM)
#define the model
model = SVC(kernel='linear',gamma='auto', random_state=101)
#load the weight
print(F"Loading {model_category} model --")
exists = os.path.isfile(svm_model_path)
if exists:
with open(svm_model_path, "rb") as sav_file:
model = pickle.load(sav_file)
else:
raise Exception("{model_category} model unavailable, check the path")
print(F"{model_category} model loaded --\nStart Prediction")
#predict the results
y_pred = model.predict(X_val_SVM)
print(f"{model_category} model Accuracy:", "%.2f" % ( metrics.accuracy_score(y_val_SVM, y_pred) * 100))
def cnn_accuracy(self, model_name):
#define the parameter
model_category = "NN"
cnn_model_path = f"{self.model_path}/{model_category}/{model_name}_model.h5"
#define the model
data_augmentation = keras.Sequential(
[layers.RandomFlip("horizontal",input_shape=(IMG_HEIGHT,IMG_WIDTH,3)),
layers.RandomRotation(0.1),
layers.RandomZoom(0.1),
])
model = Sequential([
data_augmentation,
layers.Rescaling(1./255),
layers.Conv2D(16, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(32, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Conv2D(64, 3, padding='same', activation='relu'),
layers.MaxPooling2D(),
layers.Dropout(0.2),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(self.class_number)
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
#load the weig ht
print(F"Loading {model_category} {model_name} --")
exists = os.path.isfile(cnn_model_path)
if exists:
with open(cnn_model_path, "rb") as sav_file:
model = keras.models.load_model(cnn_model_path)
else:
raise Exception("{model_category} model unavailable, check the path")
print(F"{model_category} model loaded --\nStart Prediction")
#predict the results
test_loss, test_acc = model.evaluate(self.val_ds, verbose=0)
print(f"{model_category} {model_name} Accuracy: % {test_acc * 100}")
def de_accuracy(self, model_name):
#define the parameter
model_category = "DE"
de_model_path = f"{self.model_path}/{model_category}/{model_name}.sav"
# convert dataset in order to be used for AdaBoost
X_val_DE = []
for i in range(len(self.X_val)):
X_val_DE.append(self.X_val[i].flatten().astype("float32"))
y_val_DE = [self.class_names.index(i) for i in self.y_val]
#convert the type
y_val_DE = np.array(y_val_DE)
#define the model
model = KNeighborsClassifier(n_neighbors = 5, metric = 'minkowski', p = 2)
#load the weight
print(F"Loading {model_category} model --")
exists = os.path.isfile(de_model_path)
if exists:
with open(de_model_path, "rb") as sav_file:
model = pickle.load(sav_file)
else:
raise Exception("{model_category} model unavailable, check the path")
print(F"{model_category} model loaded --\nStart Prediction")
#predict the results
y_pred = model.predict(X_val_DE)
print(f"{model_category} {model_name} Accuracy:", "%.2f" % ( metrics.accuracy_score(y_val_DE, y_pred) * 100))
def run(images_path, model_path, scale):
ac = accuracy_calculator(images_path, model_path, scale)
print(f"Load images---\n")
ac.load_data_for_NN()
ac.load_data_for_other_analysis()
print("Images Loaded---\n")
#define the name of the models
MODEL_NAME_ADABOOST = "ADABOOST_SCV"
MODEL_NAME_SVM = "SVM_linear_best"
MODEL_NAME_CNN = "NN_CNN_2"
MODEL_NAME_DE = "NearestNeighbors"
#compute the accuracy
ac.adaboost_accuracy(MODEL_NAME_ADABOOST)
ac.svm_accuracy(MODEL_NAME_SVM)
ac.cnn_accuracy(MODEL_NAME_CNN)
ac.de_accuracy(MODEL_NAME_DE)
def parseArguments():
#create argument Parser
parser = argparse.ArgumentParser()
#positional mandatory argument
parser.add_argument("images_path", help="path of the directory with all the images", type=str)
parser.add_argument("models_path", help="path of the directory with all the images", type=str)
#optional argument
parser.add_argument("-s", "--scale" , help="Percentage of the data to get, is a value between (0,1)", type=float, default=1)
# Print version
parser.add_argument("--version", action="version", version='%(prog)s - Version 1.0')
# Parse arguments
args = parser.parse_args()
return args
if __name__=="__main__":
#MODEL_PATH = "/home/giuseppe/Scrivania/model_accuracy/models_images" #TODO MODIFICARE
args = parseArguments()
#Raw print arguments
print("You are running the script with arguments: ")
for a in args.__dict__:
print(str(a) + ": " + str(args.__dict__[a]))
#run function
run(args.images_path, args.models_path, args.scale)
'''
to run:
python accuracy_images_script.py --scale 0.05 "/home/giuseppe/Scrivania/universita/Magistrale/Machine and Deep Learning/Progetto ML/Dataset/immagini-3/immagini-3" "/home/giuseppe/Scrivania/model_accuracy/models_images"
'''