{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\n\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-13T10:06:47.443897Z","iopub.execute_input":"2023-02-13T10:06:47.444776Z","iopub.status.idle":"2023-02-13T10:06:47.449363Z","shell.execute_reply.started":"2023-02-13T10:06:47.44474Z","shell.execute_reply":"2023-02-13T10:06:47.44836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tqdm import tqdm","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.451147Z","iopub.execute_input":"2023-02-13T10:06:47.451623Z","iopub.status.idle":"2023-02-13T10:06:47.466187Z","shell.execute_reply.started":"2023-02-13T10:06:47.451589Z","shell.execute_reply":"2023-02-13T10:06:47.465331Z"},"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-13T10:06:47.467595Z","iopub.execute_input":"2023-02-13T10:06:47.468212Z","iopub.status.idle":"2023-02-13T10:06:47.554628Z","shell.execute_reply.started":"2023-02-13T10:06:47.468177Z","shell.execute_reply":"2023-02-13T10:06:47.553388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['cancer'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.55756Z","iopub.execute_input":"2023-02-13T10:06:47.558004Z","iopub.status.idle":"2023-02-13T10:06:47.566767Z","shell.execute_reply.started":"2023-02-13T10:06:47.55797Z","shell.execute_reply":"2023-02-13T10:06:47.565622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"POS = 1158/53548","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.568383Z","iopub.execute_input":"2023-02-13T10:06:47.568728Z","iopub.status.idle":"2023-02-13T10:06:47.576238Z","shell.execute_reply.started":"2023-02-13T10:06:47.568702Z","shell.execute_reply":"2023-02-13T10:06:47.575349Z"},"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-13T10:06:47.577215Z","iopub.execute_input":"2023-02-13T10:06:47.577477Z","iopub.status.idle":"2023-02-13T10:06:47.58664Z","shell.execute_reply.started":"2023-02-13T10:06:47.577446Z","shell.execute_reply":"2023-02-13T10:06:47.585812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import Dataset,DataLoader","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.589484Z","iopub.execute_input":"2023-02-13T10:06:47.589797Z","iopub.status.idle":"2023-02-13T10:06:47.595632Z","shell.execute_reply.started":"2023-02-13T10:06:47.589772Z","shell.execute_reply":"2023-02-13T10:06:47.594993Z"},"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","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.596292Z","iopub.execute_input":"2023-02-13T10:06:47.59653Z","iopub.status.idle":"2023-02-13T10:06:47.607564Z","shell.execute_reply.started":"2023-02-13T10:06:47.596507Z","shell.execute_reply":"2023-02-13T10:06:47.605729Z"},"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-13T10:06:47.632912Z","iopub.execute_input":"2023-02-13T10:06:47.633282Z","iopub.status.idle":"2023-02-13T10:06:47.636967Z","shell.execute_reply.started":"2023-02-13T10:06:47.633252Z","shell.execute_reply":"2023-02-13T10:06:47.636286Z"},"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-13T10:06:47.638835Z","iopub.execute_input":"2023-02-13T10:06:47.63935Z","iopub.status.idle":"2023-02-13T10:06:47.651104Z","shell.execute_reply.started":"2023-02-13T10:06:47.639292Z","shell.execute_reply":"2023-02-13T10:06:47.65003Z"},"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-13T10:06:47.653457Z","iopub.execute_input":"2023-02-13T10:06:47.653753Z","iopub.status.idle":"2023-02-13T10:06:47.660942Z","shell.execute_reply.started":"2023-02-13T10:06:47.653729Z","shell.execute_reply":"2023-02-13T10:06:47.660164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_df = train_df.reset_index(drop=True)\ndf_train, df_val = train_test_split(train_df, test_size=0.25)\ndf_train = df_train.reset_index(drop=True)\ndf_val = df_val.reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.661703Z","iopub.execute_input":"2023-02-13T10:06:47.661981Z","iopub.status.idle":"2023-02-13T10:06:47.684815Z","shell.execute_reply.started":"2023-02-13T10:06:47.661957Z","shell.execute_reply":"2023-02-13T10:06:47.684011Z"},"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-13T10:06:47.689406Z","iopub.execute_input":"2023-02-13T10:06:47.689722Z","iopub.status.idle":"2023-02-13T10:06:47.694387Z","shell.execute_reply.started":"2023-02-13T10:06:47.689692Z","shell.execute_reply":"2023-02-13T10:06:47.693519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_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-13T10:06:47.695335Z","iopub.execute_input":"2023-02-13T10:06:47.696123Z","iopub.status.idle":"2023-02-13T10:06:47.703692Z","shell.execute_reply.started":"2023-02-13T10:06:47.696094Z","shell.execute_reply":"2023-02-13T10:06:47.703061Z"},"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-13T10:06:47.704773Z","iopub.execute_input":"2023-02-13T10:06:47.705606Z","iopub.status.idle":"2023-02-13T10:06:47.714182Z","shell.execute_reply.started":"2023-02-13T10:06:47.705545Z","shell.execute_reply":"2023-02-13T10:06:47.713407Z"},"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-13T10:06:47.715142Z","iopub.execute_input":"2023-02-13T10:06:47.715504Z","iopub.status.idle":"2023-02-13T10:06:47.72819Z","shell.execute_reply.started":"2023-02-13T10:06:47.715481Z","shell.execute_reply":"2023-02-13T10:06:47.727335Z"},"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-13T10:06:47.729403Z","iopub.execute_input":"2023-02-13T10:06:47.729653Z","iopub.status.idle":"2023-02-13T10:06:47.738522Z","shell.execute_reply.started":"2023-02-13T10:06:47.72963Z","shell.execute_reply":"2023-02-13T10:06:47.737716Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\"\nclass 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    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))\n\"\"\"    ","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.739432Z","iopub.execute_input":"2023-02-13T10:06:47.73967Z","iopub.status.idle":"2023-02-13T10:06:47.749089Z","shell.execute_reply.started":"2023-02-13T10:06:47.739642Z","shell.execute_reply":"2023-02-13T10:06:47.748292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\ndef 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\n    \"\"\"\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.750077Z","iopub.execute_input":"2023-02-13T10:06:47.750315Z","iopub.status.idle":"2023-02-13T10:06:47.76283Z","shell.execute_reply.started":"2023-02-13T10:06:47.750293Z","shell.execute_reply":"2023-02-13T10:06:47.761918Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import gc\ngc.collect()\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:47.764341Z","iopub.execute_input":"2023-02-13T10:06:47.764576Z","iopub.status.idle":"2023-02-13T10:06:48.007328Z","shell.execute_reply.started":"2023-02-13T10:06:47.764553Z","shell.execute_reply":"2023-02-13T10:06:48.006054Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install efficientnet_pytorch","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:48.008584Z","iopub.execute_input":"2023-02-13T10:06:48.00893Z","iopub.status.idle":"2023-02-13T10:06:56.494844Z","shell.execute_reply.started":"2023-02-13T10:06:48.008896Z","shell.execute_reply":"2023-02-13T10:06:56.493862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\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\nfor param in model.parameters():\n    param.requires_grad = False\nmodel._fc.requires_grad = True\n\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","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:56.4994Z","iopub.execute_input":"2023-02-13T10:06:56.499748Z","iopub.status.idle":"2023-02-13T10:06:56.564504Z","shell.execute_reply.started":"2023-02-13T10:06:56.499717Z","shell.execute_reply":"2023-02-13T10:06:56.563826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#criterion = nn.CrossEntropyLoss()\nimport torch.optim as optim\n\noptimizer = optim.SGD(model.parameters(), lr=0.001, momentum=0.9)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:56.56572Z","iopub.execute_input":"2023-02-13T10:06:56.566087Z","iopub.status.idle":"2023-02-13T10:06:56.572383Z","shell.execute_reply.started":"2023-02-13T10:06:56.566054Z","shell.execute_reply":"2023-02-13T10:06:56.571307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for epoch in range(4):\n    running_loss = 0.0\n    running_corrects = 0\n    total = 0\n    \n    for i, data in enumerate(target_dataset, 0):\n        inputs, labels = data\n        optimizer.zero_grad()\n       # print(labels.shape)\n        labels = labels.unsqueeze(1)\n        outputs = model(inputs)\n        k  = nn.Sigmoid()\n        outputs = k(outputs)\n        outputs_copy = outputs.clone().detach()\n        for i in range(len(outputs_copy)):\n            if (outputs_copy[i]>0.5):\n                outputs_copy[i] = 1\n            else:\n                outputs_copy[i] = 0\n        k  = nn.Sigmoid()\n        torch.autograd.set_detect_anomaly(True)\n       # outputs = k(outputs.clone())\n       # print(labels)\n#         for i in range(len(outputs)):\n            \n            \n#             if (outputs[i]>0.5):\n#                 outputs[i] = 1\n#             else:\n#                 outputs[i] = 0\n       # print(outputs_copy)\n             # outputs[i] = 0\n        loss = F.binary_cross_entropy_with_logits(outputs_copy, labels,pos_weight=torch.tensor(POS))\n        loss = loss.requires_grad_(requires_grad=True)\n        #loss = (outputs, labels)\n        loss.backward()\n        optimizer.step()\n\n        running_loss += loss.item()\n        _, preds = torch.max(outputs_copy, 1)\n        total += labels.size(0)\n        running_corrects += (preds == labels).sum().item()\n\n    accuracy = running_corrects / total\n    print(f\"Epoch {epoch+1} loss: {running_loss / len(target_dataset)} accuracy: {accuracy}\")\n\n   # print(f\"Epoch {epoch+1} loss: {running_loss / len(dataloader)}\")\n","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:06:56.574201Z","iopub.execute_input":"2023-02-13T10:06:56.574739Z","iopub.status.idle":"2023-02-13T10:07:23.185686Z","shell.execute_reply.started":"2023-02-13T10:06:56.574706Z","shell.execute_reply":"2023-02-13T10:07:23.184781Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:07:23.186572Z","iopub.status.idle":"2023-02-13T10:07:23.186914Z","shell.execute_reply.started":"2023-02-13T10:07:23.186741Z","shell.execute_reply":"2023-02-13T10:07:23.18676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"torch.save(model.state_dict(),\"checknew.pth\")","metadata":{"execution":{"iopub.status.busy":"2023-02-13T10:07:23.18822Z","iopub.status.idle":"2023-02-13T10:07:23.188519Z","shell.execute_reply.started":"2023-02-13T10:07:23.188371Z","shell.execute_reply":"2023-02-13T10:07:23.188387Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}