CNN_tools package#

CNN_tools.CNN_multi_class module#

class CNN_tools.CNN_multi_class.MyModel(*args, **kwargs)[source]#

Bases: Model

Subclass of the TensorFlow Keras Model class. This model is a Convolutional Neural Network (CNN) 2.5D designed for image classification tasks: it consists of two 2D convolutional layers followed by two fully connected layers, including batch normalization, activation functions, max pooling and dropout to improve its performance and generalization power.

conv1#

First convolutional layer with 3x3 kernel size and 8 filters.

Type:

tensorflow.python.keras.layers.Conv2D

bn1#

First batch normalization layer.

Type:

tensorflow.python.keras.layers.BatchNormalization

act1#

First activation function layer with a Rectified Linear Unit function.

Type:

tensorflow.python.keras.layers.Activation

pool1#

First max pooling layer with 2x2 pool size and stride 3.

Type:

tensorflow.python.keras.layers.MaxPooling2D

drop1#

First dropout layer with a 0.3 rate.

Type:

tensorflow.python.keras.layers.Dropout

conv2#

Second convolutional layer with 3x3 kernel size and 16 filters.

Type:

tensorflow.python.keras.layers.Conv2D

bn2#

Second batch normalization layer.

Type:

tensorflow.python.keras.layers.BatchNormalization

act2#

Second activation function layer with a Rectified Linear Unit function.

Type:

tensorflow.python.keras.layers.Activation

pool2#

Second max pooling layer with 3x3 pool size and stride 3.

Type:

tensorflow.python.keras.layers.MaxPooling2D

flatten#

Flatten layer.

Type:

tensorflow.python.keras.layers.Flatten

fc1#

First fully connected layer with 116 neurons and ReLU activation function.

Type:

tensorflow.python.keras.layers.Dense

fc2#

Second fully connected layer with a single neuron and sigmoid activation function for binary classification.

Type:

tensorflow.python.keras.layers.Dense

accuracy_loss_plot(history)[source]#

Generates two plots: one of model’s accuracy on the training and validation sets, the other of model’s loss on the same sets of data.

Parameters:

history (keras.callbacks.History) – History object generated during model training.

Return type:

None

call(inputs)[source]#

Implements the forward pass of the model, taking the input images and feeding them through each layer of the CNN. The order of execution is as specified in the constructor.

Parameters:

inputs (numpy.ndarray) – Input DTI images to the CNN.

Returns:

self.fc2 – A numpy array of shape (n_samples, 1) representing binary classification prediction for each input sample, where n_samples is the number of input images.

Return type:

numpy.ndarray

compile_and_fit(x_train, y_train, x_val, y_val, x_test, y_test, n_epochs, batchsize)[source]#

Compiles the model and fits it to the training data. Once fitted, the training process is shown by loss and accuracy plots (both for training and validation) and the overall performance is evaluated on the validation and test set.

Parameters:
  • x_train (numpy.ndarray) – Train subset of DTI images.

  • y_train (numpy.ndarray) – Labels corresponding to x_train.

  • x_val (ndarray) – Validation subset of DTI images.

  • y_val (numpy.ndarray) – Labels corresponding to x_val.

  • x_test (numpy.ndarray) – Test subset of DTI images.

  • y_test (numpy.ndarray) – Labels corresponding to x_test.

  • n_epochs (int) – Maximum number of epochs to train the models.

  • batchsize (int) – Batch size to use during training.

Return type:

None

load(path, x_train, y_train, x_val, y_val, x_test, y_test, n_epochs, batchsize)[source]#

Loads, compiles and re-trains a previously trained model stored in a H5 file.

Parameters:
  • path (str) – Path to the H5 file storing the pre-trained model.

  • x_train (numpy.ndarray) – Train subset of DTI images.

  • y_train (numpy.ndarray) – Labels corresponding to x_train.

  • x_val (ndarray) – Validation subset of DTI images.

  • y_val (numpy.ndarray) – Labels corresponding to x_val.

  • x_test (numpy.ndarray) – Test subset of DTI images.

  • y_test (numpy.ndarray) – Labels corresponding to x_test.

  • n_epochs (int) – Maximum number of epochs to train the models.

  • batchsize (int) – Batch size to use during training.

Return type:

None

test_roc(x_test, y_test)[source]#

Generates a plot of the ROC curve and calculates the AUC score for the model on the test set.

Parameters:
  • x_test (ndarray) – Test subset of DTI images.

  • y_test (numpy.ndarray) – Labels corresponding to x_test.

Return type:

None

validation_roc(x_val, y_val)[source]#

Generates a plot of the ROC curve and calculates the AUC score for the model on the validation set.

Parameters:
  • x_val (ndarray) – Validation subset of DTI images.

  • y_val (numpy.ndarray) – Labels corresponding to x_val.

Return type:

None

CNN_tools.CNN_multi_utilities module#

CNN_tools.CNN_multi_utilities.data_augmentation(images, labels)[source]#

Applies random rotations, zooms, crops and contrast enhancements to increase the size of the input dataset.

Parameters:
  • images (numpy.ndarray) – DTI images of the subjects.

  • labels (numpy.ndarray) – Corresponding label for each DTI image.

Returns:

  • augmented_images (numpy.ndarray) – Array containing the original DTI images and the augmented ones.

  • augmented_labels (numpy.ndarray) – Array containing the corresponding label for each image (original and augmented).

CNN_tools.CNN_multi_utilities.import_dataset(k_slice=45)[source]#

Extracts the paths for the FA, MD, and AD diffusion parameter maps and uses them to extract the corresponding NIFTI images, which are subsequently converted to a numpy array.

Parameters:

k_slice (int) – Selected slice. Default value 45 is the slice centered on the hippocampus.

Returns:

  • images (numpy.ndarray) – DTI images of the subjects. Each slice corresponds to a different parameter (FA, MD and AD in order).

  • labels (numpy.ndarray) – Corresponding labels for each DTI image. Value 1 indicates a subject with Alzheimer’s disease.

CNN_tools.CNN_multi_utilities.train_val_test_split(images, labels)[source]#

Splits arrays into random train, validation and test subsets.

Parameters:
  • images (numpy.ndarray) – DTI images of the subjects.

  • labels (numpy.ndarray) – Corresponding label for each DTI image.

Returns:

  • x_train (numpy.ndarray) – Train subset of DTI images.

  • y_train (numpy.ndarray) – Labels corresponding to x_train.

  • x_val (ndarray) – Validation subset of DTI images.

  • y_val (numpy.ndarray) – Labels corresponding to x_val.

  • x_test (numpy.ndarray) – Test subset of DTI images.

  • y_test (numpy.ndarray) – Labels corresponding to x_test.