Exemple #1
0
def globalpool_matlab_mri_vgg_3d(input_shape, num_classes):
    # Build the network of vgg for 10 classes with massive dropout and weight decay as described in the paper.
    # change padding from same to valid 18.9.8
    # 9.20 divided kernel numbers with 8
    model = Sequential()
    weight_decay = 0.0001  #0.0005 # 0.0001

    model.add(Conv3D(8, (3, 3, 3), padding='same',
                     input_shape=input_shape))  # 9.13 64 to 32 除以2
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.3))

    model.add(Conv3D(8, (3, 3, 3), padding='same'))  # 9.13 64 to 32
    model.add(Activation('relu'))
    model.add(BatchNormalization())

    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    model.add(Conv3D(32, (3, 3, 3), padding='same'))  # 9.13 128 to 64
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    model.add(Conv3D(32, (3, 3, 3), padding='same'))  # 9.13 128 to 64
    model.add(Activation('relu'))
    model.add(BatchNormalization())

    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    model.add(Conv3D(64, (3, 3, 3), padding='same'))  # 9.13 256 to 128
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    model.add(Conv3D(64, (3, 3, 3), padding='same'))  # 9.13 256 to 128
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    model.add(Conv3D(128, (3, 3, 3), padding='same'))
    model.add(Activation('relu'))
    model.add(BatchNormalization())

    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    model.add(Conv3D(128, (3, 3, 3),
                     padding='same'))  # 9.13 512 to 256 same to valid
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    #model.add(Conv3D(512, (3, 3, 3), padding='same', kernel_regularizer=regularizers.l2(weight_decay)))
    model.add(Conv3D(256, (3, 3, 3), padding='same'))
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    #model.add(Dropout(0.4))

    model.add(Conv3D(256, (3, 3, 3), padding='same'))  # 512 to 256
    model.add(Activation('relu'))
    model.add(BatchNormalization())
    # model.add(Dropout(0.2))
    model.add(GlobalAveragePooling3D())

    model.add(Dense(num_classes))
    model.add(Activation('softmax'))
    return model
Exemple #2
0
def C3D(weights='sports1M'):
    """Instantiates a C3D Kerasl model
    
    Keyword arguments:
    weights -- weights to load into model. (default is sports1M)
    
    Returns:
    A Keras model.
    
    """

    if weights not in {'sports1M', None}:
        raise ValueError('weights should be either be sports1M or None')

    if K.image_data_format() == 'channels_last':
        shape = (16, 112, 112, 3)
    else:
        shape = (3, 16, 112, 112)

    model = Sequential()
    model.add(
        Conv3D(64,
               3,
               activation='relu',
               padding='same',
               name='conv1',
               input_shape=shape))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     padding='same',
                     name='pool1'))

    model.add(Conv3D(128, 3, activation='relu', padding='same', name='conv2'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool2'))

    model.add(Conv3D(256, 3, activation='relu', padding='same', name='conv3a'))
    model.add(Conv3D(256, 3, activation='relu', padding='same', name='conv3b'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool3'))

    model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv4a'))
    model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv4b'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool4'))

    model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv5a'))
    model.add(Conv3D(512, 3, activation='relu', padding='same', name='conv5b'))
    model.add(ZeroPadding3D(padding=(0, 1, 1)))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool5'))

    model.add(Flatten())

    model.add(Dense(4096, activation='relu', name='fc6'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu', name='fc7'))
    model.add(Dropout(0.5))
    model.add(Dense(487, activation='softmax', name='fc8'))

    if weights == 'sports1M':
        model.load_weights(cfg.c3d_model_weights)

    return model
Exemple #3
0
def c3d_base(input_shape=(16, 112, 112, 3), weights=False, weights_file=None):
    """
    Build base model for c3D using Seqential API.

    This model builds a base c3D network. http://vlg.cs.dartmouth.edu/c3d/
    """
    print(input_shape)
    c3d_model = models.Sequential()
    # 1st layer group
    c3d_model.add(
        Conv3D(64, (3, 3, 3),
               activation='relu',
               name='conv1',
               input_shape=input_shape,
               strides=(1, 1, 1),
               padding="same"))

    c3d_model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     padding='valid',
                     name='pool1'))

    # 2nd layer group
    c3d_model.add(
        Conv3D(128, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv2',
               strides=(1, 1, 1)))
    c3d_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool2'))

    # 3rd layer group
    c3d_model.add(
        Conv3D(256, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv3a',
               strides=(1, 1, 1)))
    c3d_model.add(
        Conv3D(256, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv3b',
               strides=(1, 1, 1)))
    c3d_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool3'))

    # 4th layer group
    c3d_model.add(
        Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv4a',
               strides=(1, 1, 1)))
    c3d_model.add(
        Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv4b',
               strides=(1, 1, 1)))
    c3d_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool4'))

    # 5th layer group
    c3d_model.add(
        Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv5a',
               strides=(1, 1, 1)))
    c3d_model.add(
        Conv3D(512, (3, 3, 3),
               activation='relu',
               padding='same',
               name='conv5b',
               strides=(1, 1, 1)))

    c3d_model.add(ZeroPadding3D(padding=(0, 1, 1)))
    c3d_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding='valid',
                     name='pool5'))
    c3d_model.add(layers.Flatten())

    # FC layers group
    c3d_model.add(layers.Dense(4096, activation='relu', name='fc6'))
    c3d_model.add(layers.Dropout(.5))
    c3d_model.add(layers.Dense(4096, activation='relu', name='fc7'))
    c3d_model.add(layers.Dropout(.5))
    c3d_model.add(layers.Dense(487, activation='softmax', name='fc8'))

    if weights:
        c3d_model.load_weights(weights_file)

    # Create new model
    # Get input
    new_input = c3d_model.input
    # Find the layer to connect
    hidden_layer = c3d_model.layers[-2].output
    # Connect a new layer on it
    new_output = layers.Dense(3, activation='softmax',
                              name='fc9')(hidden_layer)
    # Build a new model
    c3d_model_2 = models.Model(new_input, new_output)

    # Remove last layer and add our own

    # Fix all layers but last 3
    # for layer in c3d_model_2.layers[:17]:
    #     layer.trainable = False

    if summary:
        c3d_model_2.summary()
    return c3d_model_2
