Initial commit
This commit is contained in:
73
Tensorflow/tutoriel9/CIFAR_10_vgg.py
Normal file
73
Tensorflow/tutoriel9/CIFAR_10_vgg.py
Normal file
@@ -0,0 +1,73 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from sklearn.utils import shuffle
|
||||
import matplotlib.pyplot as plot
|
||||
import cv2
|
||||
import vgg
|
||||
|
||||
def read_cifar_file(file, images, labels):
|
||||
shift=0
|
||||
f=np.fromfile(file, dtype=np.uint8)
|
||||
while shift!=f.shape[0]:
|
||||
labels.append(np.eye(10)[f[shift]])
|
||||
shift+=1
|
||||
images.append(f[shift:shift+3*32*32].reshape(3, 32, 32).transpose(1, 2, 0)/255)
|
||||
shift+=3*32*32
|
||||
|
||||
taille_batch=100
|
||||
nbr_entrainement=200
|
||||
labels=['avion', 'automobile', 'oiseau', 'chat', 'cerf', 'chien', 'grenouille', 'cheval', 'bateau', 'camion']
|
||||
|
||||
train_images=[]
|
||||
train_labels=[]
|
||||
read_cifar_file("cifar-10-batches-bin/data_batch_1.bin", train_images, train_labels)
|
||||
read_cifar_file("cifar-10-batches-bin/data_batch_2.bin", train_images, train_labels)
|
||||
read_cifar_file("cifar-10-batches-bin/data_batch_3.bin", train_images, train_labels)
|
||||
read_cifar_file("cifar-10-batches-bin/data_batch_4.bin", train_images, train_labels)
|
||||
read_cifar_file("cifar-10-batches-bin/data_batch_5.bin", train_images, train_labels)
|
||||
|
||||
test_images=[]
|
||||
test_labels=[]
|
||||
read_cifar_file("cifar-10-batches-bin/test_batch.bin", test_images, test_labels)
|
||||
|
||||
images, labels, is_training, sortie, train, accuracy, save=vgg.vggnet(nbr_classes=10, learning_rate=0.01)
|
||||
|
||||
fichier=open("log", "a")
|
||||
with tf.Session() as s:
|
||||
s.run(tf.global_variables_initializer())
|
||||
tab_train=[]
|
||||
tab_test=[]
|
||||
train_images, train_labels=shuffle(train_images, train_labels)
|
||||
for id_entrainement in np.arange(nbr_entrainement):
|
||||
print("> Entrainement", id_entrainement)
|
||||
for batch in np.arange(0, len(train_images), taille_batch):
|
||||
s.run(train, feed_dict={
|
||||
images: train_images[batch:batch+taille_batch],
|
||||
labels: train_labels[batch:batch+taille_batch],
|
||||
is_training: True
|
||||
})
|
||||
print(" entrainement OK")
|
||||
tab_accuracy_train=[]
|
||||
for batch in np.arange(0, len(train_images), taille_batch):
|
||||
p=s.run(accuracy, feed_dict={
|
||||
images: train_images[batch:batch+taille_batch],
|
||||
labels: train_labels[batch:batch+taille_batch],
|
||||
is_training: False
|
||||
})
|
||||
tab_accuracy_train.append(p)
|
||||
print(" train:", np.mean(tab_accuracy_train))
|
||||
tab_accuracy_test=[]
|
||||
for batch in np.arange(0, len(test_images), taille_batch):
|
||||
p=s.run(accuracy, feed_dict={
|
||||
images: test_images[batch:batch+taille_batch],
|
||||
labels: test_labels[batch:batch+taille_batch],
|
||||
is_training: False
|
||||
})
|
||||
tab_accuracy_test.append(p)
|
||||
print(" test :", np.mean(tab_accuracy_test))
|
||||
tab_train.append(1-np.mean(tab_accuracy_train))
|
||||
tab_test.append(1-np.mean(tab_accuracy_test))
|
||||
fichier.write("{:d}:{:f}:{:f}\n".format(id_entrainement, np.mean(tab_accuracy_train), np.mean(tab_accuracy_test)))
|
||||
fichier.close()
|
||||
|
||||
|
||||
BIN
Tensorflow/tutoriel9/Figure_1.png
Normal file
BIN
Tensorflow/tutoriel9/Figure_1.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 72 KiB |
BIN
Tensorflow/tutoriel9/Loss_avec_dropout
Normal file
BIN
Tensorflow/tutoriel9/Loss_avec_dropout
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 43 KiB |
BIN
Tensorflow/tutoriel9/Loss_sans_dropout
Normal file
BIN
Tensorflow/tutoriel9/Loss_sans_dropout
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 44 KiB |
21
Tensorflow/tutoriel9/README.md
Normal file
21
Tensorflow/tutoriel9/README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Tutoriel tensorflow
|
||||
## Surapprentissage: utilisation de dropout
|
||||
|
||||
La vidéo de ce tutoriel est disponible à l'adresse suivante: https://www.youtube.com/watch?v=reV2aoa6svM
|
||||
|
||||
## CIFAR10
|
||||
|
||||
N'oubliez pas de récuperer la base cifar10 (binary version) à l'adresse suivante:
|
||||
https://www.cs.toronto.edu/~kriz/cifar.html
|
||||
|
||||
### Courbe d'erreur du réseau sans dropout
|
||||

