{"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\n# import numpy as np # linear algebra\n# import 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-02-07T19:19:47.985876Z","iopub.execute_input":"2023-02-07T19:19:47.986881Z","iopub.status.idle":"2023-02-07T19:19:48.003646Z","shell.execute_reply.started":"2023-02-07T19:19:47.986769Z","shell.execute_reply":"2023-02-07T19:19:48.002829Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n# train_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/train.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:07.800093Z","iopub.execute_input":"2023-02-08T17:07:07.802054Z","iopub.status.idle":"2023-02-08T17:07:07.8087Z","shell.execute_reply.started":"2023-02-08T17:07:07.80201Z","shell.execute_reply":"2023-02-08T17:07:07.807186Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:07.810206Z","iopub.execute_input":"2023-02-08T17:07:07.811236Z","iopub.status.idle":"2023-02-08T17:07:07.82001Z","shell.execute_reply.started":"2023-02-08T17:07:07.811198Z","shell.execute_reply":"2023-02-08T17:07:07.818821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# base_path = \"/kaggle/input/rsna-cut-off-empty-space-from-images\"\n# all_paths = []\n# for k in tqdm(range(len(train_df))):\n#     row = train_df.iloc[k, :]\n#     all_paths.append(base_path +\"/\" +str(row.patient_id) + \"/\" + str(row.image_id) + \".png\")\n    \n# train_df[\"path\"] = all_paths","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.026666Z","iopub.execute_input":"2023-02-07T19:19:48.027057Z","iopub.status.idle":"2023-02-07T19:19:48.032237Z","shell.execute_reply.started":"2023-02-07T19:19:48.027022Z","shell.execute_reply":"2023-02-07T19:19:48.031288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.033802Z","iopub.execute_input":"2023-02-07T19:19:48.034578Z","iopub.status.idle":"2023-02-07T19:19:48.040591Z","shell.execute_reply.started":"2023-02-07T19:19:48.034543Z","shell.execute_reply":"2023-02-07T19:19:48.039669Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df = train_df[[\"patient_id\", \"image_id\", \"laterality\", \"view\", \"age\", \"implant\", \"path\", \"cancer\"]]","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.042313Z","iopub.execute_input":"2023-02-07T19:19:48.043044Z","iopub.status.idle":"2023-02-07T19:19:48.04846Z","shell.execute_reply.started":"2023-02-07T19:19:48.043004Z","shell.execute_reply":"2023-02-07T19:19:48.047553Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.preprocessing import LabelEncoder, normalize","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.051071Z","iopub.execute_input":"2023-02-07T19:19:48.051889Z","iopub.status.idle":"2023-02-07T19:19:48.056302Z","shell.execute_reply.started":"2023-02-07T19:19:48.051852Z","shell.execute_reply":"2023-02-07T19:19:48.055388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# le_laterality = LabelEncoder()\n# le_view = LabelEncoder()\n\n\n\n# train_df['laterality'] = le_laterality.fit_transform(train_df['laterality'])\n# train_df['view'] = le_view.fit_transform(train_df['view'])\n\n# train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.057919Z","iopub.execute_input":"2023-02-07T19:19:48.058749Z","iopub.status.idle":"2023-02-07T19:19:48.063982Z","shell.execute_reply.started":"2023-02-07T19:19:48.058712Z","shell.execute_reply":"2023-02-07T19:19:48.063004Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import pandas as pd\n# from sklearn.impute import SimpleImputer\n# imputer = SimpleImputer(missing_values=np.nan, strategy='mean')\n# train_df[['age']] = imputer.fit_transform(train_df[['age']])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.069657Z","iopub.execute_input":"2023-02-07T19:19:48.070331Z","iopub.status.idle":"2023-02-07T19:19:48.074351Z","shell.execute_reply.started":"2023-02-07T19:19:48.070298Z","shell.execute_reply":"2023-02-07T19:19:48.073286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df.head().isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.077164Z","iopub.execute_input":"2023-02-07T19:19:48.077805Z","iopub.status.idle":"2023-02-07T19:19:48.08256Z","shell.execute_reply.started":"2023-02-07T19:19:48.077772Z","shell.execute_reply":"2023-02-07T19:19:48.081902Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.084064Z","iopub.execute_input":"2023-02-07T19:19:48.084801Z","iopub.status.idle":"2023-02-07T19:19:48.093374Z","shell.execute_reply.started":"2023-02-07T19:19:48.084765Z","shell.execute_reply":"2023-02-07T19:19:48.092662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_df.to_csv('train_df.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:19:48.094936Z","iopub.execute_input":"2023-02-07T19:19:48.095607Z","iopub.status.idle":"2023-02-07T19:19:48.101399Z","shell.execute_reply.started":"2023-02-07T19:19:48.095573Z","shell.execute_reply":"2023-02-07T19:19:48.100747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/train-df/train_df.csv\")\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:07.821834Z","iopub.execute_input":"2023-02-08T17:07:07.822326Z","iopub.status.idle":"2023-02-08T17:07:08.020719Z","shell.execute_reply.started":"2023-02-08T17:07:07.822282Z","shell.execute_reply":"2023-02-08T17:07:08.019812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nDEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\nprint('Device available now:', DEVICE)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:08.022166Z","iopub.execute_input":"2023-02-08T17:07:08.02273Z","iopub.status.idle":"2023-02-08T17:07:08.095376Z","shell.execute_reply.started":"2023-02-08T17:07:08.022694Z","shell.execute_reply":"2023-02-08T17:07:08.093832Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:08.096565Z","iopub.execute_input":"2023-02-08T17:07:08.097709Z","iopub.status.idle":"2023-02-08T17:07:08.103436Z","shell.execute_reply.started":"2023-02-08T17:07:08.097672Z","shell.execute_reply":"2023-02-08T17:07:08.102245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# import cv2\n# import os","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:08.105403Z","iopub.execute_input":"2023-02-08T17:07:08.106203Z","iopub.status.idle":"2023-02-08T17:07:08.112046Z","shell.execute_reply.started":"2023-02-08T17:07:08.106167Z","shell.execute_reply":"2023-02-08T17:07:08.110821Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torchvision\nfrom PIL import Image\n\ndef get_transforms(aug=False):\n\n    def transforms(img):\n#         img = img.convert('RGB')#.resize((512, 512))\n        if aug:\n            tfm = [\n                torchvision.transforms.RandomHorizontalFlip(0.5),\n                torchvision.transforms.RandomRotation(degrees=(-5, 5)), \n                torchvision.transforms.RandomResizedCrop((1024, 512), scale=(0.8, 1), ratio=(0.45, 0.55)) \n            ]\n        else:\n            tfm = [\n                torchvision.transforms.RandomHorizontalFlip(0.5),\n                torchvision.transforms.Resize((256, 256))\n            ]\n        img = torchvision.transforms.Compose(tfm + [            \n            torchvision.transforms.ToTensor(),\n            torchvision.transforms.Normalize(mean=0.2179, std=0.0529),\n            \n        ])(img)\n        return img\n\n    return lambda img: transforms(img)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:08.115071Z","iopub.execute_input":"2023-02-08T17:07:08.116371Z","iopub.status.idle":"2023-02-08T17:07:08.134047Z","shell.execute_reply.started":"2023-02-08T17:07:08.116334Z","shell.execute_reply":"2023-02-08T17:07:08.132731Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from PIL import Image\nimg_path = train_df['path']","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:08.135939Z","iopub.execute_input":"2023-02-08T17:07:08.136588Z","iopub.status.idle":"2023-02-08T17:07:08.14856Z","shell.execute_reply.started":"2023-02-08T17:07:08.136546Z","shell.execute_reply":"2023-02-08T17:07:08.147358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class RSNADataset(Dataset):\n    \n    def __init__(self, df, img_path, transforms=None):\n        self.df = df\n        self.img_path = img_path\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        file = self.img_path[idx]\n        file = Image.open(file).convert('RGB')\n        X = self.transforms(file)\n        Y = self.df.loc[idx,'cancer']\n        return X.float(), torch.tensor(Y).float()","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:09.484111Z","iopub.execute_input":"2023-02-08T17:07:09.484571Z","iopub.status.idle":"2023-02-08T17:07:09.492357Z","shell.execute_reply.started":"2023-02-08T17:07:09.484535Z","shell.execute_reply":"2023-02-08T17:07:09.491304Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_ds = RSNADataset(train_df, img_path,get_transforms(False))\n#train_ds[0][0]","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:20.635707Z","iopub.execute_input":"2023-02-08T17:07:20.636415Z","iopub.status.idle":"2023-02-08T17:07:20.641255Z","shell.execute_reply.started":"2023-02-08T17:07:20.636378Z","shell.execute_reply":"2023-02-08T17:07:20.640177Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #file = cv2.imread(\"/kaggle/input/rsna-cut-off-empty-space-from-images/10006/1459541791.png\", cv2.COLOR_BGR2GRAY)\n# file = Image.open('/kaggle/input/rsna-cut-off-empty-space-from-images/10006/1459541791.png').convert('RGB')\n        \n# file = get_transforms(False)(file)\n# (file)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:21.634878Z","iopub.execute_input":"2023-02-08T17:07:21.635931Z","iopub.status.idle":"2023-02-08T17:07:21.64039Z","shell.execute_reply.started":"2023-02-08T17:07:21.635891Z","shell.execute_reply":"2023-02-08T17:07:21.638993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:22.383781Z","iopub.execute_input":"2023-02-08T17:07:22.384854Z","iopub.status.idle":"2023-02-08T17:07:22.403852Z","shell.execute_reply.started":"2023-02-08T17:07:22.384802Z","shell.execute_reply":"2023-02-08T17:07:22.402813Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:23.052731Z","iopub.execute_input":"2023-02-08T17:07:23.053675Z","iopub.status.idle":"2023-02-08T17:07:23.435046Z","shell.execute_reply.started":"2023-02-08T17:07:23.053626Z","shell.execute_reply":"2023-02-08T17:07:23.434006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = train_df.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:25.445546Z","iopub.execute_input":"2023-02-08T17:07:25.44597Z","iopub.status.idle":"2023-02-08T17:07:25.455923Z","shell.execute_reply.started":"2023-02-08T17:07:25.445935Z","shell.execute_reply":"2023-02-08T17:07:25.454782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df = train_df.copy()\n# counts = df['cancer'].value_counts()\n# df['weights'] = df['cancer'].apply(lambda x: 1/counts[x])\n# df","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:26.654327Z","iopub.execute_input":"2023-02-08T17:07:26.655391Z","iopub.status.idle":"2023-02-08T17:07:26.660103Z","shell.execute_reply.started":"2023-02-08T17:07:26.655348Z","shell.execute_reply":"2023-02-08T17:07:26.659003Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train, df_val = train_test_split(train_df, test_size=0.25)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:27.104189Z","iopub.execute_input":"2023-02-08T17:07:27.104564Z","iopub.status.idle":"2023-02-08T17:07:27.123691Z","shell.execute_reply.started":"2023-02-08T17:07:27.104532Z","shell.execute_reply":"2023-02-08T17:07:27.122797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader, Dataset","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:33.837608Z","iopub.execute_input":"2023-02-08T17:07:33.838142Z","iopub.status.idle":"2023-02-08T17:07:33.843619Z","shell.execute_reply.started":"2023-02-08T17:07:33.838108Z","shell.execute_reply":"2023-02-08T17:07:33.842666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = df_train.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:34.269684Z","iopub.execute_input":"2023-02-08T17:07:34.270622Z","iopub.status.idle":"2023-02-08T17:07:34.279526Z","shell.execute_reply.started":"2023-02-08T17:07:34.270585Z","shell.execute_reply":"2023-02-08T17:07:34.278434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val = df_val.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:34.785624Z","iopub.execute_input":"2023-02-08T17:07:34.786368Z","iopub.status.idle":"2023-02-08T17:07:34.792848Z","shell.execute_reply.started":"2023-02-08T17:07:34.786327Z","shell.execute_reply":"2023-02-08T17:07:34.791737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# df_train.loc[23659,:]","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:35.532558Z","iopub.execute_input":"2023-02-08T17:07:35.53295Z","iopub.status.idle":"2023-02-08T17:07:35.537905Z","shell.execute_reply.started":"2023-02-08T17:07:35.532915Z","shell.execute_reply":"2023-02-08T17:07:35.536807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:35.809994Z","iopub.execute_input":"2023-02-08T17:07:35.810857Z","iopub.status.idle":"2023-02-08T17:07:35.830699Z","shell.execute_reply.started":"2023-02-08T17:07:35.810812Z","shell.execute_reply":"2023-02-08T17:07:35.829809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_val","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:36.161217Z","iopub.execute_input":"2023-02-08T17:07:36.162126Z","iopub.status.idle":"2023-02-08T17:07:36.180736Z","shell.execute_reply.started":"2023-02-08T17:07:36.162077Z","shell.execute_reply":"2023-02-08T17:07:36.179665Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_img_path = df_train['path']\nvalid_img_path = df_val['path']","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:07:37.028339Z","iopub.execute_input":"2023-02-08T17:07:37.0287Z","iopub.status.idle":"2023-02-08T17:07:37.034131Z","shell.execute_reply.started":"2023-02-08T17:07:37.028668Z","shell.execute_reply":"2023-02-08T17:07:37.033057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_samples = 1000\n# val_samples = 500\n\ntrain_ds = RSNADataset(df_train, train_img_path,get_transforms(False))\nval_ds = RSNADataset(df_val, valid_img_path,get_transforms(False))\n\n# train_sampler = WeightedRandomSampler(df_train['weights'].values, train_samples)\ntrain_loader = DataLoader(train_ds, batch_size=32,shuffle=True,num_workers=4,pin_memory=True)\n\n# val_sampler = WeightedRandomSampler(df_val['weights'].values, val_samples)\nval_loader = DataLoader(val_ds, batch_size=32,shuffle=False,num_workers=4,pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:21.929561Z","iopub.execute_input":"2023-02-08T17:22:21.930315Z","iopub.status.idle":"2023-02-08T17:22:21.936413Z","shell.execute_reply.started":"2023-02-08T17:22:21.930276Z","shell.execute_reply":"2023-02-08T17:22:21.935465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img,label = next(iter(train_loader))\n#img.shape,label.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:23.34317Z","iopub.execute_input":"2023-02-08T17:22:23.343532Z","iopub.status.idle":"2023-02-08T17:22:23.348166Z","shell.execute_reply.started":"2023-02-08T17:22:23.343502Z","shell.execute_reply":"2023-02-08T17:22:23.346801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_default_device():\n    \"\"\"Pick GPU if available, else CPU\"\"\"\n    if torch.cuda.is_available():\n        return torch.device('cuda')\n    else:\n        return torch.device('cpu')\n    \ndef to_device(data, device):\n    \"\"\"Move tensor(s) to chosen device\"\"\"\n    if isinstance(data, (list,tuple)):\n        return [to_device(x, device) for x in data]\n    return data.to(device, non_blocking=True)\n\nclass DeviceDataLoader():\n    \"\"\"Wrap a dataloader to move data to a device\"\"\"\n    def __init__(self, dl, device):\n        self.dl = dl\n        self.device = device\n        \n    def __iter__(self):\n        \"\"\"Yield a batch of data after moving it to device\"\"\"\n        for b in self.dl: \n            yield to_device(b, self.device)\n\n    def __len__(self):\n        \"\"\"Number of batches\"\"\"\n        return len(self.dl)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:23.840019Z","iopub.execute_input":"2023-02-08T17:22:23.840867Z","iopub.status.idle":"2023-02-08T17:22:23.849794Z","shell.execute_reply.started":"2023-02-08T17:22:23.84082Z","shell.execute_reply":"2023-02-08T17:22:23.848647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ndevice = get_default_device()\ntrain_dl = DeviceDataLoader(train_loader,device)\nval_dl = DeviceDataLoader(val_loader,device)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:24.53867Z","iopub.execute_input":"2023-02-08T17:22:24.539371Z","iopub.status.idle":"2023-02-08T17:22:24.545453Z","shell.execute_reply.started":"2023-02-08T17:22:24.539326Z","shell.execute_reply":"2023-02-08T17:22:24.544445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#img,l = next(iter(train_dl))\n#img.double(),l.dtype","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:25.339057Z","iopub.execute_input":"2023-02-08T17:22:25.339404Z","iopub.status.idle":"2023-02-08T17:22:25.346499Z","shell.execute_reply.started":"2023-02-08T17:22:25.339375Z","shell.execute_reply":"2023-02-08T17:22:25.345405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#d = RSNADataset(train_df, img_path)\n#im = d.__getitem__(3)[0].cpu()\n#print(d.__getitem__(3)[1].cpu())\n#import matplotlib.pyplot as plt\n#plt.imshow(im[0])\n#plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:25.798361Z","iopub.execute_input":"2023-02-08T17:22:25.798706Z","iopub.status.idle":"2023-02-08T17:22:25.803319Z","shell.execute_reply.started":"2023-02-08T17:22:25.798676Z","shell.execute_reply":"2023-02-08T17:22:25.802339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torchvision\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:26.138259Z","iopub.execute_input":"2023-02-08T17:22:26.138928Z","iopub.status.idle":"2023-02-08T17:22:26.143673Z","shell.execute_reply.started":"2023-02-08T17:22:26.138891Z","shell.execute_reply":"2023-02-08T17:22:26.142709Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ImageClassificationBase(nn.Module):\n    def training_step(self, batch):\n        images, labels = batch \n        out = self(images)\n        k  = nn.Sigmoid()\n        out = k(out)\n        out = torch.squeeze(out,1)\n        loss = F.binary_cross_entropy(out, labels) # Calculate loss\n        return loss\n    \n    def validation_step(self, batch):\n        images, labels = batch \n        out = self(images)\n        k  = nn.Sigmoid()\n        out = k(out)\n        out = torch.squeeze(out,1)\n        #print(out)\n        loss = F.binary_cross_entropy(out, labels)\n        acc = accuracy(out, labels)           # Calculate accuracy\n        return {'val_loss': loss.detach(), 'val_acc': acc}\n        \n    def validation_epoch_end(self, outputs):\n        batch_losses = [x['val_loss'] for x in outputs]\n        epoch_loss = torch.stack(batch_losses).mean()   # Combine losses\n        batch_accs = [x['val_acc'] for x in outputs]\n        epoch_acc = torch.stack(batch_accs).mean()      # Combine accuracies\n        return {'val_loss': epoch_loss.item(), 'val_acc': epoch_acc.item()}\n    \n    def epoch_end(self, epoch, result):\n         print(\"Epoch [{}], train_loss: {:.4f}, val_loss: {:.4f}, val_acc: {:.4f}\".format(\n            epoch, result['train_loss'], result['val_loss'], result['val_acc']))\n        \ndef accuracy(outputs, labels):\n#     _, preds = torch.max(outputs, dim=1)\n#     print(outputs)\n#     print(labels)\n    for i in range(len(outputs)):\n        if (outputs[i]>0.5):\n            outputs[i] = 1\n        else:\n            outputs[i] = 0\n    return torch.tensor(torch.sum(outputs == labels).item() / len(outputs))","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:26.805116Z","iopub.execute_input":"2023-02-08T17:22:26.805825Z","iopub.status.idle":"2023-02-08T17:22:26.820174Z","shell.execute_reply.started":"2023-02-08T17:22:26.805784Z","shell.execute_reply":"2023-02-08T17:22:26.819105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(model, val_loader):\n    model.eval()\n    outputs = [model.validation_step(batch) for batch in val_loader]\n    return model.validation_epoch_end(outputs)\n\ndef fit(epochs, lr, model, train_loader, val_loader, opt_func=torch.optim.SGD):\n    history = []\n    optimizer = opt_func(model.parameters(), lr)\n    for epoch in range(epochs):\n        # Training Phase \n        model.train()\n        train_losses = []\n        for batch in train_loader:\n            loss = model.training_step(batch)\n            train_losses.append(loss)\n            loss.backward()\n            optimizer.step()\n            optimizer.zero_grad()\n        # Validation phase\n        result = evaluate(model, val_loader)\n        result['train_loss'] = torch.stack(train_losses).mean().item()\n        model.epoch_end(epoch, result)\n        history.append(result)\n    return history","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:27.344933Z","iopub.execute_input":"2023-02-08T17:22:27.345281Z","iopub.status.idle":"2023-02-08T17:22:27.353545Z","shell.execute_reply.started":"2023-02-08T17:22:27.345251Z","shell.execute_reply":"2023-02-08T17:22:27.35222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def conv_block(in_channels, out_channels, pool=False):\n    layers = [nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=1), \n              nn.BatchNorm2d(out_channels), \n              nn.ReLU(inplace=True)]\n    if pool: layers.append(nn.MaxPool2d(2))\n    return nn.Sequential(*layers)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:27.856357Z","iopub.execute_input":"2023-02-08T17:22:27.856719Z","iopub.status.idle":"2023-02-08T17:22:27.863818Z","shell.execute_reply.started":"2023-02-08T17:22:27.856686Z","shell.execute_reply":"2023-02-08T17:22:27.862815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class ResNet9(ImageClassificationBase):\n    def __init__(self, in_channels, num_classes):\n        super().__init__()\n        \n        self.conv1 = conv_block(in_channels, 64)\n        self.conv2 = conv_block(64, 128, pool=True)\n        self.res1 = nn.Sequential(conv_block(128, 128), conv_block(128, 128))\n        \n        self.conv3 = conv_block(128, 256, pool=True)\n        self.conv4 = conv_block(256, 512, pool=True)\n        self.res2 = nn.Sequential(conv_block(512, 512), conv_block(512, 512))\n        \n        self.classifier = nn.Sequential(nn.AdaptiveMaxPool2d(1), \n                                        nn.Flatten(), \n                                        nn.Dropout(0.2),\n                                        nn.Linear(512, num_classes))\n        \n    def forward(self, xb):\n        out = self.conv1(xb)\n        out = self.conv2(out)\n        out = self.res1(out) + out\n        out = self.conv3(out)\n        out = self.conv4(out)\n        out = self.res2(out) + out\n        out = self.classifier(out)\n        return out","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:28.60919Z","iopub.execute_input":"2023-02-08T17:22:28.611361Z","iopub.status.idle":"2023-02-08T17:22:28.619351Z","shell.execute_reply.started":"2023-02-08T17:22:28.6113Z","shell.execute_reply":"2023-02-08T17:22:28.618253Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:29.615204Z","iopub.execute_input":"2023-02-08T17:22:29.615569Z","iopub.status.idle":"2023-02-08T17:22:29.739352Z","shell.execute_reply.started":"2023-02-08T17:22:29.615538Z","shell.execute_reply":"2023-02-08T17:22:29.738194Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.cuda.empty_cache()\nmodel = to_device(ResNet9(3, 1), device) ## 1 ---no of input channels rgb\nmodel","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:29.982693Z","iopub.execute_input":"2023-02-08T17:22:29.983295Z","iopub.status.idle":"2023-02-08T17:22:32.786489Z","shell.execute_reply.started":"2023-02-08T17:22:29.98326Z","shell.execute_reply":"2023-02-08T17:22:32.785552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ii = '/kaggle/input/rsna-cut-off-empty-space-from-images'\n# import os\n# from os import listdir\n \n# # get the path/directory\n# folder_dir = ii\n# #print(folder_dir)\n# for images in os.listdir(folder_dir):\n \n#     # check if the image ends with png\n#     if (images.endswith(\".png\")):\n#         print(images)","metadata":{"execution":{"iopub.status.busy":"2023-02-08T17:22:32.788319Z","iopub.execute_input":"2023-02-08T17:22:32.788888Z","iopub.status.idle":"2023-02-08T17:22:32.794042Z","shell.execute_reply.started":"2023-02-08T17:22:32.788849Z","shell.execute_reply":"2023-02-08T17:22:32.793115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = [evaluate(model, val_dl)]\nhistory","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:20:01.866565Z","iopub.execute_input":"2023-02-07T19:20:01.867283Z","iopub.status.idle":"2023-02-07T19:22:46.994098Z","shell.execute_reply.started":"2023-02-07T19:20:01.867248Z","shell.execute_reply":"2023-02-07T19:22:46.992808Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history += fit(5,.01,model,train_dl,val_dl,torch.optim.Adam)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:22:46.996138Z","iopub.execute_input":"2023-02-07T19:22:46.996899Z","iopub.status.idle":"2023-02-07T19:36:33.691909Z","shell.execute_reply.started":"2023-02-07T19:22:46.996843Z","shell.execute_reply":"2023-02-07T19:36:33.690578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model,\"check.pth\")","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df = pd.read_csv('/kaggle/input/rsna-breast-cancer-detection/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:33.706198Z","iopub.execute_input":"2023-02-07T19:36:33.707132Z","iopub.status.idle":"2023-02-07T19:36:33.721329Z","shell.execute_reply.started":"2023-02-07T19:36:33.707081Z","shell.execute_reply":"2023-02-07T19:36:33.72025Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#PNG_TEST_IMAGES_PATH = f'test'\n#DCM_TEST_IMAGES_PATH = f'/kaggle/input/rsna-breast-cancer-detection/test_images'","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:33.72264Z","iopub.execute_input":"2023-02-07T19:36:33.723068Z","iopub.status.idle":"2023-02-07T19:36:33.729431Z","shell.execute_reply.started":"2023-02-07T19:36:33.723031Z","shell.execute_reply":"2023-02-07T19:36:33.72844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nfrom concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor\nimport re\nimport pydicom\nimport glob\nimport cv2\nimport os\n\ndef fit_image(fname, size=1024):\n    # 1. Read, resize\n    \n    \n    patient = fname.split('/')[-2]\n    image = fname.split('/')[-1][:-4]\n    dicom = pydicom.dcmread(fname)\n    img = dicom.pixel_array\n    img = (img - img.min()) / (img.max() - img.min())\n    if dicom.PhotometricInterpretation == \"MONOCHROME1\":\n        img = 1 - img\n    img = cv2.resize(img, (size, size))\n    \n    # 2. Crop\n    X = img\n    # Some images have narrow exterior \"frames\" that complicate selection of the main data. Cutting off the frame\n    X = X[5:-5, 5:-5]\n    \n    \n    # regions of non-empty pixels\n    output= cv2.connectedComponentsWithStats((X > 0.05).astype(np.uint8)[:, :], 8, cv2.CV_32S)\n    stats = output[2]\n    \n    # finding max area which always corresponds to the breast data. \n    idx = stats[1:, 4].argmax() + 1\n    x1, y1, w, h = stats[idx][:4]\n    x2 = x1 + w\n    y2 = y1 + h\n    \n    # cutting out the breast data\n    X_fit = X[y1: y2, x1: x2]\n    \n    patient_id, im_id = os.path.basename(os.path.dirname(fname)), os.path.basename(fname)[:-4]\n    os.makedirs(f'{PNG_TEST_IMAGES_PATH}/test_images/{patient_id}', exist_ok=True)\n    cv2.imwrite(f'{PNG_TEST_IMAGES_PATH}/test_images/{patient_id}/{im_id}.png', (X_fit[:, :] * 255).astype(np.uint8))\n\ndef fit_all_images(all_images):\n    with ThreadPoolExecutor(2) as p:\n        for i in tqdm(p.map(fit_image, all_images), total=len(all_images)):\n            pass\n\nall_images = glob.glob('/kaggle/input/rsna-breast-cancer-detection/test_images/*/*') \n# all_images = glob.glob('/kaggle/input/rsna-breast-cancer-detection/train_images/10006/*')\nfit_all_images(all_images)\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:33.730832Z","iopub.execute_input":"2023-02-07T19:36:33.731279Z","iopub.status.idle":"2023-02-07T19:36:36.802809Z","shell.execute_reply.started":"2023-02-07T19:36:33.731239Z","shell.execute_reply":"2023-02-07T19:36:36.801737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_img_path ='/kaggle/working/test/test_images/10008'","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.804259Z","iopub.execute_input":"2023-02-07T19:36:36.804661Z","iopub.status.idle":"2023-02-07T19:36:36.809946Z","shell.execute_reply.started":"2023-02-07T19:36:36.804606Z","shell.execute_reply":"2023-02-07T19:36:36.808897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#base_path = test_img_path\n#all_paths = []\n#for k in tqdm(range(len(test_df))):\n    \n #   row = test_df.iloc[k, :]\n #   all_paths.append(base_path +\"/\" +str(row.image_id)  + \".png\")\n    \n#test_df[\"path\"] = all_paths","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.81193Z","iopub.execute_input":"2023-02-07T19:36:36.812357Z","iopub.status.idle":"2023-02-07T19:36:36.82689Z","shell.execute_reply.started":"2023-02-07T19:36:36.812304Z","shell.execute_reply":"2023-02-07T19:36:36.825953Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_df","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.828331Z","iopub.execute_input":"2023-02-07T19:36:36.828928Z","iopub.status.idle":"2023-02-07T19:36:36.842959Z","shell.execute_reply.started":"2023-02-07T19:36:36.828893Z","shell.execute_reply":"2023-02-07T19:36:36.841993Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#path_test = test_df[\"path\"] ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.844279Z","iopub.execute_input":"2023-02-07T19:36:36.844943Z","iopub.status.idle":"2023-02-07T19:36:36.849498Z","shell.execute_reply.started":"2023-02-07T19:36:36.844909Z","shell.execute_reply":"2023-02-07T19:36:36.848367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nclass RSNATestDataset(Dataset):\n    \n    def __init__(self, df, img_path, transforms=None):\n        self.df = df\n        self.img_path = img_path\n        self.transforms = transforms\n        \n    def __len__(self):\n        return len(self.df)\n    \n    def __getitem__(self, idx):\n        file = self.img_path[idx]\n        #print(file)\n        file = Image.open(file).convert('RGB')\n        X = self.transforms(file)\n        #Y = self.df.loc[idx,'cancer']\n        return X.float()\n\"\"\"        ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.851045Z","iopub.execute_input":"2023-02-07T19:36:36.851756Z","iopub.status.idle":"2023-02-07T19:36:36.861033Z","shell.execute_reply.started":"2023-02-07T19:36:36.851721Z","shell.execute_reply":"2023-02-07T19:36:36.859987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#path_test","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.863502Z","iopub.execute_input":"2023-02-07T19:36:36.864397Z","iopub.status.idle":"2023-02-07T19:36:36.872982Z","shell.execute_reply.started":"2023-02-07T19:36:36.864362Z","shell.execute_reply":"2023-02-07T19:36:36.871831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_ds = RSNATestDataset(test_df,path_test,get_transforms(False))","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.874617Z","iopub.execute_input":"2023-02-07T19:36:36.875212Z","iopub.status.idle":"2023-02-07T19:36:36.881306Z","shell.execute_reply.started":"2023-02-07T19:36:36.875178Z","shell.execute_reply":"2023-02-07T19:36:36.880166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_ds","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.882578Z","iopub.execute_input":"2023-02-07T19:36:36.883713Z","iopub.status.idle":"2023-02-07T19:36:36.891531Z","shell.execute_reply.started":"2023-02-07T19:36:36.883679Z","shell.execute_reply":"2023-02-07T19:36:36.890517Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test_loader = DataLoader(test_ds, batch_size=4,shuffle=True,num_workers=4,pin_memory=True)\n#test_dl_final = DeviceDataLoader(test_loader,device)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.893097Z","iopub.execute_input":"2023-02-07T19:36:36.894326Z","iopub.status.idle":"2023-02-07T19:36:36.90023Z","shell.execute_reply.started":"2023-02-07T19:36:36.894299Z","shell.execute_reply":"2023-02-07T19:36:36.899259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cnn_pred = []\n#for image in test_dl_final:\n#    pred = model(image).cpu().detach().numpy()\n#     print(pred.cpu().detach().numpy())\n#    for i in range(len(pred)):\n #       cnn_pred.append(pred[i])","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:36.901657Z","iopub.execute_input":"2023-02-07T19:36:36.902421Z","iopub.status.idle":"2023-02-07T19:36:37.206329Z","shell.execute_reply.started":"2023-02-07T19:36:36.902386Z","shell.execute_reply":"2023-02-07T19:36:37.205076Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#cnn_pred","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.209751Z","iopub.execute_input":"2023-02-07T19:36:37.210174Z","iopub.status.idle":"2023-02-07T19:36:37.218839Z","shell.execute_reply.started":"2023-02-07T19:36:37.210129Z","shell.execute_reply":"2023-02-07T19:36:37.217615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_test = test_df \n#df_test['cancer'] = cnn_pred\n\n\n#df_sub = df_test.groupby('prediction_id')[['cancer']].mean()\n#df_sub","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.220651Z","iopub.execute_input":"2023-02-07T19:36:37.221408Z","iopub.status.idle":"2023-02-07T19:36:37.248849Z","shell.execute_reply.started":"2023-02-07T19:36:37.22137Z","shell.execute_reply":"2023-02-07T19:36:37.247801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#THRES = .3\n#df_sub['cancer'] = (df_sub.cancer > THRES).astype(float)\n#df_sub","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.25032Z","iopub.execute_input":"2023-02-07T19:36:37.251046Z","iopub.status.idle":"2023-02-07T19:36:37.261869Z","shell.execute_reply.started":"2023-02-07T19:36:37.251011Z","shell.execute_reply":"2023-02-07T19:36:37.260685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#df_sub.to_csv('submission.csv', index=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.263503Z","iopub.execute_input":"2023-02-07T19:36:37.263976Z","iopub.status.idle":"2023-02-07T19:36:37.273677Z","shell.execute_reply.started":"2023-02-07T19:36:37.263941Z","shell.execute_reply":"2023-02-07T19:36:37.272788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nmodel = torchvision.models.densenet121(pretrained=True)\n#print(model)\n# Replace the classifier\nnum_classes = 1 # number of classes in your target dataset\nmodel.classifier = nn.Sequential(\n    nn.Linear(1024, 512),\n    nn.ReLU(),\n    nn.Dropout(0.2),\n    nn.Linear(512, num_classes),\n)\ntarget_dataset  = val_dl\n# Freeze all layers except the classifier\nfor name, param in model.named_parameters():\n    if name.startswith(\"classifier\"):\n        param.requires_grad = True\n    else:\n        param.requires_grad = False\n\n# Train the classifier using your target dataset\n\noptimizer = torch.optim.Adam(model.parameters())\nfor epoch in range(1):\n    running_loss = 0.0\n    for inputs, labels in target_dataset:\n        #input_rgb = []\n        optimizer.zero_grad()\n       # for l in range(inputs.shape[0]):\n          #   tmp  = []\n          #  n  = inputs[l][0]\n          #  tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy())\n          #  input_rgb.append(tmp)\n            \n       # inputs = torch.from_numpy(np.asarray(input_rgb))\n        labels = labels.to(\"cpu\")\n       # print()      \n       # print(labels.shape)\n        #input_rgb = np.repeat(inputs([...,np.newaxis],3,-3))\n        outputs = model(inputs)\n        #print(outputs.shape)\n        loss = F.binary_cross_entropy_with_logits(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n    print(f\"Epoch: {epoch+1}, Loss: {running_loss/len(target_dataset)}\")\n\n# Fine-tune the model\nfor name, param in model.named_parameters():\n    param.requires_grad = True\n\n# Train the whole model again\noptimizer = torch.optim.Adam(model.parameters())\n\nfor epoch in range(1):\n    running_loss = 0.0\n    for inputs, labels in target_dataset:\n        input_rgb = []\n        \n        optimizer.zero_grad()\n        for l in range(inputs.shape[0]):\n            tmp  = []\n            n  = inputs[l][0]\n            tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy())\n            input_rgb.append(tmp)\n            \n        inputs = torch.from_numpy(np.asarray(input_rgb))\n        labels = labels.to(\"cpu\")\n        \n        outputs = model(inputs)\n        loss = F.binary_cross_entropy_with_logits(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n    print(f\"Epoch: {epoch+1}, Loss: {running_loss/len(target_dataset)}\")\n\n# Evaluate the performance of the model on your target dataset\nwith torch.no_grad():\n    correct = 0\n    total = 0\n    for inputs, labels in target_dataset:\n        \n        input_rgb = []\n        for l in range(inputs.shape[0]):\n            \n            tmp  = []\n            n  = inputs[l][0]\n            tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy())\n            input_rgb.append(tmp)\n            \n        inputs = torch.from_numpy(np.asarray(input_rgb))\n       # print(inputs.shape)\n        labels = labels.to(\"cpu\")\n        outputs = model(inputs)\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n    print(f\"Accuracy: { correct / total}%\")\n\"\"\"    \n    \n    \n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.275258Z","iopub.execute_input":"2023-02-07T19:36:37.275886Z","iopub.status.idle":"2023-02-07T19:36:37.285402Z","shell.execute_reply.started":"2023-02-07T19:36:37.275849Z","shell.execute_reply":"2023-02-07T19:36:37.284278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#!pip install efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.287125Z","iopub.execute_input":"2023-02-07T19:36:37.287534Z","iopub.status.idle":"2023-02-07T19:36:37.294132Z","shell.execute_reply.started":"2023-02-07T19:36:37.287492Z","shell.execute_reply":"2023-02-07T19:36:37.293108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#print(val_df)","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.295464Z","iopub.execute_input":"2023-02-07T19:36:37.2966Z","iopub.status.idle":"2023-02-07T19:36:37.302784Z","shell.execute_reply.started":"2023-02-07T19:36:37.296566Z","shell.execute_reply":"2023-02-07T19:36:37.301682Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\nimport torch\nimport efficientnet_pytorch\nfrom efficientnet_pytorch import EfficientNet\n\n# Initialize the model\nmodel = EfficientNet.from_name('efficientnet-b0')\n#print(model)\nnum_classes = 1 # number of classes in your target dataset\n# Replace the classifier with a custom classifier\n#model.classifier = nn.Sequential(nn.Linear(1280, num_classes), nn.Softmax(dim=1))\n#print(model)\nfor param in model.parameters():\n    param.requires_grad = False\nmodel._fc.requires_grad = True\n\n# Replace the last layer with a new dense layer with the desired number of units and activation function\nmodel._fc = nn.Sequential(\n    nn.Linear(in_features=model._fc.in_features, out_features=1),\n    nn.ReLU()\n)\n\ntarget_dataset  = val_dl\nval_dl\n# Freeze all layers except the classifier\nfor name, param in model.named_parameters():\n    if name.startswith(\"classifier\"):\n        param.requires_grad = True\n    else:\n        param.requires_grad = False\n#print(model)\n# Train the classifier using your target dataset\n\noptimizer = torch.optim.Adam(model.parameters())\nfor epoch in range(1):\n    running_loss = 0.0\n    for inputs, labels in target_dataset:\n        input_rgb = []\n        optimizer.zero_grad()\n        for l in range(inputs.shape[0]):\n            tmp  = []\n            n  = inputs[l][0]\n            tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy())\n            input_rgb.append(tmp)\n            \n        inputs = torch.from_numpy(np.asarray(input_rgb))\n        labels = labels.to(\"cpu\")\n       # print()      \n        print(labels.shape)\n        #input_rgb = np.repeat(inputs([...,np.newaxis],3,-3))\n        outputs = model(inputs)\n        print(inputs.shape)\n      #  print(outputs)\n        print(outputs.shape)\n        loss = F.binary_cross_entropy_with_logits(outputs, labels)\n        loss = loss.requires_grad_(requires_grad=True)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n    print(f\"Epoch: {epoch+1}, Loss: {running_loss/len(target_dataset)}\")\n\n# Fine-tune the model\nfor name, param in model.named_parameters():\n    param.requires_grad = True\n\n# Train the whole model again\noptimizer = torch.optim.Adam(model.parameters())\n\nfor epoch in range(1):\n    running_loss = 0.0\n    for inputs, labels in target_dataset:\n        input_rgb = []\n        \n        optimizer.zero_grad()\n        for l in range(inputs.shape[0]):\n            tmp  = []\n            n  = inputs[l][0]\n            tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy())\n            input_rgb.append(tmp)\n            \n        inputs = torch.from_numpy(np.asarray(input_rgb))\n        labels = labels.to(\"cpu\")\n        \n        outputs = model(inputs)\n        loss = F.binary_cross_entropy_with_logits(outputs, labels)\n        loss.backward()\n        optimizer.step()\n        running_loss += loss.item()\n    print(f\"Epoch: {epoch+1}, Loss: {running_loss/len(target_dataset)}\")\n\n# Evaluate the performance of the model on your target dataset\nwith torch.no_grad():\n    correct = 0\n    total = 0\n    for inputs, labels in target_dataset:\n        \n        input_rgb = []\n        for l in range(inputs.shape[0]):\n            \n            tmp  = []\n            n  = inputs[l][0]\n            tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy()); tmp.append(n.cpu().detach().numpy())\n            input_rgb.append(tmp)\n            \n        inputs = torch.from_numpy(np.asarray(input_rgb))\n       # print(inputs.shape)\n        labels = labels.to(\"cpu\")\n        outputs = model(inputs)\n        _, predicted = torch.max(outputs.data, 1)\n        total += labels.size(0)\n        correct += (predicted == labels).sum().item()\n    print(f\"Accuracy: { correct / total}%\")\n\"\"\"","metadata":{"execution":{"iopub.status.busy":"2023-02-07T19:36:37.304258Z","iopub.execute_input":"2023-02-07T19:36:37.304892Z","iopub.status.idle":"2023-02-07T19:36:37.316891Z","shell.execute_reply.started":"2023-02-07T19:36:37.304855Z","shell.execute_reply":"2023-02-07T19:36:37.315876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}