{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","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":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import matplotlib.pyplot as plt\nimport numpy as np\nimport os\nimport cv2\nimport pandas as pd\nfrom sklearn.preprocessing import OneHotEncoder\nimport random\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense , Conv2D , MaxPooling2D ,BatchNormalization ,InputLayer ,Flatten\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.losses import CategoricalCrossentropy\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns\n\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom collections import Counter\nfrom tensorflow.keras.regularizers import l2\nfrom keras.layers import Dropout","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-11-20T16:09:16.175443Z","iopub.execute_input":"2024-11-20T16:09:16.176225Z","iopub.status.idle":"2024-11-20T16:09:16.185382Z","shell.execute_reply.started":"2024-11-20T16:09:16.176196Z","shell.execute_reply":"2024-11-20T16:09:16.184415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CONFIG ={\n    'IMAGE_SIZE' : 256,\n    'TRAIN_RATIO' : 0.9,\n    'BATCH_SIZE' : 32,\n    'N_CLASSES' :5 ,\n    'N_EPOCHS' :150 ,\n    'TEST' : 43,\n    'TRAIN_PATH' :'/kaggle/input/UBC-OCEAN/train_thumbnails',\n    'TEST_PATH' :'/kaggle/input/UBC-OCEAN/test_thumbnails',\n    'TRAIN_DF' :'/kaggle/input/UBC-OCEAN/train.csv',\n    'TEST_DF' :'/kaggle/input/UBC-OCEAN/test.csv'\n\n\n}","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:13:31.620047Z","iopub.execute_input":"2024-11-20T16:13:31.62074Z","iopub.status.idle":"2024-11-20T16:13:31.62522Z","shell.execute_reply.started":"2024-11-20T16:13:31.620708Z","shell.execute_reply":"2024-11-20T16:13:31.624285Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(CONFIG['TRAIN_DF'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:42:51.188674Z","iopub.execute_input":"2024-11-20T15:42:51.189478Z","iopub.status.idle":"2024-11-20T15:42:51.211609Z","shell.execute_reply.started":"2024-11-20T15:42:51.189452Z","shell.execute_reply":"2024-11-20T15:42:51.210796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:42:54.210335Z","iopub.execute_input":"2024-11-20T15:42:54.210684Z","iopub.status.idle":"2024-11-20T15:42:54.236906Z","shell.execute_reply.started":"2024-11-20T15:42:54.210651Z","shell.execute_reply":"2024-11-20T15:42:54.236086Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def list_folders_in_folder(folder_path):\n    files = os.listdir(folder_path)\n    return files","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:42:57.000052Z","iopub.execute_input":"2024-11-20T15:42:57.000372Z","iopub.status.idle":"2024-11-20T15:42:57.004694Z","shell.execute_reply.started":"2024-11-20T15:42:57.000348Z","shell.execute_reply":"2024-11-20T15:42:57.003763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folder = list_folders_in_folder(CONFIG['TRAIN_PATH'])\nprint(folder[0].split('_')[0])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:02.919847Z","iopub.execute_input":"2024-11-20T15:43:02.920172Z","iopub.status.idle":"2024-11-20T15:43:02.964306Z","shell.execute_reply.started":"2024-11-20T15:43:02.920149Z","shell.execute_reply":"2024-11-20T15:43:02.963435Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_df[train_df['image_id'] == 3055]['label'].iloc[0])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:05.39251Z","iopub.execute_input":"2024-11-20T15:43:05.393114Z","iopub.status.idle":"2024-11-20T15:43:05.404662Z","shell.execute_reply.started":"2024-11-20T15:43:05.393084Z","shell.execute_reply":"2024-11-20T15:43:05.403814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def list_files_in_folders(folder_path):\n    files = []\n    labels = []\n    for root, dirs, filenames in os.walk(folder_path):\n        for filename in filenames:\n            image_id = train_df[train_df['image_id'] == int(filename.split('_')[0])]['label'].iloc[0]\n            labels.append(image_id)\n            files.append(os.path.join(root, filename))\n    return files ,labels","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:11.533683Z","iopub.execute_input":"2024-11-20T15:43:11.534417Z","iopub.status.idle":"2024-11-20T15:43:11.539115Z","shell.execute_reply.started":"2024-11-20T15:43:11.534392Z","shell.execute_reply":"2024-11-20T15:43:11.538205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files ,labels = list_files_in_folders(CONFIG['TRAIN_PATH'])\nprint(files)\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:13.866318Z","iopub.execute_input":"2024-11-20T15:43:13.867113Z","iopub.status.idle":"2024-11-20T15:43:15.999513Z","shell.execute_reply.started":"2024-11-20T15:43:13.867083Z","shell.execute_reply":"2024-11-20T15:43:15.99865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(files)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:22.399822Z","iopub.execute_input":"2024-11-20T15:43:22.400143Z","iopub.status.idle":"2024-11-20T15:43:22.405592Z","shell.execute_reply.started":"2024-11-20T15:43:22.400119Z","shell.execute_reply":"2024-11-20T15:43:22.404777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'path': files, 'category': labels})","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:25.162175Z","iopub.execute_input":"2024-11-20T15:43:25.163232Z","iopub.status.idle":"2024-11-20T15:43:25.167329Z","shell.execute_reply.started":"2024-11-20T15:43:25.163201Z","shell.execute_reply":"2024-11-20T15:43:25.166465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_excel('UBC.xlsx', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:27.165184Z","iopub.execute_input":"2024-11-20T15:43:27.165542Z","iopub.status.idle":"2024-11-20T15:43:27.506004Z","shell.execute_reply.started":"2024-11-20T15:43:27.165512Z","shell.execute_reply":"2024-11-20T15:43:27.505059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:32.747335Z","iopub.execute_input":"2024-11-20T15:43:32.747902Z","iopub.status.idle":"2024-11-20T15:43:32.756159Z","shell.execute_reply.started":"2024-11-20T15:43:32.747874Z","shell.execute_reply":"2024-11-20T15:43:32.755272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def read_image(df):\n    images = []\n    labels = []\n    for index, row in df.iterrows():\n       image = cv2.imread(row['path'])\n       image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n       images.append(image)\n       labels.append(row['category'])\n    return images , labels","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:35.235299Z","iopub.execute_input":"2024-11-20T15:43:35.236393Z","iopub.status.idle":"2024-11-20T15:43:35.241453Z","shell.execute_reply.started":"2024-11-20T15:43:35.236352Z","shell.execute_reply":"2024-11-20T15:43:35.240594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = read_image(df)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T15:43:40.503902Z","iopub.execute_input":"2024-11-20T15:43:40.504232Z","iopub.status.idle":"2024-11-20T15:45:30.319725Z","shell.execute_reply.started":"2024-11-20T15:43:40.504206Z","shell.execute_reply":"2024-11-20T15:45:30.318814Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.hist(labels)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:05:29.826936Z","iopub.execute_input":"2024-11-20T16:05:29.827707Z","iopub.status.idle":"2024-11-20T16:05:30.008652Z","shell.execute_reply.started":"2024-11-20T16:05:29.827678Z","shell.execute_reply":"2024-11-20T16:05:30.007646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_numbers = [random.randint(0, len(images)) for _ in range(16)]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:05:51.704164Z","iopub.execute_input":"2024-11-20T16:05:51.704467Z","iopub.status.idle":"2024-11-20T16:05:51.709337Z","shell.execute_reply.started":"2024-11-20T16:05:51.704443Z","shell.execute_reply":"2024-11-20T16:05:51.708147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_numbers","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:05:53.898015Z","iopub.execute_input":"2024-11-20T16:05:53.898311Z","iopub.status.idle":"2024-11-20T16:05:53.903854Z","shell.execute_reply.started":"2024-11-20T16:05:53.89829Z","shell.execute_reply":"2024-11-20T16:05:53.902953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_normalize_batch(images, labels):\n    resized_images = []\n    for image in images:\n        resized_image = cv2.resize(image, (CONFIG['IMAGE_SIZE'], CONFIG['IMAGE_SIZE']))\n      #  resized_image = resized_image / 255.\n        resized_images.append(resized_image)\n    return np.array(resized_images), labels","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:05:59.307196Z","iopub.execute_input":"2024-11-20T16:05:59.307835Z","iopub.status.idle":"2024-11-20T16:05:59.312156Z","shell.execute_reply.started":"2024-11-20T16:05:59.307806Z","shell.execute_reply":"2024-11-20T16:05:59.31107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nimages , labels = resize_normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:06:01.75041Z","iopub.execute_input":"2024-11-20T16:06:01.751234Z","iopub.status.idle":"2024-11-20T16:06:02.056057Z","shell.execute_reply.started":"2024-11-20T16:06:01.751205Z","shell.execute_reply":"2024-11-20T16:06:02.05511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Augmentation de données","metadata":{}},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    rotation_range=0,\n    width_shift_range=0.1,\n    height_shift_range=0.1,\n    shear_range=0,\n    zoom_range=0,\n    horizontal_flip=True,\n   fill_mode='nearest'\n)\n\n\n# Compter les images par label\nlabel_counts = Counter(labels)\n\n# Regrouper les images par label\ngrouped_images = {}\nfor img, label in zip(images, labels):\n    if label not in grouped_images:\n        grouped_images[label] = []\n    grouped_images[label].append(img)\n\naugmented_images = []\naugmented_labels = []\n\n# Traiter les images par label\nfor label, img_list in grouped_images.items():\n    for img_index, img in enumerate (img_list):\n        \n        if label_counts[label] >= 200:\n            break  \n\n        img = img.reshape((1,) + img.shape)  # Reshape de l'image\n\n\n        # Ajustement du batch_size selon le label\n        batch_size = 3 if label in [\"EC\", \"CC\"] else 4\n        stop=0\n        # Générer des images augmentées\n        for batch in datagen.flow(img, batch_size=batch_size):\n            for augmented_image in batch:\n                augmented_images.append(augmented_image.astype(np.uint8))\n                augmented_labels.append(label)\n                label_counts[label] += 1  # Incrémentez le compteur\n\n                stop+=1\n                if  label_counts[label] >= 200 :\n                    break\n            if stop>=batch_size or label_counts[label] >=200 :\n                break  # Arrêtez le flux si le nombre est atteint\n            continue\n        if label_counts[label] >= 200:\n            break  # Passez à l'étiquette suivante\n\n# Convertir les listes en tableaux numpy\nimages = np.concatenate((images, np.array(augmented_images)), axis=0)\nlabels.extend(augmented_labels)\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:06:07.814652Z","iopub.execute_input":"2024-11-20T16:06:07.815309Z","iopub.status.idle":"2024-11-20T16:06:13.325394Z","shell.execute_reply.started":"2024-11-20T16:06:07.81528Z","shell.execute_reply":"2024-11-20T16:06:13.324636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure()\nplt.hist(labels)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:06:34.710312Z","iopub.execute_input":"2024-11-20T16:06:34.711153Z","iopub.status.idle":"2024-11-20T16:06:34.818438Z","shell.execute_reply.started":"2024-11-20T16:06:34.711122Z","shell.execute_reply":"2024-11-20T16:06:34.81745Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def normalize_batch(images, labels):\n    resized_images = []\n    for image in images:\n        resized_image = image\n        resized_image = resized_image / 255.\n        resized_images.append(resized_image)\n    return np.array(resized_images), labels\n\nimages = images.astype(np.float32)\nimages , labels = normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:06:40.466068Z","iopub.execute_input":"2024-11-20T16:06:40.466808Z","iopub.status.idle":"2024-11-20T16:06:40.877965Z","shell.execute_reply.started":"2024-11-20T16:06:40.466779Z","shell.execute_reply":"2024-11-20T16:06:40.877239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_images_to_display = 16\nlast_images = images[-num_images_to_display:]  # Les 16 dernières images\nlast_labels = labels[-num_images_to_display:]  # Les 16 derniers labels\n\nplt.figure(figsize=(12, 12))\nfor i in range(num_images_to_display):\n    plt.subplot(4, 4, i + 1)  # Grille 4x4\n    plt.imshow(last_images[i].reshape(CONFIG['IMAGE_SIZE'], CONFIG['IMAGE_SIZE'],3))  # Assurez-vous que l'image est au bon format\n    plt.title(f\"Label: {last_labels[i]}\")\n    plt.axis('off')\n\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:07:14.968127Z","iopub.execute_input":"2024-11-20T16:07:14.968861Z","iopub.status.idle":"2024-11-20T16:07:16.878345Z","shell.execute_reply.started":"2024-11-20T16:07:14.968814Z","shell.execute_reply":"2024-11-20T16:07:16.877126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axes = plt.subplots(4, 4,figsize=(8,8))\naxes = axes.flatten()\n\nfor i, ax in enumerate(axes):\n    rand = random_numbers[i]\n    if rand < len(images):\n        ax.imshow(images[rand])\n        ax.set_title(labels[rand])\n        ax.axis('off')\n    else:\n        ax.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:06:43.376939Z","iopub.execute_input":"2024-11-20T16:06:43.377773Z","iopub.status.idle":"2024-11-20T16:06:44.39448Z","shell.execute_reply.started":"2024-11-20T16:06:43.377736Z","shell.execute_reply":"2024-11-20T16:06:44.393603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[5].shape","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:07:30.084968Z","iopub.execute_input":"2024-11-20T16:07:30.085614Z","iopub.status.idle":"2024-11-20T16:07:30.091425Z","shell.execute_reply.started":"2024-11-20T16:07:30.085587Z","shell.execute_reply":"2024-11-20T16:07:30.090538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index = {label: i for i, label in enumerate(set(labels))}","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:07:32.888925Z","iopub.execute_input":"2024-11-20T16:07:32.889549Z","iopub.status.idle":"2024-11-20T16:07:32.89348Z","shell.execute_reply.started":"2024-11-20T16:07:32.889523Z","shell.execute_reply":"2024-11-20T16:07:32.892692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:07:35.456492Z","iopub.execute_input":"2024-11-20T16:07:35.457366Z","iopub.status.idle":"2024-11-20T16:07:35.462513Z","shell.execute_reply.started":"2024-11-20T16:07:35.457325Z","shell.execute_reply":"2024-11-20T16:07:35.461606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [label_to_index[label] for label in labels]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:07:37.649128Z","iopub.execute_input":"2024-11-20T16:07:37.649721Z","iopub.status.idle":"2024-11-20T16:07:37.653636Z","shell.execute_reply.started":"2024-11-20T16:07:37.649693Z","shell.execute_reply":"2024-11-20T16:07:37.652671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Ajout d'un jeu test","metadata":{}},{"cell_type":"code","source":"final_test_images=images[:CONFIG['TEST']]\nfinal_test_lab=labels[:CONFIG['TEST']]\nlabels=labels[CONFIG['TEST']:]\nimages=images[CONFIG['TEST']:]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:13.423116Z","iopub.execute_input":"2024-11-20T16:08:13.423785Z","iopub.status.idle":"2024-11-20T16:08:13.427727Z","shell.execute_reply.started":"2024-11-20T16:08:13.423756Z","shell.execute_reply":"2024-11-20T16:08:13.426876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)\nfinal_test_lab=tf.convert_to_tensor(final_test_lab)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:22.475396Z","iopub.execute_input":"2024-11-20T16:08:22.476236Z","iopub.status.idle":"2024-11-20T16:08:22.974612Z","shell.execute_reply.started":"2024-11-20T16:08:22.476204Z","shell.execute_reply":"2024-11-20T16:08:22.97365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)\nfinal_test_images= tf.convert_to_tensor(final_test_images)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:28.018371Z","iopub.execute_input":"2024-11-20T16:08:28.019289Z","iopub.status.idle":"2024-11-20T16:08:28.517168Z","shell.execute_reply.started":"2024-11-20T16:08:28.019245Z","shell.execute_reply":"2024-11-20T16:08:28.516343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices((images, labels))\nfinal_test_dataset=tf.data.Dataset.from_tensor_slices((final_test_images, final_test_lab))","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:30.316872Z","iopub.execute_input":"2024-11-20T16:08:30.317723Z","iopub.status.idle":"2024-11-20T16:08:30.488161Z","shell.execute_reply.started":"2024-11-20T16:08:30.317692Z","shell.execute_reply":"2024-11-20T16:08:30.487197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shuffle_buffer_size = len(images)\ndataset = dataset.shuffle(shuffle_buffer_size)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:33.866519Z","iopub.execute_input":"2024-11-20T16:08:33.867206Z","iopub.status.idle":"2024-11-20T16:08:33.876157Z","shell.execute_reply.started":"2024-11-20T16:08:33.867176Z","shell.execute_reply":"2024-11-20T16:08:33.875281Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(images) * CONFIG['TRAIN_RATIO'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:35.580368Z","iopub.execute_input":"2024-11-20T16:08:35.581277Z","iopub.status.idle":"2024-11-20T16:08:35.585137Z","shell.execute_reply.started":"2024-11-20T16:08:35.581245Z","shell.execute_reply":"2024-11-20T16:08:35.584198Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = dataset.take(train_size)\ntest_dataset = dataset.skip(train_size)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:40.176564Z","iopub.execute_input":"2024-11-20T16:08:40.17692Z","iopub.status.idle":"2024-11-20T16:08:40.189741Z","shell.execute_reply.started":"2024-11-20T16:08:40.176895Z","shell.execute_reply":"2024-11-20T16:08:40.188862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.batch(CONFIG['BATCH_SIZE'])\ntest_dataset = test_dataset.batch(CONFIG['BATCH_SIZE'])\nfinal_test_dataset = final_test_dataset.batch(CONFIG['BATCH_SIZE'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:42.014377Z","iopub.execute_input":"2024-11-20T16:08:42.01473Z","iopub.status.idle":"2024-11-20T16:08:42.025156Z","shell.execute_reply.started":"2024-11-20T16:08:42.014703Z","shell.execute_reply":"2024-11-20T16:08:42.024442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\ntest_dataset = test_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:08:44.096847Z","iopub.execute_input":"2024-11-20T16:08:44.097179Z","iopub.status.idle":"2024-11-20T16:08:44.104683Z","shell.execute_reply.started":"2024-11-20T16:08:44.097152Z","shell.execute_reply":"2024-11-20T16:08:44.103963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Ajout de dropout et de régularisateur L2\nmodel = Sequential([\n    InputLayer(input_shape =(CONFIG['IMAGE_SIZE'] ,CONFIG['IMAGE_SIZE'],3)),\n    Conv2D(filters = 6 ,kernel_size= 3 ,padding ='valid' , activation = 'relu'),\n    BatchNormalization(),\n    MaxPooling2D(pool_size=(2,2) , strides=2),\n\n\n     Conv2D(filters = 16 ,kernel_size= 3 ,padding ='valid' , activation = 'relu'),\n    BatchNormalization(),\n    MaxPooling2D(pool_size=(2,2) , strides=2),\n\n    Flatten(),\n    Dense(8 , activation = 'relu',kernel_regularizer=l2(0.01)),\n    BatchNormalization(),\n    Dropout(0.3),\n\n    Dense(2 , activation = 'relu',kernel_regularizer=l2(0.01)),\n    BatchNormalization(),\n\n    Dense(CONFIG['N_CLASSES'], activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:13:59.390112Z","iopub.execute_input":"2024-11-20T16:13:59.390678Z","iopub.status.idle":"2024-11-20T16:13:59.465027Z","shell.execute_reply.started":"2024-11-20T16:13:59.390649Z","shell.execute_reply":"2024-11-20T16:13:59.46413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:14:05.500693Z","iopub.execute_input":"2024-11-20T16:14:05.501507Z","iopub.status.idle":"2024-11-20T16:14:05.524699Z","shell.execute_reply.started":"2024-11-20T16:14:05.501474Z","shell.execute_reply":"2024-11-20T16:14:05.523658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam' , loss = 'sparse_categorical_crossentropy' ,  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:14:10.792747Z","iopub.execute_input":"2024-11-20T16:14:10.793442Z","iopub.status.idle":"2024-11-20T16:14:10.800894Z","shell.execute_reply.started":"2024-11-20T16:14:10.793414Z","shell.execute_reply":"2024-11-20T16:14:10.800038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset, validation_data=test_dataset , epochs=CONFIG['N_EPOCHS'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:14:12.601482Z","iopub.execute_input":"2024-11-20T16:14:12.602139Z","iopub.status.idle":"2024-11-20T16:15:52.822458Z","shell.execute_reply.started":"2024-11-20T16:14:12.602108Z","shell.execute_reply":"2024-11-20T16:15:52.821704Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(test_dataset)\nprint(f\"Test Accuracy: {test_accuracy}\")\nprint(f\"Test Loss: {test_loss}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:15:56.525114Z","iopub.execute_input":"2024-11-20T16:15:56.525987Z","iopub.status.idle":"2024-11-20T16:15:56.597677Z","shell.execute_reply.started":"2024-11-20T16:15:56.525952Z","shell.execute_reply":"2024-11-20T16:15:56.596916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\n\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:02.548508Z","iopub.execute_input":"2024-11-20T16:16:02.549187Z","iopub.status.idle":"2024-11-20T16:16:02.815042Z","shell.execute_reply.started":"2024-11-20T16:16:02.549156Z","shell.execute_reply":"2024-11-20T16:16:02.814105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:11.439895Z","iopub.execute_input":"2024-11-20T16:16:11.44051Z","iopub.status.idle":"2024-11-20T16:16:11.657848Z","shell.execute_reply.started":"2024-11-20T16:16:11.440483Z","shell.execute_reply":"2024-11-20T16:16:11.65697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_class_name(index):\n    return list(label_to_index.keys())[index]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:11:49.774528Z","iopub.execute_input":"2024-11-20T16:11:49.774881Z","iopub.status.idle":"2024-11-20T16:11:49.779252Z","shell.execute_reply.started":"2024-11-20T16:11:49.774854Z","shell.execute_reply":"2024-11-20T16:11:49.77834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nfor i, (image, label) in enumerate(test_dataset.take(9)):\n  ax = plt.subplot(3, 3, i + 1)\n  ax.imshow(image[0])\n  plt.title(f\"True: {get_class_name(label.numpy()[0])}\\nPredicted: {get_class_name(np.argmax(model.predict(image), axis=1)[0])}\")\n  plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:19.408691Z","iopub.execute_input":"2024-11-20T16:16:19.40902Z","iopub.status.idle":"2024-11-20T16:16:21.069852Z","shell.execute_reply.started":"2024-11-20T16:16:19.408995Z","shell.execute_reply":"2024-11-20T16:16:21.068952Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nfor i, (image, label) in enumerate(test_dataset.take(16)):\n    ax = plt.subplot(4, 4, i + 1)\n    ax.imshow(image[0].numpy())\n    ax.set_title(f\"True: {get_class_name(label.numpy()[0])}\\nPredicted: {get_class_name(np.argmax(model.predict(image), axis=1)[0])}\")\n    ax.axis('off')\n\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:28.890871Z","iopub.execute_input":"2024-11-20T16:16:28.891185Z","iopub.status.idle":"2024-11-20T16:16:29.935273Z","shell.execute_reply.started":"2024-11-20T16:16:28.891163Z","shell.execute_reply":"2024-11-20T16:16:29.934419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_labels = []\npredicted_labels = []\nim = []\nfor images, labels in final_test_dataset:\n#    labels = encoder.inverse_transform(labels)\n    for image, label in zip(images, labels):\n\n        \n        true_labels.append(label.numpy())  # On récupère le premier élément car inverse_transform retourne un tableau 2D\n        im.append(image.numpy())\n        \n       # decoded_labels = encoder.inverse_transform(label)\n        #true_labels.append(decoded_label.numpy())\n        #im.append(image.numpy())\n        \n        predicted_label = np.argmax(model.predict(np.expand_dims(image, axis=0)), axis=1)[0]\n        predicted_labels.append(predicted_label)\nprint(true_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:36.136193Z","iopub.execute_input":"2024-11-20T16:16:36.136973Z","iopub.status.idle":"2024-11-20T16:16:38.801548Z","shell.execute_reply.started":"2024-11-20T16:16:36.136944Z","shell.execute_reply":"2024-11-20T16:16:38.800752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\nfor i in range(16):\n    ax = plt.subplot(4, 4, i + 1)\n    ax.imshow(im[i])\n    ax.set_title(f\"True: {get_class_name(true_labels[i])}\\nPredicted: {get_class_name(predicted_labels[i])}\")\n    ax.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:42.437457Z","iopub.execute_input":"2024-11-20T16:16:42.437827Z","iopub.status.idle":"2024-11-20T16:16:43.762013Z","shell.execute_reply.started":"2024-11-20T16:16:42.437797Z","shell.execute_reply":"2024-11-20T16:16:43.761131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_labels_name=[get_class_name(label)for label in true_labels]\npredicted_labels_name=[get_class_name(label)for label in predicted_labels]\n#print(true_labels_name)\n#print(predicted_labels_name)\nall_labels = sorted(set(true_labels_name) | set(predicted_labels_name))\ncm = confusion_matrix(true_labels_name, predicted_labels_name, labels=all_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:49.970953Z","iopub.execute_input":"2024-11-20T16:16:49.971537Z","iopub.status.idle":"2024-11-20T16:16:49.977462Z","shell.execute_reply.started":"2024-11-20T16:16:49.971508Z","shell.execute_reply":"2024-11-20T16:16:49.976571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(true_labels, predicted_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:52.355048Z","iopub.execute_input":"2024-11-20T16:16:52.355723Z","iopub.status.idle":"2024-11-20T16:16:52.361503Z","shell.execute_reply.started":"2024-11-20T16:16:52.355692Z","shell.execute_reply":"2024-11-20T16:16:52.360645Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, cmap='Blues', fmt='g',xticklabels=all_labels, yticklabels=all_labels)\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:16:57.326803Z","iopub.execute_input":"2024-11-20T16:16:57.32714Z","iopub.status.idle":"2024-11-20T16:16:57.575572Z","shell.execute_reply.started":"2024-11-20T16:16:57.327114Z","shell.execute_reply":"2024-11-20T16:16:57.574681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Prédiction binaire","metadata":{}},{"cell_type":"code","source":"images , labels = read_image(df)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:17:24.962959Z","iopub.execute_input":"2024-11-20T16:17:24.963683Z","iopub.status.idle":"2024-11-20T16:18:57.836054Z","shell.execute_reply.started":"2024-11-20T16:17:24.963633Z","shell.execute_reply":"2024-11-20T16:18:57.835184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def resize_normalize_batch(images, labels):\n    resized_images = []\n    for image in images:\n        resized_image = cv2.resize(image, (CONFIG['IMAGE_SIZE'], CONFIG['IMAGE_SIZE']))\n        resized_image = resized_image / 255.\n        resized_images.append(resized_image)\n    return np.array(resized_images), labels","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:19:12.882049Z","iopub.execute_input":"2024-11-20T16:19:12.882389Z","iopub.status.idle":"2024-11-20T16:19:12.887958Z","shell.execute_reply.started":"2024-11-20T16:19:12.882362Z","shell.execute_reply":"2024-11-20T16:19:12.886835Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = resize_normalize_batch(images , labels)\nprelabels=[]\nfor lab in labels :\n    if lab == 'HGSC':\n        prelabels.append(0)\n    else :\n        prelabels.append(1)\n#print(labels)\nprint(prelabels)\nprint(\"Longueur de prelabels :\", len(prelabels))","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:19:15.692213Z","iopub.execute_input":"2024-11-20T16:19:15.692784Z","iopub.status.idle":"2024-11-20T16:19:16.972994Z","shell.execute_reply.started":"2024-11-20T16:19:15.692755Z","shell.execute_reply":"2024-11-20T16:19:16.972077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index = {label: i for i, label in enumerate(set(labels))}\nprelabel_to_index = {'HSSC':0, 'Other':1}","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:20:34.702058Z","iopub.execute_input":"2024-11-20T16:20:34.7024Z","iopub.status.idle":"2024-11-20T16:20:34.706925Z","shell.execute_reply.started":"2024-11-20T16:20:34.702373Z","shell.execute_reply":"2024-11-20T16:20:34.705987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [label_to_index[label] for label in labels]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:23:19.243592Z","iopub.execute_input":"2024-11-20T16:23:19.244497Z","iopub.status.idle":"2024-11-20T16:23:19.24849Z","shell.execute_reply.started":"2024-11-20T16:23:19.244465Z","shell.execute_reply":"2024-11-20T16:23:19.247598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"final_test_images=images[-CONFIG['TEST']:]\nfinal_test_labels=labels[-CONFIG['TEST']:]\nfinal_test_prelabels=prelabels[-CONFIG['TEST']:]\nback_to_label=[list(label_to_index.keys())[lab]for lab in final_test_labels]\nplt.figure()\nplt.hist(back_to_label)\nplt.show()\n\nfinal_test_labels=tf.convert_to_tensor(final_test_labels)\nfinal_test_images=tf.convert_to_tensor(final_test_images)\nfinal_test_prelabels=tf.convert_to_tensor(final_test_prelabels)\n\nprint(\"Shape des images:\", final_test_images.shape)\nprint(\"Shape des labels:\", final_test_labels.shape)\nprint(\"Shape des prelabels:\", final_test_prelabels.shape)\n\nfinal_test_dataset2=tf.data.Dataset.from_tensor_slices((final_test_images, final_test_labels))\nfinal_test_dataset1=tf.data.Dataset.from_tensor_slices((final_test_images, final_test_prelabels))","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:23:24.262668Z","iopub.execute_input":"2024-11-20T16:23:24.263285Z","iopub.status.idle":"2024-11-20T16:23:24.444552Z","shell.execute_reply.started":"2024-11-20T16:23:24.263259Z","shell.execute_reply":"2024-11-20T16:23:24.443671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels=labels[:-CONFIG['TEST']]\nimages=images[:-CONFIG['TEST']]\nprelabels=prelabels[:-CONFIG['TEST']]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:24:33.012247Z","iopub.execute_input":"2024-11-20T16:24:33.012835Z","iopub.status.idle":"2024-11-20T16:24:33.017424Z","shell.execute_reply.started":"2024-11-20T16:24:33.012804Z","shell.execute_reply":"2024-11-20T16:24:33.016374Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)\nprelabels = tf.convert_to_tensor(prelabels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:02.878684Z","iopub.execute_input":"2024-11-20T16:32:02.879347Z","iopub.status.idle":"2024-11-20T16:32:02.884351Z","shell.execute_reply.started":"2024-11-20T16:32:02.879319Z","shell.execute_reply":"2024-11-20T16:32:02.883489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:04.59455Z","iopub.execute_input":"2024-11-20T16:32:04.595355Z","iopub.status.idle":"2024-11-20T16:32:04.902643Z","shell.execute_reply.started":"2024-11-20T16:32:04.595326Z","shell.execute_reply":"2024-11-20T16:32:04.90158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset1 = tf.data.Dataset.from_tensor_slices((images, prelabels))\ndataset2 = tf.data.Dataset.from_tensor_slices((images, labels))","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:06.674846Z","iopub.execute_input":"2024-11-20T16:32:06.675868Z","iopub.status.idle":"2024-11-20T16:32:06.83131Z","shell.execute_reply.started":"2024-11-20T16:32:06.675819Z","shell.execute_reply":"2024-11-20T16:32:06.830579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"shuffle_buffer_size = len(images)\ndataset1 = dataset1.shuffle(shuffle_buffer_size)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:08.590792Z","iopub.execute_input":"2024-11-20T16:32:08.591669Z","iopub.status.idle":"2024-11-20T16:32:08.598061Z","shell.execute_reply.started":"2024-11-20T16:32:08.591609Z","shell.execute_reply":"2024-11-20T16:32:08.597157Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(images) * CONFIG['TRAIN_RATIO'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:13.818803Z","iopub.execute_input":"2024-11-20T16:32:13.819136Z","iopub.status.idle":"2024-11-20T16:32:13.823378Z","shell.execute_reply.started":"2024-11-20T16:32:13.819112Z","shell.execute_reply":"2024-11-20T16:32:13.822385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset1 = dataset1.take(train_size)\ntest_dataset1 = dataset1.skip(train_size)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:15.996768Z","iopub.execute_input":"2024-11-20T16:32:15.99749Z","iopub.status.idle":"2024-11-20T16:32:16.004234Z","shell.execute_reply.started":"2024-11-20T16:32:15.997454Z","shell.execute_reply":"2024-11-20T16:32:16.003326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset1 = train_dataset1.batch(CONFIG['BATCH_SIZE'])\ntest_dataset1 = test_dataset1.batch(CONFIG['BATCH_SIZE'])\nfinal_test_dataset1= final_test_dataset1.batch(CONFIG['BATCH_SIZE'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:17.858847Z","iopub.execute_input":"2024-11-20T16:32:17.859425Z","iopub.status.idle":"2024-11-20T16:32:17.868116Z","shell.execute_reply.started":"2024-11-20T16:32:17.859395Z","shell.execute_reply":"2024-11-20T16:32:17.867244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset1 = train_dataset1.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)\ntest_dataset1 = test_dataset1.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:19.767043Z","iopub.execute_input":"2024-11-20T16:32:19.767731Z","iopub.status.idle":"2024-11-20T16:32:19.774757Z","shell.execute_reply.started":"2024-11-20T16:32:19.767695Z","shell.execute_reply":"2024-11-20T16:32:19.774018Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    InputLayer(shape =(CONFIG['IMAGE_SIZE'] ,CONFIG['IMAGE_SIZE'],3)),\n    Conv2D(filters = 4 ,kernel_size= 3 ,padding ='valid' , activation = 'relu'),\n    BatchNormalization(),\n    MaxPooling2D(pool_size=(2,2) , strides=2),\n\n\n    Conv2D(filters = 8 ,kernel_size= 3 ,padding ='valid' , activation = 'relu'),\n    BatchNormalization(),\n    MaxPooling2D(pool_size=(2,2) , strides=2),\n\n\n    Flatten(),\n\n     \n    Dense(8 , activation = 'relu',kernel_regularizer=l2(0.01)),\n    Dropout(0.3),\n    BatchNormalization(),\n\n    Dense(4 , activation = 'linear',kernel_regularizer=l2(0.01)),\n#    Dropout(0.3),\n    BatchNormalization(),\n\n    Dense(1, activation='sigmoid')\n\n])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:26.212823Z","iopub.execute_input":"2024-11-20T16:32:26.21361Z","iopub.status.idle":"2024-11-20T16:32:26.291375Z","shell.execute_reply.started":"2024-11-20T16:32:26.213579Z","shell.execute_reply":"2024-11-20T16:32:26.290567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:32.553597Z","iopub.execute_input":"2024-11-20T16:32:32.554406Z","iopub.status.idle":"2024-11-20T16:32:32.577228Z","shell.execute_reply.started":"2024-11-20T16:32:32.554378Z","shell.execute_reply":"2024-11-20T16:32:32.576544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='Adam' , loss = 'binary_crossentropy' ,  metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:39.982657Z","iopub.execute_input":"2024-11-20T16:32:39.983294Z","iopub.status.idle":"2024-11-20T16:32:39.990643Z","shell.execute_reply.started":"2024-11-20T16:32:39.983266Z","shell.execute_reply":"2024-11-20T16:32:39.989849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(train_dataset1, validation_data=test_dataset1 , epochs=100)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:32:47.821164Z","iopub.execute_input":"2024-11-20T16:32:47.821814Z","iopub.status.idle":"2024-11-20T16:33:49.301208Z","shell.execute_reply.started":"2024-11-20T16:32:47.821784Z","shell.execute_reply":"2024-11-20T16:33:49.300132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_loss, test_accuracy = model.evaluate(test_dataset1)\nprint(f\"Test Accuracy: {test_accuracy}\")\nprint(f\"Test Loss: {test_loss}\")","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:33:52.651172Z","iopub.execute_input":"2024-11-20T16:33:52.651795Z","iopub.status.idle":"2024-11-20T16:33:52.721414Z","shell.execute_reply.started":"2024-11-20T16:33:52.651764Z","shell.execute_reply":"2024-11-20T16:33:52.720679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\n\nplt.plot(history.history['accuracy'], label='Training Accuracy')\nplt.plot(history.history['val_accuracy'], label='Validation Accuracy')\nplt.title('Model Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:33:58.120505Z","iopub.execute_input":"2024-11-20T16:33:58.121261Z","iopub.status.idle":"2024-11-20T16:33:58.36636Z","shell.execute_reply.started":"2024-11-20T16:33:58.121229Z","shell.execute_reply":"2024-11-20T16:33:58.365442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(12, 6))\nplt.plot(history.history['loss'], label='Training Loss')\nplt.plot(history.history['val_loss'], label='Validation Loss')\nplt.title('Model Loss')\nplt.xlabel('Epoch')\nplt.ylabel('Loss')\nplt.legend()\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:34:06.747664Z","iopub.execute_input":"2024-11-20T16:34:06.748039Z","iopub.status.idle":"2024-11-20T16:34:07.025109Z","shell.execute_reply.started":"2024-11-20T16:34:06.74801Z","shell.execute_reply":"2024-11-20T16:34:07.024203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_class_name2(index) :\n    return list(prelabel_to_index.keys())[index]","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:34:12.850455Z","iopub.execute_input":"2024-11-20T16:34:12.851402Z","iopub.status.idle":"2024-11-20T16:34:12.855496Z","shell.execute_reply.started":"2024-11-20T16:34:12.851367Z","shell.execute_reply":"2024-11-20T16:34:12.854548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(15, 15))\nfor i, (image, label) in enumerate(test_dataset1.take(9)):\n  ax = plt.subplot(3, 3, i + 1)\n  ax.imshow(image[0])\n  plt.title(f\"True: {get_class_name2(label.numpy()[0])}\\nPredicted: {get_class_name2(np.argmax(model.predict(image), axis=1)[0])}\")\n  plt.axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:34:14.681798Z","iopub.execute_input":"2024-11-20T16:34:14.682479Z","iopub.status.idle":"2024-11-20T16:34:16.0587Z","shell.execute_reply.started":"2024-11-20T16:34:14.68245Z","shell.execute_reply":"2024-11-20T16:34:16.057885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_labels = []\npredicted_labels = []\nim = []\nfor images, labels in final_test_dataset1:\n\n    for image, label in zip(images, labels):\n\n        \n        true_labels.append(label.numpy()) \n        im.append(image.numpy())\n\n#        predicted_label = np.round(model.predict(image)).astype(int)[0][0]\n        predicted_label = np.round(model.predict(np.expand_dims(image, axis=0))).astype(int)[0][0]\n        predicted_labels.append(predicted_label)\nprint(true_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:34:23.293559Z","iopub.execute_input":"2024-11-20T16:34:23.293919Z","iopub.status.idle":"2024-11-20T16:34:26.133445Z","shell.execute_reply.started":"2024-11-20T16:34:23.293894Z","shell.execute_reply":"2024-11-20T16:34:26.132693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_labels_name=[get_class_name2(label)for label in true_labels]\npredicted_labels_name=[get_class_name2(label)for label in predicted_labels]\n#print(true_labels_name)\n#print(predicted_labels_name)\nall_labels = sorted(set(true_labels_name) | set(predicted_labels_name))\n#print(all_labels)\ncm = confusion_matrix(true_labels_name, predicted_labels_name, labels=all_labels)","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:34:30.778228Z","iopub.execute_input":"2024-11-20T16:34:30.779082Z","iopub.status.idle":"2024-11-20T16:34:30.785031Z","shell.execute_reply.started":"2024-11-20T16:34:30.779051Z","shell.execute_reply":"2024-11-20T16:34:30.784058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, cmap='Blues', fmt='g',xticklabels=all_labels, yticklabels=all_labels)\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-11-20T16:34:35.199974Z","iopub.execute_input":"2024-11-20T16:34:35.200683Z","iopub.status.idle":"2024-11-20T16:34:35.456384Z","shell.execute_reply.started":"2024-11-20T16:34:35.200646Z","shell.execute_reply":"2024-11-20T16:34:35.455482Z"},"trusted":true},"execution_count":null,"outputs":[]}]}