{"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\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","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#collapse-hide\n\n##### PACKAGES\n\nimport numpy as np\nimport pandas as pd\n\nimport torch\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader\n\n!pip install timm\nimport timm\n\nimport albumentations as A\nfrom albumentations.pytorch import ToTensorV2\n\nimport cv2\nimport os\n\ndevice = torch.device('cuda')","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:32:32.606936Z","iopub.execute_input":"2023-08-17T09:32:32.607466Z","iopub.status.idle":"2023-08-17T09:32:53.181959Z","shell.execute_reply.started":"2023-08-17T09:32:32.607438Z","shell.execute_reply":"2023-08-17T09:32:53.180795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#collapse-hide\n\n##### DATASET\n\nclass ImageData(Dataset):\n    \n    # init\n    def __init__(self, \n                 data, \n                 directory, \n                 transform):\n        self.data      = data\n        self.directory = directory\n        self.transform = transform\n        \n    # length\n    def __len__(self):\n        return len(self.data)\n    \n    # get item  \n    def __getitem__(self, idx):\n        \n        # import\n        image = cv2.imread(os.path.join(self.directory, self.data.iloc[idx]['image_id']))\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n            \n        # augmentations\n        image = self.transform(image = image)['image']\n        \n        return image","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:33:05.731544Z","iopub.execute_input":"2023-08-17T09:33:05.731914Z","iopub.status.idle":"2023-08-17T09:33:05.740177Z","shell.execute_reply.started":"2023-08-17T09:33:05.731874Z","shell.execute_reply":"2023-08-17T09:33:05.739039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#collapse-show\n\n##### DATA LOADER\n\n# import data\ndf = pd.read_csv('../input/cassava-leaf-disease-classification/train.csv')\ndisplay(df.head())\n\n# augmentations\ntransforms = A.Compose([A.Resize(height = 128, width = 128),\n                        A.Normalize(),\n                        ToTensorV2()])\n\n# dataset\ndata_set = ImageData(data      = df, \n                     directory = '../input/cassava-leaf-disease-classification/train_images/',\n                     transform = transforms)\n\n# dataloader\ndata_loader = DataLoader(data_set, \n                         batch_size  = 32, \n                         shuffle     = False, \n                         num_workers = 2)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:33:43.618819Z","iopub.execute_input":"2023-08-17T09:33:43.619220Z","iopub.status.idle":"2023-08-17T09:33:43.669323Z","shell.execute_reply.started":"2023-08-17T09:33:43.619189Z","shell.execute_reply":"2023-08-17T09:33:43.668301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### DEFINE MODEL\n\nmodel    = timm.create_model(model_name = 'resnet18', pretrained = True)\nmodel.fc = nn.Linear(512, 2)\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:34:09.189242Z","iopub.execute_input":"2023-08-17T09:34:09.189607Z","iopub.status.idle":"2023-08-17T09:34:13.294914Z","shell.execute_reply.started":"2023-08-17T09:34:09.189575Z","shell.execute_reply":"2023-08-17T09:34:13.293935Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### HELPER FUNCTION FOR FEATURE EXTRACTION\n\ndef get_features(name):\n    def hook(model, input, output):\n        features[name] = output.detach()\n    return hook","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:34:21.313726Z","iopub.execute_input":"2023-08-17T09:34:21.314115Z","iopub.status.idle":"2023-08-17T09:34:21.319744Z","shell.execute_reply.started":"2023-08-17T09:34:21.314083Z","shell.execute_reply":"2023-08-17T09:34:21.318480Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### REGISTER HOOK\n\nmodel.global_pool.register_forward_hook(get_features('feats'))","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:34:30.992767Z","iopub.execute_input":"2023-08-17T09:34:30.993158Z","iopub.status.idle":"2023-08-17T09:34:31.001999Z","shell.execute_reply.started":"2023-08-17T09:34:30.993127Z","shell.execute_reply":"2023-08-17T09:34:31.001062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### FEATURE EXTRACTION LOOP\n\n# placeholders\nPREDS = []\nFEATS = []\n\n# placeholder for batch features\nfeatures = {}\n\n# loop through batches\nfor idx, inputs in enumerate(data_loader):\n\n    # move to device\n    inputs = inputs.to(device)\n       \n    # forward pass [with feature extraction]\n    preds = model(inputs)\n    \n    # add feats and preds to lists\n    PREDS.append(preds.detach().cpu().numpy())\n    FEATS.append(features['feats'].cpu().numpy())\n\n    # early stop\n    if idx == 9:\n        break","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:34:43.922644Z","iopub.execute_input":"2023-08-17T09:34:43.923027Z","iopub.status.idle":"2023-08-17T09:34:54.032070Z","shell.execute_reply.started":"2023-08-17T09:34:43.922975Z","shell.execute_reply":"2023-08-17T09:34:54.030792Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"##### INSPECT FEATURES\n\nPREDS = np.concatenate(PREDS)\nFEATS = np.concatenate(FEATS)\n\nprint('- preds shape:', PREDS.shape)\nprint('- feats shape:', FEATS.shape)","metadata":{"execution":{"iopub.status.busy":"2023-08-17T09:35:00.798351Z","iopub.execute_input":"2023-08-17T09:35:00.798734Z","iopub.status.idle":"2023-08-17T09:35:00.807032Z","shell.execute_reply.started":"2023-08-17T09:35:00.798699Z","shell.execute_reply":"2023-08-17T09:35:00.805776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}