{"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":"gpu","dataSources":[{"sourceId":45867,"databundleVersionId":6924515,"sourceType":"competition"},{"sourceId":6640479,"sourceType":"datasetVersion","datasetId":3833517},{"sourceId":150860102,"sourceType":"kernelVersion"},{"sourceId":145733747,"sourceType":"kernelVersion"},{"sourceId":3729,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":2656}],"dockerImageVersionId":30615,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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\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-13T04:53:59.9523Z","iopub.execute_input":"2023-12-13T04:53:59.952671Z","iopub.status.idle":"2023-12-13T04:54:00.467158Z","shell.execute_reply.started":"2023-12-13T04:53:59.95264Z","shell.execute_reply":"2023-12-13T04:54:00.466062Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport gc\nimport cv2\nimport math\nimport copy\nimport time\nimport random\nimport glob\nfrom matplotlib import pyplot as plt\n\n# For data manipulation\nimport numpy as np\nimport pandas as pd\n\n# Pytorch Imports\nimport torch\nimport torch.nn as nn\nimport torch.optim as optim\nimport torch.nn.functional as F\nfrom torch.optim import lr_scheduler\nfrom torch.utils.data import Dataset, DataLoader\nfrom torch.cuda import amp\nimport torchvision\n\n# Utils\nimport joblib\nfrom tqdm import tqdm\nfrom collections import defaultdict\n\n# Sklearn Imports\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import StratifiedKFold\n\n# For Image Models\nimport timm\n\n# Albumentations for augmentations\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\n# For colored terminal text\nfrom colorama import Fore, Back, Style\nb_ = Fore.BLUE\nsr_ = Style.RESET_ALL\n\nimport warnings\nwarnings.filterwarnings(\"ignore\")\n\n# For descriptive error messages\nos.environ['CUDA_LAUNCH_BLOCKING'] = \"1\"","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:54:52.245127Z","iopub.execute_input":"2023-12-13T04:54:52.245856Z","iopub.status.idle":"2023-12-13T04:54:58.942291Z","shell.execute_reply.started":"2023-12-13T04:54:52.245819Z","shell.execute_reply":"2023-12-13T04:54:58.941381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"CONFIG = {\n    \"seed\": 42,\n    \"img_size\": 512,\n    \"model_name\": \"tf_efficientnet_b0_ns\",\n    \"num_classes\": 5,\n    \"valid_batch_size\": 64,\n    \"device\": torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\"),\n}","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:16.852993Z","iopub.execute_input":"2023-12-13T04:55:16.854067Z","iopub.status.idle":"2023-12-13T04:55:16.859704Z","shell.execute_reply.started":"2023-12-13T04:55:16.854029Z","shell.execute_reply":"2023-12-13T04:55:16.858564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(\"/kaggle/input/UBC-OCEAN/train.csv\")\ndf_train","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:17.490781Z","iopub.execute_input":"2023-12-13T04:55:17.491457Z","iopub.status.idle":"2023-12-13T04:55:17.533979Z","shell.execute_reply.started":"2023-12-13T04:55:17.491424Z","shell.execute_reply":"2023-12-13T04:55:17.532834Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCDataset(Dataset):\n    def __init__(self, df, transforms=None):\n        self.df = df\n        self.file_names = df['file_path'].values\n        self.labels = df['label'].values\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = self.file_names[index]\n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        label = self.labels[index]\n        \n        if self.transforms:\n            img = self.transforms(image=img)[\"image\"]\n            \n        return {\n            'image': img,\n            'label': torch.tensor(label, dtype=torch.long)\n        }","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:17.785774Z","iopub.execute_input":"2023-12-13T04:55:17.786486Z","iopub.status.idle":"2023-12-13T04:55:17.793983Z","shell.execute_reply.started":"2023-12-13T04:55:17.78645Z","shell.execute_reply":"2023-12-13T04:55:17.793017Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_transforms = {\n    \"valid\": A.Compose([\n        A.Resize(CONFIG['img_size'], CONFIG['img_size']),\n        A.Normalize(\n                mean=[0.485, 0.456, 0.406], \n                std=[0.229, 0.224, 0.225], \n                max_pixel_value=255.0, \n                p=1.0\n            ),\n        ToTensorV2()], p=1.)\n}","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:17.937257Z","iopub.execute_input":"2023-12-13T04:55:17.938129Z","iopub.status.idle":"2023-12-13T04:55:17.943924Z","shell.execute_reply.started":"2023-12-13T04:55:17.938091Z","shell.execute_reply":"2023-12-13T04:55:17.942986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def set_seed(seed=42):\n    '''Sets the seed of the entire notebook so results are the same every time we run.\n    This is for REPRODUCIBILITY.'''\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    # When running on the CuDNN backend, two further options must be set\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\n    # Set a fixed value for the hash seed\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    \nset_seed(CONFIG['seed'])","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:18.092613Z","iopub.execute_input":"2023-12-13T04:55:18.093009Z","iopub.status.idle":"2023-12-13T04:55:18.10345Z","shell.execute_reply.started":"2023-12-13T04:55:18.092981Z","shell.execute_reply":"2023-12-13T04:55:18.102574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = '/kaggle/input/UBC-OCEAN'\nTEST_DIR = '/kaggle/input/UBC-OCEAN/test_thumbnails'\nALT_TEST_DIR = '/kaggle/input/UBC-OCEAN/test_images'\nLABEL_ENCODER_BIN = \"/kaggle/input/ubcpytorchwith-classweights-training-fold1of5/label_encoder.pkl\"\nBEST_WEIGHT = \"/kaggle/input/ubcpytorchwith-classweights-training-fold1of5/Acc0.66_Loss1.0244_epoch16.bin\"","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:18.241356Z","iopub.execute_input":"2023-12-13T04:55:18.241771Z","iopub.status.idle":"2023-12-13T04:55:18.246846Z","shell.execute_reply.started":"2023-12-13T04:55:18.241737Z","shell.execute_reply":"2023-12-13T04:55:18.245799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_test_file_path(image_id):\n    if os.path.exists(f\"{TEST_DIR}/{image_id}_thumbnail.png\"):\n        return f\"{TEST_DIR}/{image_id}_thumbnail.png\"\n    else:\n        return f\"{ALT_TEST_DIR}/{image_id}.png\"","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:18.364037Z","iopub.execute_input":"2023-12-13T04:55:18.364766Z","iopub.status.idle":"2023-12-13T04:55:18.369727Z","shell.execute_reply.started":"2023-12-13T04:55:18.364731Z","shell.execute_reply":"2023-12-13T04:55:18.368737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv(f\"{ROOT_DIR}/test.csv\")\ndf['file_path'] = df['image_id'].apply(get_test_file_path)\ndf['label'] = 0 # dummy\ndf","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:18.490963Z","iopub.execute_input":"2023-12-13T04:55:18.491688Z","iopub.status.idle":"2023-12-13T04:55:18.508937Z","shell.execute_reply.started":"2023-12-13T04:55:18.491651Z","shell.execute_reply":"2023-12-13T04:55:18.50782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub = pd.read_csv(f\"{ROOT_DIR}/sample_submission.csv\")\ndf_sub","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:18.642612Z","iopub.execute_input":"2023-12-13T04:55:18.643038Z","iopub.status.idle":"2023-12-13T04:55:18.658714Z","shell.execute_reply.started":"2023-12-13T04:55:18.643008Z","shell.execute_reply":"2023-12-13T04:55:18.657699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"encoder = joblib.load( LABEL_ENCODER_BIN )","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:18.967156Z","iopub.execute_input":"2023-12-13T04:55:18.967556Z","iopub.status.idle":"2023-12-13T04:55:18.975646Z","shell.execute_reply.started":"2023-12-13T04:55:18.9675Z","shell.execute_reply":"2023-12-13T04:55:18.974545Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCDataset(Dataset):\n    def __init__(self, df, transforms=None):\n        self.df = df\n        self.file_names = df['file_path'].values\n        self.labels = df['label'].values\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, index):\n        img_path = self.file_names[index]\n        img = cv2.imread(img_path)\n        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n        label = self.labels[index]\n        \n        if self.transforms:\n            img = self.transforms(image=img)[\"image\"]\n            \n        return {\n            'image': img,\n            'label': torch.tensor(label, dtype=torch.long)\n        }","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:19.173319Z","iopub.execute_input":"2023-12-13T04:55:19.173722Z","iopub.status.idle":"2023-12-13T04:55:19.181836Z","shell.execute_reply.started":"2023-12-13T04:55:19.173689Z","shell.execute_reply":"2023-12-13T04:55:19.180482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_transforms = {\n    \"valid\": A.Compose([\n        A.Resize(CONFIG['img_size'], CONFIG['img_size']),\n        A.Normalize(\n                mean=[0.485, 0.456, 0.406], \n                std=[0.229, 0.224, 0.225], \n                max_pixel_value=255.0, \n                p=1.0\n            ),\n        ToTensorV2()], p=1.)\n}","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:19.468985Z","iopub.execute_input":"2023-12-13T04:55:19.470038Z","iopub.status.idle":"2023-12-13T04:55:19.476251Z","shell.execute_reply.started":"2023-12-13T04:55:19.469996Z","shell.execute_reply":"2023-12-13T04:55:19.475186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class GeM(nn.Module):\n    def __init__(self, p=3, eps=1e-6):\n        super(GeM, self).__init__()\n        self.p = nn.Parameter(torch.ones(1)*p)\n        self.eps = eps\n\n    def forward(self, x):\n        return self.gem(x, p=self.p, eps=self.eps)\n        \n    def gem(self, x, p=3, eps=1e-6):\n        return F.avg_pool2d(x.clamp(min=eps).pow(p), (x.size(-2), x.size(-1))).pow(1./p)\n        \n    def __repr__(self):\n        return self.__class__.__name__ + \\\n                '(' + 'p=' + '{:.4f}'.format(self.p.data.tolist()[0]) + \\\n                ', ' + 'eps=' + str(self.eps) + ')'","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:19.736072Z","iopub.execute_input":"2023-12-13T04:55:19.736471Z","iopub.status.idle":"2023-12-13T04:55:19.7458Z","shell.execute_reply.started":"2023-12-13T04:55:19.736438Z","shell.execute_reply":"2023-12-13T04:55:19.744058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class UBCModel(nn.Module):\n    def __init__(self, model_name, num_classes, pretrained=False, checkpoint_path=None):\n        super(UBCModel, self).__init__()\n        self.model = timm.create_model(model_name, pretrained=pretrained)\n\n        in_features = self.model.classifier.in_features\n        self.model.classifier = nn.Identity()\n        self.model.global_pool = nn.Identity()\n        self.pooling = GeM()\n        self.linear = nn.Linear(in_features, num_classes)\n        self.softmax = nn.Softmax(dim=1)\n\n    def forward(self, images):\n        features = self.model(images)\n        pooled_features = self.pooling(features).flatten(1)\n        output = self.linear(pooled_features)\n        return output\n\n    \nmodel = UBCModel(CONFIG['model_name'], CONFIG['num_classes'])\nmodel.load_state_dict(torch.load( BEST_WEIGHT ))\nmodel.to(CONFIG['device']);","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:20.096643Z","iopub.execute_input":"2023-12-13T04:55:20.097547Z","iopub.status.idle":"2023-12-13T04:55:23.629355Z","shell.execute_reply.started":"2023-12-13T04:55:20.097491Z","shell.execute_reply":"2023-12-13T04:55:23.628492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = UBCDataset(df, transforms=data_transforms[\"valid\"])\ntest_loader = DataLoader(test_dataset, batch_size=CONFIG['valid_batch_size'], \n                          num_workers=2, shuffle=False, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:28.907647Z","iopub.execute_input":"2023-12-13T04:55:28.908046Z","iopub.status.idle":"2023-12-13T04:55:28.914022Z","shell.execute_reply.started":"2023-12-13T04:55:28.908011Z","shell.execute_reply":"2023-12-13T04:55:28.912917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = []\nwith torch.no_grad():\n    bar = tqdm(enumerate(test_loader), total=len(test_loader))\n    for step, data in bar:        \n        images = data['image'].to(CONFIG[\"device\"], dtype=torch.float)        \n        batch_size = images.size(0)\n        outputs = model(images)\n        _, predicted = torch.max(model.softmax(outputs), 1)\n        preds.append( predicted.detach().cpu().numpy() )\npreds = np.concatenate(preds).flatten()\npred_labels = encoder.inverse_transform( preds )","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:32.521325Z","iopub.execute_input":"2023-12-13T04:55:32.521733Z","iopub.status.idle":"2023-12-13T04:55:37.836582Z","shell.execute_reply.started":"2023-12-13T04:55:32.5217Z","shell.execute_reply":"2023-12-13T04:55:37.835271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub[\"label\"] = pred_labels\ndf_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:42.692873Z","iopub.execute_input":"2023-12-13T04:55:42.693721Z","iopub.status.idle":"2023-12-13T04:55:42.70375Z","shell.execute_reply.started":"2023-12-13T04:55:42.693681Z","shell.execute_reply":"2023-12-13T04:55:42.702629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub","metadata":{"execution":{"iopub.status.busy":"2023-12-13T04:55:45.475199Z","iopub.execute_input":"2023-12-13T04:55:45.476131Z","iopub.status.idle":"2023-12-13T04:55:45.486423Z","shell.execute_reply.started":"2023-12-13T04:55:45.476095Z","shell.execute_reply":"2023-12-13T04:55:45.485371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}