# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + id="NgIo7_iIhmnR" colab_type="code" colab={}
## Ecrit par <NAME>
## pour le cours Deep Learning du Data Engineering Master de l'EHTP
## Avril 2020

# + id="mSMcZsei_IkH" colab_type="code" colab={}
import numpy as np
import matplotlib.pyplot as plt

# + [markdown] id="bm0X7gA3hT-l" colab_type="text"
# ## Exercice 1

# + id="q0l781677q3z" colab_type="code" outputId="b3d174fc-7937-4af1-e9c9-48fad8c5d21c" colab={"base_uri": "https://localhost:8080/", "height": 212}
# !wget https://leseco.ma/wp-content/uploads/2018/03/ehtp.jpg

# + id="O4mWfbsK-W7Y" colab_type="code" outputId="1de9fc6d-c618-4651-fdfd-afcf94d51257" colab={"base_uri": "https://localhost:8080/", "height": 34}
from keras.preprocessing.image import array_to_img, img_to_array, load_img

# + id="X03o2WkJ-cq6" colab_type="code" colab={}
im = load_img('ehtp.jpg' )
imRGB = img_to_array(im)

# + id="fBS12Xa_-hNQ" colab_type="code" outputId="0aa5ece9-cf03-4fd4-c5fa-2567aeeff3d9" colab={"base_uri": "https://localhost:8080/", "height": 34}
img_to_array(im).shape

# + id="FDyh7jd2_w6N" colab_type="code" colab={}
imR, imG, imB = imRGB.copy(), imRGB.copy(), imRGB.copy()
imR[:,:,[1,2]]=0.
imG[:,:,[0,2]]=0.
imB[:,:,[0,1]]=0.

# + id="SxemcG9q-1oQ" colab_type="code" outputId="ca512672-d5a7-45d7-feff-faec8e82288a" colab={"base_uri": "https://localhost:8080/", "height": 211}
plt.figure(figsize=(30,20))
plt.subplot(1,4,1)
plt.imshow(im)
plt.subplot(1,4,2)
plt.imshow(array_to_img(imR))
plt.subplot(1,4,3)
plt.imshow(array_to_img(imG))
plt.subplot(1,4,4)
plt.imshow(array_to_img(imB))

# + [markdown] id="k3x1_ZwLhY5E" colab_type="text"
# ## Exercice 2

# + [markdown] id="UsiItu-g60fe" colab_type="text"
# un réseau appris sur MNIST permuté peut aussi prédire avec un bon score les images permuté de MNIST et ça montre que le MLP ne donne pas d'importance à la structure locale

# + id="ZvsOH-FHzrXf" colab_type="code" colab={}
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop, Adam
import matplotlib.pyplot as plt

# + id="-sCyXzXny7I7" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 52} outputId="700a4337-cf77-4343-e866-d5239a450828"
# the data, split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# + id="mR9A6qsBy7MM" colab_type="code" colab={}
rng = np.random.RandomState(42)

# + id="w7cy11H6y7O6" colab_type="code" colab={}
perm = rng.permutation(784)

# + id="RbU144YC0QFT" colab_type="code" colab={}
x_train_perm = x_train.reshape(60000, 784)[:,perm].reshape(60000, 28, 28)
x_test_perm = x_test.reshape(10000, 784)[:,perm].reshape(10000, 28, 28)

# + id="WXYdZBqV0QIE" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 284} outputId="730ddb76-f54e-4a72-a97f-50ac3e2ed4fc"
randindices=np.random.randint(x_train.shape[0],size=3)
plt.figure()
plt.subplot(2,3,1)
plt.imshow(x_train[randindices[0],:,:], cmap='gray')
plt.subplot(2,3,2)
plt.imshow(x_train[randindices[1],:,:], cmap='gray')
plt.subplot(2,3,3)
plt.imshow(x_train[randindices[2],:,:], cmap='gray')
#######
plt.subplot(2,3,4)
plt.imshow(x_train_perm[randindices[0],:,:], cmap='gray')
plt.subplot(2,3,5)
plt.imshow(x_train_perm[randindices[1],:,:], cmap='gray')
plt.subplot(2,3,6)
plt.imshow(x_train_perm[randindices[2],:,:], cmap='gray')

# + id="gTo9G1id1aUS" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 52} outputId="fd19ed88-206f-40d2-f7ed-f2010726823d"
x_train_perm = x_train_perm.reshape(60000, 784)
x_test_perm = x_test_perm.reshape(10000, 784)
x_train_perm = x_train_perm.astype('float32')
x_test_perm = x_test_perm.astype('float32')
x_train_perm /= 255
x_test_perm /= 255
print(x_train_perm.shape[0], 'train samples')
print(x_test_perm.shape[0], 'test samples')

