niftynet.engine.sampler_grid_v2 module

Sampling image by a sliding window.

class GridSampler(reader, window_sizes, batch_size=1, spatial_window_size=None, window_border=None, queue_length=10, smaller_final_batch_mode='pad', name='grid_sampler')[source]

Bases: niftynet.engine.image_window_dataset.ImageWindowDataset

This class generators ND image samples with a sliding window.

layer_op()[source]

Generating each image as a window. Overriding this function to create new image sampling strategies.

This function should either yield or return a dictionary (of multiple windows per image):

return a dictionary:
{
 'image_name': a numpy array [n_samples, h, w, d, chn],
 'image_name_location': [n_samples, 7]
}

where the 7-element location vector encode the image_id, starting and ending coordinates of the image window.

Following the same notation, the dictionary can be extended to multiple modalities; the keys will be:

{'image_name_1', 'image_name_1_location',
 'image_name_2', 'image_name_2_location', ...}
Parameters:idx – image_id used to load the image at the i-th row of the input
Returns:a image data dictionary
grid_spatial_coordinates(subject_id, img_sizes, win_sizes, border_size)[source]

This function generates all coordinates of feasible windows, with step sizes specified in grid_size parameter.

The border size changes the sampling locations but not the corresponding window sizes of the coordinates.

Parameters:
  • subject_id – integer value indicates the position of of this image in image_reader.file_list
  • img_sizes – a dictionary of image shapes, {input_name: shape}
  • win_sizes – a dictionary of window shapes, {input_name: shape}
  • border_size – size of padding on both sides of each dim
Returns: