Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit 59efd5c

Browse files
author
yalaudah
authored
Fixing #324 and #325 (#338)
* update colormap to a non-discrete one -- fixes #324 * fix mask_to_disk to normalize by n_classes * changes to test.py * Updating data.py * bug fix * increased timeout time for main_build * retrigger build * retrigger the build * increase timeout
1 parent 1ec0279 commit 59efd5c

File tree

5 files changed

+57
-36
lines changed

5 files changed

+57
-36
lines changed

cv_lib/cv_lib/utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
# Copyright (c) Microsoft Corporation.
32
# Licensed under the MIT License.
43

@@ -8,6 +7,7 @@
87
import numpy as np
98
from matplotlib import pyplot as plt
109

10+
1111
def normalize(array):
1212
"""
1313
Normalizes a segmentation mask array to be in [0,1] range
@@ -16,12 +16,19 @@ def normalize(array):
1616
min = array.min()
1717
return (array - min) / (array.max() - min)
1818

19-
def mask_to_disk(mask, fname, cmap_name="Paired"):
19+
20+
def mask_to_disk(mask, fname, n_classes, cmap_name="rainbow"):
2021
"""
2122
write segmentation mask to disk using a particular colormap
23+
mask (float): this contains the predicted labels in the range [0, n_classes].
24+
fname (str): of the the image to be saved
25+
n_classes (int): total number of classes in the dataset
26+
cmap_name (str): name of the matplotlib colormap to be used. The default "rainbow"
27+
colormap works well for any number of classes.
2228
"""
2329
cmap = plt.get_cmap(cmap_name)
24-
Image.fromarray(cmap(normalize(mask), bytes=True)).save(fname)
30+
Image.fromarray(cmap(mask / n_classes, bytes=True)).save(fname)
31+
2532

2633
def image_to_disk(mask, fname, cmap_name="seismic"):
2734
"""
@@ -30,7 +37,8 @@ def image_to_disk(mask, fname, cmap_name="seismic"):
3037
cmap = plt.get_cmap(cmap_name)
3138
Image.fromarray(cmap(normalize(mask), bytes=True)).save(fname)
3239

33-
def decode_segmap(label_mask, colormap_name="Paired"):
40+
41+
def decode_segmap(label_mask, colormap_name="rainbow"):
3442
"""
3543
Decode segmentation class labels into a colour image
3644
Args:
@@ -48,6 +56,7 @@ def decode_segmap(label_mask, colormap_name="Paired"):
4856

4957
return out
5058

59+
5160
def load_log_configuration(log_config_file):
5261
"""
5362
Loads logging configuration from the given configuration file.

