{"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":"gpu","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30674,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-05T22:32:17.742278Z","iopub.execute_input":"2024-06-05T22:32:17.742633Z","iopub.status.idle":"2024-06-05T22:32:31.457216Z","shell.execute_reply.started":"2024-06-05T22:32:17.742605Z","shell.execute_reply":"2024-06-05T22:32:31.456431Z"},"editable":false,"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-06-05T22:32:31.458817Z","iopub.execute_input":"2024-06-05T22:32:31.459673Z","iopub.status.idle":"2024-06-05T22:32:31.469234Z","shell.execute_reply.started":"2024-06-05T22:32:31.459646Z","shell.execute_reply":"2024-06-05T22:32:31.468106Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(CONFIG['TRAIN_DF'])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:31.470623Z","iopub.execute_input":"2024-06-05T22:32:31.471102Z","iopub.status.idle":"2024-06-05T22:32:31.507592Z","shell.execute_reply.started":"2024-06-05T22:32:31.471072Z","shell.execute_reply":"2024-06-05T22:32:31.5069Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:31.509435Z","iopub.execute_input":"2024-06-05T22:32:31.509688Z","iopub.status.idle":"2024-06-05T22:32:31.534167Z","shell.execute_reply.started":"2024-06-05T22:32:31.509666Z","shell.execute_reply":"2024-06-05T22:32:31.533361Z"},"editable":false,"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-06-05T22:32:31.535161Z","iopub.execute_input":"2024-06-05T22:32:31.535427Z","iopub.status.idle":"2024-06-05T22:32:31.539682Z","shell.execute_reply.started":"2024-06-05T22:32:31.535405Z","shell.execute_reply":"2024-06-05T22:32:31.53867Z"},"editable":false,"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-06-05T22:32:31.540834Z","iopub.execute_input":"2024-06-05T22:32:31.541151Z","iopub.status.idle":"2024-06-05T22:32:31.682183Z","shell.execute_reply.started":"2024-06-05T22:32:31.541128Z","shell.execute_reply":"2024-06-05T22:32:31.681331Z"},"editable":false,"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-06-05T22:32:31.683279Z","iopub.execute_input":"2024-06-05T22:32:31.683596Z","iopub.status.idle":"2024-06-05T22:32:31.695438Z","shell.execute_reply.started":"2024-06-05T22:32:31.683566Z","shell.execute_reply":"2024-06-05T22:32:31.69446Z"},"editable":false,"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-06-05T22:32:31.696498Z","iopub.execute_input":"2024-06-05T22:32:31.696798Z","iopub.status.idle":"2024-06-05T22:32:31.703395Z","shell.execute_reply.started":"2024-06-05T22:32:31.696767Z","shell.execute_reply":"2024-06-05T22:32:31.702607Z"},"editable":false,"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-06-05T22:32:31.704417Z","iopub.execute_input":"2024-06-05T22:32:31.704686Z","iopub.status.idle":"2024-06-05T22:32:31.913234Z","shell.execute_reply.started":"2024-06-05T22:32:31.704665Z","shell.execute_reply":"2024-06-05T22:32:31.912379Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(files)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:31.917296Z","iopub.execute_input":"2024-06-05T22:32:31.918263Z","iopub.status.idle":"2024-06-05T22:32:31.923282Z","shell.execute_reply.started":"2024-06-05T22:32:31.918237Z","shell.execute_reply":"2024-06-05T22:32:31.922435Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'path': files, 'category': labels})","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:31.924301Z","iopub.execute_input":"2024-06-05T22:32:31.924558Z","iopub.status.idle":"2024-06-05T22:32:31.933254Z","shell.execute_reply.started":"2024-06-05T22:32:31.924536Z","shell.execute_reply":"2024-06-05T22:32:31.932438Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_excel('UBC.xlsx', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:31.934304Z","iopub.execute_input":"2024-06-05T22:32:31.934607Z","iopub.status.idle":"2024-06-05T22:32:32.279657Z","shell.execute_reply.started":"2024-06-05T22:32:31.934584Z","shell.execute_reply":"2024-06-05T22:32:32.278932Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:32.280788Z","iopub.execute_input":"2024-06-05T22:32:32.281372Z","iopub.status.idle":"2024-06-05T22:32:32.291071Z","shell.execute_reply.started":"2024-06-05T22:32:32.281339Z","shell.execute_reply":"2024-06-05T22:32:32.290227Z"},"editable":false,"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-06-05T22:32:32.292254Z","iopub.execute_input":"2024-06-05T22:32:32.292614Z","iopub.status.idle":"2024-06-05T22:32:32.300428Z","shell.execute_reply.started":"2024-06-05T22:32:32.292589Z","shell.execute_reply":"2024-06-05T22:32:32.299714Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = read_image(df)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:32:32.301467Z","iopub.execute_input":"2024-06-05T22:32:32.301787Z","iopub.status.idle":"2024-06-05T22:34:20.854703Z","shell.execute_reply.started":"2024-06-05T22:32:32.301756Z","shell.execute_reply":"2024-06-05T22:34:20.853559Z"},"editable":false,"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-06-05T22:34:20.855891Z","iopub.execute_input":"2024-06-05T22:34:20.856198Z","iopub.status.idle":"2024-06-05T22:34:20.861238Z","shell.execute_reply.started":"2024-06-05T22:34:20.856174Z","shell.execute_reply":"2024-06-05T22:34:20.860315Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_numbers","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:20.86228Z","iopub.execute_input":"2024-06-05T22:34:20.862512Z","iopub.status.idle":"2024-06-05T22:34:20.877661Z","shell.execute_reply.started":"2024-06-05T22:34:20.862492Z","shell.execute_reply":"2024-06-05T22:34:20.876915Z"},"editable":false,"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-06-05T22:34:20.878685Z","iopub.execute_input":"2024-06-05T22:34:20.878967Z","iopub.status.idle":"2024-06-05T22:34:20.885797Z","shell.execute_reply.started":"2024-06-05T22:34:20.87892Z","shell.execute_reply":"2024-06-05T22:34:20.88498Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = resize_normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:20.886862Z","iopub.execute_input":"2024-06-05T22:34:20.8872Z","iopub.status.idle":"2024-06-05T22:34:22.162693Z","shell.execute_reply.started":"2024-06-05T22:34:20.88717Z","shell.execute_reply":"2024-06-05T22:34:22.16163Z"},"editable":false,"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-06-05T22:34:22.164472Z","iopub.execute_input":"2024-06-05T22:34:22.164873Z","iopub.status.idle":"2024-06-05T22:34:23.553375Z","shell.execute_reply.started":"2024-06-05T22:34:22.164836Z","shell.execute_reply":"2024-06-05T22:34:23.55249Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[5].shape","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:23.554386Z","iopub.execute_input":"2024-06-05T22:34:23.554709Z","iopub.status.idle":"2024-06-05T22:34:23.560839Z","shell.execute_reply.started":"2024-06-05T22:34:23.55468Z","shell.execute_reply":"2024-06-05T22:34:23.559976Z"},"editable":false,"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-06-05T22:34:23.561769Z","iopub.execute_input":"2024-06-05T22:34:23.562123Z","iopub.status.idle":"2024-06-05T22:34:23.5698Z","shell.execute_reply.started":"2024-06-05T22:34:23.562093Z","shell.execute_reply":"2024-06-05T22:34:23.568939Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:23.570701Z","iopub.execute_input":"2024-06-05T22:34:23.571098Z","iopub.status.idle":"2024-06-05T22:34:23.581759Z","shell.execute_reply.started":"2024-06-05T22:34:23.57107Z","shell.execute_reply":"2024-06-05T22:34:23.58092Z"},"editable":false,"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-06-05T22:34:23.582902Z","iopub.execute_input":"2024-06-05T22:34:23.583724Z","iopub.status.idle":"2024-06-05T22:34:23.591811Z","shell.execute_reply.started":"2024-06-05T22:34:23.583694Z","shell.execute_reply":"2024-06-05T22:34:23.590915Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:23.592846Z","iopub.execute_input":"2024-06-05T22:34:23.593191Z","iopub.status.idle":"2024-06-05T22:34:24.225301Z","shell.execute_reply.started":"2024-06-05T22:34:23.593164Z","shell.execute_reply":"2024-06-05T22:34:24.224142Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:24.226896Z","iopub.execute_input":"2024-06-05T22:34:24.227365Z","iopub.status.idle":"2024-06-05T22:34:25.37264Z","shell.execute_reply.started":"2024-06-05T22:34:24.22734Z","shell.execute_reply":"2024-06-05T22:34:25.371866Z"},"editable":false,"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-06-05T22:34:25.373721Z","iopub.execute_input":"2024-06-05T22:34:25.374007Z","iopub.status.idle":"2024-06-05T22:34:26.013424Z","shell.execute_reply.started":"2024-06-05T22:34:25.373983Z","shell.execute_reply":"2024-06-05T22:34:26.012607Z"},"editable":false,"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-06-05T22:34:26.020752Z","iopub.execute_input":"2024-06-05T22:34:26.021035Z","iopub.status.idle":"2024-06-05T22:34:26.030445Z","shell.execute_reply.started":"2024-06-05T22:34:26.021011Z","shell.execute_reply":"2024-06-05T22:34:26.029667Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(images) * CONFIG['TRAIN_RATIO'])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:26.031439Z","iopub.execute_input":"2024-06-05T22:34:26.03177Z","iopub.status.idle":"2024-06-05T22:34:26.037858Z","shell.execute_reply.started":"2024-06-05T22:34:26.031746Z","shell.execute_reply":"2024-06-05T22:34:26.036973Z"},"editable":false,"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-06-05T22:34:26.039262Z","iopub.execute_input":"2024-06-05T22:34:26.039527Z","iopub.status.idle":"2024-06-05T22:34:26.052946Z","shell.execute_reply.started":"2024-06-05T22:34:26.039505Z","shell.execute_reply":"2024-06-05T22:34:26.052093Z"},"editable":false,"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-06-05T22:34:26.05401Z","iopub.execute_input":"2024-06-05T22:34:26.05428Z","iopub.status.idle":"2024-06-05T22:34:26.063773Z","shell.execute_reply.started":"2024-06-05T22:34:26.054258Z","shell.execute_reply":"2024-06-05T22:34:26.062874Z"},"editable":false,"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-06-05T22:34:26.065035Z","iopub.execute_input":"2024-06-05T22:34:26.065606Z","iopub.status.idle":"2024-06-05T22:34:26.07329Z","shell.execute_reply.started":"2024-06-05T22:34:26.065576Z","shell.execute_reply":"2024-06-05T22:34:26.072503Z"},"editable":false,"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-06-05T22:34:26.074383Z","iopub.execute_input":"2024-06-05T22:34:26.074695Z","iopub.status.idle":"2024-06-05T22:34:26.408002Z","shell.execute_reply.started":"2024-06-05T22:34:26.074665Z","shell.execute_reply":"2024-06-05T22:34:26.407044Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:34:26.409103Z","iopub.execute_input":"2024-06-05T22:34:26.40938Z","iopub.status.idle":"2024-06-05T22:34:26.435604Z","shell.execute_reply.started":"2024-06-05T22:34:26.409356Z","shell.execute_reply":"2024-06-05T22:34:26.434753Z"},"editable":false,"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-06-05T22:34:26.436824Z","iopub.execute_input":"2024-06-05T22:34:26.437166Z","iopub.status.idle":"2024-06-05T22:34:26.449633Z","shell.execute_reply.started":"2024-06-05T22:34:26.437136Z","shell.execute_reply":"2024-06-05T22:34:26.448692Z"},"editable":false,"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-06-05T22:34:26.450693Z","iopub.execute_input":"2024-06-05T22:34:26.450967Z","iopub.status.idle":"2024-06-05T22:35:29.462539Z","shell.execute_reply.started":"2024-06-05T22:34:26.450937Z","shell.execute_reply":"2024-06-05T22:35:29.461723Z"},"editable":false,"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-06-05T22:35:29.463748Z","iopub.execute_input":"2024-06-05T22:35:29.46409Z","iopub.status.idle":"2024-06-05T22:35:29.525574Z","shell.execute_reply.started":"2024-06-05T22:35:29.464059Z","shell.execute_reply":"2024-06-05T22:35:29.524758Z"},"editable":false,"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-06-05T22:35:29.52689Z","iopub.execute_input":"2024-06-05T22:35:29.527532Z","iopub.status.idle":"2024-06-05T22:35:29.883631Z","shell.execute_reply.started":"2024-06-05T22:35:29.527495Z","shell.execute_reply":"2024-06-05T22:35:29.882767Z"},"editable":false,"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-06-05T22:35:29.884938Z","iopub.execute_input":"2024-06-05T22:35:29.885217Z","iopub.status.idle":"2024-06-05T22:35:30.201218Z","shell.execute_reply.started":"2024-06-05T22:35:29.885192Z","shell.execute_reply":"2024-06-05T22:35:30.200439Z"},"editable":false,"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-06-05T22:35:30.202202Z","iopub.execute_input":"2024-06-05T22:35:30.202451Z","iopub.status.idle":"2024-06-05T22:35:30.206687Z","shell.execute_reply.started":"2024-06-05T22:35:30.202429Z","shell.execute_reply":"2024-06-05T22:35:30.205805Z"},"editable":false,"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-06-05T22:35:30.207945Z","iopub.execute_input":"2024-06-05T22:35:30.208278Z","iopub.status.idle":"2024-06-05T22:35:32.229062Z","shell.execute_reply.started":"2024-06-05T22:35:30.208248Z","shell.execute_reply":"2024-06-05T22:35:32.228139Z"},"editable":false,"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-06-05T22:35:32.230215Z","iopub.execute_input":"2024-06-05T22:35:32.230494Z","iopub.status.idle":"2024-06-05T22:35:33.398006Z","shell.execute_reply.started":"2024-06-05T22:35:32.230469Z","shell.execute_reply":"2024-06-05T22:35:33.397185Z"},"editable":false,"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-06-05T22:35:33.399232Z","iopub.execute_input":"2024-06-05T22:35:33.399557Z","iopub.status.idle":"2024-06-05T22:35:36.987949Z","shell.execute_reply.started":"2024-06-05T22:35:33.399528Z","shell.execute_reply":"2024-06-05T22:35:36.987125Z"},"editable":false,"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-06-05T22:35:36.989262Z","iopub.execute_input":"2024-06-05T22:35:36.989566Z","iopub.status.idle":"2024-06-05T22:35:38.521188Z","shell.execute_reply.started":"2024-06-05T22:35:36.98954Z","shell.execute_reply":"2024-06-05T22:35:38.520304Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(true_labels, predicted_labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.522358Z","iopub.execute_input":"2024-06-05T22:35:38.522676Z","iopub.status.idle":"2024-06-05T22:35:38.528899Z","shell.execute_reply.started":"2024-06-05T22:35:38.522648Z","shell.execute_reply":"2024-06-05T22:35:38.527983Z"},"editable":false,"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-06-05T22:35:38.530309Z","iopub.execute_input":"2024-06-05T22:35:38.530719Z","iopub.status.idle":"2024-06-05T22:35:38.821981Z","shell.execute_reply.started":"2024-06-05T22:35:38.530691Z","shell.execute_reply":"2024-06-05T22:35:38.821086Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.read_csv('/kaggle/input/UBC-OCEAN/sample_submission.csv')\ntest_df = pd.read_csv('/kaggle/input/UBC-OCEAN/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.823338Z","iopub.execute_input":"2024-06-05T22:35:38.823972Z","iopub.status.idle":"2024-06-05T22:35:38.841964Z","shell.execute_reply.started":"2024-06-05T22:35:38.823917Z","shell.execute_reply":"2024-06-05T22:35:38.841241Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.842902Z","iopub.execute_input":"2024-06-05T22:35:38.843184Z","iopub.status.idle":"2024-06-05T22:35:38.850917Z","shell.execute_reply.started":"2024-06-05T22:35:38.843161Z","shell.execute_reply":"2024-06-05T22:35:38.849959Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.852139Z","iopub.execute_input":"2024-06-05T22:35:38.852441Z","iopub.status.idle":"2024-06-05T22:35:38.864326Z","shell.execute_reply.started":"2024-06-05T22:35:38.852418Z","shell.execute_reply":"2024-06-05T22:35:38.863318Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def list_files_in_folders_in_test(folder_path):\n    files = []\n    labels = []\n    images_ids = []\n    for root, dirs, filenames in os.walk(folder_path):\n        for filename in filenames:\n            images_ids.append(int(filename.split('_')[0])) \n            image_id = submission_df[submission_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 ,images_ids","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.865305Z","iopub.execute_input":"2024-06-05T22:35:38.86555Z","iopub.status.idle":"2024-06-05T22:35:38.872882Z","shell.execute_reply.started":"2024-06-05T22:35:38.865528Z","shell.execute_reply":"2024-06-05T22:35:38.872122Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"files ,labels , ids = list_files_in_folders_in_test(CONFIG['TEST_PATH'])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.87401Z","iopub.execute_input":"2024-06-05T22:35:38.874471Z","iopub.status.idle":"2024-06-05T22:35:38.887134Z","shell.execute_reply.started":"2024-06-05T22:35:38.874435Z","shell.execute_reply":"2024-06-05T22:35:38.886254Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_cat = pd.DataFrame({'path': files, 'category': labels})","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.888209Z","iopub.execute_input":"2024-06-05T22:35:38.888529Z","iopub.status.idle":"2024-06-05T22:35:38.894486Z","shell.execute_reply.started":"2024-06-05T22:35:38.888499Z","shell.execute_reply":"2024-06-05T22:35:38.893519Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = read_image(test_df_cat)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:38.895492Z","iopub.execute_input":"2024-06-05T22:35:38.895871Z","iopub.status.idle":"2024-06-05T22:35:39.077867Z","shell.execute_reply.started":"2024-06-05T22:35:38.895847Z","shell.execute_reply":"2024-06-05T22:35:39.076768Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_cat","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.079619Z","iopub.execute_input":"2024-06-05T22:35:39.079956Z","iopub.status.idle":"2024-06-05T22:35:39.088705Z","shell.execute_reply.started":"2024-06-05T22:35:39.079905Z","shell.execute_reply":"2024-06-05T22:35:39.087795Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = resize_normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.090067Z","iopub.execute_input":"2024-06-05T22:35:39.090659Z","iopub.status.idle":"2024-06-05T22:35:39.099038Z","shell.execute_reply.started":"2024-06-05T22:35:39.090625Z","shell.execute_reply":"2024-06-05T22:35:39.098249Z"},"editable":false,"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-06-05T22:35:39.100442Z","iopub.execute_input":"2024-06-05T22:35:39.100774Z","iopub.status.idle":"2024-06-05T22:35:39.106967Z","shell.execute_reply.started":"2024-06-05T22:35:39.100744Z","shell.execute_reply":"2024-06-05T22:35:39.105978Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.108127Z","iopub.execute_input":"2024-06-05T22:35:39.108455Z","iopub.status.idle":"2024-06-05T22:35:39.117774Z","shell.execute_reply.started":"2024-06-05T22:35:39.108426Z","shell.execute_reply":"2024-06-05T22:35:39.116914Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.118873Z","iopub.execute_input":"2024-06-05T22:35:39.119141Z","iopub.status.idle":"2024-06-05T22:35:39.127104Z","shell.execute_reply.started":"2024-06-05T22:35:39.119119Z","shell.execute_reply":"2024-06-05T22:35:39.126331Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = tf.data.Dataset.from_tensor_slices((images, labels))","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.128072Z","iopub.execute_input":"2024-06-05T22:35:39.128297Z","iopub.status.idle":"2024-06-05T22:35:39.14041Z","shell.execute_reply.started":"2024-06-05T22:35:39.128277Z","shell.execute_reply":"2024-06-05T22:35:39.139498Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.batch(CONFIG['BATCH_SIZE'])","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.141551Z","iopub.execute_input":"2024-06-05T22:35:39.14212Z","iopub.status.idle":"2024-06-05T22:35:39.148917Z","shell.execute_reply.started":"2024-06-05T22:35:39.142089Z","shell.execute_reply":"2024-06-05T22:35:39.148172Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.149875Z","iopub.execute_input":"2024-06-05T22:35:39.150145Z","iopub.status.idle":"2024-06-05T22:35:39.158261Z","shell.execute_reply.started":"2024-06-05T22:35:39.150124Z","shell.execute_reply":"2024-06-05T22:35:39.157554Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.159318Z","iopub.execute_input":"2024-06-05T22:35:39.159595Z","iopub.status.idle":"2024-06-05T22:35:39.168291Z","shell.execute_reply.started":"2024-06-05T22:35:39.159573Z","shell.execute_reply":"2024-06-05T22:35:39.167436Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 10))\n\nfor i, (image, label) in enumerate(test_data.take(1)):\n    ax = plt.subplot(1, 1, i + 1)\n    ax.imshow(image[0].numpy())  \n    true_label = get_class_name(label.numpy()[0]) \n    predicted_label = get_class_name(np.argmax(model.predict(image), axis=1)[0])\n    plt.title(f\"True: {true_label}\\nPredicted: {predicted_label}\", fontsize=14)\n    plt.axis('off')\n\nplt.tight_layout()\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.169333Z","iopub.execute_input":"2024-06-05T22:35:39.169583Z","iopub.status.idle":"2024-06-05T22:35:39.551255Z","shell.execute_reply.started":"2024-06-05T22:35:39.169562Z","shell.execute_reply":"2024-06-05T22:35:39.550361Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df = pd.DataFrame({'image_id': ids, 'label': predicted_label})","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.552471Z","iopub.execute_input":"2024-06-05T22:35:39.552832Z","iopub.status.idle":"2024-06-05T22:35:39.558183Z","shell.execute_reply.started":"2024-06-05T22:35:39.5528Z","shell.execute_reply":"2024-06-05T22:35:39.557239Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.559249Z","iopub.execute_input":"2024-06-05T22:35:39.559536Z","iopub.status.idle":"2024-06-05T22:35:39.572312Z","shell.execute_reply.started":"2024-06-05T22:35:39.559513Z","shell.execute_reply":"2024-06-05T22:35:39.571537Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.573769Z","iopub.execute_input":"2024-06-05T22:35:39.57412Z","iopub.status.idle":"2024-06-05T22:35:39.581853Z","shell.execute_reply.started":"2024-06-05T22:35:39.574089Z","shell.execute_reply":"2024-06-05T22:35:39.581145Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-06-05T22:35:39.58288Z","iopub.execute_input":"2024-06-05T22:35:39.583157Z","iopub.status.idle":"2024-06-05T22:35:39.593468Z","shell.execute_reply.started":"2024-06-05T22:35:39.583135Z","shell.execute_reply":"2024-06-05T22:35:39.592566Z"},"editable":false,"trusted":true},"execution_count":null,"outputs":[]}]}