-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccuracy_text_script.py
More file actions
268 lines (205 loc) · 8.98 KB
/
accuracy_text_script.py
File metadata and controls
268 lines (205 loc) · 8.98 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
#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
#Preprocessing
from nltk.stem import WordNetLemmatizer
from sklearn.feature_extraction.text import CountVectorizer
import re
from sklearn.feature_extraction.text import TfidfTransformer
# 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
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import SGDClassifier
from sklearn.naive_bayes import ComplementNB
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
class accuracy_calculator:
def __init__(self, text_path, model_path, scale) -> None:
self.text_path = text_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(self):
data = pd.read_excel(self.text_path)
X = data["cleaned_website_text"]
y = data["Category"]
self.N_CLASSES = data['Category'].nunique()
self.CLASS_LABELS= data['Category'].unique()
stemmer = WordNetLemmatizer()
documents = []
for sen in range(0, len(X)):
# Remove all the special characters
document = re.sub(r'\W', ' ', str(X[sen]))
# remove all single characters
document = re.sub(r'\s+[a-zA-Z]\s+', ' ', document)
# Remove single characters from the start
document = re.sub(r'\^[a-zA-Z]\s+', ' ', document)
# Substituting multiple spaces with single space
document = re.sub(r'\s+', ' ', document, flags=re.I)
# Removing prefixed 'b'
document = re.sub(r'^b\s+', '', document)
# Converting to Lowercase
document = document.lower()
documents.append(document)
count_vect = CountVectorizer(stop_words="english")
X_train_counts = count_vect.fit_transform(documents)
tfidf_transformer = TfidfTransformer()
X = tfidf_transformer.fit_transform(X_train_counts)
self.X_val ,self.y_val = shuffle(X, y)
def adaboost_accuracy(self, model_name):
#define the parameter
model_category = "ADABOOST"
adaboost_model_path = f"{self.model_path}/{model_category}/{model_name}.sav"
#define the model
model = RandomForestClassifier(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(self.X_val)
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"
#define the model
model = SGDClassifier(loss='hinge', penalty='l2',alpha=1e-3, random_state=42, max_iter=5, tol=None)
#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(self.X_val)
print(f"{model_category} model Accuracy:", "%.2f" % ( metrics.accuracy_score(self.y_val, y_pred) * 100))
def nn_accuracy(self, model_name):
#define the parameter
model_category = "NN"
nn_model_path = f"{self.model_path}/{model_category}/{model_name}_model.h5"
#preprocessing
le = preprocessing.LabelEncoder()
y_val_ = le.fit(self.y_val)
y_val_ = le.transform(self.y_val)
#convert the sparse matrix to a dense matrix
X_val_ = self.X_val.toarray().astype(float)
model = keras.Sequential([
keras.layers.Input(shape=X_val_.shape[1]),
keras.layers.Dense(1280, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(640, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(320, activation="relu"),
keras.layers.Dropout(0.5),
keras.layers.Dense(self.N_CLASSES, activation="softmax"),
])
model.compile(loss='sparse_categorical_crossentropy', optimizer="Adam", metrics=["accuracy"])
#load the weig ht
print(F"Loading {model_category} {model_name} --")
exists = os.path.isfile(nn_model_path)
if exists:
with open(nn_model_path, "rb") as sav_file:
model = keras.models.load_model(nn_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(X_val_, y_val_, verbose=0)
print(f"{model_category} {model_name} Accuracy: %.2f {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"
#preprocessing
le = preprocessing.LabelEncoder()
y_val_ = le.fit(self.y_val)
y_val_ = le.transform(self.y_val)
#define the model
model = ComplementNB()
#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(self.X_val)
print(f"{model_category} {model_name} Accuracy:", "%.2f" % ( metrics.accuracy_score(y_val_, y_pred) * 100))
def run(images_path, model_path, scale):
ac = accuracy_calculator(images_path, model_path, scale)
print(f"Load text---\n")
ac.load_data()
print("Text Loaded---\n")
#define the name of the models
MODEL_NAME_ADABOOST = "ADABOOST_random_forest"
MODEL_NAME_SVM = "SVM_SGDClassifier"
MODEL_NAME_NN = "NN_dense_3"
MODEL_NAME_DE = "ComplementNB"
#compute the accuracy
ac.adaboost_accuracy(MODEL_NAME_ADABOOST) #OK
ac.svm_accuracy(MODEL_NAME_SVM) # OK
ac.nn_accuracy(MODEL_NAME_NN) #
ac.de_accuracy(MODEL_NAME_DE) #ok
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_text_script.py --scale 0.05 "/home/giuseppe/Scrivania/universita/Magistrale/Machine and Deep Learning/Progetto ML/Dataset/testi-3/testi-3/testi-3.xlsx" "/home/giuseppe/Scrivania/model_accuracy/models_text"'''