{"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%pip install --quiet torchio","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-22T13:05:15.866244Z","iopub.execute_input":"2022-08-22T13:05:15.866644Z","iopub.status.idle":"2022-08-22T13:05:28.588428Z","shell.execute_reply.started":"2022-08-22T13:05:15.866613Z","shell.execute_reply":"2022-08-22T13:05:28.587516Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import copy\n\nimport torchio as tio\nimport torch\nfrom tqdm import tqdm\nimport matplotlib as mpl\nimport matplotlib.pyplot as plt\n\ntorch.manual_seed(20220802)\nmpl.rcParams['figure.figsize'] = 12, 8","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:28.59014Z","iopub.execute_input":"2022-08-22T13:05:28.590464Z","iopub.status.idle":"2022-08-22T13:05:30.031384Z","shell.execute_reply.started":"2022-08-22T13:05:28.590433Z","shell.execute_reply":"2022-08-22T13:05:30.030069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"root_dir = '/kaggle/input/rsna-2022-cervical-spine-fracture-detection'\ndataset = tio.datasets.RSNACervicalSpineFracture(root_dir, add_segmentations=True, add_bounding_boxes=True)\nprint('Number of subjects:', len(dataset))","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:30.03674Z","iopub.execute_input":"2022-08-22T13:05:30.040365Z","iopub.status.idle":"2022-08-22T13:05:32.405946Z","shell.execute_reply.started":"2022-08-22T13:05:30.040313Z","shell.execute_reply":"2022-08-22T13:05:32.40474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0]","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:32.409111Z","iopub.execute_input":"2022-08-22T13:05:32.409569Z","iopub.status.idle":"2022-08-22T13:05:37.364192Z","shell.execute_reply.started":"2022-08-22T13:05:32.409525Z","shell.execute_reply":"2022-08-22T13:05:37.362927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for k, v in dataset[0].items():\n    print(f'k: {k}, v: {v}')","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:37.365629Z","iopub.execute_input":"2022-08-22T13:05:37.367441Z","iopub.status.idle":"2022-08-22T13:05:39.978836Z","shell.execute_reply.started":"2022-08-22T13:05:37.367396Z","shell.execute_reply":"2022-08-22T13:05:39.977672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].StudyInstanceUID","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:39.980622Z","iopub.execute_input":"2022-08-22T13:05:39.981024Z","iopub.status.idle":"2022-08-22T13:05:42.533456Z","shell.execute_reply.started":"2022-08-22T13:05:39.980984Z","shell.execute_reply":"2022-08-22T13:05:42.532231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:42.536164Z","iopub.execute_input":"2022-08-22T13:05:42.537195Z","iopub.status.idle":"2022-08-22T13:05:45.08081Z","shell.execute_reply.started":"2022-08-22T13:05:42.537128Z","shell.execute_reply":"2022-08-22T13:05:45.079697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[1].plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:45.082195Z","iopub.execute_input":"2022-08-22T13:05:45.082857Z","iopub.status.idle":"2022-08-22T13:05:53.357399Z","shell.execute_reply.started":"2022-08-22T13:05:45.082813Z","shell.execute_reply":"2022-08-22T13:05:53.356208Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].ct.hist()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:05:53.358765Z","iopub.execute_input":"2022-08-22T13:05:53.359111Z","iopub.status.idle":"2022-08-22T13:06:03.062628Z","shell.execute_reply.started":"2022-08-22T13:05:53.359053Z","shell.execute_reply":"2022-08-22T13:06:03.061385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"clamp = tio.Clamp(out_min=-1000, out_max=1900)\nfirst_subject_clamped=clamp(dataset[0])\nfirst_subject_clamped.ct.hist()\nfirst_subject_clamped.plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:03.067693Z","iopub.execute_input":"2022-08-22T13:06:03.068332Z","iopub.status.idle":"2022-08-22T13:06:10.982659Z","shell.execute_reply.started":"2022-08-22T13:06:03.068288Z","shell.execute_reply":"2022-08-22T13:06:10.981246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"rescale = tio.RescaleIntensity(percentiles=(0.5, 99.5))\npreprocess_intensity = tio.Compose([\n    clamp,\n    rescale,\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:10.984623Z","iopub.execute_input":"2022-08-22T13:06:10.985092Z","iopub.status.idle":"2022-08-22T13:06:10.993046Z","shell.execute_reply.started":"2022-08-22T13:06:10.985042Z","shell.execute_reply":"2022-08-22T13:06:10.991596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_subject_preprocessed = preprocess_intensity(dataset[0])\nfirst_subject_preprocessed.ct.hist(show=False), plt.ylim(0, 1e6)\nfirst_subject_preprocessed.plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:10.995194Z","iopub.execute_input":"2022-08-22T13:06:10.99574Z","iopub.status.idle":"2022-08-22T13:06:19.30122Z","shell.execute_reply.started":"2022-08-22T13:06:10.995699Z","shell.execute_reply":"2022-08-22T13:06:19.300107Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for idx, subject in enumerate(dataset.dry_iter()):\n    if 'seg' in subject:\n        subject_with_seg = subject\n        print(f'idx: {idx}')\n        break\nsubject_with_seg.plot(reorient=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:19.302768Z","iopub.execute_input":"2022-08-22T13:06:19.304684Z","iopub.status.idle":"2022-08-22T13:06:25.864472Z","shell.execute_reply.started":"2022-08-22T13:06:19.304637Z","shell.execute_reply":"2022-08-22T13:06:25.863347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('CT orientation:', subject_with_seg.ct.orientation)\nprint('Segmentation orientation', subject_with_seg.seg.orientation)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:25.865821Z","iopub.execute_input":"2022-08-22T13:06:25.866195Z","iopub.status.idle":"2022-08-22T13:06:25.873414Z","shell.execute_reply.started":"2022-08-22T13:06:25.866139Z","shell.execute_reply":"2022-08-22T13:06:25.872055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].keys()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:25.874456Z","iopub.execute_input":"2022-08-22T13:06:25.87512Z","iopub.status.idle":"2022-08-22T13:06:28.564529Z","shell.execute_reply.started":"2022-08-22T13:06:25.875089Z","shell.execute_reply":"2022-08-22T13:06:28.56321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].ct.keys()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:28.566041Z","iopub.execute_input":"2022-08-22T13:06:28.566946Z","iopub.status.idle":"2022-08-22T13:06:31.220733Z","shell.execute_reply.started":"2022-08-22T13:06:28.566903Z","shell.execute_reply":"2022-08-22T13:06:31.219664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].ct.data.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:31.22201Z","iopub.execute_input":"2022-08-22T13:06:31.222538Z","iopub.status.idle":"2022-08-22T13:06:33.732026Z","shell.execute_reply.started":"2022-08-22T13:06:31.222505Z","shell.execute_reply":"2022-08-22T13:06:33.730981Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[0].ct.plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:33.733307Z","iopub.execute_input":"2022-08-22T13:06:33.733632Z","iopub.status.idle":"2022-08-22T13:06:37.483172Z","shell.execute_reply.started":"2022-08-22T13:06:33.733602Z","shell.execute_reply":"2022-08-22T13:06:37.482037Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[4].keys()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:37.484805Z","iopub.execute_input":"2022-08-22T13:06:37.485215Z","iopub.status.idle":"2022-08-22T13:06:37.632877Z","shell.execute_reply.started":"2022-08-22T13:06:37.485178Z","shell.execute_reply":"2022-08-22T13:06:37.631673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset[4].seg.plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:37.634851Z","iopub.execute_input":"2022-08-22T13:06:37.635323Z","iopub.status.idle":"2022-08-22T13:06:38.351667Z","shell.execute_reply.started":"2022-08-22T13:06:37.635277Z","shell.execute_reply":"2022-08-22T13:06:38.350549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"normalize_orientation = tio.ToCanonical()\ndownsample = tio.Resample(1)\npreprocess_spatial = tio.Compose([\n    normalize_orientation,\n    downsample,\n])\npreprocess = tio.Compose([\n    preprocess_intensity,\n    preprocess_spatial,\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:38.354893Z","iopub.execute_input":"2022-08-22T13:06:38.355508Z","iopub.status.idle":"2022-08-22T13:06:38.360783Z","shell.execute_reply.started":"2022-08-22T13:06:38.355476Z","shell.execute_reply":"2022-08-22T13:06:38.359633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"subject_with_seg_preprocessed = preprocess(subject_with_seg)\nsubject_with_seg_preprocessed.plot(reorient=False)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:38.362286Z","iopub.execute_input":"2022-08-22T13:06:38.362586Z","iopub.status.idle":"2022-08-22T13:06:44.027789Z","shell.execute_reply.started":"2022-08-22T13:06:38.362559Z","shell.execute_reply":"2022-08-22T13:06:44.026706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"augment = tio.Compose([\n    tio.RandomAnisotropy(p=0.25),\n    tio.RandomAffine(),\n    tio.RandomFlip(),\n    tio.RandomNoise(p=0.25),\n    tio.RandomGamma(p=0.5),\n])","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:44.02903Z","iopub.execute_input":"2022-08-22T13:06:44.029377Z","iopub.status.idle":"2022-08-22T13:06:44.035013Z","shell.execute_reply.started":"2022-08-22T13:06:44.029345Z","shell.execute_reply":"2022-08-22T13:06:44.034202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"transform = tio.CropOrPad((130, 130, 190))\ntrain_transform = tio.Compose([\n    preprocess,\n    augment,\n    transform,\n])\nval_transform = tio.Compose([preprocess, transform])","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:44.036102Z","iopub.execute_input":"2022-08-22T13:06:44.037251Z","iopub.status.idle":"2022-08-22T13:06:44.044841Z","shell.execute_reply.started":"2022-08-22T13:06:44.0372Z","shell.execute_reply":"2022-08-22T13:06:44.044115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for _ in range(5):\n    subject_with_seg_augmented = train_transform(subject_with_seg)\n    subject_with_seg_augmented.plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:06:44.04575Z","iopub.execute_input":"2022-08-22T13:06:44.047351Z","iopub.status.idle":"2022-08-22T13:07:15.155199Z","shell.execute_reply.started":"2022-08-22T13:06:44.047318Z","shell.execute_reply":"2022-08-22T13:07:15.154056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_to_val_ratio = 0.8\nnum_subjects = len(dataset)\nnum_train_subjects = int(train_to_val_ratio * num_subjects)\nnum_val_subjects = num_subjects - num_train_subjects\nprint('Number of subjects for training:  ', num_train_subjects)\nprint('Number of subjects for validation: ', num_val_subjects)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:07:15.156569Z","iopub.execute_input":"2022-08-22T13:07:15.156897Z","iopub.status.idle":"2022-08-22T13:07:15.163961Z","shell.execute_reply.started":"2022-08-22T13:07:15.156859Z","shell.execute_reply":"2022-08-22T13:07:15.162651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"no_segs_dataset = tio.datasets.RSNACervicalSpineFracture(root_dir)\ntrain_set, val_set = torch.utils.data.random_split(no_segs_dataset, [num_train_subjects, num_val_subjects])","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:07:15.164902Z","iopub.execute_input":"2022-08-22T13:07:15.165223Z","iopub.status.idle":"2022-08-22T13:07:16.446417Z","shell.execute_reply.started":"2022-08-22T13:07:15.165194Z","shell.execute_reply":"2022-08-22T13:07:16.445182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Both subsets share the \"dataset\" attribute, so the last statement would\n# overwrite the previous one unless we deep-copy one of the subsets\nval_set = copy.deepcopy(val_set)\ntrain_set.dataset.set_transform(train_transform)\nval_set.dataset.set_transform(val_transform)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:07:16.451719Z","iopub.execute_input":"2022-08-22T13:07:16.452062Z","iopub.status.idle":"2022-08-22T13:07:16.639599Z","shell.execute_reply.started":"2022-08-22T13:07:16.45203Z","shell.execute_reply":"2022-08-22T13:07:16.638536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:07:16.64101Z","iopub.execute_input":"2022-08-22T13:07:16.641365Z","iopub.status.idle":"2022-08-22T13:07:24.978676Z","shell.execute_reply.started":"2022-08-22T13:07:16.641334Z","shell.execute_reply":"2022-08-22T13:07:24.977478Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[1].shape","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:07:24.979882Z","iopub.execute_input":"2022-08-22T13:07:24.980215Z","iopub.status.idle":"2022-08-22T13:07:41.89803Z","shell.execute_reply.started":"2022-08-22T13:07:24.980181Z","shell.execute_reply":"2022-08-22T13:07:41.897185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    train_set[i].plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:07:41.899416Z","iopub.execute_input":"2022-08-22T13:07:41.900082Z","iopub.status.idle":"2022-08-22T13:08:44.2943Z","shell.execute_reply.started":"2022-08-22T13:07:41.90004Z","shell.execute_reply":"2022-08-22T13:08:44.293192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):\n    val_set[i].plot()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:08:44.295651Z","iopub.execute_input":"2022-08-22T13:08:44.296009Z","iopub.status.idle":"2022-08-22T13:09:40.022939Z","shell.execute_reply.started":"2022-08-22T13:08:44.295979Z","shell.execute_reply":"2022-08-22T13:09:40.021807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set[0].C5","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:09:40.024483Z","iopub.execute_input":"2022-08-22T13:09:40.02554Z","iopub.status.idle":"2022-08-22T13:09:45.157838Z","shell.execute_reply.started":"2022-08-22T13:09:40.025503Z","shell.execute_reply":"2022-08-22T13:09:45.156596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = torch.utils.data.DataLoader(train_set, batch_size = 16, shuffle = True, num_workers = 2)\nval_loader = torch.utils.data.DataLoader(val_set, batch_size=16, shuffle=False, num_workers=2)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:09:45.159642Z","iopub.execute_input":"2022-08-22T13:09:45.160346Z","iopub.status.idle":"2022-08-22T13:09:45.166333Z","shell.execute_reply.started":"2022-08-22T13:09:45.160295Z","shell.execute_reply":"2022-08-22T13:09:45.165224Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# for data in train_loader:\n#     print(data)\n#     break","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:09:45.167695Z","iopub.execute_input":"2022-08-22T13:09:45.168455Z","iopub.status.idle":"2022-08-22T13:09:45.17865Z","shell.execute_reply.started":"2022-08-22T13:09:45.168413Z","shell.execute_reply":"2022-08-22T13:09:45.177867Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:09:45.181422Z","iopub.execute_input":"2022-08-22T13:09:45.181786Z","iopub.status.idle":"2022-08-22T13:09:45.188379Z","shell.execute_reply.started":"2022-08-22T13:09:45.181755Z","shell.execute_reply":"2022-08-22T13:09:45.187096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x=(N, C=1, 130, 130, 190), y = (N, C=8, 1)\ndef f(D, kernel, stride=1, padding=0):\n    return (D + 2*padding - kernel) / stride + 1\n\nf(D=130, kernel=6, stride=2), f(D=63, kernel=3), f(D=61, kernel=5, stride=2), f(D=29, kernel=3), f(D=27, kernel=5, stride=2), f(D=12, kernel=4, stride=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:09:45.190166Z","iopub.execute_input":"2022-08-22T13:09:45.190846Z","iopub.status.idle":"2022-08-22T13:09:45.20204Z","shell.execute_reply.started":"2022-08-22T13:09:45.190807Z","shell.execute_reply":"2022-08-22T13:09:45.201052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"f(D=190, kernel=6, stride=2), f(D=93, kernel=3), f(D=91, kernel=5, stride=2), f(D=44, kernel=4), f(D=41, kernel=5, stride=2), f(D=19, kernel=4, stride=1)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:09:45.203339Z","iopub.execute_input":"2022-08-22T13:09:45.204269Z","iopub.status.idle":"2022-08-22T13:09:45.213229Z","shell.execute_reply.started":"2022-08-22T13:09:45.204217Z","shell.execute_reply":"2022-08-22T13:09:45.21242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"class Net(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.conv1 = nn.Conv3d(1, 8, (5, 5, 5), stride=(1, 1, 1)) # 61, 61, ９１\n        self.pool1 = nn.MaxPool3d(3) # 58, 58, 88\n        self.conv2 = nn.Conv3d(8, 8, (5, 5, 5), stride=(1, 1, 1)) # 17, 17, 27\n        self.pool2 = nn.MaxPool3d(4) # \n        self.conv3 = nn.Conv3d(8, 8, (5, 5, 5), stride=(2, 2, 2))\n        self.pool3 = nn.MaxPool3d(4)\n        self.fc1 = nn.Linear(1134, 512)\n        self.fc2 = nn.Linear(512, 1)\n        self.relu = nn.ReLU()\n        self.sigmoid = nn.Sigmoid()\n    def forward(self, x):\n        x = self.pool1(self.conv1(x))\n        x = self.pool2(self.conv2(x))\n#         x = self.pool3(self.conv3(x))\n        x = x.view(x.size(0), x.size(1), -1)\n        x = self.relu(self.fc1(x))\n        x = self.sigmoid(self.fc2(x))\n        return x\nnet = Net()","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:16:51.138009Z","iopub.execute_input":"2022-08-22T13:16:51.13842Z","iopub.status.idle":"2022-08-22T13:16:51.155377Z","shell.execute_reply.started":"2022-08-22T13:16:51.138387Z","shell.execute_reply":"2022-08-22T13:16:51.154284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"input = torch.randn(16, 1, 130, 130, 190)\noutput = net(input)\noutput","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:16:51.38151Z","iopub.execute_input":"2022-08-22T13:16:51.382516Z","iopub.status.idle":"2022-08-22T13:16:56.006384Z","shell.execute_reply.started":"2022-08-22T13:16:51.382474Z","shell.execute_reply":"2022-08-22T13:16:56.005397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:17:08.284716Z","iopub.execute_input":"2022-08-22T13:17:08.285134Z","iopub.status.idle":"2022-08-22T13:17:08.293675Z","shell.execute_reply.started":"2022-08-22T13:17:08.285102Z","shell.execute_reply":"2022-08-22T13:17:08.292373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for data in train_loader: \n    print(data.cdt.data)","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:17:44.401601Z","iopub.execute_input":"2022-08-22T13:17:44.402582Z","iopub.status.idle":"2022-08-22T13:20:36.978323Z","shell.execute_reply.started":"2022-08-22T13:17:44.402541Z","shell.execute_reply":"2022-08-22T13:20:36.975272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = torch.optim.SGD(net.parameters(), lr=1e-3)\ncriterion = nn.BCELoss()\ndevice = \"cuda\"if torch.cuda.is_available() else \"cpu\"\nnet.train()\nfor data in train_loader:\n    x = data.ct.data.to(device)\n    \n    y = net(x)\n    t = data.C1.to(device)\n    loss = criterion(y, t)\n    loss.backward()\n    optimizer.step()\n    print(loss.detach())\n    break\n\nnet.eval()\nwith torch.no_grad():\n    for data in val_loader:\n        x = data.ct.data.to(device)\n        y = net(x)\n        t = data.C1.to(device)\n        loss = criterion(y, t)\n        print(loss.detach())","metadata":{"execution":{"iopub.status.busy":"2022-08-22T13:25:39.66713Z","iopub.execute_input":"2022-08-22T13:25:39.667541Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}