FormulaEvaluator
- class hybrid_learning.experimentation.fuzzy_exp.fuzzy_exp_helpers.FormulaEvaluator(conf=None, formulas=None, additional_formulas=None, predicates=None, formula_caches=None, predicates_cache=None, constants=None, changed_constants=None, changed_root=None, device=None)[source]
Bases:
object
Evaluate predicates and formulas with intermediate values cached according to a config.
Public Data Attributes:
The constants actually used in one of the formulas.
Public Methods:
Set
constants
to a dict only containing the used constants.calc_results_for
(images[, initial_masks, ...])Calculate predicate and formula outputs for a batch of input images.
calc_result_for
(image, initial_masks[, ...])Wrapper around
calc_results_for
that handles single inputs instead of a batch.Special Methods:
__init__
([conf, formulas, ...])Init.
- Parameters
- __init__(conf=None, formulas=None, additional_formulas=None, predicates=None, formula_caches=None, predicates_cache=None, constants=None, changed_constants=None, changed_root=None, device=None)[source]
Init. To disable caching, set
conf['results_cache_dir']=None
or assigncaching.NoCache()
toformula_caches
andpredicates_cache
.- Parameters
conf (Optional[Mapping[str, Any]]) – the experiment config to use for setting defaults; may be skipped if all other settings are given
formulas (Optional[Union[Sequence[Merge], Dict[str, Merge]]]) – the formulas to evaluate (see
formulas
); defaults to[conf['formula'], conf['gt_formula']]
; may be given as dict{new_out_key: formula_object}
or as sequence of formula objects, in which case theformula.out_key
is used asnew_out_key
.additional_formulas (Optional[Union[Sequence[Merge], Dict[str, Merge]]]) – add these to
formulas
(after setting defaults toformulas
); asformulas
, may be dict or sequencepredicates (Optional[Module]) – the predicates model (see
predicates
); defaults to the output ofget_predicates()
formula_caches (Optional[Dict[str, Cache]]) – dictionary of
{out_key: cache}
for caching formula (intermediate) outputs (seeformula_caches
); defaults to output offormula_trafo_caches_for()
; set to{}
to disablepredicates_cache (Optional[Cache]) – the cache used for the
predicates
outputs; defaults to output ofpredicates_cache_for()
; set tocaching.NoCache()
to disableconstants (Optional[Dict[str, Any]]) – constants to use; defaults to
conf['constants']
changed_constants (Optional[Dict[str, Any]]) – update
constants
(also default value)changed_root (Optional[str]) – prepend
changed_root
to all used paths fromconf
device (Optional[Union[str, device]]) – device to use for evaluation; defaults to
conf['device']
- calc_result_for(image, initial_masks, desc=None, only_formula_out=False)[source]
Wrapper around
calc_results_for
that handles single inputs instead of a batch.
- calc_results_for(images, initial_masks=None, descs=None, only_formula_out=False)[source]
Calculate predicate and formula outputs for a batch of input images.
- Parameters
images (Tensor) – batch of input images of shape
[batch, channels, height, width]
initial_masks (Optional[Dict[str, Tensor]]) – any further (pre-calculated) inputs to the formula calculation, e.g. the ground truth masks; these take precedence over newly calculated predicate outputs and
constants
descs (Optional[Sequence[Hashable]]) – the descriptors of the images for obtaining respective predicate outputs from
predicates_cache
only_formula_out (bool) – whether to prune the output to only contain the output values of the formulas from
formulas
- Returns
dictionary containing the outputs of the formulas as
{out_key: output}
without_key
fromformulas
; ifonly_formula_out
isFalse
, also the following intermediate outputs are added: predicate outputs indexed by the prediate name, all formula outputs and intermediate outputs according to the formulakeep_keys
settings (by default, all formula intermediate outputs registered for caching are added to this), allinitial_masks
,constants
- Return type
- restrict_to_used_constants()[source]
Set
constants
to a dict only containing the used constants.- Return type
- constants: Dict[str, Any]
The constants to supply to the formula evaluation. Have higher precendence than newly calculated predicate outputs.
- formula_caches: Optional[Dict[str, Cache]]
Dictionary of
{out_key: cache}
for caching formula (intermediate) outputs.
- formulas: Dict[str, Merge]
The formula objects to evaluate given as dictionary of the form
{new_out_key: formula_object}
. Thenew_out_key
is used instead of theformula_object.out_key
for the final output. The latter is only kept if there is a cache registered for it, seeformula_caches
.
- predicates: torch.nn.modules.module.Module
The model producing the dictionary of predicate outputs.