MCPcopy Create free account
hub / github.com/PAIR-code/lit / run

Method run

lit_nlp/components/image_gradient_maps.py:219–312  ·  view source on GitHub ↗

Runs the component, given a model and input(s).

(
      self,
      inputs: list[JsonDict],
      model: lit_model.Model,
      dataset: lit_dataset.Dataset,
      model_outputs: Optional[list[JsonDict]] = None,
      config: Optional[JsonDict] = None,
  )

Source from the content-addressed store, hash-verified

217 """A base class for all interpreters that use the Saliency library."""
218
219 def run(
220 self,
221 inputs: list[JsonDict],
222 model: lit_model.Model,
223 dataset: lit_dataset.Dataset,
224 model_outputs: Optional[list[JsonDict]] = None,
225 config: Optional[JsonDict] = None,
226 ) -> Optional[list[JsonDict]]:
227 """Runs the component, given a model and input(s)."""
228 input_spec = model.input_spec()
229 output_spec = model.output_spec()
230 config = config or {}
231
232 if not inputs:
233 return []
234
235 # Find all fields required for the interpretation.
236 supported_fields = find_supported_fields(input_spec, output_spec)
237 if supported_fields is None:
238 return None
239
240 grad_field_key = supported_fields.grad_field_key
241 image_field_key = supported_fields.image_field_key
242 grad_target_field_key = supported_fields.grad_target_field_key
243
244 if target_config := config.get(TARGET_INFO_KEY):
245 preds_field_key = target_config['field']
246 else:
247 preds_field_key = supported_fields.preds_field_key
248
249 preds_field_spec = output_spec[preds_field_key]
250 if not isinstance(preds_field_spec, _SUPPORTED_PRED_TYPES):
251 logging.warning(
252 "Image Salience is not compatible with field '%s'", preds_field_key
253 )
254 return None
255
256 # Determine the shape of gradients by calling the model with a single input
257 # and extracting the shape from the gradient output.
258 first_example_preds = list(model.predict([inputs[0]]))[0]
259 grad_shape = first_example_preds[grad_field_key].shape
260
261 # If it is a multiclass model, find the labels with respect to which we
262 # should compute the gradients.
263 if isinstance(preds_field_spec, types.MulticlassPreds):
264 # Get class labels.
265 label_vocab = list(preds_field_spec.vocab)
266
267 if (target_config := config.get(TARGET_INFO_KEY)) and (
268 target_class := target_config.get('index')
269 ):
270 grad_target_labels = [label_vocab[target_class] for _ in inputs]
271 else:
272 # Run the model in order to find the gradient target labels.
273 outputs = list(model.predict(inputs))
274 grad_target_labels = []
275 for model_input, model_output in zip(inputs, outputs):
276 if model_input.get(grad_target_field_key) is not None:

Callers

nothing calls this directly

Calls 10

get_saliency_objectMethod · 0.95
make_saliency_callMethod · 0.95
find_supported_fieldsFunction · 0.85
get_call_model_funcFunction · 0.85
getMethod · 0.80
input_specMethod · 0.45
output_specMethod · 0.45
predictMethod · 0.45

Tested by

no test coverage detected