Skip to content
Snippets Groups Projects
Commit 273728f1 authored by titouan's avatar titouan
Browse files

Classe Main ajoutée.

Fusion des deux méthodes d'erreurs
Retrait des méthodes getMaxSize et computeMaxSize
parent 4a30885f
No related branches found
No related tags found
No related merge requests found
Main.py 0 → 100644
from sklearn.naive_bayes import GaussianNB
import TP
path1_t = "./Init/Mer"
path2_t = "./Init/Ailleurs"
S = TP.buildSampleFromPath(path1_t, path2_t)
classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, GaussianNB())
TP.predictFromHisto(S, classifier)
print("Erreur empirique :", TP.computeError(S_train), "erreurs")
print("Erreur réelle :", TP.computeError(S_test), "erreurs")
print("Taux de réussite : ", TP.computeScore(S_test)*100, "%")
......@@ -5,8 +5,6 @@ import pandas as pd
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from random import shuffle
from sklearn.naive_bayes import GaussianNB
def buildSampleFromPath(path1, path2, size=0):
......@@ -19,7 +17,7 @@ def buildSampleFromPath(path1, path2, size=0):
:return: list"""
S = []
max_size = (224, 224) # getMaxSize(path1, path2)
max_size = (224, 224)
path_list = os.listdir(path1)[:size if size > 0 else -1]
for image_path in path_list:
S.append(computeDict(image_path, path1, 1, max_size))
......@@ -54,36 +52,6 @@ def computeDict(image_path, path, y_true_value, max_size: tuple):
"y_predicted_class": None}
def getMaxSize(path1, path2):
"""
fetch the size of the image with the most pixels in both images folder
:param path1: first folder path
:param path2: second folder path containing images
:return: tuple with max width, max height
"""
max_size_x, max_size_y = 0, 0
max_size_x, max_size_y = computeMaxSizeInFolder(max_size_x, max_size_y, path1)
max_size_x, max_size_y = computeMaxSizeInFolder(max_size_x, max_size_y, path2)
return max_size_x, max_size_y
def computeMaxSizeInFolder(max_size_x, max_size_y, path1):
"""
actually do the real calculation to get the max pixels
:param max_size_x: current max width
:param max_size_y: current max height
:param path1: current folder path
:return: new max width, new max height in a tuple
"""
for image_path in os.listdir(path1):
full_path = os.path.join(path1, image_path)
image = Image.open(full_path)
if max_size_x * max_size_y < image.size[0] * image.size[1]:
max_size_x = image.size[0]
max_size_y = image.size[1]
return max_size_x, max_size_y
def resizeImage(i, h, l):
"""
Resizing the image following the LANCZOS algorithm, with the given width and height
......@@ -142,7 +110,7 @@ def predictFromHisto(S, model, list_dict=True):
return tab
def empiricalError(S):
def computeError(S):
"""
Compute the empirical error of the model on the given sample.
:param S: the sample to test
......@@ -154,24 +122,12 @@ def empiricalError(S):
error_count += 1
return error_count
def realError(S_test):
"""
Compute the real error of the model on the test sample.
:param S_test: the test sample to test
:return: the real error of the model on the test sample.
"""
error_count = 0
for image in S_test:
if image["y_predicted_class"] != image["y_true_class"]:
error_count += 1
return error_count
def accuracyScore(S_test):
y_test = []
y_predicts = []
for image in S_test:
y_test.append(image["y_true_class"])
y_predicts.append(image["y_predicted_class"])
score = accuracy_score(y_test,y_predicts)
print("Taux de réussite : ", score*100, "%")
def computeScore(S):
y_true_classes = []
y_predicted_classes = []
for image in S:
y_true_classes.append(image["y_true_class"])
y_predicted_classes.append(image["y_predicted_class"])
return accuracy_score(y_true_classes,y_predicted_classes)
......@@ -26,19 +26,19 @@ def test_sample():
path2 = "./Init/Ailleurs"
TP.buildSampleFromPath(path1, path2)
def real_test():
def global_test():
S = TP.buildSampleFromPath(path1_t, path2_t)
classifier, S_test, y_test, S_train, y_train = TP.fitFromHisto(S, GaussianNB())
TP.predictFromHisto(S, classifier)
print(TP.empiricalError(S_train))
print(TP.realError(S_test))
TP.accuracyScore(S_test)
print("Erreur empirique :", TP.computeError(S_train), "erreurs")
print("Erreur réelle :", TP.computeError(S_test), "erreurs")
print("Taux de réussite : ", TP.computeScore(S_test)*100, "%")
if __name__ == "__main__":
#image = Image.open("./Init/Mer/838s.jpg")
test_Histogram()
#test_resizeImage(image, 1000, 1000)
real_test()
global_test()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment