{"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 CategoricalCros registered\n2024-04-03 15:41:36.442003: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registeredsentropy\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import confusion_matrix\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-03T15:41:32.154937Z","iopub.execute_input":"2024-04-03T15:41:32.155486Z","iopub.status.idle":"2024-04-03T15:41:46.742799Z","shell.execute_reply.started":"2024-04-03T15:41:32.155454Z","shell.execute_reply":"2024-04-03T15:41:46.741638Z"},"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' :100 ,\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-04-03T15:41:46.744658Z","iopub.execute_input":"2024-04-03T15:41:46.745288Z","iopub.status.idle":"2024-04-03T15:41:46.750896Z","shell.execute_reply.started":"2024-04-03T15:41:46.745259Z","shell.execute_reply":"2024-04-03T15:41:46.749801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(CONFIG['TRAIN_DF'])","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:46.752455Z","iopub.execute_input":"2024-04-03T15:41:46.753077Z","iopub.status.idle":"2024-04-03T15:41:46.793123Z","shell.execute_reply.started":"2024-04-03T15:41:46.753043Z","shell.execute_reply":"2024-04-03T15:41:46.792279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:46.795079Z","iopub.execute_input":"2024-04-03T15:41:46.795555Z","iopub.status.idle":"2024-04-03T15:41:46.810266Z","shell.execute_reply.started":"2024-04-03T15:41:46.795529Z","shell.execute_reply":"2024-04-03T15:41:46.809509Z"},"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-04-03T15:41:46.811195Z","iopub.execute_input":"2024-04-03T15:41:46.81195Z","iopub.status.idle":"2024-04-03T15:41:46.81589Z","shell.execute_reply.started":"2024-04-03T15:41:46.811924Z","shell.execute_reply":"2024-04-03T15:41:46.814902Z"},"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-04-03T15:41:46.817463Z","iopub.execute_input":"2024-04-03T15:41:46.818108Z","iopub.status.idle":"2024-04-03T15:41:46.997199Z","shell.execute_reply.started":"2024-04-03T15:41:46.818075Z","shell.execute_reply":"2024-04-03T15:41:46.996048Z"},"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-04-03T15:41:46.998611Z","iopub.execute_input":"2024-04-03T15:41:46.999585Z","iopub.status.idle":"2024-04-03T15:41:47.010742Z","shell.execute_reply.started":"2024-04-03T15:41:46.999514Z","shell.execute_reply":"2024-04-03T15:41:47.009466Z"},"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-04-03T15:41:47.01196Z","iopub.execute_input":"2024-04-03T15:41:47.012365Z","iopub.status.idle":"2024-04-03T15:41:47.019752Z","shell.execute_reply.started":"2024-04-03T15:41:47.012338Z","shell.execute_reply":"2024-04-03T15:41:47.018817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files ,labels = list_files_in_folders(CONFIG['TRAIN_PATH'])\nprint(files)\nprint(labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(files)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:47.273617Z","iopub.execute_input":"2024-04-03T15:41:47.274309Z","iopub.status.idle":"2024-04-03T15:41:47.281594Z","shell.execute_reply.started":"2024-04-03T15:41:47.27427Z","shell.execute_reply":"2024-04-03T15:41:47.2805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'path': files, 'category': labels})","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:47.283254Z","iopub.execute_input":"2024-04-03T15:41:47.283836Z","iopub.status.idle":"2024-04-03T15:41:47.292177Z","shell.execute_reply.started":"2024-04-03T15:41:47.2838Z","shell.execute_reply":"2024-04-03T15:41:47.291184Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_excel('UBC.xlsx', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:47.293408Z","iopub.execute_input":"2024-04-03T15:41:47.29428Z","iopub.status.idle":"2024-04-03T15:41:47.665922Z","shell.execute_reply.started":"2024-04-03T15:41:47.294251Z","shell.execute_reply":"2024-04-03T15:41:47.665024Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:47.667473Z","iopub.execute_input":"2024-04-03T15:41:47.668327Z","iopub.status.idle":"2024-04-03T15:41:47.67708Z","shell.execute_reply.started":"2024-04-03T15:41:47.668297Z","shell.execute_reply":"2024-04-03T15:41:47.676087Z"},"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-04-03T15:41:47.678279Z","iopub.execute_input":"2024-04-03T15:41:47.678572Z","iopub.status.idle":"2024-04-03T15:41:47.687869Z","shell.execute_reply.started":"2024-04-03T15:41:47.678546Z","shell.execute_reply":"2024-04-03T15:41:47.686923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = read_image(df)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:41:47.689391Z","iopub.execute_input":"2024-04-03T15:41:47.689765Z","iopub.status.idle":"2024-04-03T15:43:44.054163Z","shell.execute_reply.started":"2024-04-03T15:41:47.689734Z","shell.execute_reply":"2024-04-03T15:43:44.053069Z"},"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-04-03T15:43:44.055682Z","iopub.execute_input":"2024-04-03T15:43:44.056117Z","iopub.status.idle":"2024-04-03T15:43:44.062227Z","shell.execute_reply.started":"2024-04-03T15:43:44.056081Z","shell.execute_reply":"2024-04-03T15:43:44.061227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_numbers","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:44.063894Z","iopub.execute_input":"2024-04-03T15:43:44.064315Z","iopub.status.idle":"2024-04-03T15:43:44.076448Z","shell.execute_reply.started":"2024-04-03T15:43:44.064281Z","shell.execute_reply":"2024-04-03T15:43:44.075429Z"},"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-04-03T15:43:44.07764Z","iopub.execute_input":"2024-04-03T15:43:44.077971Z","iopub.status.idle":"2024-04-03T15:43:44.085766Z","shell.execute_reply.started":"2024-04-03T15:43:44.077946Z","shell.execute_reply":"2024-04-03T15:43:44.084647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = resize_normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:44.08712Z","iopub.execute_input":"2024-04-03T15:43:44.087582Z","iopub.status.idle":"2024-04-03T15:43:45.571022Z","shell.execute_reply.started":"2024-04-03T15:43:44.087548Z","shell.execute_reply":"2024-04-03T15:43:45.570078Z"},"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-04-03T15:43:45.572442Z","iopub.execute_input":"2024-04-03T15:43:45.572976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[5].shape","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index = {label: i for i, label in enumerate(set(labels))}","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = [label_to_index[label] for label in labels]","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)","metadata":{"execution":{"iopub.status.idle":"2024-04-03T15:43:48.013187Z","shell.execute_reply.started":"2024-04-03T15:43:47.005191Z","shell.execute_reply":"2024-04-03T15:43:48.012314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices((images, labels))","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:48.01462Z","iopub.execute_input":"2024-04-03T15:43:48.01522Z","iopub.status.idle":"2024-04-03T15:43:48.023796Z","shell.execute_reply.started":"2024-04-03T15:43:48.015184Z","shell.execute_reply":"2024-04-03T15:43:48.022809Z"},"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-04-03T15:43:48.029355Z","iopub.execute_input":"2024-04-03T15:43:48.029757Z","iopub.status.idle":"2024-04-03T15:43:48.039801Z","shell.execute_reply.started":"2024-04-03T15:43:48.029706Z","shell.execute_reply":"2024-04-03T15:43:48.038922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(images) * CONFIG['TRAIN_RATIO'])","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:48.041Z","iopub.execute_input":"2024-04-03T15:43:48.041923Z","iopub.status.idle":"2024-04-03T15:43:48.047975Z","shell.execute_reply.started":"2024-04-03T15:43:48.041887Z","shell.execute_reply":"2024-04-03T15:43:48.046675Z"},"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-04-03T15:43:48.04909Z","iopub.execute_input":"2024-04-03T15:43:48.049443Z","iopub.status.idle":"2024-04-03T15:43:48.062419Z","shell.execute_reply.started":"2024-04-03T15:43:48.049419Z","shell.execute_reply":"2024-04-03T15:43:48.06152Z"},"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'])","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:48.063544Z","iopub.execute_input":"2024-04-03T15:43:48.064097Z","iopub.status.idle":"2024-04-03T15:43:48.074571Z","shell.execute_reply.started":"2024-04-03T15:43:48.064069Z","shell.execute_reply":"2024-04-03T15:43:48.073486Z"},"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-04-03T15:43:48.077298Z","iopub.execute_input":"2024-04-03T15:43:48.077589Z","iopub.status.idle":"2024-04-03T15:43:48.08501Z","shell.execute_reply.started":"2024-04-03T15:43:48.077564Z","shell.execute_reply":"2024-04-03T15:43:48.083979Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = 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'),\n    BatchNormalization(),\n\n    Dense(2 , activation = 'relu'),\n    BatchNormalization(),\n\n    Dense(CONFIG['N_CLASSES'], activation='softmax')\n\n])","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:48.0863Z","iopub.execute_input":"2024-04-03T15:43:48.087161Z","iopub.status.idle":"2024-04-03T15:43:48.197624Z","shell.execute_reply.started":"2024-04-03T15:43:48.087127Z","shell.execute_reply":"2024-04-03T15:43:48.196601Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-03T15:43:48.199275Z","iopub.execute_input":"2024-04-03T15:43:48.19957Z","iopub.status.idle":"2024-04-03T15:43:48.226243Z","shell.execute_reply.started":"2024-04-03T15:43:48.199545Z","shell.execute_reply":"2024-04-03T15:43:48.225173Z"},"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-04-03T15:43:48.227485Z","iopub.execute_input":"2024-04-03T15:43:48.227814Z","iopub.status.idle":"2024-04-03T15:43:48.24133Z","shell.execute_reply.started":"2024-04-03T15:43:48.22778Z","shell.execute_reply":"2024-04-03T15:43:48.239557Z"},"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-04-03T15:43:48.242556Z","iopub.execute_input":"2024-04-03T15:43:48.242888Z","iopub.status.idle":"2024-04-03T16:13:16.493856Z","shell.execute_reply.started":"2024-04-03T15:43:48.242862Z","shell.execute_reply":"2024-04-03T16:13:16.492181Z"},"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-04-03T16:13:16.496226Z","iopub.execute_input":"2024-04-03T16:13:16.496565Z","iopub.status.idle":"2024-04-03T16:13:16.823124Z","shell.execute_reply.started":"2024-04-03T16:13:16.496539Z","shell.execute_reply":"2024-04-03T16:13:16.822136Z"},"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-04-03T16:13:16.824417Z","iopub.execute_input":"2024-04-03T16:13:16.826015Z","iopub.status.idle":"2024-04-03T16:13:17.204609Z","shell.execute_reply.started":"2024-04-03T16:13:16.825976Z","shell.execute_reply":"2024-04-03T16:13:17.202714Z"},"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-04-03T16:13:17.206962Z","iopub.execute_input":"2024-04-03T16:13:17.207431Z","iopub.status.idle":"2024-04-03T16:13:17.526632Z","shell.execute_reply.started":"2024-04-03T16:13:17.207391Z","shell.execute_reply":"2024-04-03T16:13:17.525554Z"},"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-04-03T16:13:17.528044Z","iopub.execute_input":"2024-04-03T16:13:17.528384Z","iopub.status.idle":"2024-04-03T16:13:17.532659Z","shell.execute_reply.started":"2024-04-03T16:13:17.528357Z","shell.execute_reply":"2024-04-03T16:13:17.531978Z"},"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-04-03T16:13:17.533622Z","iopub.execute_input":"2024-04-03T16:13:17.534586Z"},"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.execute_input":"2024-04-03T16:13:19.56521Z","iopub.status.idle":"2024-04-03T16:13:21.16443Z","shell.execute_reply.started":"2024-04-03T16:13:19.565172Z","shell.execute_reply":"2024-04-03T16:13:21.16092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"true_labels = []\npredicted_labels = []\nim = []\nfor images, labels in test_dataset:\n    for image, label in zip(images, labels):\n        true_labels.append(label.numpy())\n        im.append(image.numpy())\n        plt.show()\n        predicted_label = np.argmax(model.predict(np.expand_dims(image, axis=0)), axis=1)[0]\n        predicted_labels.append(predicted_label)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T16:13:21.166161Z","iopub.execute_input":"2024-04-03T16:13:21.166887Z","iopub.status.idle":"2024-04-03T16:13:26.278168Z","shell.execute_reply.started":"2024-04-03T16:13:21.16685Z","shell.execute_reply":"2024-04-03T16:13:26.277011Z"},"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-04-03T16:13:26.280274Z","iopub.execute_input":"2024-04-03T16:13:26.280705Z","iopub.status.idle":"2024-04-03T16:13:27.967448Z","shell.execute_reply.started":"2024-04-03T16:13:26.280668Z","shell.execute_reply":"2024-04-03T16:13:27.966314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(true_labels, predicted_labels)","metadata":{"execution":{"iopub.status.busy":"2024-04-03T16:13:27.968965Z","iopub.execute_input":"2024-04-03T16:13:27.969259Z","iopub.status.idle":"2024-04-03T16:13:27.978246Z","shell.execute_reply.started":"2024-04-03T16:13:27.969233Z","shell.execute_reply":"2024-04-03T16:13:27.977114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(8, 6))\nsns.heatmap(cm, annot=True, cmap='Blues', fmt='g')\nplt.xlabel('Predicted Label')\nplt.ylabel('True Label')\nplt.title('Confusion Matrix')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-03T16:13:27.979635Z","iopub.execute_input":"2024-04-03T16:13:27.97999Z","iopub.status.idle":"2024-04-03T16:13:28.328785Z","shell.execute_reply.started":"2024-04-03T16:13:27.979963Z","shell.execute_reply":"2024-04-03T16:13:28.327695Z"},"trusted":true},"execution_count":null,"outputs":[]}]}