def build(input_shape, num_outputs): print('original input shape:', input_shape) _handle_dim_ordering() if len(input_shape) != 4: raise Exception("Input shape should be a tuple (nb_channels, kernel_dim1, kernel_dim2, kernel_dim3)") print('original input shape:', input_shape) if K.image_dim_ordering() == 'tf': input_shape = (input_shape[1], input_shape[2], input_shape[3], input_shape[0]) print('change input shape:', input_shape) input = Input(shape=input_shape) # input = applyPCA(input, 30) conv1 = Conv3D(filters=16, kernel_size=(3, 3, 7), strides=(1, 1, 5), kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', use_bias=False, activation='relu')(input) conv1 = BatchNormalization()(conv1) conv2 = Conv3D(filters=32, kernel_size=(3, 3, 5), strides=(1, 1, 3), kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', use_bias=False, activation='relu')(conv1) conv2 = BatchNormalization()(conv2) print(conv2.shape) conv3 = Conv3D(filters=64, kernel_size=(3, 3, 3), strides=(1, 1, 1), kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', use_bias=False, activation='relu')(conv2) conv3 = BatchNormalization()(conv3) print(conv3.shape) conv3 = SeparableConv3D(filters=128, kernel_size=(3, 3, 1), strides=(1, 1, 1), kernel_initializer=regularizers.l2(0.01), use_bias=False, activation='relu')(conv3) print(conv3._keras_shape) conv3_shape = conv3._keras_shape l = Reshape((conv3_shape[1], conv3_shape[2], conv3_shape[3] * conv3_shape[4]))(conv3) print(l) # conv11 l = Conv2D(32, (1, 1), padding='same', kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', use_bias=False, activation='relu')(l) l = BatchNormalization()(l) print(l.shape) # l = Conv2D(64, (1, 1), padding='same', kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', # use_bias=False, activation='relu')(l) # l3 = BatchNormalization()(l) # conv12 l_offset = ConvOffset2D(32)(l) l1 = Conv2D(64, (3, 3), padding='same', strides=(1, 1), kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', use_bias=False, activation='relu')(l_offset) l1 = BatchNormalization()(l1) print(l1.shape) # conv21 l_offset = ConvOffset2D(32)(l) l2 = Conv2D(64, (5, 5), padding='same', strides=(1, 1), dilation_rate=5, kernel_regularizer=regularizers.l2(0.01), kernel_initializer='he_normal', use_bias=False, activation='relu')(l_offset) l2 = BatchNormalization()(l2) print(l2.shape) l = keras.layers.add([l1, l2]) se = squeeze_excite_block(l) l1 = multiply([l1, se]) l2 = multiply([l2, se]) l = keras.layers.add([l1, l2]) # out l = GlobalAvgPool2D()(l) # 输入分类器 # Classifier block dense = Dense(units=num_outputs, activation="softmax", kernel_initializer="he_normal")(l) model = Model(inputs=input, outputs=dense) return model
def build(input_shape, num_outputs): print('original input shape:', input_shape) _handle_dim_ordering() if len(input_shape) != 4: raise Exception( "Input shape should be a tuple (nb_channels, kernel_dim1, kernel_dim2, kernel_dim3)" ) print('original input shape:', input_shape) # orignal input shape: 1,7,7,200 if K.image_dim_ordering() == 'tf': input_shape = (input_shape[1], input_shape[2], input_shape[3], input_shape[0]) print('change input shape:', input_shape) # 用keras中函数式模型API,不用序贯模型API # 张量流输入 input = Input(shape=input_shape) # print(input.shape) # ( ?,7,7,200,1 ) conv1 = Conv3D(filters=16, kernel_size=(11, 11, 24), strides=(12, 12, 5), kernel_regularizer=regularizers.l2(0.01))(input) print(conv1.shape) # ( None, 1,1,16,16 ) l = Reshape((16, 16, 1))(conv1) print(l) # conv11 l = Conv2D(32, (3, 3), padding='same', name='conv11', trainable=False)(l) l = Activation('relu', name='conv11_relu')(l) l = BatchNormalization(name='conv11_bn')(l) # conv12 l_offset = ConvOffset2D(32, name='conv12_offset')(l) l = Conv2D(64, (3, 3), padding='same', strides=(2, 2), name='conv12', trainable=False)(l_offset) l = Activation('relu', name='conv12_relu')(l) l = BatchNormalization(name='conv12_bn')(l) # conv21 l_offset = ConvOffset2D(64, name='conv21_offset')(l) l = Conv2D(64, (3, 3), padding='same', strides=(2, 2), name='conv21', trainable=False)(l_offset) l = Activation('relu', name='conv21_relu')(l) l = BatchNormalization(name='conv21_bn')(l) # out l = GlobalAvgPool2D(name='avg_pool')(l) # 输入分类器 # Classifier block dense = Dense(units=num_outputs, activation="softmax", kernel_initializer="he_normal")(l) model = Model(inputs=input, outputs=dense) return model