Пример #1
0
categorical_inputs = []
for cat in cat_cols:
    categorical_inputs.append(Input(shape=[1], name=cat))

categorical_embeddings = []
for i, cat in enumerate(cat_cols):
    categorical_embeddings.append(Embedding(embed_sizes[i], 10)(categorical_inputs[i]))

#categorical_logits = Concatenate()([Flatten()(cat_emb) for cat_emb in categorical_embeddings])
categorical_logits = Flatten()(categorical_embeddings[0])
categorical_logits = Dense(32,activation='relu')(categorical_logits)

numerical_inputs = Input(shape=(11,), name='num')
numerical_logits = numerical_inputs
numerical_logits = BatchNormalization()(numerical_logits)

numerical_logits = Dense(128,activation='relu')(numerical_logits)
numerical_logits = Dense(64,activation='relu')(numerical_logits)

logits = Concatenate()([numerical_logits,categorical_logits])
logits = Dense(64,activation='relu')(logits)
out = Dense(1, activation='sigmoid')(logits)

model = Model(inputs = categorical_inputs + [numerical_inputs], outputs=out)
model.compile(optimizer='adam',loss=binary_crossentropy)

# In[ ]:


# Lets print our model
model = Sequential()
model.add(Embedding(output_dim=32,  # 词向量的维度
                    input_dim=2000,  # Size of the vocabulary 字典大小
                    input_length=50  # 每个数字列表的长度
                    )
          )

model.add(Conv1D(256,  # 输出大小
                 3,  # 卷积核大小
                 padding='same',
                 activation='relu'))
model.add(MaxPool1D(3, 3, padding='same'))
model.add(Conv1D(32, 3, padding='same', activation='relu'))
model.add(Flatten())
model.add(Dropout(0.3))
model.add(BatchNormalization())  # (批)规范化层
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(units=10,
                activation="softmax"))

batch_size = 256
epochs = 5

# 单GPU版本
model.summary()  # 可视化模型
model.compile(loss="sparse_categorical_crossentropy",  # 多分类
              optimizer="adam",
              metrics=["accuracy"])

