train_samples, validation_samples = train_test_split(samples, test_size=0.2)
# compile and train the model using the generator function
train_generator = generator(train_samples, batch_size=batch_size)
validation_generator = generator(validation_samples, batch_size=batch_size)

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Activation, Dropout, Cropping2D
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D

model = Sequential()
model.add(Lambda(lambda x: x / 255.0 - 0.5, input_shape=(160, 320, 3)))
model.add(Cropping2D(cropping=((60, 25), (0, 0))))
model.add(Convolution2D(24, 5, 5, activation="relu"))
model.add(MaxPooling2D())
model.add(Convolution2D(36, 5, 5, activation="relu"))
model.add(MaxPooling2D())
model.add(Convolution2D(48, 5, 5, activation="relu"))
model.add(MaxPooling2D())
model.add(Convolution2D(64, 3, 3, activation="relu"))
model.add(Convolution2D(64, 3, 3, activation="relu"))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(100))
model.add(Dropout(0.5))
model.add(Dense(50))
model.add(Dropout(0.5))
model.add(Dense(10))
model.add(Dense(1))
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Flatten
from keras.layers.convolutional import Conv2D
from keras.optimizers import Adam
from keras.layers.pooling import MaxPooling2D
from keras.layers.core import Activation

from keras.preprocessing.image import ImageDataGenerator
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

# Create the model
model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(96, 96, 1)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(3, 3)))
model.add(Dropout(0.25))

model.add(Conv2D(64, (3, 3)))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))

