Skip to content

Commit

Permalink
Small bugs and enhancements (#60)
Browse files Browse the repository at this point in the history
* Cleanup imports

Signed-off-by: Matthias Kümmerer <[email protected]>

* BUG: AUC with average=image fails if there are images without fixations"

Now images without fixations are ignored, in consistency with how
it is handled in information_gain and NSS.

Signed-off-by: Matthias Kümmerer <[email protected]>

* Ignore log warnings in uniform model

Signed-off-by: Matthias Kümmerer <[email protected]>

* ENH: avoid computing saliency maps for AUC on images without fixations

Signed-off-by: Matthias Kümmerer <[email protected]>

---------

Signed-off-by: Matthias Kümmerer <[email protected]>
  • Loading branch information
matthias-k authored Mar 26, 2024
1 parent e87a2bd commit 6c067cb
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
1 change: 1 addition & 0 deletions pysaliency/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from . import models
from . import external_models
from . import external_datasets
from . import utils

from .datasets import (
Fixations,
Expand Down
7 changes: 6 additions & 1 deletion pysaliency/external_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,9 @@
CovSal,
)

from .utils import ExternalModelMixin
from .deepgaze import (
DeepGazeI,
DeepGazeIIE,
)

from .utils import ExternalModelMixin
4 changes: 3 additions & 1 deletion pysaliency/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,9 @@ def log_likelihoods(self, stimuli, fixations, verbose=False):
for stimulus_index in stimulus_indices:
stimulus_shapes[stimulus_index] = stimuli.stimulus_objects[stimulus_index].size

stimulus_log_likelihoods = -np.log(stimulus_shapes).sum(axis=1)
with np.errstate(divide='ignore'): # ignore log(0) warnings, we won't use them anyway
stimulus_log_likelihoods = -np.log(stimulus_shapes).sum(axis=1)

return stimulus_log_likelihoods[fixations.n]


Expand Down
15 changes: 14 additions & 1 deletion pysaliency/saliency_map_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,9 +410,14 @@ def AUC_per_image(self, stimuli, fixations, nonfixations='uniform', thresholds='
nonfixations = FullShuffledNonfixationProvider(stimuli, fixations)

for n in tqdm(range(len(stimuli)), disable=not verbose):
inds = fixations.n == n
if not inds.sum():
rocs_per_image.append(np.nan)
continue

out = self.saliency_map(stimuli.stimulus_objects[n])
check_prediction_shape(out, stimuli[n])
inds = fixations.n == n

positives = np.asarray(out[fixations.y_int[inds], fixations.x_int[inds]])
if nonfixations == 'uniform':
negatives = out.flatten()
Expand Down Expand Up @@ -482,6 +487,14 @@ def AUC(self, stimuli, fixations, nonfixations='uniform', average='fixation', th

return np.average(aucs, weights=weights)
elif average == 'image':
stimulus_indices = set(fixations.n)
nan_value_indices = np.nonzero(np.isnan(aucs))[0]

if stimulus_indices.intersection(nan_value_indices):
raise ValueError("Some images with fixations returned AUC of nan, which should not happen")

aucs = aucs[~np.isnan(aucs)]

return np.mean(aucs)
else:
raise ValueError(average)
Expand Down
25 changes: 25 additions & 0 deletions tests/test_saliency_map_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ def test_auc_gauss(stimuli, fixation_trains):
np.testing.assert_allclose(aucs_single, aucs_combined)


def test_auc_per_image(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

aucs = gsmm.AUC_per_image(stimuli, fixation_trains)
np.testing.assert_allclose(aucs,
[0.196625, 0.313125],
rtol=1e-6)


def test_auc_per_image_images_without_fixations(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

aucs = gsmm.AUC_per_image(stimuli, fixation_trains[:5],)
np.testing.assert_allclose(aucs,
[0.196625, np.nan],
rtol=1e-6)


def test_auc_image_average_with_images_without_fixations(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

auc = gsmm.AUC(stimuli, fixation_trains[:5], average='image')
np.testing.assert_allclose(auc, 0.196625, rtol=1e-6)


def test_nss_gauss(stimuli, fixation_trains):
gsmm = GaussianSaliencyMapModel()

Expand Down

0 comments on commit 6c067cb

Please sign in to comment.