{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-02-13T19:45:33.429286Z","iopub.execute_input":"2022-02-13T19:45:33.429645Z","iopub.status.idle":"2022-02-13T19:45:33.454235Z","shell.execute_reply.started":"2022-02-13T19:45:33.429566Z","shell.execute_reply":"2022-02-13T19:45:33.453572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**les importation nécessaire**","metadata":{}},{"cell_type":"code","source":"import tensorflow.keras as K\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-02-18T23:56:27.311770Z","iopub.execute_input":"2022-02-18T23:56:27.312028Z","iopub.status.idle":"2022-02-18T23:56:27.316484Z","shell.execute_reply.started":"2022-02-18T23:56:27.311996Z","shell.execute_reply":"2022-02-18T23:56:27.315356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cette méthode faire le prétraitement pour former un modèle**","metadata":{}},{"cell_type":"code","source":"def preprocess_data(X, Y):\n        X = X.astype('float32')\n        Y = Y.astype('float32')\n        x_pr = K.applications.vgg16.preprocess_input(X)\n        y_pr = K.utils.to_categorical(Y, 10)\n        return(x_pr, y_pr)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T23:56:18.786184Z","iopub.execute_input":"2022-02-18T23:56:18.786838Z","iopub.status.idle":"2022-02-18T23:56:18.791335Z","shell.execute_reply.started":"2022-02-18T23:56:18.786797Z","shell.execute_reply":"2022-02-18T23:56:18.790430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**chargement des données et utilisation du prétraitement pour l'apprentissage et la validation de l'ensemble de données**","metadata":{}},{"cell_type":"code","source":"(Xt, Yt), (X, Y) = K.datasets.cifar10.load_data()\nX_train, Y_train = preprocess_data(Xt, Yt)\nX_test, Y_test= preprocess_data(X, Y)","metadata":{"execution":{"iopub.status.busy":"2022-02-18T23:56:57.929055Z","iopub.execute_input":"2022-02-18T23:56:57.929314Z","iopub.status.idle":"2022-02-18T23:57:03.903219Z","shell.execute_reply.started":"2022-02-18T23:56:57.929284Z","shell.execute_reply":"2022-02-18T23:57:03.902389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Création du modéle VGG16**","metadata":{}},{"cell_type":"code","source":"model1 = K.applications.vgg16.VGG16(include_top=False,\n                                            weights='imagenet',\n                                            pooling='avg'\n                                            )\n\nmodel= K.Sequential()\nmodel.add(K.layers.UpSampling2D())\nmodel.add(model1)\nmodel.add(K.layers.Flatten())\nmodel.add(K.layers.Dense(512, activation=('relu'))) \nmodel.add(K.layers.Dropout(0.2))\nmodel.add(K.layers.Dense(256, activation=('relu')))\nmodel.add(K.layers.Dropout(0.2))\nmodel.add(K.layers.Dense(10, activation=('softmax')))","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:12:06.489172Z","iopub.execute_input":"2022-02-19T00:12:06.490087Z","iopub.status.idle":"2022-02-19T00:12:06.762067Z","shell.execute_reply.started":"2022-02-19T00:12:06.490046Z","shell.execute_reply":"2022-02-19T00:12:06.761325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Cette méthode sert à crée Alpha**","metadata":{}},{"cell_type":"code","source":"def decay(epoch):\n    return 0.001 / (1 + 1 * 30)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:14:36.729977Z","iopub.execute_input":"2022-02-19T00:14:36.730236Z","iopub.status.idle":"2022-02-19T00:14:36.734268Z","shell.execute_reply.started":"2022-02-19T00:14:36.730206Z","shell.execute_reply":"2022-02-19T00:14:36.733582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Modèle de compilation**","metadata":{}},{"cell_type":"code","source":"callback = []\ncallback += [K.callbacks.LearningRateScheduler(decay, verbose=1)]\ncallback += [K.callbacks.ModelCheckpoint('cifar10.h5',save_best_only=True,mode='min' )]\nmodel.compile(optimizer='adam', loss='categorical_crossentropy',\n                      metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:14:40.769771Z","iopub.execute_input":"2022-02-19T00:14:40.770030Z","iopub.status.idle":"2022-02-19T00:14:40.781645Z","shell.execute_reply.started":"2022-02-19T00:14:40.770000Z","shell.execute_reply":"2022-02-19T00:14:40.780966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****Entraînement****","metadata":{}},{"cell_type":"code","source":"m_entrain=model.fit(x=X_train, y=Y_train,\n              batch_size=128,\n              validation_data=(X_test, Y_test),\n              epochs=25, shuffle=True,\n              callbacks=callback,\n              verbose=1\n              )","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:14:48.417631Z","iopub.execute_input":"2022-02-19T00:14:48.417953Z","iopub.status.idle":"2022-02-19T00:28:13.093071Z","shell.execute_reply.started":"2022-02-19T00:14:48.417913Z","shell.execute_reply":"2022-02-19T00:28:13.092288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Historique de modéle**","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:32:57.470729Z","iopub.execute_input":"2022-02-19T00:32:57.470993Z","iopub.status.idle":"2022-02-19T00:32:57.482000Z","shell.execute_reply.started":"2022-02-19T00:32:57.470963Z","shell.execute_reply":"2022-02-19T00:32:57.480255Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Évaluation**","metadata":{}},{"cell_type":"code","source":"score = model.evaluate(X_test, Y_test, verbose=0)\nprint('test loss est',score[0])\nprint('test accuracy est',score[1])","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:31:08.346300Z","iopub.execute_input":"2022-02-19T00:31:08.347060Z","iopub.status.idle":"2022-02-19T00:31:13.832658Z","shell.execute_reply.started":"2022-02-19T00:31:08.347013Z","shell.execute_reply":"2022-02-19T00:31:13.831893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Visualisation des résultat dans un graphe**","metadata":{}},{"cell_type":"code","source":"def figure(h, nb_epoch):\n        acc, loss, val_acc, val_loss = h.history['accuracy'], h.history['loss'], h.history['val_accuracy'], h.history['val_loss']\n        plt.figure(figsize=(15, 5))\n        plt.subplot(121)\n        plt.plot(range(nb_epoch), acc, label='Train')\n        plt.plot(range(nb_epoch), val_acc, label='Test')\n        plt.title('Accuracy (' + str(nb_epoch) + ' Epochs )', size=15)\n        plt.legend()\n        plt.grid(True)\n        plt.subplot(122)\n        plt.plot(range(nb_epoch), loss, label='Train')\n        plt.plot(range(nb_epoch), val_loss, label='Test')\n        plt.title('Loss (' + str(nb_epoch) + ' Epochs )', size=15)\n        plt.legend()\n        plt.grid(True)\n        plt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:51:20.720737Z","iopub.execute_input":"2022-02-19T00:51:20.721294Z","iopub.status.idle":"2022-02-19T00:51:20.730705Z","shell.execute_reply.started":"2022-02-19T00:51:20.721255Z","shell.execute_reply":"2022-02-19T00:51:20.729894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"figure(m_entrain, 25)","metadata":{"execution":{"iopub.status.busy":"2022-02-19T00:51:33.724293Z","iopub.execute_input":"2022-02-19T00:51:33.725070Z","iopub.status.idle":"2022-02-19T00:51:34.027445Z","shell.execute_reply.started":"2022-02-19T00:51:33.725028Z","shell.execute_reply":"2022-02-19T00:51:34.026773Z"},"trusted":true},"execution_count":null,"outputs":[]}]}