history = model.fit(
Пример #3
0
def cvbn(in_layer, name=None, idx=None, fs=None, act=None, size=3, stride=1, dilation=1):
    x=cv(in_layer, name+'_cv', idx, fs, act, size, stride, dilation)
    return BatchNormalization(name=name)(x)
Пример #4
0
def convolutional_block(X, f, filters, stage, block, s=2):
    """
    Implementation of the ResNet convolutional block.
    
    Arguments:
    X -- input tensor of shape (m, n_H_prev, n_W_prev, n_C_prev)
    f -- integer, specifying the shape of the middle CONV's window for the main path
    filters -- python list of integers, defining the number of filters in the CONV layers of the main path
    stage -- integer, used to name the layers, depending on their position in the network
    block -- string/character, used to name the layers, depending on their position in the network
    s -- Integer, specifying the stride to be used
    
    Returns:
    X -- output of the convolutional block, tensor of shape (n_H, n_W, n_C)
    """

    # defining name basis
    conv_name_base = 'res' + str(stage) + block + '_branch'
    bn_name_base = 'bn' + str(stage) + block + '_branch'

    # Retrieve Filters
    F1, F2, F3 = filters

    # Save the input value
    X_shortcut = X

    ##### MAIN PATH #####
    # First component of main path
    X = Conv2D(F1, (1, 1),
               strides=(s, s),
               name=conv_name_base + '2a',
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2a')(X)
    X = Activation('relu')(X)

    # Second component of main path (≈3 lines)
    X = Conv2D(filters=F2,
               kernel_size=(f, f),
               strides=(1, 1),
               padding='same',
               name=conv_name_base + '2b',
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2b')(X)
    X = Activation('relu')(X)

    # Third component of main path (≈2 lines)
    X = Conv2D(filters=F3,
               kernel_size=(1, 1),
               strides=(1, 1),
               padding='valid',
               name=conv_name_base + '2c',
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name=bn_name_base + '2c')(X)

    ##### SHORTCUT PATH #### (≈2 lines)
    X_shortcut = Conv2D(filters=F3,
                        kernel_size=(1, 1),
                        strides=(s, s),
                        padding='valid',
                        name=conv_name_base + '1',
                        kernel_initializer=glorot_uniform(seed=0))(X_shortcut)
    X_shortcut = BatchNormalization(axis=3,
                                    name=bn_name_base + '1')(X_shortcut)

    # Final step: Add shortcut value to main path, and pass it through a RELU activation (≈2 lines)
    X = Add()([X, X_shortcut])
    X = Activation('relu')(X)

    return X
    def __init__(self,
                 saving_path,
                 input_shape,
                 cnn_layers,
                 a_filters,
                 a_strides,
                 dropouts,
                 kernel_sizes,
                 rnn_dropouts,
                 alg_name=None,
                 tag=None,
                 early_stopping=False,
                 check_point=False):

        super().__init__(alg_name=alg_name,
                         tag=tag,
                         early_stopping=early_stopping,
                         check_point=check_point,
                         saving_path=saving_path)

        if cnn_layers != len(a_filters) or cnn_layers != len(
                a_strides) or cnn_layers != len(
                    rnn_dropouts) or cnn_layers != len(dropouts):
            print(
                '|--- [ConvLSTM] Error: size of filters and/or strides need to be equal to the cnn_layers!'
            )
            exit(-1)

        self.cnn_layers = cnn_layers
        self.a_filters = a_filters
        self.a_strides = a_strides
        self.dropout = dropouts
        self.rnn_dropout = rnn_dropouts
        self.kernel_sizes = kernel_sizes

        self.n_timsteps = input_shape[0]
        self.wide = input_shape[1]
        self.high = input_shape[2]
        self.channel = input_shape[3]
        self.saving_path = saving_path
        if not os.path.exists(self.saving_path):
            os.makedirs(self.saving_path)

        input = Input(shape=(self.n_timsteps, self.wide, self.high,
                             self.channel),
                      name='input')

        fw_lstm_layer1 = ConvLSTM2D(filters=self.a_filters[0],
                                    kernel_size=self.kernel_sizes[0],
                                    strides=[1, 1],
                                    padding='same',
                                    dropout=self.dropout[0],
                                    return_sequences=True,
                                    recurrent_dropout=self.rnn_dropout[0],
                                    data_format='channels_last')(input)

        fw_BatchNormalization_layer1 = BatchNormalization()(fw_lstm_layer1)

        fw_lstm_layer2 = ConvLSTM2D(
            filters=self.a_filters[1],
            kernel_size=self.kernel_sizes[1],
            strides=[1, 1],
            padding='same',
            dropout=self.dropout[1],
            return_sequences=True,
            recurrent_dropout=self.rnn_dropout[1],
            data_format='channels_last')(fw_BatchNormalization_layer1)

        fw_BatchNormalization_layer2 = BatchNormalization()(fw_lstm_layer2)

        fw_flat_layer = TimeDistributed(
            Flatten())(fw_BatchNormalization_layer2)

        fw_first_Dense = TimeDistributed(Dense(512, ))(fw_flat_layer)
        fw_second_Dense = TimeDistributed(Dense(256, ))(fw_first_Dense)
        fw_outputs = TimeDistributed(Dense(144, ))(fw_second_Dense)

        fw_outs = Flatten()(fw_outputs)
        fw_outs = Dense(512, )(fw_outs)
        fw_outs = Dropout(0.2)(fw_outs)
        fw_outs = Dense(256, )(fw_outs)
        fw_outs = Dropout(0.2)(fw_outs)
        fw_outs = Dense(144, name='pred_data')(fw_outs)

        # ------------------------------- bw net -----------------------------------------------------------------------
        bw_lstm_layer1 = ConvLSTM2D(filters=self.a_filters[0],
                                    kernel_size=self.kernel_sizes[0],
                                    strides=[1, 1],
                                    padding='same',
                                    dropout=self.dropout[0],
                                    return_sequences=True,
                                    recurrent_dropout=self.rnn_dropout[0],
                                    data_format='channels_last',
                                    go_backwards=True)(input)

        bw_BatchNormalization_layer1 = BatchNormalization()(bw_lstm_layer1)

        bw_lstm_layer2 = ConvLSTM2D(
            filters=self.a_filters[1],
            kernel_size=self.kernel_sizes[1],
            strides=[1, 1],
            padding='same',
            dropout=self.dropout[1],
            return_sequences=True,
            recurrent_dropout=self.rnn_dropout[1],
            data_format='channels_last')(bw_BatchNormalization_layer1)

        bw_BatchNormalization_layer2 = BatchNormalization()(bw_lstm_layer2)

        bw_flat_layer = TimeDistributed(
            Flatten())(bw_BatchNormalization_layer2)

        bw_first_Dense = TimeDistributed(Dense(512, ))(bw_flat_layer)
        bw_second_Dense = TimeDistributed(Dense(256, ))(bw_first_Dense)
        bw_outputs = TimeDistributed(Dense(144, ))(bw_second_Dense)

        # ------------------------------------ Data correction ---------------------------------------------------------

        fw_outputs_flatten = Flatten()(fw_outputs)
        bw_outputs_flatten = Flatten()(bw_outputs)
        input_flatten = Flatten()(input)

        input_concate = Concatenate(axis=1)(
            [input_flatten, fw_outputs_flatten, bw_outputs_flatten])

        corr_data = Dense(1024, )(input_concate)
        corr_data = Dropout(0.2)(corr_data)
        corr_data = Dense(512, )(corr_data)
        corr_data = Dropout(0.2)(corr_data)
        corr_data = Dense(256, )(corr_data)
        corr_data = Dropout(0.2)(corr_data)
        corr_data = Dense(self.wide * self.high *
                          (self.n_timsteps - 2), )(corr_data)

        corr_data = Reshape((self.n_timsteps - 2, self.wide * self.high),
                            name='corr_data')(corr_data)

        self.model = Model(inputs=input,
                           outputs=[fw_outs, corr_data],
                           name='fwbw-conv-lstm')

        self.model.compile(loss={
            'pred_data': 'mse',
            'corr_data': 'mse'
        },
                           optimizer='adam',
                           metrics=['mse', 'mae'])
Пример #6
0
start = 700
end = 1600
last = end - p_size
timepoints = range(start, last, bin_size)
steps = len(timepoints)

heatmap = np.zeros((steps, steps))
scores = np.zeros((steps, n_models))

# create net
m_in = Input(shape=(p_size, 31))
# m_off = Lambda(offset_slice)(m_in)
# m_noise = GaussianNoise(np.std(x[0][0] / 100))(m_off) # how much noice to have????

m_t = Conv1D(30, 64, padding='causal')(m_in)
m_t = BatchNormalization()(m_t)
m_t = ELU()(m_t)
m_t = AveragePooling1D(2)(m_t)
m_t = Dropout(0.2)(m_t)

m_t = Conv1D(15, 32, padding='causal')(m_t)
m_t = BatchNormalization()(m_t)
m_t = ELU()(m_t)
m_t = AveragePooling1D(2)(m_t)
m_t = Dropout(0.3)(m_t)

m_t = Conv1D(10, 16, padding='causal')(m_t)
m_t = BatchNormalization()(m_t)
m_t = ELU()(m_t)
m_t = AveragePooling1D(2)(m_t)
m_t = Dropout(0.4)(m_t)
Пример #7
0
def build_patch_model_resnet(patch_size=8,
                             use_stn=True,
                             stn_weight=None,
                             l2_reg=1e-4,
                             patch_scheme='random',
                             num_patches_total=16,
                             use_batchnorm=False):
    """
    Build PatchNet with ResNet blocks.

    # Arguments
        patch_size (int): height and width of the patch
        use_stn (bool): whether to use STN before PatchNet. If True, the
            pretrained weights must be provided as stn_weight
        stn_weight (np.array): STN weights, required if use_stn is True
        l2_reg (float): l2 weight regularization constant
        patch_scheme (str): must be one of the followings
            'no-overlap' (image is splitted into a non-overlapping grid;
                          'valid' padding),
            'random' (patches are chosen randomly; 'same' padding),
            'all' (all pixels are used as center of a patch; 'same' padding)
        num_patches_total (int): the number of total patches to use, required
            if patch_scheme is 'random'

    # Returns
        model (keras model): PatchNet as uncompiled keras model
    """

    x = Input(shape=[HEIGHT, WIDTH, CHANNEL])
    # scale to [-1, 1]
    v = Lambda(lambda x: x * 2 - 1., output_shape=(HEIGHT, WIDTH, CHANNEL))(x)
    if use_stn:
        v = SpatialTransformer(localization_net=locnet_v3(),
                               output_size=(HEIGHT, WIDTH),
                               trainable=False,
                               weights=stn_weight)(v)

    if patch_scheme == 'no-overlap':
        num_patches = HEIGHT // patch_size
        num_patches_total = num_patches**2
    elif patch_scheme == 'random':
        random_crop_layer = [RandomCropLayer(patch_size)]
    elif patch_scheme == 'all':
        num_patches_total = HEIGHT * WIDTH
        v = ZeroPadding2D(padding=(patch_size // 2, patch_size // 2))(v)
    else:
        raise ValueError("patch_scheme must be one of the followings:" +
                         "'no-overlap', 'random', 'all'")

    # Create the patch network
    layers_list = build_resnet_v2(20, l2_reg=l2_reg)

    output = []
    for i in range(num_patches_total):
        if patch_scheme == 'no-overlap':
            h = i // num_patches
            w = i % num_patches
            top_crop = h * patch_size
            bottom_crop = HEIGHT - top_crop - patch_size
            left_crop = w * patch_size
            right_crop = WIDTH - left_crop - patch_size
            u = Cropping2D(
                ((top_crop, bottom_crop), (left_crop, right_crop)))(v)
        elif patch_scheme == 'random':
            u = apply_layers(v, random_crop_layer)
        elif patch_scheme == 'all':
            top_crop = i // HEIGHT
            left_crop = i % WIDTH
            bottom_crop = HEIGHT - top_crop - (patch_size % 2)
            right_crop = WIDTH - left_crop - (patch_size % 2)
            u = Cropping2D(
                ((top_crop, bottom_crop), (left_crop, right_crop)))(v)
        # Separate batch norm for each patch
        if use_batchnorm:
            u = BatchNormalization()(u)
        u = apply_resnet(u, layers_list)
        output.append(u)

    merge = Concatenate()(output)
    reshape = Reshape([num_patches_total, NUM_CLASSES])(merge)
    mean = Lambda(lambda x: tf.reduce_mean(x, 1),
                  output_shape=(NUM_CLASSES, ))(reshape)
    model = keras.models.Model(inputs=x, outputs=mean)
    model_map = keras.models.Model(inputs=x, outputs=reshape)

    return model, model_map
Пример #8
0
    def individual_to_keras_model(self, individual, child_number=0):
        """
        creates a keras model based on an individual

        Parameters
        ----------
        individual: Individual
            instance of an individual where an keras model should be created
        child_number: int
            a valid index or id

        Returns
        -------
            an individual if a model could be translated otherwise None
        """
        try:
            if not isinstance(individual, Individual):
                raise TypeError("Individual must be the type Individual")

            from keras.layers import Input, MaxPool2D
            from keras.layers.merge import _Merge
            import tensorflow as tf

            active_nodes = np.where(individual.active)[0]

            nodes = {}
            for i in range(individual.config.num_input):
                node = Input(shape=self.input_shape, name='input-%d' % i)
                nodes[i] = node

            outputs = []
            for idx in active_nodes:
                n = individual.genes[idx]

                if isinstance(n, FunctionGen):
                    nodes[idx + individual.config.
                          num_input] = individual.config.functions[n.fnc_idx]
                elif isinstance(n, OutputGen):
                    outputs.append(idx)

            for idx in active_nodes:
                if idx >= individual.config.num_nodes:
                    continue

                node = self.name_to_layer(
                    nodes[idx + individual.config.num_input], idx)

                if isinstance(
                        node,
                        _Merge) and individual.genes[idx].num_inputs == 2:
                    x = []
                    shapes = []
                    for con in range(individual.genes[idx].num_inputs):
                        instance = nodes[individual.genes[idx].inputs[con]]
                        x.append(instance)
                        shapes.append(instance.shape.as_list())

                    _, a_width, a_height, a_channels = shapes[0]
                    _, b_width, b_height, b_channels = shapes[1]

                    if a_width > b_width:
                        x[0] = MaxPooling2D(pool_size=(a_height // b_height,
                                                       a_width // b_width))(
                                                           x[0])
                    if a_width < b_width:
                        x[1] = MaxPooling2D(pool_size=(b_height // a_height,
                                                       b_width // a_width))(
                                                           x[1])

                    if a_channels > b_channels:
                        diff = a_channels - b_channels
                        x[1] = PadZeros(diff, name='pad_%d' % idx)(x[1])
                        #x[1] = Conv2D(a_channels, kernel_size=1, padding='same', activation='relu',
                        #              kernel_initializer='he_uniform', name='pad_%d' % idx)(x[1])
                    elif a_channels < b_channels:
                        diff = b_channels - a_channels
                        x[0] = PadZeros(diff, name='pad_%d' % idx)(x[0])
                        #x[0] = Conv2D(b_channels, kernel_size=1, padding='same', activation='relu',
                        #              kernel_initializer='he_uniform', name='pad_%d' % idx)(x[0])

                elif individual.genes[idx].num_inputs == 1:
                    x = nodes[individual.genes[idx].inputs[0]]

                if self.add_batch_norm and isinstance(node, Conv2D):
                    x = node(x)
                    x = BatchNormalization(axis=-1,
                                           name='%s_bn' % node.name)(x)
                    x = Activation('relu', name='%s_act' % node.name)(x)
                else:
                    x = node(x)

                nodes[idx + individual.config.num_input] = x

            keys = list(nodes.keys())
            inputs = [nodes[i] for i in keys[:individual.config.num_input]]
            outputs = []
            for out in [
                    nodes[i]
                    for i in sorted(keys)[-individual.config.num_output:]
            ]:
                x = self.trainer.append_output_layer(out)
                outputs.append(x)

            model = Model(inputs=inputs,
                          outputs=outputs,
                          name='child-%d' % child_number)

            return model
        except Exception as ex:
            warnings.warn("can't build model:\n%s" % ex)
            return None
Пример #9
0
for file in tqdm(uni):
    img = cv2.imread(DATA_PATH+"/train/Uninfected/{}".format(file))
    img = cv2.resize(img,dsize=(130,130),interpolation=cv2.INTER_AREA)
    cv2.imwrite(os.getcwd()+"/train/0/{}".format(file),img)


# Deep Neural Network

# In[ ]:


model = Sequential()
model.add(Conv2D(filters=64,kernel_size=(5,5),activation='relu',input_shape=IMG_SIZE))
model.add(Conv2D(filters=32,kernel_size=(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())
model.add(Conv2D(filters=16,kernel_size=(5,5),activation='relu'))
model.add(Conv2D(filters=8,kernel_size=(5,5),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())
model.add(Dropout(0.5))
model.add(Conv2D(filters=4,kernel_size=(3,3),activation='relu'))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(BatchNormalization())

model.add(Flatten())
model.add(Dense(256,activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(1,activation='sigmoid'))
 
Пример #10
0
def ResNet50(input_shape=(1024, 1024, 3), classes=4):

    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)

    # Stage 1
    X = Conv2D(64, (7, 7),
               strides=(2, 2),
               name='conv1',
               kernel_initializer=glorot_uniform(seed=0))(X)
    X = BatchNormalization(axis=3, name='bn_conv1')(X)
    X = Activation('relu')(X)
    X = MaxPooling2D((3, 3), strides=(2, 2))(X)

    # Stage 2
    X = convolutional_block(X,
                            f=3,
                            filters=[64, 64, 256],
                            stage=2,
                            block='a',
                            s=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')

    # Stage 3
    X = convolutional_block(X,
                            f=3,
                            filters=[128, 128, 512],
                            stage=3,
                            block='a',
                            s=2)
    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')

    # Stage 4
    X = convolutional_block(X,
                            f=3,
                            filters=[256, 256, 1024],
                            stage=4,
                            block='a',
                            s=2)
    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')

    # Stage 5
    X = convolutional_block(X,
                            f=3,
                            filters=[512, 512, 2048],
                            stage=5,
                            block='a',
                            s=2)
    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='b')
    X = identity_block(X, 3, [512, 512, 2048], stage=5, block='c')

    # AVGPOOL
    X = AveragePooling2D(pool_size=(2, 2), padding='same')(X)

    # Output layer
    X = Flatten()(X)
    X = Dense(classes,
              activation='softmax',
              name='fc' + str(classes),
              kernel_initializer=glorot_uniform(seed=0))(X)

    # Create model
    model = Model(inputs=X_input, outputs=X, name='ResNet50')

    return model
Пример #11
0
def createModel(patchSize, numClasses):

    if K.image_data_format() == 'channels_last':
        bn_axis = -1
    else:
        bn_axis = 1

    input_tensor = Input(shape=(patchSize[0], patchSize[1], 1))

    # first conv layer
    x = Conv2D(32, (7, 7),
               strides=(2, 2),
               padding='same',
               kernel_initializer='he_normal',
               name='conv1')(input_tensor)
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)
    x = Activation('relu')(x)

    # first stage of 3 bottleneck layers
    x = projection_bottleneck_block(x, (16, 16, 64),
                                    stage=1,
                                    block=1,
                                    se_enabled=True,
                                    se_ratio=4)
    x = identity_bottleneck_block(x, (16, 16, 64),
                                  stage=1,
                                  block=2,
                                  se_enabled=True,
                                  se_ratio=4)
    x = identity_bottleneck_block(x, (16, 16, 64),
                                  stage=1,
                                  block=3,
                                  se_enabled=True,
                                  se_ratio=4)

    # second stage of 4 bottleneck layers
    x = projection_bottleneck_block(x, (32, 32, 128),
                                    stage=2,
                                    block=1,
                                    se_enabled=True,
                                    se_ratio=8)
    x = identity_bottleneck_block(x, (32, 32, 128),
                                  stage=2,
                                  block=2,
                                  se_enabled=True,
                                  se_ratio=8)
    x = identity_bottleneck_block(x, (32, 32, 128),
                                  stage=2,
                                  block=3,
                                  se_enabled=True,
                                  se_ratio=8)
    x = identity_bottleneck_block(x, (32, 32, 128),
                                  stage=2,
                                  block=4,
                                  se_enabled=True,
                                  se_ratio=8)

    # third stage of 4 bottleneck layers
    x = projection_bottleneck_block(x, (64, 64, 256),
                                    stage=3,
                                    block=1,
                                    se_enabled=True,
                                    se_ratio=16)
    x = identity_bottleneck_block(x, (64, 64, 256),
                                  stage=3,
                                  block=2,
                                  se_enabled=True,
                                  se_ratio=16)
    x = identity_bottleneck_block(x, (64, 64, 256),
                                  stage=3,
                                  block=3,
                                  se_enabled=True,
                                  se_ratio=16)
    x = identity_bottleneck_block(x, (64, 64, 256),
                                  stage=3,
                                  block=4,
                                  se_enabled=True,
                                  se_ratio=16)

    # fourth stage of 3 bottleneck layers, without SE blocks due to performance considerations (see original paper)
    x = projection_bottleneck_block(x, (128, 128, 512),
                                    stage=4,
                                    block=1,
                                    se_enabled=True,
                                    se_ratio=32)
    x = identity_bottleneck_block(x, (128, 128, 512),
                                  stage=4,
                                  block=2,
                                  se_enabled=True,
                                  se_ratio=32)
    x = identity_bottleneck_block(x, (128, 128, 512),
                                  stage=4,
                                  block=3,
                                  se_enabled=True,
                                  se_ratio=32)

    # global average pooling
    x = GlobalAveragePooling2D(data_format='channels_last')(x)

    # fully-connected layer
    output = Dense(units=numClasses,
                   activation='softmax',
                   kernel_initializer='he_normal',
                   name='fully-connected')(x)

    # create model
    sModelName = 'SE-ResNet-44'
    cnn = Model(input_tensor, output, name=sModelName)

    return cnn, sModelName
Пример #12
0
from keras.optimizers import SGD
from keras.models import Model
from keras.layers import Dense , Conv2D , MaxPooling2D , Input , Reshape
from keras.layers import BatchNormalization , Dropout , regularizers , Flatten , Activation , GlobalAveragePooling2D
from keras.optimizers import Adam , Adadelta , RMSprop , SGD
import matplotlib.pyplot as plt
import numpy as np
size=96
train_x = np.load("train.npy")
train_y = np.load("label.npy")
train_y=to_categorical(train_y)
X_train, X_test, y_train, y_test = train_test_split(train_x, train_y, test_size=0.3, random_state=0)
print(X_train.shape,X_test.shape,y_train.shape,y_test.shape)
input_data = Input(shape=[size ,size, 3])
conv1 = Conv2D(filters=32 , kernel_size=[3 , 3] , padding='same' , kernel_initializer='he_normal' , use_bias=True , activation='relu')(input_data)
conv1 = BatchNormalization()(conv1)
conv2 = Conv2D(filters=32 , kernel_size=[3 , 3] , padding='same' , kernel_initializer='he_normal' , use_bias=True , activation='relu')(conv1)
conv2 = BatchNormalization()(conv2)
pool1 = MaxPooling2D(pool_size=[2 ,2] , strides=[2 , 2])(conv2)
pool1 = Dropout(0.1)(pool1)

conv3 = Conv2D(filters=64 , kernel_size=[3 , 3] , padding='same' , kernel_initializer='he_normal' , use_bias=True , activation='relu')(pool1)
conv3 = BatchNormalization()(conv3)
conv4 = Conv2D(filters=64 , kernel_size=[3 , 3] , padding='same' , kernel_initializer='he_normal' , use_bias=True , activation='relu')(conv3)
conv4 = BatchNormalization()(conv4)
pool2 = MaxPooling2D(pool_size=[2 , 2] ,strides=[2 , 2])(conv4)
pool2 = Dropout(0.1)(pool2)

conv5 = Conv2D(filters=128 , kernel_size=[3 , 3] , padding='same' , kernel_initializer='he_normal' , use_bias=True , activation='relu')(pool2)
conv5 = BatchNormalization()(conv5)
conv6 = Conv2D(filters=128 , kernel_size=[3 , 3] , padding='same' , kernel_initializer='he_normal' , use_bias=True , activation='relu')(conv5)
Пример #13
0
    y = Activation('relu')(y)

    y = Conv2D(filters, (f_size, f_size), padding='same')(y)
    y = BatchNormalization()(y)

    y = Add()([layer_input, y])

    return Activation('relu')(y)


IMG_HEIGHT, IMG_WIDTH = 128, 128

inputs = Input((None, None, 1))

x = Conv2D(64, 9, padding='same')(inputs)
x = BatchNormalization()(x)
x = Activation('relu')(x)

x = resnet_block(x)
x = resnet_block(x)
x = resnet_block(x)

outputs = Conv2D(1, 3, padding='same', activation='sigmoid')(x)

model = Model(inputs=inputs, outputs=outputs)
model.summary()

if LOAD_WEIGHTS:
    model.load_weights('model4.h5')

model.compile(loss='MSE', optimizer='Adam')
Пример #14
0
# Params

input_size = 128
input_channels = 3

epochs = 10
batch_size = 16
learning_rate = 0.0001
lr_decay = 1e-4

valid_data_size = 5000  # Samples to withhold for validation

model = Sequential()
model.add(
    BatchNormalization(input_shape=(input_channels, input_size, input_size)))
model.add(Conv2D(32, kernel_size=(2, 2), padding='same', activation='relu'))
model.add(Conv2D(32, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(64, kernel_size=(2, 2), padding='same', activation='relu'))
model.add(Conv2D(64, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, kernel_size=(2, 2), padding='same', activation='relu'))
model.add(Conv2D(128, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
Пример #15
0
def unet_model_nec3():
    inputs = Input((1, img_size_nec, img_size_nec))
    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(inputs)
    batch1 = BatchNormalization(axis=1)(conv1)
    conv1 = Conv2D(64, (3, 3), activation='relu', padding='same')(batch1)
    batch1 = BatchNormalization(axis=1)(conv1)
    pool1 = MaxPooling2D((2, 2))(batch1)

    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(pool1)
    batch2 = BatchNormalization(axis=1)(conv2)
    conv2 = Conv2D(128, (3, 3), activation='relu', padding='same')(batch2)
    batch2 = BatchNormalization(axis=1)(conv2)
    pool2 = MaxPooling2D((2, 2))(batch2)

    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(pool2)
    batch3 = BatchNormalization(axis=1)(conv3)
    conv3 = Conv2D(256, (3, 3), activation='relu', padding='same')(batch3)
    batch3 = BatchNormalization(axis=1)(conv3)
    pool3 = MaxPooling2D((2, 2))(batch3)

    #conv4 = Conv2D(256, (3, 3), activation='relu', padding='same') (pool3)
    #conv4 = Conv2D(256, (3, 3), activation='relu', padding='same') (conv4)
    #pool4 = MaxPooling2D(pool_size=(2, 2)) (conv4)

    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(pool3)
    batch5 = BatchNormalization(axis=1)(conv5)
    conv5 = Conv2D(512, (3, 3), activation='relu', padding='same')(batch5)
    batch5 = BatchNormalization(axis=1)(conv5)

    #up6 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same') (conv5)
    #up6 = concatenate([up6, conv4])
    #conv6 = Conv2D(256, (3, 3), activation='relu', padding='same') (up6)
    #conv6 = Conv2D(256, (3, 3), activation='relu', padding='same') (conv6)

    up7 = Conv2DTranspose(256, (2, 2), strides=(2, 2), padding='same')(batch5)
    up7 = concatenate([up7, conv3], axis=1)
    conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(up7)
    batch7 = BatchNormalization(axis=1)(conv7)
    conv7 = Conv2D(256, (3, 3), activation='relu', padding='same')(batch7)
    batch7 = BatchNormalization(axis=1)(conv7)

    up8 = Conv2DTranspose(128, (2, 2), strides=(2, 2), padding='same')(batch7)
    up8 = concatenate([up8, conv2], axis=1)
    conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(up8)
    batch8 = BatchNormalization(axis=1)(conv8)
    conv8 = Conv2D(128, (3, 3), activation='relu', padding='same')(batch8)
    batch8 = BatchNormalization(axis=1)(conv8)

    up9 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(batch8)
    up9 = concatenate([up9, conv1], axis=1)
    conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(up9)
    batch9 = BatchNormalization(axis=1)(conv9)
    conv9 = Conv2D(64, (3, 3), activation='relu', padding='same')(batch9)
    batch9 = BatchNormalization(axis=1)(conv9)

    conv10 = Conv2D(1, (1, 1), activation='sigmoid')(batch9)

    model = Model(inputs=[inputs], outputs=[conv10])

    model.compile(optimizer=Adam(lr=LR),
                  loss=dice_coef_loss,
                  metrics=[dice_coef])

    return model
Пример #16
0
def define_NN_architecture():
    wavelet_inputs = Input(shape=(248, 16, 1), name='wavelet_input')

    rms_inputs = Input(shape=(16, ), name='rms_input')
    RMS_out = BatchNormalization(
                        momentum=0.99,
                        epsilon=0.001,
                        center=True,
                        scale=True,
                        beta_initializer='zeros',
                        gamma_initializer='ones',
                        moving_mean_initializer='zeros',
                        moving_variance_initializer='ones'
                        )(rms_inputs)

    x = Conv2D(
                        32,
                        (3, 3),
                        padding='same',
                        )(wavelet_inputs)

    x = BatchNormalization(
                        momentum=0.99,
                        epsilon=0.001,
                        center=True,
                        scale=True,
                        beta_initializer='zeros',
                        gamma_initializer='ones',
                        moving_mean_initializer='zeros',
                        moving_variance_initializer='ones'
                        )(x)
    x = ReLU()(x)

    x_parallel = x

    x_parallel = MaxPooling2D((2, 2), padding='same')(x_parallel)

    x = Conv2D(
                        32,
                        (3, 3),
                        padding='same',
                        )(x)
    x = BatchNormalization(
                        momentum=0.99,
                        epsilon=0.001,
                        center=True,
                        scale=True,
                        beta_initializer='zeros',
                        gamma_initializer='ones',
                        moving_mean_initializer='zeros',
                        moving_variance_initializer='ones'
                        )(x)
    x = ReLU()(x)
    x = Dropout(0.5)(x)
    x = Conv2D(
                        32,
                        (3, 3),
                        strides=(2, 2),
                        padding='same',
                        )(x)

    x = keras.layers.concatenate([x, x_parallel], axis=3)

    x_parallel = x

    x_parallel = MaxPooling2D((2, 2), padding='same')(x_parallel)

    x = BatchNormalization(
                        momentum=0.99,
                        epsilon=0.001,
                        center=True,
                        scale=True,
                        beta_initializer='zeros',
                        gamma_initializer='ones',
                        moving_mean_initializer='zeros',
                        moving_variance_initializer='ones'
                        )(x)
    x = ReLU()(x)
    x = Dropout(0.5)(x)
    x = Conv2D(
                        32,
                        (3, 3),
                        padding='same',
                        )(x)

    x = BatchNormalization(
                        momentum=0.99,
                        epsilon=0.001,
                        center=True,
                        scale=True,
                        beta_initializer='zeros',
                        gamma_initializer='ones',
                        moving_mean_initializer='zeros',
                        moving_variance_initializer='ones'
                        )(x)
    x = ReLU()(x)
    x = Dropout(0.5)(x)
    x = Conv2D(
                        32,
                        (3, 3),
                        strides=(2, 2),
                        padding='same',
                        )(x)

    x = keras.layers.concatenate([x, x_parallel], axis=3)

    x = Flatten()(x)
    x = BatchNormalization(
                        momentum=0.99,
                        epsilon=0.001,
                        center=True,
                        scale=True,
                        beta_initializer='zeros',
                        gamma_initializer='ones',
                        moving_mean_initializer='zeros',
                        moving_variance_initializer='ones'
                        )(x)
    wavelet_out = ReLU()(x)

    combined_inputs = keras.layers.concatenate(
                [RMS_out, wavelet_out]
            )

    x = Dense(120,
              activation='relu'
              )(combined_inputs)

    x = Dropout(0.5)(x)

    predictions = Dense(18,
                        activation='softmax'
                        )(x)

    model = Model(inputs=[wavelet_inputs, rms_inputs],
                  outputs=predictions)
    return model
Пример #17
0
from sklearn.linear_model import Perceptron
from keras.models import Sequential
from keras.layers import Dense, Flatten, LeakyReLU, BatchNormalization
from keras.datasets import fashion_mnist
from keras.optimizers import Adam
from keras.callbacks import ModelCheckpoint

(x_train_full, y_train_full), (x_test, y_test) = fashion_mnist.load_data()

x_valid, x_train = x_train_full[:5000] / 255.0, x_train_full[5000:] / 255.0
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
x_test = x_test / 255.0

model = Sequential()
model.add(Flatten(input_shape=(28, 28)))
model.add(BatchNormalization())
model.add(Dense(300, activation='elu', kernel_initializer='he_normal'))
model.add(BatchNormalization())
model.add(Dense(100, activation='elu', kernel_initializer='he_normal'))
model.add(LeakyReLU(alpha=0.2))
model.add(BatchNormalization())
model.add(Dense(10, activation='softmax'))

model.summary()

checkpoint = ModelCheckpoint("./model/my_keras_model.h5")

model.compile(loss='sparse_categorical_crossentropy',
              optimizer=Adam(learning_rate=0.01),
              metrics=['acc'])
hist = model.fit(x_train,
Пример #18
0
pool2 = MaxPooling2D(pool_size=2, strides=1)
model.add(pool2)

convout5 = Conv2D(256, kernel_size=3, strides=1)
model.add(convout5)
activ5 = Activation('relu')
model.add(activ5)
pool3 = MaxPooling2D(pool_size=2, strides=1)
model.add(pool3)

model.add(Flatten())
dense1 = Dense(256)
model.add(dense1)
activ6 = Activation('relu')
model.add(activ6)
batchn = BatchNormalization()
model.add(batchn)
dense2 = Dense(184)
model.add(dense2)
activ7 = Activation('softmax')
model.add(activ7)

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


img = cv2.imread('test.jpg')
img = cv2.resize(img, (64, 64))
img = np.expand_dims(img, axis=0)
classes = model.predict(img)
gpu_options = tf.GPUOptions(allow_growth=True)
session = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
K.set_session(session)

# Supress warnings about wrong compilation of TensorFlow.
tf.logging.set_verbosity(tf.logging.ERROR)

noise_size = 100

## G

z = Input(shape=[noise_size])

G = Dense(7 * 7 * 256, input_dim=100)(z)
G = BatchNormalization(momentum=0.9)(G)
G = LeakyReLU(alpha=0.2)(G)
G = Reshape((7, 7, 256))(G)

G = UpSampling2D()(G)
G = Conv2D(128, (5, 5), padding='same')(G)
G = BatchNormalization(momentum=0.9)(G)
G = LeakyReLU(alpha=0.2)(G)

G = UpSampling2D()(G)
G = Conv2D(64, (5, 5), padding='same')(G)
G = BatchNormalization(momentum=0.9)(G)
G = LeakyReLU(alpha=0.2)(G)

G = Conv2D(32, (5, 5), padding='same')(G)
G = BatchNormalization(momentum=0.9)(G)
Пример #20
0
                input1 = Input(shape=(13, 13, 13, 1), name="inputs")

                params = dict(kernel_size=(5, 5, 5), activation=None,
                              padding="same", kernel_initializer=kernel_initializer)

                # Transposed convolution parameters
                # params_trans = dict(kernel_size=(2, 2, 2), strides=(1, 1, 1), padding="same")

                # BEGIN - Encoding path
                # encodeA = ConvolutionBlock(input1, "encodeA", fms, params)

                # First Encoder
                name = "encodeA"
                x = Conv3D(filters=8, **params, name=name + "_conv0")(input1) # ** to use a dictionary instead of keyword in the function
                x = BatchNormalization(name=name + "_bn0")(x)
                x = Dropout(.20)(x)
                x = Activation("relu", name=name + "_relu0")(x)
                # x = Conv3D(filters=4, **params, name=name + "_conv1")(x)
                # x = BatchNormalization(name=name + "_bn1")(x)
                # encodeA = Activation("relu", name=name)(x)
                poolA = MaxPooling3D(name="poolA", pool_size=(2, 2, 2))(x)

                # Second Encoder
                name = "encodeB"
                x = Conv3D(filters=16, **params, name=name + "_conv0")(poolA)
                x = BatchNormalization(name=name + "_bn0")(x)
                x = Dropout(.20)(x)
                x = Activation("relu", name=name + "_relu0")(x)
                # x = Conv3D(filters=8, **params, name=name + "_conv1")(x)
                # x = BatchNormalization(name=name + "_bn1")(x)
Пример #21
0
def ResNetPreAct(input_shape=(500,500,3), nb_classes=13, layer1_params=(5,64,2), res_layer_params=(3,16,3),
        final_layer_params=None, init='glorot_normal', reg=0.0, use_shortcuts=False):
    
    """
    Return a new Residual Network using full pre-activation based on the work in
    "Identity Mappings in Deep Residual Networks"  by He et al
    http://arxiv.org/abs/1603.05027

    The following network definition achieves 92.0% accuracy on CIFAR-10 test using
    `adam` optimizer, 100 epochs, learning rate schedule of 1e.-3 / 1.e-4 / 1.e-5 with
    transitions at 50 and 75 epochs:
    ResNetPreAct(layer1_params=(3,128,2),res_layer_params=(3,32,25),reg=reg)
    
    Removed max pooling and using just stride in first convolutional layer. Motivated by
    "Striving for Simplicity: The All Convolutional Net"  by Springenberg et al
    (https://arxiv.org/abs/1412.6806) and my own experiments where I observed about 0.5%
    improvement by replacing the max pool operations in the VGG-like cifar10_cnn.py example
    in the Keras distribution.
    
    Parameters
    ----------
    input_dim : tuple of (C, H, W)
    nb_classes: number of scores to produce from final affine layer (input to softmax)
    layer1_params: tuple of (filter size, num filters, stride for conv)
    res_layer_params: tuple of (filter size, num res layer filters, num res stages)
    final_layer_params: None or tuple of (filter size, num filters, stride for conv)
    init: type of weight initialization to use
    reg: L2 weight regularization (or weight decay)
    use_shortcuts: to evaluate difference between residual and non-residual network
    """

    sz_L1_filters, nb_L1_filters, stride_L1 = layer1_params
    sz_res_filters, nb_res_filters, nb_res_stages = res_layer_params
    
    use_final_conv = (final_layer_params is not None)
    if use_final_conv:
        sz_fin_filters, nb_fin_filters, stride_fin = final_layer_params
        sz_pool_fin = input_shape[1] / (stride_L1 * stride_fin)
    else:
        sz_pool_fin = input_shape[1] / (stride_L1)


    '''
    from keras import backend as K
    # Permute dimension order if necessary
    if K.image_dim_ordering() == 'tf':
        input_shape = (input_shape[1], input_shape[2], input_shape[0])
    '''

    img_input = Input(shape=input_shape, name='cifar')

    x = Conv2D(
            filters=nb_L1_filters, 
            kernel_size=(sz_L1_filters,sz_L1_filters),
            padding='same',
            strides=(stride_L1, stride_L1),
            kernel_initializer=init,
            kernel_regularizer=l2(reg),
            use_bias=False,
            name='conv0'
        )(img_input)
    
    x = BatchNormalization(axis=3, name='bn0')(x)
    x = Activation('relu', name='relu0')(x)

    for stage in range(1,nb_res_stages+1):
        x = rnpa_bottleneck_layer(
                x,
                (nb_L1_filters, nb_res_filters),
                sz_res_filters, 
                stage,
                init=init, 
                reg=reg, 
                use_shortcuts=use_shortcuts
            )


    x = BatchNormalization(axis=3, name='bnF')(x)
    x = Activation('relu', name='reluF')(x)

    if use_final_conv:
        x = Conv2D(
                filters=nb_L1_filters, 
                kernel_size=(sz_L1_filters,sz_L1_filters),
                padding='same',
                strides=(stride_fin, stride_fin),
                kernel_initializer=init,
                kernel_regularizer=l2(reg),
                name='convF'
            )(x)

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

    # x = Flatten(name='flat')(x)
    x = Flatten()(x)
    x = Dense(nb_classes, activation='softmax', name='fc10')(x)

    return Model(img_input, x, name='rnpa')
Пример #22
0
def resnet_block(inputs,num_filters,kernel_size,strides,activation='relu'):
    x=Conv2D(num_filters,kernel_size=kernel_size,strides=strides,padding='same',kernel_initializer='he_normal',kernel_regularizer=l2(1e-3))(inputs)
    x=BatchNormalization()(x)
    if(activation):
        x=Activation('relu')(x)
    return x
def resnet_v2(input_shape, depth, num_classes=10):
    """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)

    # 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()(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
def final_model(input_dim,
                filters,
                kernel_size,
                conv_stride,
                conv_border_mode,
                units,
                use_bidirec=False,
                output_dim=29,
                recur_layers=2):
    """ Build a deep network for speech 
    """
    # Main acoustic input
    input_data = Input(name='the_input', shape=(None, input_dim))
    # TODO: Specify the layers in your network
    conv_1d = Conv1D(filters,
                     kernel_size,
                     padding=conv_border_mode,
                     strides=conv_stride,
                     activation='relu',
                     name='conv1d')(input_data)
    conv_1d = BatchNormalization(name='bn_conv_1d')(conv_1d)
    conv_1d = Dropout(0.4)(conv_1d)

    if use_bidirec == True:
        ############# 1 use_bidirec
        bn_rnn = conv_1d
        for i in range(recur_layers):
            bn_rnn = Bidirectional(
                GRU(units, activation='relu', return_sequences=True))(bn_rnn)
            bn_rnn = BatchNormalization()(bn_rnn)
            bn_rnn = Dropout(0.4)(bn_rnn)
        time_dense = TimeDistributed(Dense(output_dim))(bn_rnn)
    else:
        ###############  2
        gru = GRU(units,
                  activation='relu',
                  return_sequences=True,
                  implementation=2,
                  name='rnn')(conv_1d)
        gru = BatchNormalization()(gru)
        gru = Dropout(0.4)(gru)

        gru = GRU(units,
                  activation='relu',
                  return_sequences=True,
                  implementation=2,
                  name='rnn_2')(gru)
        gru = BatchNormalization()(gru)
        gru = Dropout(0.4)(gru)
        time_dense = TimeDistributed(Dense(output_dim))(gru)

    ###############
    y_pred = Activation('softmax', name='softmax')(time_dense)

    # Specify the model
    model = Model(inputs=input_data, outputs=y_pred)
    # TODO: Specify model.output_length
    model.output_length = lambda x: cnn_output_length(
        x, kernel_size, conv_border_mode, conv_stride)
    print(model.summary())
    return model
    z_mean, z_log_var = args
    batch = K.shape(z_mean)[0]
    dim = K.int_shape(z_mean)[1]
    epsilon = K.random_normal(shape=(batch, dim))
    return z_mean + K.exp(0.5 * z_log_var) * epsilon


#making the encoder model
encoder_input = Input(shape=(64, 64, 3), name='encoder_input')
enc = Conv2D(filters=64,
             kernel_size=5,
             strides=(2, 2),
             padding='same',
             activation='relu',
             name='encoder_first_conv')(encoder_input)
enc = BatchNormalization()(enc)
enc = Conv2D(filters=128,
             kernel_size=5,
             strides=(2, 2),
             padding='same',
             activation='relu',
             name='encoder_second_conv')(enc)
enc = BatchNormalization()(enc)
enc = Conv2D(filters=256,
             kernel_size=5,
             strides=(2, 2),
             padding='same',
             activation='relu',
             name='encoder_third_conv')(enc)
enc = BatchNormalization()(enc)
enc = Flatten()(enc)
Пример #26
0
model.add(Conv1D(128, 3, activation='relu'))
model.add(GlobalAveragePooling1D())
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))
'''
'''
model.add(LSTM(32, return_sequences=True, stateful=True, batch_input_shape=((batch_size * batch_size - 2 * batch_size + 2), None, word_length)))
model.add(LSTM(32, return_sequences=True, stateful=True))
model.add(LSTM(32, stateful=True))
model.add(Dense(num_classes, activation='softmax'))
'''
encoder_a = Input(shape=(None, vec_length))
conv_1_a = Conv1D(64, word_length)(encoder_a)
conv_2_a = Conv1D(64, 3)(conv_1_a)
norm_a = BatchNormalization()(conv_2_a)
activate_a = Activation('relu')(norm_a)
pool_a = MaxPooling1D(3)(activate_a)
drop_a = Dropout(0.5)(pool_a)
#conv_3_a = Conv1D(128, 3, activation='relu')(pool_a)
#conv_4_a = Conv1D(128, 3, activation='relu')(conv_3_a)

encoder_b = Input(shape=(None, vec_length))
conv_1_b = Conv1D(64, word_length)(encoder_b)
conv_2_b = Conv1D(64, 3)(conv_1_b)
norm_b = BatchNormalization()(conv_2_b)
activate_b = Activation('relu')(norm_b)
pool_b = MaxPooling1D(3)(activate_b)
drop_b = Dropout(0.5)(pool_b)
#conv_3_b = Conv1D(128, 3, activation='relu')(pool_b)
#conv_4_b = Conv1D(128, 3, activation='relu')(conv_3_b)
Пример #27
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)
            or "imagenet" (pre-training on ImageNet).
        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, 244)` (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 weights not in {'imagenet', None}:
        raise ValueError('The `weights` argument should be either '
                         '`None` (random initialization) or `imagenet` '
                         '(pre-training on ImageNet).')

    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)

    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((3, 3))(img_input)
    x = Conv2D(64, (7, 7), strides=(2, 2), 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)

        if K.image_data_format() == 'channels_first':
            if include_top:
                maxpool = model.get_layer(name='avg_pool')
                shape = maxpool.output_shape[1:]
                dense = model.get_layer(name='fc1000')
                layer_utils.convert_dense_weights_data_format(
                    dense, shape, 'channels_first')

            if K.backend() == 'tensorflow':
                warnings.warn('You are using the TensorFlow backend, yet you '
                              'are using the Theano '
                              'image data format convention '
                              '(`image_data_format="channels_first"`). '
                              'For best performance, set '
                              '`image_data_format="channels_last"` in '
                              'your Keras config '
                              'at ~/.keras/keras.json.')
    return model
Пример #28
0
def gen_model():
    m_in = Input(shape=x[0][0].shape)
    m_off = Lambda(offset_slice)(m_in)
    m_noise = GaussianNoise(np.std(x[0][0] / 100))(m_off) # how much noice to have????

    m_t = Conv1D(22, 20, padding='causal')(m_noise)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.3)(m_t)

    m_t = Conv1D(15, 20, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.5)(m_t)

    m_t = Conv1D(10, 12, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.6)(m_t)

    m_t = Flatten()(m_t)
    m_t = Dense(35)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_t = Dense(15)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_out1 = Dense(1)(m_t)


    m_t = Conv1D(22, 20, padding='causal')(m_noise)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.3)(m_t)

    m_t = Conv1D(15, 20, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.5)(m_t)

    m_t = Conv1D(10, 12, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.6)(m_t)

    m_t = Flatten()(m_t)
    m_t = Dense(35)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_t = Dense(15)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_out2 = Dense(1)(m_t)


    m_t = Conv1D(22, 20, padding='causal')(m_noise)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.3)(m_t)

    m_t = Conv1D(15, 20, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.5)(m_t)

    m_t = Conv1D(10, 12, padding='causal')(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = ELU()(m_t)
    m_t = AveragePooling1D(2)(m_t)
    m_t = Dropout(0.6)(m_t)

    m_t = Flatten()(m_t)
    m_t = Dense(35)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_t = Dense(15)(m_t)
    m_t = BatchNormalization()(m_t)
    m_t = Activation('tanh')(m_t)
    m_out3 = Dense(1)(m_t)

    m_conc = concatenate([m_out1, m_out2, m_out3])
    m_out = Activation('softmax')(m_conc)

    model = Model(inputs=m_in, outputs=m_out)

    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['accuracy'])
    return model
Пример #29
0
def bnac(in_layer, name=None, idx=None, fs=None, act=None, size=3, stride=1, dilation=1):
    x=BatchNormalization(name=name+'_bn')(in_layer)
    return Activation(activation=act, name=name)(x)
def _main_(args):

    config_path = args.conf
    
    with open(config_path) as config_buffer:    
        config = json.loads(config_buffer.read())

    if config['backup']['create_backup']:
        config = create_backup(config)

    keras.backend.tensorflow_backend.set_session(get_session())

    #path for the training and validation dataset
    datasetTrainPath = os.path.join(args.folder,"train")
    datasetValPath = os.path.join(args.folder,"val")

    for folder in [datasetTrainPath, datasetValPath]:
        if not os.path.isdir(folder):
            raise Exception("{} doesn't exist!".format(folder))

    classesTrain = next(os.walk(datasetTrainPath))[1]
    classesVal = next(os.walk(datasetValPath))[1]

    if not classesVal == classesTrain:
        raise Exception("The training and validation classes must be the same!")
    else:
        folders = classesTrain

    #training configuration
    epochs = config['train']['nb_epochs']
    batchSize = config['train']['batch_size']
    width = config['model']['input_size_w']
    height = config['model']['input_size_h']
    depth = 3 if config['model']['gray_mode'] == False else 1

    #config keras generators
    if len(folders) == 2: #if just have 2 classes, the model will have a binary output
        classes = 1
    else:
        classes = len(folders)

    #count all samples
    imagesTrainPaths = []
    imagesValPaths = []
    for folder in folders: 
        imagesTrainPaths+=list(list_images(os.path.join(datasetTrainPath, folder)))
        imagesValPaths+=list(list_images(os.path.join(datasetValPath, folder)))
    
    generator_config = {
        'IMAGE_H'         : height, 
        'IMAGE_W'         : width,
        'IMAGE_C'         : depth,
        'BATCH_SIZE'      : batchSize
    }  

    #callbacks    
    model_name = config['train']['saved_weights_name']
    checkPointSaverBest=ModelCheckpoint(model_name, monitor='val_acc', verbose=1, 
                                        save_best_only=True, save_weights_only=False, mode='auto', period=1)
    ckp_model_name = os.path.splitext(model_name)[1]+"_ckp.h5"
    checkPointSaver=ModelCheckpoint(ckp_model_name, verbose=1, 
                                save_best_only=False, save_weights_only=False, period=10)

    tb=TensorBoard(log_dir=config['train']['tensorboard_log_dir'], histogram_freq=0, batch_size=batchSize, write_graph=True,
                write_grads=False, write_images=False, embeddings_freq=0, embeddings_layer_names=None, embeddings_metadata=None)


    #create the classification model
    # make the feature extractor layers
    if depth == 1:
        input_size = (height, width, 1)
        input_image     = Input(shape=input_size)
    else:
        input_size = (height, width, 3)
        input_image     = Input(shape=input_size)

    feature_extractor = import_feature_extractor(config['model']['backend'], input_size)

    train_generator = BatchGenerator(imagesTrainPaths, 
                                generator_config, 
                                norm=feature_extractor.normalize,
                                jitter=True)
    val_generator = BatchGenerator(imagesValPaths, 
                                    generator_config, 
                                    norm=feature_extractor.normalize,
                                    jitter=False)  

    features = feature_extractor.extract(input_image)          

    # make the model head
    output = Conv2D(classes, (1, 1), padding="same")(features)
    output = BatchNormalization()(output)
    output = LeakyReLU(alpha=0.1)(output)
    output = GlobalAveragePooling2D()(output)
    output = Activation("sigmoid")(output) if classes == 1 else Activation("softmax")(output)

    if config['train']['pretrained_weights'] != "":
        model = load_model(config['model']['pretrained_weights'] )
    else:
        model = Model(input_image, output)   
        opt = Adam()
        model.compile(loss="binary_crossentropy" if classes == 1 else "categorical_crossentropy",
                    optimizer=opt,metrics=["accuracy"])
    model.summary()

    model.fit_generator(
            train_generator,
            steps_per_epoch=len(imagesTrainPaths)//batchSize,
            epochs=epochs,
            validation_data=val_generator,
            validation_steps=len(imagesValPaths)//batchSize,
            callbacks=[checkPointSaverBest,checkPointSaver,tb],
            workers=12,
            max_queue_size=40)