Beispiel #1
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
Beispiel #2
0
def make_model_2(img_channels, img_rows, img_cols,d3, neurons_full_layer):
    model = Sequential()

    print img_channels, img_rows, img_cols,d3

    model.add(Convolution3D(8, 4,4,4, border_mode='same', input_shape=(img_channels, img_rows, img_cols,d3)))
    model.add(MaxPooling3D(pool_size=(2, 2,2)))
    model.add(Activation('sigmoid'))
    
    #model.add(Convolution2D(64, 3, 3, border_mode='same'))
    #model.add(Activation('relu'))
    #model.add(Convolution3D(16, 4, 4,4))
    #model.add(Activation('sigmoid'))
    #model.add(MaxPooling3D(pool_size=(2, 2,2)))

    #model.add(Convolution3D(32, 4, 4,4))
    #model.add(Activation('relu'))
    #model.add(MaxPooling3D(pool_size=(2, 2,2)))

    #model.add(Dropout(0.25))

    model.add(Flatten())
    model.add(Dense(neurons_full_layer,init='lecun_uniform'))
    model.add(Activation('sigmoid'))
    #model.add(Dropout(0.20))

    model.add(Dense(img_channels,init='lecun_uniform'))
    model.add(Activation('linear'))

    model.summary()    
    # let's train the model using SGD + momentum (how original).
    opt = Adadelta()
    model.compile(loss='mse',optimizer=opt) #,              metrics=['accuracy'])
              
    return model
def conv_block(x_input,
               num_filters,
               pool=True,
               activation='relu',
               init='orthogonal'):

    x1 = Convolution3D(num_filters,
                       3,
                       3,
                       3,
                       border_mode='same',
                       W_regularizer=l2(1e-5),
                       init=init)(x_input)
    x1 = BatchNormalization(axis=1, momentum=0.99)(x1)
    # x1 = Activation('relu')(x1)
    x1 = PReLU(shared_axes=[2, 3, 4], trainable=True)(x1)
    #x1 = LeakyReLU(.01)(x1)
    #x1 = Convolution3D(num_filters / 2,3,3,3, border_mode='same',W_regularizer=l2(1e-4))(x1)
    #x1 = BatchNormalization(axis=1)(x1)
    #x1 = LeakyReLU(.1)(x1)

    if pool:
        x1 = MaxPooling3D()(x1)
    x_out = x1
    return x_out
Beispiel #4
0
    def get_model1(self):
        nb_filters = [64]
        nb_pool = [3]
        nb_conv = [7]

        # Define model
        model = Sequential()
        model.add(Convolution3D(
            nb_filters[0],
            nb_depth=nb_conv[0],
            nb_row=nb_conv[0],
            nb_col=nb_conv[0],
            input_shape=tuple([1] + self.resize),
            activation='relu'))
        model.add(MaxPooling3D(pool_size=tuple([nb_pool[0]]*3)))
        model.add(Flatten())
        model.add(Dropout(0.5))

        model.add(Dense(128, init='normal', activation='relu'))
        model.add(Dense(32, init='normal', activation='relu'))
        model.add(Dropout(0.5))
        model.add(Dense(len(self.labels), init='normal'))
        model.add(Activation('softmax'))
        model.compile(loss='categorical_crossentropy', optimizer='RMSprop')
        return model
def transition_block(input, nb_filter, dropout_rate=None, weight_decay=1E-4):
    ''' Apply BatchNorm, Relu 1x1, Conv2D, optional dropout and Maxpooling2D
    Args:
        input: keras tensor
        nb_filter: number of filters
        dropout_rate: dropout rate
        weight_decay: weight decay factor
    Returns: keras tensor, after applying batch_norm, relu-conv, dropout, maxpool
    '''

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

    x = Convolution3D(nb_filter, (1, 1, 1),
                      kernel_initializer="he_uniform",
                      padding="same",
                      use_bias=False,
                      kernel_regularizer=l2(weight_decay))(input)
    if dropout_rate is not None:
        x = Dropout(dropout_rate)(x)
    x = AveragePooling3D((2, 2, 2), strides=(2, 2, 2))(x)

    x = BatchNormalization(axis=concat_axis,
                           gamma_regularizer=l2(weight_decay),
                           beta_regularizer=l2(weight_decay))(x)

    return x