experiments/interpretation/dutchf3_patch/local/test.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,16 @@ def _patch_label_2d(
231231
outdir = f"debug/batch_{split}"
232232
generate_path(outdir)
233233
for i in range(batch.shape[0]):
234-
image_to_disk(
235-
np.array(batch[i, 0, :, :]), f"{outdir}/{batch_indexes[i][0]}_{batch_indexes[i][1]}_img.png"
236-
)
237-
# now dump model predictions
234+
path_prefix = f"{outdir}/{batch_indexes[i][0]}_{batch_indexes[i][1]}"
235+
model_output = model_output.detach().cpu()
236+
# save image:
237+
image_to_disk(np.array(batch[i, 0, :, :]), path_prefix + "_img.png")
238+
# dump model prediction:
239+
mask_to_disk(model_output[i, :, :, :].argmax(dim=1).numpy(), path_prefix + "_pred.png", num_classes)
240+
# dump model confidence values
238241
for nclass in range(num_classes):
239-
mask_to_disk(
240-
np.array(model_output[i, nclass, :, :].detach().cpu()),
241-
f"{outdir}/{batch_indexes[i][0]}_{batch_indexes[i][1]}_class_{nclass}_pred.png",
242+
image_to_disk(
243+
model_output[i, nclass, :, :].numpy(), path_prefix + f"_class_{nclass}_conf.png",
242244
)
243245

244246
# crop the output_p in the middle
@@ -279,7 +281,7 @@ def _evaluate_split(
279281

280282
running_metrics_split = runningScore(n_classes)
281283

282-
# testing mode:
284+
# evaluation mode:
283285
with torch.no_grad(): # operations inside don't track history
284286
model.eval()
285287
total_iteration = 0
@@ -307,8 +309,8 @@ def _evaluate_split(
307309
running_metrics_overall.update(gt, pred)
308310

309311
# dump images to disk for review
310-
mask_to_disk(pred.squeeze(), os.path.join(output_dir, f"{i}_pred.png"))
311-
mask_to_disk(gt.squeeze(), os.path.join(output_dir, f"{i}_gt.png"))
312+
mask_to_disk(pred.squeeze(), os.path.join(output_dir, f"{i}_pred.png"), n_classes)
313+
mask_to_disk(gt.squeeze(), os.path.join(output_dir, f"{i}_gt.png"), n_classes)
312314

313315
# get scores
314316
score, class_iou = running_metrics_split.get_scores()

experiments/interpretation/dutchf3_patch/local/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def run(*options, cfg=None, debug=False):
8383
# Set CUDNN benchmark mode:
8484
torch.backends.cudnn.benchmark = config.CUDNN.BENCHMARK
8585

86-
# we will write the model under outputs / config_file_name / model_dir
86+
# We will write the model under outputs / config_file_name / model_dir
8787
config_file_name = "default_config" if not cfg else cfg.split("/")[-1].split(".")[0]
8888

8989
# Fix random seeds:

interpretation/deepseismic_interpretation/dutchf3/data.py

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,9 @@ def __getitem__(self, index):
154154
if self.debug and "test" in self.split:
155155
outdir = f"debug/sectionLoader_{self.split}_raw"
156156
generate_path(outdir)
157-
image_to_disk(im, f"{outdir}/index_{index}_section_{section_name}_img.png")
158-
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{section_name}_lbl.png")
157+
path_prefix = f"{outdir}/index_{index}_section_{section_name}"
158+
image_to_disk(im, path_prefix + "_img.png")
159+
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)
159160

160161
if self.augmentations is not None:
161162
augmented_dict = self.augmentations(image=im, mask=lbl)
@@ -167,8 +168,9 @@ def __getitem__(self, index):
167168
if self.debug and "test" in self.split:
168169
outdir = f"debug/sectionLoader_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
169170
generate_path(outdir)
170-
image_to_disk(np.array(im[0]), f"{outdir}/index_{index}_section_{section_name}_img.png")
171-
mask_to_disk(np.array(lbl[0]), f"{outdir}/index_{index}_section_{section_name}_lbl.png")
171+
path_prefix = f"{outdir}/index_{index}_section_{section_name}"
172+
image_to_disk(np.array(im[0]), path_prefix + "_img.png")
173+
mask_to_disk(np.array(lbl[0]), path_prefix + "_lbl.png", self.n_classes)
172174

173175
return im, lbl
174176

@@ -408,8 +410,9 @@ def __getitem__(self, index):
408410
outdir = f"debug/testSectionLoaderWithDepth_{self.split}_raw"
409411
generate_path(outdir)
410412
# this needs to take the first dimension of image (no depth) but lbl only has 1 dim
411-
image_to_disk(im[0, :, :], f"{outdir}/index_{index}_section_{section_name}_img.png")
412-
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{section_name}_lbl.png")
413+
path_prefix = f"{outdir}/index_{index}_section_{section_name}"
414+
image_to_disk(im[0, :, :], path_prefix + "_img.png")
415+
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)
413416

414417
if self.augmentations is not None:
415418
im = _transform_CHW_to_HWC(im)
@@ -426,8 +429,9 @@ def __getitem__(self, index):
426429
f"debug/testSectionLoaderWithDepth_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
427430
)
428431
generate_path(outdir)
429-
image_to_disk(np.array(im[0, :, :]), f"{outdir}/index_{index}_section_{section_name}_img.png")
430-
mask_to_disk(np.array(lbl[0, :, :]), f"{outdir}/index_{index}_section_{section_name}_lbl.png")
432+
path_prefix = f"{outdir}/index_{index}_section_{section_name}"
433+
image_to_disk(np.array(im[0, :, :]), path_prefix + "_img.png")
434+
mask_to_disk(np.array(lbl[0, :, :]), path_prefix + "_lbl.png", self.n_classes)
431435

432436
return im, lbl
433437

@@ -495,8 +499,9 @@ def __getitem__(self, index):
495499
if self.debug:
496500
outdir = f"debug/patchLoader_{self.split}_raw"
497501
generate_path(outdir)
498-
image_to_disk(im, f"{outdir}/index_{index}_section_{patch_name}_img.png")
499-
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{patch_name}_lbl.png")
502+
path_prefix = f"{outdir}/index_{index}_section_{patch_name}"
503+
image_to_disk(im, path_prefix + "_img.png")
504+
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)
500505

501506
if self.augmentations is not None:
502507
augmented_dict = self.augmentations(image=im, mask=lbl)
@@ -506,8 +511,9 @@ def __getitem__(self, index):
506511
if self.debug:
507512
outdir = f"patchLoader_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
508513
generate_path(outdir)
509-
image_to_disk(im, f"{outdir}/{index}_img.png")
510-
mask_to_disk(lbl, f"{outdir}/{index}_lbl.png")
514+
path_prefix = f"{outdir}/{index}"
515+
image_to_disk(im, path_prefix + "_img.png")
516+
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)
511517

512518
if self.is_transform:
513519
im, lbl = self.transform(im, lbl)
@@ -516,8 +522,9 @@ def __getitem__(self, index):
516522
if self.debug:
517523
outdir = f"debug/patchLoader_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
518524
generate_path(outdir)
519-
image_to_disk(np.array(im[0, :, :]), f"{outdir}/index_{index}_section_{patch_name}_img.png")
520-
mask_to_disk(np.array(lbl[0, :, :]), f"{outdir}/index_{index}_section_{patch_name}_lbl.png")
525+
path_prefix = f"{outdir}/index_{index}_section_{patch_name}"
526+
image_to_disk(np.array(im[0, :, :]), path_prefix + "_img.png")
527+
mask_to_disk(np.array(lbl[0, :, :]), path_prefix + "_lbl.png", self.n_classes)
521528

522529
return im, lbl
523530

@@ -761,8 +768,9 @@ def __getitem__(self, index):
761768
if self.debug:
762769
outdir = f"debug/patchLoaderWithSectionDepth_{self.split}_raw"
763770
generate_path(outdir)
764-
image_to_disk(im[0, :, :], f"{outdir}/index_{index}_section_{patch_name}_img.png")
765-
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{patch_name}_lbl.png")
771+
path_prefix = f"{outdir}/index_{index}_section_{patch_name}"
772+
image_to_disk(im[0, :, :], path_prefix + "_img.png")
773+
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)
766774

767775
if self.augmentations is not None:
768776
im = _transform_CHW_to_HWC(im)
@@ -774,8 +782,9 @@ def __getitem__(self, index):
774782
if self.debug:
775783
outdir = f"patchLoaderWithSectionDepth_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
776784
generate_path(outdir)
777-
image_to_disk(im[0,:,:], f"{outdir}/{index}_img.png")
778-
mask_to_disk(lbl, f"{outdir}/{index}_lbl.png")
785+
path_prefix = f"{outdir}/{index}"
786+
image_to_disk(im[0, :, :], path_prefix + "_img.png")
787+
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)
779788

780789
if self.is_transform:
781790
im, lbl = self.transform(im, lbl)
@@ -786,8 +795,9 @@ def __getitem__(self, index):
786795
f"debug/patchLoaderWithSectionDepth_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
787796
)
788797
generate_path(outdir)
789-
image_to_disk(np.array(im[0, :, :]), f"{outdir}/index_{index}_section_{patch_name}_img.png")
790-
mask_to_disk(np.array(lbl[0, :, :]), f"{outdir}/index_{index}_section_{patch_name}_lbl.png")
798+
path_prefix = f"{outdir}/index_{index}_section_{patch_name}"
799+
image_to_disk(np.array(im[0, :, :]), path_prefix + "_img.png")
800+
mask_to_disk(np.array(lbl[0, :, :]), path_prefix + "_lbl.png", self.n_classes)
791801

792802
return im, lbl
793803

tests/cicd/main_build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ jobs:
272272

273273
- job: dutchf3_patch
274274
dependsOn: checkerboard_dutchf3_patch
275-
timeoutInMinutes: 25
275+
timeoutInMinutes: 60
276276
displayName: Dutch F3 patch local
277277
pool:
278278
name: deepseismicagentpool

0 commit comments

Comments
 (0)