Exemple #4
0
    def c3d(self):
        """
        Build a 3D convolutional network, aka C3D.
            https://arxiv.org/pdf/1412.0767.pdf

        With thanks:
            https://gist.github.com/albertomontesg/d8b21a179c1e6cca0480ebdf292c34d2
        """
        model = Sequential()
        # 1st layer group
        model.add(
            Conv3D(64,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv1',
                   subsample=(1, 1, 1),
                   input_shape=self.input_shape))
        model.add(
            MaxPooling3D(pool_size=(1, 2, 2),
                         strides=(1, 2, 2),
                         border_mode='valid',
                         name='pool1'))
        # 2nd layer group
        model.add(
            Conv3D(128,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv2',
                   subsample=(1, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool2'))
        # 3rd layer group
        model.add(
            Conv3D(256,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv3a',
                   subsample=(1, 1, 1)))
        model.add(
            Conv3D(256,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv3b',
                   subsample=(1, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool3'))
        # 4th layer group
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv4a',
                   subsample=(1, 1, 1)))
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv4b',
                   subsample=(1, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool4'))

        # 5th layer group
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv5a',
                   subsample=(1, 1, 1)))
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv5b',
                   subsample=(1, 1, 1)))
        model.add(ZeroPadding3D(padding=(0, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool5'))
        model.add(Flatten())

        # FC layers group
        model.add(Dense(4096, activation='relu', name='fc6'))
        model.add(Dropout(0.5))
        model.add(Dense(4096, activation='relu', name='fc7'))
        model.add(Dropout(0.5))
        model.add(Dense(self.nb_classes, activation='softmax'))

        return model
Exemple #5
0
def getCoarse2FineModel(summary=True):

    # defined input
    videoclip_cropped = Input((c, t, h, w), name='input1')
    videoclip_original = Input((c, t, h, w), name='input2')
    last_frame_bigger = Input((c, h * upsample, w * upsample), name='input3')

    # coarse saliency model
    coarse_saliency_model = Sequential()
    coarse_saliency_model.add(
        Convolution3D(64,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv1',
                      subsample=(1, 1, 1),
                      input_shape=(c, t, h, w)))
    coarse_saliency_model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     border_mode='valid',
                     name='pool1'))
    coarse_saliency_model.add(
        Convolution3D(128,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv2',
                      subsample=(1, 1, 1)))
    coarse_saliency_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool2'))
    coarse_saliency_model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3a',
                      subsample=(1, 1, 1)))
    coarse_saliency_model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3b',
                      subsample=(1, 1, 1)))
    coarse_saliency_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool3'))
    coarse_saliency_model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4a',
                      subsample=(1, 1, 1)))
    coarse_saliency_model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4b',
                      subsample=(1, 1, 1)))
    coarse_saliency_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(4, 2, 2),
                     border_mode='valid',
                     name='pool4'))
    coarse_saliency_model.add(Reshape((512, 7, 7)))
    coarse_saliency_model.add(BatchNormalization())
    coarse_saliency_model.add(
        Convolution2D(256, 3, 3, init='glorot_uniform', border_mode='same'))
    coarse_saliency_model.add(LeakyReLU(alpha=.001))
    coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
    coarse_saliency_model.add(BatchNormalization())
    coarse_saliency_model.add(
        Convolution2D(128, 3, 3, init='glorot_uniform', border_mode='same'))
    coarse_saliency_model.add(LeakyReLU(alpha=.001))
    coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
    coarse_saliency_model.add(BatchNormalization())
    coarse_saliency_model.add(
        Convolution2D(64, 3, 3, init='glorot_uniform', border_mode='same'))
    coarse_saliency_model.add(LeakyReLU(alpha=.001))
    coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
    coarse_saliency_model.add(BatchNormalization())
    coarse_saliency_model.add(
        Convolution2D(32, 3, 3, init='glorot_uniform', border_mode='same'))
    coarse_saliency_model.add(LeakyReLU(alpha=.001))
    coarse_saliency_model.add(UpSampling2D(size=(2, 2)))
    coarse_saliency_model.add(BatchNormalization())
    coarse_saliency_model.add(
        Convolution2D(16, 3, 3, init='glorot_uniform', border_mode='same'))
    coarse_saliency_model.add(LeakyReLU(alpha=.001))
    coarse_saliency_model.add(BatchNormalization())
    coarse_saliency_model.add(
        Convolution2D(1, 3, 3, init='glorot_uniform', border_mode='same'))
    coarse_saliency_model.add(LeakyReLU(alpha=.001))

    # loss on cropped image
    coarse_saliency_cropped = coarse_saliency_model(videoclip_cropped)
    cropped_output = Flatten(name='cropped_output')(coarse_saliency_cropped)

    # coarse-to-fine saliency model and loss
    coarse_saliency_original = coarse_saliency_model(videoclip_original)

    x = UpSampling2D((upsample, upsample), name='coarse_saliency_upsampled')(
        coarse_saliency_original)  # 112 x 4 = 448
    x = merge([x, last_frame_bigger], mode='concat',
              concat_axis=1)  # merge the last RGB frame

    x = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(x)
    x = Convolution2D(64, 3, 3, border_mode='same', init='he_normal')(x)
    x = LeakyReLU(alpha=.001)(x)
    x = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(x)
    x = LeakyReLU(alpha=.001)(x)
    x = Convolution2D(32, 3, 3, border_mode='same', init='he_normal')(x)
    x = LeakyReLU(alpha=.001)(x)
    x = Convolution2D(16, 3, 3, border_mode='same', init='he_normal')(x)
    x = LeakyReLU(alpha=.001)(x)
    x = Convolution2D(4, 3, 3, border_mode='same', init='he_normal')(x)
    x = LeakyReLU(alpha=.001)(x)

    fine_saliency_model = Convolution2D(1,
                                        3,
                                        3,
                                        border_mode='same',
                                        activation='relu')(x)

    # loss on full image
    full_fine_output = Flatten(name='full_fine_output')(fine_saliency_model)

    final_model = Model(
        input=[videoclip_cropped, videoclip_original, last_frame_bigger],
        output=[cropped_output, full_fine_output])

    if summary:
        print final_model.summary()

    return final_model
Exemple #6
0
def alexnet_jn():
    inputs = Input(shape=(121, 145, 121, 1), name='input1')

    # 121x145x121
    conv1 = Conv3D(48,
                   7,
                   strides=1,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal',
                   name='conv1')(inputs)
    pool1 = MaxPooling3D(pool_size=2, padding='same', name='pool1')(conv1)
    bn1 = BatchNormalization(axis=1, name='batch_normalization_1')(pool1)
    print("conv1 shape:", conv1.shape)
    print("pool1 shape:", pool1.shape)
    # conv1 shape: (?, 121, 145, 121, 48)
    # pool1 shape: (?, 61, 73, 61, 48)
    conv2 = Conv3D(128,
                   5,
                   strides=1,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal',
                   name='conv2')(bn1)
    pool2 = MaxPooling3D(pool_size=2, padding='same', name='pool2')(conv2)
    bn2 = BatchNormalization(axis=1, name='batch_normalization_2')(pool2)
    print("conv2 shape:", conv2.shape)
    print("pool2 shape:", pool2.shape)
    # conv2 shape: (?, 61, 73, 61, 128)
    # pool2 shape: (?, 31, 37, 31, 128)
    conv3 = Conv3D(192,
                   3,
                   strides=1,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal',
                   name='conv3')(bn2)
    bn3 = BatchNormalization(axis=1, name='batch_normalization_3')(conv3)
    print("conv3 shape:", conv3.shape)
    # conv3 shape: (?, 31, 37, 31, 192)
    conv4 = Conv3D(192,
                   3,
                   strides=1,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal',
                   name='conv4')(bn3)
    bn4 = BatchNormalization(axis=1, name='batch_normalization_4')(conv4)
    print("conv4 shape:", conv4.shape)
    # conv3 shape: (?, 31, 37, 31, 192)
    conv5 = Conv3D(128,
                   3,
                   strides=1,
                   activation='relu',
                   padding='same',
                   kernel_initializer='he_normal',
                   name='conv5')(bn4)
    pool3 = MaxPooling3D(pool_size=3, padding='same', name='pool3')(conv5)
    bn5 = BatchNormalization(axis=1, name='batch_normalization_5')(pool3)
    print("conv5 shape:", conv5.shape)
    print("pool3 shape:", pool3.shape)
    # conv5 shape: (?, 31, 37, 31, 128)
    # pool3 shape: (?, 11, 13, 11, 128)

    flatten1 = Flatten()(bn5)
    fc1 = Dense(500, activation='relu', name='fc1')(flatten1)
    # fc1_drop = Dropout(rate=0.25)(fc1)
    fc1_drop = Dropout(rate=0.25)(fc1)
    fc2 = Dense(250, activation='relu', name='fc2')(fc1_drop)
    # fc2_drop = Dropout(rate=0.25)(fc2)
    fc2_drop = Dropout(rate=0.25)(fc2)

    fc3 = Dense(2, name='fc3')(fc2_drop)
    output = Activation(activation='softmax')(fc3)

    model = Model(input=inputs, output=output)
    # model.compile(optimizer=SGD(lr=1e-5), loss='categorical_crossentropy', metrics=['acc'])
    model.compile(optimizer=SGD(lr=1e-6),
                  loss='categorical_crossentropy',
                  metrics=['acc'])
    return model
Exemple #7
0
x_test /= 255

# creating the model
model = Sequential()  # model made up of a linear stack of layers

