{"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":"# ! pip install -U pylibjpeg pylibjpeg-openjpeg pylibjpeg-libjpeg pydicom python-gdcm\n# ! pip install --upgrade pydicom","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pip install python-gdcm","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:06:42.800024Z","iopub.execute_input":"2023-02-23T23:06:42.80041Z","iopub.status.idle":"2023-02-23T23:06:54.526401Z","shell.execute_reply.started":"2023-02-23T23:06:42.800376Z","shell.execute_reply":"2023-02-23T23:06:54.52512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nimport torch\nfrom torchvision.io import read_image\nfrom sklearn.model_selection import train_test_split\nimport pydicom\nfrom pydicom.data import get_testdata_file\nimport re\nimport gc\nimport cv2\nimport wandb\nimport random\nimport math\nfrom glob import glob\nfrom tqdm import tqdm\nfrom pprint import pprint\nfrom time import time\nimport datetime as dtime\nfrom datetime import datetime\nimport itertools\nimport warnings\nimport pydicom # for DICOM images\nfrom skimage.transform import resize\nfrom sklearn.preprocessing import LabelEncoder, normalize\n\n# For the Visuals\nimport seaborn as sns\nimport matplotlib as mpl\nfrom matplotlib import cm\nimport matplotlib.patches as patches\nimport matplotlib.pyplot as plt\n\nfrom skimage.color import gray2rgb\n\nfrom torchvision.models import resnet34, resnet50\n\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torch.optim as optim\nimport torchvision\nimport torchvision.transforms as transforms\n\nimport torch.nn as nn\nfrom torchvision import datasets, models, transforms\nfrom torchvision.transforms import transforms  ##  Custome Dataset\nimport math","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:06:54.529118Z","iopub.execute_input":"2023-02-23T23:06:54.52953Z","iopub.status.idle":"2023-02-23T23:06:58.188435Z","shell.execute_reply.started":"2023-02-23T23:06:54.529475Z","shell.execute_reply":"2023-02-23T23:06:58.187096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Augmentation_transform = transforms.Compose([\n    transforms.RandomAdjustSharpness(p=0.25, sharpness_factor=1),\n    transforms.RandomHorizontalFlip(p=0.25),\n    transforms.RandomApply(torch.nn.ModuleList([transforms.GaussianBlur(kernel_size=(3, 3), sigma=(0.1, 2.0))]), p=0.1),\n    transforms.RandomApply(torch.nn.ModuleList([transforms.ColorJitter()]), p=0.25),\n    transforms.RandomRotation(degrees= (0, 30)),\n                                            ])","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:06:58.193817Z","iopub.execute_input":"2023-02-23T23:06:58.196282Z","iopub.status.idle":"2023-02-23T23:06:58.207366Z","shell.execute_reply.started":"2023-02-23T23:06:58.196221Z","shell.execute_reply":"2023-02-23T23:06:58.205313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# data loader class in pytorch\nfrom torch.utils.data import Dataset\nimport torch\n\nclass BreastCancerData(Dataset):\n    \n    def __init__(self,\n                 root_dir=\"/kaggle/input/rsna-mammography-images-as-pngs/images_as_pngs_1024/train_images_processed_1024/\",\n                 split=\"train\", target_size=(256,256), n=-1, transform_Aug=None):\n        # root_dir: directory of csv file\n        # split: train, or test\n        self.target_size = target_size\n        self.root_dir = root_dir\n        self.split = split\n        self.data_csv = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/\" \n                                    + \"{}.csv\".format(split)) \n        self.transform_Aug = transform_Aug\n        \n        if n!=-1:\n            self.data_csv = self.data_csv[:n]\n            \n    def __len__(self):\n        # returns the size of the dataset\n        return len(self.data_csv)\n    \n    def __getitem__(self, idx):\n        # returns the image and label of the sample number idx\n        patient_id = self.data_csv['patient_id'][idx]\n        img_id = self.data_csv['image_id'][idx]\n        \n        x = os.path.join(self.root_dir +str(patient_id) + '/' + str(img_id))\n        x = cv2.imread(x + \".png\")\n        # resize img\n        x = cv2.resize(x,self.target_size)\n\n        # normalize\n        x = (x - x.min()) / (x.max() - x.min())\n        #convert to tensor -- pytorch requires images to be in the size (C,H,W)\n          #where C: number of channels, H: hight, W:Width''''\n        x = np.transpose(x,(2,0,1))\n        x = torch.from_numpy(x).type(torch.FloatTensor)\n        \n        label = self.data_csv['cancer'][idx]\n        label = np.array([label])\n        label = torch.from_numpy(label).type(torch.FloatTensor)\n        \n        if self.transform_Aug and label==1:\n            x=self.transform_Aug(x)\n            \n        return x, label","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:06:58.213936Z","iopub.execute_input":"2023-02-23T23:06:58.216739Z","iopub.status.idle":"2023-02-23T23:06:58.232937Z","shell.execute_reply.started":"2023-02-23T23:06:58.216699Z","shell.execute_reply":"2023-02-23T23:06:58.231945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:06:58.237839Z","iopub.execute_input":"2023-02-23T23:06:58.240626Z","iopub.status.idle":"2023-02-23T23:06:58.318141Z","shell.execute_reply.started":"2023-02-23T23:06:58.240588Z","shell.execute_reply":"2023-02-23T23:06:58.316977Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = BreastCancerData(n=-1, target_size=(512,512), transform_Aug=Augmentation_transform)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:52.441764Z","iopub.execute_input":"2023-02-23T23:25:52.442148Z","iopub.status.idle":"2023-02-23T23:25:52.525127Z","shell.execute_reply.started":"2023-02-23T23:25:52.442115Z","shell.execute_reply":"2023-02-23T23:25:52.524129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset[0][0][0]","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:54.07143Z","iopub.execute_input":"2023-02-23T23:25:54.071795Z","iopub.status.idle":"2023-02-23T23:25:54.099253Z","shell.execute_reply.started":"2023-02-23T23:25:54.071765Z","shell.execute_reply":"2023-02-23T23:25:54.098271Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.resnet50(pretrained=True).to(device)\n    \nfor param in model.parameters():\n    param.requires_grad = True    \n    \nmodel.fc = nn.Sequential(\n               nn.Linear(2048, 256),\n               nn.ReLU(inplace=True),\n               nn.Linear(256, 1)).to(device)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:55.851649Z","iopub.execute_input":"2023-02-23T23:25:55.85201Z","iopub.status.idle":"2023-02-23T23:25:56.418839Z","shell.execute_reply.started":"2023-02-23T23:25:55.851979Z","shell.execute_reply":"2023-02-23T23:25:56.417757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Scaling by total/2 helps keep the loss to a similar magnitude.\n# # The sum of the weights of all examples stays the same.\n# train_csv = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")\n\n# counts = train_csv.cancer.value_counts()\n# neg = counts[0]\n# pos = counts[1]\n# total = neg + pos\n\n# weight_for_0 = (1 / neg) * (total / 2.0)\n# weight_for_1 = (1 / pos) * (total / 2.0)\n\n# class_weight = {0: weight_for_0, 1: weight_for_1}\n\n# # pairs in the dictionary\n# result = class_weight.items()\n \n# # Convert object to a list\n# data = list(result)\n \n# # Convert list to an array\n# numpyArray = np.array(data)\n\n# class_weight=torch.from_numpy(numpyArray)\n\n# # print('Weight for class 0: {:.2f}'.format(weight_for_0))\n# # print('Weight for class 1: {:.2f}'.format(weight_for_1))","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:56.421108Z","iopub.execute_input":"2023-02-23T23:25:56.421618Z","iopub.status.idle":"2023-02-23T23:25:56.427923Z","shell.execute_reply.started":"2023-02-23T23:25:56.421579Z","shell.execute_reply":"2023-02-23T23:25:56.426117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd_train  = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/train.csv\")\ny_train = pd_train.cancer\n\nclass_sample_count = np.array(\n    [len(np.where(y_train == t)[0]) for t in np.unique(y_train)])\n\nweight = 1. / class_sample_count\nsamples_weight = np.array([weight[t] for t in y_train])\nsamples_weight = torch.from_numpy(samples_weight)\n\nsampler = torch.utils.data.sampler.WeightedRandomSampler(samples_weight.type('torch.DoubleTensor'), len(samples_weight),\n                                                         replacement=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:56.430411Z","iopub.execute_input":"2023-02-23T23:25:56.430699Z","iopub.status.idle":"2023-02-23T23:25:56.504888Z","shell.execute_reply.started":"2023-02-23T23:25:56.430673Z","shell.execute_reply":"2023-02-23T23:25:56.503934Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torch.utils.data import DataLoader\nbatch_size = 16\ntrain_dataloader = DataLoader(train_dataset, batch_size=batch_size, sampler=sampler,\n                             num_workers=2, pin_memory=True)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:56.596036Z","iopub.execute_input":"2023-02-23T23:25:56.596413Z","iopub.status.idle":"2023-02-23T23:25:56.602471Z","shell.execute_reply.started":"2023-02-23T23:25:56.59638Z","shell.execute_reply":"2023-02-23T23:25:56.601426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.BCEWithLogitsLoss() \noptimizer = torch.optim.Adam(model.parameters(), lr=1e-5)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:57.143312Z","iopub.execute_input":"2023-02-23T23:25:57.143678Z","iopub.status.idle":"2023-02-23T23:25:57.149968Z","shell.execute_reply.started":"2023-02-23T23:25:57.143647Z","shell.execute_reply":"2023-02-23T23:25:57.148995Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def accuracy(x,y):\n    return (x==y).sum() / len(x)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:57.445399Z","iopub.execute_input":"2023-02-23T23:25:57.445773Z","iopub.status.idle":"2023-02-23T23:25:57.450275Z","shell.execute_reply.started":"2023-02-23T23:25:57.445742Z","shell.execute_reply":"2023-02-23T23:25:57.449317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:57.696193Z","iopub.execute_input":"2023-02-23T23:25:57.697178Z","iopub.status.idle":"2023-02-23T23:25:57.702076Z","shell.execute_reply.started":"2023-02-23T23:25:57.69713Z","shell.execute_reply":"2023-02-23T23:25:57.701087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from timeit import default_timer as timer\nfrom sklearn.metrics import f1_score\n\nepochs = 10\nmodel.train()\n\nfor e in range(epochs):\n    running_loss = 0.0\n    start = timer()\n    for x, label in train_dataloader: \n        \n        optimizer.zero_grad()\n        x = x.to(device)\n        label = label.to(device)\n        out = model(x).sigmoid()\n        pred = torch.ge(out, 0.5)\n\n        loss_value = criterion(out, label)\n\n        loss_value.backward()\n        optimizer.step()\n\n        running_loss += loss_value.item()\n        #print('Loss:{}'.format(loss_value.item()))\n        \n       #acc = accuracy(pred,label)\n       #print(acc)\n    \n    print('F1 score:', f1_score(label.cpu(), pred.cpu(), average='weighted')) \n    print('epoch Loss:',running_loss / len(train_dataloader)) \n    total_time = timer() - start\n    print(\"time:\", total_time)","metadata":{"execution":{"iopub.status.busy":"2023-02-23T23:25:57.91675Z","iopub.execute_input":"2023-02-23T23:25:57.91712Z","iopub.status.idle":"2023-02-24T04:34:06.115508Z","shell.execute_reply.started":"2023-02-23T23:25:57.917089Z","shell.execute_reply":"2023-02-24T04:34:06.114343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!mkdir models\n!mkdir models/pytorch\n\ntorch.save(model.state_dict(), 'models/pytorch/weights.pt')","metadata":{"execution":{"iopub.status.busy":"2023-02-24T04:34:06.124397Z","iopub.execute_input":"2023-02-24T04:34:06.126637Z","iopub.status.idle":"2023-02-24T04:34:08.630375Z","shell.execute_reply.started":"2023-02-24T04:34:06.126594Z","shell.execute_reply":"2023-02-24T04:34:08.628966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Test data","metadata":{}},{"cell_type":"code","source":"# test = pd.read_csv(\"/kaggle/input/rsna-breast-cancer-detection/test.csv\")\n# test_path = \"/kaggle/input/rsna-breast-cancer-detection/test_images/\"\n# all_test = []\n# for k in tqdm(range(len(test))):\n#     row = test.iloc[k, :]\n#     all_test.append(test_path + str(row.patient_id) + \"/\" + str(row.image_id) + \".dcm\")","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_imgs=[]\n# for i in range(len(all_test)):\n#     img= pydicom.dcmread(all_test[i]).pixel_array.astype(float)\n#     test_imgs.append(img)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# plt.imshow(test_imgs[0])","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}