Skip to content
Snippets Groups Projects
Commit 9ab7e696 authored by paul_pvc's avatar paul_pvc
Browse files

Fixed Gabor, made corrections with the threads, removed the rotations,

The rotation was slowing down the calculation + lowering the cross-validation score, due to an unknown issue
parent 1038f423
No related branches found
No related tags found
No related merge requests found
...@@ -21,21 +21,15 @@ print("Erreur réelle ( ͡° _ʖ ͡°):", TP.computeError(S_test), "erreurs") ...@@ -21,21 +21,15 @@ print("Erreur réelle ( ͡° _ʖ ͡°):", TP.computeError(S_test), "erreurs")
print("Taux de réussite (╯°□°)╯︵ ┻━┻ : ", TP.computeScore(S_test), "%")""" print("Taux de réussite (╯°□°)╯︵ ┻━┻ : ", TP.computeScore(S_test), "%")"""
svc_params = {"kernel": ('linear', 'rbf'), "C": [1,10]} svc_params = {"kernel": ('linear', 'rbf'), "C": [1,10]}
xgb_params = {"n_estimators": [1,10], "max_depth" : [0,10], "max_leaves": [0, 10],
"grow_policy" : ("depthwise", "lossguide"), "learning_rate" : [0.01, 0.2],
"booster" :("gbtree", "gblinear", "dart")}
rand_forest_params = {"n_estimators" : [100, 200], "criterion" : ("gini", "entropy", "log_loss")}
knn_params = {"n_neighbors" : [1, 10], "weights" : ("uniform", 'distance'), "algorithm" : ("auto", "ball_tree", "kd_tree", "brute"),
"leaf_size" : [15,45], "p" : [1., 3.]}
result = GridSearchCV(SVC(), svc_params) result = GridSearchCV(SVC(), svc_params)
S = TP.buildSampleFromPath(path1_t, path2_t) S = TP.buildSampleFromPath(path1_t, path2_t)
#classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, result) #classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, result)
"""TP.predictFromHisto(S, classifier) #TP.predictFromHisto(S, classifier)
print("Erreur empirique щ(ºДºщ):", TP.computeError(S_train), "erreurs") #print("Taux de réussite (╯°□°)╯︵ ┻━┻ : ", TP.computeScore(S_test), "%")
print("Erreur réelle ( ͡° _ʖ ͡°):", TP.computeError(S_test), "erreurs")
print("Taux de réussite (╯°□°)╯︵ ┻━┻ : ", TP.computeScore(S_test), "%")"""
print("Taux de réussite en cross validation SVC: ", TP.get_cross_val_score(result, S), "%") print("Taux de réussite en cross validation SVC: ", TP.get_cross_val_score(result, S), "%")
#print("Taux de réussite en cross validation SVC: ", TP.get_cross_val_score(XGBClassifier(), S), "%")
#print("Taux de réussite en cross validation XGBOOST: ", TP.get_cross_val_score(XGBClassifier(), S_train, S_test, y_train, y_test), "%") #print("Taux de réussite en cross validation XGBOOST: ", TP.get_cross_val_score(XGBClassifier(), S_train, S_test, y_train, y_test), "%")
#print("Taux de réussite en cross validation randomForest: ", TP.get_cross_val_score(GridSearchCV(RandomForestClassifier(), rand_forest_params), S_train, S_test, y_train, y_test), "%") #print("Taux de réussite en cross validation randomForest: ", TP.get_cross_val_score(GridSearchCV(RandomForestClassifier(), rand_forest_params), S_train, S_test, y_train, y_test), "%")
#print("Taux de réussite en cross validation KNeighbors: ", TP.get_cross_val_score(GridSearchCV(KNeighborsClassifier(), knn_params), S_train, S_test, y_train, y_test), "%") #print("Taux de réussite en cross validation KNeighbors: ", TP.get_cross_val_score(GridSearchCV(KNeighborsClassifier(), knn_params), S_train, S_test, y_train, y_test), "%")
......
...@@ -7,7 +7,8 @@ from sklearn.metrics import accuracy_score ...@@ -7,7 +7,8 @@ from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split from sklearn.model_selection import train_test_split
from skimage.feature import graycomatrix, graycoprops from skimage.feature import graycomatrix, graycoprops
from sklearn.model_selection import cross_val_score from sklearn.model_selection import cross_val_score
from skimage.filters import gabor from skimage.filters import gabor, unsharp_mask
from threading import *
import math import math
from sklearn.naive_bayes import GaussianNB from sklearn.naive_bayes import GaussianNB
...@@ -42,7 +43,7 @@ def fetch_images_to_dict(path, size=0, y_true_class=-1): ...@@ -42,7 +43,7 @@ def fetch_images_to_dict(path, size=0, y_true_class=-1):
images = [] images = []
path_list = os.listdir(path)[:size if size > 0 else -1] path_list = os.listdir(path)[:size if size > 0 else -1]
for image_path in path_list: for image_path in path_list:
images.append(computeDict(image_path, path, y_true_class, MAX_SIZE)) images += computeDict(image_path, path, y_true_class, MAX_SIZE)
return images return images
...@@ -72,34 +73,6 @@ def compute_glcm_caracteristics(image_gl): ...@@ -72,34 +73,6 @@ def compute_glcm_caracteristics(image_gl):
return [graycoprops(glcm, 'dissimilarity')[0, 0], graycoprops(glcm, 'correlation')[0, 0], graycoprops(glcm, 'contrast')[0, 0], return [graycoprops(glcm, 'dissimilarity')[0, 0], graycoprops(glcm, 'correlation')[0, 0], graycoprops(glcm, 'contrast')[0, 0],
graycoprops(glcm, 'energy')[0, 0], graycoprops(glcm, 'homogeneity')[0, 0]] graycoprops(glcm, 'energy')[0, 0], graycoprops(glcm, 'homogeneity')[0, 0]]
"""
def compute_4_histos(resized):
image = resized.copy()
histos = []
histos += computeHisto(image.crop((0,0, 112, 112)))
histos += computeHisto(image.crop((112,0, 224, 112)))
histos += computeHisto(image.crop((0,112, 112, 224)))
histos += computeHisto(image.crop((112, 112, 224, 224)))
return histos
def compute_4_glcm(resized):
image = resized.copy()
glcms = []
glcms = compute_glcm_caracteristics(image.crop((0, 0, 112, 112)))
glcms += compute_glcm_caracteristics(image.crop((0, 112, 112, 224)))
glcms += compute_glcm_caracteristics(image.crop((112, 0, 224, 112)))
glcms += compute_glcm_caracteristics(image.crop((112, 112, 224, 224)))
return glcms
def summer(glcms, image, croped):
a = compute_glcm_caracteristics(image.crop(croped))
for i in range(len(a)):
glcms[i] += a[i]
"""
def computeDict(image_path, path, y_true_value, max_size: tuple): def computeDict(image_path, path, y_true_value, max_size: tuple):
""" """
...@@ -115,21 +88,48 @@ def computeDict(image_path, path, y_true_value, max_size: tuple): ...@@ -115,21 +88,48 @@ def computeDict(image_path, path, y_true_value, max_size: tuple):
image = Image.open(full_path) image = Image.open(full_path)
image = image.convert("RGB") image = image.convert("RGB")
resized = resizeImage(image, *max_size) # On ne stocke pas resized image, on calcule tout avant de l'oublier resized = resizeImage(image, *max_size)
image_gl = resized.convert("L") #print(np.asarray(resized))
a = np.uint8(unsharp_mask(np.asarray(resized), radius=10, amount=1)*255)
#print(a)
resized = Image.fromarray(a)
#a.save("ttt.png")# On ne stocke pas resized image, on calcule tout avant de l'oublier
#image_gl = resized.convert("L")
rotated = [resized]#, resized.rotate(90), resized.rotate(180), resized.rotate(270)]
#rotated_gl = [im.convert("L") for im in rotated]
rotated_gl = [resized.convert('L')]
#rotated_gl += [rotated_gl[0].rotate(90), rotated_gl[0].rotate(180), rotated_gl[0].rotate(270)]
histogram = computeHisto(resized)
result = []
#create_dictionnary_image(full_path, histogram, rotated_gl[0], y_true_value, result)
threads = []
for image_gl in rotated_gl:
thread = Thread(target=create_dictionnary_image, args=(full_path, histogram, image_gl, y_true_value, result))
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
return result
#print(computePixelBW_histo(resized)) #print(computePixelBW_histo(resized))
return {"name_path": full_path, #return [create_dictionnary_image(full_path, histogram, image_gl, y_true_value) for image_gl in rotated_gl]
def create_dictionnary_image(full_path, histogram, image_gl, y_true_value, liste):
liste.append({"name_path": full_path,
# "resized_image": resized, # "resized_image": resized,
"X_histo": computeHisto(resized), "X_histo": histogram,
"X_pixelbw": computePixelBW_histo(resized), # "X_pixelbw": computePixelBW_histo(resized),
#"X_glcm_data": extract_data_glcm(compute_glcm(resized)),
"X_glcm_data": compute_glcm_caracteristics(image_gl), "X_glcm_data": compute_glcm_caracteristics(image_gl),
# "X_splitted_histo": compute_4_histos(resized), # "X_splitted_histo": compute_4_histos(resized),
# "X_splitted_glcm": compute_4_glcm(image_gl), # "X_splitted_glcm": compute_4_glcm(image_gl),
"gabor_features": get_gabor_filters(image_gl), "gabor_features": get_gabor_filters(image_gl),
"y_true_class": y_true_value, "y_true_class": y_true_value,
"y_predicted_class": None} "y_predicted_class": None})
def resizeImage(i, h, l): def resizeImage(i, h, l):
...@@ -155,17 +155,27 @@ def get_gabor_filters(image): ...@@ -155,17 +155,27 @@ def get_gabor_filters(image):
image_arr = np.asarray(image) image_arr = np.asarray(image)
#print(image_arr.shape, image_arr) #print(image_arr.shape, image_arr)
#frequencies = [0.2] #frequencies = [0.2]
thetas = [0, np.pi / 2] #wthetas = [0, np.pi / 2]
features = [] features = []
for theta in thetas: #for theta in thetas:
filt_real, filt_imag = gabor(image_arr, frequency=0.2, theta=theta) filt_real, filt_imag = gabor(image_arr, frequency=0.2, theta=0)
features.append(filt_real.mean()) # Moyenne du filtre réel features.append(filt_imag.mean())
features.append(filt_real.var()) # Variance du filtre réel features.append(filt_imag.var())
features.append(filt_imag.mean()) # Moyenne du filtre imaginaire mean = filt_real.mean()
features.append(filt_imag.var()) # Variance du filtre imaginaire
features.append(mean)
return np.array(features).tolist() #features.append(filt_real.mean())
features.append(filt_real.var())
#features.append(np.sum(filt_real**2))
features.append(np.max(filt_real) - np.min(filt_real))
#return np.array(features).tolist()
return features
def extract_relevant_data(l: dict) -> list: def extract_relevant_data(l: dict) -> list:
...@@ -277,10 +287,6 @@ def get_cross_val_score(classifier, S): ...@@ -277,10 +287,6 @@ def get_cross_val_score(classifier, S):
y = np.array(df["y_true_class"]) y = np.array(df["y_true_class"])
S_train, S_test, y_train, y_test = train_test_split(S, y, test_size=0.2) X = np.array([np.array(extract_relevant_data(l)) for l in S])
X_train = np.array([np.array(extract_relevant_data(l)) for l in S_train]) scores = cross_val_score(classifier, X, y, n_jobs=-1)
X_test = np.array([np.array(extract_relevant_data(l)) for l in S_test])
X = np.concatenate([X_train,X_test])
y = np.concatenate([y_train,y_test])
scores = cross_val_score(classifier, X, y, cv=10)
return np.mean(scores)*100 return np.mean(scores)*100
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment