{"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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A - Préparation de datasets (train et test)\n# A-1- Extraction des datasets\n# A-1-1- Extrain de dataset train.7z\n\n!pip install py7zr\n!python -m py7zr x ../input/cifar-10/train.7z /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-02-18T10:06:53.910063Z","iopub.execute_input":"2022-02-18T10:06:53.910645Z","iopub.status.idle":"2022-02-18T10:07:55.311252Z","shell.execute_reply.started":"2022-02-18T10:06:53.910609Z","shell.execute_reply":"2022-02-18T10:07:55.309976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A-1-2- Extrain de dataset test.7z\n!python -m py7zr x ../input/cifar-10/test.7z /kaggle/working/","metadata":{"execution":{"iopub.status.busy":"2022-02-18T10:09:04.734120Z","iopub.execute_input":"2022-02-18T10:09:04.734430Z","iopub.status.idle":"2022-02-18T10:26:17.758861Z","shell.execute_reply.started":"2022-02-18T10:09:04.734401Z","shell.execute_reply":"2022-02-18T10:26:17.757499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# A-2- Création de dataset\nimport glob\nimport os\nimport cv2\nimport numpy as np \n\n   #nombre d'images de train data\ntrain_images_path = \"/kaggle/working/train\"\nnum_of_train_images= len(glob.glob(train_images_path+\"/*\"))\nprint(\"Le nombre d'images de train data est\",num_of_train_images)\n\n   #nombre d'images de test data\ntest_images_path = \"/kaggle/working/test\"    \nnum_of_test_images= len(glob.glob(test_images_path+\"/*\"))\nprint(\"Le nombre d'images de test data est\", num_of_test_images)\n\n    # Ajouter les images de train  au dataset créée\ntrain_images = [[]]*num_of_train_images\nfor dir_name, _, filenames in os.walk('/kaggle/working/train'):\n    for filename in filenames:\n        image_index = int(filename.split(\".\")[0])-1\n        img = cv2.imread(os.path.join(dir_name,filename))\n        train_images[image_index] = img\n        \n     # Ajouter les images de test  au dataset créée\ntest_images = [[]]*num_of_test_images\nfor dir_name, _, filenames in os.walk('/kaggle/working/test'):\n    for filename in filenames:\n        image_index = int(filename.split(\".\")[0])-1\n        img = cv2.imread(os.path.join(dir_name,filename))\n        test_images[image_index] = img","metadata":{"execution":{"iopub.status.busy":"2022-02-18T10:35:10.483902Z","iopub.execute_input":"2022-02-18T10:35:10.484516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-02-13T10:05:06.928355Z","iopub.execute_input":"2022-02-13T10:05:06.928953Z","iopub.status.idle":"2022-02-13T10:05:06.933443Z","shell.execute_reply.started":"2022-02-13T10:05:06.928921Z","shell.execute_reply":"2022-02-13T10:05:06.932809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n        \n# normalisation du dataset\ntrain_images = np.asarray(train_images, dtype=float)/255\ntest_images = np.asarray(test_images, dtype=float)/255\n#conversion du dataset aux array\nnp.save(\"../working/train_images.npy\", train_images)\nnp.save(\"../working/test_images.npy\", test_images)","metadata":{"execution":{"iopub.status.busy":"2022-02-13T10:26:18.727101Z","iopub.execute_input":"2022-02-13T10:26:18.727315Z","iopub.status.idle":"2022-02-13T10:30:16.342214Z","shell.execute_reply.started":"2022-02-13T10:26:18.727292Z","shell.execute_reply":"2022-02-13T10:30:16.339554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_images))","metadata":{"execution":{"iopub.status.busy":"2022-02-13T10:30:16.343725Z","iopub.execute_input":"2022-02-13T10:30:16.345587Z","iopub.status.idle":"2022-02-13T10:30:16.352538Z","shell.execute_reply.started":"2022-02-13T10:30:16.34551Z","shell.execute_reply":"2022-02-13T10:30:16.351378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Vérifier le nombre d'image dans chaque data\ntest_images.shape\ntrain_images.shape","metadata":{"execution":{"iopub.status.busy":"2022-02-13T10:11:06.436376Z","iopub.execute_input":"2022-02-13T10:11:06.437297Z","iopub.status.idle":"2022-02-13T10:11:06.444744Z","shell.execute_reply.started":"2022-02-13T10:11:06.437237Z","shell.execute_reply":"2022-02-13T10:11:06.443621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2022-02-12T18:38:52.719617Z","iopub.execute_input":"2022-02-12T18:38:52.720004Z","iopub.status.idle":"2022-02-12T18:38:52.724766Z","shell.execute_reply.started":"2022-02-12T18:38:52.719968Z","shell.execute_reply":"2022-02-12T18:38:52.724158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels = pd.read_csv('/kaggle/input/cifar-10/trainLabels.csv')","metadata":{"execution":{"iopub.status.busy":"2022-02-12T18:38:58.412727Z","iopub.execute_input":"2022-02-12T18:38:58.413284Z","iopub.status.idle":"2022-02-12T18:38:58.504542Z","shell.execute_reply.started":"2022-02-12T18:38:58.413243Z","shell.execute_reply":"2022-02-12T18:38:58.503919Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig=plt.figure(figsize=(10, 10))\ncolumns = 4\nrows = 2\nfor i in range(1, columns*rows +1):\n    img = train_images[i]\n    fig.add_subplot(rows, columns, i, title=[train_labels.label[i]])\n    plt.imshow(img)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-02-12T18:52:05.845574Z","iopub.execute_input":"2022-02-12T18:52:05.846381Z","iopub.status.idle":"2022-02-12T18:52:06.649878Z","shell.execute_reply.started":"2022-02-12T18:52:05.846344Z","shell.execute_reply":"2022-02-12T18:52:06.648842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images[0][0][0]","metadata":{"execution":{"iopub.status.busy":"2022-02-12T18:55:29.805254Z","iopub.execute_input":"2022-02-12T18:55:29.805572Z","iopub.status.idle":"2022-02-12T18:55:29.812434Z","shell.execute_reply.started":"2022-02-12T18:55:29.805542Z","shell.execute_reply":"2022-02-12T18:55:29.811431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom tensorflow import keras\n\n# Model architecture\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras import Sequential, datasets,models,layers\nfrom tensorflow.keras.callbacks import ModelCheckpoint\nfrom tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D, DepthwiseConv2D, BatchNormalization\nfrom tensorflow.keras.models import load_model\n\n# Data processing\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom tensorflow.keras.utils import to_categorical","metadata":{"execution":{"iopub.status.busy":"2022-02-12T18:56:17.277409Z","iopub.execute_input":"2022-02-12T18:56:17.27827Z","iopub.status.idle":"2022-02-12T18:56:26.603822Z","shell.execute_reply.started":"2022-02-12T18:56:17.278226Z","shell.execute_reply":"2022-02-12T18:56:26.600925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data augumetation\ndatagen = ImageDataGenerator(\n        rotation_range=0.3,  \n        zoom_range = 0.1,  \n        width_shift_range=0.1, \n        height_shift_range=0.1,\n        horizontal_flip=True)","metadata":{},"execution_count":null,"outputs":[]}]}