Skip to content

Commit

Permalink
FOO
Browse files Browse the repository at this point in the history
  • Loading branch information
ralager committed Jul 8, 2021
1 parent 99ea2f2 commit 20f8f55
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 68 deletions.
93 changes: 54 additions & 39 deletions gewittergefahr/dissertation/run_mc_test_for_gradcam.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SMOOTHING_RADIUS_ARG_NAME = 'smoothing_radius_grid_cells'
MAX_PERCENTILE_ARG_NAME = 'max_pmm_percentile_level'
NUM_ITERATIONS_ARG_NAME = 'num_iterations'
COMPARE_RANKED_ARG_NAME = 'compare_ranked_values'
OUTPUT_FILE_ARG_NAME = 'output_file_name'

ACTUAL_FILE_HELP_STRING = (
Expand All @@ -38,6 +39,10 @@
'Max percentile level for probability-matched means (PMM).'
)
NUM_ITERATIONS_HELP_STRING = 'Number of iterations for Monte Carlo test.'
COMPARE_RANKED_HELP_STRING = (
'Boolean flag. If 1 (0), will compare spatially ranked (raw) class '
'activations.'
)
OUTPUT_FILE_HELP_STRING = (
'Path to output file (will be written by `_write_results`).'
)
Expand All @@ -63,6 +68,10 @@
'--' + NUM_ITERATIONS_ARG_NAME, type=int, required=False, default=20000,
help=NUM_ITERATIONS_HELP_STRING
)
INPUT_ARG_PARSER.add_argument(
'--' + COMPARE_RANKED_ARG_NAME, type=int, required=False, default=1,
help=COMPARE_RANKED_HELP_STRING
)
INPUT_ARG_PARSER.add_argument(
'--' + OUTPUT_FILE_ARG_NAME, type=str, required=True,
help=OUTPUT_FILE_HELP_STRING
Expand Down Expand Up @@ -135,7 +144,8 @@ def _smooth_maps(cam_matrices, guided_cam_matrices,


def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
max_pmm_percentile_level, num_iterations, output_file_name):
max_pmm_percentile_level, num_iterations, compare_ranked_values,
output_file_name):
"""Runs Monte Carlo test for class-activation maps (CAM).
This is effectively the main method.
Expand All @@ -145,6 +155,7 @@ def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
:param smoothing_radius_grid_cells: Same.
:param max_pmm_percentile_level: Same.
:param num_iterations: Same.
:param compare_ranked_values: Same.
:param output_file_name: Same.
"""

Expand Down Expand Up @@ -191,59 +202,60 @@ def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
)

# Convert from absolute values to percentiles.
num_matrices = len(actual_cam_matrices)

for j in range(num_matrices):
if actual_cam_matrices[j] is None:
continue
if compare_ranked_values:
num_matrices = len(actual_cam_matrices)

num_examples = actual_cam_matrices[j].shape[0]
this_num_channels = actual_guided_cam_matrices[j].shape[-1]
for j in range(num_matrices):
if actual_cam_matrices[j] is None:
continue

for i in range(num_examples):
this_flat_array = numpy.ravel(actual_cam_matrices[j][i, ...])
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average') /
len(this_flat_array)
)
actual_cam_matrices[j][i, ...] = numpy.reshape(
these_flat_ranks, actual_cam_matrices[j][i, ...].shape
)

this_flat_array = numpy.ravel(dummy_cam_matrices[j][i, ...])
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average') /
len(this_flat_array)
)
dummy_cam_matrices[j][i, ...] = numpy.reshape(
these_flat_ranks, dummy_cam_matrices[j][i, ...].shape
)
num_examples = actual_cam_matrices[j].shape[0]
this_num_channels = actual_guided_cam_matrices[j].shape[-1]

for k in range(this_num_channels):
this_flat_array = numpy.ravel(
actual_guided_cam_matrices[j][i, ..., k]
)
for i in range(num_examples):
this_flat_array = numpy.ravel(actual_cam_matrices[j][i, ...])
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average') /
len(this_flat_array)
)
actual_guided_cam_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
actual_guided_cam_matrices[j][i, ..., k].shape
actual_cam_matrices[j][i, ...] = numpy.reshape(
these_flat_ranks, actual_cam_matrices[j][i, ...].shape
)

this_flat_array = numpy.ravel(
dummy_guided_cam_matrices[j][i, ..., k]
)
this_flat_array = numpy.ravel(dummy_cam_matrices[j][i, ...])
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average') /
len(this_flat_array)
)
dummy_guided_cam_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
dummy_guided_cam_matrices[j][i, ..., k].shape
dummy_cam_matrices[j][i, ...] = numpy.reshape(
these_flat_ranks, dummy_cam_matrices[j][i, ...].shape
)

for k in range(this_num_channels):
this_flat_array = numpy.ravel(
actual_guided_cam_matrices[j][i, ..., k]
)
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average')
/ len(this_flat_array)
)
actual_guided_cam_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
actual_guided_cam_matrices[j][i, ..., k].shape
)

this_flat_array = numpy.ravel(
dummy_guided_cam_matrices[j][i, ..., k]
)
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average')
/ len(this_flat_array)
)
dummy_guided_cam_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
dummy_guided_cam_matrices[j][i, ..., k].shape
)

