NVIDIA Clara Train 3.1
v3.1

ai4med.components.inferers package

class InferenceContext

Bases: abc.ABC

Contains required information to do inference

abstract close()

Closes the context

abstract get_model_input_shapes()

Gets the shapes of model inputs

Returns

A dict where keys are input names and values are shapes

abstract get_model_output_shapes()

Gets the shapes of model outputs

Returns

A dict where keys are output names and values are shapes

class Inferer(predictor: ai4med.components.inferers.predictor.Predictor)

Bases: abc.ABC

Describes the procedure to do inference

abstract infer(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Get prediction values given input values and InferenceContext

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

supported_extra_inputs(desired_input_names)

Returns what extra input items are supported.

When calling infer, the caller must not provide input items that are not supported by this inferer.

Parameters

desired_input_names – A list of formal names of the input items (e.g. tensor names)

Returns

A list of input names supported

supported_extra_outputs(desired_output_names)

Returns what extra output items are supported.

When calling infer, the caller must not ask for output items that are not supported by this inferer.

Parameters

desired_output_names – A list of formal names of the output items (e.g. tensor names)

Returns

A list of output names supported

class Predictor

Bases: abc.ABC

A wrapper class that wraps different backends to get prediction values

abstract predict(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Gets prediction values

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

abstract predict_list(model_input_values_list, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Gets prediction values for a list of input

Parameters
  • model_input_values_list (list) – A list of dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A list of dict of output names (string) map to output values (numpy array)

class SimpleInferer(predictor: ai4med.components.inferers.predictor.Predictor)

Bases: ai4med.components.inferers.inferer.Inferer

This class implements a simple inferer, which feeds input values directly into its Predictor to get prediction values

infer(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Get prediction values given input values and InferenceContext

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

supported_extra_inputs(desired_input_names)

Returns what extra input items are supported.

When calling infer, the caller must not provide input items that are not supported by this inferer.

Parameters

desired_input_names – A list of formal names of the input items (e.g. tensor names)

Returns

A list of input names supported

supported_extra_outputs(desired_output_names)

Returns what extra output items are supported.

When calling infer, the caller must not ask for output items that are not supported by this inferer.

Parameters

desired_output_names – A list of formal names of the output items (e.g. tensor names)

Returns

A list of output names supported

class ScanClassWindowInferer(predictor: ai4med.components.inferers.predictor.Predictor, roi_size=None, batch_size=1, scan_interval=None, final_activation=None)

Bases: ai4med.components.inferers.inferer.Inferer

Performs scanning window inference

Breaks the input volume into smaller cubes, runs inference on those small cubes, and gathers results.

Parameters
  • roi_size – Specifies the window width in each dimension

  • batch_size – Batch size for scanning window (this means how many smaller cubes that we want to batch to get result at a time)

  • scan_interval (list) – Scan interval.

Note
For scan interval:

If None, then default heuristics is used. If list, the length need to be the same as spatial dimension of data.

  • If the number > 1, it will be treated as real interval,

  • otherwise it will multiply with the dimension of data.

infer(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Get prediction values given input values and InferenceContext

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

supported_extra_inputs(desired_input_names)

Returns what extra input items are supported.

When calling infer, the caller must not provide input items that are not supported by this inferer.

Parameters

desired_input_names – A list of formal names of the input items (e.g. tensor names)

Returns

A list of input names supported

supported_extra_outputs(desired_output_names)

Returns what extra output items are supported.

When calling infer, the caller must not ask for output items that are not supported by this inferer.

Parameters

desired_output_names – A list of formal names of the output items (e.g. tensor names)

Returns

A list of output names supported

class ScanWindowInferer(predictor: ai4med.components.inferers.predictor.Predictor, roi_size=None, batch_size=1)

Bases: ai4med.components.inferers.inferer.Inferer

Performs scanning window inference

Breaks the input volume into smaller cubes, runs inference on those small cubes, and gathers results.

Parameters
  • roi_size – Specifies the window width in each dimension

  • batch_size – Batch size for scanning window (this means how many smaller cubes that we want to batch to get result at a time)

infer(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Get prediction values given input values and InferenceContext

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

supported_extra_inputs(desired_input_names)

Returns what extra input items are supported.

When calling infer, the caller must not provide input items that are not supported by this inferer.

Parameters

desired_input_names – A list of formal names of the input items (e.g. tensor names)

Returns

A list of input names supported

supported_extra_outputs(desired_output_names)

Returns what extra output items are supported.

When calling infer, the caller must not ask for output items that are not supported by this inferer.

Parameters

desired_output_names – A list of formal names of the output items (e.g. tensor names)

Returns

A list of output names supported

class TFInferenceContext(session, model_input_tensors: dict, model_output_tensors: dict)

Bases: ai4med.components.inferers.inference_context.InferenceContext

This defines TensorFlow 1.x session-based inference context data structure

model_input and model_output are the input/output of the neural network while extra_input might be learning rate placeholder and extra_output might be some validation metrics

Parameters
  • session – A TF session

  • model_input_tensors (dict) – A dict of model input names (string) map to input tensors (TF Tensor)

  • model_output_tensors (dict) – A dict of model output names (string) map to output tensors (TF Tensor)

  • extra_input_tensors (dict) – A dict of extra input names (string) map to input tensors (TF Tensor) set by set_extra_input_tensors

  • extra_input_values (dict) – A dict of extra input names (string) map to input values (numpy array) set by set_extra_input_tensors

  • extra_output_tensors (dict) – A dict of extra output names (string) map to input tensors (TF Tensor) set by set_extra_input_tensors

close()

Closes the context

get_model_input_shapes()

Gets the shapes of model inputs

Returns

A dict where keys are input names and values are shapes

get_model_output_shapes()

Gets the shapes of model outputs

Returns

A dict where keys are output names and values are shapes

set_extra_input_tensors(tensor_dict: dict, input_values: dict)

Sets extra input tensors and their input values for inference

Parameters
  • tensor_dict (dict) – name => tensor

  • input_values (dict) – name => value

set_extra_output_tensors(tensor_dict: dict)

Sets extra output tensors for inference

Parameters

tensor_dict (dict) – name => tensor

class TFPredictor

Bases: ai4med.components.inferers.predictor.Predictor

This class implements TF1.x style procedure

predict(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Gets prediction values

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

predict_list(model_input_values_list, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Gets prediction values for a list of input

Parameters
  • model_input_values_list (list) – A list of dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A list of dict of output names (string) map to output values (numpy array)

class TFSaliencyInferer(conv_layer_name='relu3_blk/Relu:0')

Bases: ai4med.components.inferers.simple_inferer.SimpleInferer

A wrapper class using TFSaliencyPredictor with SimpleInferer to generate saliency and processed input for visualization purposes for a specified layer.

class TFSaliencyPredictor(conv_layer_name='relu3_blk/Relu:0')

Bases: ai4med.components.inferers.predictor.Predictor

This class implements TF1.x style procedure to generate saliency and processed input for visualization purposes for a specified layer.

Modified and extended from https://github.com/cydonia999/Grad-CAM-in-TensorFlow/blob/master/grad-cam-tf.py and https://github.com/Ankush96/grad-cam.tensorflow/blob/master/main.py

predict(model_input_values, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Gets prediction values

Parameters
  • model_input_values (dict) – A dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A dict of output names (string) map to output values (numpy array)

predict_list(model_input_values_list, input_shape_format: ai4med.common.shape_format.ShapeFormat, inference_ctx: ai4med.components.inferers.inference_context.InferenceContext)

Gets prediction values for a list of input

Parameters
  • model_input_values_list (list) – A list of dict of input names (string) map to input values (numpy array)

  • input_shape_format (ShapeFormat) – shape format of input images

  • inference_ctx (InferenceContext) – inference context data

Returns

A list of dict of output names (string) map to output values (numpy array)

get_names(graph= )
resize_volume(nda, output_shape, order=1, preserve_range=True, anti_aliasing=False)
class TFSimpleInferer

Bases: ai4med.components.inferers.simple_inferer.SimpleInferer

A wrapper class using TFPredictor with SimpleInferer

class TFScanClassWindowInferer(roi_size=None, batch_size=1, scan_interval=None, final_activation=None)

Bases: ai4med.components.inferers.sw_class_inferer.ScanClassWindowInferer

A wrapper class using TFPredictor with ScanWindowInferer

class TFScanWindowInferer(roi_size=None, batch_size=1)

Bases: ai4med.components.inferers.sw_inferer.ScanWindowInferer

A wrapper class using TFPredictor with ScanWindowInferer

© Copyright 2020, NVIDIA. Last updated on Feb 2, 2023.