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

Fixing #324 and #325 #338

Merged
merged 9 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions cv_lib/cv_lib/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Expand All @@ -8,6 +7,7 @@
import numpy as np
from matplotlib import pyplot as plt


def normalize(array):
"""
Normalizes a segmentation mask array to be in [0,1] range
Expand All @@ -16,12 +16,19 @@ def normalize(array):
min = array.min()
return (array - min) / (array.max() - min)

def mask_to_disk(mask, fname, cmap_name="Paired"):

def mask_to_disk(mask, fname, n_classes, cmap_name="rainbow"):
"""
write segmentation mask to disk using a particular colormap
mask (float): this contains the predicted labels in the range [0, n_classes].
fname (str): of the the image to be saved
n_classes (int): total number of classes in the dataset
cmap_name (str): name of the matplotlib colormap to be used. The default "rainbow"
colormap works well for any number of classes.
"""
cmap = plt.get_cmap(cmap_name)
Image.fromarray(cmap(normalize(mask), bytes=True)).save(fname)
Image.fromarray(cmap(mask / n_classes, bytes=True)).save(fname)


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

def decode_segmap(label_mask, colormap_name="Paired"):

def decode_segmap(label_mask, colormap_name="rainbow"):
"""
Decode segmentation class labels into a colour image
Args:
Expand All @@ -48,6 +56,7 @@ def decode_segmap(label_mask, colormap_name="Paired"):

return out


def load_log_configuration(log_config_file):
"""
Loads logging configuration from the given configuration file.
Expand Down
22 changes: 12 additions & 10 deletions experiments/interpretation/dutchf3_patch/local/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,14 +231,16 @@ def _patch_label_2d(
outdir = f"debug/batch_{split}"
generate_path(outdir)
for i in range(batch.shape[0]):
image_to_disk(
np.array(batch[i, 0, :, :]), f"{outdir}/{batch_indexes[i][0]}_{batch_indexes[i][1]}_img.png"
)
# now dump model predictions
path_prefix = f"{outdir}/{batch_indexes[i][0]}_{batch_indexes[i][1]}"
model_output = model_output.detach().cpu()
# save image:
image_to_disk(np.array(batch[i, 0, :, :]), path_prefix + "_img.png")
# dump model prediction:
mask_to_disk(model_output[i, :, :, :].argmax(dim=1).numpy(), path_prefix + "_pred.png", num_classes)
# dump model confidence values
for nclass in range(num_classes):
mask_to_disk(
np.array(model_output[i, nclass, :, :].detach().cpu()),
f"{outdir}/{batch_indexes[i][0]}_{batch_indexes[i][1]}_class_{nclass}_pred.png",
image_to_disk(
model_output[i, nclass, :, :].numpy(), path_prefix + f"_class_{nclass}_conf.png",
)

# crop the output_p in the middle
Expand Down Expand Up @@ -279,7 +281,7 @@ def _evaluate_split(

running_metrics_split = runningScore(n_classes)

# testing mode:
# evaluation mode:
with torch.no_grad(): # operations inside don't track history
model.eval()
total_iteration = 0
Expand Down Expand Up @@ -307,8 +309,8 @@ def _evaluate_split(
running_metrics_overall.update(gt, pred)

# dump images to disk for review
mask_to_disk(pred.squeeze(), os.path.join(output_dir, f"{i}_pred.png"))
mask_to_disk(gt.squeeze(), os.path.join(output_dir, f"{i}_gt.png"))
mask_to_disk(pred.squeeze(), os.path.join(output_dir, f"{i}_pred.png"), n_classes)
mask_to_disk(gt.squeeze(), os.path.join(output_dir, f"{i}_gt.png"), n_classes)

# get scores
score, class_iou = running_metrics_split.get_scores()
Expand Down
2 changes: 1 addition & 1 deletion experiments/interpretation/dutchf3_patch/local/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def run(*options, cfg=None, debug=False):
# Set CUDNN benchmark mode:
torch.backends.cudnn.benchmark = config.CUDNN.BENCHMARK

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

# Fix random seeds:
Expand Down
50 changes: 30 additions & 20 deletions interpretation/deepseismic_interpretation/dutchf3/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ def __getitem__(self, index):
if self.debug and "test" in self.split:
outdir = f"debug/sectionLoader_{self.split}_raw"
generate_path(outdir)
image_to_disk(im, f"{outdir}/index_{index}_section_{section_name}_img.png")
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{section_name}_lbl.png")
path_prefix = f"{outdir}/index_{index}_section_{section_name}"
image_to_disk(im, path_prefix + "_img.png")
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)

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

return im, lbl

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

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

return im, lbl

Expand Down Expand Up @@ -495,8 +499,9 @@ def __getitem__(self, index):
if self.debug:
outdir = f"debug/patchLoader_{self.split}_raw"
generate_path(outdir)
image_to_disk(im, f"{outdir}/index_{index}_section_{patch_name}_img.png")
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{patch_name}_lbl.png")
path_prefix = f"{outdir}/index_{index}_section_{patch_name}"
image_to_disk(im, path_prefix + "_img.png")
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)

if self.augmentations is not None:
augmented_dict = self.augmentations(image=im, mask=lbl)
Expand All @@ -506,8 +511,9 @@ def __getitem__(self, index):
if self.debug:
outdir = f"patchLoader_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
generate_path(outdir)
image_to_disk(im, f"{outdir}/{index}_img.png")
mask_to_disk(lbl, f"{outdir}/{index}_lbl.png")
path_prefix = f"{outdir}/{index}"
image_to_disk(im, path_prefix + "_img.png")
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)

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

return im, lbl

Expand Down Expand Up @@ -761,8 +768,9 @@ def __getitem__(self, index):
if self.debug:
outdir = f"debug/patchLoaderWithSectionDepth_{self.split}_raw"
generate_path(outdir)
image_to_disk(im[0, :, :], f"{outdir}/index_{index}_section_{patch_name}_img.png")
mask_to_disk(lbl, f"{outdir}/index_{index}_section_{patch_name}_lbl.png")
path_prefix = f"{outdir}/index_{index}_section_{patch_name}"
image_to_disk(im[0, :, :], path_prefix + "_img.png")
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)

if self.augmentations is not None:
im = _transform_CHW_to_HWC(im)
Expand All @@ -774,8 +782,9 @@ def __getitem__(self, index):
if self.debug:
outdir = f"patchLoaderWithSectionDepth_{self.split}_{'aug' if self.augmentations is not None else 'noaug'}"
generate_path(outdir)
image_to_disk(im[0,:,:], f"{outdir}/{index}_img.png")
mask_to_disk(lbl, f"{outdir}/{index}_lbl.png")
path_prefix = f"{outdir}/{index}"
image_to_disk(im[0, :, :], path_prefix + "_img.png")
mask_to_disk(lbl, path_prefix + "_lbl.png", self.n_classes)

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

return im, lbl

Expand Down
2 changes: 1 addition & 1 deletion tests/cicd/main_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ jobs:

- job: dutchf3_patch
dependsOn: checkerboard_dutchf3_patch
timeoutInMinutes: 25
timeoutInMinutes: 60
displayName: Dutch F3 patch local
pool:
name: deepseismicagentpool
Expand Down