def conv_block(x_input,
               num_filters,
               pool=True,
               activation='relu',
               init='orthogonal'):

    x1 = Convolution3D(num_filters,
                       3,
                       3,
                       3,
                       border_mode='same',
                       W_regularizer=l2(1e-5),
                       init=init)(x_input)
    x1 = BatchNormalization(axis=1, momentum=0.99)(x1)

    x1 = LeakyReLU(.1)(x1)

    #experimental activation kinda like a double sided leaky relu
    # x1 = Activation('srelu_fixed')(x1)

    # x1 = Convolution3D(num_filters,3,3,3, border_mode='same',W_regularizer=l2(1e-4))(x1)
    # x1 = BatchNormalization(axis=1)(x1)
    # x1 = LeakyReLU(.1)(x1)

    if pool:
        x1 = MaxPooling3D()(x1)
    x_out = x1
    return x_out
Beispiel #7
0
def C3D_ucf101_training_model_tf(summary=True, backend='tf'):
    """ Return the Keras model of the network
    """
    from keras.models import Sequential
    from keras.layers.core import Dense, Dropout, Flatten
    from keras.layers.convolutional import Convolution3D, MaxPooling3D, ZeroPadding3D
    from keras.optimizers import SGD

    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(101, activation='linear', name='fc8'))

    if summary:
        print(model.summary())

    return model
Beispiel #8
0
 def _feed_d_map(self, x, layer_idx):
     # shifting down feature maps
     x = Lambda(self._shift_depth, name='d_shift_right' + str(layer_idx))(x)
     x = Convolution3D(filters=2 * self.nb_filters,
                       kernel_size=(1, 1, 1),
                       padding='valid',
                       name='d_1x1_conv_' + str(layer_idx))(x)
     return x
def c3d_model():
    classes = 2
    
    """ Return the Keras model of the network
    """
    model = Sequential()
    input_shape=(16, 100, 100, 3) # l, h, w, c
   
    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(256, 3, 3, 3, activation='relu',
                            border_mode='same', name='conv4a'))
    model.add(Convolution3D(256, 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(MaxPooling3D(pool_size=(2, 2, 2), strides=(2, 2, 2),
                           border_mode='valid', name='pool5'))
    model.add(Flatten())
    # FC layers group
    #model.add(Dense(2048, activation='relu', name='fc6'))
    #model.add(Dropout(.5))
    #model.add(Dense(2048, activation='relu', name='fc7'))
    #model.add(Dropout(.5))
    #model.add(Dense(2, activation='sigmoid', name='fc8'))

    
    #WS FC Layer with custom parameters
    model.add(Dense(512,input_dim=4608,W_regularizer=l2(0.001),activation='relu', name='fc6'))
    model.add(Dropout(.6))
    model.add(Dense(32,activation='relu',W_regularizer=l2(0.001), name='fc7'))
    model.add(Dropout(.6))
    model.add(Dense(2, activation='sigmoid',W_regularizer=l2(0.001), name='fc8'))
    return model
Beispiel #10
0
def c3d_channels(spatial_size,
                 temporal_size,
                 classes,
                 channels,
                 weights_path=None):
    model = Sequential()
    model.add(
        Convolution3D(filters=64,
                      kernel_size=(5, 4, 4),
                      strides=(1, 2, 2),
                      padding="same",
                      activation='relu',
                      name='conv1',
                      input_shape=(temporal_size, spatial_size, spatial_size,
                                   channels)))

    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding="valid",
                     name='pool1'))

    model.add(
        Convolution3D(filters=128,
                      kernel_size=(5, 4, 4),
                      strides=(1, 2, 2),
                      padding="same",
                      activation='relu',
                      name='conv2'))

    model.add(
        MaxPooling3D(pool_size=(2, 2, 2),
                     strides=(2, 2, 2),
                     padding="valid",
                     name='pool2'))

    model.add(Dense(1024, activation='relu'))
    model.add(Dropout(0.25))
    model.add(Flatten())

    if weights_path:
        model.load_weights(weights_path)
    model.add(Dense(classes, activation='softmax'))

    print(model.summary())
    return model
