l_intelligence_du_semaphore
Différences
Ci-dessous, les différences entre deux révisions de la page.
Prochaine révision | Révision précédente | ||
l_intelligence_du_semaphore [2019/01/26 12:17] – créée serge | l_intelligence_du_semaphore [2020/12/27 15:07] (Version actuelle) – serge | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
====== L' | ====== L' | ||
+ | <WRAP center round box 60% centeralign> | ||
+ | **{{tagpage> | ||
+ | </ | ||
+ | <WRAP center round box 60% centeralign> | ||
+ | **[[les_pages_intelligence_artificielle_en_details|Les Pages Intelligence Artificielle en détails]]** | ||
+ | </ | ||
+ | <WRAP center round box 60% centeralign> | ||
+ | **{{tagpage> | ||
+ | </ | ||
+ | <WRAP center round box 60% centeralign> | ||
+ | C'est quoi un sémaphore ? | ||
+ | {{ youtube> | ||
+ | {{ media_05: | ||
+ | </ | ||
+ | <WRAP center round box 60% centeralign> | ||
+ | **Calcul de [[Le sémaphore avec TensorFlow|Le sémaphore avec TensorFlow]] sur GPU au lieu du CPU avec TensorFlow** | ||
+ | </ | ||
+ | =====Les sources sur GitHub===== | ||
+ | * **[[https:// | ||
+ | =====Les images utilisées pour l' | ||
+ | ====Images sorties de Blender==== | ||
+ | <WRAP group> | ||
+ | <WRAP third column> | ||
+ | {{media_03: | ||
+ | </ | ||
+ | <WRAP third column> | ||
+ | {{media_03: | ||
+ | </ | ||
+ | <WRAP third column> | ||
+ | {{media_03: | ||
+ | </ | ||
+ | </ | ||
+ | ====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 ! ==== | ||
+ | ====Relu Rectifier neural networks==== | ||
+ | {{ media_12: | ||
+ | * **[[https:// | ||
+ | <code python> | ||
+ | def relu(x): | ||
+ | """ | ||
+ | | ||
+ | In the context of artificial neural networks, the rectifier is an | ||
+ | activation function defined as the positive part of its argument. | ||
+ | | ||
+ | Rectifie les négatifs à 0: | ||
+ | -1 > 0 | ||
+ | 1 > 1 | ||
+ | """ | ||
+ | return np.maximum(0, | ||
+ | </ | ||
+ | ====Relu_prime==== | ||
+ | * **[[https:// | ||
+ | * **[[https:// | ||
+ | <code python> | ||
+ | def relu_prime(z): | ||
+ | """ | ||
+ | | ||
+ | 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. | ||
+ | """ | ||
+ | return np.asarray(z > 0, dtype=np.float32) | ||
+ | </ | ||
+ | ====Sigmoïd==== | ||
+ | {{ media_12: | ||
+ | * **[[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' | ||
+ | <code python> | ||
+ | def sigmoid(x): | ||
+ | """ | ||
+ | return 1 / (1 + np.exp(-x)) | ||
+ | </ | ||
+ | |||
+ | ====Sigmoïd prime==== | ||
+ | <code python> | ||
+ | def sigmoid_prime(z): | ||
+ | """ | ||
+ | return z * (1 - z) | ||
+ | </ | ||
+ | |||
+ | ====Algorithme du gradient stochastique==== | ||
+ | * **[[https:// | ||
+ | L' | ||
+ | |||
+ | ====Diagonale de 1==== | ||
+ | {{ 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 | ||
+ | * **[[http:// | ||
+ | |||
+ | ===== Réseau de neurones Perceptron multicouches | ||
+ | * **[[https:// | ||
+ | Un réseau de neurones Perceptron multicouches est un type de réseau dont l' | ||
+ | |||
+ | |||
+ | ===== Réseau de neurones Convolutif ===== | ||
+ | * **[[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 | ||
+ | |||
+ | def sigmoid(x): return 1 / (1 + np.exp(-x)) | ||
+ | def sigmoid_prime(z): | ||
+ | def relu(x): return np.maximum(0, | ||
+ | def relu_prime(z): | ||
+ | |||
+ | 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(" | ||
+ | </ | ||
+ | |||
+ | =====La même chose avec Cupy ou Tensorflow===== | ||
+ | * **[[cupy_vs_numpy|Cupy: | ||
+ | * **[[le_semaphore_avec_tensorflow|Le sémaphore avec TensorFlow]]** | ||
+ | |||
+ | ===== Etape suivante: YOLO Darknet===== | ||
+ | **[[computer_vision_and_pattern_recognition_segmentation_d_image|Computer Vision and Pattern Recognition Mask R-CNN]]** | ||
+ | |||
+ | {{tag> bge ia python sb semaphore }} | ||
l_intelligence_du_semaphore.txt · Dernière modification : 2020/12/27 15:07 de serge