{"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":"import os\nimport random\nimport glob\nimport pandas as pd\nimport numpy as np\nimport pydicom \n\n\nimport torch\nimport torch.optim as optim\nfrom albumentations import Compose, ShiftScaleRotate, Resize, Normalize, HorizontalFlip, RandomBrightnessContrast\nfrom albumentations.pytorch import ToTensorV2\nfrom torch.utils.data import Dataset,Subset\n\nfrom sklearn.metrics import roc_auc_score, f1_score, accuracy_score, jaccard_score, confusion_matrix\nfrom tqdm import notebook as tqdm","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:42:47.429496Z","iopub.execute_input":"2022-05-21T09:42:47.429872Z","iopub.status.idle":"2022-05-21T09:42:47.451758Z","shell.execute_reply.started":"2022-05-21T09:42:47.429811Z","shell.execute_reply":"2022-05-21T09:42:47.450306Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"INPUT_PATH = '../input/rsna-intracranial-hemorrhage-detection/rsna-intracranial-hemorrhage-detection/'","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.433773Z","iopub.execute_input":"2022-05-21T08:23:58.434129Z","iopub.status.idle":"2022-05-21T08:23:58.439179Z","shell.execute_reply.started":"2022-05-21T08:23:58.434084Z","shell.execute_reply":"2022-05-21T08:23:58.438195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"os.listdir(INPUT_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.441152Z","iopub.execute_input":"2022-05-21T08:23:58.441824Z","iopub.status.idle":"2022-05-21T08:23:58.461214Z","shell.execute_reply.started":"2022-05-21T08:23:58.441767Z","shell.execute_reply":"2022-05-21T08:23:58.459267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Parameters\nn_classes = 6\nn_epochs = 1\nbatch_size = 32","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.46483Z","iopub.execute_input":"2022-05-21T08:23:58.465064Z","iopub.status.idle":"2022-05-21T08:23:58.469589Z","shell.execute_reply.started":"2022-05-21T08:23:58.465018Z","shell.execute_reply":"2022-05-21T08:23:58.468481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"COLS = ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'any']","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.471407Z","iopub.execute_input":"2022-05-21T08:23:58.472208Z","iopub.status.idle":"2022-05-21T08:23:58.48146Z","shell.execute_reply.started":"2022-05-21T08:23:58.472159Z","shell.execute_reply":"2022-05-21T08:23:58.480115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_images_dir = os.path.join(INPUT_PATH, 'stage_2_train/')\ntest_images_dir = os.path.join(INPUT_PATH, 'stage_2_test/')","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.482787Z","iopub.execute_input":"2022-05-21T08:23:58.483203Z","iopub.status.idle":"2022-05-21T08:23:58.493574Z","shell.execute_reply.started":"2022-05-21T08:23:58.483157Z","shell.execute_reply":"2022-05-21T08:23:58.492624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_metadata_csv = '../input/rsna-stage-2-metadata-ihd-2019/stage_2_train_with_metadata.csv'\n# test_metadata_csv = '../input/rsna-stage-2-metadata-ihd-2019/stage_2_test_with_metadata.csv'\ntrain_metadata_csv = '../input/rsna-intracranial-sequence-metadata/train_metadata_noidx.csv'\ntest_metadata_csv = '../input/rsna-intracranial-sequence-metadata/test_metadata_noidx.csv'","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.49545Z","iopub.execute_input":"2022-05-21T08:23:58.495937Z","iopub.status.idle":"2022-05-21T08:23:58.505099Z","shell.execute_reply.started":"2022-05-21T08:23:58.495893Z","shell.execute_reply":"2022-05-21T08:23:58.503987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata = pd.read_csv(train_metadata_csv)\ntrain_metadata.columns","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:23:58.506592Z","iopub.execute_input":"2022-05-21T08:23:58.508014Z","iopub.status.idle":"2022-05-21T08:24:00.252174Z","shell.execute_reply.started":"2022-05-21T08:23:58.507975Z","shell.execute_reply":"2022-05-21T08:24:00.25128Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(os.path.join(INPUT_PATH, 'stage_2_train.csv'))\ntest = pd.read_csv(os.path.join(INPUT_PATH, 'stage_2_sample_submission.csv'))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:00.2542Z","iopub.execute_input":"2022-05-21T08:24:00.254503Z","iopub.status.idle":"2022-05-21T08:24:04.157976Z","shell.execute_reply.started":"2022-05-21T08:24:00.254461Z","shell.execute_reply":"2022-05-21T08:24:04.157003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Preprocessing : Windowing","metadata":{}},{"cell_type":"code","source":"## A function to correct pixel data and rescale intercercepts ob 12 bit images\ndef dcm_correction(dcm_img):\n        x = dcm_img.pixel_array + 1000\n        px_mode = 4096\n        x[x >= px_mode] = x[x >= px_mode] - px_mode #if there are extra bits in 12-bit grayscale(<=4096)\n        dcm_img.PixelData = x.tobytes()\n        dcm_img.RescaleIntercept = -1000 #setting a common value across all 12-bit US images\n        \n#Systemic/linear windowing\ndef window_image(dcm, window_center, window_width):\n    if (dcm.BitsStored == 12) and (dcm.PixelRepresentation == 0) and (int(dcm.RescaleIntercept) > -100):\n        dcm_correction(dcm)\n\n    img = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept #reconstructing the image from pixels\n    img_min = window_center - window_width // 2 #lowest visible value\n    img_max = window_center + window_width // 2 #highest visible value\n    img = np.clip(img, img_min, img_max)\n\n    return img\n\n#Combining all\ndef bsb_window(dcm):\n    brain_img = window_image(dcm, 40, 80)\n    subdural_img = window_image(dcm, 80, 200)\n    soft_img = window_image(dcm, 40, 380)\n\n    brain_img = (brain_img - 0) / 80\n    subdural_img = (subdural_img - (-20)) / 200\n    soft_img = (soft_img - (-150)) / 380\n    bsb_img = np.array([brain_img, subdural_img, soft_img]).transpose(1, 2, 0)\n\n    return bsb_img","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:04.162598Z","iopub.execute_input":"2022-05-21T08:24:04.162859Z","iopub.status.idle":"2022-05-21T08:24:04.174334Z","shell.execute_reply.started":"2022-05-21T08:24:04.162812Z","shell.execute_reply":"2022-05-21T08:24:04.173204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class IntracranialDataset(Dataset):\n\n    def __init__(self, csv_file, path, labels, transform=None):\n        self.path = path\n        self.data = pd.read_csv(csv_file)\n        self.transform = transform\n        self.labels = labels\n\n    def __len__(self):\n        return len(self.data)\n\n    def __getitem__(self, idx):\n#         img_name = os.path.join(self.path, self.data.loc[idx, 'Image'] + '.png')\n#         img = cv2.imread(img_name)   \n        try:\n            dicom = pydicom.dcmread(self.path, self.data.loc[idx, 'Image'] + '.dcm')\n            img = bsb_window(dicom)\n        except:\n            img = np.zeros((512, 512, 3))\n        \n        if self.transform:       \n            augmented = self.transform(image=img)\n            img = augmented['image']   \n            \n        if self.labels:\n            \n            labels = torch.tensor(\n                self.data.loc[idx, ['epidural', 'intraparenchymal', 'intraventricular', 'subarachnoid', 'subdural', 'any']])\n            return {'image': img, 'labels': labels}    \n        \n        else:      \n            \n            return {'image': img}","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:04.175996Z","iopub.execute_input":"2022-05-21T08:24:04.176681Z","iopub.status.idle":"2022-05-21T08:24:04.191686Z","shell.execute_reply.started":"2022-05-21T08:24:04.176637Z","shell.execute_reply":"2022-05-21T08:24:04.190548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.value_counts()","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:24:04.193226Z","iopub.execute_input":"2022-05-21T08:24:04.193908Z","iopub.status.idle":"2022-05-21T08:24:20.794357Z","shell.execute_reply.started":"2022-05-21T08:24:04.193854Z","shell.execute_reply":"2022-05-21T08:24:20.793152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_metadata = pd.read_csv(train_metadata_csv)\ntest_metadata = pd.read_csv(test_metadata_csv)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:20.796238Z","iopub.execute_input":"2022-05-21T08:24:20.796565Z","iopub.status.idle":"2022-05-21T08:24:22.820578Z","shell.execute_reply.started":"2022-05-21T08:24:20.796521Z","shell.execute_reply":"2022-05-21T08:24:22.817047Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_metadata[['ID', 'Image', 'Diagnosis']] = train_metadata['ID'].str.split('_', expand=True)\n#train_metadata['ImageID'] = 'ID_' + train_metadata['Image']\n#train_metadata.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:22.822478Z","iopub.execute_input":"2022-05-21T08:24:22.822766Z","iopub.status.idle":"2022-05-21T08:24:22.837522Z","shell.execute_reply.started":"2022-05-21T08:24:22.822725Z","shell.execute_reply":"2022-05-21T08:24:22.83503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_metadata.drop(['ID', 'Diagnosis'], axis=1)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:24:22.84115Z","iopub.execute_input":"2022-05-21T08:24:22.842266Z","iopub.status.idle":"2022-05-21T08:24:22.8496Z","shell.execute_reply.started":"2022-05-21T08:24:22.842218Z","shell.execute_reply":"2022-05-21T08:24:22.848296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train_metadata[:15]","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-21T08:24:22.856092Z","iopub.execute_input":"2022-05-21T08:24:22.860016Z","iopub.status.idle":"2022-05-21T08:24:22.866317Z","shell.execute_reply.started":"2022-05-21T08:24:22.859968Z","shell.execute_reply":"2022-05-21T08:24:22.864553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Preparation","metadata":{}},{"cell_type":"code","source":"# Prepare train table\ntrain[['ID', 'Image', 'Diagnosis']] = train['ID'].str.split('_', expand=True)\ntrain = train[['Image', 'Diagnosis', 'Label']]\ntrain.drop_duplicates(inplace=True)\ntrain = train.pivot(index='Image', columns='Diagnosis', values='Label').reset_index()\ntrain['Image'] = 'ID_' + train['Image']","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:22.874558Z","iopub.execute_input":"2022-05-21T08:24:22.875591Z","iopub.status.idle":"2022-05-21T08:24:50.823013Z","shell.execute_reply.started":"2022-05-21T08:24:22.875503Z","shell.execute_reply":"2022-05-21T08:24:50.821991Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove invalid instances of images(dcm, PNG)\npng = glob.glob(os.path.join(train_images_dir, '*.dcm'))\npng = [os.path.basename(png)[:-4] for png in png]\npng = np.array(png)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:50.824607Z","iopub.execute_input":"2022-05-21T08:24:50.825202Z","iopub.status.idle":"2022-05-21T08:24:55.309395Z","shell.execute_reply.started":"2022-05-21T08:24:50.825159Z","shell.execute_reply":"2022-05-21T08:24:55.308287Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train[train['Image'].isin(png)]\n# train.to_csv('train.csv', index=False)\ntrain.value_counts()","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:24:55.311142Z","iopub.execute_input":"2022-05-21T08:24:55.311478Z","iopub.status.idle":"2022-05-21T08:24:57.678526Z","shell.execute_reply.started":"2022-05-21T08:24:55.311439Z","shell.execute_reply":"2022-05-21T08:24:57.67755Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_train = pd.merge(left=train, right=train_metadata, how='left', left_on='Image', right_on='ImageId')","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:57.680961Z","iopub.execute_input":"2022-05-21T08:24:57.681343Z","iopub.status.idle":"2022-05-21T08:24:59.119947Z","shell.execute_reply.started":"2022-05-21T08:24:57.681298Z","shell.execute_reply":"2022-05-21T08:24:59.118894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"merged_train","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:24:59.121675Z","iopub.execute_input":"2022-05-21T08:24:59.122108Z","iopub.status.idle":"2022-05-21T08:24:59.150109Z","shell.execute_reply.started":"2022-05-21T08:24:59.122034Z","shell.execute_reply":"2022-05-21T08:24:59.149029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Train/valid split \ntrain_series = train_metadata['SeriesInstanceUID'].unique() #Identifying unique scans by SeriesID: there are totally \n\nvalid_series = train_series[21000:]\ntrain_series = train_series[:21000]","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:56:35.682296Z","iopub.execute_input":"2022-05-21T09:56:35.682655Z","iopub.status.idle":"2022-05-21T09:56:35.783342Z","shell.execute_reply.started":"2022-05-21T09:56:35.682623Z","shell.execute_reply":"2022-05-21T09:56:35.78229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_series))\nprint(len(valid_series))","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-21T09:56:38.598354Z","iopub.execute_input":"2022-05-21T09:56:38.598666Z","iopub.status.idle":"2022-05-21T09:56:38.606792Z","shell.execute_reply.started":"2022-05-21T09:56:38.598634Z","shell.execute_reply":"2022-05-21T09:56:38.605516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_series","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:56:47.19364Z","iopub.execute_input":"2022-05-21T09:56:47.201402Z","iopub.status.idle":"2022-05-21T09:56:47.212761Z","shell.execute_reply.started":"2022-05-21T09:56:47.201316Z","shell.execute_reply":"2022-05-21T09:56:47.211625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = merged_train[merged_train['SeriesInstanceUID'].isin(train_series)]\ntrain_df","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T09:56:50.367459Z","iopub.execute_input":"2022-05-21T09:56:50.367768Z","iopub.status.idle":"2022-05-21T09:56:50.681398Z","shell.execute_reply.started":"2022-05-21T09:56:50.367737Z","shell.execute_reply":"2022-05-21T09:56:50.680336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"valid_df = merged_train[merged_train['SeriesInstanceUID'].isin(valid_series)]\nvalid_df","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T09:56:54.939353Z","iopub.execute_input":"2022-05-21T09:56:54.939654Z","iopub.status.idle":"2022-05-21T09:56:55.074944Z","shell.execute_reply.started":"2022-05-21T09:56:54.939623Z","shell.execute_reply":"2022-05-21T09:56:55.073703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(train_df))\nprint(len(valid_df))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:56:58.289115Z","iopub.execute_input":"2022-05-21T09:56:58.289945Z","iopub.status.idle":"2022-05-21T09:56:58.296983Z","shell.execute_reply.started":"2022-05-21T09:56:58.289908Z","shell.execute_reply":"2022-05-21T09:56:58.295784Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"So the training set has 728,513 slices and a validation set 24,290 slices","metadata":{}},{"cell_type":"code","source":"os.mkdir('./data')","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-21T08:25:00.184478Z","iopub.execute_input":"2022-05-21T08:25:00.185185Z","iopub.status.idle":"2022-05-21T08:25:00.376747Z","shell.execute_reply.started":"2022-05-21T08:25:00.18515Z","shell.execute_reply":"2022-05-21T08:25:00.374874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.to_csv('data/train.csv', index=False)\nprint(train_df['any'].value_counts())\nvalid_df.to_csv('data/valid.csv', index=False)\nprint(valid_df['any'].value_counts())","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:57:03.327872Z","iopub.execute_input":"2022-05-21T09:57:03.328393Z","iopub.status.idle":"2022-05-21T09:57:09.366248Z","shell.execute_reply.started":"2022-05-21T09:57:03.328329Z","shell.execute_reply":"2022-05-21T09:57:09.365094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Prepare test table\ntest[['ID', 'Image', 'Diagnosis']] = test['ID'].str.split('_', expand=True)\ntest['Image'] = 'ID_' + test['Image']\ntest = test[['Image', 'Label']]\ntest.drop_duplicates(inplace=True)\n\ntest.to_csv('data/test.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:25:28.631847Z","iopub.execute_input":"2022-05-21T08:25:28.632909Z","iopub.status.idle":"2022-05-21T08:25:31.44231Z","shell.execute_reply.started":"2022-05-21T08:25:28.63286Z","shell.execute_reply":"2022-05-21T08:25:31.440972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data loaders\ntransform_train = Compose([Resize(256, 256),\n                           Normalize(mean=[0.1738, 0.1433, 0.1970], std=[0.3161, 0.2850, 0.3111], max_pixel_value=1.),\n                           HorizontalFlip(),\n                           ShiftScaleRotate(),\n                           RandomBrightnessContrast(),\n                           ToTensorV2()])\n\ntransform_test = Compose([Resize(256, 256),\n                          Normalize(mean=[0.1738, 0.1433, 0.1970], std=[0.3161, 0.2850, 0.3111], max_pixel_value=1.),\n                          ToTensorV2()])\n","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:57:18.806232Z","iopub.execute_input":"2022-05-21T09:57:18.806532Z","iopub.status.idle":"2022-05-21T09:57:18.814709Z","shell.execute_reply.started":"2022-05-21T09:57:18.8065Z","shell.execute_reply":"2022-05-21T09:57:18.813431Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = IntracranialDataset(\n    csv_file='data/train.csv', path=train_images_dir, transform=transform_train, labels=True)\nprint(len(train_dataset))\n\nvalid_dataset = IntracranialDataset(\n    csv_file='data/valid.csv', path=train_images_dir, transform=transform_train, labels=True)\nprint(len(valid_dataset))\n\n","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:58:09.672959Z","iopub.execute_input":"2022-05-21T09:58:09.673634Z","iopub.status.idle":"2022-05-21T09:58:12.038239Z","shell.execute_reply.started":"2022-05-21T09:58:09.673597Z","shell.execute_reply":"2022-05-21T09:58:12.036133Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = IntracranialDataset(\n    csv_file='data/test.csv', path=test_images_dir, transform=transform_test, labels=False)\n# print(len(test_dataset))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:25:47.602478Z","iopub.execute_input":"2022-05-21T08:25:47.60279Z","iopub.status.idle":"2022-05-21T08:25:47.675771Z","shell.execute_reply.started":"2022-05-21T08:25:47.602749Z","shell.execute_reply":"2022-05-21T08:25:47.674807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_loader_train = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, num_workers=4)\nprint(len(data_loader_train))\ndata_loader_valid = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, num_workers=2)\nprint(len(data_loader_valid))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:58:16.972131Z","iopub.execute_input":"2022-05-21T09:58:16.972777Z","iopub.status.idle":"2022-05-21T09:58:17.111413Z","shell.execute_reply.started":"2022-05-21T09:58:16.972741Z","shell.execute_reply":"2022-05-21T09:58:17.11036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_loader_test = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False, num_workers=2)\nprint(len(data_loader_test))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:25:53.819441Z","iopub.execute_input":"2022-05-21T08:25:53.819741Z","iopub.status.idle":"2022-05-21T08:25:53.827227Z","shell.execute_reply.started":"2022-05-21T08:25:53.81971Z","shell.execute_reply":"2022-05-21T08:25:53.826098Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model","metadata":{}},{"cell_type":"code","source":"from torch.hub import load_state_dict_from_url\nfrom torchvision.models.resnet import ResNet, Bottleneck","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:25:56.443261Z","iopub.execute_input":"2022-05-21T08:25:56.443952Z","iopub.status.idle":"2022-05-21T08:25:56.448957Z","shell.execute_reply.started":"2022-05-21T08:25:56.443917Z","shell.execute_reply":"2022-05-21T08:25:56.447981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_urls = {\n    'resnext101_32x8d': 'https://download.pytorch.org/models/ig_resnext101_32x8-c38310e5.pth',\n    'resnext101_32x16d': 'https://download.pytorch.org/models/ig_resnext101_32x16-c6f796b0.pth',\n    'resnext101_32x32d': 'https://download.pytorch.org/models/ig_resnext101_32x32-e4b90b00.pth',\n    'resnext101_32x48d': 'https://download.pytorch.org/models/ig_resnext101_32x48-3e41cc8a.pth',\n}","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-21T08:25:58.2126Z","iopub.execute_input":"2022-05-21T08:25:58.213226Z","iopub.status.idle":"2022-05-21T08:25:58.219008Z","shell.execute_reply.started":"2022-05-21T08:25:58.213179Z","shell.execute_reply":"2022-05-21T08:25:58.217757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def _resnext(arch, block, layers, pretrained, progress, **kwargs):\n    model = ResNet(block, layers, **kwargs)\n    state_dict = load_state_dict_from_url(model_urls[arch], progress=progress)\n    model.load_state_dict(state_dict)\n    return model\n\ndef resnext101_32x8d_wsl(progress=True, **kwargs):\n    \"\"\"Constructs a ResNeXt-101 32x8 model pre-trained on weakly-supervised data\n    and finetuned on ImageNet from Figure 5 in\n    `\"Exploring the Limits of Weakly Supervised Pretraining\" <https://arxiv.org/abs/1805.00932>`_\n    Args:\n        progress (bool): If True, displays a progress bar of the download to stderr.\n    \"\"\"\n    kwargs['groups'] = 32\n    kwargs['width_per_group'] = 8\n    return _resnext('resnext101_32x8d', Bottleneck, [3, 4, 23, 3], True, progress, **kwargs)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:26:00.414432Z","iopub.execute_input":"2022-05-21T08:26:00.414749Z","iopub.status.idle":"2022-05-21T08:26:00.423254Z","shell.execute_reply.started":"2022-05-21T08:26:00.414705Z","shell.execute_reply":"2022-05-21T08:26:00.421922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = resnext101_32x8d_wsl()\n#print(model)","metadata":{"_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2022-05-21T08:26:02.688258Z","iopub.execute_input":"2022-05-21T08:26:02.688562Z","iopub.status.idle":"2022-05-21T08:26:04.601186Z","shell.execute_reply.started":"2022-05-21T08:26:02.688532Z","shell.execute_reply":"2022-05-21T08:26:04.600092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"list(model.children())","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:26:05.04562Z","iopub.execute_input":"2022-05-21T08:26:05.046266Z","iopub.status.idle":"2022-05-21T08:26:05.058759Z","shell.execute_reply.started":"2022-05-21T08:26:05.046229Z","shell.execute_reply":"2022-05-21T08:26:05.057314Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNeXtModel(torch.nn.Module):\n    def __init__(self):\n        super(ResNeXtModel, self).__init__()\n        resnext = resnext101_32x8d_wsl()\n        self.base = torch.nn.Sequential(*list(resnext.children())[:-1])\n        self.fc = torch.nn.Sequential(torch.nn.Linear(2048,6))\n    \n    def forward(self, input):\n        features = self.base(input).reshape(-1, 2048)\n        out = self.fc(features)\n        return out, features","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:26:43.584682Z","iopub.execute_input":"2022-05-21T08:26:43.584978Z","iopub.status.idle":"2022-05-21T08:26:43.595494Z","shell.execute_reply.started":"2022-05-21T08:26:43.584948Z","shell.execute_reply":"2022-05-21T08:26:43.591723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Training & Validation","metadata":{}},{"cell_type":"code","source":"# Installing useful libraries\n\n!git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option=\"--cpp_ext\" --global-option=\"--cuda_ext\" ./","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:25:00.413159Z","iopub.status.idle":"2022-05-21T08:25:00.413753Z","shell.execute_reply.started":"2022-05-21T08:25:00.413427Z","shell.execute_reply":"2022-05-21T08:25:00.413457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#For Mixed precision training\nfrom apex import amp","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:26:54.387208Z","iopub.execute_input":"2022-05-21T08:26:54.387518Z","iopub.status.idle":"2022-05-21T08:26:54.423222Z","shell.execute_reply.started":"2022-05-21T08:26:54.387487Z","shell.execute_reply":"2022-05-21T08:26:54.422372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:26:56.925275Z","iopub.execute_input":"2022-05-21T08:26:56.925714Z","iopub.status.idle":"2022-05-21T08:26:56.977309Z","shell.execute_reply.started":"2022-05-21T08:26:56.925668Z","shell.execute_reply":"2022-05-21T08:26:56.976248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = ResNeXtModel()\nmodel.to(device)\n\ncriterion = torch.nn.BCEWithLogitsLoss()\noptimizer = optim.Adam(model.parameters(), lr=1e-5)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:26:59.74388Z","iopub.execute_input":"2022-05-21T08:26:59.744186Z","iopub.status.idle":"2022-05-21T08:27:03.285239Z","shell.execute_reply.started":"2022-05-21T08:26:59.744153Z","shell.execute_reply":"2022-05-21T08:27:03.284238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model, optimizer = amp.initialize(model, optimizer, opt_level=\"O1\")","metadata":{"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:27:04.705471Z","iopub.execute_input":"2022-05-21T08:27:04.70648Z","iopub.status.idle":"2022-05-21T08:27:04.728269Z","shell.execute_reply.started":"2022-05-21T08:27:04.706444Z","shell.execute_reply":"2022-05-21T08:27:04.727092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(model)","metadata":{"_kg_hide-input":true,"_kg_hide-output":true,"execution":{"iopub.status.busy":"2022-05-21T08:25:00.427323Z","iopub.status.idle":"2022-05-21T08:25:00.427835Z","shell.execute_reply.started":"2022-05-21T08:25:00.427554Z","shell.execute_reply":"2022-05-21T08:25:00.427582Z"},"jupyter":{"source_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = torch.nn.DataParallel(model)\npath= '../input/10epoch/model.pt'\nmodel.load_state_dict(torch.load(path)['model'])","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:27:08.337104Z","iopub.execute_input":"2022-05-21T08:27:08.33816Z","iopub.status.idle":"2022-05-21T08:27:09.084083Z","shell.execute_reply.started":"2022-05-21T08:27:08.338099Z","shell.execute_reply":"2022-05-21T08:27:09.083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.tensorboard import SummaryWriter","metadata":{"execution":{"iopub.status.busy":"2022-05-21T08:25:00.448316Z","iopub.status.idle":"2022-05-21T08:25:00.449158Z","shell.execute_reply.started":"2022-05-21T08:25:00.448803Z","shell.execute_reply":"2022-05-21T08:25:00.448848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#tb  = SummaryWriter('runs/ich_detection_experiment_1')\n# tb  = SummaryWriter()\nfrom sklearn.metrics import jaccard_similarity_score","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:42:22.281325Z","iopub.execute_input":"2022-05-21T09:42:22.282363Z","iopub.status.idle":"2022-05-21T09:42:22.304809Z","shell.execute_reply.started":"2022-05-21T09:42:22.282313Z","shell.execute_reply":"2022-05-21T09:42:22.302716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for param in model.parameters():\n    param.requires_grad = False\njacc=[]\nacc=[]\nf1=[]\nmodel.eval()\nnew = []\ntest_pred = np.zeros((len(valid_dataset) * n_classes, 1))\nfor i, x_batch in enumerate(tqdm.tqdm(data_loader_valid)):\n    \n    x_image = x_batch[\"image\"]\n    x_image = x_image.to(device, dtype=torch.float)\n    x_label = x_batch[\"labels\"]\n    x_label  = x_label.to(device,dtype=torch.float)\n    with torch.no_grad():\n        \n        outputs,_ = model(x_image)\n        #print(pred)\n        #print(pred.size())\n        #break\n        #test_pred[(i * batch_size * n_classes):((i + 1) * batch_size * n_classes)] = torch.sigmoid(\n         #   pred).detach().cpu().reshape((len(x_image) * n_classes, 1))\n        preds = (torch.sigmoid(outputs) >=0.5).float()*1\n\n        #new.append(torch.sigmoid(pred).detach().cpu())\n        #new1=torch.sigmoid(pred).detach().cpu()>=0.5\n        #print(new1,x_label)\n        #ans = (new1.float()==x_label.cpu())\n        #print(outputs)\n        ytrue= x_label.view(-1,6).detach().cpu().numpy()\n        ypred = preds.view(-1,6).detach().cpu().numpy()\n        jacc.append(jaccard_score(ytrue,ypred,average='macro',labels=np.unique(ypred)))\n        #acc.append(accuracy_score(new1.float(),x_label.cpu()))\n        #print(accuracy_score(new1.float(),x_label.cpu()))\n        #f1.append(f1_score(new1.float(),x_label.cpu(),average='micro'))\n        #print(f1_score(new1.float(),x_label.cpu(),average='micro'))","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:58:39.257638Z","iopub.execute_input":"2022-05-21T09:58:39.2581Z","iopub.status.idle":"2022-05-21T09:58:59.277897Z","shell.execute_reply.started":"2022-05-21T09:58:39.258002Z","shell.execute_reply":"2022-05-21T09:58:59.275041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Points to note -**\n- It is **NOT** multi-class but a **multi-label** classification problem(Refer to the paper)\n- Try using Sigmoid function to get 0or1 across each class, NOT Softmax\n- While finding accuracy, to fin correct predictions use threshold for outputs as 0.5\n\n**Observations and points to noye** -\n- On just 1 epoch, the results are **very poor**\n- Accuracy is high for obvious reasons. It **should not be used** to judge, since it is an **imbalaced problem**\n- F1 should be used when **we care about positive classes**. We should look at improving this\n- ROC-AUC should be used when you care **equally about positive and negative classes**. **Not advised on highly imbalanced** dataset(but let's check what we get and comapre with paper. Look into *average_precision_score* metric instead(when you care more about positive than negative class)\n- What did we get?\n    - Accuracy high - DO NOT go by this\n    - F1 of 0 - very bad, hope it improves when trained on more epochs\n    - ROC-AUC of 0.5 - which means not discriminating between the two claases(0/1); again hope to improve\n    \n**NOTE**\nError fixed. Should work tommorrow(::finger crossed::)\n- Tensorboard visualisation codes are right! But the site to display them doesn't seem to work through kaggle. It should work locally","metadata":{}},{"cell_type":"markdown","source":"## Testing","metadata":{}},{"cell_type":"code","source":"print(jacc)","metadata":{"execution":{"iopub.status.busy":"2022-05-21T09:54:02.796443Z","iopub.execute_input":"2022-05-21T09:54:02.796751Z","iopub.status.idle":"2022-05-21T09:54:02.805629Z","shell.execute_reply.started":"2022-05-21T09:54:02.79672Z","shell.execute_reply":"2022-05-21T09:54:02.804219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}