Ejemplo n.º 1
0
def FC(dnn_feature_columns,
       history_feature_list,
       embedding_size=8,
       hist_len_max=16,
       dnn_use_bn=False,
       dnn_hidden_units=(200, 80),
       dnn_activation='relu',
       l2_reg_dnn=0,
       dnn_dropout=0,
       init_std=0.0001,
       seed=1024,
       task='binary'):
    """Instantiates the Deep Interest Network architecture.

    :param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
    :param history_feature_list: list,to indicate  sequence sparse field
    :param embedding_size: positive integer,sparse feature embedding_size.
    :param hist_len_max: positive int, to indicate the max length of seq input
    :param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in deep net
    :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
    :param dnn_activation: Activation function to use in deep net
    :param att_hidden_size: list,list of positive integer , the layer number and units in each layer of attention net
    :param att_activation: Activation function to use in attention net
    :param att_weight_normalization: bool.Whether normalize the attention score of local activation unit.
    :param l2_reg_dnn: float. L2 regularizer strength applied to DNN
    :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
    :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
    :param init_std: float,to use as the initialize std of embedding vector
    :param seed: integer ,to use as random seed.
    :param task: str, ``"binary"`` for  binary logloss or  ``"regression"`` for regression loss
    :return: A Keras model instance.

    """

    features = build_input_features(dnn_feature_columns)

    sparse_feature_columns = list(
        filter(lambda x: isinstance(x, SparseFeat),
               dnn_feature_columns)) if dnn_feature_columns else []
    dense_feature_columns = list(
        filter(lambda x: isinstance(x, DenseFeat),
               dnn_feature_columns)) if dnn_feature_columns else []

    inputs_list = list(features.values())

    embedding_dict = create_embedding_matrix(sparse_feature_columns)

    dnn_input_emb_list = embedding_lookup(embedding_dict, features,
                                          sparse_feature_columns)
    dense_value_list = get_dense_input(features, dense_feature_columns)
    print(dense_value_list)
    deep_input_emb = concat_fun(dnn_input_emb_list)

    deep_input_emb = deep_input_emb
    print(deep_input_emb)
    deep_input_emb = Flatten()(deep_input_emb)
    print(deep_input_emb)
    dnn_input = combined_dnn_input([deep_input_emb], dense_value_list)
    print(dnn_input)
    # output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn,
    #              dnn_dropout, dnn_use_bn, seed)(dnn_input)

    output = Dense(200, activation='relu')(dnn_input)
    output = Dense(80, activation='relu')(output)

    output = Dense(1, activation='sigmoid')(output)

    # output = PredictionLayer(task)(final_logit)

    model = Model(inputs=inputs_list, outputs=output)
    return model
Ejemplo n.º 2
0
                  tamano_filtro1,
                  padding="same",
                  input_shape=(longitud, altura, 3),
                  activation='relu'))

# Agrega capa 2 un Maxpooling
cnn.add(MaxPooling2D(pool_size=tamano_pool))

# Agrega capa 3 otra capa convolucional (Input_shape solo se usa en la primera capa)
cnn.add(Convolution2D(filtrosConv2, tamano_filtro2, padding="same"))

# Agrega capa 4 Maxpooling
cnn.add(MaxPooling2D(pool_size=tamano_pool))

# Agrega capa 5 Aplana la imagen a una sola dimesion
cnn.add(Flatten())

# Agrega capa 6 agrega 256 neuronas con activacion relu
cnn.add(Dense(256, activation="relu"))

# Agrega capa 7 y activa aleatoriamente solo la mitad de las neuronas
cnn.add(Dropout(0.5))

# Agrega capa 8 con solo 3 neuronas para clasificar y su activacion es softmax
cnn.add(Dense(clases, activation="softmax"))

# Compila la cnn con la finalidad de mejorar la accuracy
cnn.compile(loss='categorical_crossentropy',
            optimizer=optimizers.Adam(lr=lr),
            metrics=['accuracy'])
Ejemplo n.º 3
0
dummy_env = create_env()
initializer = VarianceScaling()
model = Sequential([
    Conv2D(8,
           4,
           activation='elu',
           padding='same',
           input_shape=dummy_env.observation_space.shape,
           kernel_initializer=initializer),
    Conv2D(16,
           2,
           activation='elu',
           padding='valid',
           input_shape=dummy_env.observation_space.shape,
           kernel_initializer=initializer),
    Flatten(),
    Dropout(0.5),
    Dense(512, activation='elu', kernel_initializer=initializer)
])
# Optimizer with sheduled learning rate decay
optimizer = Adam(lr=3e-3, decay=1e-5)
# Run multiple instances
instances = 8
# Exploration and learning rate decay after each epoch
eps_max = 0.2
eps_decay = 0.9
learning_rate = 3e-3
learning_decay = 0.9
# Create Advantage Actor-Critic agent
agent = A2C(model,
            actions=dummy_env.action_space.n,
Ejemplo n.º 4
0
 def add(self, data_format=None, **kwargs):
     return self._add_layer(Flatten(data_format=data_format, **kwargs))
Ejemplo n.º 5
0
    def build(input_shape, num_outputs, block_fn, hp_lambda, repetitions):
        """Builds a custom ResNet like architecture.
        Args:
            input_shape: The input shape in the form (nb_channels, nb_rows, nb_cols)
            num_outputs: The number of outputs at final softmax layer
            block_fn: The block function to use. This is either `basic_block` or `bottleneck`.
                The original paper used basic_block 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
        Returns:
            The keras `Model`.
        """
        _handle_dim_ordering()
        if len(input_shape) != 3:
            raise Exception(
                "Input shape should be a tuple (nb_channels, nb_rows, nb_cols)"
            )

        # Permute dimension order if necessary
        if K.image_data_format() == 'channels_last':
            input_shape = (input_shape[1], input_shape[2], input_shape[0])

        # Load function from str if needed.
        block_fn = _get_block(block_fn)

        input = Input(shape=input_shape)
        conv1 = _conv_bn_relu(filters=16, kernel_size=(7, 7),
                              strides=(2, 2))(input)
        pool1 = MaxPooling2D(pool_size=(3, 3), strides=(2, 2),
                             padding="same")(conv1)

        block = pool1
        filters = 16
        for i, r in enumerate(repetitions):
            #block = SpatialDropout2D(rate=0.5)(block)
            block = _residual_block(block_fn,
                                    filters=filters,
                                    repetitions=r,
                                    is_first_layer=(i == 0))(block)
            filters *= 2

        # Last activation
        block_output_split = _bn_relu(block)
        #block = SpatialDropout2D(rate=0.5)(block)

        # Classifier block class label
        block_shape = K.int_shape(block_output_split)
        pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS],
                                            block_shape[COL_AXIS]),
                                 strides=(1, 1))(block_output_split)
        flatten1 = Flatten()(pool2)
        dense_class = Dense(units=num_outputs,
                            kernel_initializer="he_normal",
                            activation="sigmoid")(flatten1)

        # Classifier block domain label
        hp_lambda = 0.01
        Flip = flipGradient.GradientReversal(hp_lambda)
        block = Flip(block_output_split)

        block_shape = K.int_shape(block)
        pool2 = AveragePooling2D(pool_size=(block_shape[ROW_AXIS],
                                            block_shape[COL_AXIS]),
                                 strides=(1, 1))(block)
        flatten1 = Flatten()(pool2)

        # flatten1 = Dense(units=256, kernel_initializer="he_normal", activation="relu")(flatten1)

        # flatten1 = Dense(units=256, kernel_initializer="he_normal", activation="relu")(flatten1)

        dense_domain = Dense(units=num_outputs,
                             kernel_initializer="he_normal",
                             activation="sigmoid")(flatten1)

        model_combined = Model(inputs=input,
                               outputs=[dense_class, dense_domain])

        model_class = Model(inputs=input, outputs=dense_class)

        model_domain = Model(inputs=input, outputs=dense_domain)

        return (model_combined, model_class)
Ejemplo n.º 6
0
def EEGNet(nb_classes, Chans = 64, Samples = 128, 
             dropoutRate = 0.5, kernLength = 64, F1 = 8, 
             D = 2, F2 = 16, norm_rate = 0.25, dropoutType = 'Dropout'):
    """ Keras Implementation of EEGNet
    http://iopscience.iop.org/article/10.1088/1741-2552/aace8c/meta
    Note that this implements the newest version of EEGNet and NOT the earlier
    version (version v1 and v2 on arxiv). We strongly recommend using this
    architecture as it performs much better and has nicer properties than
    our earlier version. For example:
        
        1. Depthwise Convolutions to learn spatial filters within a 
        temporal convolution. The use of the depth_multiplier option maps 
        exactly to the number of spatial filters learned within a temporal
        filter. This matches the setup of algorithms like FBCSP which learn 
        spatial filters within each filter in a filter-bank. This also limits 
        the number of free parameters to fit when compared to a fully-connected
        convolution. 
        
        2. Separable Convolutions to learn how to optimally combine spatial
        filters across temporal bands. Separable Convolutions are Depthwise
        Convolutions followed by (1x1) Pointwise Convolutions. 
        
    
    While the original paper used Dropout, we found that SpatialDropout2D 
    sometimes produced slightly better results for classification of ERP 
    signals. However, SpatialDropout2D significantly reduced performance 
    on the Oscillatory dataset (SMR, BCI-IV Dataset 2A). We recommend using
    the default Dropout in most cases.
        
    Assumes the input signal is sampled at 128Hz. If you want to use this model
    for any other sampling rate you will need to modify the lengths of temporal
    kernels and average pooling size in blocks 1 and 2 as needed (double the 
    kernel lengths for double the sampling rate, etc). Note that we haven't 
    tested the model performance with this rule so this may not work well. 
    
    The model with default parameters gives the EEGNet-8,2 model as discussed
    in the paper. This model should do pretty well in general, although it is
	advised to do some model searching to get optimal performance on your
	particular dataset.
    We set F2 = F1 * D (number of input filters = number of output filters) for
    the SeparableConv2D layer. We haven't extensively tested other values of this
    parameter (say, F2 < F1 * D for compressed learning, and F2 > F1 * D for
    overcomplete). We believe the main parameters to focus on are F1 and D. 
    Inputs:
        
      nb_classes      : int, number of classes to classify
      Chans, Samples  : number of channels and time points in the EEG data
      dropoutRate     : dropout fraction
      kernLength      : length of temporal convolution in first layer. We found
                        that setting this to be half the sampling rate worked
                        well in practice. For the SMR dataset in particular
                        since the data was high-passed at 4Hz we used a kernel
                        length of 32.     
      F1, F2          : number of temporal filters (F1) and number of pointwise
                        filters (F2) to learn. Default: F1 = 8, F2 = F1 * D. 
      D               : number of spatial filters to learn within each temporal
                        convolution. Default: D = 2
      dropoutType     : Either SpatialDropout2D or Dropout, passed as a string.
    """
    
    if dropoutType == 'SpatialDropout2D':
        dropoutType = SpatialDropout2D
    elif dropoutType == 'Dropout':
        dropoutType = Dropout
    else:
        raise ValueError('dropoutType must be one of SpatialDropout2D '
                         'or Dropout, passed as a string.')
    
    input1   = Input(shape = (1, Chans, Samples))

    ##################################################################
    block1       = Conv2D(F1, (1, kernLength), padding = 'same',
                                   input_shape = (1, Chans, Samples),
                                   use_bias = False)(input1)
    block1       = BatchNormalization(axis = 1)(block1)
    block1       = DepthwiseConv2D((Chans, 1), use_bias = False, 
                                   depth_multiplier = D,
                                   depthwise_constraint = max_norm(1.))(block1)
    block1       = BatchNormalization(axis = 1)(block1)
    block1       = Activation('elu')(block1)
    block1       = AveragePooling2D((1, 4))(block1)
    block1       = dropoutType(dropoutRate)(block1)
    
    block2       = SeparableConv2D(F2, (1, 16),
                                   use_bias = False, padding = 'same')(block1)
    block2       = BatchNormalization(axis = 1)(block2)
    block2       = Activation('elu')(block2)
    block2       = AveragePooling2D((1, 8))(block2)
    block2       = dropoutType(dropoutRate)(block2)
        
    flatten      = Flatten(name = 'flatten')(block2)
    
    dense        = Dense(nb_classes, name = 'dense', 
                         kernel_constraint = max_norm(norm_rate))(flatten)
    softmax      = Activation('softmax', name = 'softmax')(dense)
    
    return Model(inputs=input1, outputs=softmax)
Ejemplo n.º 7
0
 def build_model_ex(self, pos_mode=0, use_mask=False, active_layers=999):
     #define embedding layer
     uid_embedding = Embedding(750000, 16, name='uid_embedding')  # for uid
     itemid_embedding = Embedding(7500000, 32,
                                  name='itemid_embedding')  # for icf1
     f1_embedding = Embedding(8, 2, name='f1_embedding')  # for ucf1
     f2_embedding = Embedding(4, 2, name='f2_embedding')  # for ucf2 & icf3
     f3_embedding = Embedding(8, 2, name='f3_embedding')  # for ucf3 & icf4
     f4_embedding = Embedding(4, 2, name='f4_embedding')  # for icf5
     f5_embedding = Embedding(256, 4, name='f5_embedding')  # icf2
     #define user input
     uid_input = Input(shape=(self.seq_len, ),
                       dtype='int32',
                       name='uid_input')
     ucf1_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='ucf1_input')
     ucf2_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='ucf2_input')
     ucf3_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='ucf3_input')
     #define item input
     icf1_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='icf1_input')
     icf2_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='icf2_input')
     icf3_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='icf3_input')
     icf4_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='icf4_input')
     icf5_input = Input(shape=(self.seq_len, ),
                        dtype='int32',
                        name='icf5_input')
     #define dense input
     v_input = Input(shape=(self.seq_len, self.d_feature), name='v_input')
     #define user embedding
     u0 = uid_embedding(uid_input)
     u1 = f1_embedding(ucf1_input)
     u2 = f2_embedding(ucf2_input)
     u3 = f3_embedding(ucf3_input)
     #define item embedding
     i1 = itemid_embedding(icf1_input)
     i2 = f5_embedding(icf2_input)
     i3 = f2_embedding(icf3_input)
     i4 = f3_embedding(icf4_input)
     i5 = f4_embedding(icf5_input)
     #define page embedding: 16+2+2+2+32+4+2+2+2=64
     page_embedding = concatenate(
         [v_input, u0, u1, u2, u3, i1, i2, i3, i4, i5],
         axis=-1,
         name='page_embedding')
     d0 = TimeDistributed(Dense(self.d_model))(page_embedding)
     pos_input = Input(shape=(self.seq_len, ),
                       dtype='int32',
                       name='pos_input')
     if pos_mode == 0:  # use fix pos embedding
         pos_embedding = Embedding(self.seq_len, self.d_model, trainable=False,\
             weights=[GetPosEncodingMatrix(self.seq_len, self.d_model)])
         p0 = pos_embedding(pos_input)
     elif pos_mode == 1:  # use trainable ebmedding
         pos_embedding = Embedding(self.seq_len, self.d_model)
         p0 = pos_embedding(pos_input)
     else:  # not use pos embedding
         p0 = None
     if p0 != None:
         combine_input = Add()([d0, p0])
     else:
         combine_input = d0  # no pos
     sub_mask = None
     if use_mask:
         sub_mask = Lambda(GetSubMask)(pos_input)
     enc_output = self.encoder(combine_input,
                               mask=sub_mask,
                               active_layers=active_layers)
     # score
     time_score_dense1 = TimeDistributed(
         Dense(self.d_model, activation='tanh'))(enc_output)
     time_score_dense2 = TimeDistributed(Dense(1))(time_score_dense1)
     flat = Flatten()(time_score_dense2)
     score_output = Activation(activation='softmax')(flat)
     base_input = [
         pos_input, uid_input, ucf1_input, ucf2_input, ucf3_input,
         icf1_input, icf2_input, icf3_input, icf4_input, icf5_input, v_input
     ]
     self.model = Model(base_input, score_output)
     return self.model
Ejemplo n.º 8
0
def DIN(dnn_feature_columns,
        history_feature_list,
        embedding_size=8,
        hist_len_max=16,
        dnn_use_bn=False,
        dnn_hidden_units=(200, 80),
        dnn_activation='relu',
        att_hidden_size=(80, 40),
        att_activation="dice",
        att_weight_normalization=False,
        l2_reg_dnn=0,
        l2_reg_embedding=1e-6,
        dnn_dropout=0,
        init_std=0.0001,
        seed=1024,
        task='binary'):
    """Instantiates the Deep Interest Network architecture.

    :param dnn_feature_columns: An iterable containing all the features used by deep part of the model.
    :param history_feature_list: list,to indicate  sequence sparse field
    :param embedding_size: positive integer,sparse feature embedding_size.
    :param hist_len_max: positive int, to indicate the max length of seq input
    :param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in deep net
    :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
    :param dnn_activation: Activation function to use in deep net
    :param att_hidden_size: list,list of positive integer , the layer number and units in each layer of attention net
    :param att_activation: Activation function to use in attention net
    :param att_weight_normalization: bool.Whether normalize the attention score of local activation unit.
    :param l2_reg_dnn: float. L2 regularizer strength applied to DNN
    :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
    :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
    :param init_std: float,to use as the initialize std of embedding vector
    :param seed: integer ,to use as random seed.
    :param task: str, ``"binary"`` for  binary logloss or  ``"regression"`` for regression loss
    :return: A Keras model instance.

    """

    features = build_input_features(dnn_feature_columns)

    sparse_feature_columns = list(
        filter(lambda x: isinstance(x, SparseFeat),
               dnn_feature_columns)) if dnn_feature_columns else []
    dense_feature_columns = list(
        filter(lambda x: isinstance(x, DenseFeat),
               dnn_feature_columns)) if dnn_feature_columns else []
    varlen_sparse_feature_columns = list(
        filter(lambda x: isinstance(x, VarLenSparseFeat),
               dnn_feature_columns)) if dnn_feature_columns else []

    history_feature_columns = []
    sparse_varlen_feature_columns = []
    history_fc_names = list(map(lambda x: "hist_" + x, history_feature_list))
    for fc in varlen_sparse_feature_columns:
        feature_name = fc.name
        if feature_name in history_fc_names:
            history_feature_columns.append(fc)
        else:
            sparse_varlen_feature_columns.append(fc)

    inputs_list = list(features.values())

    embedding_dict = create_embedding_matrix(dnn_feature_columns,
                                             l2_reg_embedding,
                                             init_std,
                                             seed,
                                             embedding_size,
                                             prefix="")

    query_emb_list = embedding_lookup(embedding_dict, features,
                                      sparse_feature_columns,
                                      history_feature_list,
                                      history_feature_list)  #query是单独的
    keys_emb_list = embedding_lookup(embedding_dict, features,
                                     history_feature_columns, history_fc_names,
                                     history_fc_names)
    dnn_input_emb_list = embedding_lookup(embedding_dict,
                                          features,
                                          sparse_feature_columns,
                                          mask_feat_list=history_feature_list)
    dense_value_list = get_dense_input(features, dense_feature_columns)

    sequence_embed_dict = varlen_embedding_lookup(
        embedding_dict, features, sparse_varlen_feature_columns)
    sequence_embed_list = get_varlen_pooling_list(
        sequence_embed_dict, features, sparse_varlen_feature_columns)
    dnn_input_emb_list += sequence_embed_list

    keys_emb = concat_fun(keys_emb_list, mask=True)
    deep_input_emb = concat_fun(dnn_input_emb_list)
    query_emb = concat_fun(query_emb_list, mask=True)

    hist = AttentionSequencePoolingLayer(
        att_hidden_size,
        att_activation,
        weight_normalization=att_weight_normalization,
        supports_masking=True)([query_emb, keys_emb])

    deep_input_emb = Concatenate()([NoMask()(deep_input_emb), hist])
    deep_input_emb = Flatten()(deep_input_emb)
    dnn_input = combined_dnn_input([deep_input_emb], dense_value_list)
    output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout,
                 dnn_use_bn, seed)(dnn_input)
    final_logit = Dense(1, use_bias=False)(output)

    output = PredictionLayer(task)(final_logit)

    model = Model(inputs=inputs_list, outputs=output)
    return model
Ejemplo n.º 9
0
    def build_explanation_model(self,
                                input_dim,
                                output_dim,
                                loss,
                                downsample_factors=(1, )):
        num_indices, num_channels, steps, downsampling_factor =\
            MaskingUtil.get_input_constants(input_dim, downsample_factors)

        if downsampling_factor != 1 and num_indices is None:
            raise ValueError(
                "Attribution downsampling is not supported for variable length inputs. "
                "Please pad your data samples to the same size to use downsampling."
            )

        input_shape = (input_dim, ) if not isinstance(
            input_dim, collections.Sequence) else input_dim
        input_layer = Input(shape=input_shape)
        last_layer = self.build(input_layer)

        if num_indices is None:
            last_layer = Dense(1, activation="linear")(last_layer)
            last_layer = Flatten()(last_layer)  # None * None outputs
            last_layer = Lambda(
                K.softmax, output_shape=K.int_shape(last_layer))(last_layer)
        else:
            last_layer = Flatten()(last_layer)
            last_layer = Dense(num_indices, activation="softmax")(last_layer)

        # Prepare extra inputs for causal loss.
        all_auxiliary_outputs = Input(shape=(output_dim, ), name="all")
        all_but_one_auxiliary_outputs_input = Input(shape=(num_indices,
                                                           output_dim),
                                                    name="all_but_one")

        if num_indices is not None:
            all_but_one_auxiliary_outputs = Lambda(lambda x: tf.unstack(
                x, axis=1))(all_but_one_auxiliary_outputs_input)
            if K.int_shape(all_but_one_auxiliary_outputs_input)[1] == 1:
                all_but_one_auxiliary_outputs = [all_but_one_auxiliary_outputs]
        else:
            all_but_one_auxiliary_outputs = all_but_one_auxiliary_outputs_input

        causal_loss_fun = partial(
            causal_loss,
            attention_weights=last_layer,
            auxiliary_outputs=all_auxiliary_outputs,
            all_but_one_auxiliary_outputs=all_but_one_auxiliary_outputs,
            loss_function=loss)
        causal_loss_fun.__name__ = "causal_loss"

        if downsampling_factor != 1:
            last_layer = Reshape(tuple(steps) + (1, ))(last_layer)

            if len(steps) == 1:
                # Add a dummy dimension to enable usage of __resize_images__.
                last_layer = Reshape(tuple(steps) + (1, 1))(last_layer)
                last_layer = Lambda(lambda x: resize_images(
                    x,
                    height_factor=downsample_factors[0],
                    width_factor=1,
                    data_format="channels_last"))(last_layer)
            elif len(steps) == 2:
                last_layer = Lambda(lambda x: resize_images(
                    x,
                    height_factor=downsample_factors[0],
                    width_factor=downsample_factors[1],
                    data_format="channels_last"))(last_layer)
            elif len(steps) == 3:
                last_layer = Lambda(lambda x: resize_volumes(
                    x,
                    depth_factor=downsample_factors[0],
                    height_factor=downsample_factors[1],
                    width_factor=downsample_factors[2],
                    data_format="channels_last"))(last_layer)
            else:
                raise ValueError(
                    "Attribution maps of larger dimensionality than 3D data are not currently supported. "
                    "Requested output dim was: {}.".format(len(steps)))

            attribution_shape = Validation.get_attribution_shape_from_input_shape(
                num_samples=1, input_dim=input_dim)[1:]
            collapsed_attribution_shape = (int(np.prod(attribution_shape)), )
            last_layer = Reshape(collapsed_attribution_shape)(last_layer)

            # Re-normalise to sum = 1 after resizing (sum = __downsampling_factor__ after resizing).
            last_layer = Lambda(lambda x: x / float(downsampling_factor))(
                last_layer)

        # We must connect all inputs to the output to bypass a bug in model saving in tf < 1.15.0rc0.
        # For easier handling when calling .fit(), we transform all outputs to be the same shape.
        # See https://github.com/tensorflow/tensorflow/pull/30244
        all_but_one_same_shape_output_layer = Lambda(lambda x: x[:, 0, :])(
            all_but_one_auxiliary_outputs_input)

        model = Model(inputs=[
            input_layer, all_auxiliary_outputs,
            all_but_one_auxiliary_outputs_input
        ],
                      outputs=[
                          last_layer, all_auxiliary_outputs,
                          all_but_one_same_shape_output_layer
                      ])
        model = self.compile_model(model,
                                   main_losses=[causal_loss_fun, "mse", "mse"],
                                   loss_weights=[1.0] * 3,
                                   learning_rate=self.learning_rate,
                                   optimizer=self.optimizer)

        prediction_model = Model(input_layer, last_layer)
        return model, prediction_model
Ejemplo n.º 10
0
def resnet_v2(input_shape, depth, num_classes=10, fused_batch_norm=False):
    """ResNet Version 2 Model builder [b]
    Stacks of (1 x 1)-(3 x 3)-(1 x 1) BN-ReLU-Conv2D or also known as
    bottleneck layer
    First shortcut connection per layer is 1 x 1 Conv2D.
    Second and onwards shortcut connection is identity.
    At the beginning of each stage, the feature map size is halved (downsampled)
    by a convolutional layer with strides=2, while the number of filter maps is
    doubled. Within each stage, the layers have the same number filters and the
    same filter map sizes.
    Features maps sizes:
    conv1  : 32x32,  16
    stage 0: 32x32,  64
    stage 1: 16x16, 128
    stage 2:  8x8,  256
    # Arguments
        input_shape (tensor): shape of input image tensor
        depth (int): number of core convolutional layers
        num_classes (int): number of classes (CIFAR10 has 10)
    # Returns
        model (Model): Keras model instance
    """
    
    if (depth - 2) % 9 != 0:
        raise ValueError('depth should be 9n+2 (eg 56 or 110 in [b])')
    # Start model definition.
    num_filters_in = 16
    num_res_blocks = int((depth - 2) / 9)

    inputs = Input(shape=input_shape)
    # v2 performs Conv2D with BN-ReLU on input before splitting into 2 paths
    x = resnet_layer(inputs=inputs,
                     num_filters=num_filters_in,
                     conv_first=True,
                     fused_batch_norm=fused_batch_norm)

    # Instantiate the stack of residual units
    for stage in range(3):
        for res_block in range(num_res_blocks):
            activation = 'relu'
            batch_normalization = True
            strides = 1
            if stage == 0:
                num_filters_out = num_filters_in * 4
                if res_block == 0:  # first layer and first stage
                    activation = None
                    batch_normalization = False
            else:
                num_filters_out = num_filters_in * 2
                if res_block == 0:  # first layer but not first stage
                    strides = 2    # downsample

            # bottleneck residual unit
            y = resnet_layer(inputs=x,
                             num_filters=num_filters_in,
                             kernel_size=1,
                             strides=strides,
                             activation=activation,
                             batch_normalization=batch_normalization,
                             conv_first=False)
            y = resnet_layer(inputs=y,
                             num_filters=num_filters_in,
                             conv_first=False)
            y = resnet_layer(inputs=y,
                             num_filters=num_filters_out,
                             kernel_size=1,
                             conv_first=False)
            if res_block == 0:
                # linear projection residual shortcut connection to match
                # changed dims
                x = resnet_layer(inputs=x,
                                 num_filters=num_filters_out,
                                 kernel_size=1,
                                 strides=strides,
                                 activation=None,
                                 batch_normalization=False)
            x = keras.layers.add([x, y])

        num_filters_in = num_filters_out

    # Add classifier on top.
    # v2 has BN-ReLU before Pooling
    x = BatchNormalization(fused=fused_batch_norm)(x)
    x = Activation('relu')(x)
    x = AveragePooling2D(pool_size=8)(x)
    y = Flatten()(x)
    outputs = Dense(num_classes,
                    activation='softmax',
                    kernel_initializer='he_normal')(y)

    # Instantiate model.
    model = Model(inputs=inputs, outputs=outputs)
    return model
Ejemplo n.º 11
0
def ResNet50(include_top=True,
             weights='imagenet',
             input_tensor=None,
             input_shape=None,
             pooling=None,
             classes=1000):
    """Instantiates the ResNet50 architecture.

    Optionally loads weights pre-trained
    on ImageNet. Note that when using TensorFlow,
    for best performance you should set
    `image_data_format='channels_last'` in your Keras config
    at ~/.keras/keras.json.

    The model and the weights are compatible with both
    TensorFlow and Theano. The data format
    convention used by the model is the one
    specified in your Keras config file.

    # Arguments
        include_top: whether to include the fully-connected
            layer at the top of the network.
        weights: one of `None` (random initialization),
              'imagenet' (pre-training on ImageNet),
              or the path to the weights file to be loaded.
        input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
            to use as image input for the model.
        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 197.
            E.g. `(200, 200, 3)` would be one valid value.
        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 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.
        classes: optional number of classes to classify images
            into, only to be specified if `include_top` is True, and
            if no `weights` argument is specified.

    # Returns
        A Keras model instance.

    # Raises
        ValueError: in case of invalid argument for `weights`,
            or invalid input shape.
    """
    if not (weights in {'imagenet', None} or os.path.exists(weights)):
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization), `imagenet` '
                         '(pre-training on ImageNet), '
                         'or the path to the weights file to be loaded.')

    if weights == 'imagenet' and include_top and classes != 1000:
        raise ValueError('If using `weights` as imagenet with `include_top`'
                         ' as true, `classes` should be 1000')

    # Determine proper input shape
    input_shape = _obtain_input_shape(input_shape,
                                      default_size=224,
                                      min_size=197,
                                      data_format=K.image_data_format(),
                                      require_flatten=include_top,
                                      weights=weights)

    if input_tensor is None:
        img_input = Input(shape=input_shape)
    else:
        if not K.is_keras_tensor(input_tensor):
            img_input = Input(tensor=input_tensor, shape=input_shape)
        else:
            img_input = input_tensor
    if K.image_data_format() == 'channels_last':
        bn_axis = 3
    else:
        bn_axis = 1

    x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(img_input)
    x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', name='conv1')(x)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)

    x = conv_block(x, 3, [64, 64, 256], stage=2, block='a', strides=(1, 1))
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='b')
    x = identity_block(x, 3, [64, 64, 256], stage=2, block='c')

    x = conv_block(x, 3, [128, 128, 512], stage=3, block='a')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='b')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='c')
    x = identity_block(x, 3, [128, 128, 512], stage=3, block='d')

    x = conv_block(x, 3, [256, 256, 1024], stage=4, block='a')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='b')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='c')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='d')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='e')
    x = identity_block(x, 3, [256, 256, 1024], stage=4, block='f')

    x = conv_block(x, 3, [512, 512, 2048], stage=5, block='a')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='b')
    x = identity_block(x, 3, [512, 512, 2048], stage=5, block='c')

    x = AveragePooling2D((7, 7), name='avg_pool')(x)

    if include_top:
        x = Flatten()(x)
        x = Dense(classes, activation='softmax', name='fc1000')(x)
    else:
        if pooling == 'avg':
            x = GlobalAveragePooling2D()(x)
        elif pooling == 'max':
            x = GlobalMaxPooling2D()(x)

    # Ensure that the model takes into account
    # any potential predecessors of `input_tensor`.
    if input_tensor is not None:
        inputs = get_source_inputs(input_tensor)
    else:
        inputs = img_input
    # Create model.
    model = Model(inputs, x, name='resnet50')

    # load weights
    if weights == 'imagenet':
        if include_top:
            weights_path = get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels.h5',
                WEIGHTS_PATH,
                cache_subdir='models',
                md5_hash='a7b3fe01876f51b976af0dea6bc144eb')
        else:
            weights_path = get_file(
                'resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5',
                WEIGHTS_PATH_NO_TOP,
                cache_subdir='models',
                md5_hash='a268eb855778b3df3c7506639542a6af')
        model.load_weights(weights_path)
        if K.backend() == 'theano':
            layer_utils.convert_all_kernels_in_model(model)
    elif weights is not None:
        model.load_weights(weights)

    return model
Ejemplo n.º 12
0
def classification_small_convnet_model(args, input_shape, num_classes):
  """Convnet classification"""
  model = keras.models.Sequential()
  model.add(keras.layers.InputLayer(input_shape=input_shape))
  model.add(
      _conv2d_layer(args, 32, (3, 3), padding='same'))
  # if args.batch_norm:
    # The default Conv2D data format is 'channels_last' and the
    # default BatchNormalization axes is -1.
    #
    # In batch norm fix a channel and average over the samples
    # and the spatial location. axis = the axis we keep fixed
    # (averaging over the rest), so for 'channels_last' this is
    # the last axis, -1.
  #   model.add(BatchNormalization())
  # model.add(Activation(args.activation))
   ## Aitor
  model.add(Activation(args.activation))
  if args.batch_norm:
    if args.mean_to_zero:
      bconstraint=keras.constraints.MaxNorm(max_value=0)
    else:
      bconstraint=None
    model.add(BatchNormalization(beta_constraint=bconstraint))
  ## Aitor
  model.add(_conv2d_layer(args, 32, (3, 3)))
  # if args.batch_norm:
  #   model.add(BatchNormalization())
  # model.add(Activation(args.activation))
   ## Aitor
  model.add(Activation(args.activation))
  if args.batch_norm:
    if args.mean_to_zero:
      bconstraint=keras.constraints.MaxNorm(max_value=0)
    else:
      bconstraint=None
    model.add(BatchNormalization(beta_constraint=bconstraint))
  ## Aitor
  model.add(MaxPooling2D(pool_size=(2, 2)))
  if args.dropout:
    model.add(Dropout(0.25))

  model.add(_conv2d_layer(args, 64, (3, 3), padding='same'))
  # if args.batch_norm:
  #   model.add(BatchNormalization())
  # model.add(Activation(args.activation))
   ## Aitor
  model.add(Activation(args.activation))
  if args.batch_norm:
    if args.mean_to_zero:
      bconstraint=keras.constraints.MaxNorm(max_value=0)
    else:
      bconstraint=None
    model.add(BatchNormalization(beta_constraint=bconstraint))
  ## Aitor
  model.add(_conv2d_layer(args, 64, (3, 3)))
  # if args.batch_norm:
  #   model.add(BatchNormalization())
  # model.add(Activation(args.activation))
   ## Aitor
  model.add(Activation(args.activation))
  if args.batch_norm:
    if args.mean_to_zero:
      bconstraint=keras.constraints.MaxNorm(max_value=0)
    else:
      bconstraint=None
    model.add(BatchNormalization(beta_constraint=bconstraint))
  ## Aitor
  model.add(MaxPooling2D(pool_size=(2, 2)))
  if args.dropout:
    model.add(Dropout(0.25))

  model.add(Flatten())
  # if args.dense:
  #     model.add(Dense(args.cnn_last_layer, name='dense'))
  #     if args.overparam > 0:
  #         for i in range(args.overparam):
  #             model.add(Dense(args.cnn_last_layer,
  #                             name='dense-overparam{}'.format(i + 1)))
  #     if args.batch_norm:
  #         model.add(BatchNormalization())
  #     model.add(Activation(args.activation))
  #     if args.dropout:
  #         model.add(Dropout(0.5))
  if args.overparam > 0:
    for i in range(args.overparam):
      model.add(
          _dense_layer(
              args, num_classes, name='output-overparam{}'.format(i + 1)))
  logits = _dense_layer(args, num_classes, name='logits')
  model.add(logits)
  model.add(Activation('softmax'))
  return model
def InceptionResNetV2(include_top=True,
                      weights='imagenet',
                      input_tensor=None,
                      input_shape=None,
                      pooling=None,
                      classes=1000):
  """Instantiates the Inception-ResNet v2 architecture.

  Optionally loads weights pre-trained on ImageNet.
  Note that when using TensorFlow, for best performance you should
  set `"image_data_format": "channels_last"` in your Keras config
  at `~/.keras/keras.json`.

  The model and the weights are compatible with TensorFlow, Theano and
  CNTK backends. The data format convention used by the model is
  the one specified in your Keras config file.

  Note that the default input image size for this model is 299x299, instead
  of 224x224 as in the VGG16 and ResNet models. Also, the input preprocessing
  function is different (i.e., do not use `imagenet_utils.preprocess_input()`
  with this model. Use `preprocess_input()` defined in this module instead).

  Arguments:
      include_top: whether to include the fully-connected
          layer at the top of the network.
      weights: one of `None` (random initialization),
            'imagenet' (pre-training on ImageNet),
            or the path to the weights file to be loaded.
      input_tensor: optional Keras tensor (i.e. output of `layers.Input()`)
          to use as image input for the model.
      input_shape: optional shape tuple, only to be specified
          if `include_top` is `False` (otherwise the input shape
          has to be `(299, 299, 3)` (with `'channels_last'` data format)
          or `(3, 299, 299)` (with `'channels_first'` data format).
          It should have exactly 3 inputs channels,
          and width and height should be no smaller than 139.
          E.g. `(150, 150, 3)` would be one valid value.
      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 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.
      classes: optional number of classes to classify images
          into, only to be specified if `include_top` is `True`, and
          if no `weights` argument is specified.

  Returns:
      A Keras `Model` instance.

  Raises:
      ValueError: in case of invalid argument for `weights`,
          or invalid input shape.
  """
  if not (weights in {'imagenet', None} or os.path.exists(weights)):
    raise ValueError('The `weights` argument should be either '
                     '`None` (random initialization), `imagenet` '
                     '(pre-training on ImageNet), '
                     'or the path to the weights file to be loaded.')

  if weights == 'imagenet' and include_top and classes != 1000:
    raise ValueError('If using `weights` as imagenet with `include_top`'
                     ' as true, `classes` should be 1000')

  # Determine proper input shape
  input_shape = _obtain_input_shape(
      input_shape,
      default_size=299,
      min_size=139,
      data_format=K.image_data_format(),
      require_flatten=False,
      weights=weights)

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

  # Stem block: 35 x 35 x 192
  x = conv2d_bn(img_input, 32, 3, strides=2, padding='valid')
  x = conv2d_bn(x, 32, 3, padding='valid')
  x = conv2d_bn(x, 64, 3)
  x = MaxPooling2D(3, strides=2)(x)
  x = conv2d_bn(x, 80, 1, padding='valid')
  x = conv2d_bn(x, 192, 3, padding='valid')
  x = MaxPooling2D(3, strides=2)(x)

  # Mixed 5b (Inception-A block): 35 x 35 x 320
  branch_0 = conv2d_bn(x, 96, 1)
  branch_1 = conv2d_bn(x, 48, 1)
  branch_1 = conv2d_bn(branch_1, 64, 5)
  branch_2 = conv2d_bn(x, 64, 1)
  branch_2 = conv2d_bn(branch_2, 96, 3)
  branch_2 = conv2d_bn(branch_2, 96, 3)
  branch_pool = AveragePooling2D(3, strides=1, padding='same')(x)
  branch_pool = conv2d_bn(branch_pool, 64, 1)
  branches = [branch_0, branch_1, branch_2, branch_pool]
  channel_axis = 1 if K.image_data_format() == 'channels_first' else 3
  x = Concatenate(axis=channel_axis, name='mixed_5b')(branches)

  # 10x block35 (Inception-ResNet-A block): 35 x 35 x 320
  for block_idx in range(1, 11):
    x = inception_resnet_block(
        x, scale=0.17, block_type='block35', block_idx=block_idx)

  # Mixed 6a (Reduction-A block): 17 x 17 x 1088
  branch_0 = conv2d_bn(x, 384, 3, strides=2, padding='valid')
  branch_1 = conv2d_bn(x, 256, 1)
  branch_1 = conv2d_bn(branch_1, 256, 3)
  branch_1 = conv2d_bn(branch_1, 384, 3, strides=2, padding='valid')
  branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x)
  branches = [branch_0, branch_1, branch_pool]
  x = Concatenate(axis=channel_axis, name='mixed_6a')(branches)

  # 20x block17 (Inception-ResNet-B block): 17 x 17 x 1088
  for block_idx in range(1, 21):
    x = inception_resnet_block(
        x, scale=0.1, block_type='block17', block_idx=block_idx)

  # Mixed 7a (Reduction-B block): 8 x 8 x 2080
  branch_0 = conv2d_bn(x, 256, 1)
  branch_0 = conv2d_bn(branch_0, 384, 3, strides=2, padding='valid')
  branch_1 = conv2d_bn(x, 256, 1)
  branch_1 = conv2d_bn(branch_1, 288, 3, strides=2, padding='valid')
  branch_2 = conv2d_bn(x, 256, 1)
  branch_2 = conv2d_bn(branch_2, 288, 3)
  branch_2 = conv2d_bn(branch_2, 320, 3, strides=2, padding='valid')
  branch_pool = MaxPooling2D(3, strides=2, padding='valid')(x)
  branches = [branch_0, branch_1, branch_2, branch_pool]
  x = Concatenate(axis=channel_axis, name='mixed_7a')(branches)

  # 10x block8 (Inception-ResNet-C block): 8 x 8 x 2080
  for block_idx in range(1, 10):
    x = inception_resnet_block(
        x, scale=0.2, block_type='block8', block_idx=block_idx)
  x = inception_resnet_block(
      x, scale=1., activation=None, block_type='block8', block_idx=10)

  # Final convolution block: 8 x 8 x 1536
  x = conv2d_bn(x, 1536, 1, name='conv_7b')

  if include_top:
    # Classification block
    x = GlobalAveragePooling2D(name='avg_pool')(x)
    x = Dense(classes, activation='softmax', name='predictions')(x)
  else:
    if pooling == 'avg':
      x = GlobalAveragePooling2D()(x)
    elif pooling == 'max':
      x = GlobalMaxPooling2D()(x)
    x = Flatten(name='custom')(x) ##DB

  # Ensure that the model takes into account
  # any potential predecessors of `input_tensor`
  if input_tensor is not None:
    inputs = layer_utils.get_source_inputs(input_tensor)
  else:
    inputs = img_input

  # Create model
  model = Model(inputs, x, name='inception_resnet_v2')

  # Load weights
  if weights == 'imagenet':
    if include_top:
      fname = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels.h5'
      weights_path = get_file(
          fname,
          BASE_WEIGHT_URL + fname,
          cache_subdir='models',
          file_hash='e693bd0210a403b3192acc6073ad2e96')
    else:
      fname = 'inception_resnet_v2_weights_tf_dim_ordering_tf_kernels_notop.h5'
      weights_path = get_file(
          fname,
          BASE_WEIGHT_URL + fname,
          cache_subdir='models',
          file_hash='d19885ff4a710c122648d3b5c3b684e4')
    model.load_weights(weights_path)
  elif weights is not None:
    model.load_weights(weights)

  return model
Ejemplo n.º 14
0
def VGG16(save_dir):
    VGG16 = Sequential()
    VGG16.add(
        Conv2D(64, (3, 3),
               strides=(1, 1),
               input_shape=(224, 224, 3),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(64, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(MaxPooling2D(pool_size=(2, 2)))
    VGG16.add(
        Conv2D(128, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(128, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(MaxPooling2D(pool_size=(2, 2)))
    VGG16.add(
        Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(256, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(MaxPooling2D(pool_size=(2, 2)))
    VGG16.add(
        Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(MaxPooling2D(pool_size=(2, 2)))
    VGG16.add(
        Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(
        Conv2D(512, (3, 3),
               strides=(1, 1),
               padding='same',
               activation='relu',
               kernel_initializer='uniform'))
    VGG16.add(MaxPooling2D(pool_size=(2, 2)))
    VGG16.add(Flatten())
    VGG16.add(Dense(4096, activation='relu'))
    VGG16.add(Dropout(0.5))
    VGG16.add(Dense(4096, activation='relu'))
    VGG16.add(Dropout(0.5))
    VGG16.add(Dense(1000, activation='softmax'))
    VGG16.compile(loss='categorical_crossentropy',
                  optimizer='sgd',
                  metrics=['accuracy'])
    VGG16.summary()
    save_dir += 'VGG16.h5'
    return VGG16
Ejemplo n.º 15
0
cnn.add(
    Convolution2D(filtrosConv1,
                  tamano_filtro1,
                  padding="same",
                  input_shape=(altura, longitud, 3),
                  activation='relu'))
cnn.add(MaxPooling2D(pool_size=tamano_pool))

cnn.add(
    Convolution2D(filtrosConv2,
                  tamano_filtro2,
                  padding="same",
                  activation='relu'))
cnn.add(MaxPooling2D(pool_size=tamano_pool))

cnn.add(Flatten())  #imagen profunda y pequeña queda plana
cnn.add(Dense(256, activation='relu')
        )  #despues de aplanar la imformacion es enviada a una capa nueva
cnn.add(Dropout(
    0.5))  #a esta capa densa se le apaga el 50% de 256 neuronas a cada paso.
cnn.add(Dense(
    clases, activation='softmax'))  #esta activacion es la que ayuda a predecir

cnn.compile(loss='categorical_crossentropy',
            optimizer=optimizers.Adam(lr=lr),
            metrics=['accuracy'])

cnn.fit_generator(imagen_entrenamiento,
                  steps_per_epoch=pasos,
                  epochs=epocas,
                  validation_data=imagen_validacion,
Ejemplo n.º 16
0
def DIN(feature_dim_dict,
        seq_feature_list,
        embedding_size=8,
        hist_len_max=16,
        dnn_use_bn=False,
        dnn_hidden_units=(200, 80),
        dnn_activation='relu',
        att_hidden_size=(80, 40),
        att_activation="dice",
        att_weight_normalization=False,
        l2_reg_dnn=0,
        l2_reg_embedding=1e-6,
        dnn_dropout=0,
        init_std=0.0001,
        seed=1024,
        task='binary'):
    """Instantiates the Deep Interest Network architecture.

    :param feature_dim_dict: dict,to indicate sparse field (**now only support sparse feature**)like {'sparse':{'field_1':4,'field_2':3,'field_3':2},'dense':[]}
    :param seq_feature_list: list,to indicate  sequence sparse field (**now only support sparse feature**),must be a subset of ``feature_dim_dict["sparse"]``
    :param embedding_size: positive integer,sparse feature embedding_size.
    :param hist_len_max: positive int, to indicate the max length of seq input
    :param dnn_use_bn: bool. Whether use BatchNormalization before activation or not in deep net
    :param dnn_hidden_units: list,list of positive integer or empty list, the layer number and units in each layer of deep net
    :param dnn_activation: Activation function to use in deep net
    :param att_hidden_size: list,list of positive integer , the layer number and units in each layer of attention net
    :param att_activation: Activation function to use in attention net
    :param att_weight_normalization: bool.Whether normalize the attention score of local activation unit.
    :param l2_reg_dnn: float. L2 regularizer strength applied to DNN
    :param l2_reg_embedding: float. L2 regularizer strength applied to embedding vector
    :param dnn_dropout: float in [0,1), the probability we will drop out a given DNN coordinate.
    :param init_std: float,to use as the initialize std of embedding vector
    :param seed: integer ,to use as random seed.
    :param task: str, ``"binary"`` for  binary logloss or  ``"regression"`` for regression loss
    :return: A Keras model instance.

    """
    check_feature_config_dict(feature_dim_dict)

    sparse_input, dense_input, user_behavior_input = get_input(
        feature_dim_dict, seq_feature_list, hist_len_max)

    sparse_embedding_dict = {
        feat.name:
        Embedding(feat.dimension,
                  embedding_size,
                  embeddings_initializer=RandomNormal(mean=0.0,
                                                      stddev=init_std,
                                                      seed=seed),
                  embeddings_regularizer=l2(l2_reg_embedding),
                  name='sparse_emb_' + str(i) + '-' + feat.name,
                  mask_zero=(feat.name in seq_feature_list))
        for i, feat in enumerate(feature_dim_dict["sparse"])
    }

    query_emb_list = get_embedding_vec_list(sparse_embedding_dict,
                                            sparse_input,
                                            feature_dim_dict['sparse'],
                                            seq_feature_list, seq_feature_list)

    keys_emb_list = get_embedding_vec_list(sparse_embedding_dict,
                                           user_behavior_input,
                                           feature_dim_dict['sparse'],
                                           seq_feature_list, seq_feature_list)

    deep_input_emb_list = get_embedding_vec_list(
        sparse_embedding_dict,
        sparse_input,
        feature_dim_dict['sparse'],
        mask_feat_list=seq_feature_list)

    keys_emb = concat_fun(keys_emb_list)
    deep_input_emb = concat_fun(deep_input_emb_list)

    query_emb = concat_fun(query_emb_list)

    hist = AttentionSequencePoolingLayer(
        att_hidden_size,
        att_activation,
        weight_normalization=att_weight_normalization,
        supports_masking=True)([query_emb, keys_emb])

    deep_input_emb = Concatenate()([NoMask()(deep_input_emb), hist])
    deep_input_emb = Flatten()(deep_input_emb)
    if len(dense_input) > 0:
        deep_input_emb = Concatenate()([deep_input_emb] +
                                       list(dense_input.values()))

    output = DNN(dnn_hidden_units, dnn_activation, l2_reg_dnn, dnn_dropout,
                 dnn_use_bn, seed)(deep_input_emb)
    final_logit = Dense(1, use_bias=False)(output)

    output = PredictionLayer(task)(final_logit)
    model_input_list = get_inputs_list(
        [sparse_input, dense_input, user_behavior_input])

    model = Model(inputs=model_input_list, outputs=output)
    return model
Ejemplo n.º 17
0
def DeepConvNet(nb_classes, Chans = 64, Samples = 256,
                dropoutRate = 0.5):
    """ Keras implementation of the Deep Convolutional Network as described in
    Schirrmeister et. al. (2017), Human Brain Mapping.
    
    This implementation assumes the input is a 2-second EEG signal sampled at 
    128Hz, as opposed to signals sampled at 250Hz as described in the original
    paper. We also perform temporal convolutions of length (1, 5) as opposed
    to (1, 10) due to this sampling rate difference. 
    
    Note that we use the max_norm constraint on all convolutional layers, as 
    well as the classification layer. We also change the defaults for the
    BatchNormalization layer. We used this based on a personal communication 
    with the original authors.
    
                      ours        original paper
    pool_size        1, 2        1, 3
    strides          1, 2        1, 3
    conv filters     1, 5        1, 10
    
    Note that this implementation has not been verified by the original 
    authors. 
    
    """

    # start the model
    input_main   = Input((1, Chans, Samples))
    block1       = Conv2D(25, (1, 5), 
                                 input_shape=(1, Chans, Samples),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(input_main)
    block1       = Conv2D(25, (Chans, 1),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block1)
    block1       = BatchNormalization(axis=1, epsilon=1e-05, momentum=0.1)(block1)
    block1       = Activation('elu')(block1)
    block1       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block1)
    block1       = Dropout(dropoutRate)(block1)
  
    block2       = Conv2D(50, (1, 5),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block1)
    block2       = BatchNormalization(axis=1, epsilon=1e-05, momentum=0.1)(block2)
    block2       = Activation('elu')(block2)
    block2       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block2)
    block2       = Dropout(dropoutRate)(block2)
    
    block3       = Conv2D(100, (1, 5),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block2)
    block3       = BatchNormalization(axis=1, epsilon=1e-05, momentum=0.1)(block3)
    block3       = Activation('elu')(block3)
    block3       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block3)
    block3       = Dropout(dropoutRate)(block3)
    
    block4       = Conv2D(200, (1, 5),
                                 kernel_constraint = max_norm(2., axis=(0,1,2)))(block3)
    block4       = BatchNormalization(axis=1, epsilon=1e-05, momentum=0.1)(block4)
    block4       = Activation('elu')(block4)
    block4       = MaxPooling2D(pool_size=(1, 2), strides=(1, 2))(block4)
    block4       = Dropout(dropoutRate)(block4)
    
    flatten      = Flatten()(block4)
    
    dense        = Dense(nb_classes, kernel_constraint = max_norm(0.5))(flatten)
    softmax      = Activation('softmax')(dense)
    
    return Model(inputs=input_main, outputs=softmax)
Ejemplo n.º 18
0
	def onBeginTraining(self):
		ue.log("starting mnist keras cnn opt training")
		model_file_name = "mnistKerasCNNOpt"
		model_directory = ue.get_content_dir() + "/Scripts/"
		model_sess_path =  model_directory + model_file_name + ".tfsess"
		model_json_path = model_directory + model_file_name + ".json"

		my_file = Path(model_json_path)

		#reset the session each time we get training calls
		K.clear_session()

		#let's train
		batch_size = 2000
		self.batch_size = batch_size
		num_classes = 10
		epochs = 20
		round_values = True

		# input image dimensions
		img_rows, img_cols = 28, 28

		# the data, shuffled and split between train and test sets
		(x_train, y_train), (x_test, y_test) = mnist.load_data()

		if K.image_data_format() == 'channels_first':
			x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
			x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
			input_shape = (1, img_rows, img_cols)
		else:
			x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
			x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
			input_shape = (img_rows, img_cols, 1)

		x_train = x_train.astype('float32')
		x_test = x_test.astype('float32')
		x_train /= 255
		x_test /= 255
		
		#so we train to sets closer to ue4 types
		if(round_values):
			x_train = x_train.round()
			x_test = x_test.round()

		ue.log('x_train shape:' + str(x_train.shape))
		ue.log(str(x_train.shape[0]) + 'train samples')
		ue.log(str(x_test.shape[0]) + 'test samples')

		# convert class vectors to binary class matrices
		y_train = keras.utils.to_categorical(y_train, num_classes)
		y_test = keras.utils.to_categorical(y_test, num_classes)
		
		# create model
		model = Sequential()
		model.add(Conv2D(30, (5, 5), input_shape=input_shape, activation='relu'))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		model.add(Conv2D(15, (3, 3), activation='relu'))
		model.add(MaxPooling2D(pool_size=(2, 2)))
		model.add(Dropout(0.2))
		model.add(Flatten())
		model.add(Dense(128, activation='relu'))
		model.add(Dense(50, activation='relu'))
		model.add(Dense(num_classes, activation='softmax'))

		model.compile(loss='categorical_crossentropy',
					  optimizer='adam',
					  metrics=['accuracy'])

		#pre-fill our callEvent data to minimize setting
		jsonPixels = {}
		size = {'x':28, 'y':28}
		jsonPixels['size'] = size
		self.jsonPixels = jsonPixels
		self.x_train = x_train

		model.fit(x_train, y_train,
				  batch_size=batch_size,
				  epochs=epochs,
				  verbose=1,
				  validation_data=(x_test, y_test),
				  callbacks=[self.stopcallback])
		score = model.evaluate(x_test, y_test, verbose=0)
		ue.log("mnist keras cnn training complete.")
		ue.log('Test loss:' + str(score[0]))
		ue.log('Test accuracy:' + str(score[1]))

		self.session = K.get_session()
		self.model = model

		stored = {'model':model, 'session': self.session}

		#run a test evaluation
		ue.log(x_test.shape)
		result_test = model.predict(np.reshape(x_test[500],(1,28,28,1)))
		ue.log(result_test)

		#flush the architecture model data to disk
		#with open(model_json_path, "w") as json_file:
		#	json_file.write(model.to_json())

		#flush the whole model and weights to disk
		#saver = tf.train.Saver()
		#save_path = saver.save(K.get_session(), model_sess_path)
		#model.save(model_path)

		
		return stored
def loadModel():
    myInput = Input(shape=(96, 96, 3))

    x = ZeroPadding2D(padding=(3, 3), input_shape=(96, 96, 3))(myInput)
    x = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(x)
    x = BatchNormalization(axis=3, epsilon=0.00001, name='bn1')(x)
    x = Activation('relu')(x)
    x = ZeroPadding2D(padding=(1, 1))(x)
    x = MaxPooling2D(pool_size=3, strides=2)(x)
    x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name='lrn_1')(x)
    x = Conv2D(64, (1, 1), name='conv2')(x)
    x = BatchNormalization(axis=3, epsilon=0.00001, name='bn2')(x)
    x = Activation('relu')(x)
    x = ZeroPadding2D(padding=(1, 1))(x)
    x = Conv2D(192, (3, 3), name='conv3')(x)
    x = BatchNormalization(axis=3, epsilon=0.00001, name='bn3')(x)
    x = Activation('relu')(x)
    x = Lambda(lambda x: tf.nn.lrn(x, alpha=1e-4, beta=0.75), name='lrn_2')(x)
    x = ZeroPadding2D(padding=(1, 1))(x)
    x = MaxPooling2D(pool_size=3, strides=2)(x)

    # Inception3a
    inception_3a_3x3 = Conv2D(96, (1, 1), name='inception_3a_3x3_conv1')(x)
    inception_3a_3x3 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3a_3x3_bn1')(inception_3a_3x3)
    inception_3a_3x3 = Activation('relu')(inception_3a_3x3)
    inception_3a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3a_3x3)
    inception_3a_3x3 = Conv2D(128, (3, 3),
                              name='inception_3a_3x3_conv2')(inception_3a_3x3)
    inception_3a_3x3 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3a_3x3_bn2')(inception_3a_3x3)
    inception_3a_3x3 = Activation('relu')(inception_3a_3x3)

    inception_3a_5x5 = Conv2D(16, (1, 1), name='inception_3a_5x5_conv1')(x)
    inception_3a_5x5 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3a_5x5_bn1')(inception_3a_5x5)
    inception_3a_5x5 = Activation('relu')(inception_3a_5x5)
    inception_3a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3a_5x5)
    inception_3a_5x5 = Conv2D(32, (5, 5),
                              name='inception_3a_5x5_conv2')(inception_3a_5x5)
    inception_3a_5x5 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3a_5x5_bn2')(inception_3a_5x5)
    inception_3a_5x5 = Activation('relu')(inception_3a_5x5)

    inception_3a_pool = MaxPooling2D(pool_size=3, strides=2)(x)
    inception_3a_pool = Conv2D(
        32, (1, 1), name='inception_3a_pool_conv')(inception_3a_pool)
    inception_3a_pool = BatchNormalization(
        axis=3, epsilon=0.00001,
        name='inception_3a_pool_bn')(inception_3a_pool)
    inception_3a_pool = Activation('relu')(inception_3a_pool)
    inception_3a_pool = ZeroPadding2D(padding=((3, 4), (3,
                                                        4)))(inception_3a_pool)

    inception_3a_1x1 = Conv2D(64, (1, 1), name='inception_3a_1x1_conv')(x)
    inception_3a_1x1 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3a_1x1_bn')(inception_3a_1x1)
    inception_3a_1x1 = Activation('relu')(inception_3a_1x1)

    inception_3a = concatenate([
        inception_3a_3x3, inception_3a_5x5, inception_3a_pool, inception_3a_1x1
    ],
                               axis=3)

    # Inception3b
    inception_3b_3x3 = Conv2D(96, (1, 1),
                              name='inception_3b_3x3_conv1')(inception_3a)
    inception_3b_3x3 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3b_3x3_bn1')(inception_3b_3x3)
    inception_3b_3x3 = Activation('relu')(inception_3b_3x3)
    inception_3b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3b_3x3)
    inception_3b_3x3 = Conv2D(128, (3, 3),
                              name='inception_3b_3x3_conv2')(inception_3b_3x3)
    inception_3b_3x3 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3b_3x3_bn2')(inception_3b_3x3)
    inception_3b_3x3 = Activation('relu')(inception_3b_3x3)

    inception_3b_5x5 = Conv2D(32, (1, 1),
                              name='inception_3b_5x5_conv1')(inception_3a)
    inception_3b_5x5 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3b_5x5_bn1')(inception_3b_5x5)
    inception_3b_5x5 = Activation('relu')(inception_3b_5x5)
    inception_3b_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3b_5x5)
    inception_3b_5x5 = Conv2D(64, (5, 5),
                              name='inception_3b_5x5_conv2')(inception_3b_5x5)
    inception_3b_5x5 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3b_5x5_bn2')(inception_3b_5x5)
    inception_3b_5x5 = Activation('relu')(inception_3b_5x5)

    inception_3b_pool = Lambda(lambda x: x**2, name='power2_3b')(inception_3a)
    inception_3b_pool = AveragePooling2D(pool_size=(3, 3),
                                         strides=(3, 3))(inception_3b_pool)
    inception_3b_pool = Lambda(lambda x: x * 9,
                               name='mult9_3b')(inception_3b_pool)
    inception_3b_pool = Lambda(lambda x: K.sqrt(x),
                               name='sqrt_3b')(inception_3b_pool)
    inception_3b_pool = Conv2D(
        64, (1, 1), name='inception_3b_pool_conv')(inception_3b_pool)
    inception_3b_pool = BatchNormalization(
        axis=3, epsilon=0.00001,
        name='inception_3b_pool_bn')(inception_3b_pool)
    inception_3b_pool = Activation('relu')(inception_3b_pool)
    inception_3b_pool = ZeroPadding2D(padding=(4, 4))(inception_3b_pool)

    inception_3b_1x1 = Conv2D(64, (1, 1),
                              name='inception_3b_1x1_conv')(inception_3a)
    inception_3b_1x1 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3b_1x1_bn')(inception_3b_1x1)
    inception_3b_1x1 = Activation('relu')(inception_3b_1x1)

    inception_3b = concatenate([
        inception_3b_3x3, inception_3b_5x5, inception_3b_pool, inception_3b_1x1
    ],
                               axis=3)

    # Inception3c
    inception_3c_3x3 = Conv2D(128, (1, 1),
                              strides=(1, 1),
                              name='inception_3c_3x3_conv1')(inception_3b)
    inception_3c_3x3 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3c_3x3_bn1')(inception_3c_3x3)
    inception_3c_3x3 = Activation('relu')(inception_3c_3x3)
    inception_3c_3x3 = ZeroPadding2D(padding=(1, 1))(inception_3c_3x3)
    inception_3c_3x3 = Conv2D(256, (3, 3),
                              strides=(2, 2),
                              name='inception_3c_3x3_conv' +
                              '2')(inception_3c_3x3)
    inception_3c_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_3c_3x3_bn' +
                                          '2')(inception_3c_3x3)
    inception_3c_3x3 = Activation('relu')(inception_3c_3x3)

    inception_3c_5x5 = Conv2D(32, (1, 1),
                              strides=(1, 1),
                              name='inception_3c_5x5_conv1')(inception_3b)
    inception_3c_5x5 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_3c_5x5_bn1')(inception_3c_5x5)
    inception_3c_5x5 = Activation('relu')(inception_3c_5x5)
    inception_3c_5x5 = ZeroPadding2D(padding=(2, 2))(inception_3c_5x5)
    inception_3c_5x5 = Conv2D(64, (5, 5),
                              strides=(2, 2),
                              name='inception_3c_5x5_conv' +
                              '2')(inception_3c_5x5)
    inception_3c_5x5 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_3c_5x5_bn' +
                                          '2')(inception_3c_5x5)
    inception_3c_5x5 = Activation('relu')(inception_3c_5x5)

    inception_3c_pool = MaxPooling2D(pool_size=3, strides=2)(inception_3b)
    inception_3c_pool = ZeroPadding2D(padding=((0, 1), (0,
                                                        1)))(inception_3c_pool)

    inception_3c = concatenate(
        [inception_3c_3x3, inception_3c_5x5, inception_3c_pool], axis=3)

    # inception 4a
    inception_4a_3x3 = Conv2D(96, (1, 1),
                              strides=(1, 1),
                              name='inception_4a_3x3_conv' + '1')(inception_3c)
    inception_4a_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4a_3x3_bn' +
                                          '1')(inception_4a_3x3)
    inception_4a_3x3 = Activation('relu')(inception_4a_3x3)
    inception_4a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4a_3x3)
    inception_4a_3x3 = Conv2D(192, (3, 3),
                              strides=(1, 1),
                              name='inception_4a_3x3_conv' +
                              '2')(inception_4a_3x3)
    inception_4a_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4a_3x3_bn' +
                                          '2')(inception_4a_3x3)
    inception_4a_3x3 = Activation('relu')(inception_4a_3x3)

    inception_4a_5x5 = Conv2D(32, (1, 1),
                              strides=(1, 1),
                              name='inception_4a_5x5_conv1')(inception_3c)
    inception_4a_5x5 = BatchNormalization(
        axis=3, epsilon=0.00001, name='inception_4a_5x5_bn1')(inception_4a_5x5)
    inception_4a_5x5 = Activation('relu')(inception_4a_5x5)
    inception_4a_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4a_5x5)
    inception_4a_5x5 = Conv2D(64, (5, 5),
                              strides=(1, 1),
                              name='inception_4a_5x5_conv' +
                              '2')(inception_4a_5x5)
    inception_4a_5x5 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4a_5x5_bn' +
                                          '2')(inception_4a_5x5)
    inception_4a_5x5 = Activation('relu')(inception_4a_5x5)

    inception_4a_pool = Lambda(lambda x: x**2, name='power2_4a')(inception_3c)
    inception_4a_pool = AveragePooling2D(pool_size=(3, 3),
                                         strides=(3, 3))(inception_4a_pool)
    inception_4a_pool = Lambda(lambda x: x * 9,
                               name='mult9_4a')(inception_4a_pool)
    inception_4a_pool = Lambda(lambda x: K.sqrt(x),
                               name='sqrt_4a')(inception_4a_pool)

    inception_4a_pool = Conv2D(128, (1, 1),
                               strides=(1, 1),
                               name='inception_4a_pool_conv' +
                               '')(inception_4a_pool)
    inception_4a_pool = BatchNormalization(axis=3,
                                           epsilon=0.00001,
                                           name='inception_4a_pool_bn' +
                                           '')(inception_4a_pool)
    inception_4a_pool = Activation('relu')(inception_4a_pool)
    inception_4a_pool = ZeroPadding2D(padding=(2, 2))(inception_4a_pool)

    inception_4a_1x1 = Conv2D(256, (1, 1),
                              strides=(1, 1),
                              name='inception_4a_1x1_conv' + '')(inception_3c)
    inception_4a_1x1 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4a_1x1_bn' +
                                          '')(inception_4a_1x1)
    inception_4a_1x1 = Activation('relu')(inception_4a_1x1)

    inception_4a = concatenate([
        inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1
    ],
                               axis=3)

    # inception4e
    inception_4e_3x3 = Conv2D(160, (1, 1),
                              strides=(1, 1),
                              name='inception_4e_3x3_conv' + '1')(inception_4a)
    inception_4e_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4e_3x3_bn' +
                                          '1')(inception_4e_3x3)
    inception_4e_3x3 = Activation('relu')(inception_4e_3x3)
    inception_4e_3x3 = ZeroPadding2D(padding=(1, 1))(inception_4e_3x3)
    inception_4e_3x3 = Conv2D(256, (3, 3),
                              strides=(2, 2),
                              name='inception_4e_3x3_conv' +
                              '2')(inception_4e_3x3)
    inception_4e_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4e_3x3_bn' +
                                          '2')(inception_4e_3x3)
    inception_4e_3x3 = Activation('relu')(inception_4e_3x3)

    inception_4e_5x5 = Conv2D(64, (1, 1),
                              strides=(1, 1),
                              name='inception_4e_5x5_conv' + '1')(inception_4a)
    inception_4e_5x5 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4e_5x5_bn' +
                                          '1')(inception_4e_5x5)
    inception_4e_5x5 = Activation('relu')(inception_4e_5x5)
    inception_4e_5x5 = ZeroPadding2D(padding=(2, 2))(inception_4e_5x5)
    inception_4e_5x5 = Conv2D(128, (5, 5),
                              strides=(2, 2),
                              name='inception_4e_5x5_conv' +
                              '2')(inception_4e_5x5)
    inception_4e_5x5 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_4e_5x5_bn' +
                                          '2')(inception_4e_5x5)
    inception_4e_5x5 = Activation('relu')(inception_4e_5x5)

    inception_4e_pool = MaxPooling2D(pool_size=3, strides=2)(inception_4a)
    inception_4e_pool = ZeroPadding2D(padding=((0, 1), (0,
                                                        1)))(inception_4e_pool)

    inception_4e = concatenate(
        [inception_4e_3x3, inception_4e_5x5, inception_4e_pool], axis=3)

    # inception5a
    inception_5a_3x3 = Conv2D(96, (1, 1),
                              strides=(1, 1),
                              name='inception_5a_3x3_conv' + '1')(inception_4e)
    inception_5a_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_5a_3x3_bn' +
                                          '1')(inception_5a_3x3)
    inception_5a_3x3 = Activation('relu')(inception_5a_3x3)
    inception_5a_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5a_3x3)
    inception_5a_3x3 = Conv2D(384, (3, 3),
                              strides=(1, 1),
                              name='inception_5a_3x3_conv' +
                              '2')(inception_5a_3x3)
    inception_5a_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_5a_3x3_bn' +
                                          '2')(inception_5a_3x3)
    inception_5a_3x3 = Activation('relu')(inception_5a_3x3)

    inception_5a_pool = Lambda(lambda x: x**2, name='power2_5a')(inception_4e)
    inception_5a_pool = AveragePooling2D(pool_size=(3, 3),
                                         strides=(3, 3))(inception_5a_pool)
    inception_5a_pool = Lambda(lambda x: x * 9,
                               name='mult9_5a')(inception_5a_pool)
    inception_5a_pool = Lambda(lambda x: K.sqrt(x),
                               name='sqrt_5a')(inception_5a_pool)

    inception_5a_pool = Conv2D(96, (1, 1),
                               strides=(1, 1),
                               name='inception_5a_pool_conv' +
                               '')(inception_5a_pool)
    inception_5a_pool = BatchNormalization(axis=3,
                                           epsilon=0.00001,
                                           name='inception_5a_pool_bn' +
                                           '')(inception_5a_pool)
    inception_5a_pool = Activation('relu')(inception_5a_pool)
    inception_5a_pool = ZeroPadding2D(padding=(1, 1))(inception_5a_pool)

    inception_5a_1x1 = Conv2D(256, (1, 1),
                              strides=(1, 1),
                              name='inception_5a_1x1_conv' + '')(inception_4e)
    inception_5a_1x1 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_5a_1x1_bn' +
                                          '')(inception_5a_1x1)
    inception_5a_1x1 = Activation('relu')(inception_5a_1x1)

    inception_5a = concatenate(
        [inception_5a_3x3, inception_5a_pool, inception_5a_1x1], axis=3)

    # inception_5b
    inception_5b_3x3 = Conv2D(96, (1, 1),
                              strides=(1, 1),
                              name='inception_5b_3x3_conv' + '1')(inception_5a)
    inception_5b_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_5b_3x3_bn' +
                                          '1')(inception_5b_3x3)
    inception_5b_3x3 = Activation('relu')(inception_5b_3x3)
    inception_5b_3x3 = ZeroPadding2D(padding=(1, 1))(inception_5b_3x3)
    inception_5b_3x3 = Conv2D(384, (3, 3),
                              strides=(1, 1),
                              name='inception_5b_3x3_conv' +
                              '2')(inception_5b_3x3)
    inception_5b_3x3 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_5b_3x3_bn' +
                                          '2')(inception_5b_3x3)
    inception_5b_3x3 = Activation('relu')(inception_5b_3x3)

    inception_5b_pool = MaxPooling2D(pool_size=3, strides=2)(inception_5a)

    inception_5b_pool = Conv2D(96, (1, 1),
                               strides=(1, 1),
                               name='inception_5b_pool_conv' +
                               '')(inception_5b_pool)
    inception_5b_pool = BatchNormalization(axis=3,
                                           epsilon=0.00001,
                                           name='inception_5b_pool_bn' +
                                           '')(inception_5b_pool)
    inception_5b_pool = Activation('relu')(inception_5b_pool)

    inception_5b_pool = ZeroPadding2D(padding=(1, 1))(inception_5b_pool)

    inception_5b_1x1 = Conv2D(256, (1, 1),
                              strides=(1, 1),
                              name='inception_5b_1x1_conv' + '')(inception_5a)
    inception_5b_1x1 = BatchNormalization(axis=3,
                                          epsilon=0.00001,
                                          name='inception_5b_1x1_bn' +
                                          '')(inception_5b_1x1)
    inception_5b_1x1 = Activation('relu')(inception_5b_1x1)

    inception_5b = concatenate(
        [inception_5b_3x3, inception_5b_pool, inception_5b_1x1], axis=3)

    av_pool = AveragePooling2D(pool_size=(3, 3), strides=(1, 1))(inception_5b)
    reshape_layer = Flatten()(av_pool)
    dense_layer = Dense(128, name='dense_layer')(reshape_layer)
    norm_layer = Lambda(lambda x: tf.math.l2_normalize(x, axis=1),
                        name='norm_layer')(dense_layer)

    # Final Model
    model = Model(inputs=[myInput], outputs=norm_layer)

    home = str(Path.home())
    if not os.path.isfile(home + '/.deepface/weights/openface_weights.h5'):
        print("openface_weights.h5 will be downloaded...")
        url = 'https://drive.google.com/uc?id=1LSe1YCV1x-BfNnfb7DFZTNpv_Q9jITxn'
        output = home + '/.deepface/weights/openface_weights.h5'
        gdown.download(url, output, quiet=False)
    model.load_weights(home + '/.deepface/weights/openface_weights.h5')
    return model
