Skip to content

Commit

Permalink
Speedup and output (#38)
Browse files Browse the repository at this point in the history
* Make UniformModel more efficient

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

* Verbosity parameter in CrossvalMultipleRegularizations

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

---------

Signed-off-by: Matthias Kümmmerer <[email protected]>
  • Loading branch information
matthias-k authored Nov 18, 2023
1 parent a66a5c5 commit 0a48f5d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
9 changes: 5 additions & 4 deletions pysaliency/baseline_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,12 @@ def _normalize_regularization_factors(args):

class CrossvalMultipleRegularizations(object):
""" Class for computing crossvalidation scores of a fixation KDE with multiple regularization models"""
def __init__(self, stimuli, fixations, regularization_models: OrderedDict, crossvalidation):
def __init__(self, stimuli, fixations, regularization_models: OrderedDict, crossvalidation, verbose=False):
self.stimuli = stimuli
self.fixations = fixations

self.cv = crossvalidation
self.verbose = verbose

X_areas = fixations_to_scikit_learn(
self.fixations, normalize=stimuli,
Expand Down Expand Up @@ -406,19 +407,19 @@ def score(self, log_bandwidth, *args, **kwargs):
bandwidth=10**log_bandwidth,
regularizations=10**log_regularizations,
regularizing_log_likelihoods=self.regularization_log_likelihoods),
self.X, cv=self.cv, verbose=1).sum() / len(self.X) / np.log(2)
self.X, cv=self.cv, verbose=self.verbose).sum() / len(self.X) / np.log(2)
val += np.log2(self.mean_area)
return val


class CrossvalGoldMultipleRegularizations(CrossvalMultipleRegularizations):
def __init__(self, stimuli, fixations, regularization_models):
def __init__(self, stimuli, fixations, regularization_models, verbose=False):
if fixations.subject_count > 1:
crossvalidation_factory = ScikitLearnImageSubjectCrossValidationGenerator
else:
crossvalidation_factory = ScikitLearnWithinImageCrossValidationGenerator

super().__init__(stimuli, fixations, regularization_models, crossvalidation_factory=crossvalidation_factory)
super().__init__(stimuli, fixations, regularization_models, crossvalidation_factory=crossvalidation_factory, verbose=verbose)


# baseline models
Expand Down
11 changes: 7 additions & 4 deletions pysaliency/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,10 +407,13 @@ def _log_density(self, stimulus):
return np.zeros((stimulus.shape[0], stimulus.shape[1])) - np.log(stimulus.shape[0]) - np.log(stimulus.shape[1])

def log_likelihoods(self, stimuli, fixations, verbose=False):
lls = []
for n in fixations.n:
lls.append(-np.log(stimuli.shapes[n][0]) - np.log(stimuli.shapes[n][1]))
return np.array(lls)
stimulus_shapes = np.zeros((len(stimuli), 2), dtype=int)
stimulus_indices = sorted(np.unique(fixations.n))
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)
return stimulus_log_likelihoods[fixations.n]


class MixtureModel(Model):
Expand Down

0 comments on commit 0a48f5d

Please sign in to comment.