Initial commit
This commit is contained in:
7
Tensorflow/tutoriel30/README.md
Normal file
7
Tensorflow/tutoriel30/README.md
Normal file
@@ -0,0 +1,7 @@
|
||||
# Tutoriel Keras
|
||||
## Réseau binaire avec Keras
|
||||
|
||||
La vidéo de ce tutoriel est disponible à l'adresse suivante: https://www.youtube.com/watch?v=YLhDlHhthIM
|
||||
|
||||
|
||||
|
||||
5
Tensorflow/tutoriel30/config.py
Normal file
5
Tensorflow/tutoriel30/config.py
Normal file
@@ -0,0 +1,5 @@
|
||||
|
||||
size=100
|
||||
|
||||
dir_neg=".\\images_negatives\\"
|
||||
dir_pos=".\\images_positives\\"
|
||||
41
Tensorflow/tutoriel30/enregistrement.py
Normal file
41
Tensorflow/tutoriel30/enregistrement.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
import config
|
||||
|
||||
saut=10
|
||||
|
||||
dir=config.dir_neg
|
||||
#dir=config.dir_pos
|
||||
os.makedirs(dir, exist_ok=True)
|
||||
|
||||
id=0
|
||||
while os.path.isfile(dir+"image-{:d}.png".format(id)):
|
||||
id+=1
|
||||
id*=saut
|
||||
|
||||
cap=cv2.VideoCapture(0)
|
||||
width=int(cap.get(3))
|
||||
enregistre=0
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
|
||||
if enregistre:
|
||||
if not id%saut:
|
||||
id_=int(id/saut)
|
||||
fichier=dir+"image-{:d}.png".format(id_)
|
||||
print("Création du fichier", fichier)
|
||||
cv2.imwrite(fichier, frame)
|
||||
id+=1
|
||||
|
||||
cv2.rectangle(frame, (0, 0), (width, 30), (100, 100, 100), cv2.FILLED)
|
||||
cv2.putText(frame, "[e] enregistrement repertoire: {} [q] quitter".format(dir), (10, 20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 0)
|
||||
if enregistre:
|
||||
cv2.circle(frame, (width-20, 15), 5, (0, 0, 255), 8)
|
||||
|
||||
cv2.imshow('Camera', frame)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('e'):
|
||||
enregistre=not enregistre
|
||||
if key==ord('q'):
|
||||
quit()
|
||||
24
Tensorflow/tutoriel30/inference.py
Normal file
24
Tensorflow/tutoriel30/inference.py
Normal file
@@ -0,0 +1,24 @@
|
||||
import tensorflow as tf
|
||||
import cv2
|
||||
import numpy as np
|
||||
import config
|
||||
|
||||
my_model=tf.keras.models.load_model('saved_model\\my_model')
|
||||
|
||||
cap=cv2.VideoCapture(0)
|
||||
width=cap.get(3)
|
||||
height=cap.get(4)
|
||||
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
img=cv2.resize(frame, (config.size, config.size))/255
|
||||
img=np.array([img], dtype=np.float32)
|
||||
prediction=my_model.predict(img)
|
||||
if prediction[0][0]>0.3:
|
||||
color=(0, 255, 0)
|
||||
else:
|
||||
color=(0, 0, 255)
|
||||
cv2.rectangle(frame, (0, int(height)-30), (int(width*prediction[0][0]), int(height)), color, cv2.FILLED)
|
||||
cv2.imshow('Camera', frame)
|
||||
if cv2.waitKey(1)&0xFF==ord('q'):
|
||||
quit()
|
||||
40
Tensorflow/tutoriel30/model.py
Normal file
40
Tensorflow/tutoriel30/model.py
Normal file
@@ -0,0 +1,40 @@
|
||||
from tensorflow.keras import layers, models
|
||||
|
||||
# Fonction d'activation à tester: sigmoid, tanh, relu,
|
||||
|
||||
def model(size, nbr_cc):
|
||||
entree=layers.Input(shape=(size, size, 3), dtype='float32')
|
||||
|
||||
result=layers.Conv2D(nbr_cc, 3, activation='relu', padding='same')(entree)
|
||||
result=layers.Conv2D(nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.MaxPool2D()(result)
|
||||
|
||||
result=layers.Conv2D(2*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.Conv2D(2*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.Conv2D(2*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.Conv2D(2*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.MaxPool2D()(result)
|
||||
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.Conv2D(4*nbr_cc, 3, activation='relu', padding='same')(result)
|
||||
result=layers.BatchNormalization()(result)
|
||||
result=layers.MaxPool2D()(result)
|
||||
|
||||
result=layers.Flatten()(result)
|
||||
result=layers.Dense(1024, activation='relu')(result)
|
||||
sortie=layers.Dense(1, activation='sigmoid')(result)
|
||||
|
||||
model=models.Model(inputs=entree, outputs=sortie)
|
||||
return model
|
||||
38
Tensorflow/tutoriel30/photo.py
Normal file
38
Tensorflow/tutoriel30/photo.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
import config
|
||||
|
||||
os.makedirs(config.dir_pos, exist_ok=True)
|
||||
id_pos=0
|
||||
while os.path.isfile(config.dir_pos+"image-{:d}.png".format(id_pos)):
|
||||
id_pos+=1
|
||||
|
||||
os.makedirs(config.dir_neg, exist_ok=True)
|
||||
id_neg=0
|
||||
while os.path.isfile(config.dir_neg+"image-{:d}.png".format(id_neg)):
|
||||
id_neg+=1
|
||||
|
||||
cap=cv2.VideoCapture(0)
|
||||
width=int(cap.get(3))
|
||||
|
||||
while True:
|
||||
ret, frame=cap.read()
|
||||
|
||||
cv2.rectangle(frame, (0, 0), (width, 30), (100, 100, 100), cv2.FILLED)
|
||||
cv2.putText(frame, "[p] photo positive [n] photo negative [q] quitter", (10, 20), cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 255), 1)
|
||||
|
||||
cv2.imshow('Camera', frame)
|
||||
key=cv2.waitKey(1)&0xFF
|
||||
if key==ord('p'):
|
||||
fichier=config.dir_pos+"image-{:d}.png".format(id_pos)
|
||||
print("Création du fichier", fichier)
|
||||
cv2.imwrite(fichier, frame)
|
||||
id_pos+=1
|
||||
if key==ord('n'):
|
||||
fichier=config.dir_neg+"image-{:d}.png".format(id_neg)
|
||||
print("Création du fichier", fichier)
|
||||
cv2.imwrite(fichier, frame)
|
||||
id_neg+=1
|
||||
if key==ord('q'):
|
||||
quit()
|
||||
50
Tensorflow/tutoriel30/train.py
Normal file
50
Tensorflow/tutoriel30/train.py
Normal file
@@ -0,0 +1,50 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
import glob
|
||||
import cv2
|
||||
import model
|
||||
import config
|
||||
|
||||
tab_images=[]
|
||||
tab_labels=[]
|
||||
|
||||
def complete_dataset(files, value):
|
||||
for image in glob.glob(files):
|
||||
img=cv2.imread(image)
|
||||
img=cv2.resize(img, (config.size, config.size))
|
||||
tab_images.append(img)
|
||||
tab_labels.append([value])
|
||||
img=cv2.flip(img, 1)
|
||||
tab_images.append(img)
|
||||
tab_labels.append([value])
|
||||
img=cv2.flip(img, 0)
|
||||
tab_images.append(img)
|
||||
tab_labels.append([value])
|
||||
|
||||
complete_dataset(config.dir_pos+'\\*.png', 1.)
|
||||
complete_dataset(config.dir_neg+'\\*.png', 0.)
|
||||
|
||||
tab_images=np.array(tab_images, dtype=np.float32)/255
|
||||
tab_labels=np.array(tab_labels, dtype=np.float32)
|
||||
|
||||
index=np.random.permutation(len(tab_images))
|
||||
tab_images=tab_images[index]
|
||||
tab_labels=tab_labels[index]
|
||||
|
||||
#for i in range(len(tab_images)):
|
||||
# cv2.imshow('Camera', tab_images[i])
|
||||
# print("Label", tab_labels[i])
|
||||
# if cv2.waitKey()&0xFF==ord('q'):
|
||||
# quit()
|
||||
|
||||
model=model.model(config.size, 8)
|
||||
|
||||
model.compile(optimizer='adam',
|
||||
loss='binary_crossentropy',
|
||||
metrics=['accuracy'])
|
||||
model.fit(tab_images,
|
||||
tab_labels,
|
||||
validation_split=0.05,
|
||||
batch_size=64,
|
||||
epochs=30)
|
||||
model.save('saved_model\\my_model')
|
||||
Reference in New Issue
Block a user