def classificationManhattan(indice): M = ldb.getData(indice) mini = np.inf index = 0 for i in range(10): d = distance.cityblock(M, DATA.matrice_moyenne[i]) if d < mini: index = i mini = d return index, mini
def Thickening_DB(nbData=70000, nom=""): dic = {} dic["derivation"] = np.zeros((nbData, 784)) i = 0 for image in range(nbData): data = ldb.getData(image).reshape((28, 28)) thicken = derivThickening(data) dic["derivation"][i] = thicken i += 1 savemat("Thickening" + nom, dic, do_compression=True)
def classificationCorrelation(indice): M = ldb.getData(indice) minimum = np.inf index = 0 for i in range(10): d = distance.correlation(M, DATA.matrice_moyenne[i]) if d < minimum: index = i minimum = d return index, minimum
def classificationChebyshev(indice): M = ldb.getData(indice) mini = np.inf index = 0 for i in range(10): d = distance.chebyshev(M, DATA.matrice_moyenne[i]) if d < mini: index = i mini = d return index, mini
def Scale_DB(nbData=70000, nom=""): dic = {} dic["derivation"] = np.zeros((nbData, 784)) i = 0 for image in range(nbData): data = ldb.getData(image).reshape((28, 28)) scale = derivScale(data) dic["derivation"][i] = scale i += 1 savemat("Scale" + nom, dic, do_compression=True)
def Rotation_DB(nbData=70000, nom="", sigma=9): dic = {} dic["derivation"] = np.zeros((nbData, 784)) i = 0 for image in range(nbData): data = ldb.getData(image).reshape((28, 28)) rot = derivRotation(data, sigma) dic["derivation"][i] = rot i += 1 savemat("Rotation" + nom, dic, do_compression=True)
def classificationTangeante(indice, testdb, trainingDb, derivsTest, derivsTraining, realLabel, log=False): p = testdb[indice] tp = np.array([derivsTest[indice]]).transpose() lst = [] for e in trainingDb: te = np.array([derivsTraining[e]]).transpose() me = ldb.getData(e) lst.append(generateT.TangenteDistance(p, me, tp, te)) #lst.append(np.linalg.norm(p-me)) # print(generateT.TangenteDistance(p,ldb.getData(db[0]),tp,np.array([Te[db[0]]]))) index = np.argmin(lst) if log and realLabel != ldb.getLabel(trainingDb[index]): print("Image reel") plt.imshow(np.array(p.reshape((28, 28))), cmap='gray') plt.figure() print("Derivée de l'image reel") plt.imshow(np.array(tp.reshape((28, 28))), cmap='gray') plt.figure() print("Image qui approxime le mieux") plt.imshow(np.array((ldb.getData(trainingDb[index])).reshape( (28, 28))), cmap='gray') plt.figure() print("Derivée de l'image qui approxime le mieux") plt.imshow(np.array([derivsTraining[trainingDb[index]]]).reshape( (28, 28)), cmap='gray') plt.figure() #print(index) return ldb.getLabel(trainingDb[index])
def classificationMinkowski(indice): M = ldb.getData(indice) minimum = np.inf index = 0 for i in range(10): d = distance.minkowski(M, DATA.matrice_moyenne[i], 3) if d < minimum: index = i minimum = d return index, minimum
def classificationMoyenne(indice): M = ldb.getData(indice) mini = np.inf index = 0 for i in range(10): D = np.subtract(M, DATA.matrice_moyenne[i]) d = np.linalg.norm(D) if d < mini: index = i mini = d return index, mini
def classificationTangeanteXY(indice): p = ldb.getData(indice) mini = np.inf tp = np.array([derivsX[indice], derivsY[indice]]).transpose() for i in range(10): e = DATA.matrice_moyenne[i] te = np.array([DerivsX_moyenne[i], DerivsY_moyenne[i]]).transpose() d = generateT.TangenteDistance(p, e, tp, te) if d < mini: index = i mini = d return index
def classificationNormeP(indice): p_value = 1.5 x = ldb.getData(indice) mini = np.inf index = 0 for i in range(10): y = DATA.matrice_moyenne[i] d = p_root(sum(pow(abs(a - b), p_value) for a, b in zip(x, y)), p_value) if d < mini: index = i mini = d return index, mini
def classificationTangeanteT(indice): p = ldb.getData(indice) mini = np.inf tp = np.array([derivsT[indice]]).transpose() for i in range(10): e = DATA.matrice_moyenne[i] te = np.array(DerivsT_moyenne[i]).reshape(784) te = np.array([te]).transpose() d = generateT.TangenteDistance(p, e, tp, te) if d < mini: index = i mini = d return index
def translationY_DB(): """ Fonction : calcul la derive de la translation par rapport à y pour chaque image dans la BD mnist et stock le resultat dans translate.mat """ dic = {} nbData = 70000 dic["derivation"] = np.zeros((nbData, 784)) i = 0 Gy = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]).transpose() for image in range(nbData): data = ldb.getData(image) deriv = convolve2d(data.reshape((28, 28)), Gy, mode='same') dic["derivation"][i] = deriv.reshape(784) i += 1 savemat("translateY", dic, do_compression=True)
def classificationCosinus(indice): M = ldb.getData(indice) normeM = np.linalg.norm(M) bestScore = 0 index = 0 for i in range(10): mean = DATA.matrice_moyenne[i] dotProduct = M.dot(mean) normeMean = np.linalg.norm(mean) cosinus = dotProduct / ( normeM * normeMean ) # normeM et normeMean sont forcement differentes de 0 if bestScore < cosinus: index = i bestScore = cosinus return index, bestScore
def translationX_DB(db=None, nom=""): """ Fonction : calcul la derive de la translation par rapport à x pour chaque image dans la BD mnist et stock le resultat dans translate.mat """ dic = {} if db == None: db = np.linspace(0, 70000, 1, dtype=int) nbData = len(db) dic["derivation"] = np.zeros((nbData, 784)) i = 0 Gx = np.array([[-1, 0, 1], [-2, 0, 2], [-1, 0, 1]]) for image in db: data = ldb.getData(image) deriv = convolve2d(data.reshape((28, 28)), Gx, mode='same') dic["derivation"][i] = deriv.reshape(784) i += 1 savemat("translateX" + nom, dic, do_compression=True)
def translationY_DB(nbData=70000, nom="", sigma=9): """ Fonction : calcul la derive de la translation par rapport à x pour chaque image dans la BD mnist et stock le resultat dans translate.mat """ dic = {} dic["derivation"] = np.zeros((nbData, 784)) i = 0 for image in range(nbData): data = ldb.getData(image).reshape((28, 28)) #plt.imshow(data.reshape((28, 28))) #plt.figure() #plt.imshow(data.reshape((28,28)),cmap='gray') #plt.show() deriv = deriv_translate_y(data, sigma) #deriv = (deriv-data)/sigma #deriv = convolve2d(data.reshape((28, 28)), Gx, mode='same') dic["derivation"][i] = deriv.reshape(784) i += 1 savemat("translateY" + nom, dic, do_compression=True)
def distance_de_base(label, image, M): v = ldb.getData(image) Mv = M[label].dot(v) # multiplication de M*v return np.linalg.norm(Mv)
# -*- coding: utf-8 -*- import load_DB as ldb import numpy as np Training, Test = ldb.seperateData() somme = [] for i in range(10): somme.append(np.zeros(784)) nb = [0 for i in range(10)] for e in Training: label = ldb.getLabel(e) arr = np.array(ldb.getData(e)) somme[label] = np.add(somme[label], arr) nb[label] += 1 moyenne = [] def calcMoy(somme, nb): moy = [] for e in somme: moy.append(int(e) // nb) return moy for i in range(10): moyenne.append(calcMoy(somme[i], nb[i]))