{"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 pandas as pd\nimport numpy as np\nimport gc\nimport os\nimport cv2\nimport time\nimport datetime\nimport warnings\nimport random\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom PIL import Image\nimport albumentations as A\nimport matplotlib.image as mpimg\n\nimport torch\nimport torchvision\nimport torch.nn.functional as F\nimport torch.nn as nn\nfrom torch.utils.data import Dataset, DataLoader, Subset\nfrom torch.optim.lr_scheduler import ReduceLROnPlateau\nfrom sklearn.metrics import accuracy_score, roc_auc_score\nfrom sklearn.model_selection import StratifiedKFold, GroupKFold, KFold\n\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:33.346962Z","iopub.execute_input":"2023-01-10T17:02:33.349493Z","iopub.status.idle":"2023-01-10T17:02:37.235350Z","shell.execute_reply.started":"2023-01-10T17:02:33.349410Z","shell.execute_reply":"2023-01-10T17:02:37.234178Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data=pd.read_csv(\"/kaggle/input/siim-isic-melanoma-classification/train.csv\")\ntrain_data.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.237481Z","iopub.execute_input":"2023-01-10T17:02:37.238697Z","iopub.status.idle":"2023-01-10T17:02:37.359266Z","shell.execute_reply.started":"2023-01-10T17:02:37.238654Z","shell.execute_reply":"2023-01-10T17:02:37.357769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"image_path\"]=\"/kaggle/input/siic-isic-224x224-images/train/\"+train_data[\"image_name\"]+\".png\"","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.360936Z","iopub.execute_input":"2023-01-10T17:02:37.361583Z","iopub.status.idle":"2023-01-10T17:02:37.394157Z","shell.execute_reply.started":"2023-01-10T17:02:37.361548Z","shell.execute_reply":"2023-01-10T17:02:37.392825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_image_path=train_data[\"image_path\"].values.tolist()","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.398369Z","iopub.execute_input":"2023-01-10T17:02:37.399300Z","iopub.status.idle":"2023-01-10T17:02:37.407584Z","shell.execute_reply.started":"2023-01-10T17:02:37.399259Z","shell.execute_reply":"2023-01-10T17:02:37.405371Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=Image.open(train_image_path[1])","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.408850Z","iopub.execute_input":"2023-01-10T17:02:37.409496Z","iopub.status.idle":"2023-01-10T17:02:37.442052Z","shell.execute_reply.started":"2023-01-10T17:02:37.409459Z","shell.execute_reply":"2023-01-10T17:02:37.440693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img.size","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.443581Z","iopub.execute_input":"2023-01-10T17:02:37.443957Z","iopub.status.idle":"2023-01-10T17:02:37.455800Z","shell.execute_reply.started":"2023-01-10T17:02:37.443921Z","shell.execute_reply":"2023-01-10T17:02:37.454121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img=np.transpose(img,(2,0,1))","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.457806Z","iopub.execute_input":"2023-01-10T17:02:37.458455Z","iopub.status.idle":"2023-01-10T17:02:37.467968Z","shell.execute_reply.started":"2023-01-10T17:02:37.458419Z","shell.execute_reply":"2023-01-10T17:02:37.466302Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data[\"kfold\"]=-1\ntrain_data=train_data.sample(frac=1).reset_index(drop=True)\ny=train_data.target.values\nkf= StratifiedKFold(n_splits=5)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.470756Z","iopub.execute_input":"2023-01-10T17:02:37.471358Z","iopub.status.idle":"2023-01-10T17:02:37.509561Z","shell.execute_reply.started":"2023-01-10T17:02:37.471322Z","shell.execute_reply":"2023-01-10T17:02:37.508629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for fold,(t,v) in enumerate(kf.split(X=train_data,y=y)):\n    train_data.loc[v,\"kfold\"]=fold\n\ntrain_data.to_csv(\"train_folds.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.510823Z","iopub.execute_input":"2023-01-10T17:02:37.511361Z","iopub.status.idle":"2023-01-10T17:02:37.705300Z","shell.execute_reply.started":"2023-01-10T17:02:37.511328Z","shell.execute_reply":"2023-01-10T17:02:37.704099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(train_data[train_data[\"kfold\"]==4])","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.712222Z","iopub.execute_input":"2023-01-10T17:02:37.712699Z","iopub.status.idle":"2023-01-10T17:02:37.727801Z","shell.execute_reply.started":"2023-01-10T17:02:37.712667Z","shell.execute_reply":"2023-01-10T17:02:37.726927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fold=4\ndf_train=train_data[train_data.kfold!=fold].reset_index(drop=True)\ndf_valid=train_data[train_data.kfold==fold].reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:22:54.862452Z","iopub.execute_input":"2023-01-10T17:22:54.862810Z","iopub.status.idle":"2023-01-10T17:22:54.880510Z","shell.execute_reply.started":"2023-01-10T17:22:54.862777Z","shell.execute_reply":"2023-01-10T17:22:54.879623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:22:59.077817Z","iopub.execute_input":"2023-01-10T17:22:59.078526Z","iopub.status.idle":"2023-01-10T17:22:59.094561Z","shell.execute_reply.started":"2023-01-10T17:22:59.078492Z","shell.execute_reply":"2023-01-10T17:22:59.093311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_valid.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:23:01.399583Z","iopub.execute_input":"2023-01-10T17:23:01.399968Z","iopub.status.idle":"2023-01-10T17:23:01.416566Z","shell.execute_reply.started":"2023-01-10T17:23:01.399936Z","shell.execute_reply":"2023-01-10T17:23:01.415226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"warnings.simplefilter('ignore')\ndef seed_everything(seed):\n    random.seed(seed)\n    os.environ['PYTHONHASHSEED'] = str(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = True\n\nseed_everything(47)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.772492Z","iopub.execute_input":"2023-01-10T17:02:37.773511Z","iopub.status.idle":"2023-01-10T17:02:37.783073Z","shell.execute_reply.started":"2023-01-10T17:02:37.773476Z","shell.execute_reply":"2023-01-10T17:02:37.781622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device=\"cuda\" if torch.cuda.is_available() else \"cpu\"","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.786031Z","iopub.execute_input":"2023-01-10T17:02:37.787023Z","iopub.status.idle":"2023-01-10T17:02:37.909905Z","shell.execute_reply.started":"2023-01-10T17:02:37.786983Z","shell.execute_reply":"2023-01-10T17:02:37.908548Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class MelanomaData(Dataset):\n    def __init__(self,data,transform):\n        self.data=data\n        self.transform=transform\n        self.image_path=self.data.image_path.values.tolist()\n        self.label_path=self.data.target.values.tolist()\n        \n    def __len__(self):\n        return len(self.data)\n    \n    def __getitem__(self,index):\n        image_id=self.image_path[index]\n        lbl=self.label_path[index]\n        \n        image=mpimg.imread(image_id)\n        augmented=self.transform(image=image)\n        img=augmented[\"image\"]\n        img=np.transpose(img,(2,0,1))\n        return img,lbl","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.915537Z","iopub.execute_input":"2023-01-10T17:02:37.916041Z","iopub.status.idle":"2023-01-10T17:02:37.928054Z","shell.execute_reply.started":"2023-01-10T17:02:37.916004Z","shell.execute_reply":"2023-01-10T17:02:37.926508Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean = (0.485, 0.456, 0.406)\nstd = (0.229, 0.224, 0.225)\n\ntrain_aug=A.Compose([\n    A.Normalize(mean, std, max_pixel_value=255.0, always_apply=True),\n    A.ShiftScaleRotate(shift_limit=0.0625, scale_limit=0.1, rotate_limit=15),\n    A.Flip(p=0.5)\n])\n\nvalid_aug=A.Compose([\n    A.Normalize(mean, std, max_pixel_value=255.0, always_apply=True)\n])","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.932925Z","iopub.execute_input":"2023-01-10T17:02:37.933335Z","iopub.status.idle":"2023-01-10T17:02:37.943841Z","shell.execute_reply.started":"2023-01-10T17:02:37.933301Z","shell.execute_reply":"2023-01-10T17:02:37.942940Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset=MelanomaData(data=df_train,transform=train_aug)\nvalid_dataset=MelanomaData(data=df_valid,transform=valid_aug)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:23:11.632225Z","iopub.execute_input":"2023-01-10T17:23:11.632810Z","iopub.status.idle":"2023-01-10T17:23:11.647260Z","shell.execute_reply.started":"2023-01-10T17:23:11.632765Z","shell.execute_reply":"2023-01-10T17:23:11.646077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(dataset = train_dataset, batch_size = 20)\nval_loader = DataLoader(dataset = valid_dataset, batch_size = 20)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:23:15.554379Z","iopub.execute_input":"2023-01-10T17:23:15.554722Z","iopub.status.idle":"2023-01-10T17:23:15.560345Z","shell.execute_reply.started":"2023-01-10T17:23:15.554693Z","shell.execute_reply":"2023-01-10T17:23:15.559250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image,label=next(iter(train_loader))","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:37.971343Z","iopub.execute_input":"2023-01-10T17:02:37.972614Z","iopub.status.idle":"2023-01-10T17:02:38.370360Z","shell.execute_reply.started":"2023-01-10T17:02:37.972553Z","shell.execute_reply":"2023-01-10T17:02:38.369259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:38.375125Z","iopub.execute_input":"2023-01-10T17:02:38.377868Z","iopub.status.idle":"2023-01-10T17:02:38.388494Z","shell.execute_reply.started":"2023-01-10T17:02:38.377827Z","shell.execute_reply":"2023-01-10T17:02:38.387211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:38.393373Z","iopub.execute_input":"2023-01-10T17:02:38.396036Z","iopub.status.idle":"2023-01-10T17:02:38.409106Z","shell.execute_reply.started":"2023-01-10T17:02:38.395995Z","shell.execute_reply":"2023-01-10T17:02:38.407758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install pretrainedmodels","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:38.412720Z","iopub.execute_input":"2023-01-10T17:02:38.414583Z","iopub.status.idle":"2023-01-10T17:02:51.935603Z","shell.execute_reply.started":"2023-01-10T17:02:38.414546Z","shell.execute_reply":"2023-01-10T17:02:51.934413Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pretrainedmodels","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:51.937613Z","iopub.execute_input":"2023-01-10T17:02:51.939176Z","iopub.status.idle":"2023-01-10T17:02:53.304590Z","shell.execute_reply.started":"2023-01-10T17:02:51.939132Z","shell.execute_reply":"2023-01-10T17:02:53.303600Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class SEResnext50_32x4d(nn.Module):\n    def __init__(self, pretrained='imagenet'):\n        super(SEResnext50_32x4d, self).__init__()\n        \n        self.base_model = pretrainedmodels.__dict__[\n            \"se_resnext50_32x4d\"\n        ](pretrained=None)\n        if pretrained is not None:\n            self.base_model.load_state_dict(\n                torch.load(\n                    \"../input/pretrained-model-weights-pytorch/se_resnext50_32x4d-a260b3a4.pth\"\n                )\n            )\n\n        self.l0 = nn.Linear(2048, 1)\n    \n    def forward(self, image):\n        batch_size, _, _, _ = image.shape\n        \n        x = self.base_model.features(image)\n        x = F.adaptive_avg_pool2d(x, 1).reshape(batch_size, -1)\n        \n        out = self.l0(x)\n        #loss = nn.BCEWithLogitsLoss()(out, targets.view(-1, 1).type_as(x))\n\n        return out","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:53.306033Z","iopub.execute_input":"2023-01-10T17:02:53.306420Z","iopub.status.idle":"2023-01-10T17:02:53.315670Z","shell.execute_reply.started":"2023-01-10T17:02:53.306383Z","shell.execute_reply":"2023-01-10T17:02:53.314457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = SEResnext50_32x4d(pretrained=\"imagenet\")\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:53.317711Z","iopub.execute_input":"2023-01-10T17:02:53.318404Z","iopub.status.idle":"2023-01-10T17:02:58.410831Z","shell.execute_reply.started":"2023-01-10T17:02:53.318328Z","shell.execute_reply":"2023-01-10T17:02:58.409697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion=nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adam(model.parameters(), lr=1e-4)\nscheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer,\n                                                       patience=3,\n                                                       threshold=0.001,\n                                                       mode=\"max\")","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:58.412374Z","iopub.execute_input":"2023-01-10T17:02:58.412837Z","iopub.status.idle":"2023-01-10T17:02:58.420447Z","shell.execute_reply.started":"2023-01-10T17:02:58.412799Z","shell.execute_reply":"2023-01-10T17:02:58.419286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:58.422245Z","iopub.execute_input":"2023-01-10T17:02:58.422942Z","iopub.status.idle":"2023-01-10T17:02:58.434077Z","shell.execute_reply.started":"2023-01-10T17:02:58.422906Z","shell.execute_reply":"2023-01-10T17:02:58.433073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def train(dataloader,model,optimizer,criterion):\n    model.train()\n    train_loss=0\n    for item in tqdm(dataloader):\n        inputs=item[0]\n        labels=item[1].unsqueeze(-1)\n        \n        inputs=inputs.to(device,dtype=torch.float)\n        labels=labels.to(device,dtype=torch.float)\n        \n        optimizer.zero_grad()\n        outputs=model(inputs)\n        loss=criterion(outputs,labels)\n        train_loss+=loss\n        loss.backward()\n        optimizer.step()\n    return train_loss/len(dataloader)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:02:58.440149Z","iopub.execute_input":"2023-01-10T17:02:58.440402Z","iopub.status.idle":"2023-01-10T17:02:58.447361Z","shell.execute_reply.started":"2023-01-10T17:02:58.440378Z","shell.execute_reply":"2023-01-10T17:02:58.446006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def evaluate(dataloader, model, criterion):\n    model.eval()\n    eval_loss = 0\n    with torch.no_grad():\n        for item in tqdm(dataloader):\n            inputs = item[0]\n            labels = item[1].unsqueeze(-1)\n\n            inputs = inputs.to(device,dtype=torch.float)\n            labels = labels.to(device,dtype=torch.float)\n\n            outputs = model(inputs)\n            loss = criterion(outputs, labels)\n            eval_loss += loss.item()\n\n    return eval_loss/len(dataloader)","metadata":{"execution":{"iopub.status.busy":"2023-01-10T17:23:43.399954Z","iopub.execute_input":"2023-01-10T17:23:43.400310Z","iopub.status.idle":"2023-01-10T17:23:43.407555Z","shell.execute_reply.started":"2023-01-10T17:23:43.400281Z","shell.execute_reply":"2023-01-10T17:23:43.406397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs = 5\n\nfor epoch in range(epochs):\n\n    train_loss = train(train_loader,\n                       model,\n                       optimizer,\n                      criterion)\n\n    val_loss = evaluate(val_loader,\n                        model,\n                        criterion)\n\n    print(f'Epoch: {epoch+1}')\n    print(f'Training Loss: {train_loss}, \\t Validation Loss: {val_loss}\\n')","metadata":{"execution":{"iopub.status.busy":"2023-01-10T18:04:31.288293Z","iopub.execute_input":"2023-01-10T18:04:31.288657Z","iopub.status.idle":"2023-01-10T18:55:03.073725Z","shell.execute_reply.started":"2023-01-10T18:04:31.288619Z","shell.execute_reply":"2023-01-10T18:55:03.072728Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}