Exemple #1
0
def define_ResiliNet_CNN_ResNet(
    input_shape=None,
    classes=10,
    block="basic",
    residual_unit="v2",
    repetitions=[2, 2, 2, 2],
    initial_filters=64,
    activation="softmax",
    include_top=True,
    input_tensor=None,
    dropout=None,
    transition_dilation_rate=(1, 1),
    initial_strides=(2, 2),
    initial_kernel_size=(7, 7),
    initial_pooling="max",
    final_pooling=None,
    top="classification",
    failout_survival_setting=[0.9, 0.9],
    skip_hyperconnection_config=default_skip_hyperconnection_config,
    reliability_setting=[1.0, 1.0],
    hyperconnection_weights_scheme=1,
    num_gpus=1,
):

    (
        hyperconnection_weight_IoTe,
        hyperconnection_weight_IoTf,
        hyperconnection_weight_ef,
        hyperconnection_weight_ec,
        hyperconnection_weight_fc,
    ) = set_hyperconnection_weights(hyperconnection_weights_scheme,
                                    reliability_setting,
                                    skip_hyperconnection_config)
    (
        multiply_hyperconnection_weight_layer_IoTe,
        multiply_hyperconnection_weight_layer_IoTf,
        multiply_hyperconnection_weight_layer_ef,
        multiply_hyperconnection_weight_layer_ec,
        multiply_hyperconnection_weight_layer_fc,
    ) = define_hyperconnection_weight_lambda_layers(
        hyperconnection_weight_IoTe,
        hyperconnection_weight_IoTf,
        hyperconnection_weight_ef,
        hyperconnection_weight_ec,
        hyperconnection_weight_fc,
    )

    input_shape, block_fn, residual_unit = init_model(input_shape, classes,
                                                      include_top, block,
                                                      residual_unit,
                                                      activation)
    img_input = layers.Input(shape=input_shape, tensor=input_tensor)

    # failout definitions
    edge_failout_lambda, fog_failout_lambda = cnn_failout_definitions(
        failout_survival_setting)

    # IoT Node
    iot_output, skip_iotfog = define_cnn_ResiliNet_architecture_IoT(
        img_input, initial_filters, initial_kernel_size, initial_strides)
    # edge
    edge_output, skip_edgecloud, filters = define_cnn_ResiliNet_architecture_edge(
        iot_output,
        repetitions[0],
        transition_dilation_rate,
        block_fn,
        initial_filters,
        dropout,
        residual_unit,
        initial_pooling,
        initial_strides,
        multiply_hyperconnection_weight_layer_IoTe,
        edge_failout_lambda=edge_failout_lambda,
    )

    # fog node
    fog_output, filters = define_cnn_ResiliNet_architecture_fog(
        skip_iotfog,
        edge_output,
        repetitions[1],
        transition_dilation_rate,
        block_fn,
        filters,
        dropout,
        residual_unit,
        multiply_hyperconnection_weight_layer_IoTf,
        multiply_hyperconnection_weight_layer_ef,
    )
    fog_output = fog_failout_lambda(fog_output)

    # cloud node
    cloud_output = define_cnn_ResiliNet_architecture_cloud(
        fog_output,
        skip_edgecloud,
        repetitions[2],
        repetitions[3],
        transition_dilation_rate,
        block_fn,
        filters,
        dropout,
        residual_unit,
        input_shape,
        classes,
        activation,
        include_top,
        top,
        final_pooling,
        multiply_hyperconnection_weight_layer_fc,
        multiply_hyperconnection_weight_layer_ec,
    )

    model, parallel_model = compile_keras_parallel_model(
        img_input, cloud_output, num_gpus)
    return model, parallel_model
Exemple #2
0
def define_DFG_CNN_MobileNet(
        input_shape=None,
        alpha=1.0,
        depth_multiplier=1,
        include_top=True,
        input_tensor=None,
        pooling=None,
        classes=1000,
        strides=(2, 2),
        skip_hyperconnection_config=default_skip_hyperconnection_config,  # binary representating if a skip hyperconnection is alive [e1,IoT]
        reliability_setting=[
            1.0, 1.0
        ],  # reliability of a node between 0 and 1 [f1,e1]
        hyperconnection_weights_scheme=1,
        num_gpus=1,
        weights=None,
        **kwargs):

    if weights == "imagenet":
        weights = None
        imagenet_related_functions(weights, input_shape, include_top, classes,
                                   depth_multiplier, alpha)

    (
        hyperconnection_weight_IoTe,
        hyperconnection_weight_IoTf,
        hyperconnection_weight_ef,
        hyperconnection_weight_ec,
        hyperconnection_weight_fc,
    ) = set_hyperconnection_weights(hyperconnection_weights_scheme,
                                    reliability_setting,
                                    skip_hyperconnection_config)
    (
        multiply_hyperconnection_weight_layer_IoTe,
        multiply_hyperconnection_weight_layer_IoTf,
        multiply_hyperconnection_weight_layer_ef,
        multiply_hyperconnection_weight_layer_ec,
        multiply_hyperconnection_weight_layer_fc,
    ) = define_hyperconnection_weight_lambda_layers(
        hyperconnection_weight_IoTe,
        hyperconnection_weight_IoTf,
        hyperconnection_weight_ef,
        hyperconnection_weight_ec,
        hyperconnection_weight_fc,
    )

    # Determine proper input shape and default size.
    img_input = layers.Input(shape=input_shape)

    # iot node
    iot_output, skip_iotfog = define_cnn_DFG_architecture_IoT(input_shape,
                                                              alpha,
                                                              img_input,
                                                              strides=strides)

    # edge node
    edge_output, skip_edgecloud = define_cnn_DFG_architecture_edge(
        iot_output,
        alpha,
        depth_multiplier,
        multiply_hyperconnection_weight_layer_IoTe,
        strides=strides,
    )

    # fog node
    fog_output = define_cnn_DFG_architecture_fog(
        skip_iotfog,
        edge_output,
        alpha,
        depth_multiplier,
        multiply_hyperconnection_weight_layer_IoTf,
        multiply_hyperconnection_weight_layer_ef,
        strides=strides,
    )

    # cloud node
    cloud_output = define_cnn_DFG_architecture_cloud(
        fog_output,
        skip_edgecloud,
        alpha,
        depth_multiplier,
        classes,
        include_top,
        pooling,
        multiply_hyperconnection_weight_layer_fc,
        multiply_hyperconnection_weight_layer_ec,
    )

    model, parallel_model = compile_keras_parallel_model(
        img_input, cloud_output, num_gpus)
    return model, parallel_model
def define_vanilla_CNN_MobileNet(input_shape=None,
                                 alpha=1.0,
                                 depth_multiplier=1,
                                 include_top=True,
                                 input_tensor=None,
                                 pooling=None,
                                 classes=1000,
                                 strides=(2, 2),
                                 num_gpus=1,
                                 dropout=1e-3,
                                 weights=None,
                                 **kwargs):
    """Instantiates the MobileNet architecture.

    # Arguments
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)`
            (with `channels_last` data format)
            or (3, 224, 224) (with `channels_first` data format).
            It should have exactly 3 inputs channels,
            and width and height should be no smaller than 32.
            E.g. `(200, 200, 3)` would be one valid value.
        alpha: controls the width of the network. This is known as the
            width multiplier in the MobileNet paper.
            - If `alpha` < 1.0, proportionally decreases the number
                of filters in each layer.
            - If `alpha` > 1.0, proportionally increases the number
                of filters in each layer.
            - If `alpha` = 1, default number of filters from the paper
                 are used at each layer.
        depth_multiplier: depth multiplier for depthwise convolution. This
            is called the resolution multiplier in the MobileNet paper.
        include_top: whether to include the fully-connected
            layer at the top of the network.
        input_tensor: optional Keras tensor (i.e. output of
            `layers.Input()`)
            to use as image input for the model.
        pooling: Optional pooling mode for feature extraction
            when `include_top` is `False`.
            - `None` means that the output of the model
                will be the 4D tensor output of the
                last convolutional block.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional block, and thus
                the output of the model will be a
                2D tensor.
            - `max` means that global max pooling will
                be applied.
        classes: optional number of classes to classify images
            into

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
        RuntimeError: If attempting to run this model with a
            backend that does not support separable convolutions.
    """
    if weights == "imagenet":
        weights = None
        imagenet_related_functions(weights, input_shape, include_top, classes,
                                   depth_multiplier, alpha)

    # Determine proper input shape and default size.
    img_input = layers.Input(shape=input_shape)
    # changed the strides from 2 to 1 since cifar-10 images are smaller
    # IoT Node
    iot = define_cnn_architecture_IoT(img_input, alpha, strides=strides)
    # edge
    edge = define_cnn_architecture_edge(iot,
                                        alpha,
                                        depth_multiplier,
                                        strides=strides)

    # fog node
    fog = layers.Lambda(lambda x: x * 1, name="node2_input")(edge)
    fog = define_cnn_architecture_fog(fog, alpha, depth_multiplier)

    # cloud node
    cloud = layers.Lambda(lambda x: x * 1, name="node1_input")(fog)
    cloud = define_cnn_architecture_cloud(cloud, alpha, depth_multiplier,
                                          classes, include_top, pooling,
                                          dropout)

    model, parallel_model = compile_keras_parallel_model(
        img_input, cloud, num_gpus)
    return model, parallel_model
Exemple #4
0
def define_ResiliNet_CNN_MobileNet(
    input_shape=None,
    alpha=1.0,
    depth_multiplier=1,
    include_top=True,
    pooling=None,
    classes=1000,
    strides=(2, 2),
    failout_survival_setting=[0.9, 0.9],
    skip_hyperconnection_config=default_skip_hyperconnection_config,
    reliability_setting=[1.0, 1.0],
    hyperconnection_weights_scheme=1,
    num_gpus=1,
    weights=None,
    **kwargs
):

    if weights == "imagenet":
        weights = None
        imagenet_related_functions(
            weights, input_shape, include_top, classes, depth_multiplier, alpha
        )

    (
        hyperconnection_weight_IoTe,
        hyperconnection_weight_IoTf,
        hyperconnection_weight_ef,
        hyperconnection_weight_ec,
        hyperconnection_weight_fc,
    ) = set_hyperconnection_weights(
        hyperconnection_weights_scheme, reliability_setting, skip_hyperconnection_config
    )
    (
        multiply_hyperconnection_weight_layer_IoTe,
        multiply_hyperconnection_weight_layer_IoTf,
        multiply_hyperconnection_weight_layer_ef,
        multiply_hyperconnection_weight_layer_ec,
        multiply_hyperconnection_weight_layer_fc,
    ) = define_hyperconnection_weight_lambda_layers(
        hyperconnection_weight_IoTe,
        hyperconnection_weight_IoTf,
        hyperconnection_weight_ef,
        hyperconnection_weight_ec,
        hyperconnection_weight_fc,
    )

    # Determine proper input shape and default size.
    img_input = layers.Input(shape=input_shape)

    # failout definitions
    edge_failout_lambda, fog_failout_lambda = cnn_failout_definitions(
        failout_survival_setting
    )

    # iot node
    iot_output, skip_iotfog = define_cnn_ResiliNet_architecture_IoT(
        input_shape, alpha, img_input, strides=strides
    )

    # edge node
    edge_output, skip_edgecloud = define_cnn_ResiliNet_architecture_edge(
        iot_output,
        alpha,
        depth_multiplier,
        multiply_hyperconnection_weight_layer_IoTe,
        strides=strides,
        edge_failout_lambda=edge_failout_lambda,
    )

    # fog node
    fog_output = define_cnn_ResiliNet_architecture_fog(
        skip_iotfog,
        edge_output,
        alpha,
        depth_multiplier,
        multiply_hyperconnection_weight_layer_IoTf,
        multiply_hyperconnection_weight_layer_ef,
        strides=strides,
    )
    fog_output = fog_failout_lambda(fog_output)

    # cloud node
    cloud_output = define_cnn_ResiliNet_architecture_cloud(
        fog_output,
        skip_edgecloud,
        alpha,
        depth_multiplier,
        classes,
        include_top,
        pooling,
        multiply_hyperconnection_weight_layer_fc,
        multiply_hyperconnection_weight_layer_ec,
    )

    model, parallel_model = compile_keras_parallel_model(
        img_input, cloud_output, num_gpus
    )
    return model, parallel_model
def define_vanilla_CNN_ResNet(
    input_shape=None,
    classes=10,
    block="basic",
    residual_unit="v2",
    repetitions=[2, 2, 2, 2],
    initial_filters=64,
    activation="softmax",
    include_top=True,
    input_tensor=None,
    dropout=None,
    transition_dilation_rate=(1, 1),
    initial_strides=(2, 2),
    initial_kernel_size=(7, 7),
    initial_pooling="max",
    final_pooling=None,
    top="classification",
    num_gpus=1,
):
    """Builds a custom ResNet18 architecture.
    
    Args:
        input_shape: optional shape tuple, only to be specified
            if `include_top` is False (otherwise the input shape
            has to be `(224, 224, 3)` (with `channels_last` dim ordering)
            or `(3, 224, 224)` (with `channels_first` dim ordering).
            It should have exactly 3 dimensions,
            and width and height should be no smaller than 8.
            E.g. `(224, 224, 3)` would be one valid value.
        classes: The number of outputs at final softmax layer
        block: The block function to use. This is either `'basic'` or `'bottleneck'`.
            The original paper used `basic` for layers < 50.
        repetitions: Number of repetitions of various block units.
            At each block unit, the number of filters are doubled and the input size
            is halved. 
        residual_unit: the basic residual unit, 'v1' for conv bn relu, 'v2' for bn relu
            conv. See [Identity Mappings in
            Deep Residual Networks](https://arxiv.org/abs/1603.05027)
            for details.
        dropout: None for no dropout, otherwise rate of dropout from 0 to 1.
            Based on [Wide Residual Networks.(https://arxiv.org/pdf/1605.07146) paper.
        transition_dilation_rate: Dilation rate for transition layers. For semantic
            segmentation of images use a dilation rate of (2, 2).
        initial_strides: Stride of the very first residual unit and MaxPooling2D call,
            with default (2, 2), set to (1, 1) for small images like cifar.
        initial_kernel_size: kernel size of the very first convolution, (7, 7) for
            imagenet and (3, 3) for small image datasets like tiny imagenet and cifar.
            See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
        initial_pooling: Determine if there will be an initial pooling layer,
            'max' for imagenet and None for small image datasets.
            See [ResNeXt](https://arxiv.org/abs/1611.05431) paper for details.
        final_pooling: Optional pooling mode for feature extraction at the final
            model layer when `include_top` is `False`.
            - `None` means that the output of the model
                will be the 4D tensor output of the
                last convolutional layer.
            - `avg` means that global average pooling
                will be applied to the output of the
                last convolutional layer, and thus
                the output of the model will be a
                2D tensor.
            - `max` means that global max pooling will
                be applied.
        top: Defines final layers to evaluate based on a specific problem type. Options
            are 'classification' for ImageNet style problems, 'segmentation' for
            problems like the Pascal VOC dataset, and None to exclude these layers
            entirely.
    Returns:
        The keras `Model`.
    """

    input_shape, block_fn, residual_unit = init_model(input_shape, classes,
                                                      include_top, block,
                                                      residual_unit,
                                                      activation)
    img_input = layers.Input(shape=input_shape, tensor=input_tensor)

    # IoT Node
    iot = define_cnn_architecture_IoT(img_input, initial_filters,
                                      initial_kernel_size, initial_strides)
    # edge
    edge, filters = define_cnn_architecture_edge(
        iot,
        repetitions[0],
        transition_dilation_rate,
        block_fn,
        initial_filters,
        dropout,
        residual_unit,
        initial_pooling,
        initial_strides,
    )

    # fog node
    fog = layers.Lambda(lambda x: x * 1, name="node2_input")(edge)
    fog, filters = define_cnn_architecture_fog(
        fog,
        repetitions[1],
        transition_dilation_rate,
        block_fn,
        filters,
        dropout,
        residual_unit,
    )

    # cloud node
    cloud = layers.Lambda(lambda x: x * 1, name="node1_input")(fog)
    cloud = define_cnn_architecture_cloud(
        cloud,
        repetitions[2],
        repetitions[3],
        transition_dilation_rate,
        block_fn,
        filters,
        dropout,
        residual_unit,
        input_shape,
        classes,
        activation,
        include_top,
        top,
        final_pooling,
    )

    model, parallel_model = compile_keras_parallel_model(
        img_input, cloud, num_gpus)
    return model, parallel_model