{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"!conda install -c conda-forge gdcm -y","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport torch\nimport glob\nimport gdcm\nfrom tqdm import tqdm\nimport torch.nn as nn\nimport torch.nn.functional as F\nimport torchvision\nfrom torch.utils.data import Dataset\nimport pydicom as dcm\nfrom torchvision import transforms\nfrom torch.utils.data.sampler import SubsetRandomSampler\nimport warnings\nwarnings.filterwarnings('ignore')\nimport gc\nfrom skimage.transform import resize","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"path=\"../input/rsna-str-pulmonary-embolism-detection/\"\ntrain=pd.read_csv(path+\"train.csv\")\ntest=pd.read_csv(path+\"test.csv\")\nsub=pd.read_csv(path+\"sample_submission.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"print(\"shape of train dataframe : {}\".format(train.shape))\nprint(\"shape of test dataframe : {}\".format(test.shape))\nprint(\"shape of submission dataframe : {}\".format(sub.shape))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(8, 8))\nimg=dcm.dcmread(\"../input/rsna-str-pulmonary-embolism-detection/train/4833c9b6a5d0/57e3e3c5f910/f4fdc88f2ace.dcm\").pixel_array\nax.imshow(img)\nprint(img.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class bw_to_rgb():\n    def __call__(self,array):\n        array = array.reshape((128, 128, 1))\n        return np.stack([array, array, array], axis=2).reshape((128, 128, 3))\n    def __repr__(self):\n        return self.__class__.__name__ + '()'\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class getdata(Dataset):\n    def __init__(self,df,mode=\"train\",transform=None):\n        self.mode=mode\n        self.transform=transform\n        self.df=df\n        self.path=\"../input/rsna-str-pulmonary-embolism-detection/\"\n    def __getitem__(self,idx):\n        fnames = self.df[['StudyInstanceUID', 'SeriesInstanceUID', 'SOPInstanceUID']]\n        if self.mode==\"train\":\n            stuid=fnames.loc[idx].values[0]\n            siuid=fnames.loc[idx].values[1]\n            souid=fnames.loc[idx].values[2]\n            img=dcm.dcmread(self.path+\"train/\"+stuid+\"/\"+siuid+\"/\"+souid+\".dcm\").pixel_array\n            img = resize(img, (128,128), anti_aliasing=True).astype('float')\n            #img=img.reshape((512,512,1)).astype('float')\n            \n            y=self.df[['negative_exam_for_pe', 'rv_lv_ratio_gte_1', 'rv_lv_ratio_lt_1',\n                     'leftsided_pe', 'chronic_pe', 'rightsided_pe',\n                     'acute_and_chronic_pe', 'central_pe', 'indeterminate']].loc[idx].values\n            if self.transform:\n                img=transform(img)\n            return img,y\n        else:\n            stuid=fnames.loc[idx].values[0]\n            siuid=fnames.loc[idx].values[1]\n            souid=fnames.loc[idx].values[2]\n            img=dcm.dcmread(self.path+\"test/\"+stuid+\"/\"+siuid+\"/\"+souid+\".dcm\").pixel_array\n            img = resize(img, (128,128), anti_aliasing=True).astype('float')\n            #img=img.reshape((512,512,1)).astype('float')\n            if self.transform:\n                img=transform(img)\n            return img\n    def __len__(self):\n        return len(self.df)\n            ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# img=next(iter(getdata(test,mode=\"test\",transform=transform)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"transform=transforms.Compose([\n    bw_to_rgb(),\n    transforms.ToTensor()\n])\n\ntrain_ds=getdata(train,transform=transform)\nindices=list(range(len(train_ds)))\nsplit = int(np.floor(0.35 * len(train_ds)))\nnp.random.shuffle(indices)\ntrain_indices, val_indices = indices[split:], indices[:split]\n\n\ntrain_sampler = SubsetRandomSampler(train_indices)\nvalid_sampler = SubsetRandomSampler(val_indices)\n\n\ntrain_loader = torch.utils.data.DataLoader(train_ds, batch_size=64, \n                                           sampler=train_sampler)\nvalidation_loader = torch.utils.data.DataLoader(train_ds, batch_size=64,\n                                                sampler=valid_sampler)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"len(train_loader),len(train_ds)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for a,(i,y) in enumerate(train_loader):\n    print(i.shape)\n    print(y.shape)\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"y[:,0]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model=model.cpu()\n# i=i.cpu()\n# model(i)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"class train_model(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.cn1=nn.Conv2d(3,32,kernel_size=(3,3),padding=1,stride=1)\n        self.cn2=nn.Conv2d(32,60,kernel_size=(3,3),padding=1,stride=1)\n        self.cn3=nn.Conv2d(60,128,kernel_size=(3,3),padding=1,stride=1)\n        self.cn4=nn.Conv2d(128,128,kernel_size=(3,3),padding=1,stride=1)\n        self.pool=nn.MaxPool2d(kernel_size=(2,2))\n        \n        self.l1=nn.Linear(8192,128)\n        self.l2=nn.Linear(128,100)\n        self.l3=nn.Linear(100,9)\n        \n        \n    def forward(self,x):\n        x=self.pool(self.cn1(x))\n        x=self.pool(self.cn2(x))\n        x=self.pool(self.cn3(x))\n        x=self.pool(self.cn4(x))\n        x=x.view(x.shape[0],-1)\n        x=F.relu(self.l1(x))\n        x=F.relu(self.l2(x))\n        x=self.l3(x)\n        return x","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"# model=train_model()\n# x=model(i.float())\n# x.shape\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"gc.collect()\ndevice='cuda' if torch.cuda.is_available() else 'cpu'\nmodel=train_model()\nmodel=model.to(device)\noptimizer=torch.optim.Adam(model.parameters())\nloss=nn.BCEWithLogitsLoss()\nepochs=1\n\nfor epoch in range(epochs):\n    total_loss=0\n    model.train()\n    for i,(img,y) in tqdm(enumerate(train_loader)):\n        optimizer.zero_grad()\n        img,y=img.float().to(device),y.float().to(device)\n        outputs=model(img)\n        loss_train= loss(outputs,y)\n        total_loss += loss_train.item()\n        loss_train.backward()\n        optimizer.step()\n    print(\"*\"*80)\n    print(\"\")\n    print(\"Train\")\n    print(\"\")\n    print(total_loss/len(train_loader))\n    print(\"\")\n    model.eval()\n    for i,(img,y) in enumerate(test_loader):\n        \n        img,y=img.float().to(device),y.to(device)\n        outputs=model(img)\n        loss_train= loss(outputs, y.float())\n        total_loss += loss_train.item()\n    print(\"val loss : \",loss_train)\n    print(\"\")\n    print(\"*\"*80)\n    print(\"*\"*80)\n    print(\"\")\n    print(epoch+\" : \"+ total_loss/len(test_loader))\n    print(\"\")\n    print(\"*\"*80)\n    print(\"*\"*80)\n    ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for i,(img,y) in tqdm(enumerate(train_loader)):\n    img,y=img.float().to(device),y.float().to(device)\n    outputs=model(img)\n    break","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"img.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}