# creating 3D convolution layer, with 32 filters, and a 9 x 11 x 11 filter size (time x height x width)
# filters move across video travelling 1 pixel in each dimension each time they are applied
model.add(
    Convolution3D(32, (9, 11, 11),
                  padding='same',
                  input_shape=(20, 54, 54, 1),
                  strides=(1, 1, 1)))
model.add(Activation('relu'))  # performs rectified linear activation
model.add(Convolution3D(32, (7, 9, 9)))
model.add(Activation('relu'))
model.add(MaxPooling3D(pool_size=(1, 2, 2)))  # halves the height and width
model.add(Convolution3D(32, (6, 9, 9)))
model.add(Activation('relu'))
model.add(Flatten())
model.add(Dense(512))  # fully connected layer with 512 neurons
model.add(Activation('relu'))
model.add(
    Dropout(0.5)
)  # ignores ~ half of the neurons, selected randomly: prevents overfitting
model.add(Dense(
    3))  # fully connected layer with 3 neurons corresponding to each word
model.add(Activation('softmax'))

# creating callbacks
tbCallBack = keras.callbacks.TensorBoard(log_dir='./log',
                                         histogram_freq=1,
Exemple #8
0
def conv_pool(x,kernel_size):
    x = Convolution3D(32, (kernel_size,kernel_size,kernel_size), activation='relu', padding='same', data_format='channels_first')(x)
    x = MaxPooling3D((3,3,3),data_format='channels_first')(x)
    return x
def get_model(summary=False, backend='tf'):
    """ Return the Keras model of the network
    """
    model = Sequential()
    if backend == 'tf':
        input_shape = (16, 112, 112, 3)  # l, h, w, c
    else:
        input_shape = (3, 16, 112, 112)  # c, l, h, w
    model.add(
        Convolution3D(64,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv1',
                      input_shape=input_shape))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     border_mode='valid',
                     name='pool1'))
    # 2nd layer group
    model.add(
        Convolution3D(128,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv2'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool2'))
    # 3rd layer group
    model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3a'))
    model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3b'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool3'))
    # 4th layer group
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4a'))
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4b'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool4'))
    # 5th layer group
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5a'))
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5b'))
    model.add(ZeroPadding3D(padding=((0, 0), (0, 1), (0, 1)), name='zeropad5'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool5'))
    model.add(Flatten())
    # FC layers group
    model.add(Dense(4096, activation='relu', name='fc6'))
    model.add(Dropout(.5))
    model.add(Dense(4096, activation='relu', name='fc7'))
    model.add(Dropout(.5))
    model.add(Dense(487, activation='softmax', name='fc8'))
    # model.add(Dense(101, activation='softmax', name='fc9'))

    if summary:
        print(model.summary())

    return model
Exemple #10
0
def get_model(summary=False):
    #model = Sequential()

    main_input = Input(shape=(48, 48, 48, 1), name='main_input')
    bn0 = BatchNormalization(name='bn0')(main_input)
    conv1_1 = Conv3D(filters=16,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv1_1',
                     kernel_initializer='he_normal')(bn0)
    bn1_1 = BatchNormalization(scale=True, name='bn1_1')(conv1_1)
    relu1_1 = LeakyReLU(alpha=0.01, name='relu1_1')(bn1_1)

    # 1st layer group

    conv1_2 = Conv3D(filters=32,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv1_2',
                     kernel_initializer='he_normal')(relu1_1)
    bn1_2 = BatchNormalization(scale=True, name='bn1_2')(conv1_2)
    relu1_2 = LeakyReLU(alpha=0.01, name='relu1_2')(bn1_2)
    dropout1_1 = Dropout(.5, name='dropout1_1')(relu1_2)
    conv1_3 = Conv3D(filters=32,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv1_3',
                     kernel_initializer='he_normal')(dropout1_1)
    conv1_4 = Conv3D(filters=32,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv1_4',
                     kernel_initializer='he_normal')(relu1_1)
    eltwise1 = add([conv1_3, conv1_4])

    bn1_3 = BatchNormalization(scale=True, name='bn1_3')(eltwise1)
    relu1_3 = LeakyReLU(alpha=0.01, name='relu1_3')(bn1_3)
    conv1_5 = Conv3D(filters=32,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv1_5',
                     kernel_initializer='he_normal')(relu1_3)
    bn1_4 = BatchNormalization(scale=True, name='bn1_4')(conv1_5)
    relu1_4 = LeakyReLU(alpha=0.01, name='relu1_4')(bn1_4)
    conv1_6 = Conv3D(filters=32,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv1_6',
                     kernel_initializer='he_normal')(relu1_4)
    eltwise2 = add([conv1_6, eltwise1])

    #bn3 = BatchNormalization(scale=True,name='bn3')(eltwise1)
    #relu3 = LeakyReLU(alpha=0.01, name='relu3')(bn3)

    # 2nd layer group
    bn2_1 = BatchNormalization(scale=True, name='bn2_1')(eltwise2)
    relu2_1 = LeakyReLU(alpha=0.01, name='relu2_1')(bn2_1)

    conv2_2 = Conv3D(filters=64,
                     kernel_size=(3, 3, 3),
                     strides=(2, 2, 2),
                     padding='same',
                     name='conv2_2',
                     kernel_initializer='he_normal')(relu2_1)
    bn2_2 = BatchNormalization(scale=True, name='bn2_2')(conv2_2)
    relu2_2 = LeakyReLU(alpha=0.01, name='relu2_2')(bn2_2)
    dropout2_1 = Dropout(.2, name='dropout2_1')(relu2_2)
    conv2_3 = Conv3D(filters=64,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv2_3',
                     kernel_initializer='he_normal')(dropout2_1)
    conv2_4 = Conv3D(filters=64,
                     kernel_size=(3, 3, 3),
                     strides=(2, 2, 2),
                     padding='same',
                     name='conv2_4',
                     kernel_initializer='he_normal')(relu2_1)
    eltwise3 = add([conv2_3, conv2_4])

    bn2_3 = BatchNormalization(scale=True, name='bn2_3')(eltwise3)
    relu2_3 = LeakyReLU(alpha=0.01, name='relu2_3')(bn2_3)
    conv2_5 = Conv3D(filters=64,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv2_5',
                     kernel_initializer='he_normal')(relu2_3)
    bn2_4 = BatchNormalization(scale=True, name='bn2_4')(conv2_5)
    relu2_4 = LeakyReLU(alpha=0.01, name='relu2_4')(bn2_4)
    conv2_6 = Conv3D(filters=64,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv2_6',
                     kernel_initializer='he_normal')(relu2_4)
    eltwise4 = add([conv2_6, eltwise3])

    # 3rd layer group
    bn3_1 = BatchNormalization(scale=True, name='bn3_1')(eltwise4)
    relu3_1 = LeakyReLU(alpha=0.01, name='relu3_1')(bn3_1)

    conv3_2 = Conv3D(filters=128,
                     kernel_size=(3, 3, 3),
                     strides=(2, 2, 2),
                     padding='same',
                     name='conv3_2',
                     kernel_initializer='he_normal')(relu3_1)
    bn3_2 = BatchNormalization(scale=True, name='bn3_2')(conv3_2)
    relu3_2 = LeakyReLU(alpha=0.01, name='relu3_2')(bn3_2)
    dropout3_1 = Dropout(.2, name='dropout3_1')(relu3_2)
    conv3_3 = Conv3D(filters=128,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv3_3',
                     kernel_initializer='he_normal')(dropout3_1)
    conv3_4 = Conv3D(filters=128,
                     kernel_size=(3, 3, 3),
                     strides=(2, 2, 2),
                     padding='same',
                     name='conv3_4',
                     kernel_initializer='he_normal')(relu3_1)
    eltwise5 = add([conv3_3, conv3_4])

    bn3_3 = BatchNormalization(scale=True, name='bn3_3')(eltwise5)
    relu3_3 = LeakyReLU(alpha=0.01, name='relu3_3')(bn3_3)
    conv3_5 = Conv3D(filters=128,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv3_5',
                     kernel_initializer='he_normal')(relu3_3)
    bn3_4 = BatchNormalization(scale=True, name='bn3_4')(conv3_5)
    relu3_4 = LeakyReLU(alpha=0.01, name='relu3_4')(bn3_4)
    conv3_6 = Conv3D(filters=128,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv3_6',
                     kernel_initializer='he_normal')(relu3_4)
    eltwise6 = add([conv3_6, eltwise5])

    # 4th layer group
    bn4_1 = BatchNormalization(scale=True, name='bn4_1')(eltwise6)
    relu4_1 = LeakyReLU(alpha=0.01, name='relu4_1')(bn4_1)

    conv4_2 = Conv3D(filters=256,
                     kernel_size=(3, 3, 3),
                     strides=(2, 2, 2),
                     padding='same',
                     name='conv4_2',
                     kernel_initializer='he_normal')(relu4_1)
    bn4_2 = BatchNormalization(scale=True, name='bn4_2')(conv4_2)
    relu4_2 = LeakyReLU(alpha=0.01, name='relu4_2')(bn4_2)
    dropout4_1 = Dropout(.2, name='dropout4_1')(relu4_2)
    conv4_3 = Conv3D(filters=256,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv4_3',
                     kernel_initializer='he_normal')(dropout4_1)
    conv4_4 = Conv3D(filters=256,
                     kernel_size=(3, 3, 3),
                     strides=(2, 2, 2),
                     padding='same',
                     name='conv4_4',
                     kernel_initializer='he_normal')(relu4_1)
    eltwise7 = add([conv4_3, conv4_4])

    bn4_3 = BatchNormalization(scale=True, name='bn4_3')(eltwise7)
    relu4_3 = LeakyReLU(alpha=0.01, name='relu4_3')(bn4_3)
    conv4_5 = Conv3D(filters=256,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv4_5',
                     kernel_initializer='he_normal')(relu4_3)
    bn4_4 = BatchNormalization(scale=True, name='bn4_4')(conv4_5)
    relu4_4 = LeakyReLU(alpha=0.01, name='relu4_4')(bn4_4)
    conv4_6 = Conv3D(filters=256,
                     kernel_size=(3, 3, 3),
                     strides=(1, 1, 1),
                     padding='same',
                     name='conv4_6',
                     kernel_initializer='he_normal')(relu4_4)
    eltwise8 = add([conv4_6, eltwise7])
    # remaining parts
    bn5_1 = BatchNormalization(scale=True, name='bn5_1')(eltwise8)
    relu5_1 = LeakyReLU(alpha=0.01, name='relu5_1')(bn5_1)
    pool1 = MaxPooling3D(pool_size=(3, 5, 5),
                         strides=None,
                         padding='valid',
                         data_format=None)(relu5_1)
    flatten1 = Flatten()(pool1)
    #fc1 = Dense(64,activation='softmax')(flatten1)
    fc2 = Dense(2, activation='softmax')(flatten1)

    model = Model(input=main_input, output=fc2)
    if summary:
        print(model.summary())
    return model
Exemple #11
0
#Model written in sequential manner
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers.convolutional import Convolution3D, MaxPooling3D
from keras.models import Sequential

model = Sequential()
model.add(Convolution3D(32, (3,3,3), input_shape=(1,61,73,61),activation='relu', padding='same', data_format='channels_first'))
model.add(MaxPooling3D((3,3,3),data_format='channels_first'))
model.add(Convolution3D(32, (3,3,3), activation='relu', padding='same', data_format='channels_first'))
model.add(MaxPooling3D((3,3,3),data_format='channels_first'))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(nb_classes, activation='sigmoid'))

# compile
model.compile(loss='binary_crossentropy', optimizer='adadelta', metrics=['accuracy'])
# summarize
print(model.summary())


#Model written with Input and Output
from keras.layers.core import Dense, Dropout, Activation, Flatten
from keras.layers import Input
from keras.layers.convolutional import Convolution3D, MaxPooling3D
from keras.models import Model

def conv_pool(x,kernel_size):
    x = Convolution3D(32, (kernel_size,kernel_size,kernel_size), activation='relu', padding='same', data_format='channels_first')(x)
    x = MaxPooling3D((3,3,3),data_format='channels_first')(x)
    return x
Exemple #12
0
def C3D_model(input_shape, num_classes, weights_path=None, summary=False, trainable=True, num_layers_remove=0):
    """
    Return a Conv3D model with an option to load pretrained weights
    """

    model = Sequential()

    # 1st layer group
    model.add(Conv3D(64, (3, 3, 3), activation="relu", name="conv1", 
                     input_shape=input_shape, strides=(1, 1, 1), padding="same"))  
    model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2), name="pool1", padding="valid"))

    # 2nd layer group  
    model.add(Conv3D(128, (3, 3, 3), activation="relu",name="conv2", 
                     strides=(1, 1, 1), padding="same"))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), name="pool2", padding="valid"))

    # 3rd layer group   
    model.add(Conv3D(256, (3, 3, 3), activation="relu",name="conv3a", 
                     strides=(1, 1, 1), padding="same"))
    model.add(Conv3D(256, (3, 3, 3), activation="relu",name="conv3b", 
                     strides=(1, 1, 1), padding="same"))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), name="pool3", padding="valid"))

    # 4th layer group  
    model.add(Conv3D(512, (3, 3, 3), activation="relu",name="conv4a", 
                     strides=(1, 1, 1), padding="same"))   
    model.add(Conv3D(512, (3, 3, 3), activation="relu",name="conv4b", 
                     strides=(1, 1, 1), padding="same"))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), name="pool4", padding="valid"))

    # 5th layer group  
    model.add(Conv3D(512, (3, 3, 3), activation="relu",name="conv5a", 
                     strides=(1, 1, 1), padding="same"))   
    model.add(Conv3D(512, (3, 3, 3), activation="relu",name="conv5b",
                      strides=(1, 1, 1), padding="same"))
    model.add(ZeroPadding3D(padding=(0, 1, 1)))	
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2), name="pool5", padding="valid"))
    model.add(Flatten())
                     
    # FC layers group
    model.add(Dense(4096, activation='relu', name='fc6'))
    model.add(Dropout(.5))
    model.add(Dense(4096, activation='relu', name='fc7'))
    model.add(Dropout(.5))
    model.add(Dense(num_classes, activation='softmax', name='fc8'))

    # Print model summary, by default False
    if summary:
        print('Original model summary:' )
        print(model.summary())

    # Path to the model file with pretrained weights, by default None
    if weights_path:
        print('Loading Model Weights from %s' % weights_path)
        model.load_weights(weights_path)

    # Pop layers from the top of the network
    if num_layers_remove > 0:
        print('Popping last %s layers' % num_layers_remove)
        for idx in range(num_layers_remove):
            model.pop()

        if summary:
            print('Summary after popping:')
            print(model.summary())
    
    # Set all the layers as trainable, by default True
    for layer in model.layers:
        layer.trainable = trainable

    return model
