def sep_conv_norm(x, n_filters, kernel_size, strides=(1, 1), dilation_rate=(1, 1)): x = DepthwiseConv2D(kernel_size=kernel_size, strides=strides, padding='same', dilation_rate=dilation_rate)(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = Conv2D(n_filters, 1, padding='same')(x) # Should another norm be put here? return x
def SepConv_BN(x, dropout, filters, prefix, stride=1, kernel_size=3, rate=1, depth_activation=False, epsilon=1e-3): """ SepConv with BN between depthwise & pointwise. Optionally add activation after BN Implements right "same" padding for even kernel sizes Args: x: input tensor filters: num of filters in pointwise convolution prefix: prefix before name stride: stride at depthwise conv kernel_size: kernel size for depthwise convolution rate: atrous rate for depthwise convolution depth_activation: flag to use activation between depthwise & pointwise convs epsilon: epsilon to use in BN layer """ if stride == 1: depth_padding = 'same' if not depth_activation: x = Activation('relu')(x) x = DepthwiseConv2D((kernel_size, kernel_size), strides=(stride, stride), dilation_rate=(rate, rate), padding=depth_padding, use_bias=False, name=prefix + '_depthwise')(x) x = BatchNormalization(name=prefix + '_depthwise_BN', epsilon=epsilon)(x) if depth_activation: x = Activation('relu')(x) x = Conv2D(filters, (1, 1), padding='same', use_bias=False, name=prefix + '_pointwise')(x) x = BatchNormalization(name=prefix + '_pointwise_BN', epsilon=epsilon)(x) if depth_activation: x = Activation('relu')(x) if dropout != 0.0: x = Dropout(dropout)(x) return x
def encoder_model(self, architecture): model = Sequential() if architecture == 'EEGNet': model.add(Conv2D(8, (1, 32), padding='same', use_bias=False)) model.add(BatchNormalization(axis=3)) model.add( DepthwiseConv2D((self.chans, 1), use_bias=False, depth_multiplier=2, depthwise_constraint=max_norm(1.))) model.add(BatchNormalization(axis=3)) model.add(Activation('elu')) model.add(AveragePooling2D((1, 4))) model.add(Dropout(0.25)) model.add( SeparableConv2D(16, (1, 16), use_bias=False, padding='same')) model.add(BatchNormalization(axis=3)) model.add(Activation('elu')) model.add(AveragePooling2D((1, 8))) model.add(Dropout(0.25)) model.add(Flatten()) elif architecture == 'DeepConvNet': model.add(Conv2D(25, (1, 5))) model.add(Conv2D(25, (self.chans, 1), use_bias=False)) model.add(BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)) model.add(Activation('elu')) model.add(MaxPooling2D(pool_size=(1, 2), strides=(1, 2))) model.add(Dropout(0.5)) model.add(Conv2D(50, (1, 5), use_bias=False)) model.add(BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)) model.add(Activation('elu')) model.add(MaxPooling2D(pool_size=(1, 2), strides=(1, 2))) model.add(Dropout(0.5)) model.add(Conv2D(100, (1, 5), use_bias=False)) model.add(BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)) model.add(Activation('elu')) model.add(MaxPooling2D(pool_size=(1, 2), strides=(1, 2))) model.add(Dropout(0.5)) model.add(Conv2D(200, (1, 5), use_bias=False)) model.add(BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)) model.add(Activation('elu')) model.add(MaxPooling2D(pool_size=(1, 2), strides=(1, 2))) model.add(Dropout(0.5)) model.add(Flatten()) elif architecture == 'ShallowConvNet': model.add(Conv2D(40, (1, 13))) model.add(Conv2D(40, (self.chans, 1), use_bias=False)) model.add(BatchNormalization(axis=3, epsilon=1e-05, momentum=0.1)) model.add(Activation(lambda x: tf.square(x))) model.add(AveragePooling2D(pool_size=(1, 35), strides=(1, 7))) model.add( Activation(lambda x: tf.log( tf.clip(x, min_value=1e-7, max_value=10000)))) model.add(Dropout(0.5)) model.add(Flatten()) input = Input(shape=(self.chans, self.samples, 1)) latent = model(input) return Model(input, latent, name='enc')
def inverted_res_block(inputs, expansion, stride, alpha, filters, block_id): in_channels = K.int_shape(inputs)[-1] pointwise_conv_filters = int(filters * alpha) pointwise_filters = _make_divisible(pointwise_conv_filters, 8) x = inputs # Expand x = Conv2D(expansion * in_channels, kernel_size=1, padding='same', activation=None)(x) x = BatchNormalization()(x) x = Activation(relu6)(x) if stride == 2: x = ZeroPadding2D()(x) x = DepthwiseConv2D(kernel_size=3, strides=stride, activation=None, padding='same' if stride == 1 else 'valid')(x) x = BatchNormalization()(x) x = Activation(relu6)(x) # Project x = Conv2D(pointwise_filters, kernel_size=1, padding='same', use_bias=False, activation=None)(x) x = BatchNormalization()(x) if in_channels == pointwise_filters and stride == 1: return Add()([inputs, x]) return x
def _bottleneck(inputs, filters, kernel, t, s, indim, channels, r=False, layername=''): """Bottleneck This function defines a basic bottleneck structure. # Arguments inputs: Tensor, input tensor of conv layer. filters: Integer, the dimensionality of the output space. kernel: An integer or tuple/list of 2 integers, specifying the width and height of the 2D convolution window. t: Integer, expansion factor. t is always applied to the input size. s: An integer or tuple/list of 2 integers,specifying the strides of the convolution along the width and height.Can be a single integer to specify the same value for all spatial dimensions. t: Boolean, Whether to use the residuals. # Returns Output tensor. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 tchannel = K.int_shape(inputs)[channel_axis] * t convblockname = 'covba' + layername x, indim, channels = ExNet._conv_block(inputs, tchannel, (1, 1), (1, 1), indim=indim, channels=channels, layername=convblockname) dwblockname = 'dw_' + layername + str(indim) + str(channels) x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same', name=dwblockname)(x) print(s, indim) if s == 2: indim = int((indim + 1) / 2) print(indim) bnlayername = 'bn_' + layername + str(indim) + '_' + str(channels) x = BatchNormalization(axis=channel_axis, name=bnlayername)(x) aclayername = 'ac_' + layername + str(indim) + '_' + str(channels) x = ReLU(6., name=aclayername)(x) convlayername = 'conv_' + layername + str(indim) + '_' + str( channels) + '_' + str(filters) x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name=convlayername)(x) bnlayername = 'bn1_' + layername + str(indim) + '_' + str(channels) x = BatchNormalization(axis=channel_axis, name=bnlayername)(x) if r: addlayername = 'add1_' + layername + str(indim) + '_' + str( channels) x = add([x, inputs], name=addlayername) return x, indim, channels
def separable_conv_bn(x, n_filters, stride=1, kernel_size=3, rate=1, depth_activation=False, weight_decay=1e-4, kernel_initializer="he_normal", bn_epsilon=1e-3, bn_momentum=0.99): """ Separable convolution, with BN between depthwise and pointwise. :param x: 4-D tensor, shape of (batch_size, height, width, channel). :param n_filters: int, number of filters in pointwise convolution. :param stride: int, default 1. :param kernel_size: int, default 3. :param rate: int, default 1. :param depth_activation: bool, whether to add activation after BN, default False. :param weight_decay: float, default 1e-4. :param kernel_initializer: string, default "he_normal". :param bn_epsilon: float, default 1e-3. :param bn_momentum: float, default 0.99. :return: 4-D tensor, shape of (batch_size, height, width, channel) """ if stride == 1: depth_padding = 'same' else: kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) pad_total = kernel_size_effective - 1 pad_beg = pad_total // 2 pad_end = pad_total - pad_beg x = ZeroPadding2D((pad_beg, pad_end))(x) depth_padding = 'valid' if not depth_activation: x = Activation('relu')(x) x = DepthwiseConv2D(kernel_size=(kernel_size, kernel_size), strides=(stride, stride), dilation_rate=(rate, rate), padding=depth_padding, use_bias=False, kernel_regularizer=l2(weight_decay), kernel_initializer=kernel_initializer)(x) x = BatchNormalization(epsilon=bn_epsilon, momentum=bn_momentum)(x) if depth_activation: x = Activation('relu')(x) x = Conv2D(n_filters, (1, 1), padding='same', use_bias=False, kernel_regularizer=l2(weight_decay), kernel_initializer=kernel_initializer)(x) x = BatchNormalization(epsilon=bn_epsilon, momentum=bn_momentum)(x) if depth_activation: x = Activation('relu')(x) return x
def block(X): X = DepthwiseConv2D( kernel_size=kernel_size, strides=strides, padding='SAME', depth_multiplier=1, depthwise_initializer=EfficientNetCovInitializer())(X) X = BatchNormalization(axis=3)(X) X = Swish()(X) return X
def inverted_bottleneck_block(x, t, c, n, s): input_channels = K.int_shape(x)[-1] expand = input_channels * t input_x = x if s > 1: # first block should be a strided conv, no res m = Conv2D(expand, (1, 1), strides=s, padding='same')(x) m = BatchNormalization()(m) m = Activation(relu6)(m) m = DepthwiseConv2D((3, 3), padding='same')(m) m = BatchNormalization()(m) m = Activation(relu6)(m) m = Conv2D(c, (1, 1), padding='same')(m) input_x = BatchNormalization()(m) input_channels = c n = n - 1 for _ in range(n): m = Conv2D(expand, (1, 1), strides=1, padding='same')(input_x) m = BatchNormalization()(m) m = Activation(relu6)(m) m = DepthwiseConv2D((3, 3), padding='same')(m) m = BatchNormalization()(m) m = Activation(relu6)(m) m = Conv2D(c, (1, 1), padding='same')(m) m = BatchNormalization()(m) # if output_channels > input_channels, perform a projection to higher dimensions if c > input_channels: # pad the input_x ch = (c - input_channels) // 2 input_x = Lambda(lambda tens: tf.pad(tens, [[0, 0], [0, 0], [0, 0], [ch, ch]]))(input_x) input_channels = c # if input_channels > output_channels, learn a projection down elif input_channels > c: input_x = Conv2D(c, (1, 1), padding='same')(input_x) input_channels = c input_x = Add()([m, input_x]) return input_x
def SepConv_BN(x, filters, prefix, stride=1, kernel_size=3, rate=1, depth_activation=False, epsilon=1e-3): """ SepConv with BN between depthwise & pointwise. Optionally add activation after BN Implements right "same" padding for even kernel sizes Args: x: input tensor filters: num of filters in pointwise convolution prefix: prefix before name stride: stride at depthwise conv kernel_size: kernel size for depthwise convolution rate: atrous rate for depthwise convolution depth_activation: flag to use activation between depthwise & poinwise convs epsilon: epsilon to use in BN layer """ if stride == 1: depth_padding = 'same' else: kernel_size_effective = kernel_size + (kernel_size - 1) * (rate - 1) pad_total = kernel_size_effective - 1 pad_beg = pad_total // 2 pad_end = pad_total - pad_beg x = ZeroPadding2D((pad_beg, pad_end))(x) depth_padding = 'valid' if not depth_activation: x = Activation('relu')(x) x = DepthwiseConv2D((kernel_size, kernel_size), strides=(stride, stride), dilation_rate=(rate, rate), padding=depth_padding, use_bias=False, name=prefix + '_depthwise')(x) x = InstanceNormalization(name=prefix + '_depthwise_BN', epsilon=epsilon)(x) if depth_activation: x = Activation('relu')(x) x = Conv2D(filters, (1, 1), padding='same', use_bias=False, name=prefix + '_pointwise')(x) x = InstanceNormalization(name=prefix + '_pointwise_BN', epsilon=epsilon)(x) if depth_activation: x = Activation('relu')(x) return x
def EEGNet(nb_classes=3, Chans=3, Samples=75, dropoutRate=0.25, kernLength=150, F1=2, D=2, F2=8, dropoutType='Dropout'): dropoutType = Dropout input1 = Input(shape=(1, Samples, Chans)) block1 = Conv2D(F1, (1, kernLength), padding='same', input_shape=(1, Samples, Chans), use_bias=False)(input1) block1 = BatchNormalization(axis=1)(block1) block1 = DepthwiseConv2D((Chans, 1), use_bias=False, depth_multiplier=D, depthwise_constraint=max_norm(1.), data_format='channels_first')(block1) block1 = BatchNormalization(axis=1)(block1) block1 = Activation('elu')(block1) block1 = AveragePooling2D((1, 2), data_format='channels_first')(block1) block1 = dropoutType(dropoutRate)(block1) block2 = SeparableConv2D(F2, (1, 16), use_bias=False, padding='same')(block1) block2 = BatchNormalization(axis=1)(block2) block2 = Activation('elu')(block2) block2 = AveragePooling2D((1, 1))(block2) block2 = dropoutType(dropoutRate)(block2) flatten = Flatten(name='flatten')(block2) lstm = LSTM(128, activation='tanh', recurrent_activation='hard_sigmoid', \ use_bias=True, kernel_initializer='glorot_uniform', \ recurrent_initializer='orthogonal', \ unit_forget_bias=True, kernel_regularizer=None, \ recurrent_regularizer=None, \ bias_regularizer=None, activity_regularizer=None, \ kernel_constraint=None, recurrent_constraint=None, \ bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, \ implementation=1, return_sequences=True, return_state=False, \ go_backwards=False, stateful=False, unroll=False)(block2) dense = Dense(nb_classes, name='dense', kernel_constraint=max_norm(0.25))(lstm) softmax = Activation('softmax', name='softmax')(dense) return Model(inputs=input1, outputs=softmax)
def EEGNet(nb_classes=3, Chans=5, Samples=300, dropoutRate=0.25, kernLength=150, F1=2, D=2, F2=8, dropoutType='Dropout'): if dropoutType == 'SpatialDropout2D': dropoutType = SpatialDropout2D elif dropoutType == 'Dropout': dropoutType = Dropout else: raise ValueError('dropoutType must be one of SpatialDropout2D ' 'or Dropout, passed as a string.') input1 = Input(shape=(1, Samples, Chans)) block1 = Conv2D(F1, (1, kernLength), padding='same', input_shape=(1, Samples, Chans), use_bias=False)(input1) block1 = BatchNormalization(axis=1)(block1) block1 = DepthwiseConv2D((Chans, 1), use_bias=False, depth_multiplier=D, depthwise_constraint=max_norm(1.), data_format='channels_first')(block1) block1 = BatchNormalization(axis=1)(block1) block1 = Activation('elu')(block1) block1 = AveragePooling2D((1, 2), data_format='channels_first')(block1) block1 = dropoutType(dropoutRate)(block1) block2 = SeparableConv2D(F2, (1, 16), use_bias=False, padding='same')(block1) block2 = BatchNormalization(axis=1)(block2) block2 = Activation('elu')(block2) block2 = AveragePooling2D((1, 1))(block2) block2 = dropoutType(dropoutRate)(block2) flatten = Flatten(name='flatten')(block2) dense = Dense(nb_classes, name='dense', kernel_constraint=max_norm(0.25))(flatten) softmax = Activation('softmax', name='softmax')(dense) return Model(inputs=input1, outputs=softmax)
def _bottleneck(self, inputs, filters, strides=(1, 1), t=6, kernel=(3, 3), name='conv'): """ bottleneck 子层的实现 :param inputs: 输入的tensor :param filters: 输出的维度 :param kernel: DW卷积核大小 :param t:初次卷积的channel放大倍数 :param strides: 卷积步数 :param name:该层卷积的名称起始 :return: 单层BN的tensor """ inputs = BatchNormalization(axis=-1)(inputs) # 判断输入的通道数,channel_first\channel_last模式不同在于把channel的数量放在了第一个维度还是最后一个维度 axis_channel = 1 if K.image_data_format() == 'channel_first' else -1 input_shape = K.int_shape(inputs) #print(input_shape) channel = input_shape[axis_channel] * t add = strides[0] == 1 and strides[1] == 1 and filters == input_shape[ axis_channel] x = Conv2D(channel, (1, 1), strides=(1, 1), padding='same', name=name + '_1')(inputs) x = Activation('relu')(x) x = DepthwiseConv2D(kernel, strides=strides, padding='same', name=name + '_2')(x) x = Activation('relu')(x) x = Conv2D(filters, (1, 1), strides=(1, 1), padding='same', name=name + '_3')(x) if add: x = Add()([x, inputs]) return x
def _bottleneck(inputs, filters, kernel, t, alpha, s, r=False): """Bottleneck This function defines a basic bottleneck structure. # Arguments inputs: Tensor, input tensor of conv layer. filters: Integer, the dimensionality of the output space. kernel: An integer or tuple/list of 2 integers, specifying the width and height of the 2D convolution window. t: Integer, expansion factor. t is always applied to the input size. s: An integer or tuple/list of 2 integers,specifying the strides of the convolution along the width and height.Can be a single integer to specify the same value for all spatial dimensions. alpha: Integer, width multiplier. r: Boolean, Whether to use the residuals. # Returns Output tensor. """ channel_axis = 1 if K.image_data_format() == 'channels_first' else -1 # Depth tchannel = K.int_shape(inputs)[channel_axis] * t # Width cchannel = int(filters * alpha) x = _conv_block(inputs, tchannel, (1, 1), (1, 1)) x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x) x = BatchNormalization(axis=channel_axis)(x) x = Activation(relu6)(x) x = Conv2D(cchannel, (1, 1), strides=(1, 1), padding='same')(x) x = BatchNormalization(axis=channel_axis)(x) if r: x = Add()([x, inputs]) return x
def bottleneck(inputs, filters, kernel, t, s, r=False): tchannel = K.int_shape(inputs)[-1] * t x = conv_block(inputs, 'conv', tchannel, (1, 1), strides=(1, 1)) x = DepthwiseConv2D(kernel, strides=(s, s), depth_multiplier=1, padding='same')(x) x = BatchNormalization()(x) x = Activation('relu')(x) x = conv_block(x, 'conv', filters, (1, 1), strides=(1, 1), padding='same', relu=False) if r: x = Add()([x, inputs]) return x
def EEGNet_SSVEP(nb_classes, Chans=64, Samples=128, regRate=0.0001, dropoutRate=0.25, kernLength=64, numFilters=8): """ Keras Implementation of the variant of EEGNet that was used to classify signals from an SSVEP task (https://arxiv.org/abs/1803.04566) Inputs: nb_classes : int, number of classes to classify Chans, Samples : number of channels and time points in the EEG data regRate : regularization parameter for L1 and L2 penalties dropoutRate : dropout fraction kernLength : length of temporal convolution in first layer numFilters : number of temporal-spatial filter pairs to learn """ input1 = Input(shape=(1, Chans, Samples)) ################################################################## layer1 = Conv2D(numFilters, (1, kernLength), padding='same', kernel_regularizer=l1_l2(l1=0.0, l2=0.0), input_shape=(1, Chans, Samples), use_bias=False)(input1) layer1 = BatchNormalization(axis=1)(layer1) layer1 = DepthwiseConv2D((Chans, 1), depthwise_regularizer=l1_l2(l1=regRate, l2=regRate), use_bias=False)(layer1) layer1 = BatchNormalization(axis=1)(layer1) layer1 = Activation('elu')(layer1) layer1 = SpatialDropout2D(dropoutRate)(layer1) layer2 = SeparableConv2D(numFilters, (1, 8), depthwise_regularizer=l1_l2(l1=0.0, l2=regRate), use_bias=False, padding='same')(layer1) layer2 = BatchNormalization(axis=1)(layer2) layer2 = Activation('elu')(layer2) layer2 = AveragePooling2D((1, 4))(layer2) layer2 = SpatialDropout2D(dropoutRate)(layer2) layer3 = SeparableConv2D(numFilters * 2, (1, 8), depth_multiplier=2, depthwise_regularizer=l1_l2(l1=0.0, l2=regRate), use_bias=False, padding='same')(layer2) layer3 = BatchNormalization(axis=1)(layer3) layer3 = Activation('elu')(layer3) layer3 = AveragePooling2D((1, 4))(layer3) layer3 = SpatialDropout2D(dropoutRate)(layer3) flatten = Flatten(name='flatten')(layer3) dense = Dense(nb_classes, name='dense')(flatten) softmax = Activation('softmax', name='softmax')(dense) return Model(inputs=input1, outputs=softmax)
gfe_layer = bottleneck_block(gfe_layer, 96, (3, 3), t=6, strides=2, n=3) gfe_layer = bottleneck_block(gfe_layer, 128, (3, 3), t=6, strides=1, n=3) gfe_layer = pyramid_pooling_block(gfe_layer, [2, 4, 6, 8]) ff_layer1 = conv_block(lds_layer, 'conv', 128, kernel_size=(1, 1), padding='same', strides=(1, 1), relu=False) ff_layer2 = UpSampling2D(size=(4, 4))(gfe_layer) ff_layer2 = DepthwiseConv2D(128, strides=(1, 1), depth_multiplier=1, padding='same')(ff_layer2) ff_layer2 = BatchNormalization()(ff_layer2) ff_layer2 = Activation('relu')(ff_layer2) ff_layer2 = Conv2D(128, kernel_size=(1, 1), strides=(1, 1), padding='same', activation=None)(ff_layer2) ff_final = Add()([ff_layer1, ff_layer2]) ff_final = BatchNormalization()(ff_final) ff_final = Activation('relu')(ff_final) classifier = SeparableConv2D(128, kernel_size=(3, 3),
def build_model(X_train, row, cell, nb_classes=3, Chans=3, Samples=75, dropoutRate=0.25, kernLength=150, F1=2, D=2, F2=8, dropoutType='Dropout'): model = Sequential() dropoutType = Dropout model.add( Conv2D(F1, (1, kernLength), padding='same', input_shape=(1, Samples, Chans), use_bias=False)) model.add(BatchNormalization(axis=1)) model.add( DepthwiseConv2D((Chans, 1), use_bias=False, depth_multiplier=D, depthwise_constraint=max_norm(1.), data_format='channels_first')) model.add(BatchNormalization(axis=1)) model.add(Activation('elu')) model.add(AveragePooling2D((1, 2), data_format='channels_first')) model.add(SeparableConv2D(F2, (1, 16), use_bias=False, padding='same')) model.add(BatchNormalization(axis=1)) model.add(Activation('elu')) model.add(AveragePooling2D((1, 1))) model.add(dropoutType(dropoutRate)) model.add(Reshape((2, 8 * 73))) # model.add(Flatten(name='flatten')) model.add(LSTM(128, activation='tanh', recurrent_activation='hard_sigmoid', \ use_bias=True, kernel_initializer='glorot_uniform', \ recurrent_initializer='orthogonal', \ unit_forget_bias=True, kernel_regularizer=None, \ recurrent_regularizer=None, \ bias_regularizer=None, activity_regularizer=None, \ kernel_constraint=None, recurrent_constraint=None, \ bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, \ implementation=1, return_sequences=True, return_state=False, \ go_backwards=False, stateful=False, unroll=False)) model.add(dropoutType(dropoutRate)) model.add(LSTM(64, activation='tanh', recurrent_activation='hard_sigmoid', \ use_bias=True, kernel_initializer='glorot_uniform', \ recurrent_initializer='orthogonal', \ unit_forget_bias=True, kernel_regularizer=None, \ recurrent_regularizer=None, \ bias_regularizer=None, activity_regularizer=None, \ kernel_constraint=None, recurrent_constraint=None, \ bias_constraint=None, dropout=0.0, recurrent_dropout=0.0, \ implementation=1, return_sequences=False, return_state=False, \ go_backwards=False, stateful=False, unroll=False)) model.add(Dense(nb_classes, name='dense', kernel_constraint=max_norm(0.25))) model.add(Activation('softmax', name='softmax')) opt = optimizers.adam(lr=0.001) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=['accuracy']) plot_model(model, to_file='model.png', show_shapes=True) model.summary() return model
def _depthwise_conv_block(inputs, pointwise_conv_filters, alpha, depth_multiplier=1, strides=(1, 1), block_id=1): """Adds a depthwise convolution block. A depthwise convolution block consists of a depthwise conv, batch normalization, relu6, pointwise convolution, batch normalization and relu6 activation. # Arguments inputs: Input tensor of shape `(rows, cols, channels)` (with `channels_last` data format) or (channels, rows, cols) (with `channels_first` data format). pointwise_conv_filters: Integer, the dimensionality of the output space (i.e. the number of output filters in the pointwise convolution). alpha: controls the width of the network. - If `alpha` < 1.0, proportionally decreases the number of filters in each layer. - If `alpha` > 1.0, proportionally increases the number of filters in each layer. - If `alpha` = 1, default number of filters from the paper are used at each layer. depth_multiplier: The number of depthwise convolution output channels for each input channel. The total number of depthwise convolution output channels will be equal to `filters_in * depth_multiplier`. strides: An integer or tuple/list of 2 integers, specifying the strides of the convolution along the width and height. Can be a single integer to specify the same value for all spatial dimensions. Specifying any stride value != 1 is incompatible with specifying any `dilation_rate` value != 1. block_id: Integer, a unique identification designating the block number. # Input shape 4D tensor with shape: `(batch, channels, rows, cols)` if data_format='channels_first' or 4D tensor with shape: `(batch, rows, cols, channels)` if data_format='channels_last'. # Output shape 4D tensor with shape: `(batch, filters, new_rows, new_cols)` if data_format='channels_first' or 4D tensor with shape: `(batch, new_rows, new_cols, filters)` if data_format='channels_last'. `rows` and `cols` values might have changed due to stride. # Returns Output tensor of block. """ channel_axis = -1 pointwise_conv_filters = int(pointwise_conv_filters * alpha) if strides == (1, 1): x = inputs else: x = ZeroPadding2D(((0, 1), (0, 1)), name='conv_pad_%d' % block_id)(inputs) x = DepthwiseConv2D((3, 3), padding='same' if strides == (1, 1) else 'valid', depth_multiplier=depth_multiplier, strides=strides, use_bias=False, name='conv_dw_%d' % block_id)(x) x = BatchNormalization(axis=channel_axis, name='conv_dw_%d_bn' % block_id)(x) x = ReLU(6., name='conv_dw_%d_relu' % block_id)(x) x = Conv2D(pointwise_conv_filters, (1, 1), padding='same', use_bias=False, strides=(1, 1), name='conv_pw_%d' % block_id)(x) x = BatchNormalization(axis=channel_axis, name='conv_pw_%d_bn' % block_id)(x) return ReLU(6., name='conv_pw_%d_relu' % block_id)(x)
def EEGNet(nb_classes, Chans=64, Samples=128, dropoutRate=0.25, kernLength=64, F1=4, D=2, F2=8, dropoutType='Dropout'): """ Keras Implementation of EEGNet (https://arxiv.org/abs/1611.08024) Note that this implements the newest version of EEGNet and NOT the earlier version (version v1 and v2 on arxiv). We strongly recommend using this architecture as it performs much better and has nicer properties than our earlier version. For example: 1. Depthwise Convolutions to learn spatial filters within a temporal convolution. The use of the depth_multiplier option maps exactly to the number of spatial filters learned within a temporal filter. This matches the setup of algorithms like FBCSP which learn spatial filters within each filter in a filter-bank. This also limits the number of free parameters to fit when compared to a fully-connected convolution. 2. Separable Convolutions to learn how to optimally combine spatial filters across temporal bands. Separable Convolutions are Depthwise Convolutions followed by (1x1) Pointwise Convolutions. While the original paper used Dropout, we found that SpatialDropout2D sometimes produced slightly better results for classification of ERP signals. However, SpatialDropout2D significantly reduced performance on the Oscillatory dataset (SMR, BCI-IV Dataset 2A). We recommend using the default Dropout in most cases. Assumes the input signal is sampled at 128Hz. If you want to use this model for any other sampling rate you will need to modify the lengths of temporal kernels and average pooling size in blocks 1 and 2 as needed (double the kernel lengths for double the sampling rate, etc). Note that we haven't tested the model performance with this rule so this may not work well. The model with default parameters gives the EEGNet-4,2 model as discussed in the paper. This model should do pretty well in general, although as the paper discussed the EEGNet-8,2 (with 8 temporal kernels and 2 spatial filters per temporal kernel) can do slightly better on the SMR dataset. Other variations that we found to work well are EEGNet-4,1 and EEGNet-8,1. We set F2 = F1 * D (number of input filters = number of output filters) for the SeparableConv2D layer. We haven't extensively tested other values of this parameter (say, F2 < F1 * D for compressed learning, and F2 > F1 * D for overcomplete). We believe the main parameters to focus on are F1 and D. Inputs: nb_classes : int, number of classes to classify Chans, Samples : number of channels and time points in the EEG data dropoutRate : dropout fraction kernLength : length of temporal convolution in first layer. We found that setting this to be half the sampling rate worked well in practice. For the SMR dataset in particular since the data was high-passed at 4Hz we used a kernel length of 32. F1, F2 : number of temporal filters (F1) and number of pointwise filters (F2) to learn. Default: F1 = 4, F2 = F1 * D. D : number of spatial filters to learn within each temporal convolution. Default: D = 2 dropoutType : Either SpatialDropout2D or Dropout, passed as a string. """ if dropoutType == 'SpatialDropout2D': dropoutType = SpatialDropout2D elif dropoutType == 'Dropout': dropoutType = Dropout else: raise ValueError('dropoutType must be one of SpatialDropout2D ' 'or Dropout, passed as a string.') input1 = Input(shape=(1, Chans, Samples)) ################################################################## block1 = Conv2D(F1, (1, kernLength), padding='same', input_shape=(1, Chans, Samples), use_bias=False)(input1) block1 = BatchNormalization(axis=1)(block1) block1 = DepthwiseConv2D((Chans, 1), use_bias=False, depth_multiplier=D, depthwise_constraint=max_norm(1.))(block1) block1 = BatchNormalization(axis=1)(block1) block1 = Activation('elu')(block1) block1 = AveragePooling2D((1, 4))(block1) block1 = dropoutType(dropoutRate)(block1) block2 = SeparableConv2D(F2, (1, 16), use_bias=False, padding='same')(block1) block2 = BatchNormalization(axis=1)(block2) block2 = Activation('elu')(block2) block2 = AveragePooling2D((1, 8))(block2) block2 = dropoutType(dropoutRate)(block2) flatten = Flatten(name='flatten')(block2) dense = Dense(nb_classes, name='dense', kernel_constraint=max_norm(0.25))(flatten) softmax = Activation('softmax', name='softmax')(dense) return Model(inputs=input1, outputs=softmax)
def create_ae(self): encoder = Sequential() encoder.add( Conv2D(16, kernel_size=3, input_shape=self.img_shape, padding="same")) encoder.add(Activation('relu')) encoder.add(BatchNormalization()) encoder.add(MaxPooling2D()) encoder.add(Dropout(0.1)) encoder.add(DepthwiseConv2D(3, padding="same")) encoder.add(Activation('relu')) encoder.add(BatchNormalization()) encoder.add(Conv2D(32, kernel_size=3, padding="same")) encoder.add(MaxPooling2D()) encoder.add(Dropout(0.1)) encoder.add(DepthwiseConv2D(3, padding="same")) encoder.add(Activation('relu')) encoder.add(BatchNormalization()) encoder.add(Conv2D(64, kernel_size=3, padding="same")) encoder.add(MaxPooling2D()) encoder.add(Dropout(0.1)) encoder.add(DepthwiseConv2D(3, padding="same")) encoder.add(Activation('relu')) encoder.add(BatchNormalization()) encoder.add(Conv2D(128, kernel_size=3, padding="same")) encoder.add(MaxPooling2D()) encoder.add(Dropout(0.1)) encoder.add(DepthwiseConv2D(3, padding="same")) encoder.add(Activation('relu')) encoder.add(BatchNormalization()) encoder.add(Conv2D(256, kernel_size=3, padding="same")) encoder.add(Dropout(0.1)) encoder.add(DepthwiseConv2D(3, padding="same")) encoder.add(Activation('relu')) encoder.add(BatchNormalization()) encoder.add(Conv2D(256, kernel_size=3, padding="same")) encoder.add(Flatten()) encoder.add(Dense(self.latent_dim, activation="relu", use_bias=False)) encoder.add(BatchNormalization()) encoder.add(Dense(self.latent_dim, activation="relu", use_bias=False)) encoder.add(BatchNormalization()) encoder.add(Dense(self.latent_dim, activation="relu", use_bias=False)) encoder.summary ############ decoder = Sequential() decoder.add(Dense(self.latent_dim, activation="relu", use_bias=False)) encoder.add(BatchNormalization()) decoder.add(Dense(self.latent_dim, activation="relu", use_bias=False)) encoder.add(BatchNormalization()) decoder.add( Dense(6 * 9 * 8, activation="relu", use_bias=False, input_dim=(self.latent_dim))) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(Reshape((6, 9, 8))) decoder.add(Dense(256, use_bias=False)) decoder.add(Activation("relu")) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(Conv2D(256, kernel_size=(3, 3), padding="same")) decoder.add(Activation("relu")) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(UpSampling2D()) decoder.add(Conv2D(128, kernel_size=(3, 3), padding="same")) decoder.add(Activation("relu")) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(UpSampling2D()) decoder.add(ZeroPadding2D(padding=((0, 1), (0, 1)))) decoder.add(Conv2D(64, kernel_size=(3, 3), padding="same")) decoder.add(Activation("relu")) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(UpSampling2D()) decoder.add(ZeroPadding2D(padding=(0, (1, 0)))) decoder.add(Conv2D(32, kernel_size=(3, 3), padding="same")) decoder.add(Activation("relu")) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(UpSampling2D()) decoder.add(Conv2D(16, kernel_size=(3, 3), padding="same")) decoder.add(Activation("relu")) decoder.add(BatchNormalization(momentum=0.8)) decoder.add(Dense(self.channels)) decoder.add(Activation("tanh")) inp = Input(shape=(self.img_shape)) y = encoder(inp) x = decoder(y) ae = Model(inp, x) ae.summary() return encoder, decoder, ae
y = y[indices] # prepare weighting for classes since they're unbalanced class_totals = y.sum(axis=0) class_weight = class_totals.max() / class_totals img_rows, img_cols = X.shape[1:] # add additional dimension X = X.reshape(X.shape[0], X.shape[1], X.shape[2], 1) model = Sequential() model.add( Conv2D(32, (3, 3), activation='relu', input_shape=(img_rows, img_cols, 1))) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(DepthwiseConv2D((3, 3), activation='relu')) model.add(BatchNormalization()) model.add(Dropout(0.25)) model.add(GlobalAveragePooling2D()) # model.add(Flatten()) model.add(Dense(128, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(nb_classes, activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) config.total_params = model.count_params() model.fit(X,
def ADSNet(): # encoder: layers definition && data flow -------------------------------------- # CNN module 1 ------------------------------------- encoder_inputs = Input(shape=(num_frames_truth, 159, 159, 1), name='encoder_inputs') # (bs, 3, 159, 159, 1) encoder_conv2d_1 = TimeDistributed(Conv2D(filters=4, kernel_size=(5, 5), padding='same'), name='en_conv2d_1')(encoder_inputs) encoder_conv2d_1 = TimeDistributed(Activation('relu'))(encoder_conv2d_1) encoder_conv2d_1 = TimeDistributed(MaxPooling2D(padding='same'))(encoder_conv2d_1) encoder_conv2d_2 = TimeDistributed(Conv2D(filters=4, kernel_size=(5, 5), padding='same'), name='en_conv2d_2')(encoder_conv2d_1) encoder_conv2d_2 = TimeDistributed(Activation('relu'))(encoder_conv2d_2) encoder_conv2d_2 = TimeDistributed(MaxPooling2D(padding='same'))(encoder_conv2d_2) # --------------------------------------------------- _, en_h, en_c = ConvLSTM2D(filters=8, kernel_size=(5, 5), return_sequences=True, return_state=True, padding='same', name='en_convlstm')(encoder_conv2d_2) # -------------------------------------------------------------------------------- # # encoder to decoder: layers definition && data flow -------------------- en_h = Conv2D(filters=128, kernel_size=(1, 1), padding="same", name='en_de_h', activation='relu')(en_h) en_c = Conv2D(filters=128, kernel_size=(1, 1), padding="same", name='en_de_c', activation='relu')(en_c) # -------------------------------------------------------------------------------- # decoder: layers definition && dataflow ----------------------------------------- decoder_inputs = Input(shape=(num_frames, 159, 159, fea_dim), name='decoder_inputs') # (bs, 12, 159, 159, fea_dim) norm_inputs = Reshape((num_frames, 159 * 159, fea_dim))(decoder_inputs) norm_inputs = Lambda(_min_max, arguments={'axis': 2})(norm_inputs) norm_inputs = Reshape((num_frames, 159, 159, fea_dim))(norm_inputs) # CNN module 2 ----------------------------------------------------------- de_conv2d_1 = Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu') de_conv2d_2 = Conv2D(filters=32, kernel_size=(5, 5), padding='same', activation='relu') # ------------------------------------------------------------------------- # attention module ---------------------------------------------------------------- att_conv2d_1 = TimeDistributed(DepthwiseConv2D(kernel_size=(5, 5), padding='same', depth_multiplier=1), name='att_conv2d_1')(norm_inputs) att_conv2d_1 = TimeDistributed(Activation('relu'))(att_conv2d_1) att_conv2d_1 = TimeDistributed(MaxPooling2D(padding='same'))(att_conv2d_1) att_conv2d_2 = TimeDistributed(DepthwiseConv2D(kernel_size=(5, 5), padding='same', depth_multiplier=1), name='att_conv2d_2')(att_conv2d_1) att_conv2d_2 = TimeDistributed(Activation('relu'))(att_conv2d_2) att_conv2d_2 = TimeDistributed(MaxPooling2D(padding='same'))(att_conv2d_2) att_conv_hc_to_x = Conv2D(filters=1, name='att_conv_hc_to_x', kernel_size=(1, 1)) att_conv_x = DepthwiseConv2D(name='att_conv_x', kernel_size=(1, 1)) de_convlstm = ConvLSTM2D(filters=128, return_sequences=True, return_state=True, kernel_size=(5, 5), name='de_convlstm_f', padding='same') alpha_list = [] out_list = [] att_h = en_h att_c = en_c for t in range(num_frames): _att_x_t = Cropping3D(data_format='channels_last', cropping=((t, num_frames - t - 1), (0, 0), (0, 0)))( att_conv2d_2) # (bs,1,40,40,fea_dim) norm_x_t = Cropping3D(data_format='channels_last', cropping=((t, num_frames - t - 1), (0, 0), (0, 0)))( norm_inputs) # (bs,1,159,159,fea_dim) att_x_t = Lambda(lambda x: K.squeeze(x, axis=1))(_att_x_t) # (bs,40,40,fea_dim) att_x_t = att_conv_x(att_x_t) # (bs,40,40,fea_dim) hc = Concatenate(axis=-1)([att_h, att_c]) # output: (bs,40,40,128*2) hc = att_conv_hc_to_x(hc) # (bs,40,40,1) hc_x = multiply([hc, att_x_t]) # (bs,40,40,fea_dim) e = Lambda(lambda x: K.sum(x, axis=[1, 2], keepdims=False))(hc_x) # (bs,fea_dim) e = Lambda(lambda x: x * 0.025)(e) # (bs,fea_dim) alpha = Activation('softmax')(e) # output: (bs,fea_dim) norm_x_t = multiply([alpha, norm_x_t]) # output: (bs,1,40,40,fea_dim) norm_x_t = TimeDistributed(de_conv2d_1)(norm_x_t) norm_x_t = TimeDistributed(MaxPooling2D(padding='same'))(norm_x_t) norm_x_t = TimeDistributed(de_conv2d_2)(norm_x_t) norm_x_t = TimeDistributed(MaxPooling2D(padding='same'))(norm_x_t) # (bs, 1, 40, 40, 32) att_o, att_h, att_c = de_convlstm([norm_x_t, att_h, att_c]) alpha_list.append(alpha) out_list.append(att_o) decoder_convlstm = Concatenate(axis=1)(out_list) # output: (bs,12,40,40,128) # --------------------------------------------------------------------------------- # DCNN module ------------------------------------------------------------ decoder_conv2dT_1 = TimeDistributed(Conv2DTranspose(filters=32, kernel_size=(5, 5), strides=(2, 2), padding='same'), name='de_conv2dT_1')(decoder_convlstm) decoder_conv2dT_1 = TimeDistributed(Activation('relu'))(decoder_conv2dT_1) decoder_conv2dT_2 = TimeDistributed(Conv2DTranspose(filters=32, kernel_size=(5, 5), strides=(2, 2), padding='same'), name='de_conv2dT_2')(decoder_conv2dT_1) decoder_conv2dT_2 = TimeDistributed(Activation('relu'))(decoder_conv2dT_2) decoder_outputs = TimeDistributed(Conv2D(filters=1, kernel_size=(1, 1), padding="same"), name='de_out_conv2d')( decoder_conv2dT_2) # --------------------------------------------------------------------------------- decoder_outputs = Cropping3D(cropping=((0, 0), (0, 1), (0, 1)))(decoder_outputs) decoder_outputs = Reshape((-1, 159 * 159, 1), input_shape=(-1, 159, 159, 1))(decoder_outputs) return Model([decoder_inputs, encoder_inputs], decoder_outputs, name='ADSNet')
def customized_num_filter_graph(blocks, block_conv, block_channels, dropout_rate, input_shape, channels, model_dest, num_nontrainable_conv): nontrainable_conv = [x for x in range(num_nontrainable_conv)] padding = padding_calculate(input_shape, blocks) output_frame = 4 up_block_channels = [ block_channels[-(x + 2)] for x in range(len(block_channels) - 1) ] + [output_frame] maxp_list = list() input_img = Input(batch_shape=(None, None, None, channels)) nontrainable_name = list() #----down sampling---- for i in range(blocks): for j in range(block_conv): layer_name = 'down_block_' + str(i) + '_conv_' + str(j) layer_name_BN = 'down_block_' + str(i) + '_BN_' + str(j) layer_name_depth = 'down_block_' + str(i) + '_depth' if i == 0 and i in nontrainable_conv and j == 0: conv = Conv2D(int(block_channels[i] / 2), name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(input_img) conv = BatchNormalization(name=layer_name_BN)(conv) conv_depth_input = DepthwiseConv2D( depth_multiplier=8, name=layer_name_depth, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1))(input_img) conv_depth_input = BatchNormalization(name=layer_name_depth + '_BN')(conv_depth_input) pointwise_conv = Conv2D(int(block_channels[i] / 2), name='pointwise_' + str(i), kernel_size=(1, 1), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='sigmoid')(conv_depth_input) pointwise_conv = BatchNormalization(name=layer_name_depth + '_BN_2')(pointwise_conv) combine = concatenate([pointwise_conv, conv]) elif i == 0 and i not in nontrainable_conv and j == 0: conv = Conv2D(block_channels[i], name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(input_img) combine = BatchNormalization(name=layer_name_BN)(conv) elif i != 0 and i in nontrainable_conv and j == 0: conv = Conv2D(int(block_channels[i] / 2), name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(maxp_list[i - 1]) conv = BatchNormalization(name=layer_name_BN)(conv) conv_depth = DepthwiseConv2D(depth_multiplier=8, name=layer_name_depth, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1))( maxp_list[i - 1]) conv_depth = BatchNormalization(name=layer_name_depth + '_BN')(conv_depth) pointwise_conv = Conv2D(int(block_channels[i] / 2), name='pointwise_' + str(i), kernel_size=(1, 1), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='sigmoid')(conv_depth) pointwise_conv = BatchNormalization(name=layer_name_depth + '_BN_2')(pointwise_conv) combine = concatenate([pointwise_conv, conv]) elif i != 0 and i not in nontrainable_conv and j == 0: conv = Conv2D(block_channels[i], name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(maxp_list[i - 1]) combine = BatchNormalization(name=layer_name_BN)(conv) elif j == 1: conv = Conv2D(block_channels[i], name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(combine) conv = BatchNormalization(name=layer_name_BN)(conv) else: conv = Conv2D(block_channels[i], name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(conv) conv = BatchNormalization(name=layer_name_BN)(conv) if i in nontrainable_conv: nontrainable_name.append(layer_name_depth) maxp = MaxPooling2D(name='down_block_' + str(i) + '_maxp')(conv) maxp = Dropout(dropout_rate, name='down_block_' + str(i) + '_dropout')(maxp) maxp_list.append(maxp) #----down sampling---- #-----up sampling----- for i in range(blocks): for j in range(block_conv + 1): layer_name = 'up_block_' + str(i) + '_conv_' + str(j) layer_name_BN = 'up_block_' + str(i) + '_BN_' + str(j) layer_name_convt = 'up_block_' + str(i) + '_ConvT' if i == 0 and j == 0: up = Conv2D(block_channels[-1], name='mini_maxp_conv1', kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(maxp_list[-(i + 1)]) up = BatchNormalization(name='mini_maxp_BN1')(up) up = Conv2D(block_channels[-1], name='mini_maxp_conv2', kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(up) up = BatchNormalization(name='mini_maxp_BN2')(up) up = Dropout(dropout_rate, name='mini_maxp_BN2_dropout')(up) up = Conv2DTranspose(up_block_channels[i], name=layer_name_convt, kernel_size=(2, 2), strides=(2, 2), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(up) up = BatchNormalization(name=layer_name_BN)(up) if padding[i]: up = ZeroPadding2D(padding=((0, 1), (1, 0)), name='up_block_' + str(i) + '_zeropadd')(up) elif i != 0 and j == 0: up = add([maxp_list[-(i + 1)], up]) up = Conv2DTranspose(up_block_channels[i], name=layer_name_convt, kernel_size=(2, 2), strides=(2, 2), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(up) up = BatchNormalization(name=layer_name_BN)(up) if padding[i]: up = ZeroPadding2D(padding=((0, 1), (1, 0)), name='up_block_' + str(i) + '_zeropadd')(up) else: up = Conv2D(up_block_channels[i], name=layer_name, kernel_size=(3, 3), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='relu')(up) up = BatchNormalization(name=layer_name_BN)(up) #-----up sampling----- output = Conv2D(1, name='output', kernel_size=(1, 1), padding='same', dilation_rate=(1, 1), kernel_initializer='glorot_uniform', activation='sigmoid')(up) model = Model(input_img, output) layer_dict = dict([(layer.name, layer) for layer in model.layers]) for name in nontrainable_name: input_channel = layer_dict[name].input_shape[3] bias = layer_dict[name].get_weights()[1] init_filter = filters(input_channel) # print(input_channel,init_filter.shape,layer_dict[name].get_weights()[0].shape) layer_dict[name].set_weights([init_filter, bias]) layer_dict[name].trainable = False plot_model(model, to_file=model_dest + '/model.png', show_shapes=True) trainable_count = int( np.sum([K.count_params(p) for p in set(model.trainable_weights)])) nontrainable_count = int( np.sum([K.count_params(p) for p in set(model.non_trainable_weights)])) with open(model_dest + '/modelsummary.txt', 'w') as f: f.write('Number of non-trainable parameters: ' + str(nontrainable_count) + '\n') f.write('Number of trainable parameters: ' + str(trainable_count) + '\n') f.write('Number of total parameters: ' + str(nontrainable_count + trainable_count) + '\n') with redirect_stdout(f): model.summary() print( '######################################-Summary-#########################################' ) print('Number of non-trainable parameters:', nontrainable_count) print('Number of trainable parameters: ', trainable_count) print('Number of total parameters: ', nontrainable_count + trainable_count) print( '########################################################################################' ) return model pass