-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_lin.py
131 lines (100 loc) · 3.74 KB
/
train_lin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import torch
import torch.nn as nn
import torch.nn.functional as F
from model.trans_3DUnet import get_model_dict
import monai
from torch.utils.data import DataLoader, Dataset
import glob
import nibabel as nib
class pancreas(Dataset):
def __init__(self, path):
self.path = path
self.x_paths = glob.glob(path+'/images/*.nii.gz')
def __len__(self):
return len(self.x_paths)
def __getitem__(self, idx):
image = nib.load(self.x_paths[idx]).get_fdata()
image = torch.tensor(image).unsqueeze(0)
image = monai.transforms.spatial.functional.resize(
image,
out_size=(256, 256, 80),
mode="nearest",
align_corners=None,
dtype=None,
input_ndim=3,
anti_aliasing=False,
anti_aliasing_sigma=None,
lazy=False,
transform_info=None
)
image = (image - torch.mean(image)) / torch.std(image)
img_name = self.x_paths[idx].split('/')[-1]
label = nib.load(self.path+'/labels/'+img_name).get_fdata()
label = torch.tensor(label).unsqueeze(0)
label = monai.transforms.spatial.functional.resize(
label,
out_size=(256, 256, 80),
mode="nearest",
align_corners=None,
dtype=None,
input_ndim=3,
anti_aliasing=False,
anti_aliasing_sigma=None,
lazy=False,
transform_info=None
)
return image, label
#PyTorch
class DiceBCELoss(nn.Module):
def __init__(self, weight=None, size_average=True):
super(DiceBCELoss, self).__init__()
def forward(self, inputs, targets, smooth=1e-3):
#comment out if your model contains a sigmoid or equivalent activation layer
inputs = F.sigmoid(inputs)
#flatten label and prediction tensors
inputs = inputs.view(-1)
targets = targets.view(-1)
intersection = (inputs * targets).sum()
dice_loss = 1 - (2.*intersection + smooth)/(inputs.sum() + targets.sum() + smooth)
BCE = F.binary_cross_entropy(inputs, targets, reduction='mean')
Dice_BCE = BCE + dice_loss
return Dice_BCE
def train_fn(model, optimizer, criterion, loader, device):
model.train()
total_loss = 0.
score = 0.
for i, data in enumerate(loader):
x = data[0].to(device)
y = data[1].to(device)
optimizer.zero_grad()
y_hat,_ = model(x)
loss = criterion(y_hat, y)
loss.backward()
optimizer.step()
total_loss += loss.item()
return total_loss/len(loader)
if __name__ == "__main__":
device = torch.device('cuda')
epochs = 1000
model_fn = get_model_dict("MaskTransUnet")
model = model_fn(
num_layers=[32, 64, 128, 128],
roi_size_list=[40, 30, 20, 20],
is_roi_list=[False, True, False, True],
dim_input=1,
dim_output=1,
kernel_size=3
)
model.to(device)
criterion = DiceBCELoss().to(device)
train_ds = pancreas("/data/onkar/NeurIPS_Liver_unlabelled/training_t1")
train_dataloader = DataLoader(train_ds, batch_size=4, shuffle=True, num_workers=2)
optimizer = torch.optim.AdamW(model.parameters(), lr=1e-4)
for epoch in range(epochs):
loss = train_fn(model, optimizer, criterion, train_dataloader, device)
print(f"Epoch {epoch + 1} loss: ", loss)
if epoch+1 % 5 == 0:
checkpoint = {
"model":model.state_dict()
}
torch.save(checkpoint, f"/datadrive/pan_dataset/zheyuan_model/{epoch+1}.pth")