# Load training images and labels that are stored in numpy array
"""
ntraining_set = numpy.load(
    'numpy_training_datasets/late_microexpfusenetnoseimages.npy')
etraining_set = numpy.load(
    'numpy_training_datasets/late_microexpfuseneteyeimages.npy')
eye_traininglabels = numpy.load(
    'numpy_training_datasets/late_microexpfusenetnoselabels.npy')
nose_traininglabels = numpy.load(
    'numpy_training_datasets/late_microexpfuseneteyelabels.npy')

# Late MicroExpFuseNet Model
eye_input = Input(shape=(1, 32, 32, 18))
eye_conv = Convolution3D(32, (3, 3, 15))(eye_input)
ract_1 = Activation('relu')(eye_conv)
maxpool_1 = MaxPooling3D(pool_size=(3, 3, 3))(ract_1)
ract_2 = Activation('relu')(maxpool_1)
dropout_1 = Dropout(0.5)(ract_2)
flatten_1 = Flatten()(dropout_1)
dense_1 = Dense(1024, )(flatten_1)
dropout_2 = Dropout(0.5)(dense_1)
dense_2 = Dense(128, )(dropout_2)
dropout_3 = Dropout(0.5)(dense_2)

nose_input = Input(shape=(1, 32, 32, 18))
nose_conv = Convolution3D(32, (3, 3, 15))(nose_input)
ract_3 = Activation('relu')(nose_conv)
maxpool_2 = MaxPooling3D(pool_size=(3, 3, 3))(ract_3)
ract_4 = Activation('relu')(maxpool_2)
dropout_6 = Dropout(0.5)(ract_4)
flatten_2 = Flatten()(dropout_6)
Exemple #14
0
def c3d_model():
    """
    Build a 3D convolutional network, aka C3D.
        https://arxiv.org/pdf/1412.0767.pdf
    With thanks:
        https://gist.github.com/albertomontesg/d8b21a179c1e6cca0480ebdf292c34d2
    """
    input_shape = (frame_sequences, frame_height, frame_widht, channel)

    print("Input shape", input_shape)

    model = Sequential()

    # 1st layer group
    model.add(
        Convolution3D(filters=64,
                      kernel_size=(3, 3, 3),
                      input_shape=input_shape,
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(MaxPooling3D(pool_size=(1, 2, 2), strides=(1, 2, 2)))

    # 2nd layer group
    model.add(
        Convolution3D(filters=128,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    # 3rd layer group
    model.add(
        Convolution3D(filters=256,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(
        Convolution3D(filters=256,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    # 4th layer group
    model.add(
        Convolution3D(filters=512,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(
        Convolution3D(filters=512,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    # 5th layer group
    model.add(
        Convolution3D(filters=512,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(
        Convolution3D(filters=512,
                      kernel_size=(3, 3, 3),
                      padding='same',
                      strides=(1, 1, 1),
                      activation='relu'))
    model.add(ZeroPadding3D(padding=(0, 1, 1)))
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
    model.add(Flatten())

    # FC layers group
    model.add(Dense(units=4096, activation='relu'))
    model.add(Dropout(rate=0.5))
    model.add(Dense(units=4096, activation='relu'))
    model.add(Dropout(rate=0.5))
    model.add(Dense(units=len(actions), activation='softmax'))

    return model
Exemple #15
0
def C3D_conv_features(summary=False):
    """ Return the Keras model of the network until the fc6 layer where the
    convolutional features can be extracted.
    """
    from keras.layers.convolutional import Convolution3D, MaxPooling3D, ZeroPadding3D
    from keras.layers.core import Dense, Dropout, Flatten
    from keras.models import Sequential

    model = Sequential()
    # 1st layer group
    model.add(
        Convolution3D(64,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv1',
                      subsample=(1, 1, 1),
                      input_shape=(3, 16, 112, 112),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     border_mode='valid',
                     name='pool1'))
    # 2nd layer group
    model.add(
        Convolution3D(128,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv2',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool2'))
    # 3rd layer group
    model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3a',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3b',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool3'))
    # 4th layer group
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4a',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4b',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool4'))
    # 5th layer group
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5a',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5b',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(ZeroPadding3D(padding=(0, 1, 1), name='zeropadding'))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool5'))
    model.add(Flatten(name='flatten'))
    # FC layers group
    model.add(Dense(4096, activation='relu', name='fc6', trainable=False))
    model.add(Dropout(.5, name='do1'))
    model.add(Dense(4096, activation='relu', name='fc7'))
    model.add(Dropout(.5, name='do2'))
    model.add(Dense(487, activation='softmax', name='fc8'))

    # Load weights
    model.load_weights('data/models/c3d-sports1M_weights.h5')

    for _ in range(4):
        model.pop_layer()

    if summary:
        print(model.summary())
    return model
Exemple #16
0
def get_int_model(model, layer, backend='tf'):

    if backend == 'tf':
        input_shape = (16, 112, 112, 3)  # l, h, w, c
    else:
        input_shape = (3, 16, 112, 112)  # c, l, h, w

    int_model = Sequential()

    int_model.add(
        Convolution3D(64,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv1',
                      input_shape=input_shape,
                      weights=model.layers[0].get_weights()))
    if layer == 'conv1':
        return int_model
    int_model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     border_mode='valid',
                     name='pool1'))
    if layer == 'pool1':
        return int_model

    # 2nd layer group
    int_model.add(
        Convolution3D(128,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv2',
                      weights=model.layers[2].get_weights()))
    if layer == 'conv2':
        return int_model
    int_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool2'))
    if layer == 'pool2':
        return int_model

    # 3rd layer group
    int_model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3a',
                      weights=model.layers[4].get_weights()))
    if layer == 'conv3a':
        return int_model
    int_model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3b',
                      weights=model.layers[5].get_weights()))
    if layer == 'conv3b':
        return int_model
    int_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool3'))
    if layer == 'pool3':
        return int_model

    # 4th layer group
    int_model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4a',
                      weights=model.layers[7].get_weights()))
    if layer == 'conv4a':
        return int_model
    int_model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4b',
                      weights=model.layers[8].get_weights()))
    if layer == 'conv4b':
        return int_model
    int_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool4'))
    if layer == 'pool4':
        return int_model

    # 5th layer group
    int_model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5a',
                      weights=model.layers[10].get_weights()))
    if layer == 'conv5a':
        return int_model
    int_model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5b',
                      weights=model.layers[11].get_weights()))
    if layer == 'conv5b':
        return int_model
    int_model.add(ZeroPadding3D(padding=(0, 1, 1), name='zeropad'))
    int_model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool5'))
    if layer == 'pool5':
        return int_model

    int_model.add(Flatten())
    # FC layers group
    int_model.add(
        Dense(4096,
              activation='relu',
              name='fc6',
              weights=model.layers[15].get_weights()))
    if layer == 'fc6':
        return int_model
    int_model.add(Dropout(.5))
    int_model.add(
        Dense(4096,
              activation='relu',
              name='fc7',
              weights=model.layers[17].get_weights()))
    if layer == 'fc7':
        return int_model
    int_model.add(Dropout(.5))
    int_model.add(
        Dense(487,
              activation='softmax',
              name='fc8',
              weights=model.layers[19].get_weights()))
    if layer == 'fc8':
        return int_model

    return None
Exemple #17
0
    def get_model(self):
        model = Sequential()
        # 1st layer group
        model.add(
            Conv3D(64,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv1',
                   subsample=(1, 1, 1),
                   input_shape=self.input_shape))
        model.add(
            MaxPooling3D(pool_size=(1, 2, 2),
                         strides=(1, 2, 2),
                         border_mode='valid',
                         name='pool1'))
        # 2nd layer group
        model.add(
            Conv3D(128,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv2',
                   subsample=(1, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool2'))
        # 3rd layer group
        model.add(
            Conv3D(256,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv3a',
                   subsample=(1, 1, 1)))
        model.add(
            Conv3D(256,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv3b',
                   subsample=(1, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool3'))
        # 4th layer group
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv4a',
                   subsample=(1, 1, 1)))
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv4b',
                   subsample=(1, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool4'))

        # 5th layer group
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv5a',
                   subsample=(1, 1, 1)))
        model.add(
            Conv3D(512,
                   3,
                   3,
                   3,
                   activation='relu',
                   border_mode='same',
                   name='conv5b',
                   subsample=(1, 1, 1)))
        model.add(ZeroPadding3D(padding=(0, 1, 1)))
        model.add(
            MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='valid',
                         name='pool5'))
        model.add(Flatten())

        # FC layers group
        model.add(Dense(4096, activation='relu', name='fc6'))
        model.add(Dropout(0.5))
        model.add(Dense(4096, activation='relu', name='fc7'))
        model.add(Dropout(0.5))
        model.add(Dense(self.output_shape, activation='linear'))

        return model
Exemple #18
0
# level of convolution to perform at each layer (CONV x CONV)
#nb_conv = [5,5]

# Pre-processing
train_set = train_set.astype('float64')
train_set -= np.mean(train_set)
train_set /=np.max(train_set)


# In[15]:

# Define model
model = Sequential()
model.add(Convolution3D(8,nb_depth=2, nb_row=2, nb_col=2,border_mode='full',input_shape=(1,16,16,16),activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Dropout(0.25))

model.add(Convolution3D(16,nb_depth=2, nb_row=2, nb_col=2,border_mode='full',activation='relu'))
model.add(MaxPooling3D(pool_size=(2, 2, 2)))
model.add(Dropout(0.25))

#model.add(Convolution3D(64,nb_depth=3, nb_row=3, nb_col=3,border_mode='full', activation='relu'))
#model.add(MaxPooling3D(pool_size=(2,2,2)))
#model.add(Dropout(0.5))

#model.add(Convolution3D(128,nb_depth=3, nb_row=3, nb_col=3,border_mode='full', activation='relu'))
#model.add(MaxPooling3D(pool_size=(2, 2, 2)))
#model.add(Dropout(0.5))
model.add(Flatten())
train_set /= np.max(train_set)

#train_set /= 255
#Model

model = Sequential()
model.add(
    Convolution3D(filters[0],
                  kernel_dim1=conv[0],
                  kernel_dim2=conv[0],
                  kernel_dim3=conv[0],
                  input_shape=(1, img_rows, img_cols, patch_size),
                  activation='relu'))

model.add(MaxPooling3D(pool_size=(pool[0], pool[0], pool[0])))

model.add(Dropout(0.5))

model.add(Flatten())

model.add(Dense(128, init='normal', activation='relu'))

model.add(Dropout(0.5))

model.add(Dense(classes, init='normal'))

model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',
              optimizer='RMSprop',
Exemple #20
0
def c3d_model(summary=False):
    """ Return the Keras model of the network
    """
    main_input = Input(shape=(32, 112, 112, 3), name="main_input")
    # 1st layer group
    x = Conv3D(
        64,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv1",
        strides=(1, 1, 1),
    )(main_input)
    x = MaxPooling3D(
        pool_size=(1, 2, 2), strides=(1, 2, 2), padding="valid", name="pool1"
    )(x)
    # 2nd layer group
    x = Conv3D(
        128,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv2",
        strides=(1, 1, 1),
    )(x)
    x = MaxPooling3D(
        pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool2"
    )(x)
    # 3rd layer group
    x = Conv3D(
        256,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv3a",
        strides=(1, 1, 1),
    )(x)
    x = Conv3D(
        256,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv3b",
        strides=(1, 1, 1),
    )(x)
    x = MaxPooling3D(
        pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool3"
    )(x)
    # 4th layer group
    x = Conv3D(
        512,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv4a",
        strides=(1, 1, 1),
    )(x)
    x = Conv3D(
        512,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv4b",
        strides=(1, 1, 1),
    )(x)
    x = MaxPooling3D(
        pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool4"
    )(x)
    # 5th layer group
    x = Conv3D(
        512,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv5a",
        strides=(1, 1, 1),
    )(x)
    x = Conv3D(
        512,
        kernel_size=(3, 3, 3),
        activation="relu",
        padding="same",
        name="conv5b",
        strides=(1, 1, 1),
    )(x)
    x = ZeroPadding3D(padding=(0, 1, 1))(x)
    x = MaxPooling3D(
        pool_size=(2, 2, 2), strides=(2, 2, 2), padding="valid", name="pool5"
    )(x)
    x = Flatten()(x)
    # FC layers group
    x = Dense(2048, activation="relu", name="fc6")(x)
    x = Dropout(0.5)(x)
    x = Dense(2048, activation="relu", name="fc7")(x)
    x = Dropout(0.5)(x)
    predictions = Dense(2, activation="softmax", name="fc8")(x)

    model = Model(inputs=main_input, outputs=predictions)
    if summary:
        print(model.summary())
    return model
Exemple #21
0
Y_train = np_utils.to_categorical(Y_train, nb_classes)
Y_test = np_utils.to_categorical(Y_test, nb_classes)

# number of convolutional filters to use at each layer
nb_filters = [16, 32]

# level of pooling to perform at each layer (POOL x POOL)
nb_pool = [3, 3]

# level of convolution to perform at each layer (CONV x CONV)
nb_conv = [7, 3]

model = Sequential()
model.add(Convolution3D(nb_filters[0],nb_depth=nb_conv[0], nb_row=nb_conv[0], nb_col=nb_conv[0], border_mode='full',
                        input_shape=(1, patch_size, patch_size, patch_size), activation='relu'))
model.add(MaxPooling3D(pool_size=(nb_pool[0], nb_pool[0], nb_pool[0])))
model.add(Dropout(0.5))
model.add(Convolution3D(nb_filters[1],nb_depth=nb_conv[1], nb_row=nb_conv[1], nb_col=nb_conv[1], border_mode='full',
                        activation='relu'))
model.add(MaxPooling3D(pool_size=(nb_pool[1], nb_pool[1], nb_pool[1])))
model.add(Flatten())
model.add(Dropout(0.5))
model.add(Dense(16, init='normal', activation='relu'))
model.add(Dense(nb_classes, init='normal'))
model.add(Activation('softmax'))

sgd = RMSprop(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd)

model.fit(X_train, Y_train, batch_size=batch_size, nb_epoch=nb_epoch, show_accuracy=True, verbose=2,
          validation_data=(X_test, Y_test))
Exemple #22
0
def conv3d_model():
    input_shape = (32, 112, 112, 3)
    weight_decay = 0.005
    nb_classes = 2

    inputs = Input(input_shape)
    x = Conv3D(
        64,
        (3, 3, 3),
        strides=(1, 1, 1),
        padding="same",
        activation="relu",
        kernel_regularizer=l2(weight_decay),
    )(inputs)
    x = MaxPooling3D((2, 2, 1), strides=(2, 2, 1), padding="same")(x)

    x = Conv3D(
        128,
        (3, 3, 3),
        strides=(1, 1, 1),
        padding="same",
        activation="relu",
        kernel_regularizer=l2(weight_decay),
    )(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)

    x = Conv3D(
        128,
        (3, 3, 3),
        strides=(1, 1, 1),
        padding="same",
        activation="relu",
        kernel_regularizer=l2(weight_decay),
    )(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)

    x = Conv3D(
        256,
        (3, 3, 3),
        strides=(1, 1, 1),
        padding="same",
        activation="relu",
        kernel_regularizer=l2(weight_decay),
    )(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)

    x = Conv3D(
        256,
        (3, 3, 3),
        strides=(1, 1, 1),
        padding="same",
        activation="relu",
        kernel_regularizer=l2(weight_decay),
    )(x)
    x = MaxPooling3D((2, 2, 2), strides=(2, 2, 2), padding="same")(x)

    x = Flatten()(x)
    x = Dense(2048, activation="relu", kernel_regularizer=l2(weight_decay))(x)
    x = Dropout(0.5)(x)
    x = Dense(2048, activation="relu", kernel_regularizer=l2(weight_decay))(x)
    x = Dropout(0.5)(x)
    x = Dense(nb_classes, kernel_regularizer=l2(weight_decay))(x)
    x = Activation("softmax")(x)

    model = Model(inputs, x)
    return model
Exemple #23
0
filters_3D_1 = 0
kernel_1 = 0
kernel_2 = 0

#如果模型存在,加载已有模型
if (os.path.exists('model.h5')):
    print('model is loading')
    model = load_model('model.h5')

#否则使用代码里写的模型
else:
    model = Sequential()
    #第一层卷积层
    model.add(Conv3D(filters_3D_1, (kernel_1, kernel_2)))
    #第一层最大池化层
    model.add(MaxPooling3D())
    #第二层卷积层
    model.add(Conv3D())
    #第二层最大池化层
    model.add(MaxPooling3D())
    #第三层卷积层
    model.add(Conv3D())
    #第三层最大池化层
    model.add(MaxPooling3D())
    #第一层dropout层防止过拟合
    model.add(Dropout())
    #第一层LSTM层
    model.add(LSTM())
    #第二层LSTM层
    model.add(LSTM())
    #第二层dropout层防止过拟合
Exemple #24
0
                                 encoding='utf-8')

model = Sequential()

model.add(
    Conv3D(filters=64,
           kernel_size=(3, 3, 3),
           strides=(1, 1, 1),
           padding="same",
           activation="relu",
           data_format="channels_last",
           input_shape=(16, 128, 128, 3)))
#model.add(BatchNormalization())
model.add(
    MaxPooling3D(pool_size=(1, 2, 2),
                 strides=(1, 2, 2),
                 padding="valid",
                 data_format=None))

model.add(
    Conv3D(filters=128,
           kernel_size=(3, 3, 3),
           strides=(1, 1, 1),
           padding="same",
           activation="relu"))
model.add(
    MaxPooling3D(pool_size=(2, 2, 2),
                 strides=(2, 2, 2),
                 padding="valid",
                 data_format=None))

model.add(
from sklearn import preprocessing

model = Sequential()
#`channels_last` corresponds to inputs with shape `(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)`
strides = (1, 1, 1)
kernel_size = (3, 3, 3)
model.add(
    Conv3D(32,
           kernel_size,
           strides=strides,
           activation='relu',
           padding='same',
           input_shape=(32, 64, 96, 3)))
print(model.output_shape)
model.add(BatchNormalization())
model.add(MaxPooling3D(pool_size=(1, 2, 2)))
print(model.output_shape)

model.add(
    Conv3D(64, kernel_size, strides=strides, activation='relu',
           padding='same'))
print(model.output_shape)
model.add(BatchNormalization())
model.add(MaxPooling3D(pool_size=(1, 2, 2)))
print(model.output_shape)

model.add(
    Conv3D(128,
           kernel_size,
           strides=strides,
           activation='relu',
Exemple #26
0
def unet_model():

    inputs = Input(shape=(1, max_slices, img_size, img_size))
    conv1 = Convolution3D(width,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(inputs)
    conv1 = BatchNormalization(axis=1)(conv1)
    conv1 = Convolution3D(width * 2,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv1)
    conv1 = BatchNormalization(axis=1)(conv1)
    pool1 = MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='same')(conv1)

    conv2 = Convolution3D(width * 2,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(pool1)
    conv2 = BatchNormalization(axis=1)(conv2)
    conv2 = Convolution3D(width * 4,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv2)
    conv2 = BatchNormalization(axis=1)(conv2)
    pool2 = MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='same')(conv2)

    conv3 = Convolution3D(width * 4,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(pool2)
    conv3 = BatchNormalization(axis=1)(conv3)
    conv3 = Convolution3D(width * 8,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv3)
    conv3 = BatchNormalization(axis=1)(conv3)
    pool3 = MaxPooling3D(pool_size=(2, 2, 2),
                         strides=(2, 2, 2),
                         border_mode='same')(conv3)

    conv4 = Convolution3D(width * 8,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(pool3)
    conv4 = BatchNormalization(axis=1)(conv4)
    conv4 = Convolution3D(width * 8,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv4)
    conv4 = BatchNormalization(axis=1)(conv4)
    conv4 = Convolution3D(width * 16,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv4)
    conv4 = BatchNormalization(axis=1)(conv4)

    up5 = merge([UpSampling3D(size=(2, 2, 2))(conv4), conv3],
                mode='concat',
                concat_axis=1)
    conv5 = SpatialDropout3D(0.2)(up5)
    conv5 = Convolution3D(width * 8,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv5)
    #conv5 = BatchNormalization()(conv5)
    conv5 = Convolution3D(width * 8,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv5)
    #conv5 = BatchNormalization()(conv5)

    up6 = merge([UpSampling3D(size=(2, 2, 2))(conv5), conv2],
                mode='concat',
                concat_axis=1)
    conv6 = SpatialDropout3D(0.2)(up6)
    conv6 = Convolution3D(width * 4,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv6)
    #conv6 = BatchNormalization()(conv6)
    conv6 = Convolution3D(width * 4,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv6)
    #conv6 = BatchNormalization()(conv6)

    up7 = merge([UpSampling3D(size=(2, 2, 2))(conv6), conv1],
                mode='concat',
                concat_axis=1)
    conv7 = SpatialDropout3D(0.2)(up7)
    conv7 = Convolution3D(width * 2,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv7)
    #conv7 = BatchNormalization()(conv7)
    conv7 = Convolution3D(width * 2,
                          3,
                          3,
                          3,
                          activation='relu',
                          border_mode='same')(conv7)
    #conv7 = BatchNormalization()(conv7)

    conv8 = Convolution3D(1, 1, 1, 1, activation='sigmoid')(conv7)

    model = Model(input=inputs, output=conv8)
    model.compile(optimizer=Adam(lr=1e-5),
                  loss=dice_coef_loss,
                  metrics=[dice_coef])

    return model
Exemple #27
0
def get_model(summary=False):
    model = Sequential()
    # Layer 01
    model.add(
        Convolution3D(64,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv1',
                      subsample=(1, 1, 1),
                      input_shape=(3, 16, 112, 112),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(1, 2, 2),
                     strides=(1, 2, 2),
                     border_mode='valid',
                     name='pool1'))
    # layer02
    model.add(
        Convolution3D(128,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv2',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool2'))
    # 3rd layer group
    model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3a',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        Convolution3D(256,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv3b',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool3'))
    # 4th layer group
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4a',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv4b',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool4'))
    # 5th layer group
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5a',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(
        Convolution3D(512,
                      3,
                      3,
                      3,
                      activation='relu',
                      border_mode='same',
                      name='conv5b',
                      subsample=(1, 1, 1),
                      trainable=False))
    model.add(ZeroPadding3D(padding=(0, 1, 1)))
    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     border_mode='valid',
                     name='pool5'))
    model.add(Flatten())

    model.add(Dense(4096, activation='relu', name='fc6'))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation='relu', name='fc7'))
    model.add(Dropout(0.5))
    model.add(Dense(487, activation='softmax', name='fc8'))
    if summary:
        print(model.summary())
    return model
    def build(input_shape, num_outputs, block_fn, repetitions, reg_factor):
        """Instantiate a vanilla ResNet3D keras model.
        # Arguments
            input_shape: Tuple of input shape in the format
            (conv_dim1, conv_dim2, conv_dim3, channels) if dim_ordering='tf'
            (filter, conv_dim1, conv_dim2, conv_dim3) if dim_ordering='th'
            num_outputs: The number of outputs at the final softmax layer
            block_fn: Unit block to use {'basic_block', 'bottlenack_block'}
            repetitions: Repetitions of unit blocks
        # Returns
            model: a 3D ResNet model that takes a 5D tensor (volumetric images
            in batch) as input and returns a 1D vector (prediction) as output.
        """
        _handle_data_format()
        if len(input_shape) != 4:
            raise ValueError("Input shape should be a tuple "
                             "(conv_dim1, conv_dim2, conv_dim3, channels) "
                             "for tensorflow as backend or "
                             "(channels, conv_dim1, conv_dim2, conv_dim3) "
                             "for theano as backend")

        block_fn = _get_block(block_fn)
        input = Input(shape=input_shape)
        # first conv
        conv1 = _conv_bn_relu3D(filters=64,
                                kernel_size=(7, 7, 7),
                                strides=(2, 2, 2),
                                kernel_regularizer=l2(reg_factor))(input)
        pool1 = MaxPooling3D(pool_size=(3, 3, 3),
                             strides=(2, 2, 2),
                             padding="same")(conv1)

        # repeat blocks
        block = pool1
        filters = 64
        for i, r in enumerate(repetitions):
            block = _residual_block3d(block_fn,
                                      filters=filters,
                                      kernel_regularizer=l2(reg_factor),
                                      repetitions=r,
                                      is_first_layer=(i == 0))(block)
            filters *= 2

        # last activation
        block_output = _bn_relu(block)

        # average poll and classification
        pool2 = AveragePooling3D(pool_size=(block._keras_shape[DEPTH_AXIS],
                                            block._keras_shape[HEIGHT_AXIS],
                                            block._keras_shape[WIDTH_AXIS]),
                                 strides=(1, 1, 1))(block_output)
        flatten1 = Flatten()(pool2)
        if num_outputs > 1:
            dense = Dense(units=num_outputs,
                          kernel_initializer="he_normal",
                          activation="softmax",
                          kernel_regularizer=l2(reg_factor))(flatten1)
        else:
            dense = Dense(units=num_outputs,
                          kernel_initializer="he_normal",
                          activation="sigmoid",
                          kernel_regularizer=l2(reg_factor))(flatten1)

        model = Model(inputs=input, outputs=dense)
        return model
Exemple #29
0
def __create_dense_net(nb_layers,
                       growth_rate=12,
                       nb_filter=64,
                       bottleneck=True,
                       reduction=0.1,
                       dropout_rate=None,
                       subsample_initial_block=True):

    inputs = Input(shape=(280, 280, 16, 1))
    print("0 :inputs shape:", inputs.shape)

    # 设定每个denseblock中convblock的数量:nb_layers = [3,3,3]

    concat_axis = -1  # 设定concat的轴(即叠加的轴)
    bn_axis = -1  # 设定BN的轴(即叠加的轴)
    nb_dense_block = nb_layers.__len__(
    )  # nb_dense_block :denseblock的数量,需要和nb_layers对应
    final_nb_layer = nb_layers[-1]
    compression = 1.0 - reduction  # denseblock的通道衰减率,即实际输出通道数=原输出通道数x通道衰减率

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

    x = Conv3D(nb_filter,
               initial_kernel,
               kernel_initializer='he_normal',
               padding='same',
               strides=initial_strides,
               use_bias=False)(inputs)

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

    print("0 :Initial conv shape:", x.shape)
    # Initial convolution finished ================================================================================

    # Add dense blocks start  ==================================================================================
    for block_idx in range(nb_dense_block - 1):
        x, nb_filter = __dense_block(x,
                                     nb_layers[block_idx],
                                     nb_filter,
                                     growth_rate,
                                     concat_axis=concat_axis,
                                     bn_axis=bn_axis,
                                     bottleneck=bottleneck,
                                     dropout_rate=dropout_rate,
                                     grow_nb_filters=True)
        print(block_idx + 1, ":dense_block shape:", x.shape)

        x = __transition_block(x,
                               nb_filter,
                               compression=compression,
                               concat_axis=concat_axis,
                               bias_allow=False)
        print(block_idx + 1, ":transition_block shape:", x.shape)

        nb_filter = int(nb_filter * compression)
    # Add dense blocks finish ==================================================================================

    # The last dense_block does not have a transition_block
    x, nb_filter = __dense_block(x,
                                 final_nb_layer,
                                 nb_filter,
                                 growth_rate,
                                 concat_axis=concat_axis,
                                 bn_axis=bn_axis,
                                 bottleneck=bottleneck,
                                 dropout_rate=dropout_rate,
                                 grow_nb_filters=True)
    print(nb_dense_block, ":dense_block shape:", x.shape)

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

    out = GlobalAveragePooling3D(data_format='channels_last')(x)
    print("GApooling shape:", out.shape)
    out_drop = Dropout(rate=0.3)(out)
    out = Dense(1, name='fc1')(out_drop)
    print("out shape:", out.shape)
    output = Activation(activation='sigmoid')(out)

    model = Model(input=inputs, output=output)
    #mean_squared_logarithmic_error or binary_crossentropy
    model.compile(optimizer=SGD(lr=1e-6, momentum=0.9),
                  loss=EuiLoss,
                  metrics=[y_t, y_pre, Acc])

    return model
Exemple #30
0
def c3d(input_shape, nb_classes):
    """
    Build a 3D convolutional network, based loosely on C3D.
        https://arxiv.org/pdf/1412.0767.pdf
    """
    # 8.27加入一些正则项
    # Model.
    weight_decay = 0.0001  # 0.0005 # 0.0001 #0.001
    model = Sequential()
    # 10.2 divided fliters with two
    model.add(
        Conv3D(8, (3, 3, 3),
               padding='same',
               input_shape=input_shape,
               activation='relu')
    )  # 9.20 change kernel number from 32 to 8 # 10.2 change same to valid; fliter size 8 to 4
    #model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))
    model.add(Conv3D(
        8, (3, 3, 3), padding='same',
        activation='relu'))  # 8.31加深层数 #9.20 change kernel number from 32 to 8
    model.add(BatchNormalization())  # 10.2 remove
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    model.add(Conv3D(16, (3, 3, 3), padding='same', activation='relu'))
    model.add(Conv3D(16, (3, 3, 3), padding='same', activation='relu'))
    model.add(BatchNormalization())  # 10.2 remove
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    # 9.17 去掉卷积层的正则约束
    model.add(Conv3D(32, (3, 3, 3), padding='same', activation='relu')
              )  # 8.31加深层数 #9.20 change kernel number from 64 to 16
    model.add(Conv3D(32, (3, 3, 3), padding='same',
                     activation='relu'))  # 8.29加入
    model.add(BatchNormalization())  # 9.20加入,试图加快收敛
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    model.add(Conv3D(64, (3, 3, 3), padding='same', activation='relu'))
    model.add(Conv3D(
        64,
        (3, 3, 3),
        padding='same',
        activation='relu',
    ))  # 128 to 32
    model.add(BatchNormalization())  # 9.20加入,试图加快收敛  # 10.2 remove
    model.add(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2)))

    #model.add(Conv3D(256, (2, 2, 2), activation='relu',kernel_regularizer=regularizers.l2(weight_decay)))
    # 9.18为以下四个卷积层加入正则化方法
    model.add(
        Conv3D(128, (3, 3, 3),
               padding='same',
               activation='relu',
               kernel_regularizer=regularizers.l2(
                   weight_decay)))  # 9.10 8.31修改为3,3,3卷积核
    #model.add(Conv3D(128, (3, 3, 3), padding='same', activation='relu',kernel_regularizer=regularizers.l2(weight_decay))) # 256 to 64\

    model.add(MaxPooling3D(pool_size=(2, 2, 2),
                           strides=(2, 2, 2)))  # 9.10 加入最大池化和512个卷积核
    #model.add(Conv3D(256, (3, 3, 3), padding='same', activation='relu',kernel_regularizer=regularizers.l2(weight_decay)))  # 512 to 128
    #model.add(Conv3D(256, (3, 3, 3), padding='same', activation='relu',kernel_regularizer=regularizers.l2(weight_decay)))  #  512 to 128
    model.add(BatchNormalization())  # 9.20加入,试图加快收敛

    model.add(Flatten())
    #model.add(GlobalAveragePooling3D())
    #model.add(Dense(1024)) # add 10.17
    model.add(Dropout(0.5))
    #model.add(Dense(512, kernel_regularizer=regularizers.l2(weight_decay))) #1024
    model.add(Dense(512))
    #model.add(Dropout(0.5))
    #model.add(Dense(nb_classes, activation='softmax',kernel_regularizer=regularizers.l2(0.0001)))
    model.add(Dense(nb_classes, activation='softmax'))

    return model