Repository navigation
Expand file tree
/
Copy pathtrain.py
More file actions
186 lines (138 loc) · 6.41 KB
/
Copy pathtrain.py
File metadata and controls
186 lines (138 loc) · 6.41 KB
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import tifffile
import torch
from data import create_dataset
from torch.utils.data import DataLoader
from argparse import Namespace
from util.visualizer import Visualizer
from torch.optim.lr_scheduler import LambdaLR
from torch.nn import BCEWithLogitsLoss, MSELoss
from funlib.learn.torch.models import UNet, ConvPass
import os
import matplotlib.pyplot as plt
from copy import deepcopy
from config_utils import load_config
def train(opt):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
dataset = create_dataset(opt)
dataset_size = len(dataset)
print('The number of training images = %d' % dataset_size)
print(len(dataset))
if len(dataset) == 0:
raise ValueError("Dataset is empty. Please check your data loading implementation.")
out_channel = 4 # fg + 3 dim direction vector
downsample_factors = [[2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]
ks = [3] * 3
ks = [ks] * 2
ks = [ks] * (len(downsample_factors) + 1)
print(ks)
unet = UNet(
in_channels=1,
num_fmaps=16,
fmap_inc_factor=2,
# fmap_dec_factor=[2, 2, 2, 2],
downsample_factors=downsample_factors,
kernel_size_down=ks,
kernel_size_up=ks,
# upsampling="transposed_conv",
padding="same",
num_heads=1
).to(device)
print("unet created")
if opt.last_conv_batchnorm:
last_conv = torch.nn.Sequential(
ConvPass(unet.out_channels, out_channel, [[1, 1, 1]], activation=None, padding='same'),
torch.nn.BatchNorm3d(out_channel)
)
else:
last_conv = ConvPass(
unet.out_channels, out_channel, [[1, 1, 1]], activation=None, padding='same'
)
print("last conv created")
model = torch.nn.Sequential(
unet,
last_conv
)
print("model created")
model = model.to(device)
print("model to device")
optimizer = torch.optim.Adam(model.parameters(), lr=opt.learning_rate)
max_epochs = opt.max_epochs
print(f"number of epochs:{max_epochs}")
def linear_decay(epoch):
return 1 - epoch / max_epochs
visualizer = Visualizer(opt)
scheduler = LambdaLR(optimizer, lr_lambda=linear_decay)
train_loader = dataset
val_loader = deepcopy(dataset)
train_losses = []
val_losses = []
for epoch in range(max_epochs):
print(f"Starting epoch {epoch + 1}")
model.train()
epoch_loss = 0.0
visualizer.reset()
print("Visualizer is reset")
if hasattr(train_loader.dataset, 'shuffle_patch_pairs'):
train_loader.dataset.shuffle_patch_pairs()
for batch_idx, (images, labels) in enumerate(train_loader):
images, labels = images.to(device), labels.to(device)
seg_mask = labels[:, 0:1, ...]
ground_truth_foreground_mask = (seg_mask > 0.5).float()
weight_map = ground_truth_foreground_mask * 1.0 + (1 - ground_truth_foreground_mask) * 0.1
direction_vectors = labels[:, 1:, ...]
optimizer.zero_grad()
outputs = model(images)
predicted_seg_mask = outputs[:, 0:1, ...]
predicted_direction_vectors = outputs[:, 1:, ...]
if opt.bce_pos_weight is not None:
pos_weight = torch.tensor([opt.bce_pos_weight]).to(device)
BCEloss = BCEWithLogitsLoss(pos_weight=pos_weight)(predicted_seg_mask, seg_mask)
else:
BCEloss = BCEWithLogitsLoss()(predicted_seg_mask, seg_mask)
mseloss_vectors = (weight_map * (predicted_direction_vectors - direction_vectors) ** 2).mean()
lambda_seg = 1
lambda_vec = 1
loss = lambda_seg * BCEloss + lambda_vec * mseloss_vectors
loss.backward()
optimizer.step()
epoch_loss += loss.item()
predictions = (torch.sigmoid(predicted_seg_mask) > 0.5).float()
predictions_vector = predicted_direction_vectors > 0
visuals = {'output image': predictions, 'flow_vectors': predictions_vector, 'input image': images,
'Labels': seg_mask}
losses = {'Segmentation(BCE)': BCEloss, 'vectors(MSE)': mseloss_vectors}
visualizer.display_current_results(visuals, epoch)
visualizer.plot_current_losses(losses)
avg_train_loss = epoch_loss / len(train_loader)
train_losses.append(avg_train_loss)
scheduler.step()
model.eval()
with torch.no_grad():
val_loss = 0.0
for val_images, val_labels in val_loader:
val_images, val_labels = val_images.to(device), val_labels.to(device)
seg_mask = val_labels[:, 0:1, ...]
ground_truth_foreground_mask = (seg_mask > 0.5).float()
weight_map = ground_truth_foreground_mask * 1.0 + (1 - ground_truth_foreground_mask) * 0.1
direction_vectors = val_labels[:, 1:, ...]
val_outputs = model(val_images)
val_predicted_seg_mask = val_outputs[:, 0:1, ...]
val_predicted_direction_vectors = val_outputs[:, 1:, ...]
val_BCEloss = BCEWithLogitsLoss()(val_predicted_seg_mask, seg_mask)
val_mseloss_vectors = (
weight_map * (val_predicted_direction_vectors - direction_vectors) ** 2).mean()
lambda_seg = 1
lambda_vec = 1
val_loss = lambda_seg * val_BCEloss + lambda_vec * val_mseloss_vectors
avg_val_loss = val_loss / len(val_loader)
val_losses.append(avg_val_loss.item())
print(f"Validation Loss: {avg_val_loss.item()}")
print(f"Epoch {epoch + 1}/{max_epochs}, Training Loss: {avg_train_loss}, Validation Loss: {avg_val_loss.item()}")
checkpoints_dir = opt.checkpoints_dir
if (epoch + 1) % 5 == 0:
checkpoint_path = os.path.join(checkpoints_dir, f'unet_epoch_{epoch + 1}.pth')
torch.save(model.state_dict(), checkpoint_path)
print(f"Checkpoint saved at {checkpoint_path}")
torch.save(model.state_dict(), "unet_segmentation_model.pth")
opt = load_config("config.yaml")
train(opt)