l_intelligence_du_semaphore
Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédenteProchaine révisionLes deux révisions suivantes | ||
l_intelligence_du_semaphore [2019/02/06 15:13] – serge | l_intelligence_du_semaphore [2020/02/04 16:27] – ↷ Liens modifiés en raison d'un déplacement. serge | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== L' | ====== L' | ||
+ | <WRAP center round box 80% centeralign> | ||
+ | **{{tagpage> | ||
+ | </ | ||
+ | <WRAP center round box 60% centeralign> | ||
+ | C'est quoi un sémaphore ? | ||
+ | {{ youtube> | ||
+ | {{ chappe.jpeg? | ||
+ | </ | ||
<WRAP center round box 60% centeralign> | <WRAP center round box 60% centeralign> | ||
- | **{{tagpage> | + | **Calcul de [[Le sémaphore avec TensorFlow|Le sémaphore avec TensorFlow]] |
</ | </ | ||
+ | =====Les sources sur GitHub===== | ||
+ | * **[[https:// | ||
+ | =====Les images utilisées pour l' | ||
+ | ====Images sorties de Blender==== | ||
+ | <WRAP group> | ||
+ | <WRAP third column> | ||
+ | {{: | ||
+ | </ | ||
+ | <WRAP third column> | ||
+ | {{: | ||
+ | </ | ||
+ | <WRAP third column> | ||
+ | {{: | ||
+ | </ | ||
+ | </ | ||
+ | ====Images adaptées pour l' | ||
+ | L' | ||
+ | <WRAP group> | ||
+ | <WRAP third column> | ||
+ | {{media_01: | ||
+ | </ | ||
+ | <WRAP third column> | ||
+ | {{media_01: | ||
+ | </ | ||
+ | <WRAP third column> | ||
+ | {{media_01: | ||
+ | </ | ||
+ | </ | ||
+ | |||
===== Des maths ! ==== | ===== Des maths ! ==== | ||
====Relu Rectifier neural networks==== | ====Relu Rectifier neural networks==== | ||
+ | {{ relu.jpg? | ||
* **[[https:// | * **[[https:// | ||
<code python> | <code python> | ||
Ligne 26: | Ligne 64: | ||
<code python> | <code python> | ||
def relu_prime(z): | def relu_prime(z): | ||
- | """ | + | """ |
- | | + | |
- | Une fonction | + | Une fonction indicatrice, |
- | | + | C'est donc la fonction H (discontinue en 0) prenant la valeur 1 pour tous les réels positifs et la valeur 0 pour les réels strictement négatifs. |
- | | + | |
- | C'est donc la fonction H (discontinue en 0) prenant la valeur 1 pour tous | + | |
- | | + | |
""" | """ | ||
return np.asarray(z > 0, dtype=np.float32) | return np.asarray(z > 0, dtype=np.float32) | ||
</ | </ | ||
====Sigmoïd==== | ====Sigmoïd==== | ||
+ | {{ sigmoid.png? | ||
* **[[https:// | * **[[https:// | ||
- | Elle représente la fonction de répartition de la loi logistique. Elle est souvent utilisée dans les réseaux de neurones parce qu' | + | Elle représente la fonction de répartition de la loi logistique. Elle est souvent utilisée dans les réseaux de neurones parce qu' |
<code python> | <code python> | ||
def sigmoid(x): | def sigmoid(x): | ||
- | """ | + | """ |
- | https://fr.wikipedia.org/ | + | |
return 1 / (1 + np.exp(-x)) | return 1 / (1 + np.exp(-x)) | ||
</ | </ | ||
Ligne 52: | Ligne 87: | ||
return z * (1 - z) | return z * (1 - z) | ||
</ | </ | ||
- | ===== Réseau de neurones convolutifs | ||
- | Un réseau de neurones convolutifs | + | ====Algorithme du gradient stochastique==== |
+ | * **[[https:// | ||
+ | L' | ||
- | **Remarque | + | ====Diagonale de 1==== |
- | Nous pouvons en déduire | + | {{ media_01: |
+ | |||
+ | numpy.eye(N, | ||
+ | |||
+ | Return a 2-D array with ones on the diagonal and zeros elsewhere. | ||
+ | Matrice ou la sortie est idéale: le 1 correspond à entée[i] = sortie[i], et entée[j], | ||
+ | |||
+ | ====Initialisation de X. Glorot et He==== | ||
+ | X = Xavier = prénom | ||
+ | | ||
+ | |||
+ | ===== Réseau | ||
+ | * **[[https:// | ||
+ | Un réseau de neurones Perceptron multicouches est un type de réseau dont l' | ||
+ | |||
+ | |||
+ | ===== Réseau | ||
+ | * **[[https:// | ||
+ | Un réseau de neurones Convolutif est un type de réseau de neurones artificiels dans lequel le motif de connexion entre les neurones est inspiré par le cortex visuel des animaux. Actuellement, | ||
+ | |||
+ | Ce type de réseau est développé avec [[yolo_avec_mes_propres_images|Yolo Darknet Préparation de mes propres images]] puis [[yolo_sans_carte_graphique|Yolo Darknet sans carte graphique]] et enfin [[yolo_darknet_sur_un_portable_optimus|Yolo Darknet sur un portable Optimus]]. | ||
+ | |||
+ | =====Réseau de neurones Perceptron multicouches en python===== | ||
+ | **Du code expliqué avec beaucoup d' | ||
+ | Enfin, là c'est de l' | ||
+ | |||
+ | ====Notre réseau==== | ||
+ | **Une colonne de 1600 en entrée, 2 nodes de 100, une sortie de 27 caractères.** | ||
+ | |||
+ | {{ media_01: | ||
+ | |||
+ | La totalité du projet est à **[[https:// | ||
+ | |||
+ | ====Installation==== | ||
+ | Installation de pip3: | ||
+ | sudo apt install pip3 | ||
+ | Installation de numpy et opencv | ||
+ | sudo pip3 install opencv numpy | ||
+ | Il faut installer mon [[pymultilame|module python perso]] disponible sur Github | ||
+ | sudo pip3 install -e git+https:// | ||
+ | |||
+ | ====Le script==== | ||
+ | <file python ia.py> | ||
+ | #!/ | ||
+ | # -*- coding: UTF-8 -*- | ||
+ | |||
+ | import shutil | ||
+ | import numpy as np | ||
+ | import cv2 | ||
+ | from pymultilame import MyTools | ||
- | =====Code qui marche à 96 %===== | ||
- | <code python> | ||
- | import numpy as np, cv2 | ||
def sigmoid(x): return 1 / (1 + np.exp(-x)) | def sigmoid(x): return 1 / (1 + np.exp(-x)) | ||
def sigmoid_prime(z): | def sigmoid_prime(z): | ||
def relu(x): return np.maximum(0, | def relu(x): return np.maximum(0, | ||
def relu_prime(z): | def relu_prime(z): | ||
- | layers = [1600, 100, 100, 27] | ||
- | activations, | ||
- | f = np.load('/ | ||
- | x_train, y_train = f[' | ||
- | x_train = 1 - x_train | ||
- | x_test, y_test = x_train[50000:,: | ||
- | x_train, y_train = x_train[: | ||
- | Y = np.eye(27, 27) | ||
- | activations_prime = [globals()[f.__name__ + ' | ||
- | A = {} | ||
- | W = [np.random.randn(layers[k+1], | ||
- | print(" | ||
- | cv2.namedWindow(' | ||
- | for epoch in range(1): | ||
- | for i, (a, d) in enumerate(zip(x_train, | ||
- | if i % 100 == 0: | ||
- | print(epoch, | ||
- | cv2.imshow(" | ||
- | cv2.waitKey(1) | ||
- | a = np.array(a, ndmin=2).T | ||
- | A[0] = a | ||
- | for k in range(len(layers)-1): | ||
- | z = np.dot(W[k], | ||
- | a = activations[k](z) | ||
- | A[k+1] = a | ||
- | delta_a = a - Y[:,[d]] | ||
- | for k in range(len(layers)-2, | ||
- | dz = delta_a * activations_prime[k](A[k+1]) | ||
- | dW = np.dot(dz, A[k].T) | ||
- | delta_a = np.dot(W[k].T, | ||
- | W[k] -= learningrate * dW | ||
- | print(" | ||
- | S = 0 | ||
- | for a, d in zip(x_test, y_test): | ||
- | for k in range(len(layers)-1): | ||
- | a = activations[k](np.dot(W[k], | ||
- | if np.argmax(a) == d: | ||
- | S += 1 | ||
- | print(" | ||
- | np.save(' | ||
- | </ | ||
- | {{tag> | + | class SemaphoreIA: |
+ | def __init__(self, | ||
+ | self.root = root | ||
+ | self.learningrate = learningrate | ||
+ | self.failed = failed | ||
+ | self.tools = MyTools() | ||
+ | |||
+ | # Dossier des ratés | ||
+ | if self.failed: | ||
+ | # Suppression du dossier failed et recréation pour le vider | ||
+ | try: | ||
+ | shutil.rmtree(self.root + ' | ||
+ | except: | ||
+ | print(' | ||
+ | self.tools.create_directory(self.root + ' | ||
+ | |||
+ | # Réseau de neurones: colonne 1600 en entrée, 2 nodes de 100, sortie de 27 caractères | ||
+ | self.layers = [1600, 100, 100, 27] | ||
+ | # Fonction d' | ||
+ | self.activations = [relu, relu, sigmoid] | ||
+ | |||
+ | fichier = np.load(self.root + ' | ||
+ | self.x_train, | ||
+ | self.x_train = 1 - self.x_train | ||
+ | self.x_test, | ||
+ | self.x_train, | ||
+ | |||
+ | # Affichage des images pour distraire | ||
+ | cv2.namedWindow(' | ||
+ | |||
+ | def training(self): | ||
+ | """ | ||
+ | print(" | ||
+ | |||
+ | # Matrice diagonale de 1 | ||
+ | diagonale = np.eye(27, 27) | ||
+ | |||
+ | # globals() Return a dictionary representing the current global symbol table. | ||
+ | self.activations_prime = [globals()[fonction.__name__ + ' | ||
+ | |||
+ | node_dict = {} | ||
+ | |||
+ | # Liste des poids | ||
+ | # Initialisation des poids des nodes, pour ne pas à être à 0 | ||
+ | # Construit 3 matrices (100x1600, 100x100, 27x100) | ||
+ | # /np.sqrt() résultat expérimental de l' | ||
+ | weight_list = [np.random.randn(self.layers[k+1], | ||
+ | | ||
+ | |||
+ | # vecteur_ligne = image en ligne à la 1ère itération | ||
+ | # nombre_lettre = nombre correspondant à la lettre de l' | ||
+ | # i pour itération, vecteur_colonne = x_train de i, nombre_lettre = y_train de i | ||
+ | for i, (vecteur_ligne, | ||
+ | |||
+ | # Affichage pour distraire les mangalore | ||
+ | if i % 10000 == 0: | ||
+ | print(i, nombre_lettre) | ||
+ | img = vecteur_ligne.reshape(40, | ||
+ | img = cv2.resize(img, | ||
+ | cv2.imshow(" | ||
+ | cv2.waitKey(1) | ||
+ | |||
+ | # la ligne devient colonne | ||
+ | vecteur_colonne = np.array(vecteur_ligne, | ||
+ | |||
+ | # Forward propagation | ||
+ | node_dict[0] = vecteur_colonne | ||
+ | for k in range(len(self.layers)-1): | ||
+ | # weight_list[k] (100x1600, 100x100 27x100) vecteur_colonne (1600,) | ||
+ | # z de format 100 x 1 | ||
+ | z = np.dot(weight_list[k], | ||
+ | |||
+ | # self.activations = non linéaire sinon sortie fonction linéaire de l' | ||
+ | # imite le seuil d' | ||
+ | vecteur_colonne = self.activations[k](z) | ||
+ | |||
+ | node_dict[k+1] = vecteur_colonne | ||
+ | |||
+ | # Retro propagation, | ||
+ | delta_a = vecteur_colonne - diagonale[:, | ||
+ | # Parcours des nodes en sens inverse pour corriger proportionnellement | ||
+ | # les poids en fonction de l' | ||
+ | # Descente du Gradient stochastique | ||
+ | for k in range(len(self.layers)-2, | ||
+ | delta_z = delta_a * self.activations_prime[k](node_dict[k+1]) | ||
+ | delta_w = np.dot(delta_z, | ||
+ | delta_a = np.dot(weight_list[k].T, | ||
+ | # Pour converger vers le minimum d' | ||
+ | weight_list[k] -= self.learningrate * delta_w | ||
+ | |||
+ | # Dans un fichier | ||
+ | np.save(self.root + ' | ||
+ | print(' | ||
+ | cv2.destroyAllWindows() | ||
+ | |||
+ | def testing(self): | ||
+ | """ | ||
+ | print(" | ||
+ | |||
+ | weight_list = np.load(self.root + ' | ||
+ | |||
+ | # Nombre de bonnes reconnaissance | ||
+ | success = 0 | ||
+ | |||
+ | # Dict avec le nombre d' | ||
+ | failed_dict = {} | ||
+ | |||
+ | for vecteur_ligne, | ||
+ | # image en ligne au 1er passage pour les failed | ||
+ | img = vecteur_ligne.copy() | ||
+ | |||
+ | for k in range(len(self.layers)-1): | ||
+ | vecteur_ligne = self.activations[k](np.dot(weight_list[k], | ||
+ | vecteur_ligne)) | ||
+ | |||
+ | reconnu = np.argmax(vecteur_ligne) | ||
+ | if reconnu == nombre_lettre: | ||
+ | success += 1 | ||
+ | else: | ||
+ | if self.failed: | ||
+ | self.write_failed(img, | ||
+ | if nombre_lettre in failed_dict: | ||
+ | failed_dict[nombre_lettre] += 1 | ||
+ | else: | ||
+ | if self.failed: | ||
+ | self.tools.create_directory(self.root + ' | ||
+ | failed_dict[nombre_lettre] = 1 | ||
+ | |||
+ | if self.failed: | ||
+ | sorted_by_value = sorted(failed_dict.items(), | ||
+ | print(sorted_by_value) | ||
+ | |||
+ | resp = 100.0 * success / len(self.x_test) | ||
+ | return resp | ||
+ | |||
+ | def write_failed(self, | ||
+ | """ | ||
+ | / | ||
+ | 11 est la lettre k, donc dans le dossier il ny a que la lettre k | ||
+ | et le 2ème nombre est la lettre reconnue fausse | ||
+ | """ | ||
+ | name = str(nombre_lettre) + ' | ||
+ | fichier = self.root + ' | ||
+ | img = img.reshape(40, | ||
+ | cv2.imwrite(fichier, | ||
+ | |||
+ | |||
+ | if __name__ == " | ||
+ | print(MyTools().get_absolute_path(__file__)) | ||
+ | root = MyTools().get_absolute_path(__file__)[: | ||
+ | print(" | ||
+ | |||
+ | for i in range(5): | ||
+ | learningrate = 0.022 | ||
+ | failed = 0 | ||
+ | sia = SemaphoreIA(root, | ||
+ | sia.training() | ||
+ | resp = sia.testing() | ||
+ | print(" | ||
+ | </ | ||
+ | |||
+ | |||
+ | ===== Etape suivante: YOLO Darknet===== | ||
+ | **[[computer_vision_and_pattern_recognition_segmentation_d_image|Computer Vision and Pattern Recognition Mask R-CNN]]** | ||
+ | |||
+ | {{tag> | ||
l_intelligence_du_semaphore.txt · Dernière modification : 2020/12/27 15:07 de serge