Utils

Autograd

diffinytrace.utils.autograd.grad(outputs: Tensor | Sequence[Tensor], inputs: Tensor | Sequence[Tensor] | GradientEdge | Sequence[GradientEdge], grad_outputs: Tensor | Sequence[Tensor] | None = None, retain_graph: bool | None = True, create_graph: bool = True, only_inputs: bool = True, is_grads_batched: bool = False, materialize_grads: bool = False, remove_no_grad_outputs: bool = True)[source]

Computes the gradients of the outputs with respect to the inputs.

Parameters:
  • outputs (torch.Tensor or tuple of torch.Tensor) – The output tensors.

  • inputs (torch.Tensor or tuple of torch.Tensor) – The input tensors.

  • grad_outputs (torch.Tensor or tuple of torch.Tensor, optional) – The gradients of the outputs.

  • retain_graph (bool, optional) – Whether to retain the graph after computing gradients.

  • create_graph (bool, optional) – Whether to create the graph for higher-order gradients.

  • only_inputs (bool, optional) – Whether to only compute gradients for the inputs.

  • is_grads_batched (bool, optional) – Whether the gradients are batched.

  • materialize_grads (bool, optional) – Whether to materialize the gradients.

  • remove_no_grad_outputs (bool, optional) – Whether to remove outputs that do not require gradients.

Returns:

A list of gradients for each input tensor.

Return type:

list

Irradiance Importer

diffinytrace.utils.irradiance_importer.create_irradiance_from_image_square(file_name, padding_ratio, grey_ratio, aperture_radius, dtype=torch.float64, shape=None)[source]

Create a function that returns the desired irradiance from an image file.

Parameters:
  • file_name (str) – The path to the image file.

  • padding_ratio (float) – The ratio of padding to be applied to the image.

  • grey_ratio (float) – The ratio of grey value to be applied to the image.

  • aperture_radius (float) – The radius of the aperture.

  • dtype (torch.dtype) – The data type for the output tensor.

  • shape (tuple, optional) – The shape to resize the image to. If None, no resizing is done.

Returns:

A function that takes a tensor as input and returns the desired irradiance.

Return type:

function

diffinytrace.utils.irradiance_importer.pil_center_crop(image)[source]

Crop the image to a square by centering it.

Parameters:

image (PIL.Image) – The image to be cropped.

Returns:

The cropped image.

Return type:

PIL.Image

diffinytrace.utils.irradiance_importer.load_image(name, padding_ratio, grey_ratio, shape=None)[source]

Load an image, convert it to grayscale, and apply padding.

Parameters:
  • name (str) – The path to the image file.

  • padding_ratio (float) – The ratio of padding to be applied to the image.

  • grey_ratio (float) – The ratio of grey value to be applied to the image.

  • shape (tuple, optional) – The shape to resize the image to. If None, no resizing is done.

Returns:

The processed image as a numpy array.

Return type:

np.ndarray

Module Contents