Exemplo n.º 1
0
    def call(self, inputs, **kwargs):
        states_encoder = inputs[0]
        states_decoder = inputs[1]

        # (0) adjust the size of encoder state to the size of decoder state
        if isinstance(self.n_state, int):
            # Key Vector와 Value Vector을 다르게 둚
            key_vector = self.key_dense(states_encoder)
            val_vector = self.val_dense(states_encoder)
        else:
            key_vector = states_encoder
            val_vector = states_encoder

        # (1) Calculate Score
        expanded_states_encoder = key_vector[:, None, ...]
        # >>> (batch size, 1, length of encoder sequence, num hidden)
        expanded_states_decoder = states_decoder[..., None, :]
        # >>> (batch size, length of decoder sequence, 1, num hidden)
        score = K.sum(expanded_states_encoder * expanded_states_decoder,
                      axis=-1)
        # >>> (batch size, length of decoder input, length of encoder input)
        # (2) Normalize score
        attention = Softmax(axis=-1, name='attention')(score)

        # (3) Calculate Context Vector
        expanded_val_vector = val_vector[:, None, ...]
        context = K.sum(expanded_val_vector * attention[..., None], axis=2)
        # >>> (batch size, length of decoder input, num hidden)
        return context, attention
Exemplo n.º 2
0
def attach_multibox_head(network, source_layer_names,
                         num_priors=4, num_classes=10, activation='softmax'):
    heads = []
    for idx, layer_name in enumerate(source_layer_names):
        source_layer = network.get_layer(layer_name).output

        # Classification
        clf = Conv2D(num_priors * num_classes, (3, 3),
                     padding='same', name=f'clf_head{idx}_logit')(source_layer)
        clf = Reshape((-1, num_classes),
                      name=f'clf_head{idx}_reshape')(clf)
        if activation == 'softmax':
            clf = Softmax(axis=-1, name=f'clf_head{idx}')(clf)
        elif activation == 'sigmoid':
            clf = sigmoid(clf)
        else:
            raise ValueError('activation은 {softmax,sigmoid}중에서 되어야 합니다.')

        # Localization
        loc = Conv2D(num_priors * 4, (3,3), padding='same',
                     name=f'loc_head{idx}')(source_layer)
        loc = Reshape((-1,4),
                      name=f'loc_head{idx}_reshape')(loc)
        head = Concatenate(axis=-1, name=f'head{idx}')([clf, loc])
        heads.append(head)

    if len(heads) > 1:
        predictions = Concatenate(axis=1, name='predictions')(heads)
    else:
        predictions = K.identity(heads[0],name='predictions')
    return predictions