# + id="ia45f62U2J54" colab_type="code" colab={}
num_classes = 10

y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# + id="5x9kQ5hi2NBZ" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 336} outputId="a9983822-09d8-48f2-c03d-a33a8c8a66ca"
model = Sequential()
model.add(Dense(512, activation='relu', input_shape=(784,)))
model.add(Dropout(0.2))
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_classes, activation='softmax'))

model.summary()

# + id="Nu5vUJXa2NKI" colab_type="code" colab={}
model.compile(loss='categorical_crossentropy',
              optimizer=Adam(lr=1e-3),
              metrics=['accuracy'])

# + id="Qu7_D5gD2NO9" colab_type="code" colab={}
batch_size = 128
epochs = 20

# + id="Zy-x0eJ42NR8" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 745} outputId="7117eb32-054a-428b-d7ce-8f32e21b2f3b"
history = model.fit(x_train_perm, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test_perm, y_test))

# + id="qwvG4KL12ZSH" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 52} outputId="df2d9321-c981-4e50-c8f8-3ea0361e33a0"
score = model.evaluate(x_test_perm, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# + id="WEl2LS1b2gHA" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 284} outputId="eeeb3a72-8cc3-4cd6-b552-7b6944117e11"
randindices=np.random.randint(x_test.shape[0],size=3)
plt.figure()
plt.subplot(2,3,1)
plt.imshow(x_test[randindices[0],:,:], cmap='gray')
plt.subplot(2,3,2)
plt.imshow(x_test[randindices[1],:,:], cmap='gray')
plt.subplot(2,3,3)
plt.imshow(x_test[randindices[2],:,:], cmap='gray')
#######
plt.subplot(2,3,4)
plt.imshow(np.reshape(x_test_perm[randindices[0],:],(28,28)), cmap='gray')
plt.subplot(2,3,5)
plt.imshow(np.reshape(x_test_perm[randindices[1],:],(28,28)), cmap='gray')
plt.subplot(2,3,6)
plt.imshow(np.reshape(x_test_perm[randindices[2],:],(28,28)), cmap='gray')

# + id="UaAWR5sU2gKI" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 70} outputId="18fb7c65-2b9f-43cc-ecfe-23edc43c1ed1"
predictions=model.predict(x_test_perm)
print("Le réseau reconnaît le chiffre "+ str(np.argmax(predictions[randindices[0],:])) + ' avec une confiance ' + str(np.max(predictions[randindices[0],:]) * 100) + '%.')
print("Le réseau reconnaît le chiffre "+ str(np.argmax(predictions[randindices[1],:])) + ' avec une confiance ' + str(np.max(predictions[randindices[1],:]) * 100) + '%.')
print("Le réseau reconnaît le chiffre "+ str(np.argmax(predictions[randindices[2],:])) + ' avec une confiance ' + str(np.max(predictions[randindices[2],:]) * 100) + '%.')

# + [markdown] id="nDQ5vmH37HaG" colab_type="text"
# # Exercice 3 : LeNet5 pour MNIST

# + [markdown] id="NPASvcBGWRA8" colab_type="text"
# a little bit modified version of LeNet5

# + id="HvHT0x5FVB81" colab_type="code" colab={}
from keras.layers import Conv2D, MaxPooling2D, Flatten

# + id="q_fLUZs_Vrlf" colab_type="code" colab={}
# Reshape the dataset into 4D array
x_train = x_train.reshape(x_train.shape[0], 28,28,1)
x_test = x_test.reshape(x_test.shape[0], 28,28,1)

# + id="pdcVTvkz7Iqk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 443} outputId="19dae512-47d4-41fb-da95-8126b4c8b594"
model = keras.Sequential()
model.add(Conv2D(filters=6, kernel_size=(5, 5), activation='relu', padding='valid', input_shape=(28,28,1)))
model.add(MaxPooling2D())
model.add(Conv2D(filters=16, kernel_size=(5, 5), activation='relu', padding='valid'))
model.add(MaxPooling2D())
model.add(Flatten())
model.add(Dense(units=120, activation='relu'))
model.add(Dense(units=84, activation='relu'))
model.add(Dense(units=10, activation = 'softmax'))

model.summary()

# + id="IgSChBBs2XRm" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 745} outputId="70acf4b5-505b-4fc8-9486-244135e212ca"
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

history = model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))


# + id="Is1aDwnY2aCL" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="c11fe66b-517e-4075-be1f-17a996b21993"
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='val')
plt.legend()

# + id="pVAZ4iIz2ZWK" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 52} outputId="99a20e50-a58f-4981-8a7a-4b9850a791a2"
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])

