{"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":"","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !ls /kaggle/input/pyvips-python-and-deb-package\n# # intall the deb packages\n# !dpkg -i --force-depends /kaggle/input/pyvips-python-and-deb-package/linux_packages/archives/*.deb\n# # install the python wrapper\n# !pip install pyvips -f /kaggle/input/pyvips-python-and-deb-package/python_packages/ --no-index","metadata":{"execution":{"iopub.status.busy":"2023-11-02T07:39:25.242756Z","iopub.execute_input":"2023-11-02T07:39:25.243014Z","iopub.status.idle":"2023-11-02T07:39:25.248049Z","shell.execute_reply.started":"2023-11-02T07:39:25.24299Z","shell.execute_reply":"2023-11-02T07:39:25.247073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import timm\nimport torch\nfrom timm.models.layers import DropPath, trunc_normal_\nfrom timm.models import register_model\nfrom torch import nn\n# from einops import rearrange\nfrom functools import partial\nimport torch.nn.functional as F\n\n# Sklearn Imports\nfrom sklearn.preprocessing import LabelEncoder\nimport os\nimport gc\nimport cv2\nimport math\nimport copy\nimport time\nimport random\nfrom PIL import Image\n\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 torchvision\n\nimport torchvision.transforms as transforms\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\"\n\nCONFIG = {\n    \"seed\": 42,\n    \"epochs\": 30, #20\n    \"img_size\": 512,\n    \"model_name\": \"tf_efficientnet_b0_ns\",\n    \"checkpoint_path\" : \"/kaggle/input/tf-efficientnet/pytorch/tf-efficientnet-b0/1/tf_efficientnet_b0_aa-827b6e33.pth\",\n    \"num_classes\": 5,\n    \"train_batch_size\": 32,\n    \"valid_batch_size\": 64,\n    \"learning_rate\": 1e-4, #1e-4\n    \"scheduler\": 'CosineAnnealingLR',\n    \"min_lr\": 1e-6,\n    \"T_max\": 500,\n    \"weight_decay\": 1e-6,\n    \"fold\" : 0,\n    \"n_fold\": 5,\n    \"n_accumulate\": 1,\n    'tta':4,\n    \"device\": torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\"),\n}\ndef 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-11-02T13:43:48.338137Z","iopub.execute_input":"2023-11-02T13:43:48.338601Z","iopub.status.idle":"2023-11-02T13:43:48.359581Z","shell.execute_reply.started":"2023-11-02T13:43:48.338558Z","shell.execute_reply":"2023-11-02T13:43:48.358291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preprocesing","metadata":{}},{"cell_type":"code","source":"# DATASET_FOLDER = \"/kaggle/input/UBC-OCEAN/\"\n# IMAGES_FOLDER = \"/kaggle/temp/test_tiles\"\n\n# !mkdir -p /kaggle/temp/test_tiles","metadata":{"execution":{"iopub.status.busy":"2023-11-02T07:35:00.315518Z","iopub.execute_input":"2023-11-02T07:35:00.318111Z","iopub.status.idle":"2023-11-02T07:35:00.327024Z","shell.execute_reply.started":"2023-11-02T07:35:00.318033Z","shell.execute_reply":"2023-11-02T07:35:00.325595Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os\n# import pyvips\n# import numpy as np\n# import random\n\n# from PIL import Image\n\n# def extract_image_tiles(p_img, folder, size: int = 2048, scale: float = 0.5, drop_thr: float = 0.85) -> list:\n#     name, _ = os.path.splitext(os.path.basename(p_img))\n#     im = pyvips.Image.new_from_file(p_img)\n#     w = h = size\n#     # https://stackoverflow.com/a/47581978/4521646\n#     idxs = [(y, y + h, x, x + w) for y in range(0, im.height, h) for x in range(0, im.width, w)]\n#     files = []\n#     for k, (y, y_, x, x_) in enumerate(idxs):\n#         # https://libvips.github.io/pyvips/vimage.html#pyvips.Image.crop\n#         tile = im.crop(x, y, min(w, im.width - x), min(h, im.height - y)).numpy()[..., :3]\n#         mask_bg = np.sum(tile, axis=2) == 0\n#         if np.sum(mask_bg) >= (np.prod(mask_bg.shape) * drop_thr):\n#             #print(f\"skip almost empty tile: {k:06}_{int(x_ / w)}-{int(y_ / h)}\")\n#             continue\n#         if tile.shape[:2] != (h, w):\n#             tile_ = tile\n#             tile_size = (h, w) if tile.ndim == 2 else (h, w, tile.shape[2])\n#             tile = np.zeros(tile_size, dtype=tile.dtype)\n#             tile[:tile_.shape[0], :tile_.shape[1], ...] = tile_\n#         p_img = os.path.join(folder, f\"{k:06}_{int(x_ / w)}-{int(y_ / h)}.png\")\n#         # print(tile.shape, tile.dtype, tile.min(), tile.max())\n#         new_size = int(size * scale), int(size * scale)\n#         Image.fromarray(tile).resize(new_size, Image.LANCZOS).save(p_img)\n#         files.append(p_img)\n#     return files, idxs\n\n# def subsample_rand_prune(files: list, max_samples: float = 1.0):\n#     max_samples = max_samples if isinstance(max_samples, int) else int(len(files) * max_samples)\n#     random.shuffle(files)\n#     for p_img in files[max_samples:]:\n#         os.remove(p_img)\n        \n# def extract_prune_tiles(\n#     path_img: str, folder: str, size: int = 2048, scale: float = 0.25,\n#     drop_thr: float = 0.9, max_samples: float = 1.0) -> None:\n#     print(f\"processing: {path_img}\")\n#     name, _ = os.path.splitext(os.path.basename(path_img))\n#     folder = os.path.join(folder, name)\n#     os.makedirs(folder, exist_ok=True)\n#     tiles, _ = extract_image_tiles(path_img, folder, size, scale, drop_thr)\n#     subsample_rand_prune(tiles, max_samples)\n    \n# import glob\n# from tqdm.auto import tqdm\n# from joblib import Parallel, delayed\n\n# ls = sorted(glob.glob(os.path.join(DATASET_FOLDER, \"test_images\", '*.png')))\n# print(f\"found images: {len(ls)}\")\n# img_name = lambda p_img: os.path.splitext(os.path.basename(p_img))[0]\n    \n# _= Parallel(n_jobs=3)(\n#     delayed(extract_prune_tiles)\n#     (id_pimg, IMAGES_FOLDER, size=2048, drop_thr=0.85, scale=0.25)\n#     for id_pimg in tqdm(ls)\n# )","metadata":{"execution":{"iopub.status.busy":"2023-11-02T07:27:07.932888Z","iopub.execute_input":"2023-11-02T07:27:07.933324Z","iopub.status.idle":"2023-11-02T07:28:02.930707Z","shell.execute_reply.started":"2023-11-02T07:27:07.933288Z","shell.execute_reply":"2023-11-02T07:28:02.929093Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import os, glob\n# import pandas as pd\n# import numpy as np\n# import matplotlib.pyplot as plt\n# import glob\n# from PIL import Image\n# from tqdm.auto import tqdm\n# from joblib import Parallel, delayed\n\n# DATASET_FOLDER = \"/kaggle/input/UBC-OCEAN/\"\n# IMAGES_FOLDER = \"/kaggle/temp/test_thumbnails\"\n\n# !mkdir -p /kaggle/temp/test_thumbnails\n# # !cp /kaggle/input/UBC-OCEAN/test_thumbnails/*.png /kaggle/temp/test_thumbnails/","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:44:54.039693Z","iopub.execute_input":"2023-11-02T13:44:54.04015Z","iopub.status.idle":"2023-11-02T13:44:54.045825Z","shell.execute_reply.started":"2023-11-02T13:44:54.040106Z","shell.execute_reply":"2023-11-02T13:44:54.044614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# def prune_scale_image(img_path: str, out_dir: str, size: int = 1024) -> None:\n#     img = np.array(Image.open(img_path))\n#     img = prune_image_rows_cols(img)\n#     mask = np.sum(img[..., :3], axis=2) == 0\n#     img[mask, :] = 255\n#     img = Image.fromarray(img)\n#     img.thumbnail((size, size))\n#     img.save(os.path.join(out_dir, os.path.basename(img_path)))\n\n# def prune_image_rows_cols(img, thr=0.001):\n#     # delete empty columns\n#     for l in reversed(range(img.shape[1])):\n#         if (np.sum(img[:, l]) / float(img.shape[0])) < thr:\n#             img = np.delete(img, l, 1)\n#     # delete empty rows\n#     for l in reversed(range(img.shape[0])):\n#         if (np.sum(img[l, :]) / float(img.shape[1])) < thr:\n#             img = np.delete(img, l, 0)\n#     return img\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:44:58.146833Z","iopub.execute_input":"2023-11-02T13:44:58.147234Z","iopub.status.idle":"2023-11-02T13:44:58.153745Z","shell.execute_reply.started":"2023-11-02T13:44:58.147204Z","shell.execute_reply":"2023-11-02T13:44:58.152176Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ls = glob.glob(os.path.join('/kaggle/input/UBC-OCEAN/test_thumbnails', '*.png'))\n# print(f\"found images: {len(ls)}\")\n\n# ! mkdir -p test_thumbnails\n    \n# _= Parallel(n_jobs=4)(\n#     delayed(prune_scale_image)(p_img, IMAGES_FOLDER) for p_img in tqdm(ls)\n# )\n# ls = glob.glob(os.path.join(IMAGES_FOLDER, '*.png'))\n\n# print(f\"found images: {ls}\")","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:45:00.981224Z","iopub.execute_input":"2023-11-02T13:45:00.98165Z","iopub.status.idle":"2023-11-02T13:45:00.987423Z","shell.execute_reply.started":"2023-11-02T13:45:00.981615Z","shell.execute_reply":"2023-11-02T13:45:00.986195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ROOT_DIR = '/kaggle/input/UBC-OCEAN'\nTEST_DIR = '/kaggle/input/UBC-OCEAN/test_thumbnails'  #'/kaggle/temp/test_thumbnails'\n\n# LABEL_ENCODER_BIN = \"/kaggle/input/ubc-pytorch-cnn-training-fold1of5/label_encoder.pkl\"\n# BEST_WEIGHT = \"/kaggle/input/ubc-pytorch-cnn-training-fold1of5/Acc0.69_Loss0.9592_epoch20.bin\"\n\nLABEL_ENCODER_BIN = \"/kaggle/input/exp1-myresnet101/label_encoder.pkl\"\nBEST_WEIGHT = \"/kaggle/input/exp2-nextvit/Acc0.52_Loss1.3533_epoch29.bin\"\n\n# def get_evaluate_file_path(image_id):\n#     path=f\"{EVALUATION_DIR}/{image_id}.png\"\n#     if os.path.exists(path):\n#         return path\n\n# evaluate_df = pd.read_csv(f\"{ROOT_DIR}/train.csv\")\n# evaluate_df['file_path'] = evaluate_df['image_id'].apply(get_evaluate_file_path)\n# encoder = LabelEncoder()\n# evaluate_df['label'] = encoder.fit_transform(evaluate_df['label'])\n\n\ndef get_test_file_path(image_id):\n    return f\"{TEST_DIR}/{image_id}_thumbnail.png\"\n\ndf = pd.read_csv(f\"{ROOT_DIR}/test.csv\")\ndf['file_path'] = df['image_id'].apply(get_test_file_path)\ndf['label'] = 0 # dummy\n\nencoder = joblib.load( LABEL_ENCODER_BIN )\ndf_sub = pd.read_csv(f\"{ROOT_DIR}/sample_submission.csv\")\ndisplay(df_sub,df)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:45:08.561637Z","iopub.execute_input":"2023-11-02T13:45:08.562075Z","iopub.status.idle":"2023-11-02T13:45:08.592498Z","shell.execute_reply.started":"2023-11-02T13:45:08.56204Z","shell.execute_reply":"2023-11-02T13:45:08.591481Z"},"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        # Convert the NumPy array to a PIL Image\n        img = Image.fromarray(img)\n\n        \n        if self.transforms:\n            img = self.transforms(img)\n            \n            \n        return {\n            'image': img,\n            'label': torch.tensor(label, dtype=torch.long)\n        }\n    \n# transform = transforms.Compose([\n#     transforms.RandomHorizontalFlip(),\n#     transforms.RandomCrop(224, padding=4),\n#     transforms.ToTensor(), \n#     transforms.Normalize((0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)) # mean and std\n# ])\n# \n# mean = [0.48828688, 0.42932517, 0.49162089]\n# std = [0.41380908, 0.37492874, 0.41795654]\nmean = [0.8721593659261734, 0.7799686061900686, 0.8644588534918227]\nstd = [0.08258995918115268, 0.10991684444009092, 0.06839816226731532]\ntransform = transforms.Compose([\n#     transforms.Resize((300, 300)),\n    transforms.RandomCrop(224, padding=4),\n    transforms.ToTensor(),\n    transforms.Normalize(mean, std)  # Use your own calculated mean and std\n])\ntest_dataset = UBCDataset(df, transforms=transform) #data_transforms[\"valid\"]\ntest_loader = DataLoader(test_dataset, batch_size=CONFIG['valid_batch_size'], num_workers=2, shuffle=False, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:45:49.294901Z","iopub.execute_input":"2023-11-02T13:45:49.295298Z","iopub.status.idle":"2023-11-02T13:45:49.308846Z","shell.execute_reply.started":"2023-11-02T13:45:49.295268Z","shell.execute_reply":"2023-11-02T13:45:49.307367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"backbone_list= [\n    'tf_efficientnet_b4_ns',\n    'tf_efficientnet_b4_ns'\n    'swin_tiny_patch4_window7_224',\n    'tf_efficientnet_b3_ns',\n    'seresnext50_32x4d',\n    \"tf_efficientnet_b0_ns\",\n]\nbackbone=backbone_list[3]\nclass enet_v2(nn.Module):\n\n    def __init__(self, backbone, out_dim, pretrained=False):\n        super(enet_v2, self).__init__()\n        self.enet = timm.create_model(backbone, pretrained=pretrained)\n        in_ch = self.enet.classifier.in_features\n        self.myfc = nn.Sequential(nn.Dropout(0.5),nn.Linear(in_ch, out_dim))\n        self.enet.classifier = nn.Identity()\n\n    def forward(self, x):\n        x = self.enet(x)\n        x = self.myfc(x)\n        return x\nmodel=enet_v2('tf_efficientnet_b3_ns',out_dim=5).to('cpu') #CONFIG[\"device\"]\n# second_model=enet_v2('tf_efficientnet_b3_ns',out_dim=5).to(CONFIG[\"device\"])\n# third_model=enet_v2('tf_efficientnet_b4_ns',out_dim=5).to(CONFIG[\"device\"])\n# fourth_model=enet_v2('tf_efficientnet_b4_ns',out_dim=5).to(CONFIG[\"device\"])\nmodel.load_state_dict(torch.load('/kaggle/input/eff-net-b3/Acc0.73_Loss0.7607_epoch24.bin',map_location=torch.device('cpu')))\n# all_model.append(model.load_state_dict(torch.load('/kaggle/input/efficientnet-b4-dropout-0-5/Acc0.73_Loss0.8058_epoch3.bin')))\n# all_model.append(second_model.load_state_dict(torch.load('/kaggle/input/efficientnetb3/Acc0.69_Loss0.8949_epoch4.bin')))\n# all_model.append(third_model.load_state_dict(torch.load('/kaggle/input/efficientnet-b4-dropout-0-5/Acc0.70_Loss0.9211_epoch28.bin')))\n# all_model.append(fourth_model.load_state_dict(torch.load('/kaggle/input/efficientnet-b4-dropout-0-5/Acc0.71_Loss0.8757_epoch1.bin')))","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:46:02.837482Z","iopub.execute_input":"2023-11-02T13:46:02.83795Z","iopub.status.idle":"2023-11-02T13:46:03.241032Z","shell.execute_reply.started":"2023-11-02T13:46:02.837916Z","shell.execute_reply":"2023-11-02T13:46:03.239882Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Voting  \nIf ensemble         \nhttps://www.kaggle.com/code/capiru/cassavanet-inference-tta-easy-submission/notebook          ","metadata":{}},{"cell_type":"code","source":"# MODEL_PATH = '/kaggle/input/efficientnet-b4-dropout-0-5/'\n# os.listdir(MODEL_PATH)\n# MODEL_LIST = [0,1,2,3]\n# # ====================================================\n# # Model Loading\n# # ====================================================\n# models = []\n# count = 0\n# for model_fpath in os.listdir(MODEL_PATH):\n#     if count in MODEL_LIST:\n#         print(\"Model Loaded:\",model_fpath)\n#         model = enet_v2(backbone,out_dim=5,pretrained = False).to(CONFIG[\"device\"])\n#         info = torch.load(MODEL_PATH + model_fpath)\n#         model.load_state_dict(info)\n#         models.append(model)\n#     count+=1","metadata":{"execution":{"iopub.status.busy":"2023-10-31T17:55:54.85063Z","iopub.execute_input":"2023-10-31T17:55:54.851335Z","iopub.status.idle":"2023-10-31T17:55:59.54395Z","shell.execute_reply.started":"2023-10-31T17:55:54.8513Z","shell.execute_reply":"2023-10-31T17:55:59.543078Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"####### TTA HELPER FUNCTION\n\n'''\nBorrowed from https://github.com/haqishen/SIIM-ISIC-Melanoma-Classification-1st-Place-Solution\n'''\n\ndef get_tta_flips(img, i):\n\n    if i >= 4:\n        img = img.transpose(2, 3)\n    if i % 4 == 0:\n        return img\n    elif i % 4 == 1:\n        return img.flip(2)\n    elif i % 4 == 2:\n        return img.flip(3)\n    elif i % 4 == 3:\n        return img.flip(2).flip(3)\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:46:08.161502Z","iopub.execute_input":"2023-11-02T13:46:08.161976Z","iopub.status.idle":"2023-11-02T13:46:08.169578Z","shell.execute_reply.started":"2023-11-02T13:46:08.161937Z","shell.execute_reply":"2023-11-02T13:46:08.168189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Single model","metadata":{}},{"cell_type":"code","source":"preds = []\n# probs placeholder\nPROBS = []\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        # preds placeholders\n        probs = torch.zeros((images.shape[0], 5), device = CONFIG[\"device\"])\n        # inference with TTA\n        for tta_idx in range(CONFIG['tta']): \n            preds  = model(get_tta_flips(images, tta_idx))\n            probs += preds.softmax(axis = 1)\n    PROBS.append(probs.detach().cpu() / CONFIG['tta'])\n    # transform predictions\n    PROBS = torch.cat(PROBS).numpy()\n    preds = np.concatenate(PROBS).argmax().flatten()\n    pred_labels = encoder.inverse_transform(preds)\n#     pred_labels = encoder.inverse_transform(preds)\n#     print(preds)\n#         outputs2 = second_model(images)\n#         outputs3=third_model(images)\n#         outputs4=fourth_model(images)\n#         output=(nn.Softmax(dim=1)(outputs) + nn.Softmax(dim=1)(outputs2)+ nn.Softmax(dim=1)(outputs3)+  nn.Softmax(dim=1)(outputs4))/4\n        \n#         print((nn.Softmax(dim=1)(outputs1)+nn.Softmax(dim=1)(outputs2))/2)\n#         print(nn.Softmax(dim=1)(outputs))\n#         _, predicted = torch.max(nn.Softmax(dim=1)(output), 1)\n#         preds.append(predicted.detach().cpu().numpy())\n# preds = np.concatenate(preds).flatten()\n# pred_labels = encoder.inverse_transform(preds)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:48:30.86482Z","iopub.execute_input":"2023-11-02T13:48:30.865222Z","iopub.status.idle":"2023-11-02T13:48:31.86979Z","shell.execute_reply.started":"2023-11-02T13:48:30.86519Z","shell.execute_reply":"2023-11-02T13:48:31.868308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# GradCam visualize Result\nhttps://www.kaggle.com/code/steamedsheep/hpa-gradcam-of-dual-head-model            ","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !pip install grad-cam\n# !pip install ttach","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:46:39.84734Z","iopub.execute_input":"2023-11-02T13:46:39.847802Z","iopub.status.idle":"2023-11-02T13:46:39.85306Z","shell.execute_reply.started":"2023-11-02T13:46:39.847761Z","shell.execute_reply":"2023-11-02T13:46:39.851851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from pytorch_grad_cam import GradCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM\n# from pytorch_grad_cam.utils.image import show_cam_on_image","metadata":{"execution":{"iopub.status.busy":"2023-11-02T09:55:05.453012Z","iopub.execute_input":"2023-11-02T09:55:05.453468Z","iopub.status.idle":"2023-11-02T09:55:05.459587Z","shell.execute_reply.started":"2023-11-02T09:55:05.453435Z","shell.execute_reply":"2023-11-02T09:55:05.457836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.enet.conv_head","metadata":{"execution":{"iopub.status.busy":"2023-11-02T10:33:37.217448Z","iopub.execute_input":"2023-11-02T10:33:37.218365Z","iopub.status.idle":"2023-11-02T10:33:37.22457Z","shell.execute_reply.started":"2023-11-02T10:33:37.218324Z","shell.execute_reply":"2023-11-02T10:33:37.223414Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# model.enet.conv_head\n# cam = GradCAM(model, model.enet.classifier, use_cuda=False) #target_layer= use_cuda=","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:46:56.6067Z","iopub.execute_input":"2023-11-02T13:46:56.607108Z","iopub.status.idle":"2023-11-02T13:46:56.612117Z","shell.execute_reply.started":"2023-11-02T13:46:56.607077Z","shell.execute_reply":"2023-11-02T13:46:56.610724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# cls_cams = {}\n# with 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#         for cat in range(5):\n#             grayscale_cam = cam(input_tensor=images, target_category=cat)\n#             cls_cams[cat] = grayscale_cam[0, :]\n","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:47:00.849078Z","iopub.execute_input":"2023-11-02T13:47:00.849574Z","iopub.status.idle":"2023-11-02T13:47:00.854441Z","shell.execute_reply.started":"2023-11-02T13:47:00.849539Z","shell.execute_reply":"2023-11-02T13:47:00.853573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# f, ax = plt.subplots(4, 5, figsize=(20, 20))\n# for i in range(5):\n#     ax[i//5][i%5].imshow(full_img[0].numpy()[:, :, :3])\n#     ax[i//5][i%5].imshow(cls_cams[i], alpha=0.3)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Ensemble\nhttps://www.kaggle.com/code/shubham108/ensemble-visiontransformet-vit-efficientnet-5f#Combine                  ","metadata":{}},{"cell_type":"code","source":"# def inference (model, data_loader, device):\n#     preds = []\n#     model.to(device)\n#     model.eval()\n#     bar = tqdm(enumerate(test_loader), total=len(test_loader))\n# #     for step, data in bar:\n#     for images in test_tqdm:\n#         images = data['image'].to(CONFIG[\"device\"], dtype=torch.float)        \n# #         batch_size = images.size(0)\n#         preds.extend(model(images).detach().cpu().numpy())\n#     return preds","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# effnet_preds = []\n# for effnet_model_name in os.listdir(MODEL_PATH):\n#     effnet_model = enet_v2('tf_efficientnet_b3_ns', 5, pretrained=False).to(CONFIG[\"device\"])\n#     ckpt = torch.load('/kaggle/input/efficientnet-b4-dropout-0-5/'+ effnet_model_name)\n#     effnet_model.load_state_dict(ckpt)\n","metadata":{"execution":{"iopub.status.busy":"2023-10-31T18:12:50.552702Z","iopub.execute_input":"2023-10-31T18:12:50.553604Z","iopub.status.idle":"2023-10-31T18:12:50.557603Z","shell.execute_reply.started":"2023-10-31T18:12:50.553571Z","shell.execute_reply":"2023-10-31T18:12:50.556623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# effnet_preds = []\n# for effnet_model_name in CFG['effnet_models']:\n#     print(\"Model: \", effnet_model_name)\n#     effnet_model = CassavaImageClassifier('tf_efficientnet_b3_ns', 5, pretrained=False)\n#     ckpt = torch.load('/kaggle/input/cassava-efnet-5folds/'+effnet_model_name, map_location=torch.device(CFG['device']))\n#     effnet_model.load_state_dict(ckpt['model'])\n#     with torch.no_grad():\n#         for i in range(CFG['tta']):\n#             effnet_preds += [inference(effnet_model, effnet_test_loader, CFG['device'])]\n# effnet_preds = np.mean(effnet_preds, axis=0)","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Evaluation method","metadata":{}},{"cell_type":"code","source":"# eval_preds = []\n# eval_labels = []\n# evaluate_dataset = UBCDataset(evaluate_df, transforms=transform) #data_transforms[\"valid\"]\n# evaluate_loader = DataLoader(evaluate_dataset, batch_size=CONFIG['valid_batch_size'], num_workers=2, shuffle=False, pin_memory=True)\n\n# # # Assuming val_loader is your validation DataLoader\n# with torch.no_grad():\n#     for step, data in enumerate(evaluate_loader):        \n#         images = data['image'].to(CONFIG[\"device\"], dtype=torch.float)\n#         labels = data['label'].to(CONFIG[\"device\"], dtype=torch.long)\n#         output = model(images)\n#         pred = torch.argmax(output, dim=1)  # For multi-class classification\n#         eval_preds.extend(pred.cpu().numpy())\n#         eval_labels.extend(labels.cpu().numpy())\n\n# preds = torch.tensor(eval_preds)\n# labels = torch.tensor(eval_labels).long()\n\n# acc = torchmetrics.Accuracy(num_classes=5, task='multiclass')(preds, labels)\n# precision = torchmetrics.Precision(average='macro', num_classes=5, task='multiclass')(preds, labels)\n# recall = torchmetrics.Recall(average='macro', num_classes=5, task='multiclass')(preds, labels)\n# cm = torchmetrics.ConfusionMatrix(num_classes=5, task='multiclass')(preds, labels)\n\n# print(f'Val Accuracy {acc}')\n# print(f'Val Precision {precision}')\n# print(f'Val Recall {recall}')\n# print(f'Val ConfusionMatrix {cm}')","metadata":{"execution":{"iopub.status.busy":"2023-11-01T09:51:17.584447Z","iopub.execute_input":"2023-11-01T09:51:17.584804Z","iopub.status.idle":"2023-11-01T09:51:17.590453Z","shell.execute_reply.started":"2023-11-01T09:51:17.584776Z","shell.execute_reply":"2023-11-01T09:51:17.589432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submission","metadata":{}},{"cell_type":"code","source":"df_sub[\"label\"] = pred_labels\ndf_sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:47:09.548702Z","iopub.execute_input":"2023-11-02T13:47:09.549088Z","iopub.status.idle":"2023-11-02T13:47:09.561759Z","shell.execute_reply.started":"2023-11-02T13:47:09.549058Z","shell.execute_reply":"2023-11-02T13:47:09.560682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_sub","metadata":{"execution":{"iopub.status.busy":"2023-11-02T13:47:11.21723Z","iopub.execute_input":"2023-11-02T13:47:11.217644Z","iopub.status.idle":"2023-11-02T13:47:11.228544Z","shell.execute_reply.started":"2023-11-02T13:47:11.217611Z","shell.execute_reply":"2023-11-02T13:47:11.22715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}