def create_model(input_size, filters, c=10 ** -4):
    l2 = regularizers.l2(c)
    input = Input(shape=(FOUR, FOUR, FOUR, input_size))

    conv_1 = normalized_relu(line_convolution(input, filters, l2))
    conv_2 = normalized_relu(line_convolution(conv_1, filters, l2))
    conv_3 = normalized_relu(line_convolution(conv_2, filters, l2))
    last_conv = Conv3D(filters, 1, kernel_regularizer=l2)(conv_3)

    collapse_play = normalized_relu(Conv3D(filters // 2, (1, 1, 4), kernel_regularizer=l2)(last_conv))
    squash_play = normalized_relu(Conv3D(3, 1, kernel_regularizer=l2)(collapse_play))
    flatten_play = Flatten()(squash_play)
    dense_play = Dense(16, activation='relu', kernel_regularizer=l2)(flatten_play)
    sigmoid_play = Dense(16, activation='sigmoid', kernel_regularizer=l2)(dense_play)
    output_play = Softmax()(sigmoid_play)

    collapse_win = normalized_relu(Conv3D(3, (1, 1, 4), kernel_regularizer=l2)(last_conv))
    flatten_win = Flatten()(collapse_win)
    dense_win = Dense(16, activation='relu', kernel_regularizer=l2)(flatten_win)
    output_win = Dense(1, activation='tanh', kernel_regularizer=l2)(dense_win)

    model = Model(inputs=input, outputs=[output_play, output_win])
    optimizer = Adam()
    metics = {'softmax': 'categorical_accuracy', 'dense_3': 'mae'}
    model.compile(optimizer, ['categorical_crossentropy', 'mse'], metrics=metics)
    return model
Exemplo n.º 4
0
    def pnet(self, input_shape=None):

        if input_shape is None:
            input_shape = (None, None, 3)

        net = Input(input_shape)

        pnet_layer = Conv2D(10, (3, 3), strides=(1, 1), padding='valid')(net)
        pnet_layer = PReLU(shared_axes=[1, 2])(pnet_layer)
        pnet_layer = MaxPooling2D((2, 2), strides=(2, 2), padding='same')(pnet_layer)

        pnet_layer = Conv2D(16, (3, 3), strides=(1, 1), padding='valid')(pnet_layer)
        pnet_layer = PReLU(shared_axes=[1, 2])(pnet_layer)

        pnet_layer = Conv2D(32, (3, 3), strides=(1, 1), padding='valid')(pnet_layer)
        pnet_layer = PReLU(shared_axes=[1, 2])(pnet_layer)

        pnet_out1 = Conv2D(2, (1, 1), strides=(1, 1))(pnet_layer)
        pnet_out1 = Softmax(axis=3)(pnet_out1)

        pnet_out2 = Conv2D(4, (1, 1), strides=(1, 1))(pnet_layer)

        p_net = Model(net, [pnet_out2, pnet_out1])

        return p_net
Exemplo n.º 5
0
    def rnet(self, input_shape=None):

        if input_shape is None:
            input_shape = (24, 24, 3)

        net = Input(input_shape)

        rnet_layer = Conv2D(28, (3, 3), strides=(1, 1), padding='valid')(net)
        rnet_layer = PReLU(shared_axes=[1, 2])(rnet_layer)
        rnet_layer = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(rnet_layer)

        rnet_layer = Conv2D(48, (3, 3), strides=(1, 1), padding='valid')(rnet_layer)
        rnet_layer = PReLU(shared_axes=[1, 2])(rnet_layer)
        rnet_layer = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(rnet_layer)

        rnet_layer = Conv2D(64, (2, 2), strides=(1, 1), padding='valid')(rnet_layer)
        rnet_layer = PReLU(shared_axes=[1, 2])(rnet_layer)
        rnet_layer = Flatten()(rnet_layer)

        rnet_layer = Dense(128)(rnet_layer)
        rnet_layer = PReLU()(rnet_layer)

        rnet_out1 = Dense(2)(rnet_layer)
        rnet_out1 = Softmax(axis=1)(rnet_out1)

        rnet_out2 = Dense(4)(rnet_layer)

        rnet = Model(net, [rnet_out2, rnet_out1])

        return rnet
Exemplo n.º 6
0
    def onet(self, input_shape=None):

        if input_shape is None:
            input_shape = (48, 48, 3)

        net = Input(input_shape)

        onet_layer = Conv2D(32, (3, 3), strides=(1, 1), padding='valid')(net)
        onet_layer = PReLU(shared_axes=[1, 2])(onet_layer)
        onet_layer = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(onet_layer)

        onet_layer = Conv2D(64, (3, 3), strides=(1, 1), padding='valid')(onet_layer)
        onet_layer = PReLU(shared_axes=[1, 2])(onet_layer)
        onet_layer = MaxPooling2D((3, 3), strides=(2, 2), padding='valid')(onet_layer)

        onet_layer = Conv2D(64, (3, 3), strides=(1, 1), padding='valid')(onet_layer)
        onet_layer = PReLU(shared_axes=[1, 2])(onet_layer)
        onet_layer = MaxPooling2D((2, 2), strides=(2, 2), padding='same')(onet_layer)

        onet_layer = Conv2D(128, (2, 2), strides=(1, 1), padding='valid')(onet_layer)
        onet_layer = PReLU(shared_axes=[1, 2])(onet_layer)

        onet_layer = Flatten()(onet_layer)
        onet_layer = Dense(256)(onet_layer)
        onet_layer = PReLU()(onet_layer)

        onet_out1 = Dense(2)(onet_layer)
        onet_out1 = Softmax(axis=1)(onet_out1)

        onet_out2 = Dense(4)(onet_layer)
        onet_out3 = Dense(10)(onet_layer)

        onet = Model(net, [onet_out2, onet_out3, onet_out1])

        return onet
Exemplo n.º 7
0
    def _build_model(self):

        sequence_source_id = Input([900], name='source_id', dtype=tf.int32)
        sequence_target_id = Input([200], name='target_id', dtype=tf.int32)
        sequence_target_mask = Input([200], name='target_mask', dtype=tf.int32)

        self.encoder = Encoder(self.vocab_size, self.emb_dim,
                               self.dropout_keep, self.encode_dim,
                               self.n_agents, self.encoder_layers_num,
                               self.batch_size)
        self.decode = Decoder(self.mode, self.attention_units, self.encode_dim,
                              self.decode_len, self.vocab_size, self.emb_dim,
                              self.batch_size, self.word2id)
        self.softmax = Softmax(axis=-1)

        encoder_outputs = self.encoder(sequence_source_id)
        # encoder_outputs = Concatenate(encoder_outputs, axis=1)
        decoder_output = self.decode(sequence_target_id, encoder_outputs)
        print('##########', encoder_outputs)
        print('##########', decoder_output.shape)
        # decoder_output = Lambda(lambda x:tf.unstack(x))(decoder_output)
        vocab_dists = self.softmax(decoder_output)

        self.loss = Seq2SeqLoss(sequence_target_mask, self.batch_size)

        model = Model([sequence_source_id, sequence_target_id], vocab_dists)
        loss = losses(self.decode_len, sequence_target_mask, self.batch_size,
                      vocab_dists, sequence_target_id)
        model.add_loss(loss)
        model.compile(Adam(self.learning_rate))

        return model
    def _predict_masks(self, bert_text: str) -> ([str], float):
        '''
        Predict masked tokens in bert_text
        :param bert_text: input sequence for bert
        :return: Top k prediction for input sequence
        '''

        tokenized_text = tokenizer.tokenize('[CLS] ' + bert_text + '. [SEP]')
        input_ids = tokenizer.convert_tokens_to_ids(tokenized_text)
        outputs = MODEL(tf.convert_to_tensor([input_ids]))
        mask_idx = tokenized_text.index(MASK)
        top_k_output = tf.math.top_k(outputs.logits[0][mask_idx],
                                     k=50,
                                     sorted=True,
                                     name=None)
        probabilities = Softmax()(top_k_output.values.numpy()).numpy()

        top_k_pred = [([word], prob) for word, prob in zip(
            tokenizer.convert_ids_to_tokens(top_k_output.indices),
            probabilities)]

        if tokenized_text.count(MASK) > 1:
            for top_k in top_k_pred.copy():
                top_k_pred.remove(top_k)

                pred = top_k[0]
                pred_for_last_mask = pred[-1]
                results_of_next_pred = self._predict_masks(
                    bert_text.replace(MASK, pred_for_last_mask, 1))
                for pred_for_next_mask, prob in results_of_next_pred:
                    top_k_pred.append((pred + pred_for_next_mask, prob))

            return top_k_pred

        return top_k_pred
Exemplo n.º 9
0
def make_cnn_model(
    input_shape: Tuple[int, int],
    conv_blocks: Sequence[Sequence[int]],
    dense_size: int,
    output_size: int,
    dropout: float,
) -> Sequential:
    model = Sequential()
    model.add(Conv2D(conv_blocks[0][0], (3, 3), activation="relu", input_shape=(*input_shape, 3)))

    is_first = True
    for conv_sizes in conv_blocks:
        for size in conv_sizes:
            if is_first:
                is_first = False
                continue
            model.add(Conv2D(size, (3, 3), activation="relu"))
        model.add(MaxPooling2D())

    model.add(Flatten())
    model.add(Dense(dense_size))
    model.add(Dropout(dropout))
    model.add(Dense(output_size))
    model.add(Softmax())
    return model
Exemplo n.º 10
0
def _get_3_classes_model(inputs):
    """Build Unet architecture that return a 3-classes prediction.

    Parameters
    ----------
    inputs : tensorflow.keras.Input object
        Input layer with shape (B, H, W, 1) or (H, W, 1).

    Returns
    -------
    output : tensorflow.keras.layers object
        Output layer (softmax) with shape (B, H, W, 3) or (H, W, 3).

    """
    # compute feature map
    features_core = EncoderDecoder(name="encoder_decoder")(
        inputs)  # (B, H, W, 32)

    # compute 3-classes output
    features_3_classes = SameConv(filters=3,
                                  kernel_size=(1, 1),
                                  activation="linear",
                                  name="final_conv")(
                                      features_core)  # (B, H, W, 3)
    output = Softmax(axis=-1,
                     name="label_3")(features_3_classes)  # (B, H, W, 3)

    return output
Exemplo n.º 11
0
 def build_model(self, input_shape: Tuple[int, int, int]) -> Model:
     x = Input(shape=input_shape, name="input")
     sub_sampling_out, sub_sampling_stack = self.__build_sub_sampling_stack(
         x=x)
     filter_extraction_conv = Conv2D(filters=1024,
                                     kernel_size=(3, 3),
                                     padding="same",
                                     name="filter_extraction_conv",
                                     activation="relu")(sub_sampling_out)
     filter_extraction_conv_bn = BatchNormalization(
         name=f"filter_extraction_conv_bn")(filter_extraction_conv)
     up_sampling_input = Conv2D(
         filters=512,
         kernel_size=(3, 3),
         padding="same",
         name="up_sampling_input",
         activation="relu")(filter_extraction_conv_bn)
     up_sampling_output = self.__build_up_sampling_stack(
         x=up_sampling_input, sub_sampling_stack=sub_sampling_stack)
     out = Conv2D(filters=self._num_classes,
                  kernel_size=(1, 1),
                  padding="same",
                  name="output")(up_sampling_output)
     flatten_out = Softmax(name="output_soft_max")(out)
     return Model(x, flatten_out)
Exemplo n.º 12
0
def semantic_prediction(semantic_names,
                        semantic_features,
                        target_level=0,
                        input_target=None,
                        n_filters=64,
                        n_dense=64,
                        ndim=2,
                        n_classes=3,
                        semantic_id=0):
    """Creates the prediction head from a list of semantic features

    Args:
        semantic_names (list): A list of the names of the semantic feature layers
        semantic_features (list): A list of semantic features
            NOTE: The semantic_names and semantic features should be in decreasing order
            e.g. [Q6, Q5, Q4, ...]
        target_level (int, optional): Defaults to 0. The level we need to reach.
            Performs 2x upsampling until we're at the target level
        input_target (tensor, optional): Defaults to None. Tensor with the input image.
        n_dense (int, optional): Defaults to 256. The number of filters for dense layers.
        n_classes (int, optional): Defaults to 3.  The number of classes to be predicted.
        semantic_id (int): Defaults to 0. An number to name the final layer. Allows for multiple
            semantic heads.
    Returns:
        tensor: The softmax prediction for the semantic segmentation head

    Raises:
        ValueError: ndim is not 2 or 3
    """

    acceptable_ndims = [2, 3]
    if ndim not in acceptable_ndims:
        raise ValueError('Only 2 and 3 dimensional networks are supported')

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = -1

    # Add all the semantic layers
    semantic_sum = semantic_features[0]
    for semantic_feature in semantic_features[1:]:
        semantic_sum = Add()([semantic_sum, semantic_feature])

    # Final upsampling
    min_level = int(re.findall(r'\d+', semantic_names[-1])[0])
    n_upsample = min_level - target_level
    x = semantic_upsample(semantic_sum, n_upsample, target=input_target)

    # First tensor product
    x = TensorProduct(n_dense)(x)
    x = BatchNormalization(axis=channel_axis)(x)
    x = Activation('relu')(x)

    # Apply tensor product and softmax layer
    x = TensorProduct(n_classes)(x)
    x = Softmax(axis=channel_axis, name='semantic_' + str(semantic_id))(x)

    return x
Exemplo n.º 13
0
def caltech_pyramid_ensemble(input_shape=None, input_tensor=None, n_classes=None,
                             weights_path: Union[None, str] = None) -> Model:
    """
    Defines a caltech network.

    :param n_classes: used in order to be compatible with the main script.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained custom network's weights.
    :return: Keras functional API Model.
    """
    output_list = []
    inputs = create_inputs(input_shape, input_tensor)

    # Submodel Strong.
    submodel_strong = caltech_pyramid_ensemble_submodel_strong(input_shape, input_tensor, n_classes, weights_path)
    outputs_submodel_strong = submodel_strong.output

    # Submodel Weak 1.
    submodel_weak1 = caltech_pyramid_ensemble_submodel_weak1(input_shape, input_tensor, n_classes, weights_path)
    outputs_submodel_weak1 = Dense(61, name='outputs_submodel_weak1')(submodel_weak1.layers[-1])

    # Average the predictions for the first half classes.
    averaged_first_half_classes = Average(name='averaged_first_half_classes')(
        [
            Crop(1, 0, 61)(outputs_submodel_strong),
            outputs_submodel_weak1
        ]
    )

    output_list.append(averaged_first_half_classes)

    # Submodel Weak 2.
    # Block1.
    submodel_weak2 = caltech_pyramid_ensemble_submodel_weak2(input_shape, input_tensor, n_classes, weights_path)
    outputs_submodel_weak2 = Dense(61, name='outputs_submodel_weak2')(submodel_weak2.layers[-1])

    # Average the predictions for the last half classes.
    averaged_last_half_classes = Average(name='averaged_last_half_classes')(
        [
            Crop(1, 61, 102)(outputs_submodel_strong),
            outputs_submodel_weak2
        ]
    )

    output_list.append(averaged_last_half_classes)

    # Concatenate all class predictions together.
    outputs = Concatenate(name='output')(output_list)
    outputs = Softmax(name='output_softmax')(outputs)

    # Create model.
    model = Model(inputs, outputs, name='caltech_pyramid_ensemble')
    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
Exemplo n.º 14
0
	def create_classifier_layers(self,add_dropout=True):
		self.model.add(Flatten())
		self.model.add(Dense(units=100))
		self.model.add(self.get_activation())
		if add_dropout:
			self.model.add(Dropout(0.3))

		self.model.add(Dense(units=CLASS))
		self.model.add(Softmax())
		return
Exemplo n.º 15
0
def attach_multibox_head(network,
                         source_layer_names,
                         num_priors=4,
                         num_classes=11,
                         activation='softmax'):  # 10->11
    heads = []
    batch_size = 64  # OpenCV에서 인식을못해서 하드코딩한다 reshape를 인식못한다
    for idx, layer_name in enumerate(source_layer_names):
        source_layer = network.get_layer(layer_name).output

        # OpenCV Loading Error
        # "Can't create layer \"loc_head2_reshape_2/Shape\" of type \"Shape\""
        # 조치 : class개수를 10에서 11로 바꿔줌

        w = source_layer.get_shape().as_list()[1]
        h = source_layer.get_shape().as_list()[2]
        print("w : ", w)
        print("h : ", h)
        print("num_priors : ", num_priors)
        # Classification
        clf = Conv2D(num_priors * num_classes, (3, 3),
                     padding='same',
                     name=f'clf_head{idx}_logit')(source_layer)
        print("clf shape입니다 : ", clf.shape)
        clf = tf.reshape(clf,
                         shape=(batch_size, w * h * num_priors, num_classes),
                         name=f'clf_head{idx}_reshape')
        # clf = Reshape((w*h*num_priors, num_classes), name=f'clf_head{idx}_reshape')(clf)  # (-1, num_classes) # w*h*num_priors
        print("clf의 reshape 후입니다 : ", clf.shape)
        if activation == 'softmax':
            clf = Softmax(axis=-1, name=f'clf_head{idx}')(clf)
        elif activation == 'sigmoid':
            clf = sigmoid(clf)
        else:
            raise ValueError('activation은 {softmax,sigmoid}중에서 되어야 합니다.')

        # Localization
        loc = Conv2D(num_priors * 4, (3, 3),
                     padding='same',
                     name=f'loc_head{idx}')(source_layer)
        print("loc의 shape입니다 : ", loc.shape)
        loc = tf.reshape(loc,
                         shape=(batch_size, w * h * num_priors, 4),
                         name='loc_head{}_reshape'.format(idx))
        # loc = Reshape((w*h*num_priors, 4), name=f'loc_head{idx}_reshape')(loc)  #Reshape((-1, 4),
        print("loc의 reshape 후입니다 : ", loc.shape)
        head = Concatenate(axis=-1, name=f'head{idx}')([clf, loc])
        heads.append(head)

    if len(heads) > 1:
        predictions = Concatenate(axis=1, name='predictions')(heads)
    else:
        predictions = K.identity(heads[0], name='predictions')
    return predictions
Exemplo n.º 16
0
 def __init__(self, w3, dropout_rate):
     # w3 are the window sizes of the convolutions, hyperparameters
     super().__init__()
     self.logger = logging.getLogger(__name__)
     self.conv1 = TimeDistributed(
         Conv1D(1,
                w3,
                activation=None,
                padding='causal',
                name='atn_weight_conv1'))
     self.dropout = Dropout(dropout_rate)
     self.softmax = TimeDistributed(Softmax(name='atn_weight_softmax'))
Exemplo n.º 17
0
def omniglot_pyramid_ensemble(input_shape=None,
                              input_tensor=None,
                              n_classes=None,
                              weights_path: Union[None, str] = None) -> Model:
    """
    Defines a omniglot network.

    :param n_classes: used in order to be compatible with the main script.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained custom network's weights.
    :return: Keras functional API Model.
    """
    inputs = create_inputs(input_shape, input_tensor)

    # Generate Submodels.
    submodel_strong = omniglot_pyramid_ensemble_submodel_strong(
        input_shape, input_tensor, n_classes, weights_path)
    submodel_weak1 = omniglot_pyramid_ensemble_submodel_weak1(
        input_shape, input_tensor, 3, weights_path)
    submodel_weak2 = omniglot_pyramid_ensemble_submodel_weak2(
        input_shape, input_tensor, 3, weights_path)
    # Get their outputs.
    outputs_submodel_strong = submodel_strong.output
    outputs_submodel_weak1 = submodel_weak1.output
    outputs_submodel_weak2 = submodel_weak2.output

    # Average classes.
    first_classes = Average(name='averaged_first_classes')([
        Crop(1, 0, 812,
             name='first_classes_submodel_strong')(outputs_submodel_strong),
        Crop(1, 0, 812,
             name='first_classes_submodel_weak1')(outputs_submodel_weak1)
    ])

    last_classes = Average(name='averaged_last_classes')([
        Crop(1, 812, 1623,
             name='last_classes_submodel_strong')(outputs_submodel_strong),
        Crop(1, 0, 811,
             name='last_classes_submodel_weak2')(outputs_submodel_weak2)
    ])

    # Concatenate all class predictions together.
    outputs = Concatenate(name='output')([first_classes, last_classes])
    outputs = Softmax(name='output_softmax')(outputs)

    # Create model.
    model = Model(inputs, outputs, name='omniglot_pyramid_ensemble')
    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
Exemplo n.º 18
0
def heatmap_loss(target_mask, heatmap_3d):
    X = 2
    Y = 3
    Z = 4

    dims = heatmap_3d.shape  # batches, joints, x, y, z
    batch_size = dims[0]
    nb_joints = dims[1]

    heatmap_ = tf.reshape(heatmap_3d, [batch_size, nb_joints, -1])
    heatmap_ = Softmax(axis=2)(heatmap_)
    target_mask = tf.reshape(target_mask, [batch_size, nb_joints, -1])

    return binary_crossentropy(target_mask, heatmap_)
Exemplo n.º 19
0
def make_transfer_learning_model(
    input_shape: Tuple[int, int], n_classes: int, model_factory: Callable[..., Model], dropout: float, dense_size: int,
) -> Sequential:
    input_shape = (*input_shape, 3)
    base_model: Model = model_factory(weights="imagenet", input_shape=input_shape, include_top=False)

    return Sequential(
        [
            InputLayer(input_shape),
            base_model,
            Flatten(),
            Dense(dense_size, activation="relu"),
            Dropout(dropout),
            Dense(n_classes),
            Softmax(),
        ]
    )
    def __init__(self):
        super(Model, self).__init__()

        self.gru_1 = GRU(hidden_state,
                         return_sequences=True,
                         input_shape=(testing_samples_per_video,
                                      feature_field),
                         dropout=.5)  # recurrent layer
        # self.gru_2 = GRU(hidden_state, return_sequences=True)

        self.attention_layer = Dense(1)  # gets attention weight for time step
        self.attention_normalizer = Softmax(
            axis=1
        )  # normalizes the 3d tensor to give weight for each time step

        self.FC_1 = Dense(hidden_state // 2, activation='selu')
        # recurrent_fusion_model.add(BatchNormalization())
        # self.FC_2 = Dense(hidden_state // 4, activation='selu')
        # self.BN_1 = BatchNormalization()
        self.classification_layer = Dense(num_actions, activation='softmax')
Exemplo n.º 21
0
    def __init__(self, n_classes):
        """
        Last layer of the model outputting the probabilities
        for each class.
        Performs a SeparableConv2D 1x1, BN, GlobalAveragePooling2D,
        FC, Dropout, Softmax.

        Arguments:
            n_classes: output size.
        """
        super(Classifier, self).__init__(name='classifier')

        self.conv = SeparableConv2D(filters=1280,
                                    kernel_size=(1, 1),
                                    kernel_regularizer=l2(WEIGHT_DECAY),
                                    activation='relu')
        self.bn = BatchNormalization()
        self.avg_pool = GlobalAveragePooling2D()
        self.fc = Dense(units=n_classes)
        self.droput = Dropout(0.2)
        self.softmax = Softmax()
Exemplo n.º 22
0
def self_attn_block(inp, n_c, squeeze_factor=8):
    """ GAN Self Attention Block
    Code borrows from https://github.com/taki0112/Self-Attention-GAN-Tensorflow
    """
    msg = "Input channels must be >= {}, recieved nc={}".format(
        squeeze_factor, n_c)
    assert n_c // squeeze_factor > 0, msg
    var_x = inp
    shape_x = var_x.get_shape().as_list()

    var_f = Conv2D(
        n_c // squeeze_factor,
        1,
        kernel_regularizer=regularizers.l2(GAN22_REGULARIZER))(var_x)
    var_g = Conv2D(
        n_c // squeeze_factor,
        1,
        kernel_regularizer=regularizers.l2(GAN22_REGULARIZER))(var_x)
    var_h = Conv2D(
        n_c, 1, kernel_regularizer=regularizers.l2(GAN22_REGULARIZER))(var_x)

    shape_f = var_f.get_shape().as_list()
    shape_g = var_g.get_shape().as_list()
    shape_h = var_h.get_shape().as_list()
    flat_f = Reshape((-1, shape_f[-1]))(var_f)
    flat_g = Reshape((-1, shape_g[-1]))(var_g)
    flat_h = Reshape((-1, shape_h[-1]))(var_h)

    var_s = Lambda(lambda var_x: K.batch_dot(var_x[0],
                                             Permute((2, 1))(var_x[1])))(
                                                 [flat_g, flat_f])

    beta = Softmax(axis=-1)(var_s)
    var_o = Lambda(lambda var_x: K.batch_dot(var_x[0], var_x[1]))(
        [beta, flat_h])
    var_o = Reshape(shape_x[1:])(var_o)
    var_o = Scale()(var_o)

    out = add([var_o, inp])
    return out
Exemplo n.º 23
0
 def __init__(self, hyperparameters: Dict[str, any]):
     super().__init__()
     self.logger = logging.getLogger(__name__)
     vocabulary_size = hyperparameters['vocabulary_size']
     embedding_dim = hyperparameters['embedding_dim']
     max_chunk_length = hyperparameters['max_chunk_length']
     dropout_rate = hyperparameters['dropout_rate']
     w1 = hyperparameters['w1']
     w2 = hyperparameters['w2']
     w3 = hyperparameters['w3']
     k1 = hyperparameters['k1']
     k2 = hyperparameters['k2']
     self.embedding_layer = TimeDistributed(
         Embedding(vocabulary_size,
                   embedding_dim,
                   mask_zero=True,
                   input_length=max_chunk_length,
                   name='cnn_att_embedding'))
     self.gru_layer = TimeDistributed(
         GRU(
             k2,
             return_state=True,
             return_sequences=True,
             # recurrent_dropout=dropout_rate,
             name='cnn_att_gru'))
     self.attention_feature_layer = AttentionFeatures(
         k1, w1, k2, w2, dropout_rate)
     self.attention_weights_alpha_layer = AttentionWeights(w3, dropout_rate)
     self.attention_weights_kappa_layer = AttentionWeights(w3, dropout_rate)
     self.lambda_conv_layer = TimeDistributed(
         Conv1D(1, w3, activation='sigmoid'))
     self.max_layer = TimeDistributed(MaxPooling1D(pool_size=1, strides=50))
     # dense layer: E * n_t + bias, mapped to probability of words embedding
     self.bias = self.add_weight(name='bias',
                                 shape=[
                                     vocabulary_size,
                                 ],
                                 initializer='zeros',
                                 trainable=True)
     self.softmax_layer = TimeDistributed(Softmax())
Exemplo n.º 24
0
    def prediction_from_heatmap(self, heatmaps):
        X = 2
        Y = 3
        Z = 4
        dims = heatmaps.shape  # batches, joints, x, y, z
        batch_size = dims[0]
        nb_joints = dims[1]
        max_x = tf.cast(dims[X], dtype=tf.float32)
        max_y = tf.cast(dims[Y], dtype=tf.float32)
        max_z = tf.cast(dims[Z], dtype=tf.float32)
        # reshape into batches, joints, location
        heatmaps = tf.reshape(
            heatmaps, [batch_size, nb_joints, dims[X] * dims[Y] * dims[Z]])
        # apply softmax on locations
        heatmaps = Softmax(axis=2)(heatmaps)
        # reshape back to batches, joints,x,y,z
        heatmaps = tf.reshape(heatmaps, dims)
        # reduce probability on each axis
        prob_x = tf.reduce_sum(heatmaps, axis=(Y, Z))
        prob_y = tf.reduce_sum(heatmaps, axis=(X, Z))
        prob_z = tf.reduce_sum(heatmaps, axis=(X, Y))

        estimate_x = prob_x * tf.range(0, max_x)
        estimate_y = prob_y * tf.range(0, max_y)
        estimate_z = prob_z * tf.range(0, max_z)

        estimate_x = tf.reduce_sum(estimate_x, axis=2, keepdims=True)
        estimate_y = tf.reduce_sum(estimate_y, axis=2, keepdims=True)
        estimate_z = tf.reduce_sum(estimate_z, axis=2, keepdims=True)

        # normalize to -0.5;+0.5
        estimate_x = estimate_x / max_x - 0.5
        estimate_y = estimate_y / max_y - 0.5
        estimate_z = estimate_z / max_z - 0.5

        prediction = tf.concat([estimate_x, estimate_y, estimate_z], axis=2)
        # reshape into batches, joints, [x,y,z]
        prediction = tf.reshape(prediction, [batch_size, nb_joints * 3])
        return prediction
 def build_model(self, input_shape: Tuple[int, int, int]) -> Model:
     x = Input(shape=input_shape, name="input")
     big_branch_out = self.__build_big_output_head(x=x)
     sub_sampled_branches_out = self.__build_sub_sampled_branches_out(x=x)
     medium_branch_up = UpSampling2D(
         size=(2, 2), name="medium_branch_up")(sub_sampled_branches_out)
     medium_branch_up_refine = Conv2D(
         filters=32,
         kernel_size=(3, 3),
         padding="same",
         dilation_rate=(2, 2),
         activation="relu",
         name="medium_branch_up_refine")(medium_branch_up)
     fuse_add = Add(name="big_medium_fuse_add")(
         [big_branch_out, medium_branch_up_refine])
     fuse_add_bn = BatchNormalization(
         name=f"big_medium_fuse_add_bn")(fuse_add)
     cls_conv = Conv2D(filters=self._num_classes,
                       kernel_size=(1, 1),
                       padding="same",
                       name="output")(fuse_add_bn)
     flatten_out = Softmax(name="output_soft_max")(cls_conv)
     return Model(x, flatten_out)
Exemplo n.º 26
0
def __create_semantic_head(pyramid_dict,
                           n_classes=3,
                           n_filters=64,
                           n_dense=128,
                           semantic_id=0,
                           ndim=2,
                           include_top=False,
                           target_level=2,
                           **kwargs):
    """Creates a semantic head from a feature pyramid network.

    Args:
        pyramid_dict: dict of pyramid names and features
        n_classes (int): Defaults to 3.  The number of classes to be predicted
        n_filters (int): Defaults to 64. The number of convolutional filters.
        n_dense (int): Defaults to 128. Number of dense filters.
        semantic_id (int): Defaults to 0.
        ndim (int): Defaults to 2, 3d supported.
        include_top (bool): Defaults to False.
        target_level (int, optional): Defaults to 2. The level we need to reach.
            Performs 2x upsampling until we're at the target level

    Returns:
        keras.layers.Layer: The semantic segmentation head
    """
    if K.image_data_format() == 'channels_first':
        channel_axis = 1
    else:
        channel_axis = -1

    if n_classes == 1:
        include_top = False

    # Get pyramid names and features into list form
    pyramid_names = get_sorted_keys(pyramid_dict)
    pyramid_features = [pyramid_dict[name] for name in pyramid_names]

    # Reverse pyramid names and features
    pyramid_names.reverse()
    pyramid_features.reverse()

    semantic_sum = pyramid_features[-1]
    semantic_names = pyramid_names[-1]

    # Final upsampling
    min_level = int(re.findall(r'\d+', semantic_names[-1])[0])
    n_upsample = min_level
    x = semantic_upsample(semantic_sum, n_upsample, ndim=ndim)

    # First tensor product
    x = TensorProduct(n_dense)(x)
    x = BatchNormalization(axis=channel_axis)(x)
    x = Activation('relu')(x)

    # Apply tensor product and softmax layer
    x = TensorProduct(n_classes)(x)

    if include_top:
        x = Softmax(axis=channel_axis, name='semantic_{}'.format(semantic_id))(x)
    else:
        x = Activation('relu', name='semantic_{}'.format(semantic_id))(x)

    return x
Exemplo n.º 27
0
def bn_feature_net_2D(receptive_field=61,
                      input_shape=(256, 256, 1),
                      inputs=None,
                      n_features=3,
                      n_channels=1,
                      reg=1e-5,
                      n_conv_filters=64,
                      n_dense_filters=200,
                      VGG_mode=False,
                      init='he_normal',
                      norm_method='std',
                      location=False,
                      dilated=False,
                      padding=False,
                      padding_mode='reflect',
                      multires=False,
                      include_top=True):
    """Creates a 2D featurenet.

    Args:
        receptive_field (int): the receptive field of the neural network.
        input_shape (tuple): If no input tensor, create one with this shape.
        inputs (tensor): optional input tensor
        n_features (int): Number of output features
        n_channels (int): number of input channels
        reg (int): regularization value
        n_conv_filters (int): number of convolutional filters
        n_dense_filters (int): number of dense filters
        VGG_mode (bool): If multires, uses VGG_mode for multiresolution
        init (str): Method for initalizing weights.
        norm_method (str): ImageNormalization mode to use
        location (bool): Whether to include location data
        dilated (bool): Whether to use dilated pooling.
        padding (bool): Whether to use padding.
        padding_mode (str): Type of padding, one of 'reflect' or 'zero'
        multires (bool): Enables multi-resolution mode
        include_top (bool): Whether to include the final layer of the model

    Returns:
        tensorflow.keras.Model: 2D FeatureNet
    """
    # Create layers list (x) to store all of the layers.
    # We need to use the functional API to enable the multiresolution mode
    x = []

    win = (receptive_field - 1) // 2

    if dilated:
        padding = True

    if K.image_data_format() == 'channels_first':
        channel_axis = 1
        row_axis = 2
        col_axis = 3

        if not dilated:
            input_shape = (n_channels, receptive_field, receptive_field)

    else:
        row_axis = 1
        col_axis = 2
        channel_axis = -1
        if not dilated:
            input_shape = (receptive_field, receptive_field, n_channels)

    if inputs is not None:
        if not K.is_keras_tensor(inputs):
            img_input = Input(tensor=inputs, shape=input_shape)
        else:
            img_input = inputs
        x.append(img_input)
    else:
        x.append(Input(shape=input_shape))

    x.append(
        ImageNormalization2D(norm_method=norm_method,
                             filter_size=receptive_field)(x[-1]))

    if padding:
        if padding_mode == 'reflect':
            x.append(ReflectionPadding2D(padding=(win, win))(x[-1]))
        elif padding_mode == 'zero':
            x.append(ZeroPadding2D(padding=(win, win))(x[-1]))

    if location:
        x.append(Location2D(in_shape=tuple(x[-1].shape.as_list()[1:]))(x[-1]))
        x.append(Concatenate(axis=channel_axis)([x[-2], x[-1]]))

    layers_to_concat = []

    rf_counter = receptive_field
    block_counter = 0
    d = 1

    while rf_counter > 4:
        filter_size = 3 if rf_counter % 2 == 0 else 4
        x.append(
            Conv2D(n_conv_filters,
                   filter_size,
                   dilation_rate=d,
                   kernel_initializer=init,
                   padding='valid',
                   kernel_regularizer=l2(reg))(x[-1]))
        x.append(BatchNormalization(axis=channel_axis)(x[-1]))
        x.append(Activation('relu')(x[-1]))

        block_counter += 1
        rf_counter -= filter_size - 1

        if block_counter % 2 == 0:
            if dilated:
                x.append(
                    DilatedMaxPool2D(dilation_rate=d, pool_size=(2, 2))(x[-1]))
                d *= 2
            else:
                x.append(MaxPool2D(pool_size=(2, 2))(x[-1]))

            if VGG_mode:
                n_conv_filters *= 2

            rf_counter = rf_counter // 2

            if multires:
                layers_to_concat.append(len(x) - 1)

    if multires:
        c = []
        for l in layers_to_concat:
            output_shape = x[l].get_shape().as_list()
            target_shape = x[-1].get_shape().as_list()

            row_crop = int(output_shape[row_axis] - target_shape[row_axis])
            if row_crop % 2 == 0:
                row_crop = (row_crop // 2, row_crop // 2)
            else:
                row_crop = (row_crop // 2, row_crop // 2 + 1)

            col_crop = int(output_shape[col_axis] - target_shape[col_axis])
            if col_crop % 2 == 0:
                col_crop = (col_crop // 2, col_crop // 2)
            else:
                col_crop = (col_crop // 2, col_crop // 2 + 1)

            cropping = (row_crop, col_crop)

            c.append(Cropping2D(cropping=cropping)(x[l]))

        if multires:
            x.append(Concatenate(axis=channel_axis)(c))

    x.append(
        Conv2D(n_dense_filters, (rf_counter, rf_counter),
               dilation_rate=d,
               kernel_initializer=init,
               padding='valid',
               kernel_regularizer=l2(reg))(x[-1]))
    x.append(BatchNormalization(axis=channel_axis)(x[-1]))
    x.append(Activation('relu')(x[-1]))

    if include_top:
        x.append(
            TensorProduct(n_dense_filters,
                          kernel_initializer=init,
                          kernel_regularizer=l2(reg))(x[-1]))
        x.append(BatchNormalization(axis=channel_axis)(x[-1]))
        x.append(Activation('relu')(x[-1]))

        x.append(
            TensorProduct(n_features,
                          kernel_initializer=init,
                          kernel_regularizer=l2(reg))(x[-1]))

        if not dilated:
            x.append(Flatten()(x[-1]))

        x.append(Softmax(axis=channel_axis)(x[-1]))

    if inputs is not None:
        real_inputs = keras_utils.get_source_inputs(x[0])
    else:
        real_inputs = x[0]

    model = Model(inputs=real_inputs, outputs=x[-1])

    return model
Exemplo n.º 28
0
def TD(input_shape, optimizer, weight_decay, class_weights, method='tf'):

    input_1 = Input(shape=input_shape)
    interpolation = 'nearest'

    sigma = 1

    if method == 'gaussian':
        input_2 = Lambda(downscale_gaussian, arguments={'sigma':
                                                        sigma})(input_1)
        input_4 = Lambda(downscale_gaussian,
                         arguments={
                             'sigma': 2 * sigma,
                             'down_factor': 4
                         })(input_1)
    elif method == 'noblur':
        input_2 = Lambda(downscale_noblur)(input_1)
        input_4 = Lambda(downscale_noblur)(input_2)
    elif method == 'antialias':
        input_2 = Lambda(downscale_antialias)(input_1)
        input_4 = Lambda(downscale_antialias)(input_2)
    elif method == 'tf':
        input_2 = Lambda(downscale_tf)(input_1)
        input_4 = Lambda(downscale_tf, arguments={'down_factor': 4})(input_1)
    else:
        input_2 = Lambda(downscale_pool)(input_1)
        input_4 = Lambda(downscale_pool)(input_2)

    regularizer = regularizers.l2(weight_decay)

    conv_1 = conv_block(input_1,
                        2,
                        num_filters=64,
                        k_size=3,
                        regularizer=regularizer)
    conv_2 = conv_block(input_2,
                        2,
                        num_filters=128,
                        k_size=3,
                        regularizer=regularizer)

    x = conv_block(input_4,
                   2,
                   num_filters=256,
                   k_size=3,
                   regularizer=regularizer)

    # 14x14
    x = Conv2D(filters=128,
               kernel_size=1,
               padding='same',
               kernel_regularizer=regularizer)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Lambda(upscale, arguments={'method': interpolation})(x)
    x = add([x, conv_2])
    x = Concatenate(axis=-1)([x, conv_2])
    x = conv_block(x, 3, num_filters=128, k_size=3, regularizer=regularizer)

    x = Conv2D(filters=64,
               kernel_size=1,
               padding='same',
               kernel_regularizer=regularizer)(x)
    x = BatchNormalization()(x)
    x = Activation('relu')(x)
    x = Lambda(upscale, arguments={'method': interpolation})(x)
    x = add([x, conv_1])
    x = Concatenate(axis=-1)([x, conv_1])
    x = conv_block(x, 3, num_filters=64, k_size=3, regularizer=regularizer)

    x = Conv2D(filters=12,
               kernel_size=1,
               padding='same',
               kernel_regularizer=regularizer)(x)
    x = BatchNormalization()(x)

    out = Softmax()(x)

    model = Model(inputs=input_1, outputs=out)
    w_loss = weighted_centropy(class_weights)
    model.compile(optimizer=optimizer, loss=w_loss, metrics=['accuracy', mIoU])

    return model
Exemplo n.º 29
0
def cifar100_complicated_ensemble(
        input_shape=None,
        input_tensor=None,
        n_classes=None,
        weights_path: Union[None, str] = None) -> Model:
    """
    Defines a cifar100 network.

    :param n_classes: used in order to be compatible with the main script.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained custom network's weights.
    :return: Keras functional API Model.
    """
    output_list = []
    inputs = create_inputs(input_shape, input_tensor)

    # Define a weight decay for the regularisation.
    weight_decay = 1e-4

    # Submodel 1.
    # Block1.
    x1 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel1_block1_conv1',
                kernel_regularizer=l2(weight_decay))(inputs)
    x1 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel1_block1_conv2',
                kernel_regularizer=l2(weight_decay))(x1)
    x1 = BatchNormalization(name='submodel1_block1_batch-norm')(x1)
    x1 = MaxPooling2D(pool_size=(2, 2), name='submodel1_block1_pool')(x1)

    # Block2
    x1 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel1_block2_conv1',
                kernel_regularizer=l2(weight_decay))(x1)
    x1 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel1_block2_conv2',
                kernel_regularizer=l2(weight_decay))(x1)
    x1 = BatchNormalization(name='submodel1_block2_batch-norm')(x1)
    x1 = MaxPooling2D(pool_size=(2, 2), name='submodel1_block2_pool')(x1)

    # Block3
    x1 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel1_block3_conv1',
                kernel_regularizer=l2(weight_decay))(x1)
    x1 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel1_block3_conv2',
                kernel_regularizer=l2(weight_decay))(x1)
    x1 = BatchNormalization(name='submodel1_block3_batch-norm')(x1)
    x1 = MaxPooling2D(pool_size=(2, 2), name='submodel1_block3_pool')(x1)

    # Add Submodel 1 top layers.
    x1 = Flatten(name='submodel1_flatten')(x1)
    outputs1 = Dense(20, name='submodel1_output')(x1)
    # Crop outputs1 in order to create the first submodel's output.
    outputs_first_submodel = Crop(1, 0, 10,
                                  name='first_ten_classes_submodel')(outputs1)
    output_list.append(outputs_first_submodel)

    # Submodel 2.
    # Block1.
    x2 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel2_block1_conv1',
                kernel_regularizer=l2(weight_decay))(inputs)
    x2 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel2_block1_conv2',
                kernel_regularizer=l2(weight_decay))(x2)
    x2 = BatchNormalization(name='submodel2_block1_batch-norm')(x2)
    x2 = MaxPooling2D(pool_size=(2, 2), name='submodel2_block1_pool')(x2)

    # Block2
    x2 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel2_block2_conv1',
                kernel_regularizer=l2(weight_decay))(x2)
    x2 = Conv2D(256, (3, 3),
                padding='same',
                activation='elu',
                name='submodel2_block2_conv2',
                kernel_regularizer=l2(weight_decay))(x2)
    x2 = BatchNormalization(name='submodel2_block2_batch-norm')(x2)
    x2 = MaxPooling2D(pool_size=(2, 2), name='submodel2_block2_pool')(x2)

    # Add Submodel 2 top layers.
    x2 = Flatten(name='submodel2_flatten')(x2)
    outputs2 = Dense(30, name='submodel2_output')(x2)

    # Average the predictions for the second class of the first two submodels.
    averaged_classes_20_30 = Average(name='averaged_second_ten_classes')(
        [Crop(1, 10, 20)(outputs1),
         Crop(1, 0, 10)(outputs2)])
    # Crop outputs2 in order to create the third ten classes output.
    outputs_classes_30_40 = Crop(1, 10, 20, name='third_ten_classes')(outputs2)
    # Concatenate classes outputs in order to create the second submodel's output.
    outputs_second_submodel = Concatenate(name='second_submodel')(
        [averaged_classes_20_30, outputs_classes_30_40])
    output_list.append(outputs_second_submodel)

    # Submodel 3.
    # Block1.
    x3 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel3_block1_conv1',
                kernel_regularizer=l2(weight_decay))(inputs)
    x3 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel3_block1_conv2',
                kernel_regularizer=l2(weight_decay))(x3)
    x3 = BatchNormalization(name='submodel3_block1_batch-norm')(x3)
    x3 = MaxPooling2D(pool_size=(2, 2), name='submodel3_block1_pool')(x3)

    # Block2
    x3 = Conv2D(256, (3, 3),
                padding='same',
                activation='elu',
                name='submodel3_block2_conv1',
                kernel_regularizer=l2(weight_decay))(x3)
    x3 = Conv2D(256, (3, 3),
                padding='same',
                activation='elu',
                name='submodel3_block2_conv2',
                kernel_regularizer=l2(weight_decay))(x3)
    x3 = BatchNormalization(name='submodel3_block2_batch-norm')(x3)
    x3 = MaxPooling2D(pool_size=(2, 2), name='submodel3_block2_pool')(x3)

    # Add Submodel 3 top layers.
    x3 = Flatten(name='submodel3_flatten')(x3)
    outputs3 = Dense(30, name='submodel3_output')(x3)

    # Average the predictions for the fourth class of the last two submodels.
    averaged_classes_30_40 = Average(name='averaged_fourth_ten_class')(
        [Crop(1, 20, 30)(outputs2),
         Crop(1, 0, 10)(outputs3)])
    # Crop outputs3 in order to create the fifth abd sixth class outputs.
    outputs_classes_40_50 = Crop(1, 10, 20, name='fifth_ten_class')(outputs3)
    outputs_classes_50_60 = Crop(1, 20, 30, name='sixth_ten_class')(outputs3)
    # Concatenate classes outputs in order to create the third submodel's output.
    outputs_third_submodel = Concatenate(name='third_submodel')(
        [averaged_classes_30_40, outputs_classes_40_50, outputs_classes_50_60])
    output_list.append(outputs_third_submodel)

    # Submodel 4.
    # Block1.
    x4 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel4_block1_conv1',
                kernel_regularizer=l2(weight_decay))(inputs)
    x4 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel4_block1_conv2',
                kernel_regularizer=l2(weight_decay))(x4)
    x4 = BatchNormalization(name='submodel4_block1_batch-norm')(x4)
    x4 = MaxPooling2D(pool_size=(2, 2), name='submodel4_block1_pool')(x4)

    # Block2
    x4 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel4_block2_conv1',
                kernel_regularizer=l2(weight_decay))(x4)
    x4 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel4_block2_conv2',
                kernel_regularizer=l2(weight_decay))(x4)
    x4 = BatchNormalization(name='submodel4_block2_batch-norm')(x4)
    x4 = MaxPooling2D(pool_size=(2, 2), name='submodel4_block2_pool')(x4)

    # Block3
    x4 = Conv2D(256, (3, 3),
                padding='same',
                activation='elu',
                name='submodel4_block3_conv1',
                kernel_regularizer=l2(weight_decay))(x4)
    x4 = Conv2D(256, (3, 3),
                padding='same',
                activation='elu',
                name='submodel4_block3_conv2',
                kernel_regularizer=l2(weight_decay))(x4)
    x4 = BatchNormalization(name='submodel4_block3_batch-norm')(x4)
    x4 = MaxPooling2D(pool_size=(2, 2), name='submodel4_block3_pool')(x4)

    # Add Submodel 4 top layers.
    x4 = Flatten(name='submodel4_flatten')(x4)
    outputs4 = Dense(20, name='60-80_classes_submodel4')(x4)
    output_list.append(outputs4)

    # Submodel 5.
    # Block1.
    x5 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel5_block1_conv1',
                kernel_regularizer=l2(weight_decay))(inputs)
    x5 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel5_block1_conv2',
                kernel_regularizer=l2(weight_decay))(x5)
    x5 = BatchNormalization(name='submodel5_block1_batch-norm')(x5)
    x5 = MaxPooling2D(pool_size=(2, 2), name='submodel5_block1_pool')(x5)

    # Block2
    x5 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel5_block2_conv1',
                kernel_regularizer=l2(weight_decay))(x5)
    x5 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel5_block2_conv2',
                kernel_regularizer=l2(weight_decay))(x5)
    x5 = BatchNormalization(name='submodel5_block2_batch-norm')(x5)
    x5 = MaxPooling2D(pool_size=(2, 2), name='submodel5_block2_pool')(x5)

    # Block3
    x5 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel5_block3_conv1',
                kernel_regularizer=l2(weight_decay))(x5)
    x5 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel5_block3_conv2',
                kernel_regularizer=l2(weight_decay))(x5)
    x5 = BatchNormalization(name='submodel5_block3_batch-norm')(x5)
    x5 = MaxPooling2D(pool_size=(2, 2), name='submodel5_block3_pool')(x5)

    # Add Submodel 5 top layers.
    x5 = Flatten(name='submodel5_flatten')(x5)
    outputs5 = Dense(20, name='80-100_classes_submodel4')(x5)
    output_list.append(outputs5)

    # Concatenate all class predictions together.
    outputs = Concatenate(name='output')(output_list)
    outputs = Softmax(name='output_softmax')(outputs)

    # Create model.
    model = Model(inputs, outputs, name='cifar100_complicated_ensemble')
    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model
