Skip to content
Snippets Groups Projects
Commit 9ebfcb57 authored by André Anjos's avatar André Anjos :speech_balloon:
Browse files

[script.*] Change prediction to write dataset predictions on a subfolder named...

[script.*] Change prediction to write dataset predictions on a subfolder named after the dataset name in the current run
parent aa5519fe
No related branches found
No related tags found
No related merge requests found
Pipeline #40009 failed
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
"""The main entry for bob ip binseg (click-based) scripts.""" """The main entry for bob ip binseg (click-based) scripts."""
import os import os
import re
import sys import sys
import time import time
import tempfile import tempfile
...@@ -20,6 +21,30 @@ import logging ...@@ -20,6 +21,30 @@ import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def escape_name(v):
"""Escapes a name so it contains filesystem friendly characters only
This function escapes every character that's not a letter, ``_``, ``-``,
``.`` or space with an ``-``.
Parameters
==========
v : str
String to be escaped
Returns
=======
s : str
Escaped string
"""
return re.sub(r'[^\w\-_\. ]', '-', v)
def save_sh_command(destfile): def save_sh_command(destfile):
"""Records command-line to reproduce this experiment """Records command-line to reproduce this experiment
......
...@@ -45,6 +45,38 @@ def _validate_threshold(t, dataset): ...@@ -45,6 +45,38 @@ def _validate_threshold(t, dataset):
return t return t
def _get_folder(folder, name):
"""Guesses the prediction folder to use based on the dataset name
This function will look for ``folder/name`` if it exists, and
return this. Otherwise defaults to ``folder``.
Parameters
==========
folder : str
Path to the root of the predictions folder
name : str
The name of the dataset for which we are trying to find the predictions
folder
Returns
=======
path : str
The best path to use as the root of the predictions folder for this
dataset.
"""
candidate = os.path.join(folder, name)
if os.path.exists(candidate):
return candidate
return folder
@click.command( @click.command(
entry_point_group="bob.ip.binseg.config", entry_point_group="bob.ip.binseg.config",
cls=ConfigCommand, cls=ConfigCommand,
...@@ -170,13 +202,17 @@ def evaluate( ...@@ -170,13 +202,17 @@ def evaluate(
second_annotator = {} second_annotator = {}
elif not isinstance(second_annotator, dict): elif not isinstance(second_annotator, dict):
second_annotator = {"test": second_annotator} second_annotator = {"test": second_annotator}
#else, second_annotator must be a dict # else, second_annotator must be a dict
if isinstance(threshold, str): if isinstance(threshold, str):
# first run evaluation for reference dataset, do not save overlays # first run evaluation for reference dataset, do not save overlays
logger.info(f"Evaluating threshold on '{threshold}' set") logger.info(f"Evaluating threshold on '{threshold}' set")
threshold = run(dataset[threshold], threshold, predictions_folder, threshold = run(
steps=steps) dataset[threshold],
threshold,
_get_folder(predictions_folder, threshold),
steps=steps,
)
logger.info(f"Set --threshold={threshold:.5f}") logger.info(f"Set --threshold={threshold:.5f}")
# now run with the # now run with the
...@@ -185,13 +221,22 @@ def evaluate( ...@@ -185,13 +221,22 @@ def evaluate(
logger.info(f"Skipping dataset '{k}' (not to be evaluated)") logger.info(f"Skipping dataset '{k}' (not to be evaluated)")
continue continue
logger.info(f"Analyzing '{k}' set...") logger.info(f"Analyzing '{k}' set...")
run(v, k, predictions_folder, output_folder, overlayed, threshold, run(
steps=steps) v,
k,
_get_folder(predictions_folder, k),
output_folder,
overlayed,
threshold,
steps=steps,
)
second = second_annotator.get(k) second = second_annotator.get(k)
if second is not None: if second is not None:
if not second.all_keys_match(v): if not second.all_keys_match(v):
logger.warning(f"Key mismatch between `dataset[{k}]` and " \ logger.warning(
f"`second_annotator[{k}]` - skipping " \ f"Key mismatch between `dataset[{k}]` and "
f"second-annotator comparisons for {k} subset") f"`second_annotator[{k}]` - skipping "
f"second-annotator comparisons for {k} subset"
)
else: else:
compare_annotators(v, second, k, output_folder, overlayed) compare_annotators(v, second, k, output_folder, overlayed)
...@@ -145,4 +145,8 @@ def predict(output_folder, model, dataset, batch_size, device, weight, ...@@ -145,4 +145,8 @@ def predict(output_folder, model, dataset, batch_size, device, weight,
shuffle=False, shuffle=False,
pin_memory=torch.cuda.is_available(), pin_memory=torch.cuda.is_available(),
) )
run(model, data_loader, device, output_folder, overlayed) # this avoids collisions if we have, e.g., multi-resolution versions
# of the same dataset being evaluated, or datasets for which filenames
# may match.
use_output_folder = os.path.join(output_folder, k)
run(model, data_loader, device, use_output_folder, overlayed)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment