!pip install scikit-plot
*******************************
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D,Reshape
from keras.layers import Flatten, Dense
import numpy as np
import matplotlib.pyplot as plt
from keras import models, layers, optimizers, utils, losses
from tensorflow.keras import datasets
import scikitplot as skplt
**************************
(x_train, y_train), (x_test, y_test) = datasets.mnist.load_data()
x_train = x_train.reshape(60000, 784)/255
x_test = x_test.reshape(10000, 784)/255
x_train = x_train[:1024,:]
x_test = x_test[:501,:]
y_train = utils.to_categorical(y_train, 10)
y_test = utils.to_categorical(y_test, 10)
y_train = y_train[:1024,:]
y_test = y_test[:501,:]
***********************
print(y_train.shape,x_train.shape)
print(y_test.shape,x_test.shape)
************************
my_model = Sequential()
# Création d'un réseau de neurones vide
my_model.add(Reshape((28, 28, 1)))
my_model.add(Conv2D(64, (3, 3), padding='same', input_shape=((28, 28,
1)), activation='relu'))
my_model.add(Conv2D(filters=64,kernel_size=(3,3),padding="same",
activation="relu"))
my_model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
my_model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=128, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
my_model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=256, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
my_model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2)))
my_model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Conv2D(filters=512, kernel_size=(3,3), padding="same",
activation="relu"))
my_model.add(Flatten())
# Conversion des matrices 3D en vecteur 1D
my_model.add(Dense(256, activation='relu'))
my_model.add(Dense(256, activation='relu'))
my_model.add(Dense(10, activation='softmax'))
*******************************
train_cls = np.argmax(y_train, axis=1)
print (train_cls)
********************************
test_cls = np.argmax(y_test, axis=1)
print (test_cls)
********************************
plt.figure(figsize= (10,10))
for i in range(0,9):
plt.subplot(3,3,i+1)
plt.imshow(x_test[i].reshape(28,28), cmap='gray', interpolation='none')
plt.title(" Class {}".format(test_cls[i]))
plt.figure()
******************************
my_model.compile(loss=losses.categorical_crossentropy,
optimizer=optimizers.Adam(), metrics=['accuracy'])
*********************************
my_model.fit(x_train, y_train, batch_size=128, epochs=50, verbose=2,
shuffle=True)
********************************
score = my_model.evaluate(x_test, y_test, verbose=0)
*********************
print("taux de précision:", score[1])
***********************
predicted_classes =my_model.predict(x_test)
print (predicted_classes )
******************
cls_pred = np.argmax(predicted_classes, axis=1)
print (cls_pred)
*******************
skplt.metrics.plot_confusion_matrix(test_cls, cls_pred, normalize=False)
***************************
def plot_images(images, cls_true, cls_pred=None):
assert len(images) == len(cls_true) == 9
fig, axes = plt.subplots(3, 3)
fig.subplots_adjust(hspace=0.3, wspace=0.3)
for i, ax in enumerate(axes.flat):
ax.imshow(images[i].reshape(28,28), cmap='binary')
if cls_pred is None:
xlabel = "True: {0}".format(cls_true[i])
else:
xlabel = "True: {0}, Pred: {1}".format(cls_true[i],
cls_pred[i])
ax.set_xlabel(xlabel)
ax.set_xticks([])
ax.set_yticks([])
def plot_example_errors(cls_pred,test_class,test_img):
incorrect = (cls_pred != test_class)
comp =0
for i in enumerate(incorrect):
if (incorrect[i]):
comp+=1
print,("nombre des fausse classification : ")
print (comp)
images = test_img[incorrect]
cls_pred = cls_pred[incorrect]
cls_true = test_class[incorrect]
plot_images(images=images[0:9],
cls_true=cls_true[0:9],
cls_pred=cls_pred[0:9])
************************
plot_example_errors(cls_pred,test_cls,x_test)
1 / 3 100%
La catégorie de ce document est-elle correcte?
Merci pour votre participation!

Faire une suggestion

Avez-vous trouvé des erreurs dans linterface ou les textes ? Ou savez-vous comment améliorer linterface utilisateur de StudyLib ? Nhésitez pas à envoyer vos suggestions. Cest très important pour nous !