med_dataloader package

Submodules

med_dataloader.cli module

Console script for med_dataloader.

med_dataloader.med_dataloader module

Main module.

class med_dataloader.med_dataloader.DataLoader(mode, imgA_label=None, imgB_label=None, input_size=None, data_dir='./Data', output_dir=None, is_B_categorical=False, num_classes=None, norm_boundsA=None, norm_boundsB=None, extract_only=None, use_3D=False)[source]

Bases: object

[summary]

__init__(mode, imgA_label=None, imgB_label=None, input_size=None, data_dir='./Data', output_dir=None, is_B_categorical=False, num_classes=None, norm_boundsA=None, norm_boundsB=None, extract_only=None, use_3D=False)[source]

[summary]

Parameters
  • mode ([type]) – [description]

  • imgA_label (str) – Identifier for class A. It’s the name of the folder inside data_dir that contains images labeled as class A.

  • imgB_label (str) – Identifier for class B. It’s the name of the folder inside data_dir that contains images labeled as class B.

  • input_size (int) – Dimension of a single image, defined as input_size x input_size. Currently, it supports only squared images.

  • data_dir (str, optional) – Path to directory that contains the Dataset. This folder must contain two subfolders named like imgA_label and imgB_label. Defaults to ‘./Data’.

  • output_dir ([type], optional) – [description]. Defaults to None.

  • is_B_categorical (bool, optional) – [description]. Defaults to False.

  • num_classes ([type], optional) – [description]. Defaults to None.

  • norm_boundsA ([type], optional) – [description]. Defaults to None.

  • norm_boundsB ([type], optional) – [description]. Defaults to None.

  • extract_only (int, optional) – Indicate wheter to partially cache a certain amount of elements in the dataset. Please remember that if output_dir folder is already populated, you need to clean this folder content to recreate a partial cache file. When it is set to None, the entire Dataset is cached. Defaults to None.

  • use_3D – Indicate whether to use three-dimensional data in the cache (if True) or to extract two-dimensional slices from the 3D volumes (if False). Defaults to False.

Raises
  • ValueError – [description]

  • FileNotFoundError – [description]

  • ValueError – [description]

  • FileNotFoundError – [description]

  • FileNotFoundError – [description]

  • ValueError – [description]

  • ValueError – [description]

  • ValueError – [description]

  • FileNotFoundError – [description]

static check_type(path)[source]
fix_image_dims(img, size)[source]

Fix tensor dimensions so that they are of the proper size to carry out Tensorflow operations.

This function performs three steps:

  1. Squeeze to remove axis with dimension of 1

  2. Expand the dimensions of the tensor by adding one axis

  3. Resize and pad the tensor to a target width and height

If use_3D was enabled, volume is not resized and padded.

Parameters
  • img – image or volume to be processed

  • size – desired size of image or volume in the two/three axis.

get_dataset(batch_size=32, augmentation=False, random_crop_size=None, random_rotate=False, random_flip=False)[source]
get_imgs(img_paths, img_label, img_type, is_RGB, is_categorical=False, num_classes=None, norm_bounds=None)[source]

Open image files for one class and store it inside cache.

This function performs all the (usually) slow reading operations that is necessary to execute at least the first time. After the first execution information are saved inside some cache file inside Cache folder (typically created in your Dataset folder, at the same level of Images folder). This function detects if cache files are already present, and in that case it skips the definition of these files. Please take into account that cache files will be as big as your Dataset overall size. First execution may result in a considerably bigger amount of time.

Parameters

img_paths (str) – Path to single class images.

Returns

Tensorflow dataset object containing images of one

classes converted in Tensor format, without any other computations.

Return type

tf.Data.Dataset

get_imgs_paths()[source]

Get paths of every single image divided by classes.

Returns

two list containing the paths of every images for both

classes. The list is sorted alphabetically, this can be usefull when images are named with a progressive number inside a folder (e.g.: 001.xxx, 002.xxx, …, 999.xxx)

Return type

list, list

static is_3D_data(path)[source]
static is_RGB_data(path)[source]
static norm_with_bounds(image, bounds)[source]

Image normalisation. Normalises image in the range defined by lb and ub to fit[0, 1] range.

open_img(path)[source]

Open an image file and convert it to a tensor.

Parameters

path (tf.Tensor) – Tensor containing the path to the file to be opened.

Returns

Tensor containing the actual image content.

Return type

tf.Tensor

static random_crop(imgA, imgB, crop_size=256)[source]
static random_flip(imgA, imgB)[source]
static random_rotate(imgA, imgB)[source]
med_dataloader.med_dataloader.generate_dataset(data_dir, imgA_label, imgB_label, input_size, output_dir=None, extract_only=None, norm_boundsA=None, norm_boundsB=None, is_B_categorical=False, num_classes=None, use_3D=False)[source]
med_dataloader.med_dataloader.get_dataset(data_dir, percentages, batch_size, train_augmentation=True, random_crop_size=None, random_rotate=True, random_flip=True)[source]

Module contents

Top-level package for Medical Images Dataloader.