-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
263 lines (234 loc) · 9.24 KB
/
main.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import itertools
import logging
import pathlib
import sys
from typing import List, Tuple, Optional
import torch
import torch.nn.functional as F
import torchvision
from PIL import Image
from torch.utils.data import DataLoader, Dataset, random_split
from tqdm import tqdm
# from salt_dataset import SaltSeismologyDataset
from pet_dataset import PetDataset
logging.basicConfig(level=logging.INFO)
class UNetConvBlock(torch.nn.Module):
"""
Simple chain of Conv2d -> ReLU -> Conv
"""
def __init__(self, input_channels: int, output_channels: int) -> None:
super().__init__()
self.first_conv = torch.nn.Conv2d(
in_channels=input_channels,
out_channels=output_channels,
kernel_size=3,
padding="same",
)
self.second_conv = torch.nn.Conv2d(
in_channels=output_channels,
out_channels=output_channels,
kernel_size=3,
padding="same",
)
def forward(self, tensor: torch.Tensor) -> torch.Tensor:
tensor = F.relu(self.first_conv(tensor))
tensor = self.second_conv(tensor)
return F.relu(tensor)
class UNet(torch.nn.Module):
def __init__(
self,
encoder_channel_stages: List[int] = [3, 16, 32, 64],
num_classes: int = 1,
retain_dim: bool = True,
output_size: Tuple[int, int] = (128, 128),
) -> None:
super().__init__()
# Encoder is just a stack of these blocks
self.encoder_blocks_ = torch.nn.ModuleList(
[
UNetConvBlock(in_chan, out_chan)
for in_chan, out_chan in itertools.pairwise(encoder_channel_stages)
]
)
# The decoder has channels staged in a reverse pattern from the encoder, and has no input layer
decoder_channel_stages = encoder_channel_stages[1:][::-1]
self.transposed_convs_ = torch.nn.ModuleList(
[
torch.nn.ConvTranspose2d(
in_channels=in_chan, out_channels=out_chan, kernel_size=2, stride=2
)
for in_chan, out_chan in itertools.pairwise(decoder_channel_stages)
]
)
self.decoder_blocks_ = torch.nn.ModuleList(
[
UNetConvBlock(in_chan, out_chan)
for in_chan, out_chan in itertools.pairwise(decoder_channel_stages)
]
)
# The final conv that generates the mask takes the decoder output and generates logit scores for each class
self.head = torch.nn.Conv2d(
decoder_channel_stages[-1], num_classes, kernel_size=1
)
self.retain_dim = retain_dim
self.output_size = output_size
def forward(self, tensor: torch.Tensor) -> torch.Tensor:
encoder_activations = []
# Encoder pass, save layer activations which will need to be forwarded
# to corresponding decoder layer
for i, encoder_block in enumerate(self.encoder_blocks_):
tensor = encoder_block(tensor)
encoder_activations.append(tensor)
tensor = F.max_pool2d(tensor, kernel_size=2)
# Decoder pass
for decoder_block, transposed_conv, encoder_activation in zip(
self.decoder_blocks_,
self.transposed_convs_,
reversed(encoder_activations[:-1]),
):
tensor = transposed_conv(tensor)
# center crop + concat the encoder activation (tensor dim labels are BCHW)
encoder_activation = torchvision.transforms.CenterCrop(tensor.shape[-2:])(
encoder_activation
)
tensor = torch.cat([tensor, encoder_activation], dim=1)
# then run through regular conv block
tensor = decoder_block(tensor)
# convert decoder output to classification map
tensor = self.head(tensor)
if self.retain_dim:
tensor = F.interpolate(tensor, self.output_size)
return tensor
def create_train_test_segmentation_datasets(
images_folder: pathlib.Path, masks_folder: pathlib.Path, train_percent: float = 0.8
) -> Tuple[Dataset, Dataset]:
"""
Collect sample images and masks from their respective folders and create train/test datasets
"""
# validate path objects
if not images_folder.is_dir():
raise Exception(f"{images_folder} is not a folder")
if not masks_folder.is_dir():
raise Exception(f"{masks_folder} is not a folder")
image_paths = sorted(
[
filepath
for filepath in images_folder.iterdir()
if filepath.suffix == ".png" or filepath.suffix == ".jpg"
]
)
mask_paths = sorted(
[filepath for filepath in masks_folder.iterdir() if ".png" == filepath.suffix]
)
if len(image_paths) != len(mask_paths):
raise UserWarning(
f"number of images ({len(image_paths)}) and number of masks ({len(mask_paths)}) don't match"
)
train_pairs, test_pairs = random_split(
list(zip(image_paths, mask_paths)), # type:ignore
lengths=[train_percent, 1.0 - train_percent],
)
augmentations = [
torchvision.transforms.RandomHorizontalFlip(p=1.0),
torchvision.transforms.RandomVerticalFlip(p=1.0),
]
train_image_paths, train_mask_paths = zip(*train_pairs)
train_set = PetDataset(
train_image_paths, train_mask_paths, augmentations=augmentations
)
test_image_paths, test_mask_paths = zip(*test_pairs)
test_set = PetDataset(test_image_paths, test_mask_paths)
return train_set, test_set
def fit_unet(
model: UNet,
train_set: Dataset,
test_set: Dataset,
batch_size: int,
num_epochs: int,
learning_rate: float = 0.001,
):
# Pixel level binary cross-entropy, between ground-truth mask and generated mask
loss_fn = torch.nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(params=model.parameters(), lr=learning_rate)
train_loader = DataLoader(
dataset=train_set,
shuffle=True,
batch_size=batch_size,
pin_memory=True,
num_workers=2,
)
test_loader = DataLoader(
dataset=test_set, shuffle=False, batch_size=batch_size, pin_memory=True
)
# Pretty vanilla training loop
# NOTE: See https://pytorch.org/tutorials/beginner/basics/optimization_tutorial.html#optimization-loop
last_test_loss = sys.float_info.max
for epoch in range(num_epochs):
model.train()
train_loss = 0
for image_batch, ground_truth_mask_batch in tqdm(train_loader):
generated_mask_batch = model(image_batch.cuda())
loss = loss_fn(generated_mask_batch, ground_truth_mask_batch.cuda())
optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss += loss
# Following epoch of training, evaluate model
with torch.no_grad():
model = model.eval()
loss = 0.0
for test_image_batch, test_ground_truth_mask_batch in test_loader:
test_generated_mask_batch = model(test_image_batch.cuda())
loss += loss_fn(
test_generated_mask_batch, test_ground_truth_mask_batch.cuda()
).cpu()
logging.info(f"Epoch {epoch} Train Loss:{train_loss} Test Loss: {loss}")
if loss < last_test_loss:
last_test_loss = loss
visualize_sample(test_set, model, limit=10)
torch.save(model, "unet_pets.pkl")
visualize_sample(test_set, model, limit=64)
@torch.no_grad()
def visualize_sample(dataset: Dataset, model: UNet, limit: int = 64):
demo_loader = DataLoader(
dataset=dataset, shuffle=False, batch_size=1, pin_memory=True
)
for i, (demo_image, demo_ground_truth_mask) in enumerate(demo_loader):
demo_generated_mask = model(demo_image.cuda()).cpu().squeeze()
demo_generated_mask = torch.clamp(
(torch.sigmoid(demo_generated_mask) * 255), min=0, max=255
).to(torch.uint8)
demo_generated_mask_rgb = torch.stack([demo_generated_mask] * 3)
demo_ground_truth_mask_rgb = torch.stack([demo_ground_truth_mask.squeeze()] * 3)
side_by_side = torch.cat(
[demo_image.squeeze(), demo_generated_mask_rgb, demo_ground_truth_mask_rgb],
dim=1,
)
comparison_image: Image.Image = torchvision.transforms.ToPILImage()(
side_by_side
)
comparison_image.save(f"demo/{i}.png")
i += 1
if i >= limit:
break
if __name__ == "__main__":
# parse CLI input
if len(sys.argv) != 3 or sys.argv[1] in ["-h", "--help", "-help"]:
logging.critical(
"Usage: python3 main.py /path/to/training/images/ /path/to/training/masks"
)
sys.exit()
images_folder, masks_folder = map(pathlib.Path, sys.argv[1:3])
train_set, test_set = create_train_test_segmentation_datasets(
images_folder, masks_folder, 0.90
)
# Uncomment these lines to get samples from your pre-trained model
# model: UNet = torch.load("unet_pets.pkl")
# visualize_sample(test_set, model)
fit_unet(
model=UNet(encoder_channel_stages=[3, 64, 128, 256, 512, 1024]).cuda(),
train_set=train_set,
test_set=test_set,
batch_size=64,
num_epochs=50,
)