Initial commit
This commit is contained in:
11
Tensorflow/tutoriel15/README.md
Normal file
11
Tensorflow/tutoriel15/README.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Tutoriel tensorflow
|
||||
## Réseau GoogleNet (inception v1)
|
||||
|
||||
La vidéo de ce tutoriel est disponible à l'adresse suivante: https://www.youtube.com/watch?v=b4vv_vLVyho
|
||||
|
||||
## STL10
|
||||
|
||||
N'oubliez pas de récupérer la base stl10 (binary version) à l'adresse suivante:
|
||||
https://cs.stanford.edu/~acoates/stl10/
|
||||
|
||||
|
||||
55
Tensorflow/tutoriel15/STL_10_inception.py
Normal file
55
Tensorflow/tutoriel15/STL_10_inception.py
Normal file
@@ -0,0 +1,55 @@
|
||||
import tensorflow as tf
|
||||
import numpy as np
|
||||
import random
|
||||
from sklearn.utils import shuffle
|
||||
import common
|
||||
|
||||
taille_batch=100
|
||||
nbr_entrainement=400
|
||||
learning_rate=1E-3
|
||||
|
||||
labels, train_images, train_labels, test_images, test_labels=common.stl10("stl10_binary")
|
||||
train_images=train_images/255
|
||||
test_images=test_images/255
|
||||
|
||||
ph_images, ph_labels, ph_is_training, ph_learning_rate, socs, train, accuracy, saver=common.inception_v1(10)
|
||||
|
||||
fichier=open("log", "a")
|
||||
with tf.Session() as s:
|
||||
s.run(tf.global_variables_initializer())
|
||||
tab_train=[]
|
||||
tab_test=[]
|
||||
for id_entrainement in np.arange(nbr_entrainement):
|
||||
print("> Entrainement", id_entrainement)
|
||||
if not id_entrainement%10:
|
||||
learning_rate*=0.99
|
||||
print("lr:", learning_rate)
|
||||
train_images, train_labels=shuffle(train_images, train_labels)
|
||||
for batch in np.arange(0, len(train_images), taille_batch):
|
||||
s.run(train, feed_dict={
|
||||
ph_images: train_images[batch:batch+taille_batch],
|
||||
ph_labels: train_labels[batch:batch+taille_batch],
|
||||
ph_learning_rate: learning_rate,
|
||||
ph_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={
|
||||
ph_images: train_images[batch:batch+taille_batch],
|
||||
ph_labels: train_labels[batch:batch+taille_batch]
|
||||
})
|
||||
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={
|
||||
ph_images: test_images[batch:batch+taille_batch],
|
||||
ph_labels: test_labels[batch:batch+taille_batch]
|
||||
})
|
||||
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()
|
||||
105
Tensorflow/tutoriel15/common.py
Normal file
105
Tensorflow/tutoriel15/common.py
Normal file
@@ -0,0 +1,105 @@
|
||||
import numpy as np
|
||||
import tensorflow as tf
|
||||
from sklearn.utils import shuffle
|
||||
|
||||
def stl10(path):
|
||||
labels=['avion', 'oiseau', 'voiture', 'chat', 'cerf', 'chien', 'cheval', 'singe', 'bateau', 'camion']
|
||||
train_images=np.fromfile(path+"/train_X.bin", dtype=np.uint8).reshape(-1, 3, 96, 96).transpose(0, 2, 3, 1)
|
||||
train_labels=np.eye(10)[np.fromfile(path+"/train_y.bin", dtype=np.uint8)-1]
|
||||
train_images, train_labels=shuffle(train_images, train_labels)
|
||||
test_images=np.fromfile(path+"/test_X.bin", dtype=np.uint8).reshape(-1, 3, 96, 96).transpose(0, 2, 3, 1)
|
||||
test_labels=np.eye(10)[np.fromfile(path+"/test_y.bin", dtype=np.uint8)-1]
|
||||
return labels, train_images, train_labels, test_images, test_labels
|
||||
|
||||
def couche_convolution(couche_prec, taille_noyau, nbr_noyau, stride, b_norm, f_activation, training):
|
||||
w_filtre=tf.Variable(tf.random.truncated_normal(shape=(taille_noyau, taille_noyau, int(couche_prec.get_shape()[-1]), nbr_noyau)))
|
||||
b_filtre=np.zeros(nbr_noyau)
|
||||
result=tf.nn.conv2d(couche_prec, w_filtre, strides=[1, stride, stride, 1], padding='SAME')+b_filtre
|
||||
if b_norm is True:
|
||||
result=tf.layers.batch_normalization(result, training=training)
|
||||
if f_activation is not None:
|
||||
result=f_activation(result)
|
||||
return result
|
||||
|
||||
def couche_fc(couche_prec, nbr_neurone, b_norm, f_activation, training):
|
||||
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
|
||||
if b_norm is True:
|
||||
result=tf.layers.batch_normalization(result, training=training)
|
||||
if f_activation is not None:
|
||||
result=f_activation(result)
|
||||
return result
|
||||
|
||||
def b_inception_v1(input, nbr_1, nbr_3r, nbr_3, nbr_5r, nbr_5, nbr_pool, training):
|
||||
result1=couche_convolution(input, 1, nbr_1, 1, True, tf.nn.relu, training)
|
||||
|
||||
result2=couche_convolution(input, 1, nbr_3r, 1, True, tf.nn.relu, training)
|
||||
result2=couche_convolution(result2, 3, nbr_3, 1, True, tf.nn.relu, training)
|
||||
|
||||
result3=couche_convolution(input, 1, nbr_5r, 1, True, tf.nn.relu, training)
|
||||
result3=couche_convolution(result3, 5, nbr_5, 1, True, tf.nn.relu, training)
|
||||
|
||||
result4=tf.nn.max_pool(input, ksize=[1, 3, 3, 1], strides=[1, 1, 1, 1], padding='SAME')
|
||||
result4=couche_convolution(result4, 1, nbr_pool, 1, True, tf.nn.relu, training)
|
||||
|
||||
result=tf.concat([result1, result2, result3, result4], 3)
|
||||
return result
|
||||
|
||||
def aux(input, training, nbr_classes):
|
||||
result=tf.nn.avg_pool(input, ksize=[1, 5, 5, 1], strides=[1, 3, 3, 1], padding='VALID')
|
||||
result=couche_convolution(result, 1, 128, 1, True, tf.nn.relu, training)
|
||||
result=tf.contrib.layers.flatten(result)
|
||||
result=couche_fc(result, 1000, True, tf.nn.relu, training)
|
||||
result=tf.layers.dropout(result, 0.7, training=training)
|
||||
result=couche_fc(result, nbr_classes, False, None, training)
|
||||
return result
|
||||
|
||||
def inception_v1(nbr_classes):
|
||||
ph_images=tf.placeholder(shape=(None, 96, 96, 3), dtype=tf.float32)
|
||||
ph_labels=tf.placeholder(shape=(None, nbr_classes), dtype=tf.float32)
|
||||
ph_is_training=tf.placeholder_with_default(False, (), name='is_training')
|
||||
ph_learning_rate=tf.placeholder(dtype=tf.float32)
|
||||
|
||||
result=couche_convolution(ph_images, 5, 64, 2, True, tf.nn.relu, ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
#result=couche_convolution(result, 3, 64, 2, True, tf.nn.relu, ph_is_training)
|
||||
result=couche_convolution(result, 3, 192, 1, True, tf.nn.relu, ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
result=b_inception_v1(result, 64, 96, 128, 16, 32, 32, ph_is_training)
|
||||
result=b_inception_v1(result, 128, 128, 192, 32, 96, 64, ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
result=b_inception_v1(result, 192, 96, 208, 16, 48, 64, ph_is_training)
|
||||
|
||||
aux1=aux(result, ph_is_training, nbr_classes)
|
||||
|
||||
result=b_inception_v1(result, 160, 112, 224, 24, 64, 64, ph_is_training)
|
||||
result=b_inception_v1(result, 128, 128, 256, 24, 64, 64, ph_is_training)
|
||||
result=b_inception_v1(result, 112, 144, 288, 32, 64, 64, ph_is_training)
|
||||
|
||||
aux2=aux(result, ph_is_training, nbr_classes)
|
||||
|
||||
result=b_inception_v1(result, 256, 160, 320, 32, 128, 128, ph_is_training)
|
||||
result=tf.nn.max_pool(result, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1], padding='SAME')
|
||||
result=b_inception_v1(result, 256, 160, 320, 32, 128, 128, ph_is_training)
|
||||
result=b_inception_v1(result, 384, 192, 384, 48, 128, 128, ph_is_training)
|
||||
taille=result.get_shape()[1]
|
||||
result=tf.nn.avg_pool(result, ksize=[1, taille, taille, 1], strides=[1, 1, 1, 1], padding='SAME')
|
||||
|
||||
result=tf.contrib.layers.flatten(result)
|
||||
|
||||
result=couche_fc(result, 1000, True, tf.nn.relu, ph_is_training)
|
||||
result=tf.layers.dropout(result, 0.4, training=ph_is_training)
|
||||
result=couche_fc(result, nbr_classes, False, None, ph_is_training)
|
||||
socs=tf.nn.softmax(result)
|
||||
|
||||
loss=tf.nn.softmax_cross_entropy_with_logits_v2(labels=ph_labels, logits=result)+\
|
||||
0.3*tf.nn.softmax_cross_entropy_with_logits_v2(labels=ph_labels, logits=aux1)+\
|
||||
0.3*tf.nn.softmax_cross_entropy_with_logits_v2(labels=ph_labels, logits=aux2)
|
||||
|
||||
extra_update_ops=tf.get_collection(tf.GraphKeys.UPDATE_OPS)
|
||||
with tf.control_dependencies(extra_update_ops):
|
||||
train=tf.train.RMSPropOptimizer(ph_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, ph_learning_rate, socs, train, accuracy, tf.train.Saver()
|
||||
Reference in New Issue
Block a user