# + id="2TB_fNKR7i9S" colab_type="code" colab={}


# + [markdown] id="qyJ3s_OS7kE4" colab_type="text"
# # Exercice 4: LSTM pour prédicition de nombre de voyageurs d'une compagnie aérienne
#
# Source: https://machinelearningmastery.com/time-series-prediction-lstm-recurrent-neural-networks-python-keras/ 

# + id="yFgCUkTC77sa" colab_type="code" colab={}
from keras.layers import LSTM
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import mean_squared_error

# + id="189srTNo7vlz" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 212} outputId="0bbf983a-0e1d-448a-a776-769560db1f07"
# !wget https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv

# + id="okxFjwmV8BRH" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 197} outputId="9b021239-6f0d-4750-e90f-9d7f6c238832"
dataset = pd.read_csv('airline-passengers.csv', usecols=[1], engine='python')
dataset.head()

# + id="hkbFy0cS_WIg" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 282} outputId="91a1fa4f-26d0-46a9-b267-96c3ebfdc82f"
plt.plot(dataset)

# + id="D2s_soj_AaHR" colab_type="code" colab={}
# normalize the dataset
scaler = MinMaxScaler(feature_range=(0, 1))
dataset = scaler.fit_transform(dataset)

# + id="3jZWyopoEcR2" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 34} outputId="3a021ad1-2995-403e-8aca-5fe530b84946"
# split into train and test sets
train_size = int(len(dataset) * 0.67)
test_size = len(dataset) - train_size
train, test = dataset[0:train_size,:], dataset[train_size:len(dataset),:]
print(len(train), len(test))


# + id="YRgnRyHNEcUl" colab_type="code" colab={}
# convert an array of values into a dataset matrix
def create_dataset(dataset, look_back=1):
	dataX, dataY = [], []
	for i in range(len(dataset)-look_back-1):
		a = dataset[i:(i+look_back), 0]
		dataX.append(a)
		dataY.append(dataset[i + look_back, 0])
	return np.array(dataX), np.array(dataY)

# + id="9dIXNATLEcZp" colab_type="code" colab={}
# reshape into X=t and Y=t+1
look_back = 1
trainX, trainY = create_dataset(train, look_back)
testX, testY = create_dataset(test, look_back)

# + id="cWPIBg82EcX9" colab_type="code" colab={}
# reshape input to be [samples, time steps, features]
trainX = np.reshape(trainX, (trainX.shape[0], 1, trainX.shape[1]))
testX = np.reshape(testX, (testX.shape[0], 1, testX.shape[1]))

# + id="z_0EvJVhEmlz" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 230} outputId="9252671a-be23-49df-a20c-faf57f03e1f4"
# create and fit the LSTM network
model = Sequential()
model.add(LSTM(4, input_shape=(1, look_back)))
model.add(Dense(1))

model.summary()

# + id="32y1ZoXTEmon" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="a6da28ce-c750-4242-fc0b-5a559ccf637d"
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(trainX, trainY, epochs=100, batch_size=1, verbose=2)

# + id="dtnhYdObEmrY" colab_type="code" colab={}
# make predictions
trainPredict = model.predict(trainX)
testPredict = model.predict(testX)
# invert predictions
trainPredict = scaler.inverse_transform(trainPredict)
#trainY = scaler.inverse_transform([trainY])
testPredict = scaler.inverse_transform(testPredict)
#testY = scaler.inverse_transform([testY])
# calculate root mean squared error
#trainScore = math.sqrt(mean_squared_error(trainY[0], trainPredict[:,0]))
#print('Train Score: %.2f RMSE' % (trainScore))
#testScore = math.sqrt(mean_squared_error(testY[0], testPredict[:,0]))
#print('Test Score: %.2f RMSE' % (testScore))

# + id="oA6fjz0VEmuk" colab_type="code" colab={"base_uri": "https://localhost:8080/", "height": 264} outputId="708ac4fa-e74d-4e70-ed9a-a0eb4ebc1c2c"
# shift train predictions for plotting
trainPredictPlot = np.empty_like(dataset)
trainPredictPlot[:, :] = np.nan
trainPredictPlot[look_back:len(trainPredict)+look_back, :] = trainPredict
# shift test predictions for plotting
testPredictPlot = np.empty_like(dataset)
testPredictPlot[:, :] = np.nan
testPredictPlot[len(trainPredict)+(look_back*2)+1:len(dataset)-1, :] = testPredict
# plot baseline and predictions
plt.plot(scaler.inverse_transform(dataset))
plt.plot(trainPredictPlot)
plt.plot(testPredictPlot)
plt.show()

StarcoderdataJupyterScriptsDedupFiltered