-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nested_Checkpoint.py
327 lines (192 loc) · 5.9 KB
/
Nested_Checkpoint.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#!/usr/bin/env python
# coding: utf-8
# In[5]:
from data import RoadSatelliteModule
from system import SemanticSegmentationSystem
from models import *
import numpy as np
import matplotlib.pyplot as plt
import random
import torchvision
import torchvision.transforms.functional as F
import pytorch_lightning as pl
from pytorch_lightning.callbacks import EarlyStopping
from pytorch_lightning.callbacks import ModelCheckpoint
import torch
from torchinfo import summary
import regex as re
# In[6]:
import sys
import warnings
if not sys.warnoptions:
warnings.simplefilter("ignore")
# # 1. Preparation
# In[7]:
batch_size = 8
num_workers = 8
# In[8]:
pl.seed_everything(7, workers=True)
# ## 1.1 DataModule
# In[9]:
road_data = RoadSatelliteModule(num_workers=num_workers, batch_size=batch_size)
# In[10]:
# ## 1.2 Inspect Data
# In[12]:
def show_image(imgs):
if not isinstance(imgs, list):
imgs = [imgs]
fix, axs = plt.subplots(ncols=len(imgs), squeeze=False)
for i, img in enumerate(imgs):
img = img.detach()
img = F.to_pil_image(img)
axs[0, i].imshow(np.asarray(img))
axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])
# In[13]:
# In[14]:
# # 2. Define Model / System
# In[15]:
load_from_checkpoint = True
# In[16]:
model = NestedUNet(1, 3)
model_name = str(model).partition('(')[0]
# In[17]:
system = SemanticSegmentationSystem(model, road_data, n_closing=0)
# In[18]:
if load_from_checkpoint:
system = SemanticSegmentationSystem.load_from_checkpoint(f'{"./lightning_logs/" + model_name + "_data"}/{model_name + "_data"}.ckpt', model=model, datamodule=road_data)
# # 4. Training
# In[19]:
if torch.cuda.is_available():
gpu_count = -1
gpu_auto_select = True
else:
gpu_count = 0
gpu_auto_select = False
# In[20]:
checkpoint_callback = ModelCheckpoint(
monitor='validation_f1',
dirpath='./lightning_logs',
filename=model_name + '_fix',
save_top_k=1,
verbose=2,
mode='max'
)
# In[21]:
early_stop_callback = EarlyStopping(
monitor='validation_f1',
patience=50,
verbose=1,
mode='max'
)
# In[22]:
if load_from_checkpoint:
trainer = pl.Trainer(
#fast_dev_run=True,
gpus=gpu_count,
auto_select_gpus=gpu_auto_select,
stochastic_weight_avg=True,
benchmark=True,
callbacks=[early_stop_callback, checkpoint_callback],
resume_from_checkpoint=f'{"./lightning_logs/" + model_name + "_data"}/{model_name + "_data"}.ckpt'
)
else:
trainer = pl.Trainer(
#fast_dev_run=True,
gpus=gpu_count,
auto_select_gpus=gpu_auto_select,
stochastic_weight_avg=True,
benchmark=True,
callbacks=[early_stop_callback, checkpoint_callback]
)
# In[23]:
if not load_from_checkpoint:
trainer.fit(system)
# In[27]:
if gpu_count != 0:
try:
model.cuda()
except:
print("model not defined")
try:
model_fix.cuda()
except:
print("model fix not defined")
try:
model_fix_mask.cuda()
except:
print("model fix mask not defined")
else:
try:
model.cpu()
except:
print("model not defined")
try:
model_fix.cpu()
except:
print("model fix not defined")
try:
model_fix_mask.cpu()
except:
print("model fix mask not defined")
# # 5. Predict
# In[30]:
if load_from_checkpoint:
system = SemanticSegmentationSystem.load_from_checkpoint(f'{"./lightning_logs/" + model_name + "_data"}/{model_name + "_data"}.ckpt', model=model, datamodule=road_data)
# In[31]:
if load_from_checkpoint:
trainer.test(system, ckpt_path=f'{"./lightning_logs/" + model_name + "_data"}/{model_name + "_data"}.ckpt')
else:
trainer.test(system)
# In[33]:
foreground_threshold = 0.25 # percentage of pixels > 1 required to assign a foreground label to a patch
# assign a label to a patch
def patch_to_label(patch):
df = np.mean(patch)
if df > foreground_threshold:
return 1
else:
return 0
def mask_to_patched_mask(image):
patched_image = image.squeeze().detach().clone()
image = np.asarray(image.squeeze())
patch_size = 16
for j in range(0, image.shape[1], patch_size):
for i in range(0, image.shape[0], patch_size):
patch = image[i:i + patch_size, j:j + patch_size]
label = patch_to_label(patch)
patched_image[i:i + patch_size, j:j + patch_size] = label
return patched_image
def mask_to_submission_strings(im, name):
"""Reads a single image and outputs the strings that should go into the submission file"""
img_number = int(re.search(r"\d+", name).group(0))
#im = mpimg.imread(image_filename)
# image is gray scale therefore size MxN with imread
patch_size = 16
for j in range(0, im.shape[1], patch_size):
for i in range(0, im.shape[0], patch_size):
patch = im[i:i + patch_size, j:j + patch_size]
label = patch_to_label(patch)
yield("{:03d}_{}_{},{}".format(img_number, j, i, label))
def masks_to_submission(submission_filename, *images):
"""Converts images into a submission file"""
with open(submission_filename, 'w') as f:
f.write('id,prediction\n')
for imgs, fn in images[0:]:
f.writelines('{}\n'.format(s) for s in mask_to_submission_strings(imgs, fn))
# In[34]:
batches = system.test_results
# In[38]:
model_name = 'Cillers'
# In[39]:
submission_filename = model_name + '_predictions.csv'
pred_counter = 0
# In[40]:
with open(submission_filename, 'w') as f:
f.write('id,prediction\n')
for mask, name in batches:
predicted_mask = np.asarray(mask.cpu().squeeze())
ids = mask_to_submission_strings(predicted_mask, name)
f.writelines('{}\n'.format('\n'.join(ids)))
pred_counter += 1
pred_counter
# In[ ]: