{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"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)\nimport cv2\n\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers\nfrom tensorflow.keras.models import Sequential\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\n#for 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":"2023-12-27T08:06:08.854811Z","iopub.execute_input":"2023-12-27T08:06:08.856146Z","iopub.status.idle":"2023-12-27T08:06:19.981962Z","shell.execute_reply.started":"2023-12-27T08:06:08.856083Z","shell.execute_reply":"2023-12-27T08:06:19.980735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:19.984773Z","iopub.execute_input":"2023-12-27T08:06:19.986021Z","iopub.status.idle":"2023-12-27T08:06:19.991466Z","shell.execute_reply.started":"2023-12-27T08:06:19.985944Z","shell.execute_reply":"2023-12-27T08:06:19.99022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_path = \"/kaggle/input/UBC-OCEAN/train_thumbnails/52275_thumbnail.png\"\n","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:19.992965Z","iopub.execute_input":"2023-12-27T08:06:19.993439Z","iopub.status.idle":"2023-12-27T08:06:20.003588Z","shell.execute_reply.started":"2023-12-27T08:06:19.993396Z","shell.execute_reply":"2023-12-27T08:06:20.002312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image = Image.open(image_path)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:20.006923Z","iopub.execute_input":"2023-12-27T08:06:20.007438Z","iopub.status.idle":"2023-12-27T08:06:20.059509Z","shell.execute_reply.started":"2023-12-27T08:06:20.00738Z","shell.execute_reply":"2023-12-27T08:06:20.058384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:20.060918Z","iopub.execute_input":"2023-12-27T08:06:20.062008Z","iopub.status.idle":"2023-12-27T08:06:21.887465Z","shell.execute_reply.started":"2023-12-27T08:06:20.061955Z","shell.execute_reply":"2023-12-27T08:06:21.88521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.size","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:21.889703Z","iopub.execute_input":"2023-12-27T08:06:21.890687Z","iopub.status.idle":"2023-12-27T08:06:21.89934Z","shell.execute_reply.started":"2023-12-27T08:06:21.890624Z","shell.execute_reply":"2023-12-27T08:06:21.897254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import subprocess\n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:21.901616Z","iopub.execute_input":"2023-12-27T08:06:21.902157Z","iopub.status.idle":"2023-12-27T08:06:21.92647Z","shell.execute_reply.started":"2023-12-27T08:06:21.902106Z","shell.execute_reply":"2023-12-27T08:06:21.925268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv_train_data = pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:21.928071Z","iopub.execute_input":"2023-12-27T08:06:21.92896Z","iopub.status.idle":"2023-12-27T08:06:21.956666Z","shell.execute_reply.started":"2023-12-27T08:06:21.928913Z","shell.execute_reply":"2023-12-27T08:06:21.955449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"csv_train_data.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:21.958291Z","iopub.execute_input":"2023-12-27T08:06:21.959307Z","iopub.status.idle":"2023-12-27T08:06:21.981987Z","shell.execute_reply.started":"2023-12-27T08:06:21.959265Z","shell.execute_reply":"2023-12-27T08:06:21.980914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import random","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:21.987503Z","iopub.execute_input":"2023-12-27T08:06:21.988271Z","iopub.status.idle":"2023-12-27T08:06:21.992986Z","shell.execute_reply.started":"2023-12-27T08:06:21.988225Z","shell.execute_reply":"2023-12-27T08:06:21.991951Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Définissez le chemin vers le dossier contenant vos 100 images IRM\ndossier_images = \"/kaggle/input/UBC-OCEAN/train_thumbnails/\"\n\n#créer une liste pour stocker vos 100 images\ntoutes_les_images = []\n\n#Parcourez le dossier et ajouter toutes les images à la liste\nfor nom_fichier in os.listdir(dossier_images):\n    if nom_fichier.endswith(\".png\"):\n        chemin_complet = os.path.join(dossier_images, nom_fichier)\n        image = Image.open(chemin_complet)\n        toutes_les_images.append(image)\n\n#Sélectionner 10 images au hasard parmi les 100\nselect_10_images_train = random.sample(toutes_les_images, 10)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:21.994717Z","iopub.execute_input":"2023-12-27T08:06:21.995443Z","iopub.status.idle":"2023-12-27T08:06:29.626692Z","shell.execute_reply.started":"2023-12-27T08:06:21.995397Z","shell.execute_reply":"2023-12-27T08:06:29.62524Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(toutes_les_images)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:29.628314Z","iopub.execute_input":"2023-12-27T08:06:29.628799Z","iopub.status.idle":"2023-12-27T08:06:29.636552Z","shell.execute_reply.started":"2023-12-27T08:06:29.628748Z","shell.execute_reply":"2023-12-27T08:06:29.635304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Sélectionner 10 images au hasard parmi les 100\nselect_10_images_train = random.sample(toutes_les_images, 10)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:29.637954Z","iopub.execute_input":"2023-12-27T08:06:29.638523Z","iopub.status.idle":"2023-12-27T08:06:29.648035Z","shell.execute_reply.started":"2023-12-27T08:06:29.638468Z","shell.execute_reply":"2023-12-27T08:06:29.64669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"select_10_images_train[0]","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:29.650716Z","iopub.execute_input":"2023-12-27T08:06:29.651115Z","iopub.status.idle":"2023-12-27T08:06:31.924509Z","shell.execute_reply.started":"2023-12-27T08:06:29.651082Z","shell.execute_reply":"2023-12-27T08:06:31.923098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"select_10_images_train[0].size","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:31.926267Z","iopub.execute_input":"2023-12-27T08:06:31.927072Z","iopub.status.idle":"2023-12-27T08:06:31.936466Z","shell.execute_reply.started":"2023-12-27T08:06:31.927018Z","shell.execute_reply":"2023-12-27T08:06:31.934974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Etapes de resize\n\n#Définissez les dimensions souhaitées pour la redimension\nnouvelle_taille = (224,224) # taille choisi selon souvenir wagon\n\n#Créer une liste pour stocker les images redimensionnées\nselect_10_images_train_resize = []\n\n#Parcourir chaque image dans la liste des \"select_10_images_train\" et redimensionner \nfor image in select_10_images_train:\n    image_redimensionnee = image.resize(nouvelle_taille)\n    select_10_images_train_resize.append(image_redimensionnee)\n   ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:31.938067Z","iopub.execute_input":"2023-12-27T08:06:31.938417Z","iopub.status.idle":"2023-12-27T08:06:35.182319Z","shell.execute_reply.started":"2023-12-27T08:06:31.938388Z","shell.execute_reply":"2023-12-27T08:06:35.181329Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Vérification\nselect_10_images_train_resize[0].size","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.183821Z","iopub.execute_input":"2023-12-27T08:06:35.18477Z","iopub.status.idle":"2023-12-27T08:06:35.193169Z","shell.execute_reply.started":"2023-12-27T08:06:35.184722Z","shell.execute_reply":"2023-12-27T08:06:35.191899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Vérification\nselect_10_images_train_resize[8].size","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.19488Z","iopub.execute_input":"2023-12-27T08:06:35.195661Z","iopub.status.idle":"2023-12-27T08:06:35.205648Z","shell.execute_reply.started":"2023-12-27T08:06:35.19562Z","shell.execute_reply":"2023-12-27T08:06:35.204781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(select_10_images_train_resize)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.20685Z","iopub.execute_input":"2023-12-27T08:06:35.207873Z","iopub.status.idle":"2023-12-27T08:06:35.218853Z","shell.execute_reply.started":"2023-12-27T08:06:35.207827Z","shell.execute_reply":"2023-12-27T08:06:35.217388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Regarder s'il y a du bruit sur les images\n#convert(\"L\") pour les images gris\n#Première étape : passer les images en gris \nselect_10_images_train_resize_gris = []\n\nfor image in select_10_images_train_resize:\n    image_gris = image.convert('L')\n    select_10_images_train_resize_gris.append(image_gris)\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.220423Z","iopub.execute_input":"2023-12-27T08:06:35.220896Z","iopub.status.idle":"2023-12-27T08:06:35.230565Z","shell.execute_reply.started":"2023-12-27T08:06:35.220841Z","shell.execute_reply":"2023-12-27T08:06:35.229581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" select_10_images_train_resize_gris[8]","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.232184Z","iopub.execute_input":"2023-12-27T08:06:35.233333Z","iopub.status.idle":"2023-12-27T08:06:35.248132Z","shell.execute_reply.started":"2023-12-27T08:06:35.233287Z","shell.execute_reply":"2023-12-27T08:06:35.246833Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" select_10_images_train_resize_gris[8]","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.250125Z","iopub.execute_input":"2023-12-27T08:06:35.250585Z","iopub.status.idle":"2023-12-27T08:06:35.264471Z","shell.execute_reply.started":"2023-12-27T08:06:35.250542Z","shell.execute_reply":"2023-12-27T08:06:35.263086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Comment j'indique le bon chemin, il ya un problème de lecture du chemin \n\n#test pour le bruit sur une image\n# Chargez votre image redimensionnée (assurez-vous qu'elle est en niveaux de gris)\n#image = cv2.imread(\"select_10_images_train_resize_gris[8]\", cv2.IMREAD_GRAYSCALE)\n\n# Calculez le gradient en utilisant un filtre de Sobel\ngradient_x = cv2.Sobel(np.array(select_10_images_train_resize_gris[8]), cv2.CV_64F, 1, 0, ksize=3)\ngradient_y = cv2.Sobel(np.array(select_10_images_train_resize_gris[8]), cv2.CV_64F, 0, 1, ksize=3)\n\n# Calculez la magnitude du gradient\ngradient_magnitude = np.sqrt(gradient_x**2 + gradient_y**2)\n\n# Calculez la moyenne de la magnitude du gradient\nmean_gradient = np.mean(gradient_magnitude)\n\n# Vous pouvez définir un seuil pour déterminer s'il y a du bruit\nseuil = 20  # Vous pouvez ajuster ce seuil en fonction de vos besoins\n\nif mean_gradient > seuil:\n    print(\"L'image contient probablement du bruit.\")\nelse:\n    print(\"L'image semble propre.\")\n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.266587Z","iopub.execute_input":"2023-12-27T08:06:35.267211Z","iopub.status.idle":"2023-12-27T08:06:35.300883Z","shell.execute_reply.started":"2023-12-27T08:06:35.267173Z","shell.execute_reply":"2023-12-27T08:06:35.299729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Comment enlever les images avec du bruit\n#Filtre de Gauss\n# Chargez votre image avec du bruit (peut-être déjà redimensionnée)\n#image = cv2.imread(\"select_10_images_train_resize_gris[8]\")\n\n# Appliquez un filtre de Gauss pour réduire le bruit\n#image_lisse = cv2.GaussianBlur(image, (5, 5), 0)  # Vous pouvez ajuster la taille du noyau (5, 5) selon vos besoins\n","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.3024Z","iopub.execute_input":"2023-12-27T08:06:35.303127Z","iopub.status.idle":"2023-12-27T08:06:35.307866Z","shell.execute_reply.started":"2023-12-27T08:06:35.303093Z","shell.execute_reply":"2023-12-27T08:06:35.306486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.309531Z","iopub.execute_input":"2023-12-27T08:06:35.309888Z","iopub.status.idle":"2023-12-27T08:06:35.319635Z","shell.execute_reply.started":"2023-12-27T08:06:35.309856Z","shell.execute_reply":"2023-12-27T08:06:35.318131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Avec mon image de gris (par exemple, après avoir réduit le bruit)\n#image = cv2.imread(\"nselect_10_images_train_resize_gris[8]\", cv2.IMREAD_GRAYSCALE)\n\nimage = np.array(select_10_images_train_resize_gris[8])\n# Égalisez l'histogramme\nimage_egalisee = cv2.equalizeHist(image)\n\n# Affichez l'histogramme avant et après égalisation\nhist_before = cv2.calcHist([image], [0], None, [256], [0, 256])\nhist_after = cv2.calcHist([image_egalisee], [0], None, [256], [0, 256])\n\nplt.figure(figsize=(12, 6))\n\nplt.subplot(2, 2, 1)\nplt.title(\"Image originale\")\nplt.imshow(image, cmap='gray')\n\nplt.subplot(2, 2, 2)\nplt.title(\"Histogramme avant égalisation\")\nplt.plot(hist_before, color='black')\n\nplt.subplot(2, 2, 3)\nplt.title(\"Image après égalisation\")\nplt.imshow(image_egalisee, cmap='gray')\n\nplt.subplot(2, 2, 4)\nplt.title(\"Histogramme après égalisation\")\nplt.plot(hist_after, color='black')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:35.32112Z","iopub.execute_input":"2023-12-27T08:06:35.321477Z","iopub.status.idle":"2023-12-27T08:06:36.434782Z","shell.execute_reply.started":"2023-12-27T08:06:35.321436Z","shell.execute_reply":"2023-12-27T08:06:36.433314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalisation des images \n# avoir une image en niveaux de gris\nimage_normalisee = image.astype('float32') / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.436801Z","iopub.execute_input":"2023-12-27T08:06:36.437296Z","iopub.status.idle":"2023-12-27T08:06:36.443282Z","shell.execute_reply.started":"2023-12-27T08:06:36.437245Z","shell.execute_reply":"2023-12-27T08:06:36.441818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_normalisee","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.456871Z","iopub.execute_input":"2023-12-27T08:06:36.457324Z","iopub.status.idle":"2023-12-27T08:06:36.466626Z","shell.execute_reply.started":"2023-12-27T08:06:36.457288Z","shell.execute_reply":"2023-12-27T08:06:36.465164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# faire un bloc de code qui permet d'accéder directement à mes fichiers\nfrom pathlib import Path\n\nBASE_DIR = Path(\"/kaggle/input/UBC-OCEAN\")\nTRAIN_THUMBNAILS_DIR = BASE_DIR / \"train_thumbnails\"\nTRAIN_CSV = BASE_DIR / \"train.csv\"\n","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.46873Z","iopub.execute_input":"2023-12-27T08:06:36.4692Z","iopub.status.idle":"2023-12-27T08:06:36.476263Z","shell.execute_reply.started":"2023-12-27T08:06:36.469163Z","shell.execute_reply":"2023-12-27T08:06:36.474939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(TRAIN_CSV)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.478398Z","iopub.execute_input":"2023-12-27T08:06:36.478767Z","iopub.status.idle":"2023-12-27T08:06:36.491905Z","shell.execute_reply.started":"2023-12-27T08:06:36.478733Z","shell.execute_reply":"2023-12-27T08:06:36.490506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(10)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.494545Z","iopub.execute_input":"2023-12-27T08:06:36.495172Z","iopub.status.idle":"2023-12-27T08:06:36.508328Z","shell.execute_reply.started":"2023-12-27T08:06:36.495124Z","shell.execute_reply":"2023-12-27T08:06:36.507462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"image_id\"]","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.510211Z","iopub.execute_input":"2023-12-27T08:06:36.510888Z","iopub.status.idle":"2023-12-27T08:06:36.524466Z","shell.execute_reply.started":"2023-12-27T08:06:36.510853Z","shell.execute_reply":"2023-12-27T08:06:36.523289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train[\"label\"]","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.526496Z","iopub.execute_input":"2023-12-27T08:06:36.527192Z","iopub.status.idle":"2023-12-27T08:06:36.536923Z","shell.execute_reply.started":"2023-12-27T08:06:36.527155Z","shell.execute_reply":"2023-12-27T08:06:36.53596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#je me suis apercu que dans mon CSV, il y avait des images petites mais également des images grandes.\n#Il faut que je parvienne à sélectionner dans mon CSV que les images petites qui vont avec mon datasetthumbnails\n#quinzeimages_id = train.loc[train[\"is_tma\"] == False,\"image_id\"].head(15)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.538246Z","iopub.execute_input":"2023-12-27T08:06:36.539409Z","iopub.status.idle":"2023-12-27T08:06:36.548327Z","shell.execute_reply.started":"2023-12-27T08:06:36.53936Z","shell.execute_reply":"2023-12-27T08:06:36.547078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#quinzeimages_id","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.549826Z","iopub.execute_input":"2023-12-27T08:06:36.550199Z","iopub.status.idle":"2023-12-27T08:06:36.559251Z","shell.execute_reply.started":"2023-12-27T08:06:36.550169Z","shell.execute_reply":"2023-12-27T08:06:36.5582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#quinzeimages_label=train.loc[train[\"is_tma\"] == False,\"label\"].head(15)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.561181Z","iopub.execute_input":"2023-12-27T08:06:36.561925Z","iopub.status.idle":"2023-12-27T08:06:36.569265Z","shell.execute_reply.started":"2023-12-27T08:06:36.561878Z","shell.execute_reply":"2023-12-27T08:06:36.568229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#quinzeimages_label","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.571165Z","iopub.execute_input":"2023-12-27T08:06:36.57154Z","iopub.status.idle":"2023-12-27T08:06:36.580565Z","shell.execute_reply.started":"2023-12-27T08:06:36.571507Z","shell.execute_reply":"2023-12-27T08:06:36.579401Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n#zipped = zip(quinzeimages_id,quinzeimages_label)\n#list_14 = list(zipped)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.581888Z","iopub.execute_input":"2023-12-27T08:06:36.58278Z","iopub.status.idle":"2023-12-27T08:06:36.591991Z","shell.execute_reply.started":"2023-12-27T08:06:36.582747Z","shell.execute_reply":"2023-12-27T08:06:36.591033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#list_14","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.59331Z","iopub.execute_input":"2023-12-27T08:06:36.593705Z","iopub.status.idle":"2023-12-27T08:06:36.603484Z","shell.execute_reply.started":"2023-12-27T08:06:36.593672Z","shell.execute_reply":"2023-12-27T08:06:36.602321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Faire l'exercice sur toutes les images \nttesimages_id = train.loc[train[\"is_tma\"] == False,\"image_id\"]\nttesimages_label=train.loc[train[\"is_tma\"] == False,\"label\"]\nzipped = zip(ttesimages_id,ttesimages_label)\nlist_totale = list(zipped)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.605085Z","iopub.execute_input":"2023-12-27T08:06:36.605744Z","iopub.status.idle":"2023-12-27T08:06:36.618365Z","shell.execute_reply.started":"2023-12-27T08:06:36.605693Z","shell.execute_reply":"2023-12-27T08:06:36.617331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#list_totale","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.620089Z","iopub.execute_input":"2023-12-27T08:06:36.620712Z","iopub.status.idle":"2023-12-27T08:06:36.635072Z","shell.execute_reply.started":"2023-12-27T08:06:36.620676Z","shell.execute_reply":"2023-12-27T08:06:36.634031Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Exemple pour 14 images\n#image_list = []\n#label_list = []\n#nouvelle_taille = (224,224)\n#for image_id,label in list_14:\n    #image_path = TRAIN_THUMBNAILS_DIR / f\"{image_id}_thumbnail.png\"\n    #image = Image.open(image_path)\n    #image_redimensionnee_15 = image.resize(nouvelle_taille)\n    #image_gris_15 = image_redimensionnee_15.convert('L')\n    #Ici je fais passer en array car le astype ne fonctionne qu'avec un array numpy\n    #image_array_15 = np.array(image_gris_15)\n    #image_normalisee_15 = image_array_15.astype('float32') / 255.0\n    #image_list.append(image_normalisee_15)\n    #label_list.append(label)\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:36.636677Z","iopub.execute_input":"2023-12-27T08:06:36.637063Z","iopub.status.idle":"2023-12-27T08:06:36.647763Z","shell.execute_reply.started":"2023-12-27T08:06:36.637031Z","shell.execute_reply":"2023-12-27T08:06:36.646231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm.notebook import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:09:14.588609Z","iopub.execute_input":"2023-12-27T08:09:14.589104Z","iopub.status.idle":"2023-12-27T08:09:14.716517Z","shell.execute_reply.started":"2023-12-27T08:09:14.589064Z","shell.execute_reply":"2023-12-27T08:09:14.715268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Exemples pour toutes les images\nttes_image_list = []\nttes_label_list = []\nnouvelle_taille = (224,224)\nfor ttesimages_id,ttesimages_label in tqdm(list_totale):\n    image_path = TRAIN_THUMBNAILS_DIR / f\"{ttesimages_id}_thumbnail.png\"\n    image = Image.open(image_path)\n    image_redimensionnee_ttes = image.resize(nouvelle_taille)\n    image_gris_ttes = image_redimensionnee_ttes.convert('L')\n    #Ici je fais passer en array car le astype ne fonctionne qu'avec un array numpy\n    image_array_ttes = np.array(image_gris_ttes)\n    image_normalisee_ttes = image_array_ttes.astype('float32') / 255.0\n    ttes_image_list.append(image_normalisee_ttes)\n    ttes_label_list.append(ttesimages_label)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:09:43.68897Z","iopub.execute_input":"2023-12-27T08:09:43.689452Z","iopub.status.idle":"2023-12-27T08:12:05.689339Z","shell.execute_reply.started":"2023-12-27T08:09:43.689419Z","shell.execute_reply":"2023-12-27T08:12:05.687991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:14:48.658831Z","iopub.execute_input":"2023-12-27T08:14:48.660297Z","iopub.status.idle":"2023-12-27T08:14:49.044511Z","shell.execute_reply.started":"2023-12-27T08:14:48.660234Z","shell.execute_reply":"2023-12-27T08:14:49.043357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttes_label_list = le.fit_transform(ttes_label_list)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:14:52.183343Z","iopub.execute_input":"2023-12-27T08:14:52.183766Z","iopub.status.idle":"2023-12-27T08:14:52.190353Z","shell.execute_reply.started":"2023-12-27T08:14:52.183731Z","shell.execute_reply":"2023-12-27T08:14:52.189074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#pour récuperer les ID, ","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:37.141192Z","iopub.status.idle":"2023-12-27T08:06:37.141617Z","shell.execute_reply.started":"2023-12-27T08:06:37.14141Z","shell.execute_reply":"2023-12-27T08:06:37.14143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#label_list","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:37.142985Z","iopub.status.idle":"2023-12-27T08:06:37.143459Z","shell.execute_reply.started":"2023-12-27T08:06:37.143235Z","shell.execute_reply":"2023-12-27T08:06:37.143256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#le.classes_","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:06:37.144872Z","iopub.status.idle":"2023-12-27T08:06:37.145307Z","shell.execute_reply.started":"2023-12-27T08:06:37.145107Z","shell.execute_reply":"2023-12-27T08:06:37.145127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(ttes_image_list)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:14:55.604147Z","iopub.execute_input":"2023-12-27T08:14:55.604615Z","iopub.status.idle":"2023-12-27T08:14:55.613107Z","shell.execute_reply.started":"2023-12-27T08:14:55.604582Z","shell.execute_reply":"2023-12-27T08:14:55.611874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#créer le modèle\n\nmodel = Sequential([\n  layers.Conv2D(16, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(32, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Conv2D(64, 3, padding='same', activation='relu'),\n  layers.MaxPooling2D(),\n  layers.Flatten(),\n  layers.Dense(128, activation='relu'),\n  layers.Dense(5)\n])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:19.048591Z","iopub.execute_input":"2023-12-27T08:32:19.049067Z","iopub.status.idle":"2023-12-27T08:32:19.075116Z","shell.execute_reply.started":"2023-12-27T08:32:19.049025Z","shell.execute_reply":"2023-12-27T08:32:19.074223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:21.963241Z","iopub.execute_input":"2023-12-27T08:32:21.963672Z","iopub.status.idle":"2023-12-27T08:32:21.97872Z","shell.execute_reply.started":"2023-12-27T08:32:21.963639Z","shell.execute_reply":"2023-12-27T08:32:21.977626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttes_image_list = np.array(ttes_image_list)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:43.353656Z","iopub.execute_input":"2023-12-27T08:32:43.354205Z","iopub.status.idle":"2023-12-27T08:32:43.409967Z","shell.execute_reply.started":"2023-12-27T08:32:43.354163Z","shell.execute_reply":"2023-12-27T08:32:43.408478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttes_image_list.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:45.287194Z","iopub.execute_input":"2023-12-27T08:32:45.288047Z","iopub.status.idle":"2023-12-27T08:32:45.294929Z","shell.execute_reply.started":"2023-12-27T08:32:45.287994Z","shell.execute_reply":"2023-12-27T08:32:45.294061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttes_image_list = ttes_image_list.reshape(513,224,224,1)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:48.825361Z","iopub.execute_input":"2023-12-27T08:32:48.82578Z","iopub.status.idle":"2023-12-27T08:32:48.831683Z","shell.execute_reply.started":"2023-12-27T08:32:48.825749Z","shell.execute_reply":"2023-12-27T08:32:48.830076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ttes_image_list.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:51.923422Z","iopub.execute_input":"2023-12-27T08:32:51.923882Z","iopub.status.idle":"2023-12-27T08:32:51.931041Z","shell.execute_reply.started":"2023-12-27T08:32:51.923843Z","shell.execute_reply":"2023-12-27T08:32:51.929684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_test, y_train, y_test = train_test_split(ttes_image_list,np.array( ttes_label_list),test_size = 0.3,random_state = 6)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:55.633433Z","iopub.execute_input":"2023-12-27T08:32:55.633874Z","iopub.status.idle":"2023-12-27T08:32:55.674719Z","shell.execute_reply.started":"2023-12-27T08:32:55.633838Z","shell.execute_reply":"2023-12-27T08:32:55.673523Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = tf.data.Dataset.from_tensor_slices((X_train,y_train))\ntrain_dataset = train_dataset.batch(32)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:58.771199Z","iopub.execute_input":"2023-12-27T08:32:58.771672Z","iopub.status.idle":"2023-12-27T08:32:58.84352Z","shell.execute_reply.started":"2023-12-27T08:32:58.771623Z","shell.execute_reply":"2023-12-27T08:32:58.841899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = tf.data.Dataset.from_tensor_slices((X_test,y_test))\ntest_dataset = test_dataset.batch(32)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:32:59.925256Z","iopub.execute_input":"2023-12-27T08:32:59.925674Z","iopub.status.idle":"2023-12-27T08:32:59.961205Z","shell.execute_reply.started":"2023-12-27T08:32:59.925641Z","shell.execute_reply":"2023-12-27T08:32:59.960147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(\n  train_dataset,\n  epochs=epochs,\n    validation_data = test_dataset,\n    batch_size = 32\n)","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:33:01.484701Z","iopub.execute_input":"2023-12-27T08:33:01.485154Z","iopub.status.idle":"2023-12-27T08:35:51.344089Z","shell.execute_reply.started":"2023-12-27T08:33:01.485116Z","shell.execute_reply":"2023-12-27T08:35:51.342497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:36:12.302073Z","iopub.execute_input":"2023-12-27T08:36:12.30252Z","iopub.status.idle":"2023-12-27T08:36:12.339603Z","shell.execute_reply.started":"2023-12-27T08:36:12.302485Z","shell.execute_reply":"2023-12-27T08:36:12.338312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"acc = history.history['accuracy']\nval_acc = history.history['val_accuracy']\n\nloss = history.history['loss']\nval_loss = history.history['val_loss']\n\nepochs_range = range(epochs)\n\nplt.figure(figsize=(8, 8))\nplt.subplot(1, 2, 1)\nplt.plot(epochs_range, acc, label='Training Accuracy')\nplt.plot(epochs_range, val_acc, label='Validation Accuracy')\nplt.legend(loc='lower right')\nplt.title('Training and Validation Accuracy')\n\nplt.subplot(1, 2, 2)\nplt.plot(epochs_range, loss, label='Training Loss')\nplt.plot(epochs_range, val_loss, label='Validation Loss')\nplt.legend(loc='upper right')\nplt.title('Training and Validation Loss')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-27T08:46:30.43574Z","iopub.execute_input":"2023-12-27T08:46:30.436297Z","iopub.status.idle":"2023-12-27T08:46:31.0154Z","shell.execute_reply.started":"2023-12-27T08:46:30.436255Z","shell.execute_reply":"2023-12-27T08:46:31.014214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}