|
||||
|
||||
### Courbe d'erreur du réseau avec dropout
|
||||

|
||||
|
||||
### Courbes d'erreur sur la base de validation sur le même graphique:
|
||||

|
||||
|
||||
L'apprentissage prend 2h40 sur une GeForce 1080
|
||||
|
||||
60
Tensorflow/tutoriel9/STL10_vgg.py
Normal file
60
Tensorflow/tutoriel9/STL10_vgg.py
Normal file
@@ -0,0 +1,60 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
from sklearn.utils import shuffle
|
||||
import matplotlib.pyplot as plot
|
||||
import cv2
|
||||
import vgg
|
||||
|
||||
labels=['avion', 'oiseau', 'voiture', 'chat', 'cerf', 'chien', 'cheval', 'singe', 'bateau', 'camion']
|
||||
train_images=np.fromfile("stl10_binary/train_X.bin", dtype=np.uint8).reshape(-1, 3, 96, 96).transpose(0, 2, 3, 1)/255
|
||||
train_labels=np.eye(10)[np.fromfile("stl10_binary/train_y.bin", dtype=np.uint8)-1]
|
||||
test_images=np.fromfile("stl10_binary/test_X.bin", dtype=np.uint8).reshape(-1, 3, 96, 96).transpose(0, 2, 3, 1)/255
|
||||
test_labels=np.eye(10)[np.fromfile("stl10_binary/test_y.bin", dtype=np.uint8)-1]
|
||||
|
||||
taille_batch=100
|
||||
nbr_entrainement=200
|
||||
|
||||
images, labels, is_training, sortie, train, accuracy, save=vgg.vggnet(nbr_classes=10, learning_rate=0.001)
|
||||
|
||||
#train_images=tf.image.resize_images(train_images, size=[32, 32])
|
||||
#test_images=tf.image.resize_images(train_images, size=[32, 32])
|
||||
|
||||
fichier=open("log", "a")
|
||||
with tf.Session() as s:
|
||||
s.run(tf.global_variables_initializer())
|
||||
tab_train=[]
|
||||
tab_test=[]
|
||||
train_images, train_labels=shuffle(train_images, train_labels)
|
||||
for id_entrainement in np.arange(nbr_entrainement):
|
||||
print("> Entrainement", id_entrainement)
|
||||
for batch in np.arange(0, len(train_images), taille_batch):
|
||||
s.run(train, feed_dict={
|
||||
images: train_images[batch:batch+taille_batch],
|
||||
labels: train_labels[batch:batch+taille_batch],
|
||||
is_training: True
|
||||
})
|
||||
print(" entrainement OK")
|
||||
tab_accuracy_train=[]
|
||||
for batch in np.arange(0, len(train_images), taille_batch):
|
||||
p=s.run(accuracy, feed_dict={
|
||||
images: train_images[batch:batch+taille_batch],
|
||||
labels: train_labels[batch:batch+taille_batch],
|
||||
is_training: False
|
||||
})
|
||||
tab_accuracy_train.append(p)
|
||||
print(" train:", np.mean(tab_accuracy_train))
|
||||
tab_accuracy_test=[]
|
||||
for batch in np.arange(0, len(test_images), taille_batch):
|
||||
p=s.run(accuracy, feed_dict={
|
||||
images: test_images[batch:batch+taille_batch],
|
||||
labels: test_labels[batch:batch+taille_batch],
|
||||
is_training: False
|
||||
})
|
||||
tab_accuracy_test.append(p)
|
||||
print(" test :", np.mean(tab_accuracy_test))
|
||||
tab_train.append(1-np.mean(tab_accuracy_train))
|
||||
tab_test.append(1-np.mean(tab_accuracy_test))
|
||||
fichier.write("{:d}:{:f}:{:f}\n".format(id_entrainement, np.mean(tab_accuracy_train), np.mean(tab_accuracy_test)))
|
||||
fichier.close()
|
||||
|
||||
|
||||
102
Tensorflow/tutoriel9/vgg.py
Normal file
102
Tensorflow/tutoriel9/vgg.py
Normal file
@@ -0,0 +1,102 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
|
||||
def convolution(couche_prec, taille_noyau, nbr_noyau):
|
||||
w=tf.Variable(tf.random.truncated_normal(shape=(taille_noyau, taille_noyau, int(couche_prec.get_shape()[-1]), nbr_noyau)))
|
||||
b=np.zeros(nbr_noyau)
|
||||
result=tf.nn.conv2d(couche_prec, w, strides=[1, 1, 1, 1], padding='SAME')+b
|
||||
return result
|
||||
|
||||
def fc(couche_prec, nbr_neurone):
|
||||
w=tf.Variable(tf.random.truncated_normal(shape=(int(couche_prec.get_shape()[-1]), nbr_neurone), dtype=tf.float32))
|
||||
b=tf.Variable(np.zeros(shape=(nbr_neurone)), dtype=tf.float32)
|
||||
result=tf.matmul(couche_prec, w)+b
|
||||
return result
|
||||
|
||||
def vggnet(nbr_classes, learning_rate=1E-3, momentum=0.99):
|
||||
ph_images=tf.placeholder(shape=(None, 32, 32, 3), dtype=tf.float32, name='images')
|
||||
ph_labels=tf.placeholder(shape=(None, nbr_classes), dtype=tf.float32)
|
||||
ph_is_training=tf.placeholder_with_default(False, (), name='is_training')
|
||||
|
||||
result=convolution(ph_images, 3, 64)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.layers.dropout(result, 0.2, training=ph_is_training)
|
||||
result=tf.nn.relu(result)
|
||||
result=convolution(result, 3, 64)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.2, training=ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
result=convolution(result, 3, 128)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.2, training=ph_is_training)
|
||||
result=convolution(result, 3, 128)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.3, training=ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
result=convolution(result, 3, 256)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.3, training=ph_is_training)
|
||||
result=convolution(result, 3, 256)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.3, training=ph_is_training)
|
||||
result=convolution(result, 3, 256)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.3, training=ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
result=convolution(result, 3, 512)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.3, training=ph_is_training)
|
||||
result=convolution(result, 3, 512)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.4, training=ph_is_training)
|
||||
result=convolution(result, 3, 512)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.4, training=ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
|
||||
result=convolution(result, 3, 512)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.4, training=ph_is_training)
|
||||
result=convolution(result, 3, 512)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.4, training=ph_is_training)
|
||||
result=convolution(result, 3, 512)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.nn.relu(result)
|
||||
result=tf.layers.dropout(result, 0.5, training=ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
result=tf.contrib.layers.flatten(result)
|
||||
|
||||
result=fc(result, 1024)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.layers.dropout(result, 0.5, training=ph_is_training)
|
||||
result=tf.nn.relu(result)
|
||||
result=fc(result, 1024)
|
||||
result=tf.layers.batch_normalization(result, training=ph_is_training, momentum=momentum)
|
||||
result=tf.layers.dropout(result, 0.5, training=ph_is_training)
|
||||
result=tf.nn.relu(result)
|
||||
result=fc(result, nbr_classes)
|
||||
socs=tf.nn.softmax(result, name="sortie")
|
||||
|
||||
loss=tf.nn.softmax_cross_entropy_with_logits_v2(labels=ph_labels, logits=result)
|
||||
extra_update_ops=tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
||||
with tf.control_dependencies(extra_update_ops):
|
||||
train=tf.train.AdamOptimizer(learning_rate).minimize(loss)
|
||||
accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(socs, 1), tf.argmax(ph_labels, 1)), tf.float32))
|
||||
|
||||
return ph_images, ph_labels, ph_is_training, socs, train, accuracy, tf.train.Saver()
|
||||
|
||||
Reference in New Issue
Block a user