{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"}],"dockerImageVersionId":30558,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport cv2 as cv\nimport matplotlib.pyplot as plt\nfrom matplotlib import gridspec\nimport matplotlib.image as img\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-21T13:55:14.828807Z","iopub.execute_input":"2023-12-21T13:55:14.830117Z","iopub.status.idle":"2023-12-21T13:55:14.858231Z","shell.execute_reply.started":"2023-12-21T13:55:14.830068Z","shell.execute_reply":"2023-12-21T13:55:14.857308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nmy_data = pd.read_csv('/kaggle/input/UBC-OCEAN/test.csv')\nprint(my_data.head())\nprint(my_data.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.254524Z","iopub.execute_input":"2023-12-21T13:55:15.25497Z","iopub.status.idle":"2023-12-21T13:55:15.267312Z","shell.execute_reply.started":"2023-12-21T13:55:15.254933Z","shell.execute_reply":"2023-12-21T13:55:15.265985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/UBC-OCEAN/sample_submission.csv\")\nprint(sample_submission.head())\nsample_submission.shape","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.26992Z","iopub.execute_input":"2023-12-21T13:55:15.270688Z","iopub.status.idle":"2023-12-21T13:55:15.281995Z","shell.execute_reply.started":"2023-12-21T13:55:15.270653Z","shell.execute_reply":"2023-12-21T13:55:15.281117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\ntrain = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\nprint(train.head())\nprint(train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.28296Z","iopub.execute_input":"2023-12-21T13:55:15.283311Z","iopub.status.idle":"2023-12-21T13:55:15.298595Z","shell.execute_reply.started":"2023-12-21T13:55:15.283282Z","shell.execute_reply":"2023-12-21T13:55:15.297112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.info()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.299917Z","iopub.execute_input":"2023-12-21T13:55:15.300329Z","iopub.status.idle":"2023-12-21T13:55:15.316547Z","shell.execute_reply.started":"2023-12-21T13:55:15.300295Z","shell.execute_reply":"2023-12-21T13:55:15.31493Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\nlabels=train['label'].unique()\nprint(labels)\nid_train=train['image_id'].value_counts()\nprint(id_train)\ntrain_is_tma=train[train['is_tma']==False].value_counts()\nprint(train_is_tma)\ntrain_is_tma1=train[train['is_tma']==True].value_counts()\nprint(train_is_tma1)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.320697Z","iopub.execute_input":"2023-12-21T13:55:15.321239Z","iopub.status.idle":"2023-12-21T13:55:15.353006Z","shell.execute_reply.started":"2023-12-21T13:55:15.32119Z","shell.execute_reply":"2023-12-21T13:55:15.35196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/UBC-OCEAN/train.csv')\n\n\ntrain_is_tma_false = train[train['is_tma'] == False]\nunique_image_ids = train_is_tma_false['image_id'].unique()\nprint(unique_image_ids)\n\nunique_labels = train_is_tma_false['label'].unique()\n\nprint(unique_labels)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.354071Z","iopub.execute_input":"2023-12-21T13:55:15.354431Z","iopub.status.idle":"2023-12-21T13:55:15.37154Z","shell.execute_reply.started":"2023-12-21T13:55:15.3544Z","shell.execute_reply":"2023-12-21T13:55:15.370512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#import matplotlib.image as img\n#import matplotlib.pyplot as plt\n#image = img.imread(\"/kaggle/input/UBC-OCEAN/train_images/13387.png\")\n#plt.imshow(image)\n#image.size","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.373105Z","iopub.execute_input":"2023-12-21T13:55:15.373458Z","iopub.status.idle":"2023-12-21T13:55:15.378795Z","shell.execute_reply.started":"2023-12-21T13:55:15.373429Z","shell.execute_reply":"2023-12-21T13:55:15.377571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#image2 = img.imread(\"/kaggle/input/UBC-OCEAN/train_thumbnails/13387_thumbnail.png\")\n#plt.imshow(image2)\n#image2.size","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.380632Z","iopub.execute_input":"2023-12-21T13:55:15.381112Z","iopub.status.idle":"2023-12-21T13:55:15.390367Z","shell.execute_reply.started":"2023-12-21T13:55:15.381063Z","shell.execute_reply":"2023-12-21T13:55:15.388863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfrom PIL import Image\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.394132Z","iopub.execute_input":"2023-12-21T13:55:15.394782Z","iopub.status.idle":"2023-12-21T13:55:15.402705Z","shell.execute_reply.started":"2023-12-21T13:55:15.394737Z","shell.execute_reply":"2023-12-21T13:55:15.401538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root = \"/kaggle/input/UBC-OCEAN/train_thumbnails\"\nfnames = os.listdir(root)\nlen(fnames)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.403811Z","iopub.execute_input":"2023-12-21T13:55:15.404219Z","iopub.status.idle":"2023-12-21T13:55:15.419484Z","shell.execute_reply.started":"2023-12-21T13:55:15.404185Z","shell.execute_reply":"2023-12-21T13:55:15.418117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root2 = \"/kaggle/input/UBC-OCEAN/test_thumbnails\"\nfnames2 = os.listdir(root2)\nlen(fnames2)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.608795Z","iopub.execute_input":"2023-12-21T13:55:15.609559Z","iopub.status.idle":"2023-12-21T13:55:15.61811Z","shell.execute_reply.started":"2023-12-21T13:55:15.60952Z","shell.execute_reply":"2023-12-21T13:55:15.617088Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root3 = \"/kaggle/input/UBC-OCEAN/train_images\"\nfnames3 = os.listdir(root3)\nlen(fnames3)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.621382Z","iopub.execute_input":"2023-12-21T13:55:15.622275Z","iopub.status.idle":"2023-12-21T13:55:15.631263Z","shell.execute_reply.started":"2023-12-21T13:55:15.622226Z","shell.execute_reply":"2023-12-21T13:55:15.630112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root4 = \"/kaggle/input/UBC-OCEAN/test_images\"\nfnames4 = os.listdir(root4)\nlen(fnames4)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.633078Z","iopub.execute_input":"2023-12-21T13:55:15.633836Z","iopub.status.idle":"2023-12-21T13:55:15.642284Z","shell.execute_reply.started":"2023-12-21T13:55:15.633789Z","shell.execute_reply":"2023-12-21T13:55:15.641059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, axs = plt.subplots(nrows=2,ncols=5)\naxs = axs.flatten()\nfor i in range(10):\n    filepath = os.path.join(root,fnames[i])\n    img = Image.open(filepath)\n    axs[i].imshow(img)\n    #axs[i].axis(\"off\")\n    axs[i].set_title(fnames[i])\n    \nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:15.643764Z","iopub.execute_input":"2023-12-21T13:55:15.64465Z","iopub.status.idle":"2023-12-21T13:55:24.531137Z","shell.execute_reply.started":"2023-12-21T13:55:15.644603Z","shell.execute_reply":"2023-12-21T13:55:24.529908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimport os\n\nimage_data=[]\nimage_label=[]\npath_thumbnails='/kaggle/input/UBC-OCEAN/train_thumbnails'\nfor img , label in zip(train_is_tma_false['image_id'],train_is_tma_false['label']):\n    image_path=os.path.join('/kaggle/input/UBC-OCEAN/train_thumbnails',str(img))\n    image = Image.open(filepath)\n    image = image.resize((512,512))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    image_data.append(image)\n    image_label.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:55:24.53523Z","iopub.execute_input":"2023-12-21T13:55:24.536167Z","iopub.status.idle":"2023-12-21T13:56:30.503291Z","shell.execute_reply.started":"2023-12-21T13:55:24.536114Z","shell.execute_reply":"2023-12-21T13:56:30.501641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(image_data))\nprint(len( image_label))","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2023-12-21T13:56:30.505541Z","iopub.execute_input":"2023-12-21T13:56:30.505991Z","iopub.status.idle":"2023-12-21T13:56:30.511642Z","shell.execute_reply.started":"2023-12-21T13:56:30.505954Z","shell.execute_reply":"2023-12-21T13:56:30.510711Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_label_1 = []\nfor i in image_label:\n    if i==\"CC\":\n        image_label_1.append(0)\n    elif i==\"EC\":\n        image_label_1.append(1)\n    elif i==\"HGSC\":\n        image_label_1.append(2)\n    elif i==\"LGSC\":\n        image_label_1.append(3)\n    elif i==\"MC\":\n        image_label_1.append(4)\nprint(image_label_1 )\nprint(len(image_label_1 ))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:30.513127Z","iopub.execute_input":"2023-12-21T13:56:30.513678Z","iopub.status.idle":"2023-12-21T13:56:30.53081Z","shell.execute_reply.started":"2023-12-21T13:56:30.513646Z","shell.execute_reply":"2023-12-21T13:56:30.529791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=np.array(image_data)\ny=np.array(image_label_1)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:30.532314Z","iopub.execute_input":"2023-12-21T13:56:30.532904Z","iopub.status.idle":"2023-12-21T13:56:30.722046Z","shell.execute_reply.started":"2023-12-21T13:56:30.532869Z","shell.execute_reply":"2023-12-21T13:56:30.720361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test = train_test_split (x,y ,test_size=0.3, random_state=42)\nprint (len(x_train))\nprint (len(x_test))\nprint (len( y_train))\nprint (len(y_test))","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:30.723986Z","iopub.execute_input":"2023-12-21T13:56:30.724429Z","iopub.status.idle":"2023-12-21T13:56:30.88117Z","shell.execute_reply.started":"2023-12-21T13:56:30.724395Z","shell.execute_reply":"2023-12-21T13:56:30.879698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Normalized\nx_train_normalize=np.array(x_train)/255\nx_test_normalize=np.array(x_test)/255","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:30.882573Z","iopub.execute_input":"2023-12-21T13:56:30.884181Z","iopub.status.idle":"2023-12-21T13:56:33.010035Z","shell.execute_reply.started":"2023-12-21T13:56:30.884135Z","shell.execute_reply":"2023-12-21T13:56:33.008457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense, BatchNormalization\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.metrics import accuracy_score\n#from sklearn.metrics import accuracy_score\n\nmodel = Sequential()\n\n#Feature extractor\n# the first layer is an input layer (condense or extract all the information to make an output classification)\n#convolution layer \nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu', input_shape = (512,512,3)))\nmodel.add(Conv2D(filters = 32, kernel_size = (5,5),padding = 'Same',activation ='relu'))\n# gets the maximum data to condense the information \nmodel.add(MaxPool2D(pool_size=(2,2)))\n\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\nmodel.add(Conv2D(filters = 64, kernel_size = (3,3),padding = 'Same',activation ='relu'))\n\nmodel.add(MaxPool2D(pool_size=(2,2), strides=(2,2)))\n\nmodel.add(Dropout(0.25))\n#condenses down to single dimension \nmodel.add(Flatten())\n\n#Classifier\nmodel.add(Dense(64, activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(5, activation = \"softmax\"))\n\nmodel.compile(optimizer=\"adam\",loss=\"sparse_categorical_crossentropy\",metrics=[\"accuracy\"])\nmodel.summary()\n\n\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:33.012336Z","iopub.execute_input":"2023-12-21T13:56:33.012899Z","iopub.status.idle":"2023-12-21T13:56:33.718363Z","shell.execute_reply.started":"2023-12-21T13:56:33.012847Z","shell.execute_reply":"2023-12-21T13:56:33.711345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data Augmentation\ndatagen = ImageDataGenerator(rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True)\ndatagen.fit(x_train_normalize)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:33.720582Z","iopub.execute_input":"2023-12-21T13:56:33.721671Z","iopub.status.idle":"2023-12-21T13:56:34.981487Z","shell.execute_reply.started":"2023-12-21T13:56:33.721616Z","shell.execute_reply":"2023-12-21T13:56:34.980142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.callbacks import EarlyStopping\n\nearly_stopping = EarlyStopping(monitor='val_loss', patience=3)\n\nbatch_size = 64\nepochs = 3\n\nhist = model.fit(datagen.flow(x_train_normalize, y_train, batch_size=batch_size),\n                 epochs=epochs,\n                 validation_data=(x_test_normalize, y_test),\n                 callbacks=[early_stopping])","metadata":{"execution":{"iopub.status.busy":"2023-12-21T13:56:34.983092Z","iopub.execute_input":"2023-12-21T13:56:34.983456Z","iopub.status.idle":"2023-12-21T14:25:43.899974Z","shell.execute_reply.started":"2023-12-21T13:56:34.983425Z","shell.execute_reply":"2023-12-21T14:25:43.898379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate on test set\ntest_loss, test_acc = model.evaluate(x_test_normalize, y_test)\nprint(f'Test Accuracy: {test_acc}')","metadata":{"execution":{"iopub.status.busy":"2023-12-21T14:25:43.904915Z","iopub.execute_input":"2023-12-21T14:25:43.905359Z","iopub.status.idle":"2023-12-21T14:26:30.23587Z","shell.execute_reply.started":"2023-12-21T14:25:43.905321Z","shell.execute_reply":"2023-12-21T14:26:30.234667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# You can also plot the training history for better visualization\nimport matplotlib.pyplot as plt\nplt.plot(hist.history['accuracy'], label='Training Accuracy')\nplt.plot(hist.history['val_accuracy'], label='Validation Accuracy')\nplt.xlabel('Epoch')\nplt.ylabel('Accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T14:26:30.238151Z","iopub.execute_input":"2023-12-21T14:26:30.238638Z","iopub.status.idle":"2023-12-21T14:26:30.652004Z","shell.execute_reply.started":"2023-12-21T14:26:30.238593Z","shell.execute_reply":"2023-12-21T14:26:30.650667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(hist.history['loss'],color='red',label='training loss')\nplt.plot(hist.history['val_loss'],color='blue',label='validation loss')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-21T14:26:30.653669Z","iopub.execute_input":"2023-12-21T14:26:30.654062Z","iopub.status.idle":"2023-12-21T14:26:30.978607Z","shell.execute_reply.started":"2023-12-21T14:26:30.654002Z","shell.execute_reply":"2023-12-21T14:26:30.977254Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.models import load_model\nimport numpy as np\n\nmodel.save('my_model.h6')\n\n\nloaded_model = load_model('my_model.h6')\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T14:26:30.98027Z","iopub.execute_input":"2023-12-21T14:26:30.9809Z","iopub.status.idle":"2023-12-21T14:26:35.964621Z","shell.execute_reply.started":"2023-12-21T14:26:30.980854Z","shell.execute_reply":"2023-12-21T14:26:35.963284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = loaded_model.predict(x_test_normalize)\n\n\npredicted_classes = np.argmax(predictions, axis=1)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-21T14:26:35.969152Z","iopub.execute_input":"2023-12-21T14:26:35.969569Z","iopub.status.idle":"2023-12-21T14:27:58.735957Z","shell.execute_reply.started":"2023-12-21T14:26:35.969535Z","shell.execute_reply":"2023-12-21T14:27:58.734637Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = accuracy_score(y_test, predicted_classes)\nprint(f'Test Accuracy (from saved model): {accuracy}')","metadata":{"execution":{"iopub.status.busy":"2023-12-21T14:27:58.737643Z","iopub.execute_input":"2023-12-21T14:27:58.738846Z","iopub.status.idle":"2023-12-21T14:27:58.746095Z","shell.execute_reply.started":"2023-12-21T14:27:58.738801Z","shell.execute_reply":"2023-12-21T14:27:58.745114Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}