pvcracks.utils package

Subpackages

Submodules

pvcracks.utils.img_functions module

class pvcracks.utils.img_functions.ChanneledFixResize(size)[source]

Bases: object

Resize images and multi-channel masks to a common square shape.

Store the target square size for per-channel resizing.

Parameters

size (int) – Desired height and width after resizing.

class pvcracks.utils.img_functions.Compose(transforms)[source]

Bases: object

Store a sequence of paired image/mask transforms.

Parameters

transforms (Iterable[callable]) – Transform callables accepting (image, mask).

class pvcracks.utils.img_functions.FixResize(size)[source]

Bases: object

Resize PIL images and numpy masks to a fixed square size. This is for single-channel masks.

Store the target square size for resizing operations.

Parameters

size (int) – Desired height and width after resizing. Must be multiple of 16.

class pvcracks.utils.img_functions.Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225))[source]

Bases: object

Store channel statistics used to normalize image tensors.

Parameters
  • mean (tuple) – Per-channel mean values.

  • std (tuple) – Per-channel standard deviations.

class pvcracks.utils.img_functions.PILToTensor[source]

Bases: object

Transform the image to tensor. Keep raw type.

class pvcracks.utils.img_functions.SolarDataset(root, image_folder, mask_folder, transforms, mode='train', random_seed=42)[source]

Bases: VisionDataset

A dataset for our solar panel images and masks.

Set up the dataset by collecting and shuffling image and mask paths.

Parameters
  • root (str or Path) – Root directory that contains the data folders.

  • image_folder (str or Path) – Subdirectory with image files.

  • mask_folder (str or Path) – Subdirectory with mask files stored as numpy arrays.

  • transforms (callable) – Callable applied to (image, mask) during __getitem__.

  • mode (str, optional) – Dataset split indicator; kept for backward compatibility.

  • random_seed (int, optional) – Seed used when shuffling to keep pairs aligned.

get_img_path(index)[source]

Return the full path to the image file at index.

Parameters

index (int) – Dataset index referencing the desired image.

Returns

Absolute path to the image file.

Return type

str

class pvcracks.utils.img_functions.ToTensor[source]

Bases: object

Transform the image to tensor. Scale the image to [0,1] float32. Transform the mask to tensor.

pvcracks.utils.segmentation module

Created on Tue Nov 4 21:19:19 2025

@authors: nrjost

pvcracks.utils.segmentation.segment(image, device, model, category_mapping={0: 'dark', 1: 'busbar', 2: 'crack', 3: 'non-cell'}, threshold=0.5)[source]
Run segmentation model on an image to extract defect masks:
  • dark area mask

  • busbars mask

  • cracks mask

  • no-cell area mask

Parameters
  • image (numpy.ndarray or PIL.Image.Image) – Input image to segment.

  • device (torch.device) – Computation device for running the model (e.g., CPU or CUDA).

  • model (torch.nn.Module) – Pretrained segmentation network returning per-class logits.

  • category_mapping (dict, optional) – Mapping from class index to display name. Default: {0: ‘dark’, 1: ‘busbar’, 2: ‘crack’, 3: ‘non-cell’}.

  • threshold (float, optional) – Probability cutoff for converting logits to binary masks. Default: 0.5.

Returns

dark, bb, crack, nocell – Binary masks for dark areas, busbars, cracks, and no-cell areas, respectively.

Return type

numpy.ndarray

pvcracks.utils.train_functions module

pvcracks.utils.train_functions.get_save_dir(base_dir, checkpoint_name) str[source]

Create and return the next sequential checkpoint directory path.

Parameters
  • base_dir (str or Path) – Base directory that contains the checkpoints subfolder.

  • checkpoint_name (str) – Prefix used when naming the new checkpoint directory.

Returns

Absolute path to the newly created checkpoint directory.

Return type

str

pvcracks.utils.train_functions.load_dataset(root, full_set: Literal[True]) img_functions.SolarDataset[source]
pvcracks.utils.train_functions.load_dataset(root, full_set: Literal[False] = False) tuple[img_functions.SolarDataset, img_functions.SolarDataset]

Instantiate dataset objects with a common preprocessing pipeline.

Parameters
  • root (str or Path) – Root directory containing image and annotation folders.

  • full_set (bool, optional) – When True, return the combined dataset; otherwise return the train/validation split. Defaults to False.

Returns

When full_set=True,

returns a single SolarDataset with all data. When full_set=False, returns a tuple of (train_dataset, val_dataset).

Return type

SolarDataset | tuple[SolarDataset, SolarDataset]

pvcracks.utils.train_functions.load_device_and_model(category_mapping, existing_weight_path=None) tuple[torch.device, torch.nn.modules.module.Module][source]

Select an execution device and construct a DataParallel UNet model.

Parameters
  • category_mapping (Mapping[str, int]) – Mapping from class names to indices.

  • existing_weight_path (str or Path, optional) – Checkpoint file to load into the model.

Returns

Active device and initialized model module.

Return type

tuple[torch.device, torch.nn.Module]

pvcracks.utils.unet_model module

class pvcracks.utils.unet_model.Block(src_channels, dst_channels)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class pvcracks.utils.unet_model.Bottleneck(in_channels, out_channels)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class pvcracks.utils.unet_model.ConvBNAct(in_channels, out_channels)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(inputs)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class pvcracks.utils.unet_model.UNet(encoder_blocks, encoder_channels, n_cls)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(x)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

class pvcracks.utils.unet_model.UNetUp(down_channels, right_channels)[source]

Bases: Module

Initialize internal Module state, shared by both nn.Module and ScriptModule.

forward(left, bottom)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

pvcracks.utils.unet_model.construct_unet(n_cls, pretrain=False)[source]

pvcracks.utils.viz_functions module

pvcracks.utils.viz_functions.channeled_inference_and_show(data_loader, device, model, category_mapping, idx, threshold=0.5, custom_title='Model Prediction', save_path=None, rows=3, model_two=None, label_prefix_one='', label_prefix_two='', secondary_mapping=None, secondary_mapping_index=1)[source]

Run inference on a single dataset element and visualize predictions per class.

Parameters
  • data_loader (torch.utils.data.DataLoader) – Loader that provides dataset access.

  • device (torch.device) – Computation device for running the model.

  • model (torch.nn.Module) – Trained segmentation network returning channel logits.

  • category_mapping (Mapping[int, str]) – Mapping from class index to display name.

  • idx (int) – Index of the sample to visualize inside the dataset.

  • threshold (float, optional) – Probability cutoff for converting logits to masks.