Beispiel #11
0
def train_model():
    input_img = Input(shape=(128, 128, 128, 1))
    x = Convolution3D(16, (5, 5, 5), activation='relu', padding='same')(input_img)
    x = MaxPooling3D((2, 2, 2), padding='same')(x)
    x = Convolution3D(16, (5, 5, 5), activation='relu', padding='same')(x)
    x = MaxPooling3D((2, 2, 2), padding='same')(x)
    x = Convolution3D(16, (5, 5, 5), activation='relu', padding='same')(x)
    encoded = MaxPooling3D((2, 2, 2), padding='same', name='encoder')(x)

    print("shape of encoded: ")
    print(K.int_shape(encoded))

    x = Convolution3D(16, (5, 5, 5), activation='relu', padding='same')(encoded)
    x = UpSampling3D((2, 2, 2))(x)
    x = Convolution3D(16, (5, 5, 5), activation='relu', padding='same')(x)
    x = UpSampling3D((2, 2, 2))(x)
    x = Convolution3D(16, (5, 5, 5), activation='relu', padding='same')(x)
    x = UpSampling3D((2, 2, 2))(x)
    decoded = Convolution3D(1, (5, 5, 5), activation='sigmoid', padding='same')(x)
    print("shape of decoded: ")
    print(K.int_shape(decoded))

    autoencoder = Model(input_img, decoded)
    autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')

    autoencoder.fit(train_data, train_data,
              epochs=10,
              batch_size=14,
              validation_data=(val_data, val_data),
              callbacks=[TensorBoard(log_dir='/tmp/autoencoder')])

    autoencoder.save('autoencoder.h5')
Beispiel #12
0
    def construct_model(self):
        # th input (channels, height, width)
        # tf input (height, width, channels)
        keras.backend.set_image_dim_ordering('tf')
        model = Sequential()

        #Encode:
        model.add(Convolution3D(16,(2,3,3), activation='relu', input_shape=(config.frames_per_input, 501, 501, 3),border_mode='same'))        
        model.add(MaxPooling3D(pool_size=(2,2,2)))
        model.add(Convolution3D(16,(2,3,3), activation='relu', border_mode='same'))
        model.add(Convolution3D(16,(2,3,3), activation='relu', border_mode='same'))
        model.add(MaxPooling3D(pool_size=(2,2,2)))
        model.add(Convolution3D(8,(1,3,3), activation='relu', border_mode='same'))
        #model.add(Convolution3D(8,(1,3,3), activation='relu', border_mode='same'))
        #model.add(MaxPooling3D(pool_size=(1,2,2)))        
        #model.add(Convolution3D(8,(1,3,3), activation='relu', border_mode='same'))
        #model.add(Convolution3D(8,(1,3,3), activation='relu', border_mode='same'))
        #Decode:
        model.add(Convolution3D(16,(2,3,3), activation='relu', border_mode='same'))
        model.add(UpSampling3D((1,2,2)))
        model.add(Convolution3D(16,(2,3,3), activation='relu', border_mode='same'))
        model.add(UpSampling3D((1,2,2)))
        model.add(Convolution3D(3,(2,3,3), activation='relu', border_mode='same'))
        # model.add(Convolution3D(16,(2,3,3), activation='relu', border_mode='same'))
        # model.add(UpSampling3D((1,1,1)))
        # model.add(Convolution3D(3,(2,3,3), activation='relu', border_mode='same'))
        # model.add(UpSampling3D((1,1,1)))
        # model.add(Flatten())

        model.compile(loss='mean_squared_error', optimizer='adam')
        model.compile(loss=y_std, optimizer='adam')
        model.compile(loss='binary_crossentropy', optimizer='adam') # doesn't reset weights
        
        model.summary()        
        self.model = model
Beispiel #13
0
 def _feed_v_map(self, x, layer_idx):
     # shifting down feature maps
     print('v_shift_down' + str(layer_idx))
     x = Lambda(self._shift_vert, name='v_shift_down' + str(layer_idx))(x)
     x = Convolution3D(filters=2 * self.nb_filters,
                       kernel_size=(1, 1, 1),
                       padding='valid',
                       name='feed_v_1x1x1_conv_' + str(layer_idx))(x)
     return x
