{"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-03-29T15:25:44.94483Z","iopub.execute_input":"2024-03-29T15:25:44.945689Z","iopub.status.idle":"2024-03-29T15:25:59.401102Z","shell.execute_reply.started":"2024-03-29T15:25:44.945654Z","shell.execute_reply":"2024-03-29T15:25:59.400066Z"},"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-03-29T15:25:59.402913Z","iopub.execute_input":"2024-03-29T15:25:59.403433Z","iopub.status.idle":"2024-03-29T15:25:59.408737Z","shell.execute_reply.started":"2024-03-29T15:25:59.403405Z","shell.execute_reply":"2024-03-29T15:25:59.407611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(CONFIG['TRAIN_DF'])","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:25:59.40982Z","iopub.execute_input":"2024-03-29T15:25:59.410076Z","iopub.status.idle":"2024-03-29T15:25:59.456197Z","shell.execute_reply.started":"2024-03-29T15:25:59.410053Z","shell.execute_reply":"2024-03-29T15:25:59.455414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:25:59.458261Z","iopub.execute_input":"2024-03-29T15:25:59.458884Z","iopub.status.idle":"2024-03-29T15:25:59.475391Z","shell.execute_reply.started":"2024-03-29T15:25:59.458856Z","shell.execute_reply":"2024-03-29T15:25:59.474416Z"},"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-03-29T15:25:59.476516Z","iopub.execute_input":"2024-03-29T15:25:59.476807Z","iopub.status.idle":"2024-03-29T15:25:59.481122Z","shell.execute_reply.started":"2024-03-29T15:25:59.476782Z","shell.execute_reply":"2024-03-29T15:25:59.480279Z"},"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-03-29T15:25:59.482212Z","iopub.execute_input":"2024-03-29T15:25:59.48254Z","iopub.status.idle":"2024-03-29T15:25:59.618087Z","shell.execute_reply.started":"2024-03-29T15:25:59.482511Z","shell.execute_reply":"2024-03-29T15:25:59.617071Z"},"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-03-29T15:25:59.619232Z","iopub.execute_input":"2024-03-29T15:25:59.619524Z","iopub.status.idle":"2024-03-29T15:25:59.630347Z","shell.execute_reply.started":"2024-03-29T15:25:59.6195Z","shell.execute_reply":"2024-03-29T15:25:59.629322Z"},"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-03-29T15:25:59.631684Z","iopub.execute_input":"2024-03-29T15:25:59.631971Z","iopub.status.idle":"2024-03-29T15:25:59.641658Z","shell.execute_reply.started":"2024-03-29T15:25:59.631948Z","shell.execute_reply":"2024-03-29T15:25:59.640688Z"},"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-03-29T15:25:59.642643Z","iopub.execute_input":"2024-03-29T15:25:59.642906Z","iopub.status.idle":"2024-03-29T15:25:59.877412Z","shell.execute_reply.started":"2024-03-29T15:25:59.642884Z","shell.execute_reply":"2024-03-29T15:25:59.876515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(files)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:25:59.881727Z","iopub.execute_input":"2024-03-29T15:25:59.882037Z","iopub.status.idle":"2024-03-29T15:25:59.887906Z","shell.execute_reply.started":"2024-03-29T15:25:59.882011Z","shell.execute_reply":"2024-03-29T15:25:59.88695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({'path': files, 'category': labels})","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:25:59.889342Z","iopub.execute_input":"2024-03-29T15:25:59.889939Z","iopub.status.idle":"2024-03-29T15:25:59.897498Z","shell.execute_reply.started":"2024-03-29T15:25:59.889913Z","shell.execute_reply":"2024-03-29T15:25:59.896604Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.to_excel('UBC.xlsx', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:25:59.898622Z","iopub.execute_input":"2024-03-29T15:25:59.898906Z","iopub.status.idle":"2024-03-29T15:26:00.282672Z","shell.execute_reply.started":"2024-03-29T15:25:59.898883Z","shell.execute_reply":"2024-03-29T15:26:00.281851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:26:00.284301Z","iopub.execute_input":"2024-03-29T15:26:00.284954Z","iopub.status.idle":"2024-03-29T15:26:00.294103Z","shell.execute_reply.started":"2024-03-29T15:26:00.284926Z","shell.execute_reply":"2024-03-29T15:26:00.293222Z"},"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-03-29T15:26:00.295067Z","iopub.execute_input":"2024-03-29T15:26:00.295319Z","iopub.status.idle":"2024-03-29T15:26:00.304807Z","shell.execute_reply.started":"2024-03-29T15:26:00.295298Z","shell.execute_reply":"2024-03-29T15:26:00.303925Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = read_image(df)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:26:00.305845Z","iopub.execute_input":"2024-03-29T15:26:00.306101Z","iopub.status.idle":"2024-03-29T15:27:42.0659Z","shell.execute_reply.started":"2024-03-29T15:26:00.306079Z","shell.execute_reply":"2024-03-29T15:27:42.064766Z"},"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-03-29T15:27:42.067087Z","iopub.execute_input":"2024-03-29T15:27:42.067387Z","iopub.status.idle":"2024-03-29T15:27:42.072367Z","shell.execute_reply.started":"2024-03-29T15:27:42.06734Z","shell.execute_reply":"2024-03-29T15:27:42.071449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_numbers","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:42.073639Z","iopub.execute_input":"2024-03-29T15:27:42.073965Z","iopub.status.idle":"2024-03-29T15:27:42.087165Z","shell.execute_reply.started":"2024-03-29T15:27:42.073933Z","shell.execute_reply":"2024-03-29T15:27:42.086285Z"},"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-03-29T15:27:42.088177Z","iopub.execute_input":"2024-03-29T15:27:42.08848Z","iopub.status.idle":"2024-03-29T15:27:42.098469Z","shell.execute_reply.started":"2024-03-29T15:27:42.088454Z","shell.execute_reply":"2024-03-29T15:27:42.097647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = resize_normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:42.099542Z","iopub.execute_input":"2024-03-29T15:27:42.099804Z","iopub.status.idle":"2024-03-29T15:27:43.394563Z","shell.execute_reply.started":"2024-03-29T15:27:42.099782Z","shell.execute_reply":"2024-03-29T15:27:43.393501Z"},"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-03-29T15:27:43.395843Z","iopub.execute_input":"2024-03-29T15:27:43.396192Z","iopub.status.idle":"2024-03-29T15:27:44.783999Z","shell.execute_reply.started":"2024-03-29T15:27:43.396162Z","shell.execute_reply":"2024-03-29T15:27:44.783079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images[5].shape","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:44.785013Z","iopub.execute_input":"2024-03-29T15:27:44.785274Z","iopub.status.idle":"2024-03-29T15:27:44.791346Z","shell.execute_reply.started":"2024-03-29T15:27:44.785252Z","shell.execute_reply":"2024-03-29T15:27:44.790349Z"},"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-03-29T15:27:44.792402Z","iopub.execute_input":"2024-03-29T15:27:44.792696Z","iopub.status.idle":"2024-03-29T15:27:44.802239Z","shell.execute_reply.started":"2024-03-29T15:27:44.792669Z","shell.execute_reply":"2024-03-29T15:27:44.801325Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:44.803578Z","iopub.execute_input":"2024-03-29T15:27:44.803909Z","iopub.status.idle":"2024-03-29T15:27:44.815672Z","shell.execute_reply.started":"2024-03-29T15:27:44.803879Z","shell.execute_reply":"2024-03-29T15:27:44.814764Z"},"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-03-29T15:27:44.816935Z","iopub.execute_input":"2024-03-29T15:27:44.817195Z","iopub.status.idle":"2024-03-29T15:27:44.82651Z","shell.execute_reply.started":"2024-03-29T15:27:44.817172Z","shell.execute_reply":"2024-03-29T15:27:44.825598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:44.827539Z","iopub.execute_input":"2024-03-29T15:27:44.827792Z","iopub.status.idle":"2024-03-29T15:27:45.529066Z","shell.execute_reply.started":"2024-03-29T15:27:44.827769Z","shell.execute_reply":"2024-03-29T15:27:45.528114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:45.530511Z","iopub.execute_input":"2024-03-29T15:27:45.530904Z","iopub.status.idle":"2024-03-29T15:27:46.716424Z","shell.execute_reply.started":"2024-03-29T15:27:45.530869Z","shell.execute_reply":"2024-03-29T15:27:46.715613Z"},"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-03-29T15:27:46.717443Z","iopub.execute_input":"2024-03-29T15:27:46.717702Z","iopub.status.idle":"2024-03-29T15:27:47.384605Z","shell.execute_reply.started":"2024-03-29T15:27:46.717681Z","shell.execute_reply":"2024-03-29T15:27:47.383532Z"},"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-03-29T15:27:47.39238Z","iopub.execute_input":"2024-03-29T15:27:47.392702Z","iopub.status.idle":"2024-03-29T15:27:47.403218Z","shell.execute_reply.started":"2024-03-29T15:27:47.392675Z","shell.execute_reply":"2024-03-29T15:27:47.402348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_size = int(len(images) * CONFIG['TRAIN_RATIO'])","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:47.404297Z","iopub.execute_input":"2024-03-29T15:27:47.404672Z","iopub.status.idle":"2024-03-29T15:27:47.410641Z","shell.execute_reply.started":"2024-03-29T15:27:47.404644Z","shell.execute_reply":"2024-03-29T15:27:47.40953Z"},"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-03-29T15:27:47.411765Z","iopub.execute_input":"2024-03-29T15:27:47.412027Z","iopub.status.idle":"2024-03-29T15:27:47.428768Z","shell.execute_reply.started":"2024-03-29T15:27:47.412005Z","shell.execute_reply":"2024-03-29T15:27:47.42774Z"},"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-03-29T15:27:47.430098Z","iopub.execute_input":"2024-03-29T15:27:47.430592Z","iopub.status.idle":"2024-03-29T15:27:47.441523Z","shell.execute_reply.started":"2024-03-29T15:27:47.430558Z","shell.execute_reply":"2024-03-29T15:27:47.44065Z"},"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-03-29T15:27:47.442656Z","iopub.execute_input":"2024-03-29T15:27:47.442931Z","iopub.status.idle":"2024-03-29T15:27:47.453448Z","shell.execute_reply.started":"2024-03-29T15:27:47.442902Z","shell.execute_reply":"2024-03-29T15:27:47.45251Z"},"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-03-29T15:27:47.454603Z","iopub.execute_input":"2024-03-29T15:27:47.454876Z","iopub.status.idle":"2024-03-29T15:27:47.580668Z","shell.execute_reply.started":"2024-03-29T15:27:47.454854Z","shell.execute_reply":"2024-03-29T15:27:47.579669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:27:47.581806Z","iopub.execute_input":"2024-03-29T15:27:47.582089Z","iopub.status.idle":"2024-03-29T15:27:47.610053Z","shell.execute_reply.started":"2024-03-29T15:27:47.582064Z","shell.execute_reply":"2024-03-29T15:27:47.609114Z"},"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-03-29T15:27:47.611053Z","iopub.execute_input":"2024-03-29T15:27:47.611313Z","iopub.status.idle":"2024-03-29T15:27:47.623253Z","shell.execute_reply.started":"2024-03-29T15:27:47.61129Z","shell.execute_reply":"2024-03-29T15:27:47.622304Z"},"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-03-29T15:27:47.624582Z","iopub.execute_input":"2024-03-29T15:27:47.62486Z","iopub.status.idle":"2024-03-29T15:28:53.318529Z","shell.execute_reply.started":"2024-03-29T15:27:47.624836Z","shell.execute_reply":"2024-03-29T15:28:53.31764Z"},"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-03-29T15:28:53.319751Z","iopub.execute_input":"2024-03-29T15:28:53.320054Z","iopub.status.idle":"2024-03-29T15:28:53.385276Z","shell.execute_reply.started":"2024-03-29T15:28:53.320028Z","shell.execute_reply":"2024-03-29T15:28:53.384419Z"},"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-03-29T15:28:53.386588Z","iopub.execute_input":"2024-03-29T15:28:53.386864Z","iopub.status.idle":"2024-03-29T15:28:53.767572Z","shell.execute_reply.started":"2024-03-29T15:28:53.386841Z","shell.execute_reply":"2024-03-29T15:28:53.766532Z"},"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-03-29T15:28:53.768988Z","iopub.execute_input":"2024-03-29T15:28:53.769672Z","iopub.status.idle":"2024-03-29T15:28:54.102683Z","shell.execute_reply.started":"2024-03-29T15:28:53.769634Z","shell.execute_reply":"2024-03-29T15:28:54.101554Z"},"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-03-29T15:28:54.10414Z","iopub.execute_input":"2024-03-29T15:28:54.10467Z","iopub.status.idle":"2024-03-29T15:28:54.10979Z","shell.execute_reply.started":"2024-03-29T15:28:54.104633Z","shell.execute_reply":"2024-03-29T15:28:54.108751Z"},"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-03-29T15:28:54.111194Z","iopub.execute_input":"2024-03-29T15:28:54.111931Z","iopub.status.idle":"2024-03-29T15:28:56.256875Z","shell.execute_reply.started":"2024-03-29T15:28:54.111898Z","shell.execute_reply":"2024-03-29T15:28:56.255521Z"},"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-03-29T15:28:56.258162Z","iopub.execute_input":"2024-03-29T15:28:56.258545Z","iopub.status.idle":"2024-03-29T15:28:57.509233Z","shell.execute_reply.started":"2024-03-29T15:28:56.258511Z","shell.execute_reply":"2024-03-29T15:28:57.508324Z"},"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-03-29T15:28:57.5107Z","iopub.execute_input":"2024-03-29T15:28:57.511067Z","iopub.status.idle":"2024-03-29T15:29:01.510814Z","shell.execute_reply.started":"2024-03-29T15:28:57.511032Z","shell.execute_reply":"2024-03-29T15:29:01.509751Z"},"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-03-29T15:29:01.512458Z","iopub.execute_input":"2024-03-29T15:29:01.513122Z","iopub.status.idle":"2024-03-29T15:29:03.168783Z","shell.execute_reply.started":"2024-03-29T15:29:01.513084Z","shell.execute_reply":"2024-03-29T15:29:03.167758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(true_labels, predicted_labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.170161Z","iopub.execute_input":"2024-03-29T15:29:03.170508Z","iopub.status.idle":"2024-03-29T15:29:03.177176Z","shell.execute_reply.started":"2024-03-29T15:29:03.17048Z","shell.execute_reply":"2024-03-29T15:29:03.176049Z"},"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-03-29T15:29:03.178293Z","iopub.execute_input":"2024-03-29T15:29:03.178572Z","iopub.status.idle":"2024-03-29T15:29:03.471924Z","shell.execute_reply.started":"2024-03-29T15:29:03.17855Z","shell.execute_reply":"2024-03-29T15:29:03.470976Z"},"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-03-29T15:29:03.473352Z","iopub.execute_input":"2024-03-29T15:29:03.473742Z","iopub.status.idle":"2024-03-29T15:29:03.487158Z","shell.execute_reply.started":"2024-03-29T15:29:03.473709Z","shell.execute_reply":"2024-03-29T15:29:03.485954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.488157Z","iopub.execute_input":"2024-03-29T15:29:03.48852Z","iopub.status.idle":"2024-03-29T15:29:03.497284Z","shell.execute_reply.started":"2024-03-29T15:29:03.488494Z","shell.execute_reply":"2024-03-29T15:29:03.496174Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.498487Z","iopub.execute_input":"2024-03-29T15:29:03.499066Z","iopub.status.idle":"2024-03-29T15:29:03.509704Z","shell.execute_reply.started":"2024-03-29T15:29:03.49904Z","shell.execute_reply":"2024-03-29T15:29:03.508736Z"},"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-03-29T15:29:03.510896Z","iopub.execute_input":"2024-03-29T15:29:03.511221Z","iopub.status.idle":"2024-03-29T15:29:03.520055Z","shell.execute_reply.started":"2024-03-29T15:29:03.511187Z","shell.execute_reply":"2024-03-29T15:29:03.519164Z"},"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-03-29T15:29:03.521131Z","iopub.execute_input":"2024-03-29T15:29:03.521495Z","iopub.status.idle":"2024-03-29T15:29:03.532714Z","shell.execute_reply.started":"2024-03-29T15:29:03.521465Z","shell.execute_reply":"2024-03-29T15:29:03.531849Z"},"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-03-29T15:29:03.53386Z","iopub.execute_input":"2024-03-29T15:29:03.534199Z","iopub.status.idle":"2024-03-29T15:29:03.539541Z","shell.execute_reply.started":"2024-03-29T15:29:03.534165Z","shell.execute_reply":"2024-03-29T15:29:03.538696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = read_image(test_df_cat)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.540747Z","iopub.execute_input":"2024-03-29T15:29:03.541124Z","iopub.status.idle":"2024-03-29T15:29:03.740205Z","shell.execute_reply.started":"2024-03-29T15:29:03.541093Z","shell.execute_reply":"2024-03-29T15:29:03.739097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df_cat","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.741496Z","iopub.execute_input":"2024-03-29T15:29:03.741794Z","iopub.status.idle":"2024-03-29T15:29:03.751035Z","shell.execute_reply.started":"2024-03-29T15:29:03.74177Z","shell.execute_reply":"2024-03-29T15:29:03.750078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images , labels = resize_normalize_batch(images , labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.752255Z","iopub.execute_input":"2024-03-29T15:29:03.75323Z","iopub.status.idle":"2024-03-29T15:29:03.761013Z","shell.execute_reply.started":"2024-03-29T15:29:03.753201Z","shell.execute_reply":"2024-03-29T15:29:03.760246Z"},"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-03-29T15:29:03.762025Z","iopub.execute_input":"2024-03-29T15:29:03.762604Z","iopub.status.idle":"2024-03-29T15:29:03.769979Z","shell.execute_reply.started":"2024-03-29T15:29:03.762562Z","shell.execute_reply":"2024-03-29T15:29:03.76895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images = tf.convert_to_tensor(images)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.771065Z","iopub.execute_input":"2024-03-29T15:29:03.771325Z","iopub.status.idle":"2024-03-29T15:29:03.782979Z","shell.execute_reply.started":"2024-03-29T15:29:03.771302Z","shell.execute_reply":"2024-03-29T15:29:03.782142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = tf.convert_to_tensor(labels)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.784034Z","iopub.execute_input":"2024-03-29T15:29:03.784326Z","iopub.status.idle":"2024-03-29T15:29:03.793482Z","shell.execute_reply.started":"2024-03-29T15:29:03.784282Z","shell.execute_reply":"2024-03-29T15:29:03.792432Z"},"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-03-29T15:29:03.794537Z","iopub.execute_input":"2024-03-29T15:29:03.794793Z","iopub.status.idle":"2024-03-29T15:29:03.807247Z","shell.execute_reply.started":"2024-03-29T15:29:03.794771Z","shell.execute_reply":"2024-03-29T15:29:03.806426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = test_data.batch(CONFIG['BATCH_SIZE'])","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.808316Z","iopub.execute_input":"2024-03-29T15:29:03.8086Z","iopub.status.idle":"2024-03-29T15:29:03.817917Z","shell.execute_reply.started":"2024-03-29T15:29:03.808577Z","shell.execute_reply":"2024-03-29T15:29:03.817107Z"},"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-03-29T15:29:03.819317Z","iopub.execute_input":"2024-03-29T15:29:03.819745Z","iopub.status.idle":"2024-03-29T15:29:03.83005Z","shell.execute_reply.started":"2024-03-29T15:29:03.819712Z","shell.execute_reply":"2024-03-29T15:29:03.8292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label_to_index","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:03.831235Z","iopub.execute_input":"2024-03-29T15:29:03.831566Z","iopub.status.idle":"2024-03-29T15:29:03.841617Z","shell.execute_reply.started":"2024-03-29T15:29:03.831541Z","shell.execute_reply":"2024-03-29T15:29:03.840595Z"},"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-03-29T15:29:03.842724Z","iopub.execute_input":"2024-03-29T15:29:03.843087Z","iopub.status.idle":"2024-03-29T15:29:04.284204Z","shell.execute_reply.started":"2024-03-29T15:29:03.843054Z","shell.execute_reply":"2024-03-29T15:29:04.283258Z"},"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-03-29T15:29:04.28556Z","iopub.execute_input":"2024-03-29T15:29:04.285997Z","iopub.status.idle":"2024-03-29T15:29:04.291577Z","shell.execute_reply.started":"2024-03-29T15:29:04.285961Z","shell.execute_reply":"2024-03-29T15:29:04.290684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:04.29283Z","iopub.execute_input":"2024-03-29T15:29:04.293131Z","iopub.status.idle":"2024-03-29T15:29:04.307253Z","shell.execute_reply.started":"2024-03-29T15:29:04.293107Z","shell.execute_reply":"2024-03-29T15:29:04.3062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:04.30833Z","iopub.execute_input":"2024-03-29T15:29:04.308637Z","iopub.status.idle":"2024-03-29T15:29:04.31921Z","shell.execute_reply.started":"2024-03-29T15:29:04.308613Z","shell.execute_reply":"2024-03-29T15:29:04.318308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df","metadata":{"execution":{"iopub.status.busy":"2024-03-29T15:29:04.32063Z","iopub.execute_input":"2024-03-29T15:29:04.320916Z","iopub.status.idle":"2024-03-29T15:29:04.331639Z","shell.execute_reply.started":"2024-03-29T15:29:04.320892Z","shell.execute_reply":"2024-03-29T15:29:04.330782Z"},"trusted":true},"execution_count":null,"outputs":[]}]}