niftynet.contrib.csv_reader.sampler_balanced_v2_csv module

Generate image windows from a balanced sampling map as if every label had the same probability of occurrence.

Consider a mask with three classes I, J, K with prevalence 0.1, 0.1, and 0.8, respectively. If 100 samples are drawn from the balanced sampler, the classes should be approximately 33 I, 33 J, and 33 K.

This can also be considered a “balanced random cropping” layer of the input image.

class BalancedSamplerCSV(reader, csv_reader, window_sizes, batch_size=1, windows_per_image=1, queue_length=10, name='balanced_sampler')[source]

Bases: niftynet.engine.sampler_uniform_v2.UniformSampler

This class generators samples from a user provided frequency map for each input volume. The sampling likelihood of each voxel is proportional its intra class frequency. That is, if a given voxel is of class A and there are 20 voxels with class A, the probability of selecting this voxel is 5%. If there are 10 classes, the probability becomes 10% * 5% = 0.5%.

In general, the likelihood of sampling a voxel is given by:
p(v) = (1)/(# of unique labels * # of voxels with same class as v)

This is done for balanced sampling. In the case of unbalanced labels, this sampler should produce a roughly equal probability of sampling each class.

This layer can be considered as a “balanced random cropping” layer of the input image.

balanced_spatial_coordinates(n_samples, img_spatial_size, win_spatial_size, sampler_map)[source]

Perform balanced sampling.

Each label in the input tensor has an equal probability of being sampled.

Parameters:
  • n_samples – number of random coordinates to generate
  • img_spatial_size – input image size
  • win_spatial_size – input window size
  • sampler_map – sampling prior map, it’s spatial shape should be consistent with img_spatial_size
Returns:

(n_samples, N_SPATIAL) coordinates representing sampling window centres relative to img_spatial_size