{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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)\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\n#import os\n#for 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-10-17T14:13:08.761392Z","iopub.execute_input":"2023-10-17T14:13:08.761775Z","iopub.status.idle":"2023-10-17T14:13:08.76718Z","shell.execute_reply.started":"2023-10-17T14:13:08.761747Z","shell.execute_reply":"2023-10-17T14:13:08.766002Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import warnings\nwarnings.simplefilter('ignore')\nimport os\nimport cv2\nimport random\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:13:08.769476Z","iopub.execute_input":"2023-10-17T14:13:08.769775Z","iopub.status.idle":"2023-10-17T14:13:08.789849Z","shell.execute_reply.started":"2023-10-17T14:13:08.769751Z","shell.execute_reply":"2023-10-17T14:13:08.788968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/UBC-OCEAN/train.csv')\ntest = pd.read_csv('../input/UBC-OCEAN/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:13:08.791571Z","iopub.execute_input":"2023-10-17T14:13:08.792035Z","iopub.status.idle":"2023-10-17T14:13:08.814456Z","shell.execute_reply.started":"2023-10-17T14:13:08.792005Z","shell.execute_reply":"2023-10-17T14:13:08.813167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tma = train.loc[train['is_tma']==True,:]\ntrain_tma.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:13:08.816156Z","iopub.execute_input":"2023-10-17T14:13:08.816498Z","iopub.status.idle":"2023-10-17T14:13:08.829383Z","shell.execute_reply.started":"2023-10-17T14:13:08.816471Z","shell.execute_reply":"2023-10-17T14:13:08.828342Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_no_tma = train.loc[train['is_tma']==False,:]\ntrain_no_tma.head()","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:13:08.833349Z","iopub.execute_input":"2023-10-17T14:13:08.83368Z","iopub.status.idle":"2023-10-17T14:13:08.85457Z","shell.execute_reply.started":"2023-10-17T14:13:08.833654Z","shell.execute_reply":"2023-10-17T14:13:08.853346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tma['img_id_ext']=[str(i)+\".png\" for i in train_tma['image_id']]\ntrain_no_tma['img_id_ext']=[str(i)+\"_thumbnail.png\" for i in train_no_tma['image_id']]\ntest['img_id_ext']=[str(i)+\"_thumbnail.png\" for i in test['image_id']]","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:13:08.855976Z","iopub.execute_input":"2023-10-17T14:13:08.856577Z","iopub.status.idle":"2023-10-17T14:13:08.864334Z","shell.execute_reply.started":"2023-10-17T14:13:08.856546Z","shell.execute_reply":"2023-10-17T14:13:08.863027Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimage_data = []\nimage_label = []\n\nfor img , label in zip(train_no_tma['img_id_ext'],train_no_tma['label']):\n    image = Image.open(\"/kaggle/input/UBC-OCEAN/train_thumbnails/\"+img)\n    image = image.resize((200,200))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    image = image.astype('float64')\n    image_data.append(image.reshape([3, 200, 200]))\n    image_label.append(label)\n\nfor img , label in zip(train_tma['img_id_ext'],train_tma['label']):\n    image = Image.open(\"/kaggle/input/UBC-OCEAN/train_images/\"+img)\n    image = image.resize((200,200))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    image = image.astype('float64')\n    image_data.append(image.reshape([3, 200, 200]))\n    image_label.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:13:08.865714Z","iopub.execute_input":"2023-10-17T14:13:08.866266Z","iopub.status.idle":"2023-10-17T14:15:24.14146Z","shell.execute_reply.started":"2023-10-17T14:13:08.866197Z","shell.execute_reply":"2023-10-17T14:15:24.139879Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(image_data))\nprint(len(image_label))","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.143742Z","iopub.execute_input":"2023-10-17T14:15:24.14436Z","iopub.status.idle":"2023-10-17T14:15:24.150651Z","shell.execute_reply.started":"2023-10-17T14:15:24.144311Z","shell.execute_reply":"2023-10-17T14:15:24.149568Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import LabelEncoder\nle = LabelEncoder()\nle.fit(image_label)\n\nfinal_label = le.transform(image_label)\nfinal_label","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.152685Z","iopub.execute_input":"2023-10-17T14:15:24.1533Z","iopub.status.idle":"2023-10-17T14:15:24.171319Z","shell.execute_reply.started":"2023-10-17T14:15:24.153272Z","shell.execute_reply":"2023-10-17T14:15:24.170167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(image_data)\ny = np.array(final_label)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.172691Z","iopub.execute_input":"2023-10-17T14:15:24.173463Z","iopub.status.idle":"2023-10-17T14:15:24.447711Z","shell.execute_reply.started":"2023-10-17T14:15:24.173433Z","shell.execute_reply":"2023-10-17T14:15:24.446295Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# importing necessary libraries\nimport torch\nimport torchvision\nfrom time import time\nfrom torchvision import datasets, transforms\nfrom torch import nn, optim\nimport torch.utils.data as Data\nfrom torch import Tensor\nfrom torch.autograd import Variable","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.449797Z","iopub.execute_input":"2023-10-17T14:15:24.450179Z","iopub.status.idle":"2023-10-17T14:15:24.456522Z","shell.execute_reply.started":"2023-10-17T14:15:24.450152Z","shell.execute_reply":"2023-10-17T14:15:24.455154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_tensor = torch.tensor(y, dtype=torch.long)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.45827Z","iopub.execute_input":"2023-10-17T14:15:24.45875Z","iopub.status.idle":"2023-10-17T14:15:24.471777Z","shell.execute_reply.started":"2023-10-17T14:15:24.458714Z","shell.execute_reply":"2023-10-17T14:15:24.470906Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch_dataset_train = Data.TensorDataset(Tensor(np.array(x)), y_tensor)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.47285Z","iopub.execute_input":"2023-10-17T14:15:24.473343Z","iopub.status.idle":"2023-10-17T14:15:24.974029Z","shell.execute_reply.started":"2023-10-17T14:15:24.473303Z","shell.execute_reply":"2023-10-17T14:15:24.973081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trainloader = torch.utils.data.DataLoader(torch_dataset_train, batch_size=2, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.979006Z","iopub.execute_input":"2023-10-17T14:15:24.9795Z","iopub.status.idle":"2023-10-17T14:15:24.984964Z","shell.execute_reply.started":"2023-10-17T14:15:24.979457Z","shell.execute_reply":"2023-10-17T14:15:24.983307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# shape of training data\ndataiter = iter(trainloader)\nimages = next(dataiter)\nimages[0].shape","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:24.986394Z","iopub.execute_input":"2023-10-17T14:15:24.987601Z","iopub.status.idle":"2023-10-17T14:15:25.00147Z","shell.execute_reply.started":"2023-10-17T14:15:24.98756Z","shell.execute_reply":"2023-10-17T14:15:25.000317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class CNN(nn.Module):\n    def __init__(self, num_classes):\n        super().__init__()\n        self.features = nn.Sequential(\n            nn.Conv2d(3, 32, kernel_size=3, stride=2, padding=1),\n            nn.ReLU(),\n            nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n            nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n            nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1),\n            nn.ReLU(),\n            nn.MaxPool2d(2),\n        )\n        self.classifier = nn.Linear(256, num_classes)\n\n    def forward(self, x):\n        x = self.features(x)\n        x = x.view(x.size(0), -1)\n        x = self.classifier(x)\n        return x","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:25.003102Z","iopub.execute_input":"2023-10-17T14:15:25.003852Z","iopub.status.idle":"2023-10-17T14:15:25.014985Z","shell.execute_reply.started":"2023-10-17T14:15:25.003813Z","shell.execute_reply":"2023-10-17T14:15:25.013993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#define the optimizer and loss function \n# defining the model\nmodel = CNN(5)\n# defining the optimizer\noptimizer = optim.SGD(model.parameters(), lr=0.0001)\n# defining the loss function\ncriterion = nn.CrossEntropyLoss()\n# checking if GPU is available\nprint(torch.cuda.is_available())\nif torch.cuda.is_available():\n    model = model.to(\"cuda\")\n    criterion = criterion.to(\"cuda\")\n    \nprint(model)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:25.016517Z","iopub.execute_input":"2023-10-17T14:15:25.017172Z","iopub.status.idle":"2023-10-17T14:15:25.042543Z","shell.execute_reply.started":"2023-10-17T14:15:25.017134Z","shell.execute_reply":"2023-10-17T14:15:25.041326Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train this model for 15 epochs\nfor i in range(10):\n    \n    running_loss = 0\n    model.train() # indicator for training phase\n   \n    for images, labels in trainloader:\n\n        if torch.cuda.is_available():\n            images = images.to(\"cuda\")\n            \n            labels = labels.to(\"cuda\")\n    \n        # Training pass\n        optimizer.zero_grad()\n        \n        output = model(images)\n        \n        loss = criterion(output, labels)\n        \n        #This is where the model learns by backpropagating\n        loss.backward()\n        \n        #And optimizes its weights here\n        optimizer.step()\n        \n        running_loss += loss.item()\n    else:\n        print(\"Epoch {} - Training loss: {}\".format(i+1, running_loss/len(trainloader)))","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:15:25.043667Z","iopub.execute_input":"2023-10-17T14:15:25.044091Z","iopub.status.idle":"2023-10-17T14:16:13.869761Z","shell.execute_reply.started":"2023-10-17T14:15:25.044062Z","shell.execute_reply":"2023-10-17T14:16:13.868274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_image = []\ntest_label = []\ntest.dropna()\nfor img , label in zip(test['img_id_ext'],[0]*len(test)): \n    image = Image.open(\"/kaggle/input/UBC-OCEAN/test_thumbnails/\"+img)\n    image = image.resize((200,200))\n    image = image.convert(\"RGB\")\n    image = np.array(image)\n    test_image.append(image.reshape([3, 200, 200]))\n    test_label.append(label)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:13.871607Z","iopub.execute_input":"2023-10-17T14:16:13.872386Z","iopub.status.idle":"2023-10-17T14:16:14.114431Z","shell.execute_reply.started":"2023-10-17T14:16:13.872344Z","shell.execute_reply":"2023-10-17T14:16:14.113574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(test_image))\nprint(len(test_label))","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.115865Z","iopub.execute_input":"2023-10-17T14:16:14.116617Z","iopub.status.idle":"2023-10-17T14:16:14.122507Z","shell.execute_reply.started":"2023-10-17T14:16:14.116578Z","shell.execute_reply":"2023-10-17T14:16:14.121369Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = torch.tensor(test_label, dtype=torch.long)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.124667Z","iopub.execute_input":"2023-10-17T14:16:14.125933Z","iopub.status.idle":"2023-10-17T14:16:14.137835Z","shell.execute_reply.started":"2023-10-17T14:16:14.125885Z","shell.execute_reply":"2023-10-17T14:16:14.136472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch_dataset_test = Data.TensorDataset(Tensor(np.array(test_image)),y)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.139282Z","iopub.execute_input":"2023-10-17T14:16:14.140373Z","iopub.status.idle":"2023-10-17T14:16:14.152478Z","shell.execute_reply.started":"2023-10-17T14:16:14.140323Z","shell.execute_reply":"2023-10-17T14:16:14.15099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"testloader = torch.utils.data.DataLoader(torch_dataset_test, batch_size=2, shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.154395Z","iopub.execute_input":"2023-10-17T14:16:14.155023Z","iopub.status.idle":"2023-10-17T14:16:14.166373Z","shell.execute_reply.started":"2023-10-17T14:16:14.154981Z","shell.execute_reply":"2023-10-17T14:16:14.165358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_list = []\n\nwith torch.no_grad():\n    for x_batch, y_batch in testloader:\n        x_batch, y_batch = x_batch.to(), y_batch.to()\n        y_test_pred = model(x_batch)\n        _, y_pred_tag = torch.max(y_test_pred, dim = 1)\n        y_pred_list.extend(y_pred_tag.cpu().numpy())\nprint(y_pred_list)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.16778Z","iopub.execute_input":"2023-10-17T14:16:14.168534Z","iopub.status.idle":"2023-10-17T14:16:14.190606Z","shell.execute_reply.started":"2023-10-17T14:16:14.168493Z","shell.execute_reply":"2023-10-17T14:16:14.189457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result = le.inverse_transform(y_pred_list)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.192343Z","iopub.execute_input":"2023-10-17T14:16:14.193092Z","iopub.status.idle":"2023-10-17T14:16:14.199505Z","shell.execute_reply.started":"2023-10-17T14:16:14.193054Z","shell.execute_reply":"2023-10-17T14:16:14.19825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"result[0]","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.201014Z","iopub.execute_input":"2023-10-17T14:16:14.201727Z","iopub.status.idle":"2023-10-17T14:16:14.212912Z","shell.execute_reply.started":"2023-10-17T14:16:14.201687Z","shell.execute_reply":"2023-10-17T14:16:14.211438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_to_submit = pd.DataFrame({'image_id': test['image_id']})\n\ndf_to_submit[\"label\"] = np.array(result)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.214323Z","iopub.execute_input":"2023-10-17T14:16:14.215416Z","iopub.status.idle":"2023-10-17T14:16:14.22552Z","shell.execute_reply.started":"2023-10-17T14:16:14.215375Z","shell.execute_reply":"2023-10-17T14:16:14.224553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_to_submit","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.227093Z","iopub.execute_input":"2023-10-17T14:16:14.22812Z","iopub.status.idle":"2023-10-17T14:16:14.242593Z","shell.execute_reply.started":"2023-10-17T14:16:14.228078Z","shell.execute_reply":"2023-10-17T14:16:14.240939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_to_submit.to_csv(\"submission.csv\", index=None)","metadata":{"execution":{"iopub.status.busy":"2023-10-17T14:16:14.24428Z","iopub.execute_input":"2023-10-17T14:16:14.24535Z","iopub.status.idle":"2023-10-17T14:16:14.263595Z","shell.execute_reply.started":"2023-10-17T14:16:14.245307Z","shell.execute_reply":"2023-10-17T14:16:14.262295Z"},"trusted":true},"execution_count":null,"outputs":[]}]}