model.add(Conv2D(128, (3, 3), padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(1024, activation="relu"))

model.add(Dropout(0.5))
validation_generator = generator(validation_samples, batch_size=8)

ch, col, row = 3, 90, 320  # Trimmed image format

model = Sequential()
# Crop the image, to remove the sky and the hood
model.add(Cropping2D(cropping=((50, 20), (0, 0)), input_shape=(160, 320, 3)))
# Preprocess incoming data, centered around zero with small standard deviation
model.add(
    Lambda(lambda x: x / 127.5 - 1.,
           input_shape=(row, col, ch),
           output_shape=(row, col, ch)))

# Convnets
model.add(Conv2D(16, (3, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))
model.add(Activation('relu'))

model.add(Conv2D(32, (3, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))
model.add(Activation('relu'))

model.add(Conv2D(32, (3, 3)))
model.add(MaxPooling2D((2, 2)))
model.add(Dropout(0.25))
model.add(Activation('relu'))

# Fully connected layers
model.add(Flatten())
Beispiel #4
0
# Define IoU metric
def mean_iou(y_true, y_pred):
    prec = []
    for t in np.arange(0.5, 1.0, 0.05):
        y_pred_ = tf.to_int32(y_pred > t)
        score, up_opt = tf.metrics.mean_iou(y_true, y_pred_, 2)
        K.get_session().run(tf.local_variables_initializer())
        with tf.control_dependencies([up_opt]):
            score = tf.identity(score)
        prec.append(score)
    return K.mean(K.stack(prec), axis=0)

# Build model
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = Lambda(lambda x: x / 255) (inputs)

c1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (s)
c1 = Dropout(.3)(c1)
c1 = Conv2D(16, (3, 3), activation='relu', kernel_initializer='he_normal', padding='same') (c1)
p1 = MaxPooling2D((2, 2)) (c1)

c4 = Conv2DTranspose(16, (2, 2), strides=(2, 2), padding='same') (p1)
outputs = Conv2D(1, (1, 1), activation='sigmoid') (c4)

model = Model(inputs=[inputs], outputs=[outputs])
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=[mean_iou])
model.summary()

results = model.fit(X_train, Y_train, validation_split=0.2, batch_size=16, epochs=100)
Beispiel #5
0
def build_model(input_layer, start_neurons, DropoutRatio=0.5):
    # 101 -> 50
    conv1 = Conv2D(start_neurons * 1, (3, 3), activation=None,
                   padding='same')(input_layer)
    conv1 = residual_block(conv1, start_neurons * 1)
    conv1 = residual_block(conv1, start_neurons * 1, True)
    pool1 = MaxPooling2D((2, 2))(conv1)
    pool1 = Dropout(DropoutRatio / 2)(pool1)

    # 50 -> 25
    conv2 = Conv2D(start_neurons * 2, (3, 3), activation=None,
                   padding='same')(pool1)
    conv2 = residual_block(conv2, start_neurons * 2)
    conv2 = residual_block(conv2, start_neurons * 2, True)
    pool2 = MaxPooling2D((2, 2))(conv2)
    pool2 = Dropout(DropoutRatio)(pool2)

    # 25 -> 12
    conv3 = Conv2D(start_neurons * 4, (3, 3), activation=None,
                   padding='same')(pool2)
    conv3 = residual_block(conv3, start_neurons * 4)
    conv3 = residual_block(conv3, start_neurons * 4, True)
    pool3 = MaxPooling2D((2, 2))(conv3)
    pool3 = Dropout(DropoutRatio)(pool3)

    # 12 -> 6
    conv4 = Conv2D(start_neurons * 8, (3, 3), activation=None,
                   padding='same')(pool3)
    conv4 = residual_block(conv4, start_neurons * 8)
    conv4 = residual_block(conv4, start_neurons * 8, True)
    pool4 = MaxPooling2D((2, 2))(conv4)
    pool4 = Dropout(DropoutRatio)(pool4)

    # Middle
    convm = Conv2D(start_neurons * 16, (3, 3), activation=None,
                   padding='same')(pool4)
    convm = residual_block(convm, start_neurons * 16)
    convm = residual_block(convm, start_neurons * 16, True)

    # 6 -> 12
    deconv4 = Conv2DTranspose(start_neurons * 8, (3, 3),
                              strides=(2, 2),
                              padding='same')(convm)
    uconv4 = concatenate([deconv4, conv4])
    uconv4 = Dropout(DropoutRatio)(uconv4)

    uconv4 = Conv2D(start_neurons * 8, (3, 3), activation=None,
                    padding='same')(uconv4)
    uconv4 = residual_block(uconv4, start_neurons * 8)
    uconv4 = residual_block(uconv4, start_neurons * 8, True)

    # 12 -> 25
    deconv3 = Conv2DTranspose(start_neurons * 4, (3, 3),
                              strides=(2, 2),
                              padding='valid')(uconv4)
    uconv3 = concatenate([deconv3, conv3])
    uconv3 = Dropout(DropoutRatio)(uconv3)

    uconv3 = Conv2D(start_neurons * 4, (3, 3), activation=None,
                    padding='same')(uconv3)
    uconv3 = residual_block(uconv3, start_neurons * 4)
    uconv3 = residual_block(uconv3, start_neurons * 4, True)

    # 25 -> 50
    deconv2 = Conv2DTranspose(start_neurons * 2, (3, 3),
                              strides=(2, 2),
                              padding='same')(uconv3)
    uconv2 = concatenate([deconv2, conv2])
    uconv2 = Dropout(DropoutRatio)(uconv2)

    uconv2 = Conv2D(start_neurons * 2, (3, 3), activation=None,
                    padding='same')(uconv2)
    uconv2 = residual_block(uconv2, start_neurons * 2)
    uconv2 = residual_block(uconv2, start_neurons * 2, True)

    # 50 -> 101
    deconv1 = Conv2DTranspose(start_neurons * 1, (3, 3),
                              strides=(2, 2),
                              padding='valid')(uconv2)
    uconv1 = concatenate([deconv1, conv1])
    uconv1 = Dropout(DropoutRatio)(uconv1)

    uconv1 = Conv2D(start_neurons * 1, (3, 3), activation=None,
                    padding='same')(uconv1)
    uconv1 = residual_block(uconv1, start_neurons * 1)
    uconv1 = residual_block(uconv1, start_neurons * 1, True)

    output_layer_noActi = Conv2D(1, (1, 1), padding='same',
                                 activation=None)(uconv1)
    output_layer = Activation('sigmoid')(output_layer_noActi)

    return output_layer
def pooling(x, ks, st):
    x = MaxPooling2D((ks, ks), strides=(st, st))(x)
    return x
Beispiel #7
0
# convert class vectors to binary class matrices
Y_train = keras.utils.to_categorical(Y_train, num_classes)
Y_valid = keras.utils.to_categorical(Y_valid, num_classes)

# build model
model = Sequential()

# CNN
# block 1
model.add(
    Conv2D(32, (5, 5),
           padding='valid',
           activation='relu',
           input_shape=input_shape))
model.add(ZeroPadding2D(padding=(1, 1), data_format='channels_last'))
model.add(MaxPooling2D(pool_size=(5, 5), strides=(2, 2)))
model.add(Dropout(0.3))

# block 2
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(ZeroPadding2D(padding=(1, 1), data_format='channels_last'))
model.add(Dropout(0.3))

# block 3
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(AveragePooling2D(pool_size=(3, 3), strides=(2, 2)))
model.add(ZeroPadding2D(padding=(1, 1), data_format='channels_last'))
model.add(Dropout(0.3))

# block 4
model.add(Conv2D(256, (3, 3), activation='relu'))
Beispiel #8
0
def DenseNet169(nb_dense_block=4, growth_rate=32, nb_filter=64, reduction=0.0, dropout_rate=0.0, weight_decay=1e-4, classes=1000, weights_path=None):
    '''Instantiate the DenseNet architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
      concat_axis = 3
      img_input = Input(shape=(224, 224, 3), name='data')
    else:
      concat_axis = 1
      img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 64
    nb_layers = [6,12,32,32] # For DenseNet-169

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter, 7, 7, subsample=(2, 2), name='conv1', bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx+2
        x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate, weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv'+str(final_stage)+'_blk_bn')(x)
    x = Scale(axis=concat_axis, name='conv'+str(final_stage)+'_blk_scale')(x)
    x = Activation('relu', name='relu'+str(final_stage)+'_blk')(x)
    x = GlobalAveragePooling2D(name='pool'+str(final_stage))(x)
    print(x.shape)

    out = Dense(classes, name='fc6')(x)
    out = Activation('softmax', name='prob')(out)

    model = Model(img_input, out, name='densenet')

    if weights_path is not None:
      model.load_weights(weights_path)

    model2 = Model(img_input, x)
    return model2
Beispiel #9
0
def DenseUNet(nb_dense_block=4,
              growth_rate=48,
              nb_filter=96,
              reduction=0.0,
              dropout_rate=0.0,
              weight_decay=1e-4,
              weights_path=None,
              args=None):
    '''Instantiate the DenseNet 161 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(batch_shape=(args.b, args.input_size,
                                       args.input_size, 3),
                          name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [6, 12, 36, 24]  # For DenseNet-161
    box = []
    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, (7, 7), strides=(2, 2), name='conv1',
               use_bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    box.append(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)
        box.append(x)
        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)
    box.append(x)

    up0 = UpSampling2D(size=(2, 2))(x)
    line0 = Conv2D(2208, (1, 1),
                   padding="same",
                   kernel_initializer="normal",
                   name="line0")(box[3])
    up0_sum = add([line0, up0])
    conv_up0 = Conv2D(768, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up0")(up0_sum)
    bn_up0 = BatchNormalization(name="bn_up0")(conv_up0)
    ac_up0 = Activation('relu', name='ac_up0')(bn_up0)

    up1 = UpSampling2D(size=(2, 2))(ac_up0)
    up1_sum = add([box[2], up1])
    conv_up1 = Conv2D(384, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up1")(up1_sum)
    bn_up1 = BatchNormalization(name="bn_up1")(conv_up1)
    ac_up1 = Activation('relu', name='ac_up1')(bn_up1)

    up2 = UpSampling2D(size=(2, 2))(ac_up1)
    up2_sum = add([box[1], up2])
    conv_up2 = Conv2D(96, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up2")(up2_sum)
    bn_up2 = BatchNormalization(name="bn_up2")(conv_up2)
    ac_up2 = Activation('relu', name='ac_up2')(bn_up2)

    up3 = UpSampling2D(size=(2, 2))(ac_up2)
    up3_sum = add([box[0], up3])
    conv_up3 = Conv2D(96, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up3")(up3_sum)
    bn_up3 = BatchNormalization(name="bn_up3")(conv_up3)
    ac_up3 = Activation('relu', name='ac_up3')(bn_up3)

    up4 = UpSampling2D(size=(2, 2))(ac_up3)
    conv_up4 = Conv2D(64, (3, 3),
                      padding="same",
                      kernel_initializer="normal",
                      name="conv_up4")(up4)
    conv_up4 = Dropout(rate=0.3)(conv_up4)
    bn_up4 = BatchNormalization(name="bn_up4")(conv_up4)
    ac_up4 = Activation('relu', name='ac_up4')(bn_up4)

    x = Conv2D(3, (1, 1),
               padding="same",
               kernel_initializer="normal",
               name="dense167classifer")(ac_up4)
    #pdb.set_trace()
    model = Model(img_input, x, name='denseu161')

    return model
Beispiel #10
0
def __create_dense_net(nb_classes, img_input, include_top, depth=40, nb_dense_block=3, growth_rate=12, nb_filter=-1,
                       nb_layers_per_block=-1, bottleneck=False, reduction=0.0, dropout_rate=None, weight_decay=1e-4,
                       subsample_initial_block=False, activation='softmax'):
    ''' Build the DenseNet model
    Args:
        nb_classes: number of classes
        img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        include_top: flag to include the final Dense layer
        depth: number or layers
        nb_dense_block: number of dense blocks to add to end (generally = 3)
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters. Default -1 indicates initial number of filters is 2 * growth_rate
        nb_layers_per_block: number of layers in each dense block.
                Can be a -1, positive integer or a list.
                If -1, calculates nb_layer_per_block from the depth of the network.
                If positive integer, a set number of layers per dense block.
                If list, nb_layer is used as provided. Note that list size must
                be (nb_dense_block + 1)
        bottleneck: add bottleneck blocks
        reduction: reduction factor of transition blocks. Note : reduction value is inverted to compute compression
        dropout_rate: dropout rate
        weight_decay: weight decay rate
        subsample_initial_block: Set to True to subsample the initial convolution and
                add a MaxPool2D before the dense blocks are added.
        subsample_initial:
        activation: Type of activation at the top layer. Can be one of 'softmax' or 'sigmoid'.
                Note that if sigmoid is used, classes must be 1.
    Returns: keras tensor with nb_layers of conv_block appended
    '''

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

    if reduction != 0.0:
        assert reduction <= 1.0 and reduction > 0.0, 'reduction value must lie between 0.0 and 1.0'

    # layers in each dense block
    if type(nb_layers_per_block) is list or type(nb_layers_per_block) is tuple:
        nb_layers = list(nb_layers_per_block)  # Convert tuple to list

        assert len(nb_layers) == (nb_dense_block), 'If list, nb_layer is used as provided. ' \
                                                   'Note that list size must be (nb_dense_block)'
        final_nb_layer = nb_layers[-1]
        nb_layers = nb_layers[:-1]
    else:
        if nb_layers_per_block == -1:
            assert (depth - 4) % 3 == 0, 'Depth must be 3 N + 4 if nb_layers_per_block == -1'
            count = int((depth - 4) / 3)

            if bottleneck:
                count = count // 2

            nb_layers = [count for _ in range(nb_dense_block)]
            final_nb_layer = count
        else:
            final_nb_layer = nb_layers_per_block
            nb_layers = [nb_layers_per_block] * nb_dense_block

    # compute initial nb_filter if -1, else accept users initial nb_filter
    if nb_filter <= 0:
        nb_filter = 1 * growth_rate

    # compute compression factor
    compression = 1.0 - reduction

    # Initial convolution
    if subsample_initial_block:
        initial_kernel = (7, 7)
        initial_strides = (2, 2)
    else:
        initial_kernel = (3, 3)
        initial_strides = (1, 1)

    x = Conv2D(nb_filter, initial_kernel, kernel_initializer='he_normal', padding='same',
               strides=initial_strides, use_bias=False, kernel_regularizer=l2(weight_decay))(img_input)

    if subsample_initial_block:
        x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
        x = Activation('relu')(x)
        x = MaxPooling2D((3, 3), strides=(2, 2), padding='same')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x, nb_layers[block_idx], nb_filter, growth_rate, bottleneck=bottleneck,
                                     dropout_rate=dropout_rate, weight_decay=weight_decay)
        # add transition_block
        x = __transition_block(x, nb_filter, compression=compression, weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)
        print(block_idx, '---------------------------------')

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x, final_nb_layer, nb_filter, growth_rate, bottleneck=bottleneck,
                                 dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(axis=concat_axis, epsilon=1.1e-5)(x)
    x = Activation('relu')(x)
    x = GlobalAveragePooling2D()(x)

    if include_top:
        x = Dense(nb_classes, activation=activation)(x)

    return x
Beispiel #11
0
e = 2.0
c = w - w * K.log(1 + (w/e))

def wingLoss(y_true, y_pred, w=w, e=e, c=c):
    error = y_true - y_pred
    cond = K.abs(error) < w
    true = w * (K.log(1 + (K.abs(error)/e)))
    otherwise = K.abs(error) - c
    return tf.where(cond, true, otherwise)


input = Input((192, 192, 3), name='Input')
           
conv1 = initial_conv_block1(input)
#conv1 = Residual2(32, 64, conv1)
pool1 = MaxPooling2D(pool_size=(2, 2), name='MaxPool1')(conv1)
    
conv2 = Residual3(64, 128, pool1)
#conv2 = Residual4(128, 128, conv2)
pool2 = MaxPooling2D(pool_size=(2, 2), name='MaxPool2')(conv2)
    
conv3 = Residual5(128, 256, pool2)
#conv3 = Residual6(256, 256, conv3)
pool3 = MaxPooling2D(pool_size=(2, 2), name='MaxPool3')(conv3)
    
#conv4 = Residual7(256, 512, pool3)
#conv4 = Residual8(512, 512, conv4)
#drop4 = Dropout(0.2, name='Dropout1')(conv4)
#pool4 = MaxPooling2D(pool_size=(2, 2), name='MaxPool4')(drop4)

conv5 = Residual9(256, 512, pool3)
Beispiel #12
0
def make_network(X_DC, X_IC, num_labels, DC_drop_value, IC_drop_value,
                 connected_drop_value):

    # DEEP CORE #
    #print("Train Data DC", X_DC.shape)
    strings = X_DC.shape[1]
    dom_per_string = X_DC.shape[2]
    dom_variables = X_DC.shape[3]

    # Conv DC + batch normalization, later dropout and maxpooling
    input_DC = Input(shape=(strings, dom_per_string, dom_variables))

    conv1_DC = Conv2D(100,
                      kernel_size=(strings, 5),
                      padding='same',
                      activation='tanh')(input_DC)
    batch1_DC = BatchNormalization()(conv1_DC)
    pool1_DC = MaxPooling2D(pool_size=(1, 2))(batch1_DC)
    drop1_DC = Dropout(DC_drop_value)(pool1_DC)

    conv2_DC = Conv2D(100,
                      kernel_size=(strings, 7),
                      padding='same',
                      activation='relu')(drop1_DC)
    batch2_DC = BatchNormalization()(conv2_DC)
    drop2_DC = Dropout(DC_drop_value)(batch2_DC)

    conv3_DC = Conv2D(100,
                      kernel_size=(strings, 7),
                      padding='same',
                      activation='relu')(drop2_DC)
    batch3_DC = BatchNormalization()(conv3_DC)
    drop3_DC = Dropout(DC_drop_value)(batch3_DC)

    conv4_DC = Conv2D(100,
                      kernel_size=(strings, 3),
                      padding='valid',
                      activation='relu')(drop3_DC)
    batch4_DC = BatchNormalization()(conv4_DC)
    pool4_DC = MaxPooling2D(pool_size=(1, 2))(batch4_DC)
    drop4_DC = Dropout(DC_drop_value)(pool4_DC)

    conv5_DC = Conv2D(100,
                      kernel_size=(1, 7),
                      padding='same',
                      activation='relu')(drop4_DC)
    batch5_DC = BatchNormalization()(conv5_DC)
    drop5_DC = Dropout(DC_drop_value)(batch5_DC)

    conv6_DC = Conv2D(100,
                      kernel_size=(1, 7),
                      padding='same',
                      activation='relu')(drop5_DC)
    batch6_DC = BatchNormalization()(conv6_DC)
    drop6_DC = Dropout(DC_drop_value)(batch6_DC)

    conv7_DC = Conv2D(100,
                      kernel_size=(1, 1),
                      padding='same',
                      activation='relu')(drop6_DC)
    batch7_DC = BatchNormalization()(conv7_DC)
    drop7_DC = Dropout(DC_drop_value)(batch7_DC)

    conv8_DC = Conv2D(100,
                      kernel_size=(1, 1),
                      padding='same',
                      activation='relu')(drop7_DC)
    batch8_DC = BatchNormalization()(conv8_DC)
    drop8_DC = Dropout(DC_drop_value)(batch8_DC)

    flat_DC = Flatten()(drop8_DC)

    # ICECUBE NEAR DEEPCORE #
    #print("Train Data IC", X_IC.shape)
    strings_IC = X_IC.shape[1]
    dom_per_string_IC = X_IC.shape[2]
    dom_variables_IC = X_IC.shape[3]

    # Conv DC + batch normalization, later dropout and maxpooling
    input_IC = Input(shape=(strings_IC, dom_per_string_IC, dom_variables_IC))

    conv1_IC = Conv2D(100,
                      kernel_size=(strings_IC, 5),
                      padding='same',
                      activation='tanh')(input_IC)
    batch1_IC = BatchNormalization()(conv1_IC)
    pool1_IC = MaxPooling2D(pool_size=(1, 2))(batch1_IC)
    drop1_IC = Dropout(IC_drop_value)(pool1_IC)

    conv2_IC = Conv2D(100,
                      kernel_size=(strings_IC, 7),
                      padding='same',
                      activation='relu')(drop1_IC)
    batch2_IC = BatchNormalization()(conv2_IC)
    drop2_IC = Dropout(IC_drop_value)(batch2_IC)

    conv3_IC = Conv2D(100,
                      kernel_size=(strings_IC, 7),
                      padding='same',
                      activation='relu')(drop2_IC)
    batch3_IC = BatchNormalization()(conv3_IC)
    drop3_IC = Dropout(IC_drop_value)(batch3_IC)

    conv4_IC = Conv2D(100,
                      kernel_size=(strings_IC, 3),
                      padding='valid',
                      activation='relu')(drop3_IC)
    batch4_IC = BatchNormalization()(conv4_IC)
    pool4_IC = MaxPooling2D(pool_size=(1, 2))(batch4_IC)
    drop4_IC = Dropout(IC_drop_value)(pool4_IC)

    conv5_IC = Conv2D(100,
                      kernel_size=(1, 7),
                      padding='same',
                      activation='relu')(drop4_IC)
    batch5_IC = BatchNormalization()(conv5_IC)
    drop5_IC = Dropout(IC_drop_value)(batch5_IC)

    conv6_IC = Conv2D(100,
                      kernel_size=(1, 7),
                      padding='same',
                      activation='relu')(drop5_IC)
    batch6_IC = BatchNormalization()(conv6_IC)
    drop6_IC = Dropout(IC_drop_value)(batch6_IC)

    conv7_IC = Conv2D(100,
                      kernel_size=(1, 1),
                      padding='same',
                      activation='relu')(drop6_IC)
    batch7_IC = BatchNormalization()(conv7_IC)
    drop7_IC = Dropout(IC_drop_value)(batch7_IC)

    conv8_IC = Conv2D(100,
                      kernel_size=(1, 1),
                      padding='same',
                      activation='relu')(drop7_IC)
    batch8_IC = BatchNormalization()(conv8_IC)
    drop8_IC = Dropout(IC_drop_value)(batch8_IC)

    flat_IC = Flatten()(drop8_IC)

    # PUT TOGETHER #
    concatted = concatenate([flat_DC, flat_IC])

    full1 = Dense(300, activation='relu')(concatted)
    batch1_full = BatchNormalization()(full1)
    dropf = Dropout(connected_drop_value)(batch1_full)

    output = Dense(num_labels, activation='linear')(dropf)
    model_DC = Model(inputs=[input_DC, input_IC], outputs=output)

    return model_DC
Beispiel #13
0
    return K.mean(K.categorical_crossentropy(y_pred, y_true), axis=-1)


def identity_loss(y_true, y_pred):
    return y_pred


#%%
weight_masks = Input((128, 128, 1))
true_masks = Input((128, 128, 2))
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = Lambda(lambda x: x / 255)(inputs)

c1 = Conv2D(8, (3, 3), activation='relu', padding='same')(s)
c1 = Conv2D(8, (3, 3), activation='relu', padding='same')(c1)
p1 = MaxPooling2D((2, 2))(c1)

c2 = Conv2D(16, (3, 3), activation='relu', padding='same')(p1)
c2 = Conv2D(16, (3, 3), activation='relu', padding='same')(c2)
p2 = MaxPooling2D((2, 2))(c2)

c3 = Conv2D(32, (3, 3), activation='relu', padding='same')(p2)
c3 = Conv2D(32, (3, 3), activation='relu', padding='same')(c3)
p3 = MaxPooling2D((2, 2))(c3)

c4 = Conv2D(64, (3, 3), activation='relu', padding='same')(p3)
c4 = Conv2D(64, (3, 3), activation='relu', padding='same')(c4)
p4 = MaxPooling2D(pool_size=(2, 2))(c4)

c5 = Conv2D(128, (3, 3), activation='relu', padding='same')(p4)
c5 = Conv2D(128, (3, 3), activation='relu', padding='same')(c5)
def main(__):

    # TODO: Build the Final Test Neural Network in Keras Here
    model = Sequential()
    # Cropping to 90x320x3 that only with the view of roads
    model.add(
        Cropping2D(cropping=((50, 20), (0, 0)), input_shape=(160, 320, 3)))

    #Normalization of the images
    model.add(Lambda(lambda x: x / 255.0 - 0.5))

    # Layer 1: 43x158x24
    model.add(Convolution2D(24, 5, 5))
    model.add(MaxPooling2D((2, 2)))
    model.add(Activation('relu'))
    # Layer 2: 20x77x36
    model.add(Convolution2D(36, 5, 5))
    model.add(MaxPooling2D((2, 2)))
    model.add(Activation('relu'))
    #Layer 3: 8x37x48
    model.add(Convolution2D(48, 5, 5))
    model.add(MaxPooling2D((2, 2)))
    model.add(Activation('relu'))
    #Layer 4: 6x35x64
    model.add(Convolution2D(64, 3, 3))
    model.add(Activation('relu'))
    #Layer 5: 4x33x64
    model.add(Convolution2D(64, 3, 3))
    model.add(Activation('relu'))
    model.add(Flatten())
    #Layer 6: fully connected layer
    model.add(Dense(100))
    #Layer 7: fully connected layer
    model.add(Dense(50))
    #Layer 8: fully connected layer
    model.add(Dense(10))
    #Layer 9: fully connected layer
    model.add(Dense(1))

    #Reading from the command flag to decide wether transfer learning or not
    if FLAGS.re_training is True:
        model.load_weights('my_model_weights.h5')
        print(
            'The weights of the model has been loaded from my_model_weights.h5...'
        )

    #Using adam optimizer
    model.compile(optimizer='adam', loss='mse')

    history = model.fit_generator(train_generator,
                                  samples_per_epoch=len(train_samples),
                                  validation_data=validation_generator,
                                  nb_val_samples=len(validation_samples),
                                  nb_epoch=2,
                                  verbose=1)

    ### print the keys contained in the history object
    print(history.history.keys())
    ### plot the training and validation loss for each epoch

    plt.plot(history.history['loss'])
    plt.plot(history.history['val_loss'])
    plt.title('model mean squared error loss')
    plt.ylabel('mean squared error loss')
    plt.xlabel('epoch')
    plt.legend(['training set', 'validation set'], loc='upper right')
    plt.show()

    #Save the model and its weights
    model.save('model.h5')
    model.save_weights('my_model_weights.h5')
    exit()
Beispiel #15
0
def __create_pre_residual_of_residual(nb_classes,
                                      img_input,
                                      include_top,
                                      depth=28,
                                      width=1,
                                      dropout=0.0):
    '''
    Creates a Residual Network of Residual Network with specified parameters

    Example : To create a Pre-RoR model, use k = 1
              model = ResidualOfResidual(depth=28, width=1) # Pre-RoR-3

              To create a RoR-WRN model, use k > 1
              model = ResidualOfResidual(depth=28, width=10) # Pre-RoR-3,  RoR-3-WRN-28-10

    Args:
        nb_classes: number of classes
        img_input: tuple of shape (channels, rows, columns) or (rows, columns, channels)
        include_top: flag to include the final Dense layer
        depth: depth of the network
        width: width of the network
        dropout: Adds dropout if value is greater than 0.0.
                 Note : Generally not used in RoR

    Returns: a Keras Model
    '''

    N = (depth - 4) // 6

    channel_axis = 1 if K.image_dim_ordering() == 'th' else -1

    # Initial convolution layer
    x = Conv2D(16, (3, 3), padding='same',
               kernel_initializer='he_normal')(img_input)
    nb_conv = 4  # Dont count 4 long residual connections in WRN models

    conv0_level1_shortcut = Conv2D(64 * width, (1, 1),
                                   padding='same',
                                   strides=(4, 4),
                                   name='conv0_level1_shortcut')(x)

    conv1_level2_shortcut = Conv2D(16 * width, (1, 1),
                                   padding='same',
                                   name='conv1_level2_shortcut')(x)
    for i in range(N):
        initial = (i == 0)
        x = __initial_conv_block(x, k=width, dropout=dropout, initial=initial)
        nb_conv += 2

    # Add Level 2 shortcut
    x = add([x, conv1_level2_shortcut])

    x = MaxPooling2D((2, 2))(x)

    conv2_level2_shortcut = Conv2D(32 * width, (1, 1),
                                   padding='same',
                                   name='conv2_level2_shortcut')(x)
    for i in range(N):
        x = __conv_block(x, k=width, dropout=dropout)
        nb_conv += 2

    # Add Level 2 shortcut
    x = add([x, conv2_level2_shortcut])

    x = MaxPooling2D((2, 2))(x)

    conv3_level2_shortcut = Conv2D(64 * width, (1, 1),
                                   padding='same',
                                   name='conv3_level2_shortcut')(x)
    for i in range(N):
        x = __conv_block(x, nb_filters=64, k=width, dropout=dropout)
        nb_conv += 2

    # Add Level 2 shortcut
    x = add([x, conv3_level2_shortcut])

    # Add Level 1 shortcut
    x = add([x, conv0_level1_shortcut])
    x = BatchNormalization(axis=channel_axis)(x)
    x = Activation('relu')(x)

    x = AveragePooling2D((8, 8))(x)

    if include_top:
        x = Flatten()(x)
        x = Dense(nb_classes, activation='softmax')(x)

    return x
Beispiel #16
0
def pooling(x, ks, st, name):
    x = MaxPooling2D((ks, ks), strides=(st, st), name=name)(x)
    return x
Beispiel #17
0
def get_unet(input_img, n_filters=16, dropout=0.5, batchnorm=True):
    c01 = conv2d_block(input_img,
                       n_filters=n_filters * 1,
                       kernel_size=3,
                       batchnorm=batchnorm)
    p01 = MaxPooling2D((2, 2))(c01)
    d01 = Dropout(dropout * 0.05)(p01)

    c02 = conv2d_block(d01,
                       n_filters=n_filters * 2,
                       kernel_size=3,
                       batchnorm=batchnorm)
    p02 = MaxPooling2D((2, 2))(c02)
    d02 = Dropout(dropout)(p02)

    c1 = conv2d_block(d02,
                      n_filters=n_filters * 4,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p1 = MaxPooling2D((2, 2))(c1)
    d1 = Dropout(dropout)(p1)

    c2 = conv2d_block(d1,
                      n_filters=n_filters * 8,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p2 = MaxPooling2D((2, 2))(c2)
    d2 = Dropout(dropout)(p2)

    c3 = conv2d_block(d2,
                      n_filters=n_filters * 16,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p3 = MaxPooling2D((2, 2))(c3)
    d3 = Dropout(dropout)(p3)

    c4 = conv2d_block(d3,
                      n_filters=n_filters * 32,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p4 = MaxPooling2D((2, 2))(c4)
    d4 = Dropout(dropout)(p4)

    c5 = conv2d_block(p4,
                      n_filters=n_filters * 64,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u6 = Conv2DTranspose(n_filters * 32, (3, 3),
                         strides=(2, 2),
                         padding='same')(c5)
    u6 = concatenate([u6, c4])
    u6 = Dropout(dropout)(u6)
    c6 = conv2d_block(u6,
                      n_filters=n_filters * 32,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u7 = Conv2DTranspose(n_filters * 16, (3, 3),
                         strides=(2, 2),
                         padding='same')(c6)
    u7 = concatenate([u7, c3])
    u7 = Dropout(dropout)(u7)
    c7 = conv2d_block(u7,
                      n_filters=n_filters * 16,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u8 = Conv2DTranspose(n_filters * 8, (3, 3), strides=(2, 2),
                         padding='same')(c7)
    u8 = concatenate([u8, c2])
    u8 = Dropout(dropout)(u8)
    c8 = conv2d_block(u8,
                      n_filters=n_filters * 8,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u9 = Conv2DTranspose(n_filters * 4, (3, 3), strides=(2, 2),
                         padding='same')(c8)
    u9 = concatenate([u9, c1], axis=3)
    u9 = Dropout(dropout)(u9)
    c9 = conv2d_block(u9,
                      n_filters=n_filters * 4,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u10 = Conv2DTranspose(n_filters * 2, (3, 3),
                          strides=(2, 2),
                          padding='same')(c9)
    u10 = concatenate([u10, c02], axis=3)
    u10 = Dropout(dropout)(u10)
    c10 = conv2d_block(u10,
                       n_filters=n_filters * 2,
                       kernel_size=3,
                       batchnorm=batchnorm)

    u11 = Conv2DTranspose(n_filters * 1, (3, 3),
                          strides=(2, 2),
                          padding='same')(c10)
    u11 = concatenate([u11, c01], axis=3)
    u11 = Dropout(dropout)(u11)
    c11 = conv2d_block(u11,
                       n_filters=n_filters * 1,
                       kernel_size=3,
                       batchnorm=batchnorm)

    outputs = Conv2D(1, (1, 1), activation='linear')(c11)

    model = Model(inputs=[input_img], outputs=[outputs])
    # model.compile(optimizer=Adam(lr=1e-3), loss=dice_coef_loss, metrics=[dice_coef])
    return model
def DenseNet(nb_dense_block=4,
             growth_rate=20,
             nb_filter=32,
             reduction=0.0,
             dropout_rate=0.0,
             weight_decay=0,
             classes=1000,
             weights_path=None):
    '''Instantiate the DenseNet 121 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    with tf.device('/gpu:0'):
        eps = 1.1e-5

        # compute compression factor
        compression = 1.0 - reduction

        # Handle Dimension Ordering for different backends
        global concat_axis
        if K.image_dim_ordering() == 'tf':
            concat_axis = 3
            img_input = Input(shape=(512, 512, 3), name='data')
        else:
            concat_axis = 1
            img_input = Input(shape=(3, 224, 224), name='data')

        # From architecture for ImageNet (Table 1 in the paper)
        nb_filter = 16  ##make this 16
        nb_layers = [3, 6, 12, 8]  #[6,12,24,16] # For DenseNet-121

        # Initial convolution
        x = ZeroPadding2D((1, 1), name='conv1_zeropadding')(img_input)
        x = Conv2D(nb_filter * 2, (7, 7),
                   strides=(4, 4),
                   name='conv1',
                   kernel_initializer='he_normal',
                   padding='same')(
                       x)  ### Put strides of 2*2 here and padding = valid
        x = BatchNormalization(axis=concat_axis, name='conv1_bn')(x)
        x = Scale(axis=concat_axis, name='conv1_scale')(x)
        x = Activation('relu', name='relu1')(x)
        #x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
        x = MaxPooling2D(
            (3, 3), strides=(2, 2),
            name='pool1')(x)  ### Put strides of 2*2 and pooling  3*3

        # Add dense blocks
        for block_idx in range(nb_dense_block - 1):
            stage = block_idx + 2
            x, nb_filter = dense_block(x,
                                       stage,
                                       nb_layers[block_idx],
                                       nb_filter,
                                       growth_rate,
                                       dropout_rate=dropout_rate,
                                       weight_decay=weight_decay)

            # Add transition_block
            x = transition_block(x,
                                 stage,
                                 nb_filter,
                                 compression=compression,
                                 dropout_rate=dropout_rate,
                                 weight_decay=weight_decay)
            nb_filter = int(nb_filter * compression)

        final_stage = stage + 1
        x, nb_filter = dense_block(x,
                                   final_stage,
                                   nb_layers[-1],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        #x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv'+str(final_stage)+'_blk_bn')(x)
        x = Scale(axis=concat_axis,
                  name='conv' + str(final_stage) + '_blk_scale')(x)
        '''x1 = Conv2D(32,(3,3),dilation_rate = 6, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv1', activation = 'relu',data_format='channels_last')(x)
	    x2 = Conv2D(32,(3,3),dilation_rate = 12, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv2', activation = 'relu',data_format='channels_last')(x)
	    x3 = Conv2D(32,(3,3),dilation_rate = 18, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv3', activation = 'relu',data_format='channels_last')(x)
	    x4 = Conv2D(32,(3,3),dilation_rate = 24, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv4', activation = 'relu',data_format='channels_last')(x)
	    
	    sum_b = add([x1,x2,x3,x4],name = 'sum_b')'''

        #x = Activation('relu', name='relu'+str(final_stage)+'_blk')(x)
        x = Conv2D(1, (1, 1), padding='valid',
                   kernel_initializer='he_normal')(x)

        model = Model(img_input, x, name='densenet')
        '''xp = model.get_layer(name = 'conv5_blk_scale').output
 	    x1 = Conv2D(16,(3,3),dilation_rate = 6, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv1', activation = 'relu',data_format='channels_last')(xp)
	    x2 = Conv2D(16,(3,3),dilation_rate = 12, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv2', activation = 'relu',data_format='channels_last')(xp)
	    x3 = Conv2D(16,(3,3),dilation_rate = 18, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv3', activation = 'relu',data_format='channels_last')(xp)
            x4 = Conv2D(16,(3,3),dilation_rate = 24, padding = 'same', kernel_initializer = 'he_normal', name = 'dil_conv4', activation = 'relu',data_format='channels_last')(xp)    
	    sum_b = add([x1,x2,x3,x4],name = 'sum_b')
	    #sum_b_activation = Activation('relu', name='relu_sum')(sum_b)
	    out = Conv2D(1,(1,1),padding = 'valid',kernel_initializer='he_normal',activation = 'relu')(sum_b)
	    model_2 = Model(inputs = model.input, outputs = out) '''
        if weights_path is not None:
            model.load_weights(weights_path)

        return model
def vgg_block(x, weight_decay):
    ''' # Block 1
    x = conv(x, 64, 3, "conv1_1", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 64, 3, "conv1_2", (weight_decay, 0))
    x = relu(x)
    x = pooling(x, 2, 2, "pool1_1")

    # Block 2
    x = conv(x, 128, 3, "conv2_1", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 128, 3, "conv2_2", (weight_decay, 0))
    x = relu(x)
    x = pooling(x, 2, 2, "pool2_1")

    # Block 3
    x = conv(x, 256, 3, "conv3_1", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 256, 3, "conv3_2", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 256, 3, "conv3_3", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 256, 3, "conv3_4", (weight_decay, 0))
    x = relu(x)
    x = pooling(x, 2, 2, "pool3_1")

    # Block 4
    x = conv(x, 512, 3, "conv4_1", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 512, 3, "conv4_2", (weight_decay, 0))
    x = relu(x)

    # Additional non vgg layers
    x = conv(x, 256, 3, "conv4_3_CPM", (weight_decay, 0))
    x = relu(x)
    x = conv(x, 128, 3, "conv4_4_CPM", (weight_decay, 0))
    x = relu(x)'''

    '''
    x = conv(x, 64, 3, "conv1_1", (weight_decay, 0),strides=(2,2))                 #64
    x = BatchNormalization(axis=bn_axis, epsilon=1e-5, momentum=0.9)(x)
    x = relu(x)
    x = conv(x, 64, 3, "conv1_2", (weight_decay, 0))                               #64
    x = BatchNormalization(axis=bn_axis, epsilon=1e-5, momentum=0.9)(x)
    x = relu(x)
    # Block 2
    x = conv(x, 128, 3, "conv2_1", (weight_decay, 0),strides=(2,2))                 #128
    x = BatchNormalization(axis=bn_axis, epsilon=1e-5, momentum=0.9)(x)
    x = relu(x)
    x = conv(x, 128, 3, "conv2_2", (weight_decay, 0),strides=(2,2))                 #256
    x = BatchNormalization(axis=bn_axis, epsilon=1e-5, momentum=0.9)(x)
    x = relu(x)
    '''
    bn_axis = 1 if K.image_data_format() == 'channels_first' else -1
    x = ZeroPadding2D((2, 2))(x)  # 对图片界面填充0,保证特征图的大小#
    x = conv(x, 64, 7, strides=(2, 2), name='conv1', weight_decay=(weight_decay, 0))  # 定义卷积层#
    x = BatchNormalization(axis=bn_axis, name='bn_conv1')(x)  # 批标准化#
    x = Activation('relu')(x)  # 激活函数#
    x = MaxPooling2D((3, 3), strides=(2, 2))(x)  # 最大池化层#

    x = STEM_block(x, [64, 64], 1, (weight_decay, 0))
    x = STEM_block(x, [64, 64], 2, (weight_decay, 0))
    x = STEM_block(x, [64, 64], 3, (weight_decay, 0))
    x = pooling(x, 2, 2)
    x = STEM_block(x, [128, 128], 4, (weight_decay, 0), change=True)
    x = STEM_block(x, [128, 128], 5, (weight_decay, 0))
    x = STEM_block(x, [128, 128], 6, (weight_decay, 0))
    x = STEM_block(x, [128, 128], 7, (weight_decay, 0))
    x1=x
    x = STEM_block(x, [256, 256], 8, (weight_decay, 0), change=True)
    x = STEM_block(x, [256, 256], 9, (weight_decay, 0))
    x = STEM_block(x, [256, 256], 10, (weight_decay, 0))
    x = STEM_block(x, [256, 256], 11, (weight_decay, 0))


    return x,x1
def conv_block(x, stage, branch, nb_filter, dropout_rate=None, weight_decay=0):
    '''Apply BatchNorm, Relu, bottleneck 1x1 Conv2D, 3x3 Conv2D, and option dropout
        # Arguments
            x: input tensor 
            stage: index for dense block
            branch: layer index within each dense block
            nb_filter: number of filters
            dropout_rate: dropout rate
            weight_decay: weight decay factor
    '''
    eps = 1.1e-5
    conv_name_base = 'conv' + str(stage) + '_' + str(branch)
    relu_name_base = 'relu' + str(stage) + '_' + str(branch)

    # 1x1 Convolution (Bottleneck layer)
    inter_channel = nb_filter * 3
    #x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x1_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x1_scale')(x)
    #x = Activation('relu', name=relu_name_base+'_x1')(x)
    x1 = Conv2D(inter_channel, (1, 1),
                name=conv_name_base + '_x1',
                kernel_initializer='he_normal',
                activation='relu')(x)

    x2_1 = Conv2D(inter_channel / 2, (1, 1),
                  name=conv_name_base + '_x2_1',
                  kernel_initializer='he_normal',
                  activation='relu')(x)
    x2_2 = Conv2D(inter_channel, (3, 3),
                  name=conv_name_base + '_x2_2',
                  kernel_initializer='he_normal',
                  activation='relu',
                  padding='same')(x2_1)

    x3_1 = Conv2D(inter_channel / 2, (1, 1),
                  name=conv_name_base + '_x3_1',
                  kernel_initializer='he_normal',
                  activation='relu')(x)
    x3_2 = Conv2D(inter_channel, (3, 3),
                  name=conv_name_base + '_x3_2',
                  kernel_initializer='he_normal',
                  activation='relu',
                  padding='same')(x3_1)
    x3_3 = Conv2D(inter_channel, (3, 3),
                  name=conv_name_base + '_x3_3',
                  kernel_initializer='he_normal',
                  activation='relu',
                  padding='same')(x3_2)

    x4_1 = MaxPooling2D((2, 2), strides=(1, 1), padding='same')(x)
    x4_2 = Conv2D(inter_channel, (1, 1),
                  name=conv_name_base + '_x4_2',
                  kernel_initializer='he_normal',
                  activation='relu')(x4_1)

    x5 = add([x1, x2_2, x3_3, x4_2])
    if dropout_rate:
        x5 = Dropout(dropout_rate)(x5)

    # 3x3 Convolution
    #x = BatchNormalization(epsilon=eps, axis=concat_axis, name=conv_name_base+'_x2_bn')(x)
    x = Scale(axis=concat_axis, name=conv_name_base + '_x2_scale')(x5)
    #x = Activation('relu', name=relu_name_base+'_x2')(x)
    #x = ZeroPadding2D((1, 1), name=conv_name_base+'_x2_zeropadding')(x)
    x = Conv2D(nb_filter, (1, 1),
               name=conv_name_base + '_x2',
               kernel_initializer='he_normal',
               activation='relu')(x)

    if dropout_rate:
        x = Dropout(dropout_rate)(x)

    return x
def train_model(itername, train_generator, train_size, valid_generator, valid_size, display_generator, display_size, \
                batch_size, yimagerange, ysize, xsize, epochs, modelfilename, modelfileext, modellayoutpicfilename, \
                modellayoutpicfileext, sMP, bdisplay = False, bdebug = False):
    # ...
    # Train model
    # ...
    # Inputs
    # ...
    # itername               : name of training iteration
    # train_generator        : variable pointing to function that retrieves next values from training generator
    # train_size             : total number of training data sets
    # valid_generator        : variable pointing to function that retrieves next values from validation generator
    # valid_size             : total number of validation data sets
    # display_generator      : variable pointing to function that retrieves next values from display generator
    # display_size           : total number of display data sets
    # batch_size             : batch size
    # yimagerange            : range of pixels used from source images in vertical direction
    # ysize                  : source image height in pixels
    # xsize                  : source image width in pixels
    # epochs                 : number of epochs
    # modelfilename          : file name in which model will be saved
    # modelfileext           : file extension for file in which model will be saved
    # modellayoutpicfilename : picture file in which model layout will be stored
    # modellayoutpicfileext  : file extension for picture file in which model layout will be stored
    # sMP                    : object containing model parameters that define the model layout
    # bdisplay               : boolean for 'display information'
    # bdebug                 : boolean for 'debug generator'

    # display information
    if bdisplay:

        # display number of batches
        print(
            '= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ='
        )
        print('Configuration:', itername)
        print(
            '- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -'
        )
        print('Batch size:', batch_size)

    # debug generator
    if bdebug:

        # loop through all training data
        for display_dataset in range(0, display_size, batch_size):

            # get and display training data
            get_and_display_generator_data(display_generator, display_dataset,
                                           display_size, bdisplay)

    # define Keras model
    model = Sequential()

    # define Keras input adjustments
    model.add(
        Cropping2D(cropping=((yimagerange[0], (ysize - yimagerange[1])), (0,
                                                                          0)),
                   input_shape=(ysize, xsize, 3)))
    model.add(Lambda(lambda x: (x / 255.0) - 0.5, \
                     input_shape = (3, (ysize - (yimagerange[0] + (ysize - yimagerange[1]))), xsize)))

    # define Keras convolutional layers
    for conv_layer in sMP.conv_layers:
        model.add(Conv2D(conv_layer.features, conv_layer.filter_size[0], conv_layer.filter_size[1], \
                         subsample = conv_layer.strides, activation = "relu", kernel_regularizer = sMP.regularizer))
        if conv_layer.busepooling: model.add(MaxPooling2D())

    # define Keras dense layers
    model.add(Flatten())
    for full_layer in sMP.full_layers:
        if (full_layer.keep_percentage < 1):
            model.add(Dropout(full_layer.keep_percentage))
        model.add(Dense(full_layer.features))

    # print the layout of the model
    plot_model(model, to_file = (modellayoutpicfilename + '_' + itername + modellayoutpicfileext), show_shapes = True, \
               show_layer_names = True)
    model.summary()

    # generate model
    model.compile(loss='mse', optimizer='adam')

    # train model
    history_object = model.fit_generator(train_generator, samples_per_epoch = np.int(train_size // batch_size), \
                                         validation_data = valid_generator, \
                                         nb_val_samples = np.int(valid_size // batch_size), nb_epoch = epochs, verbose = 1)

    # display information
    if bdisplay:

        # print the keys contained in the history object
        #print(history_object.history.keys())

        # plot the training and validation loss for each epoch
        plt.plot(history_object.history['loss'])
        plt.plot(history_object.history['val_loss'])
        plt.title('model mean squared error loss')
        plt.ylabel('mean squared error loss')
        plt.xlabel('epoch')
        plt.legend(['training set', 'validation set'], loc='upper right')
        plt.show()

    # save trained model
    model.save((modelfilename + '_' + itername + modelfileext))
def densenet161_model(img_rows,
                      img_cols,
                      color_type=1,
                      nb_dense_block=4,
                      growth_rate=48,
                      nb_filter=96,
                      reduction=0.5,
                      dropout_rate=0.0,
                      weight_decay=1e-4,
                      num_classes=None):
    '''
    DenseNet 161 Model for Keras
    Model Schema is based on 
    https://github.com/flyyufelix/DenseNet-Keras
    ImageNet Pretrained Weights 
    Theano: https://drive.google.com/open?id=0Byy2AcGyEVxfVnlCMlBGTDR3RGs
    TensorFlow: https://drive.google.com/open?id=0Byy2AcGyEVxfUDZwVjU2cFNidTA
    # Arguments
        nb_dense_block: number of dense blocks to add to end
        growth_rate: number of filters to add per dense block
        nb_filter: initial number of filters
        reduction: reduction factor of transition blocks.
        dropout_rate: dropout rate
        weight_decay: weight decay factor
        classes: optional number of classes to classify images
        weights_path: path to pre-trained weights
    # Returns
        A Keras model instance.
    '''
    eps = 1.1e-5

    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    if K.image_dim_ordering() == 'tf':
        concat_axis = 3
        img_input = Input(shape=(224, 224, 3), name='data')
    else:
        concat_axis = 1
        img_input = Input(shape=(3, 224, 224), name='data')

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [6, 12, 36, 24]  # For DenseNet-161

    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Convolution2D(nb_filter,
                      7,
                      7,
                      subsample=(2, 2),
                      name='conv1',
                      bias=False)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, name='conv1_bn')(x)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx + 2
        x, nb_filter = dense_block(x,
                                   stage,
                                   nb_layers[block_idx],
                                   nb_filter,
                                   growth_rate,
                                   dropout_rate=dropout_rate,
                                   weight_decay=weight_decay)

        # Add transition_block
        x = transition_block(x,
                             stage,
                             nb_filter,
                             compression=compression,
                             dropout_rate=dropout_rate,
                             weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x,
                               final_stage,
                               nb_layers[-1],
                               nb_filter,
                               growth_rate,
                               dropout_rate=dropout_rate,
                               weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps,
                           axis=concat_axis,
                           name='conv' + str(final_stage) + '_blk_bn')(x)
    x = Scale(axis=concat_axis,
              name='conv' + str(final_stage) + '_blk_scale')(x)
    x = Activation('relu', name='relu' + str(final_stage) + '_blk')(x)

    x_fc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_fc = Dense(1000, name='fc6')(x_fc)
    x_fc = Activation('softmax', name='prob')(x_fc)

    model = Model(img_input, x_fc, name='densenet')
    weights_path = densenet161_weights

    #    if K.image_dim_ordering() == 'th':
    #      # Use pre-trained weights for Theano backend
    #      weights_path = 'imagenet_models/densenet161_weights_th.h5'
    #    else:
    #      # Use pre-trained weights for Tensorflow backend
    #      weights_path = 'imagenet_models/densenet161_weights_tf.h5'

    model.load_weights(weights_path, by_name=True)

    # Truncate and replace softmax layer for transfer learning
    # Cannot use model.layers.pop() since model is not of Sequential() type
    # The method below works since pre-trained weights are stored in layers but not in the model
    x_newfc = GlobalAveragePooling2D(name='pool' + str(final_stage))(x)
    x_newfc = Dense(num_classes, name='fc6')(x_newfc)
    x_newfc = Activation('softmax', name='prob')(x_newfc)

    model = Model(img_input, x_newfc)

    # Learning rate is changed to 0.001
    #    sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    #    model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    return model
Beispiel #23
0
    s.login(fromaddr, passwd)
    text = msg.as_string()
    s.sendmail(fromaddr, toaddr, text)
    s.quit()


###################################################################################################

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(LRN2D, 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(LRN2D, 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(
Beispiel #24
0
def inception_block_1a(X):
    """
    Implementation of an inception block
    """

    X_3x3 = Conv2D(96, (1, 1),
                   data_format='channels_first',
                   name='inception_3a_3x3_conv1')(X)
    X_3x3 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_3x3_bn1')(X_3x3)
    X_3x3 = Activation('relu')(X_3x3)
    X_3x3 = ZeroPadding2D(padding=(1, 1), data_format='channels_first')(X_3x3)
    X_3x3 = Conv2D(128, (3, 3),
                   data_format='channels_first',
                   name='inception_3a_3x3_conv2')(X_3x3)
    X_3x3 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_3x3_bn2')(X_3x3)
    X_3x3 = Activation('relu')(X_3x3)

    X_5x5 = Conv2D(16, (1, 1),
                   data_format='channels_first',
                   name='inception_3a_5x5_conv1')(X)
    X_5x5 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_5x5_bn1')(X_5x5)
    X_5x5 = Activation('relu')(X_5x5)
    X_5x5 = ZeroPadding2D(padding=(2, 2), data_format='channels_first')(X_5x5)
    X_5x5 = Conv2D(32, (5, 5),
                   data_format='channels_first',
                   name='inception_3a_5x5_conv2')(X_5x5)
    X_5x5 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_5x5_bn2')(X_5x5)
    X_5x5 = Activation('relu')(X_5x5)

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

    X_1x1 = Conv2D(64, (1, 1),
                   data_format='channels_first',
                   name='inception_3a_1x1_conv')(X)
    X_1x1 = BatchNormalization(axis=1,
                               epsilon=0.00001,
                               name='inception_3a_1x1_bn')(X_1x1)
    X_1x1 = Activation('relu')(X_1x1)

    # CONCAT
    inception = concatenate([X_3x3, X_5x5, X_pool, X_1x1], axis=1)

    return inception
Beispiel #25
0
def DenseUNet(img_input, nb_dense_block=4, growth_rate=48, nb_filter=96, reduction=0.0, dropout_rate=0.0, weight_decay=1e-4, classes=1000, weights_path=None):
    '''Instantiate the DenseNet 161 architecture,
        # Arguments
            nb_dense_block: number of dense blocks to add to end
            growth_rate: number of filters to add per dense block
            nb_filter: initial number of filters
            reduction: reduction factor of transition blocks.
            dropout_rate: dropout rate
            weight_decay: weight decay factor
            classes: optional number of classes to classify images
            weights_path: path to pre-trained weights
        # Returns
            A Keras model instance.
    '''
    eps = 1.1e-5
    # compute compression factor
    compression = 1.0 - reduction

    # Handle Dimension Ordering for different backends
    global concat_axis
    concat_axis = 3

    # From architecture for ImageNet (Table 1 in the paper)
    nb_filter = 96
    nb_layers = [6,12,36,24] # For DenseNet-161
    box = []
    # Initial convolution
    x = ZeroPadding2D((3, 3), name='conv1_zeropadding')(img_input)
    x = Conv2D(nb_filter, (7, 7), strides=(2, 2), name='conv1', use_bias=False, trainable=True)(x)
    x = BatchNormalization(epsilon=eps, axis=concat_axis, momentum = 1, name='conv1_bn', trainable=False)(x, training=False)
    x = Scale(axis=concat_axis, name='conv1_scale')(x)
    x = Activation('relu', name='relu1')(x)
    box.append(x)
    x = ZeroPadding2D((1, 1), name='pool1_zeropadding')(x)
    x = MaxPooling2D((3, 3), strides=(2, 2), name='pool1')(x)

    # Add dense blocks
    for block_idx in range(nb_dense_block - 1):
        stage = block_idx+2
        x, nb_filter = dense_block(x, stage, nb_layers[block_idx], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)
        box.append(x)
        # Add transition_block
        x = transition_block(x, stage, nb_filter, compression=compression, dropout_rate=dropout_rate, weight_decay=weight_decay)
        nb_filter = int(nb_filter * compression)

    final_stage = stage + 1
    x, nb_filter = dense_block(x, final_stage, nb_layers[-1], nb_filter, growth_rate, dropout_rate=dropout_rate, weight_decay=weight_decay)

    x = BatchNormalization(epsilon=eps, axis=concat_axis, momentum = 1, name='conv'+str(final_stage)+'_blk_bn', trainable=False)(x, training=False)
    x = Scale(axis=concat_axis, name='conv'+str(final_stage)+'_blk_scale')(x)
    x = Activation('relu', name='relu'+str(final_stage)+'_blk')(x)
    box.append(x)

    up0 = UpSampling2D(size=(2,2))(x)
    conv_up0 = Conv2D(768, (3, 3), padding="same", name = "conv_up0", trainable=True)(up0)
    bn_up0 = BatchNormalization(name = "bn_up0", momentum = 1, trainable=False)(conv_up0, training=False)
    ac_up0 = Activation('relu', name='ac_up0')(bn_up0)

    up1 = UpSampling2D(size=(2,2))(ac_up0)
    conv_up1 = Conv2D(384, (3, 3), padding="same", name = "conv_up1", trainable=True)(up1)
    bn_up1 = BatchNormalization(name = "bn_up1", momentum = 1, trainable=False)(conv_up1, training=False)
    ac_up1 = Activation('relu', name='ac_up1')(bn_up1)

    up2 = UpSampling2D(size=(2,2))(ac_up1)
    conv_up2 = Conv2D(96, (3, 3), padding="same", name = "conv_up2", trainable=True)(up2)
    bn_up2 = BatchNormalization(name = "bn_up2", momentum = 1, trainable=False)(conv_up2, training=False)
    ac_up2 = Activation('relu', name='ac_up2')(bn_up2)

    up3 = UpSampling2D(size=(2,2))(ac_up2)
    conv_up3 = Conv2D(96, (3, 3), padding="same", name = "conv_up3", trainable=True)(up3)
    bn_up3 = BatchNormalization(name = "bn_up3", momentum = 1, trainable=False)(conv_up3, training=False)
    ac_up3 = Activation('relu', name='ac_up3')(bn_up3)

    up4 = UpSampling2D(size=(2, 2))(ac_up3)
    conv_up4 = Conv2D(64, (3, 3), padding="same", name="conv_up4", trainable=True)(up4)
    bn_up4 = BatchNormalization(name="bn_up4", momentum = 1, trainable=False)(conv_up4, training=False)
    ac_up4 = Activation('relu', name='ac_up4')(bn_up4)

    x = Conv2D(3, (1,1), padding="same", name='dense167classifer', trainable=True)(ac_up4)

    return ac_up4, x
Beispiel #26
0
def faceRecoModel(input_shape):
    """
    Implementation of the Inception model used for FaceNet
    
    Arguments:
    input_shape -- shape of the images of the dataset
    Returns:
    model -- a Model() instance in Keras
    """

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

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

    # First Block
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(X)
    X = BatchNormalization(axis=1, name='bn1')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D((3, 3), strides=2)(X)

    # Second Block
    X = Conv2D(64, (1, 1), strides=(1, 1), name='conv2')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn2')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)

    # Second Block
    X = Conv2D(192, (3, 3), strides=(1, 1), name='conv3')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn3')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D(pool_size=3, strides=2)(X)

    # Inception 1: a/b/c
    X = inception_block_1a(X)
    X = inception_block_1b(X)
    X = inception_block_1c(X)

    # Inception 2: a/b
    X = inception_block_2a(X)
    X = inception_block_2b(X)

    # Inception 3: a/b
    X = inception_block_3a(X)
    X = inception_block_3b(X)

    # Top layer
    X = AveragePooling2D(pool_size=(3, 3),
                         strides=(1, 1),
                         data_format='channels_first')(X)
    X = Flatten()(X)
    X = Dense(128, name='dense_layer')(X)

    # L2 normalization
    X = Lambda(lambda x: K.l2_normalize(x, axis=1))(X)

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

    return model
def get_unet(input_img, n_filters=16, dropout=0.5, batchnorm=True):
    # contracting path
    c1 = conv2d_block(input_img,
                      n_filters=n_filters * 1,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p1 = MaxPooling2D((2, 2))(c1)
    p1 = Dropout(dropout * 0.5)(p1)

    c2 = conv2d_block(p1,
                      n_filters=n_filters * 2,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p2 = MaxPooling2D((2, 2))(c2)
    p2 = Dropout(dropout)(p2)

    c3 = conv2d_block(p2,
                      n_filters=n_filters * 4,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p3 = MaxPooling2D((2, 2))(c3)
    p3 = Dropout(dropout)(p3)

    c4 = conv2d_block(p3,
                      n_filters=n_filters * 8,
                      kernel_size=3,
                      batchnorm=batchnorm)
    p4 = MaxPooling2D(pool_size=(2, 2))(c4)
    p4 = Dropout(dropout)(p4)

    c5 = conv2d_block(p4,
                      n_filters=n_filters * 16,
                      kernel_size=3,
                      batchnorm=batchnorm)

    # expansive path
    u6 = Conv2DTranspose(n_filters * 8, (3, 3), strides=(2, 2),
                         padding='same')(c5)
    u6 = concatenate([u6, c4])
    u6 = Dropout(dropout)(u6)
    c6 = conv2d_block(u6,
                      n_filters=n_filters * 8,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u7 = Conv2DTranspose(n_filters * 4, (3, 3), strides=(2, 2),
                         padding='same')(c6)
    u7 = concatenate([u7, c3])
    u7 = Dropout(dropout)(u7)
    c7 = conv2d_block(u7,
                      n_filters=n_filters * 4,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u8 = Conv2DTranspose(n_filters * 2, (3, 3), strides=(2, 2),
                         padding='same')(c7)
    u8 = concatenate([u8, c2])
    u8 = Dropout(dropout)(u8)
    c8 = conv2d_block(u8,
                      n_filters=n_filters * 2,
                      kernel_size=3,
                      batchnorm=batchnorm)

    u9 = Conv2DTranspose(n_filters * 1, (3, 3), strides=(2, 2),
                         padding='same')(c8)
    u9 = concatenate([u9, c1], axis=3)
    u9 = Dropout(dropout)(u9)
    c9 = conv2d_block(u9,
                      n_filters=n_filters * 1,
                      kernel_size=3,
                      batchnorm=batchnorm)

    outputs = Conv2D(1, (1, 1), activation='sigmoid')(c9)
    model = Model(inputs=[input_img], outputs=[outputs])
    return model
Beispiel #28
0
def create_model():
    myInput = Input(shape=(192, 192, 3))

    x = ZeroPadding2D(padding=(3, 3), input_shape=(192, 192, 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(LRN2D, 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(LRN2D, 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, 3), strides=1)(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=((1, 1), (1,
                                                        1)))(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 = AveragePooling2D(pool_size=(3, 3),
                                         strides=1)(inception_3a)
    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=((1, 1), (1,
                                                        1)))(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 = utils.conv2d_bn(inception_3b,
                                       layer='inception_3c_3x3',
                                       cv1_out=128,
                                       cv1_filter=(1, 1),
                                       cv2_out=256,
                                       cv2_filter=(3, 3),
                                       cv2_strides=(2, 2),
                                       padding=(1, 1))

    inception_3c_5x5 = utils.conv2d_bn(inception_3b,
                                       layer='inception_3c_5x5',
                                       cv1_out=32,
                                       cv1_filter=(1, 1),
                                       cv2_out=64,
                                       cv2_filter=(5, 5),
                                       cv2_strides=(2, 2),
                                       padding=(2, 2))

    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 = utils.conv2d_bn(inception_3c,
                                       layer='inception_4a_3x3',
                                       cv1_out=96,
                                       cv1_filter=(1, 1),
                                       cv2_out=192,
                                       cv2_filter=(3, 3),
                                       cv2_strides=(1, 1),
                                       padding=(1, 1))
    inception_4a_5x5 = utils.conv2d_bn(inception_3c,
                                       layer='inception_4a_5x5',
                                       cv1_out=32,
                                       cv1_filter=(1, 1),
                                       cv2_out=64,
                                       cv2_filter=(5, 5),
                                       cv2_strides=(1, 1),
                                       padding=(2, 2))

    inception_4a_pool = AveragePooling2D(pool_size=(3, 3),
                                         strides=1)(inception_3c)
    inception_4a_pool = utils.conv2d_bn(inception_4a_pool,
                                        layer='inception_4a_pool',
                                        cv1_out=128,
                                        cv1_filter=(1, 1),
                                        padding=((1, 1), (1, 1)))
    inception_4a_1x1 = utils.conv2d_bn(inception_3c,
                                       layer='inception_4a_1x1',
                                       cv1_out=256,
                                       cv1_filter=(1, 1))
    inception_4a = concatenate([
        inception_4a_3x3, inception_4a_5x5, inception_4a_pool, inception_4a_1x1
    ],
                               axis=3)

    #inception4e
    inception_4e_3x3 = utils.conv2d_bn(inception_4a,
                                       layer='inception_4e_3x3',
                                       cv1_out=160,
                                       cv1_filter=(1, 1),
                                       cv2_out=256,
                                       cv2_filter=(3, 3),
                                       cv2_strides=(2, 2),
                                       padding=(1, 1))
    inception_4e_5x5 = utils.conv2d_bn(inception_4a,
                                       layer='inception_4e_5x5',
                                       cv1_out=64,
                                       cv1_filter=(1, 1),
                                       cv2_out=128,
                                       cv2_filter=(5, 5),
                                       cv2_strides=(2, 2),
                                       padding=(2, 2))
    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 = utils.conv2d_bn(inception_4e,
                                       layer='inception_5a_3x3',
                                       cv1_out=96,
                                       cv1_filter=(1, 1),
                                       cv2_out=384,
                                       cv2_filter=(3, 3),
                                       cv2_strides=(1, 1),
                                       padding=(1, 1))

    inception_5a_pool = AveragePooling2D(pool_size=(3, 3),
                                         strides=1)(inception_4e)
    inception_5a_pool = utils.conv2d_bn(inception_5a_pool,
                                        layer='inception_5a_pool',
                                        cv1_out=96,
                                        cv1_filter=(1, 1),
                                        padding=((1, 1), (1, 1)))
    inception_5a_1x1 = utils.conv2d_bn(inception_4e,
                                       layer='inception_5a_1x1',
                                       cv1_out=256,
                                       cv1_filter=(1, 1))

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

    #inception_5b
    inception_5b_3x3 = utils.conv2d_bn(inception_5a,
                                       layer='inception_5b_3x3',
                                       cv1_out=96,
                                       cv1_filter=(1, 1),
                                       cv2_out=384,
                                       cv2_filter=(3, 3),
                                       cv2_strides=(1, 1),
                                       padding=(1, 1))
    inception_5b_pool = MaxPooling2D(pool_size=3, strides=1)(inception_5a)
    inception_5b_pool = utils.conv2d_bn(inception_5b_pool,
                                        layer='inception_5b_pool',
                                        cv1_out=96,
                                        cv1_filter=(1, 1))
    inception_5b_pool = ZeroPadding2D(padding=((1, 1), (1,
                                                        1)))(inception_5b_pool)

    inception_5b_1x1 = utils.conv2d_bn(inception_5a,
                                       layer='inception_5b_1x1',
                                       cv1_out=256,
                                       cv1_filter=(1, 1))
    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: K.l2_normalize(x, axis=1),
                        name='norm_layer')(dense_layer)

    return Model(inputs=[myInput], outputs=norm_layer)
Beispiel #29
0
    Computes the precision, a metric for multi-label classification of
    how many selected items are relevant.
    """
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision


## build the unet model with keras
inputs = Input((IMG_HEIGHT, IMG_WIDTH, IMG_CHANNELS))
s = Lambda(lambda x: x / 255) (inputs)

conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(s)
conv1 = Conv2D(64, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool1)
conv2 = Conv2D(128, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool2)
conv3 = Conv2D(256, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool3)
conv4 = Conv2D(512, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(conv4)
drop4 = Dropout(0.5)(conv4)
#pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)

#conv5 = Conv2D(1024, 3, activation = 'relu', padding = 'same', kernel_initializer = 'he_normal')(pool4)
Beispiel #30
0
def get_siamese_model(input_shape):
    """
        Model architecture based on the one provided in: http://www.cs.utoronto.ca/~gkoch/files/msc-thesis.pdf
    """

    # Define the tensors for the two input images
    left_input = Input(input_shape)
    right_input = Input(input_shape)

    # Convolutional Neural Network
    model = Sequential()
    model.add(
        Conv2D(64, (10, 10),
               activation='relu',
               input_shape=input_shape,
               kernel_initializer=initialize_weights,
               kernel_regularizer=l2(2e-4)))
    model.add(MaxPooling2D())
    model.add(
        Conv2D(128, (7, 7),
               activation='relu',
               kernel_initializer=initialize_weights,
               bias_initializer=initialize_bias,
               kernel_regularizer=l2(2e-4)))
    model.add(MaxPooling2D())
    model.add(
        Conv2D(128, (4, 4),
               activation='relu',
               kernel_initializer=initialize_weights,
               bias_initializer=initialize_bias,
               kernel_regularizer=l2(2e-4)))
    model.add(MaxPooling2D())
    model.add(
        Conv2D(256, (4, 4),
               activation='relu',
               kernel_initializer=initialize_weights,
               bias_initializer=initialize_bias,
               kernel_regularizer=l2(2e-4)))
    model.add(Flatten())
    model.add(
        Dense(10,
              activation='sigmoid',
              kernel_regularizer=l2(1e-3),
              kernel_initializer=initialize_weights,
              bias_initializer=initialize_bias))

    # Generate the encodings (feature vectors) for the two images
    encoded_l = model(left_input)
    encoded_r = model(right_input)

    # Add a customized layer to compute the absolute difference between the encodings
    L1_layer = Lambda(lambda tensors: K.abs(tensors[0] - tensors[1]))
    L1_distance = L1_layer([encoded_l, encoded_r])

    # Add a dense layer with a sigmoid unit to generate the similarity score
    prediction = Dense(1,
                       activation='sigmoid',
                       bias_initializer=initialize_bias)(L1_distance)

    # Connect the inputs with the outputs
    siamese_net = Model(inputs=[left_input, right_input], outputs=prediction)

    # return the model
    return siamese_net