# Do Monte Carlo test.
actual_cam_matrices = [
None if a is None else numpy.expand_dims(a, axis=-1)
Expand Down Expand Up @@ -300,5 +312,8 @@ def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
max_pmm_percentile_level=getattr(
INPUT_ARG_OBJECT, MAX_PERCENTILE_ARG_NAME),
num_iterations=getattr(INPUT_ARG_OBJECT, NUM_ITERATIONS_ARG_NAME),
compare_ranked_values=bool(
getattr(INPUT_ARG_OBJECT, COMPARE_RANKED_ARG_NAME)
),
output_file_name=getattr(INPUT_ARG_OBJECT, OUTPUT_FILE_ARG_NAME),
)
73 changes: 44 additions & 29 deletions gewittergefahr/dissertation/run_mc_test_for_saliency.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
SMOOTHING_RADIUS_ARG_NAME = 'smoothing_radius_grid_cells'
MAX_PERCENTILE_ARG_NAME = 'max_pmm_percentile_level'
NUM_ITERATIONS_ARG_NAME = 'num_iterations'
COMPARE_RANKED_ARG_NAME = 'compare_ranked_values'
OUTPUT_FILE_ARG_NAME = 'output_file_name'

ACTUAL_FILE_HELP_STRING = (
Expand All @@ -38,6 +39,10 @@
'Max percentile level for probability-matched means (PMM).'
)
NUM_ITERATIONS_HELP_STRING = 'Number of iterations for Monte Carlo test.'
COMPARE_RANKED_HELP_STRING = (
'Boolean flag. If 1 (0), will compare spatially ranked (raw) class '
'activations.'
)
OUTPUT_FILE_HELP_STRING = (
'Path to output file (will be written by `_write_results`).'
)
Expand All @@ -63,6 +68,10 @@
'--' + NUM_ITERATIONS_ARG_NAME, type=int, required=False, default=20000,
help=NUM_ITERATIONS_HELP_STRING
)
INPUT_ARG_PARSER.add_argument(
'--' + COMPARE_RANKED_ARG_NAME, type=int, required=False, default=1,
help=COMPARE_RANKED_HELP_STRING
)
INPUT_ARG_PARSER.add_argument(
'--' + OUTPUT_FILE_ARG_NAME, type=str, required=True,
help=OUTPUT_FILE_HELP_STRING
Expand Down Expand Up @@ -120,7 +129,8 @@ def _smooth_maps(saliency_matrices, smoothing_radius_grid_cells):


def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
max_pmm_percentile_level, num_iterations, output_file_name):
max_pmm_percentile_level, num_iterations, compare_ranked_values,
output_file_name):
"""Runs Monte Carlo test for saliency maps.
This is effectively the main method.
Expand All @@ -130,6 +140,7 @@ def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
:param smoothing_radius_grid_cells: Same.
:param max_pmm_percentile_level: Same.
:param num_iterations: Same.
:param compare_ranked_values: Same.
:param output_file_name: Same.
"""

Expand Down Expand Up @@ -175,37 +186,38 @@ def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
)

# Convert saliency from absolute values to percentiles.
num_matrices = len(actual_saliency_matrices)
num_examples = actual_saliency_matrices[0].shape[0]
if compare_ranked_values:
num_matrices = len(actual_saliency_matrices)
num_examples = actual_saliency_matrices[0].shape[0]

for j in range(num_matrices):
for i in range(num_examples):
this_num_channels = actual_saliency_matrices[j].shape[-1]
for j in range(num_matrices):
for i in range(num_examples):
this_num_channels = actual_saliency_matrices[j].shape[-1]

for k in range(this_num_channels):
this_flat_array = numpy.ravel(
actual_saliency_matrices[j][i, ..., k]
)
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average') /
len(this_flat_array)
)
actual_saliency_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
actual_saliency_matrices[j][i, ..., k].shape
)
for k in range(this_num_channels):
this_flat_array = numpy.ravel(
actual_saliency_matrices[j][i, ..., k]
)
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average')
/ len(this_flat_array)
)
actual_saliency_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
actual_saliency_matrices[j][i, ..., k].shape
)

this_flat_array = numpy.ravel(
dummy_saliency_matrices[j][i, ..., k]
)
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average') /
len(this_flat_array)
)
dummy_saliency_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
dummy_saliency_matrices[j][i, ..., k].shape
)
this_flat_array = numpy.ravel(
dummy_saliency_matrices[j][i, ..., k]
)
these_flat_ranks = (
scipy.stats.rankdata(this_flat_array, method='average')
/ len(this_flat_array)
)
dummy_saliency_matrices[j][i, ..., k] = numpy.reshape(
these_flat_ranks,
dummy_saliency_matrices[j][i, ..., k].shape
)

# Do Monte Carlo test.
monte_carlo_dict = monte_carlo.run_monte_carlo_test(
Expand Down Expand Up @@ -234,5 +246,8 @@ def _run(actual_file_name, dummy_file_name, smoothing_radius_grid_cells,
max_pmm_percentile_level=getattr(
INPUT_ARG_OBJECT, MAX_PERCENTILE_ARG_NAME),
num_iterations=getattr(INPUT_ARG_OBJECT, NUM_ITERATIONS_ARG_NAME),
compare_ranked_values=bool(
getattr(INPUT_ARG_OBJECT, COMPARE_RANKED_ARG_NAME)
),
output_file_name=getattr(INPUT_ARG_OBJECT, OUTPUT_FILE_ARG_NAME),
)

0 comments on commit 20f8f55

Please sign in to comment.