Exemplo n.º 30
0
def cifar100_pyramid_ensemble(input_shape=None,
                              input_tensor=None,
                              n_classes=None,
                              weights_path: Union[None, str] = None) -> Model:
    """
    Defines a cifar100 network.

    :param n_classes: used in order to be compatible with the main script.
    :param input_shape: the input shape of the network. Can be omitted if input_tensor is used.
    :param input_tensor: the input tensor of the network. Can be omitted if input_shape is used.
    :param weights_path: a path to a trained custom network's weights.
    :return: Keras functional API Model.
    """
    output_list = []
    inputs = create_inputs(input_shape, input_tensor)

    # Submodel Strong.
    # Block1.
    x1 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_strong_block1_conv1')(inputs)
    x1 = Conv2D(64, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_strong_block1_conv2')(x1)
    x1 = MaxPooling2D(pool_size=(2, 2), name='submodel_strong_block1_pool')(x1)

    # Block2
    x1 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_strong_block2_conv1')(x1)
    x1 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_strong_block2_conv2')(x1)
    x1 = MaxPooling2D(pool_size=(2, 2), name='submodel_strong_block2_pool')(x1)

    # Block3
    x1 = BatchNormalization(name='submodel_strong_block3_batch-norm')(x1)
    x1 = Conv2D(256, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_strong_block3_conv')(x1)
    x1 = Dropout(0.5, name='submodel_strong_block3_dropout', seed=0)(x1)

    # Add Submodel Strong top layers.
    x1 = Flatten(name='submodel_strong_flatten')(x1)
    outputs_submodel_strong = Dense(100, name='submodel_strong_output')(x1)

    # Submodel Weak 1.
    # Block1.
    x2 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_weak_1_block1_conv1')(inputs)
    x2 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_weak_1_block1_conv2')(x2)
    x2 = MaxPooling2D(pool_size=(2, 2), name='submodel_weak_1_block1_pool')(x2)

    # Add Submodel Weak 1 top layers.
    x2 = Flatten(name='submodel_weak_1_flatten')(x2)
    outputs2 = Dense(50, name='submodel_weak_1_output')(x2)

    # Average the predictions for the first five classes.
    averaged_first_half_classes = Average(name='averaged_first_half_classes')(
        [Crop(1, 0, 50)(outputs_submodel_strong), outputs2])

    output_list.append(averaged_first_half_classes)

    # Submodel Weak 2.
    # Block1.
    x3 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_weak_2_block1_conv1')(inputs)
    x3 = Conv2D(128, (3, 3),
                padding='same',
                activation='elu',
                name='submodel_weak_2_block1_conv2')(x3)
    x3 = MaxPooling2D(pool_size=(2, 2), name='submodel_weak_2_block1_pool')(x3)

    # Add Submodel Weak 2 top layers.
    x3 = Flatten(name='submodel_weak_2_flatten')(x3)
    outputs3 = Dense(50, name='submodel_weak_2_output')(x3)

    # Average the predictions for the last five classes.
    averaged_last_half_classes = Average(name='averaged_last_half_classes')(
        [Crop(1, 50, 100)(outputs_submodel_strong), outputs3])

    output_list.append(averaged_last_half_classes)

    # Concatenate all class predictions together.
    outputs = Concatenate(name='output')(output_list)
    outputs = Softmax(name='output_softmax')(outputs)

    # Create model.
    model = Model(inputs, outputs, name='cifar100_pyramid_ensemble')
    # Load weights, if they exist.
    load_weights(weights_path, model)

    return model