def layer(input_tensor): # extracting params and names for layers conv_params = get_conv_params() bn_params = get_bn_params() conv_name, bn_name, relu_name, sc_name = handle_block_names( stage, block) x = Conv2D(filters, (1, 1), name=conv_name + '1', **conv_params)(input_tensor) x = BatchNormalization(name=bn_name + '1', **bn_params)(x) x = Activation('relu', name=relu_name + '1')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = GroupConv2D(filters, (3, 3), conv_params, conv_name + '2', strides=strides)(x) x = BatchNormalization(name=bn_name + '2', **bn_params)(x) x = Activation('relu', name=relu_name + '2')(x) x = Conv2D(filters * 2, (1, 1), name=conv_name + '3', **conv_params)(x) x = BatchNormalization(name=bn_name + '3', **bn_params)(x) shortcut = Conv2D(filters * 2, (1, 1), name=sc_name, strides=strides, **conv_params)(input_tensor) shortcut = BatchNormalization(name=sc_name + '_bn', **bn_params)(shortcut) x = Add()([x, shortcut]) x = Activation('relu', name=relu_name)(x) return x
def layer(input_tensor): # extracting params and names for layers conv_params = get_conv_params(padding='same') bn_params = get_bn_params() conv_name, bn_name, relu_name, sc_name, add_name, attention_name = handle_block_names( stage, block) x = Conv2D(int(filters) // 2, (1, 1), name=conv_name + '1', **conv_params)(input_tensor) x = BatchNormalization(name=bn_name + '1x1_1', **bn_params)(x) x = Activation('relu', name=relu_name + '1x1_1')(x) x = SK_block_deepwise(filters, stage, block, r=r, L=L)(x) x = Conv2D(filters, (1, 1), name=conv_name + '2', **conv_params)(x) x = BatchNormalization(name=bn_name + '1x1_2', **bn_params)(x) shortcut = Conv2D(filters, (1, 1), name=sc_name, **conv_params)(input_tensor) shortcut = BatchNormalization(name=sc_name + '_bn', **bn_params)(shortcut) x = Add()([x, shortcut]) x = Activation('relu', name=relu_name + 'addout')(x) return x
def layer(input_tensor): # extracting params and names for layers conv_params = get_conv_params(padding='same', use_bias=False) conv_params_dilation = get_conv_params(padding='same', use_bias=False, dilation_rate=2) bn_params = get_bn_params() fc_bn_params = get_bn_params(axis=-1) conv_name, bn_name, relu_name, sc_name, add_name, attention_name = handle_block_names( stage, block) x_3 = DepthwiseConv2D((3, 3), padding='same', name=conv_name + '3_3')(input_tensor) x_3 = BatchNormalization(name=bn_name + '3_3', **bn_params)(x_3) x_3 = Activation('relu', name=relu_name + '3_3')(x_3) x_5 = DepthwiseConv2D((5, 5), padding='same', name=conv_name + '5_5')(input_tensor) x_5 = BatchNormalization(name=bn_name + '5_5', **bn_params)(x_5) x_5 = Activation('relu', name=relu_name + '5_5')(x_5) u = Add(name=add_name)([x_3, x_5]) s = GlobalAveragePooling2D(name=attention_name + 'gap')(u) d = max(int(s.shape[-1]) // r, L) z = Dense(d, name=attention_name + 'fc1')(s) z = BatchNormalization(name=bn_name + 'vector', **fc_bn_params)(z) z = Activation('relu', name=relu_name + 'vector')(z) c = s.get_shape().as_list()[-1] z1 = Dense(c * 2, name=attention_name + 'fc2')(z) z1 = Reshape([2, -1], name=attention_name + 'reshape1')(z1) soft = Softmax(axis=1, name=attention_name + 'softmax')(z1) A = Lambda(lambda vector: vector[:, 0, :])(soft) A = Reshape([1, 1, -1], name=attention_name + 'reshape_A')(A) B = Lambda(lambda vector: vector[:, 1, :])(soft) B = Reshape([1, 1, -1], name=attention_name + 'reshape_B')(B) x_3 = Lambda(scale_feature)([A, x_3]) x_5 = Lambda(scale_feature)([B, x_5]) v = Add()([x_3, x_5]) return v
def layer(x): # extracting params and names for layers conv_params = get_conv_params(padding='same') bn_params = get_bn_params() conv_name, bn_name, relu_name, sc_name, add_name, attention_name = handle_block_names( stage, block) x = Conv2D(int(filters) // 2, (1, 1), name=conv_name + '1', **conv_params)(x) x = BatchNormalization(name=bn_name + '1x1_1', **bn_params)(x) x = Activation('relu', name=relu_name + '1x1_1')(x) x = SK_block(filters, stage, block, r=r, L=L)(x) x = Conv2D(filters, (1, 1), name=conv_name + '2', **conv_params)(x) x = BatchNormalization(name=bn_name + '1x1_2', **bn_params)(x) x = Activation('relu', name=relu_name + '1x1_2')(x) return x
def layer(input_tensor): conv_params = get_conv_params() bn_params = get_bn_params() conv_name, bn_name, relu_name, sc_name = handle_block_names( stage, block) x = Conv2D(filters, (1, 1), name=conv_name + '1', **conv_params)(input_tensor) x = BatchNormalization(name=bn_name + '1', **bn_params)(x) x = Activation('relu', name=relu_name + '1')(x) x = ZeroPadding2D(padding=(1, 1))(x) x = GroupConv2D(filters, (3, 3), conv_params, conv_name + '2')(x) x = BatchNormalization(name=bn_name + '2', **bn_params)(x) x = Activation('relu', name=relu_name + '2')(x) x = Conv2D(filters * 2, (1, 1), name=conv_name + '3', **conv_params)(x) x = BatchNormalization(name=bn_name + '3', **bn_params)(x) x = Add()([x, input_tensor]) x = Activation('relu', name=relu_name)(x) return x
def layer(x): # extracting params and names for layers conv_params = get_conv_params(padding='same') bn_params = get_bn_params() conv_name, bn_name, relu_name, sc_name, add_name, attention_name = handle_block_names( stage, block) if x.shape[-1] < filters: behind_sknet = True else: behind_sknet = False if not behind_sknet: x = Conv2D(filters, (1, 1), name=conv_name + '1', **conv_params)(x) x = BatchNormalization(name=bn_name + '1x1_1', **bn_params)(x) x = Activation('relu', name=relu_name + '1x1_1')(x) x = SK_block(filters, stage, block, r=r, L=L)(x) if behind_sknet: x = Conv2D(filters, (1, 1), name=conv_name + '1', **conv_params)(x) x = BatchNormalization(name=bn_name + '1x1_1', **bn_params)(x) x = Activation('relu', name=relu_name + '1x1_1')(x) return x
def build_mycov(repetitions=(2, 2, 2, 2), input_tensor=None, input_shape=None, classes=1, first_filters=64): """ TODO """ if input_tensor is None: img_input = Input(shape=input_shape, name='data') else: if not K.is_keras_tensor(input_tensor): img_input = Input(tensor=input_tensor, shape=input_shape) else: img_input = input_tensor # get parameters for model layers no_scale_bn_params = get_bn_params(scale=False) conv_params = get_conv_params(padding='same') init_filters = first_filters # resnext bottom x = BatchNormalization(name='bn_data', **no_scale_bn_params)(img_input) # down dw_h_convs = OrderedDict() for stage, rep in enumerate(repetitions): for block in range(rep): filters = init_filters * (2**stage) x = SK_change_block(filters, stage, block, behind_sknet=True)(x) if block == 1: dw_h_convs[stage] = x x = MaxPooling2D((2, 2), name='pooling' + str(stage + 1))(x) filters = init_filters * (2**4) x = SK_change_block(filters, 4, 0, behind_sknet=True)(x) x = SK_change_block(filters, 4, 1, behind_sknet=True)(x) # up for stage, rep in enumerate(repetitions): stage += 5 filters = init_filters * (2**(4 - stage + 5 - 1)) x = Conv2DTranspose(filters, (2, 2), strides=(2, 2), name='up' + str(stage + 1), padding='same')(x) x = Concatenate(axis=-1, name='merge' + str(stage + 1) + str(9 - stage))([x, dw_h_convs[8 - stage]]) for block in range(rep): x = SK_change_block(filters, stage, block, behind_sknet=False)(x) x = Conv2D(classes, (1, 1), activation='sigmoid', name='output', **conv_params)(x) # Ensure that the model takes into account any potential predecessors of `input_tensor`. if input_tensor is not None: inputs = get_source_inputs(input_tensor) else: inputs = img_input # Create model model = Model(inputs, x) return model