Ejemplo n.º 20
0
def instance_network(shape):
    input_img = Input(shape=(*shape, 1, MAX_INSTANCES), name='fg_input_img')

    input_packed = Lambda(lambda x: pack_instance(x),
                          name='fg_pack_input')(input_img)
    input_img_3 = Lambda(lambda x: tf.tile(x, [1, 1, 1, 3]),
                         name='fg_input_tile')(input_packed)

    # VGG16 without top layers
    VGG_model = applications.vgg16.VGG16(weights='imagenet',
                                         include_top=False,
                                         input_shape=(224, 224, 3))
    model_3 = Model(VGG_model.input,
                    VGG_model.layers[-6].output,
                    name='fg_model_3')(input_img_3)

    # Global features
    conv2d_6 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(2, 2),
                      activation='relu',
                      name='fg_conv2d_6')(model_3)
    batch_normalization_1 = BatchNormalization(
        name='fg_batch_normalization_1')(conv2d_6)
    conv2d_7 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(1, 1),
                      activation='relu',
                      name='fg_conv2d_7')(batch_normalization_1)
    batch_normalization_2 = BatchNormalization(
        name='fg_batch_normalization_2')(conv2d_7)

    conv2d_8 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(2, 2),
                      activation='relu',
                      name='fg_conv2d_8')(batch_normalization_2)
    batch_normalization_3 = BatchNormalization(
        name='fg_batch_normalization_3')(conv2d_8)
    conv2d_9 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(1, 1),
                      activation='relu',
                      name='fg_conv2d_9')(batch_normalization_3)
    batch_normalization_4 = BatchNormalization(
        name='fg_batch_normalization_4')(conv2d_9)

    # Global feature pass back to colorization + classification
    flatten_1 = Flatten(name='fg_flatten_1')(batch_normalization_4)
    dense_1 = Dense(1024, activation='relu', name='fg_dense_1')(flatten_1)
    dense_2 = Dense(512, activation='relu', name='fg_dense_2')(dense_1)
    dense_3 = Dense(256, activation='relu', name='fg_dense_3')(dense_2)
    repeat_vector_1 = RepeatVector(28 * 28, name='fg_repeat_vector_1')(dense_3)
    reshape_1 = Reshape((28, 28, 256), name='fg_reshape_1')(repeat_vector_1)

    # Mid-level features
    conv2d_10 = Conv2D(512, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_10')(model_3)
    batch_normalization_5 = BatchNormalization(
        name='fg_batch_normalization_5')(conv2d_10)
    conv2d_11 = Conv2D(256, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_11')(batch_normalization_5)
    batch_normalization_6 = BatchNormalization(
        name='fg_batch_normalization_6')(conv2d_11)

    # Fusion of (VGG16 -> Mid-level) + (VGG16 -> Global) + Colorization
    concatenate_2 = concatenate([batch_normalization_6, reshape_1],
                                name='fg_concatenate_2')

    conv2d_12 = Conv2D(256, (1, 1),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_12')(concatenate_2)
    conv2d_13 = Conv2D(128, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_13')(conv2d_12)
    up_sampling2d_1 = UpSampling2D(size=(2, 2),
                                   name='fg_up_sampling2d_1',
                                   interpolation='bilinear')(conv2d_13)
    # conv2dt_1 = Conv2DTranspose(64, (4, 4), padding='same', strides=(2, 2), name='fg_conv2dt_1')(conv2d_13)

    conv2d_14 = Conv2D(64, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_14')(up_sampling2d_1)
    conv2d_15 = Conv2D(64, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_15')(conv2d_14)
    up_sampling2d_2 = UpSampling2D(size=(2, 2),
                                   name='fg_up_sampling2d_2',
                                   interpolation='bilinear')(conv2d_15)
    # conv2dt_2 = Conv2DTranspose(32, (4, 4), padding='same', strides=(2, 2), name='fg_conv2dt_2')(conv2d_15)

    conv2d_16 = Conv2D(32, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='fg_conv2d_16')(up_sampling2d_2)
    conv2d_17 = Conv2D(2, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='sigmoid',
                       name='fg_conv2d_17')(conv2d_16)
    up_sampling2d_3 = UpSampling2D(size=(2, 2),
                                   name='fg_up_sampling2d_3')(conv2d_17)

    model_3_unpack = Lambda(lambda x: unpack_instance(x),
                            name='fg_model_3_unpack')(model_3)
    conv2d_11_unpack = Lambda(lambda x: unpack_instance(x),
                              name='fg_conv2d_11_unpack')(conv2d_11)
    conv2d_13_unpack = Lambda(lambda x: unpack_instance(x),
                              name='fg_conv2d_13_unpack')(conv2d_13)
    conv2d_15_unpack = Lambda(lambda x: unpack_instance(x),
                              name='fg_conv2d_15_unpack')(conv2d_15)
    conv2d_17_unpack = Lambda(lambda x: unpack_instance(x),
                              name='fg_conv2d_17_unpack')(conv2d_17)
    up_sampling2d_3_unpack = Lambda(
        lambda x: unpack_instance(x),
        name='up_sampling2d_3_unpack')(up_sampling2d_3)
    generated = Model(inputs=input_img,
                      outputs=[
                          model_3_unpack, conv2d_11_unpack, conv2d_13_unpack,
                          conv2d_15_unpack, conv2d_17_unpack,
                          up_sampling2d_3_unpack
                      ])

    return generated
Ejemplo n.º 21
0
                                                interpolation='bicubic',
                                                class_mode='categorical',
                                                shuffle=False,
                                                batch_size=BATCH_SIZE)
'''
# output the index number of each class
for cls, idx in train_batches_ref.class_indices.items():
    print('Class #{} = {}'.format(idx, cls))

# use pretrained resnet50 and drop the last fully connected layer
net = ResNet50(include_top=False,
               weights='imagenet',
               input_tensor=None,
               input_shape=(IMAGE_SIZE[0], IMAGE_SIZE[1], 3))
x = net.output
x = Flatten()(x)

# add a dropout layer
x = Dropout(0.5)(x)

# add another Dense layer
#x = Dense(2048, activation='relu', name='readToOutput')(x)
#x = Dropout(0.5)(x)

# add a dense layer with softmax
output_layer = Dense(NUM_CLASSES, activation='softmax', name='softmax')(x)

# set the layers to be whether freezed or trained
net_final = Model(inputs=net.input, outputs=output_layer)
for layer in net_final.layers[:FREEZE_LAYERS]:
    layer.trainable = False
Ejemplo n.º 22
0
def fusion_network(shape, batch_size):
    input_img = Input(shape=(*shape, 1), name='input_img')
    input_img_3 = Lambda(lambda x: tf.tile(x, [1, 1, 1, 3]),
                         name='input_tile')(input_img)

    bbox = Input(shape=(4, MAX_INSTANCES), name='bbox')
    mask = Input(shape=(*shape, MAX_INSTANCES), name='mask')

    # VGG16 without top layers
    VGG_model = applications.vgg16.VGG16(weights='imagenet',
                                         include_top=False,
                                         input_shape=(224, 224, 3))
    vgg_model_3_pre = Model(VGG_model.input,
                            VGG_model.layers[-6].output,
                            name='model_3')(input_img_3)
    fg_model_3 = Input(shape=(*vgg_model_3_pre.get_shape().as_list()[1:],
                              MAX_INSTANCES),
                       name='fg_model_3')  # <-
    vgg_model_3 = WeightGenerator(64, batch_size, name='weight_generator_1')(
        [fg_model_3, vgg_model_3_pre, bbox, mask])  # <-

    # Global features
    conv2d_6 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(2, 2),
                      activation='relu',
                      name='conv2d_6')(vgg_model_3)
    batch_normalization_1 = BatchNormalization(
        name='batch_normalization_1')(conv2d_6)
    conv2d_7 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(1, 1),
                      activation='relu',
                      name='conv2d_7')(batch_normalization_1)
    batch_normalization_2 = BatchNormalization(
        name='batch_normalization_2')(conv2d_7)

    conv2d_8 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(2, 2),
                      activation='relu',
                      name='conv2d_8')(batch_normalization_2)
    batch_normalization_3 = BatchNormalization(
        name='batch_normalization_3')(conv2d_8)
    conv2d_9 = Conv2D(512, (3, 3),
                      padding='same',
                      strides=(1, 1),
                      activation='relu',
                      name='conv2d_9')(batch_normalization_3)
    batch_normalization_4 = BatchNormalization(
        name='batch_normalization_4')(conv2d_9)

    # Classification
    flatten_2 = Flatten(name='flatten_2')(batch_normalization_4)
    dense_4 = Dense(4096, activation='relu', name='dense_4')(flatten_2)
    dense_5 = Dense(4096, activation='relu', name='dense_5')(dense_4)
    dense_6 = Dense(1000, activation='softmax', name='dense_6')(dense_5)

    # Global feature pass back to colorization + classification
    flatten_1 = Flatten(name='flatten_1')(batch_normalization_4)
    dense_1 = Dense(1024, activation='relu', name='dense_1')(flatten_1)
    dense_2 = Dense(512, activation='relu', name='dense_2')(dense_1)
    dense_3 = Dense(256, activation='relu', name='dense_3')(dense_2)
    repeat_vector_1 = RepeatVector(28 * 28, name='repeat_vector_1')(dense_3)
    reshape_1 = Reshape((28, 28, 256), name='reshape_1')(repeat_vector_1)

    # Mid-level features
    conv2d_10 = Conv2D(512, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='conv2d_10')(vgg_model_3)
    batch_normalization_5 = BatchNormalization(
        name='batch_normalization_5')(conv2d_10)
    conv2d_11_pre = Conv2D(256, (3, 3),
                           padding='same',
                           strides=(1, 1),
                           activation='relu',
                           name='conv2d_11')(batch_normalization_5)
    fg_conv2d_11 = Input(shape=(*conv2d_11_pre.get_shape().as_list()[1:],
                                MAX_INSTANCES),
                         name='fg_conv2d_11')  # <-
    conv2d_11 = WeightGenerator(32, batch_size, name='weight_generator_2')(
        [fg_conv2d_11, conv2d_11_pre, bbox, mask])  # <-
    batch_normalization_6 = BatchNormalization(
        name='batch_normalization_6')(conv2d_11)

    # Fusion of (VGG16 -> Mid-level) + (VGG16 -> Global) + Colorization
    concatenate_2 = concatenate([batch_normalization_6, reshape_1],
                                name='concatenate_2')

    conv2d_12 = Conv2D(256, (1, 1),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='conv2d_12')(concatenate_2)
    conv2d_13_pre = Conv2D(128, (3, 3),
                           padding='same',
                           strides=(1, 1),
                           activation='relu',
                           name='conv2d_13')(conv2d_12)
    fg_conv2d_13 = Input(shape=(*conv2d_13_pre.get_shape().as_list()[1:],
                                MAX_INSTANCES),
                         name='fg_conv2d_13')  # <-
    conv2d_13 = WeightGenerator(16, batch_size, name='weight_generator_3')(
        [fg_conv2d_13, conv2d_13_pre, bbox, mask])  # <-
    # conv2dt_1 = Conv2DTranspose(64, (4, 4), padding='same', strides=(2, 2), name='conv2dt_1')(conv2d_13)
    up_sampling2d_1 = UpSampling2D(size=(2, 2),
                                   name='up_sampling2d_1',
                                   interpolation='bilinear')(conv2d_13)

    conv2d_14 = Conv2D(64, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='conv2d_14')(up_sampling2d_1)
    conv2d_15_pre = Conv2D(64, (3, 3),
                           padding='same',
                           strides=(1, 1),
                           activation='relu',
                           name='conv2d_15')(conv2d_14)
    fg_conv2d_15 = Input(shape=(*conv2d_15_pre.get_shape().as_list()[1:],
                                MAX_INSTANCES),
                         name='fg_conv2d_15')  # <-
    conv2d_15 = WeightGenerator(16, batch_size, name='weight_generator_4')(
        [fg_conv2d_15, conv2d_15_pre, bbox, mask])  # <-
    # conv2dt_2 = Conv2DTranspose(32, (4, 4), padding='same', strides=(2, 2), name='conv2dt_2')(conv2d_15)
    up_sampling2d_2 = UpSampling2D(size=(2, 2),
                                   name='up_sampling2d_2',
                                   interpolation='bilinear')(conv2d_15)

    conv2d_16 = Conv2D(32, (3, 3),
                       padding='same',
                       strides=(1, 1),
                       activation='relu',
                       name='conv2d_16')(up_sampling2d_2)
    conv2d_17_pre = Conv2D(2, (3, 3),
                           padding='same',
                           strides=(1, 1),
                           activation='sigmoid',
                           name='conv2d_17')(conv2d_16)
    fg_conv2d_17 = Input(shape=(*conv2d_17_pre.get_shape().as_list()[1:],
                                MAX_INSTANCES),
                         name='fg_conv2d_17')  # <-
    conv2d_17 = WeightGenerator(16, batch_size, name='weight_generator_5')(
        [fg_conv2d_17, conv2d_17_pre, bbox, mask])  # <-
    # conv2dt_3 = Conv2DTranspose(2, (4, 4), padding='same', strides=(2, 2), name='conv2dt_3')(conv2d_17)
    up_sampling2d_3 = UpSampling2D(size=(2, 2),
                                   name='up_sampling2d_3',
                                   interpolation='bilinear')(conv2d_17)

    return Model(inputs=[
        input_img, fg_model_3, fg_conv2d_11, fg_conv2d_13, fg_conv2d_15,
        fg_conv2d_17, bbox, mask
    ],
                 outputs=[up_sampling2d_3, dense_6])
Ejemplo n.º 23
0
    def create_VGG_net(self, raw=height, column=width, channel=1):
        print('create VGG model!!')

        inputShape = (raw, column, channel)

        init = 'he_normal'
        # init = 'glorot_normal'
        activation = 'relu'
        keep_prob_conv = 0.25
        keep_prob_dense = 0.5

        chanDim = -1
        classes = 3

        model = Sequential()

        # CONV => RELU => POOL
        model.add(
            Conv2D(32, (3, 3),
                   padding="same",
                   input_shape=inputShape,
                   kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(3, 3)))
        model.add(Dropout(keep_prob_conv))

        # (CONV => RELU) * 2 => POOL
        model.add(Conv2D(64, (3, 3), padding="same", kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(64, (3, 3), padding="same", kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(keep_prob_conv))

        # (CONV => RELU) * 2 => POOL
        model.add(Conv2D(128, (3, 3), padding="same", kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(128, (3, 3), padding="same", kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(keep_prob_conv))

        # (CONV => RELU) * 2 => POOL
        model.add(Conv2D(128, (3, 3), padding="same", kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(Conv2D(128, (3, 3), padding="same", kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization(axis=chanDim))
        model.add(MaxPooling2D(pool_size=(2, 2)))
        model.add(Dropout(keep_prob_conv))

        # first (and only) set of FC => RELU layers
        model.add(Flatten())
        model.add(Dense(1024, kernel_initializer=init))
        model.add(Activation(activation))
        model.add(BatchNormalization())
        model.add(Dropout(keep_prob_dense))

        # softmax classifier
        model.add(Dense(classes))
        model.add(Activation("softmax"))

        # return the constructed network architecture
        self.model = model
Ejemplo n.º 24
0
	def build_autoencoder(self, image_size = 64, max_pool_size = 2, conv_size = 3, channels = 1, code_size = 4):
		autoencoder = None
		# this is our input placeholder
		input_img = Input(shape=(image_size, image_size, channels), name = 'input')
		x = Conv2D(256, (conv_size, conv_size), activation='relu', padding='same')(input_img) # tanh?
		x = MaxPooling2D((max_pool_size, max_pool_size), padding='same')(x)
		x = Conv2D(128, (conv_size, conv_size), activation='relu', padding='same')(x)
		x = MaxPooling2D((max_pool_size, max_pool_size), padding='same')(x)
		x = Conv2D(128, (conv_size, conv_size), activation='relu', padding='same')(x)
		x = MaxPooling2D((max_pool_size, max_pool_size), padding='same')(x)
		#encoded = MaxPooling2D((max_pool_size, max_pool_size), padding='same', name = 'encoded')(x)
	

#		x = Conv2D(16, (conv_size, conv_size), activation='relu', padding='same')(x)
#		x = MaxPooling2D((max_pool_size, max_pool_size), padding='same')(x)
#		x = Conv2D(8, (conv_size, conv_size), activation='relu', padding='same')(x)
#		x = MaxPooling2D((max_pool_size, max_pool_size), padding='same')(x)
#		x = Conv2D(1, (3, 3), activation='relu', padding='same')(x)
#		x = MaxPooling2D((2, 2), padding='same')(x)
#		print x.shape
		x= Flatten()(x)
#		x= Dense(code_size)(x)
#		encoded =  Reshape(target_shape=(4,), name='encoded')(x) # encoded shape should by 2*2*2
		encoded = Dense(code_size, name='encoded')(x) 	


		print  ('encoded shape ', encoded.shape)
	#	x = Reshape(target_shape=(8,8,1))(encoded) #-12
#		x = Dense(code_size, activation='relu')(encoded)
#		x = Reshape(target_shape=(4, 4, 1))(x) #-12
#		x = Conv2D(2, (conv_size, conv_size), activation='relu', padding='same')(x) #-13
#		x = UpSampling2D((max_pool_size, max_pool_size))(x)
#		x = Conv2D(8, (conv_size, conv_size), activation='relu', padding='same')(x)
#		x = UpSampling2D((max_pool_size, max_pool_size))(x)
#		x = Conv2D(8, (conv_size, conv_size), activation='relu', padding='same')(x)
#		x = UpSampling2D((max_pool_size, max_pool_size))(x)
	#	x = Conv2D(8, (conv_size, conv_size), activation='relu', padding='same')(x)
	#	x = UpSampling2D((max_pool_size, max_pool_size))(x)
		ims = 8
		first = True
		x = Dense(int(ims*ims), activation='relu')(encoded)
		x = Reshape(target_shape=(ims, ims, 1))(x) #-12
		while ims!=image_size:
			x = Conv2D(int(ims*ims/2), (conv_size, conv_size), activation='relu', padding='same')(x)
			x = UpSampling2D((max_pool_size, max_pool_size))(x)
			ims = ims*max_pool_size
		decoded = Conv2D(channels, (conv_size, conv_size), activation = 'sigmoid', padding='same', name= 'decoded')(x)
		
		print ('decoded shape ', decoded.shape)

		autoencoder = Model(input_img, decoded)
		autoencoder.compile(optimizer='adam', loss='mean_squared_error')
	#	autoencoder.compile(optimizer='adam', loss='mse', metrics=['accuracy'])
		#autoencoder.summary()

		# Create a separate encoder model
		encoder = Model(input_img, encoded)
		encoder.compile(optimizer='adam', loss='mean_squared_error')
		encoder.summary()

		# Create a separate decoder model
		decoder_inp = Input(shape=(code_size,))
#		decoder_inp = Input(shape=encoded.output_shape)
		enc_layer_idx = self.getLayerIndexByName(autoencoder, 'encoded')
		print ('encoder layer idx ', enc_layer_idx)
		decoder_layer = autoencoder.layers[enc_layer_idx+1](decoder_inp)			
		for i in range(enc_layer_idx+2, len(autoencoder.layers)):
			decoder_layer = autoencoder.layers[i](decoder_layer)
		decoder = Model(decoder_inp, decoder_layer)
		decoder.compile(optimizer='adam', loss='mean_squared_error')
		decoder.summary()

		return autoencoder, encoder, decoder
Ejemplo n.º 25
0
def adversarial_autoencoder(z, x, dropout_rate, dropout, config):
    outputs = {}

    with tf.variable_scope('Encoder'):
        encoder = build_unified_encoder(x.get_shape().as_list(),
                                        config.intermediateResolutions)

        temp_out = x
        for layer in encoder:
            temp_out = layer(temp_out)

    with tf.variable_scope("Bottleneck"):
        intermediate_conv = Conv2D(temp_out.get_shape().as_list()[3] // 8,
                                   1,
                                   padding='same')
        intermediate_conv_reverse = Conv2D(temp_out.get_shape().as_list()[3],
                                           1,
                                           padding='same')
        dropout_layer = Dropout(dropout_rate)
        temp_out = intermediate_conv(temp_out)

        reshape = temp_out.get_shape().as_list()[1:]
        z_layer = Dense(config.zDim)
        dec_dense = Dense(np.prod(reshape))

        outputs['z_'] = z_ = dropout_layer(z_layer(Flatten()(temp_out)),
                                           dropout)
        reshaped = tf.reshape(dropout_layer(dec_dense(z_), dropout),
                              [-1, *reshape])
        temp_out = intermediate_conv_reverse(reshaped)

    with tf.variable_scope('Decoder'):
        decoder = build_unified_decoder(config.outputWidth,
                                        config.intermediateResolutions,
                                        config.numChannels)

        # Decode: z -> x_hat
        for layer in decoder:
            temp_out = layer(temp_out)

        outputs['x_hat'] = temp_out

    # Discriminator
    with tf.variable_scope('Discriminator'):
        discriminator = [
            Dense(50, activation=leaky_relu),
            Dense(50, activation=leaky_relu),
            Dense(1)
        ]

        # fake
        temp_out = z_
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_'] = temp_out

        # real
        temp_out = z
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d'] = temp_out

        # adding noise
        epsilon = tf.random_uniform([config.batchsize, 1],
                                    minval=0.,
                                    maxval=1.)
        outputs['z_hat'] = z_hat = z + epsilon * (z - z_)

        temp_out = z_hat
        for layer in discriminator:
            temp_out = layer(temp_out)
        outputs['d_hat'] = temp_out

    return outputs
Ejemplo n.º 26
0
def cnnlin(xtrain,
           ytrain,
           xtest,
           ytest,
           input_shape,
           num_classes,
           batch_size,
           epochs,
           callbacks,
           ismodelsaved=False,
           tl=False):
    if ismodelsaved == False:
        inputs = Input(shape=input_shape, name='main_input')
        conv_1 = Conv2D(10, (1, 1), padding='same', activation='relu')(inputs)
        conv_2 = Conv2D(10, (1, 2), padding='same', activation='relu')(inputs)
        conv_3 = Conv2D(10, (1, 3), padding='same', activation='relu')(inputs)
        conv_4 = Conv2D(10, (1, 5), padding='same', activation='relu')(inputs)
        #
        conv_output = concatenate([conv_1, conv_2, conv_3, conv_4])
        bn_output = BatchNormalization()(conv_output)
        pooling_output = MaxPool2D(pool_size=(1, 5),
                                   strides=None,
                                   padding='valid')(bn_output)
        flatten_output = Flatten()(pooling_output)
        #
        x = Dense(100, activation='relu')(flatten_output)
        x = Dense(23, activation='relu')(x)
        x = Dropout(0.15)(x)
        predictions = Dense(num_classes, name='main_output')(x)
        #
        cnnlin = Model(inputs, predictions)
        adamopt = tf.keras.optimizers.Adam(lr=1e-4)
        cnnlin.compile(loss='binary_crossentropy',
                       optimizer=adamopt,
                       metrics=['accuracy'])
        #
        history_cnnreplica = cnnlin.fit(xtrain,
                                        ytrain,
                                        batch_size=batch_size,
                                        epochs=20,
                                        verbose=1,
                                        validation_data=(xtest, ytest))
        #
        if True:
            plt.figure()
            plt.plot(history_cnnreplica.history['loss'], label='train loss')
            plt.plot(history_cnnreplica.history['val_loss'], label='test loss')
            plt.title('Learning Curves')
            plt.xlabel('epochs')
            plt.ylabel('loss')
            plt.legend()
            plt.show()
    else:
        if np.cumprod(input_shape)[-1] == 92:
            cnnlin = tf.keras.models.load_model(
                flpath + '/saved_model_4x23/cnnlinn_4x23')
        else:
            if tl:
                cnnlin = tf.keras.models.load_model(
                    flpath + '/saved_model_guideseq_8x23/cnnlinn_8x23')
            else:
                cnnlin = tf.keras.models.load_model(
                    flpath + '/saved_model_crispr_8x23/cnnlinncrispr_8x23')
    p("CNN Lin: Done")
    return cnnlin
Ejemplo n.º 27
0
def default_model(num_action, input_shape, actor_critic='actor'):
    from tensorflow.python.keras.models import Model
    from tensorflow.python.keras.layers import Input, Lambda, Concatenate
    LR = 1e-4  # Lower lr stabilises training greatly
    img_in = Input(shape=input_shape, name='img_in')
    if actor_critic == "actor":
        # Perception

        x = Convolution2D(filters=24,
                          kernel_size=(5, 5),
                          strides=(2, 2),
                          activation='relu')(img_in)
        x = Convolution2D(filters=32,
                          kernel_size=(5, 5),
                          strides=(2, 2),
                          activation='relu')(x)
        x = Convolution2D(filters=64,
                          kernel_size=(5, 5),
                          strides=(2, 2),
                          activation='relu')(x)
        x = Convolution2D(filters=64,
                          kernel_size=(3, 3),
                          strides=(2, 2),
                          activation='relu')(x)
        x = Convolution2D(filters=64,
                          kernel_size=(3, 3),
                          strides=(1, 1),
                          activation='relu')(x)
        x = Flatten(name='flattened')(x)
        s_in = Input(shape=(1, ), name='speed')
        adv_in = Input(shape=(1, ), name='adv')
        old_prediction = Input(shape=(2 * num_action, ),
                               name='old_prediction_input')

        # speed layer
        s = Dense(64)(s_in)
        s = Dropout(0.5)(s)
        s = Activation('relu')(s)
        s = Dense(64)(s)
        s = Dropout(0.5)(s)
        s = Activation('relu')(s)

        # action layer
        o = Concatenate(axis=1)([x, s])
        o = Dense(64)(o)
        o = Dropout(0.5)(o)
        o = Activation('relu')(o)
        mu = Dense(num_action, activation='tanh', name="actor_output_mu")(o)
        sigma = Dense(num_action,
                      activation='softplus',
                      name="actor_output_sigma")(o)
        mu_and_sigma = Concatenate(axis=-1)([mu, sigma])

        model = Model(inputs=[img_in, s_in, adv_in, old_prediction],
                      outputs=mu_and_sigma)
        model.compile(optimizer=Adam(lr=0.002),
                      loss=proximal_policy_optimization_loss_continuous(
                          advantage=adv_in, old_prediction=old_prediction))
        # action, action_matrix, prediction from trial_run
        # reward is a function( angle, throttle)
        return model

    if actor_critic == 'critic':
        # Perception
        x = Convolution2D(filters=24,
                          kernel_size=(5, 5),
                          strides=(2, 2),
                          activation='relu')(img_in)
        x = Convolution2D(filters=32,
                          kernel_size=(5, 5),
                          strides=(2, 2),
                          activation='relu')(x)
        x = Convolution2D(filters=64,
                          kernel_size=(5, 5),
                          strides=(2, 2),
                          activation='relu')(x)
        x = Convolution2D(filters=64,
                          kernel_size=(3, 3),
                          strides=(2, 2),
                          activation='relu')(x)
        x = Convolution2D(filters=64,
                          kernel_size=(3, 3),
                          strides=(1, 1),
                          activation='relu')(x)
        x = Flatten(name='flattened')(x)
        s_in = Input(shape=(1, ), name='speed')

        # speed layer
        s = Dense(64)(s_in)
        s = Dropout(0.5)(s)
        s = Activation('relu')(s)
        s = Dense(64)(s)
        s = Dropout(0.5)(s)
        s = Activation('relu')(s)

        o = Concatenate(axis=1)([x, s])
        o = Dense(64)(o)
        o = Dropout(0.5)(o)
        o = Activation('relu')(o)
        total_reward = Dense(units=1, activation='linear',
                             name='total_reward')(o)
        model = Model(inputs=[img_in, s_in], outputs=total_reward)

        return model
Ejemplo n.º 28
0
def cnnthree(xtrain,
             ytrain,
             xtest,
             ytest,
             input_shape,
             num_classes,
             batch_size,
             epochs,
             callbacks,
             ismodelsaved=False,
             tl=False):
    if ismodelsaved == False:
        cnn3 = Sequential()
        cnn3.add(
            Conv2D(32,
                   kernel_size=(3, 3),
                   activation='relu',
                   input_shape=input_shape))
        cnn3.add(MaxPooling2D(pool_size=(2, 2)))
        cnn3.add(Dropout(0.25))
        cnn3.add(Flatten())
        cnn3.add(Dense(128, activation='relu'))
        cnn3.add(Dropout(0.5))
        cnn3.add(Dense(num_classes, activation='softmax'))
        #
        cnn3.compile(loss=tfkeras.losses.categorical_crossentropy,
                     optimizer=tf.keras.optimizers.RMSprop(0.001, rho=0.9),
                     metrics=['accuracy'])
        #
        history_cnn3 = cnn3.fit(xtrain,
                                ytrain,
                                batch_size=batch_size,
                                epochs=epochs,
                                verbose=0,
                                validation_data=(xtest, ytest),
                                callbacks=callbacks)
        score = cnn3.evaluate(xtest, ytest, verbose=0)
        print('Test loss:', score[0])
        print('Test accuracy:', score[1])
        #
        if True:
            plt.figure()
            plt.plot(history_cnn3.history['loss'], label='train loss')
            plt.plot(history_cnn3.history['val_loss'], label='test loss')
            plt.title('Learning Curves')
            plt.xlabel('epochs')
            plt.ylabel('loss')
            plt.legend()
            plt.show()
    else:
        if np.cumprod(input_shape)[-1] == 92:
            cnn3 = tf.keras.models.load_model(flpath +
                                              '/saved_model_4x23/cnn3_4x23')
        else:
            if tl:
                cnn3 = tf.keras.models.load_model(
                    flpath + '/saved_model_guideseq_8x23/cnn3_8x23')
            else:
                cnn3 = tf.keras.models.load_model(
                    flpath + '/saved_model_crispr_8x23/cnn3crispr_8x23')
    p("CNN3: Done")
    return cnn3
                          1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], X_test.shape[1], X_test.shape[2],
                        1).astype('float32')

X_train /= 255
X_test /= 255

num_of_classes = 10
y_train = np_utils.to_categorical(y_train, num_of_classes)
y_test = np_utils.to_categorical(y_test, num_of_classes)

model = Sequential()
model.add(
    Conv2D(32, (5, 5),
           input_shape=(X_train.shape[1], X_train.shape[2], 1),
           activation='relu'))
model.add(MaxPooling2D())
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D())
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dense(num_of_classes, activation='softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer=Adam(),
              metrics=['accuracy'])
model.fit(X_train, y_train, 200, 10, validation_data=(X_test, y_test))
model.save("../resources/mnist_model2")
metrics = model.evaluate(X_test, y_test, verbose=0)
Ejemplo n.º 30
0
input_img = Input(shape=x_train.shape[1:])  # (32, 32, 3)


def inception(input):
    pool_1 = AveragePooling2D((3, 3), strides=(1, 1), padding='same')(input)
    conv_1 = Conv2D(64, (1, 1), padding='same', activation='relu')(pool_1)
    conv_2 = Conv2D(64, (1, 1), padding='same', activation='relu')(input)
    conv_3 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_2)
    conv_4 = Conv2D(64, (3, 3), padding='same', activation='relu')(conv_3)
    concat = concatenate([conv_1, conv_2, conv_3, conv_4], axis=3)
    return concat


concat_1 = inception(input_img)

output = Flatten()(concat_1)
out = Dense(10, activation='softmax')(output)

from tensorflow.python.keras.models import Model

model = Model(inputs=input_img, outputs=out)

model.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model.summary()

from tensorflow.python.keras.models import load_model
from keras.callbacks import ModelCheckpoint