pyhealth.processors.ImageProcessor#

Processor for image data.

class pyhealth.processors.ImageProcessor(image_size=224, to_tensor=True, normalize=False, mean=None, std=None, mode=None)[source]#

Bases: FeatureProcessor

Feature processor for loading images from disk and converting them to tensors.

Parameters:
  • image_size (int) – Desired output image size (will resize to square image). Defaults to 224.

  • to_tensor (bool) – Whether to convert image to tensor. Defaults to True.

  • normalize (bool) – Whether to normalize image values to [0, 1]. Defaults to False.

  • mean (Optional[List[float]]) – Precomputed mean for normalization.

  • std (Optional[List[float]]) – Precomputed std for normalization.

  • mode (Optional[str]) – PIL image mode to convert the image to before processing. Common modes: ‘RGB’, ‘RGBA’, ‘L’ (grayscale), ‘P’ (palette). If None, keeps the original mode. Defaults to None.

Raises:

ValueError – If normalization parameters are inconsistent.

process(value)[source]#

Process a single image path into a transformed tensor image.

Parameters:

value (Union[str, Path]) – Path to image file as string or Path object.

Return type:

Any

Returns:

Transformed image tensor.

Raises:

FileNotFoundError – If the image file does not exist.

is_token()[source]#

Image data is continuous (float-valued pixel intensities), not discrete tokens.

Return type:

bool

Returns:

False.

schema()[source]#

Single tensor output.

Return type:

tuple[str, ...]

Returns:

(“value”,)

dim()[source]#

Output tensor has 3 dimensions: (C, H, W).

Return type:

tuple[int, ...]

Returns:

(3,)

spatial()[source]#

Spatial axes for the output tensor (C, H, W).

Channels are not spatial; height and width are.

Return type:

tuple[bool, ...]

Returns:

(False, True, True)

fit(samples, field)#

Fit the processor to the samples.

Parameters:

samples (Iterable[Dict[str, Any]]) – List of sample dictionaries.

Return type:

None

load(path)#

Optional: Load processor state from disk.

Parameters:

path (str) – File path to load processor state from.

Return type:

None

save(path)#

Optional: Save processor state to disk.

Parameters:

path (str) – File path to save processor state.

Return type:

None