{"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 git+https://github.com/shijianjian/EfficientNet-PyTorch-3D\nimport numpy as np\n\nimport pandas as pd \n\nimport os\n\nimport matplotlib.pyplot as plt\n\nimport pydicom\n\nfrom glob import glob\n\nimport gc\n\nimport plotly.express as px\n\nimport cv2  \n\nimport math\nimport torch\nimport torch.nn as nn\nimport torch.nn.functional as F\nfrom math import sqrt\nfrom collections import OrderedDict\nfrom torch.utils.data import Dataset, DataLoader\n\n# from efficientnet_pytorch_3d import EfficientNet3D","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-14T19:03:29.602051Z","iopub.execute_input":"2021-09-14T19:03:29.603358Z","iopub.status.idle":"2021-09-14T19:03:34.368750Z","shell.execute_reply.started":"2021-09-14T19:03:29.603089Z","shell.execute_reply":"2021-09-14T19:03:34.367630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F\n\nclass Snet(nn.Module):\n    def __init__(self):\n        super().__init__()\n\n        self.features = nn.Sequential(\n            nn.Conv3d(in_channels=1, out_channels=4, kernel_size=(7,7,7), stride=(1,1,1), padding=(1,1,1), bias=False),\n            nn.Tanh(),\n            nn.MaxPool3d((2, 2, 2), stride=(2, 2, 2)),\n\n            nn.Conv3d(in_channels=4, out_channels=8, kernel_size=(5,5,5), stride=(1,1,1), padding=(1,1,1), bias=False),\n            nn.Tanh(),\n            nn.MaxPool3d((2, 2, 2), stride=(2, 2, 2)),\n\n            nn.Conv3d(in_channels=8, out_channels=16, kernel_size=(3,3,3), stride=(1,1,1), padding=(1,1,1), bias=False),\n            nn.Tanh(),\n            nn.MaxPool3d((2, 2, 2), stride=(2, 2, 2)),\n        )\n\n        self.ReLU = nn.ReLU(inplace=True)\n        self.Dropout = nn.Dropout(p=0.7)\n        self.fc6 = nn.Linear(16 * 11 * 17 * 17, 256)\n        self.fc7 = nn.Linear(256, 128)\n        self.fc2 = nn.Linear(128, 1)\n        \n    def forward(self, input):\n        out = self.features(input) \n        out = out.view(out.size(0), -1)\n        \n        out = self.fc6(out)\n        out = self.ReLU(out)\n        out = self.Dropout(out)\n\n        out = self.fc7(out)\n        out = self.ReLU(out)\n        out = self.Dropout(out)\n        \n        out = self.fc2(out)\n\n        return out","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:34.373414Z","iopub.execute_input":"2021-09-14T19:03:34.373804Z","iopub.status.idle":"2021-09-14T19:03:34.388358Z","shell.execute_reply.started":"2021-09-14T19:03:34.373766Z","shell.execute_reply":"2021-09-14T19:03:34.387084Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n\n# model = EfficientNet3D.from_name(\"efficientnet-b0\", override_params={'num_classes': 1}, in_channels=1).to(device)\n\nmodel = Snet().to(device)\n\nprint(device)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:34.391355Z","iopub.execute_input":"2021-09-14T19:03:34.392118Z","iopub.status.idle":"2021-09-14T19:03:40.046337Z","shell.execute_reply.started":"2021-09-14T19:03:34.392057Z","shell.execute_reply":"2021-09-14T19:03:40.044929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"criterion = nn.BCEWithLogitsLoss()\noptimizer = torch.optim.Adagrad(model.parameters(), lr=0.01, lr_decay=0.0001,eps=1e-08)","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:40.048571Z","iopub.execute_input":"2021-09-14T19:03:40.049093Z","iopub.status.idle":"2021-09-14T19:03:40.059700Z","shell.execute_reply.started":"2021-09-14T19:03:40.049052Z","shell.execute_reply":"2021-09-14T19:03:40.058461Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data_path1 = glob(f'/kaggle/input/btrc-pp-flair-1/*.npz')","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:40.063923Z","iopub.execute_input":"2021-09-14T19:03:40.064255Z","iopub.status.idle":"2021-09-14T19:03:40.075176Z","shell.execute_reply.started":"2021-09-14T19:03:40.064226Z","shell.execute_reply":"2021-09-14T19:03:40.073866Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class trainData(Dataset):\n    \n    def __init__(self, X_data, y_data):\n        self.X_data = X_data\n        self.y_data = y_data\n        \n    def __getitem__(self, index):\n        return self.X_data[index], self.y_data[index]\n        \n    def __len__ (self):\n        return len(self.X_data)\n\n\n# train_data = trainData(torch.FloatTensor(X_train), torch.FloatTensor(y_train))\n## test data    \nclass testData(Dataset):\n    \n    def __init__(self, X_data):\n        self.X_data = X_data\n        \n    def __getitem__(self, index):\n        return self.X_data[index]\n        \n    def __len__ (self):\n        return len(self.X_data)\n    \n\n# test_data = testData(torch.FloatTensor(X_test))","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:40.078921Z","iopub.execute_input":"2021-09-14T19:03:40.079297Z","iopub.status.idle":"2021-09-14T19:03:40.089532Z","shell.execute_reply.started":"2021-09-14T19:03:40.079264Z","shell.execute_reply":"2021-09-14T19:03:40.087851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def binary_acc(y_pred, y_test):\n    y_pred_tag = torch.round(torch.sigmoid(y_pred))\n\n    correct_results_sum = (y_pred_tag == y_test).sum().float()\n    acc = correct_results_sum/y_test.shape[0]\n    acc = torch.round(acc * 100)\n    \n    return acc","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:40.091754Z","iopub.execute_input":"2021-09-14T19:03:40.092336Z","iopub.status.idle":"2021-09-14T19:03:40.104719Z","shell.execute_reply.started":"2021-09-14T19:03:40.092274Z","shell.execute_reply":"2021-09-14T19:03:40.103599Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for t in range(1, 81):\n    epoch_loss = 0\n    epoch_acc = 0\n    model.train()\n    \n    mnb = 0\n    nji = 0\n    \n    for i in range(len(data_path1)-1):\n        \n        data1 = np.load(data_path1[i])        \n        data = data1['arr_0']\n        label = data1['arr_1']\n        \n        del data1\n        gc.collect()\n        \n        train_data = trainData(torch.FloatTensor(data), torch.FloatTensor(label))\n        \n        del data, label\n        gc.collect()\n        \n        train_loader = DataLoader(dataset=train_data, batch_size=32, shuffle=True)\n        \n        del train_data\n        gc.collect()\n        \n        for X_batch, y_batch in train_loader:\n            X_batch, y_batch = X_batch.to(device), y_batch.to(device)\n            optimizer.zero_grad()\n            y_pred = model(X_batch)\n        \n            loss = criterion(y_pred, torch.reshape(y_batch, (-1,1)))\n            acc = binary_acc(y_pred, torch.reshape(y_batch, (-1,1)))\n\n            loss.backward()\n            optimizer.step()\n        \n            epoch_loss += loss.item()\n            epoch_acc += acc.item()\n            \n            mnb +=1\n            \n            del X_batch,y_batch\n            gc.collect()\n            torch.cuda.empty_cache()\n            \n        del train_loader\n        gc.collect()\n        \n    model.eval()\n    \n    val_loss = 0\n    val_acc = 0\n    data = np.load(data_path1[4])\n    data_= data['arr_0']\n    label = data['arr_1']\n    \n    del data\n    gc.collect()\n    \n    val_data = trainData(torch.FloatTensor(data_), torch.FloatTensor(label))\n    \n    del data_, label\n    gc.collect()\n    \n    val_loader = DataLoader(dataset=val_data, batch_size=32)\n    \n    del val_data\n    gc.collect()\n\n    with torch.no_grad():\n        for x, y in val_loader:\n            \n            x, y = x.to(device), y.to(device)\n            y_pred = model(x)\n                    \n            loss = criterion(y_pred, torch.reshape(y, (-1,1)))\n            acc = binary_acc(y_pred, torch.reshape(y, (-1,1)))\n            \n            val_loss += loss.item()\n            val_acc +=acc.item()\n            \n            nji+=1\n            del x, y\n            gc.collect()\n            torch.cuda.empty_cache()\n        del val_loader\n        gc.collect()\n    \n    if t%5==0:\n        torch.save(model.state_dict(), \"flair_ef-b3\"+str(t)+\".pth\")\n        \n    print(f'Epoch:{t} | Loss: {epoch_loss/mnb:.5f} | Acc: {epoch_acc/mnb:.3f} | Val_Loss: {val_loss/nji:.5f} | Val_Acc: {val_acc/nji:.3f}')","metadata":{"execution":{"iopub.status.busy":"2021-09-14T19:03:40.108293Z","iopub.execute_input":"2021-09-14T19:03:40.109078Z"},"trusted":true},"execution_count":null,"outputs":[]}]}