Skip to content

Arch base

Architecture_Base ¤

Bases: ABC

An abstract base class for an Architecture class.

This class provides functionality for running the create_model function, which returns a tensorflow.keras model.

Create a custom Architecture
from aucmedi.neural_network.architectures import Architecture_Base
from tensorflow.keras import Input
from tensorflow.keras.layers import Conv2D, MaxPooling2D

class My_custom_Architecture(Architecture_Base):
    def __init__(self, classification_head, channels, input_shape=(224, 224),
                 pretrained_weights=False):
        self.classifier = classification_head
        self.input = input_shape + (channels,)
        self.pretrained_weights = pretrained_weights

    def create_model(self):
        # Initialize input layer
        model_input = Input(shape=self.input)

        # Add whatever architecture you want
        model_base = Conv2D(filters=32)(model_input)
        model_base = Conv2D(filters=64)(model_base)
        model_base = MaxPooling2D(pool_size=2)(model_base)

        # Add classification head via Classifier
        my_keras_model = self.classifier.build(model_input=model_input,
                                              model_output=model_base)
        # Return created model
        return my_keras_model
Required Functions
Function Description
__init__() Object creation function.
create_model() Creating and returning the architecture model.
Source code in aucmedi/neural_network/architectures/arch_base.py
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
class Architecture_Base(ABC):
    """ An abstract base class for an Architecture class.

    This class provides functionality for running the create_model function,
    which returns a [tensorflow.keras model](https://www.tensorflow.org/api_docs/python/tf/keras/Model).

    ???+ example "Create a custom Architecture"
        ```python
        from aucmedi.neural_network.architectures import Architecture_Base
        from tensorflow.keras import Input
        from tensorflow.keras.layers import Conv2D, MaxPooling2D

        class My_custom_Architecture(Architecture_Base):
            def __init__(self, classification_head, channels, input_shape=(224, 224),
                         pretrained_weights=False):
                self.classifier = classification_head
                self.input = input_shape + (channels,)
                self.pretrained_weights = pretrained_weights

            def create_model(self):
                # Initialize input layer
                model_input = Input(shape=self.input)

                # Add whatever architecture you want
                model_base = Conv2D(filters=32)(model_input)
                model_base = Conv2D(filters=64)(model_base)
                model_base = MaxPooling2D(pool_size=2)(model_base)

                # Add classification head via Classifier
                my_keras_model = self.classifier.build(model_input=model_input,
                                                      model_output=model_base)
                # Return created model
                return my_keras_model
        ```

    ???+ info "Required Functions"
        | Function            | Description                                    |
        | ------------------- | ---------------------------------------------- |
        | `__init__()`        | Object creation function.                      |
        | `create_model()`    | Creating and returning the architecture model. |
    """
    #---------------------------------------------#
    #                Initialization               #
    #---------------------------------------------#
    @abstractmethod
    def __init__(self, classification_head, channels, input_shape=(224, 224),
                 pretrained_weights=False):
        """ Functions which will be called during the Architecture object creation.

        This function can be used to pass variables and options in the Architecture instance.

        There are some mandatory required parameters for the initialization: The classification head as
        [Classifier][aucmedi.neural_network.architectures.classifier], the number of channels, and the
        input shape (x, y) for an image architecture or (x, y, z) for a volume architecture.

        Args:
            classification_head (Classifier):   Classifier object for building the classification head of the model.
            channels (int):                     Number of channels. For example: Grayscale->1 or RGB->3.
            input_shape (tuple):                Input shape of the image data for the first model layer (excluding channel axis).
            pretrained_weights (bool):          Option whether to utilize pretrained weights e.g. for ImageNet.
        """
        self.classifier = classification_head
        self.input = input_shape + (channels,)
        self.pretrained_weights = pretrained_weights

    #---------------------------------------------#
    #                Create Model                 #
    #---------------------------------------------#
    @abstractmethod
    def create_model(self):
        """ Create the deep learning or convolutional neural network model.

        This function will be called inside the AUCMEDI model class and have to return a functional
        Keras model. The model itself should be created here or in a subfunction called
        by this function.

        At the end of the model building process, the classification head must be appended
        via calling the `build()` function of a [Classifier][aucmedi.neural_network.architectures.classifier].

        Returns:
            model (tf.keras model):            A Keras model.
        """
        return None

__init__(classification_head, channels, input_shape=(224, 224), pretrained_weights=False) abstractmethod ¤

Functions which will be called during the Architecture object creation.

This function can be used to pass variables and options in the Architecture instance.

There are some mandatory required parameters for the initialization: The classification head as Classifier, the number of channels, and the input shape (x, y) for an image architecture or (x, y, z) for a volume architecture.

Parameters:

Name Type Description Default
classification_head Classifier

Classifier object for building the classification head of the model.

required
channels int

Number of channels. For example: Grayscale->1 or RGB->3.

required
input_shape tuple

Input shape of the image data for the first model layer (excluding channel axis).

(224, 224)
pretrained_weights bool

Option whether to utilize pretrained weights e.g. for ImageNet.

False
Source code in aucmedi/neural_network/architectures/arch_base.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
@abstractmethod
def __init__(self, classification_head, channels, input_shape=(224, 224),
             pretrained_weights=False):
    """ Functions which will be called during the Architecture object creation.

    This function can be used to pass variables and options in the Architecture instance.

    There are some mandatory required parameters for the initialization: The classification head as
    [Classifier][aucmedi.neural_network.architectures.classifier], the number of channels, and the
    input shape (x, y) for an image architecture or (x, y, z) for a volume architecture.

    Args:
        classification_head (Classifier):   Classifier object for building the classification head of the model.
        channels (int):                     Number of channels. For example: Grayscale->1 or RGB->3.
        input_shape (tuple):                Input shape of the image data for the first model layer (excluding channel axis).
        pretrained_weights (bool):          Option whether to utilize pretrained weights e.g. for ImageNet.
    """
    self.classifier = classification_head
    self.input = input_shape + (channels,)
    self.pretrained_weights = pretrained_weights

create_model() abstractmethod ¤

Create the deep learning or convolutional neural network model.

This function will be called inside the AUCMEDI model class and have to return a functional Keras model. The model itself should be created here or in a subfunction called by this function.

At the end of the model building process, the classification head must be appended via calling the build() function of a Classifier.

Returns:

Name Type Description
model tf.keras model

A Keras model.

Source code in aucmedi/neural_network/architectures/arch_base.py
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
@abstractmethod
def create_model(self):
    """ Create the deep learning or convolutional neural network model.

    This function will be called inside the AUCMEDI model class and have to return a functional
    Keras model. The model itself should be created here or in a subfunction called
    by this function.

    At the end of the model building process, the classification head must be appended
    via calling the `build()` function of a [Classifier][aucmedi.neural_network.architectures.classifier].

    Returns:
        model (tf.keras model):            A Keras model.
    """
    return None