def seq3DCNN(n_flow=4, seq_len=3, map_height=32, map_width=32):
    model=Sequential()
    # model.add(ZeroPadding3D(padding=(0, 1, 1), input_shape=(n_flow, seq_len, map_height, map_width)))
    # model.add(Convolution3D(64, 2, 3, 3, border_mode='valid'))
    model.add(Convolution3D(64, 2, 3, 3, border_mode='same', input_shape=(n_flow, seq_len, map_height, map_width)))
    model.add(Activation('relu'))

    model.add(Convolution3D(128, 2, 3, 3, border_mode='same'))
    model.add(Activation('relu'))

    model.add(Convolution3D(64, 2, 3, 3, border_mode='same'))
    model.add(Activation('relu'))

    model.add(ZeroPadding3D(padding=(0, 1, 1)))
    model.add(Convolution3D(n_flow, seq_len, 3, 3, border_mode='valid'))
    # model.add(Convolution3D(n_flow, seq_len-2, 3, 3, border_mode='same'))
    model.add(Activation('tanh'))
    return model
Beispiel #15
0
    def CNN_convLSTM(activation="relu",
                     loss="binary_crossentropy",
                     optimizer="Adadelta",
                     layer=0,
                     height=0,
                     width=0,
                     days=0,
                     timesteps=0):
        """
        INPUT -> [CONV -> RELU] -> OUT
        """
        model = Sequential()

        # model.add(ZeroPadding3D((1, 1, 1), data_format="channels_last",
        # input_shape=(timesteps, height, width, layer)))
        # model.add(BatchNormalization())
        model.add(
            ConvLSTM2D(filters=16,
                       kernel_size=(3, 3),
                       padding='same',
                       activation=activation,
                       data_format="channels_last",
                       input_shape=(None, height, width, layer),
                       return_sequences=True))
        model.add(BatchNormalization())
        model.add(
            ConvLSTM2D(filters=16,
                       kernel_size=(3, 3),
                       padding='same',
                       activation=activation,
                       data_format="channels_last",
                       return_sequences=True))
        model.add(BatchNormalization())
        model.add(
            ConvLSTM2D(filters=32,
                       kernel_size=(3, 3),
                       padding='same',
                       activation=activation,
                       data_format="channels_last",
                       return_sequences=True))
        model.add(BatchNormalization())
        model.add(
            ConvLSTM2D(filters=64,
                       kernel_size=(3, 3),
                       padding='same',
                       activation=activation,
                       data_format="channels_last",
                       return_sequences=True))
        model.add(BatchNormalization())
        model.add(
            Convolution3D(filters=layer,
                          kernel_size=(3, 3, layer),
                          padding="same",
                          data_format="channels_last"))

        model.compile(loss=loss, optimizer=optimizer)
        return model
Beispiel #16
0
def conv2_block(input, k=1, dropout=0.0):
    init = input

    # Check if input number of filters is same as 32 * k, else create convolution2d for this input
    if init._keras_shape[1] != 32 * k:
        init = Convolution3D(32 * k,
                             1,
                             1,
                             1,
                             activation='linear',
                             border_mode='same',
                             init=weight_init,
                             W_regularizer=l2(weight_decay),
                             bias=use_bias)(init)

    x = Convolution3D(32 * k,
                      3,
                      3,
                      3,
                      border_mode='same',
                      init=weight_init,
                      W_regularizer=l2(weight_decay),
                      bias=use_bias)(input)
    x = BatchNormalization(axis=1)(x)
    # x = Activation('relu')(x)
    x = LeakyReLU(0.3)(x)

    if dropout > 0.0:
        x = Dropout(dropout)(x)

    x = Convolution3D(32 * k,
                      3,
                      3,
                      3,
                      border_mode='same',
                      init=weight_init,
                      W_regularizer=l2(weight_decay),
                      bias=use_bias)(x)
    x = BatchNormalization(axis=1)(x)
    # x = Activation('relu')(x)
    x = LeakyReLU(0.3)(x)

    m = merge([init, x], mode='sum')
    return m
Beispiel #17
0
 def f(input):
     activation = _bn_relu(input)
     return Convolution3D(nb_filter=nb_filter,
                          kernel_dim1=kernel_dim1,
                          kernel_dim2=kernel_dim2,
                          kernel_dim3=kernel_dim3,
                          subsample=subsample,
                          init=init,
                          border_mode=border_mode,
                          W_regularizer=W_regularizer)(activation)
Beispiel #18
0
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, 1, 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'))

    if summary:
        print(model.summary())

    return model