def createEncoder(self): base_model=InceptionV3(input_shape=(224,224,3),weights=None,include_top=False) x=base_model.output x=GlobalAveragePooling2D()(x) x = LayerNormalization()(x) model=Model(inputs=base_model.input,outputs=x) return model
def create_model2(): base_model = VGG16(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) x = base_model.output # x = Flatten()(x) x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) x = Dropout(0.25)(x) x = Dense(512, activation='relu')(x) x = Dropout(0.25)(x) x = Dense(256, activation='relu')(x) x = Dropout(0.25)(x) preds = Dense(2, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=preds) for i, layer in enumerate(model.layers): print(i, layer.name) # Fix the weight fo the first 20 layers for layer in model.layers[:20]: layer.trainable = False for layer in model.layers[20:]: layer.trainable = True return model
def build_lenet(self): base_model = InceptionV3(weights='imagenet', include_top=False) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # let's add a fully-connected layer x = Dense(1024, activation='relu')(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(self.output_classes, activation=self.output_activation)(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers for layer in base_model.layers: layer.trainable = False # compile the model (should be done *after* setting layers to non-trainable) model.compile(optimizer=self.optimizer, loss=self.loss, metrics=['accuracy']) return model
def create_model(model_size): my_new_model = Sequential() if model_size == 'L': resnet_weights_path = 'input/resnet50/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5' resnet = ResNet50(include_top=False, pooling='avg', weights=resnet_weights_path) #resnet.summary() my_new_model.add(resnet) my_new_model.layers[0].trainable = False else: vgg_weights_path = 'input/vgg16/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5' vgg = VGG16(include_top=False, weights=vgg_weights_path) vgg.summary() my_new_model.add(vgg) my_new_model.add(GlobalAveragePooling2D()) my_new_model.layers[0].trainable = False my_new_model.layers[1].trainable = False my_new_model.add(Dense(NUM_CLASSES, activation='softmax')) # Say no to train first layer (ResNet) model. It is already trained opt = optimizers.adam() my_new_model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) return my_new_model
def squeeze_excite_block(self, input_tensor, ratio=16): """ Create a channel-wise squeeze-excite block Args: input_tensor: input Keras tensor ratio: number of output filters Returns: a Keras tensor References - [Squeeze and Excitation Networks](https://arxiv.org/abs/1709.01507) """ init = input_tensor se_shape = (1, 1, self.depth) se = GlobalAveragePooling2D()(init) se = Reshape(se_shape)(se) se = Dense(self.depth // ratio, activation='relu', kernel_initializer='he_normal', use_bias=False)(se) se = Dense(self.depth, activation='sigmoid', kernel_initializer='he_normal', use_bias=False)(se) x = K.layers.multiply([init, se]) return x
def ResNet18(input_shape, classes): inputs = Input(shape=input_shape) x = ZeroPadding2D(padding=(3, 3), name='conv1_pad')(inputs) x = Conv2D(64, (7, 7), strides=(2, 2), padding='valid', kernel_initializer='he_normal', name='conv1')(x) x = BatchNormalization(axis=3, name='bn_conv1')(x) x = Activation('relu')(x) x = ZeroPadding2D(padding=(1, 1), name='pool1_pad')(x) x = MaxPooling2D((3, 3), strides=(2, 2))(x) x = conv_block(x, 3, 64, stage=2, block='a', strides=(1, 1)) x = identity_block(x, 3, 64, stage=2, block='b') x = conv_block(x, 3, 128, stage=3, block='a') x = identity_block(x, 3, 128, stage=3, block='b') x = conv_block(x, 3, 256, stage=4, block='a') x = identity_block(x, 3, 256, stage=4, block='b') x = conv_block(x, 3, 512, stage=5, block='a') x = identity_block(x, 3, 512, stage=5, block='b') x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dense(classes, activation='softmax', name='fc')(x) model = Model(inputs, x, name='resnet18') return model
def make_resnet(net): match = re.match(r'([a-z]*)(\d+)', net) net_type, n_layers = match.group(1), match.group(2) residual = (net_type == 'resnet') n = (int(n_layers) - 2) // 6 print("Net: detected n = {} {} shortcuts".format( n, 'with' if residual else 'without')) Block = ConvBNReluResidualBlock if residual else ConvBNReluBlock _layers = [ ConvBNRelu(strides=1, filters=16, input_shape=(32, 32, 3)), Block(strides=1, filters=16), [Block(strides=1, filters=16) for _ in range(n - 1)], Block(strides=2, filters=32), [Block(strides=1, filters=32) for _ in range(n - 1)], Block(strides=2, filters=64), [Block(strides=1, filters=64) for _ in range(n - 1)], GlobalAveragePooling2D(), Dense(10, 'softmax') ] layers = [] for x in _layers: try: for l in x: layers.append(l) except TypeError: layers.append(x) model = Sequential(layers) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['acc']) return model
def __init__(self, num_classes, is_tesla_k80, alpha=1, dropout=1e-3, pre_trained=True): self.is_teslaK80 = is_tesla_k80 # input layer self.inputs = Input(shape=(224, 224, 3), name="input_spatial") # data normalization self.data_norm = BatchNormalization(3, name='data_norm', center=False, scale=False) # create the base pre-trained model self.mobile_net = MobileNet( weights='imagenet' if pre_trained else None, include_top=False) self.GlobalAveragePooling2D = GlobalAveragePooling2D() shape = (1, 1, int(1024 * alpha)) self.Reshape_1 = Reshape(shape, name='reshape_1') self.Dropout = Dropout(dropout, name='dropout') self.Conv2D = Conv2D(num_classes, (1, 1), padding='same', name='conv_preds') self.Activation = Activation('softmax', name='act_softmax') self.Reshape_2 = Reshape((num_classes, ), name='reshape_2')
def get_model(lambda_centerloss, input_shape=(192, 192, 3), num_classes=8, regularizer=None, lr=0.01): image = Input(shape=input_shape) label = Input(shape=(num_classes, )) X = low_level_edge_detector(image) X = dsrc_module(X, num_filters=16, module_name='a') X = dsrc_module(X, num_filters=32, module_name='b') X = dsrc_module(X, num_filters=64, module_name='c') X = dsrc_module(X, num_filters=128, module_name='d') X = dsrc_module(X, num_filters=256, module_name='e') X = dsrc_module(X, num_filters=512, module_name='f') X = Conv2D(filters=num_classes, kernel_size=(1, 1))(X) X = GlobalAveragePooling2D()(X) X = Flatten()(X) center_loss = CenterLossLayer(alpha=0.5, name='centerlosslayer')([X, label]) if regularizer is None: X = Dense(num_classes, name='fc' + str(num_classes))(X) else: X = Dense(num_classes, name='fc' + str(num_classes), kernel_regularizer=l2(regularizer))(X) predicted_class = Activation('softmax', name='prediction')(X) model = Model(inputs=[image, label], outputs=[predicted_class, center_loss]) model.compile(optimizer=optimizers.Adam(lr=lr), loss=[losses.categorical_crossentropy, zero_loss], loss_weights=[1, lambda_centerloss], metrics=['accuracy']) return model
def Network(lr, retrain_vgg=False): # get VGG16 base_model = vgg16.VGG16(weights='imagenet', include_top=False, input_shape=(32, 32, 3)) if (retrain_vgg == False): for layer in base_model.layers: layer.trainable = False # build New Network model = Sequential() model.add(base_model) model.add(GlobalAveragePooling2D()) model.add(BatchNormalization()) model.add(Flatten()) model.add(Dense(256, activation='relu')) model.add(Dense(256, activation='relu')) model.add(Dropout(0.4)) model.add(Dense(10, activation='softmax')) # set optimizer model.compile(loss='categorical_crossentropy', optimizer=Adam(lr), metrics=['accuracy']) return model
def __init__(self): self.input_size = 96 base = MobileNetV2(input_shape=(96, 96, 3), include_top=False, weights=os.path.dirname(os.path.abspath(__file__))+'/weight/mobilenetv2/mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_96_no_top.h5') top_layer = GlobalAveragePooling2D()(base.output) gender_layer = Dense(2, activation='softmax', name='gender_prediction')(top_layer) age_layer = Dense(101, activation='softmax', name='age_prediction')(top_layer) super().__init__(inputs=base.input, outputs=[gender_layer, age_layer], name='AgenderNetMobileNetV2')
def finetune_inceptionv3(base_model, transfer_layer, x_trainable, dropout, fc_layers, num_classes, new_weights=""): # number of freezed layers: 0 (by a default) freezed_layers = 0 if x_trainable != "all": if x_trainable == 0: for layer in base_model.layers: layer.trainable = False freezed_layers = freezed_layers + 1 else: for layer in base_model.layers[:-x_trainable]: layer.trainable = False freezed_layers = freezed_layers + 1 all_layers = len(base_model.layers) print("Number of all layers in a feature-extractor part of model: {}.". format(all_layers)) print( "Number of freezed (untrainable) layers in a feature-extractor part of model: {}." .format(freezed_layers)) # adding custom layers to the classification part of a model x = transfer_layer.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(dropout)(x) predictions = Dense(num_classes, activation='softmax')(x) finetune_model = Model(inputs=base_model.input, outputs=predictions) if new_weights != "": finetune_model.load_weights(new_weights) return finetune_model
def visnet_lrn2d_model(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) convnet_output = GlobalAveragePooling2D()(vgg_model.output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) first_maxpool = MaxPooling2D(pool_size=4, strides=4)(vgg_model.input) first_conv = Conv2D(96, kernel_size=8, strides=4, activation='relu')(first_maxpool) first_lrn2d = LRN2D(n=5)(first_conv) first_zero_padding = ZeroPadding2D(padding=(3, 3))(first_lrn2d) first_maxpool2 = MaxPooling2D(pool_size=7, strides=4, padding='same')(first_zero_padding) first_maxpool2 = Flatten()(first_maxpool2) first_maxpool2 = Lambda(lambda x: K.l2_normalize(x, axis=1))(first_maxpool2) second_maxpool = MaxPooling2D(pool_size=8, strides=8)(vgg_model.input) second_conv = Conv2D(96, kernel_size=8, strides=4, activation='relu')(second_maxpool) second_lrn2d = LRN2D(n=5)(second_conv) second_zero_padding = ZeroPadding2D(padding=(1, 1))(second_lrn2d) second_maxpool2 = MaxPooling2D(pool_size=3, strides=2, padding='same')(second_zero_padding) second_maxpool2 = Flatten()(second_maxpool2) second_maxpool2 = Lambda(lambda x: K.l2_normalize(x, axis=1))(second_maxpool2) merge_one = concatenate([first_maxpool2, second_maxpool2]) merge_two = concatenate([merge_one, convnet_output]) emb = Dense(4096)(merge_two) l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb) final_model = Model(inputs=vgg_model.input, outputs=l2_norm_final) return final_model
def visnet_model(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) convnet_output = GlobalAveragePooling2D()(vgg_model.output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) first_conv = Conv2D(96, kernel_size=(8, 8), strides=(16, 16), padding='same')(vgg_model.input) first_max = MaxPool2D(pool_size=(3, 3), strides=(4, 4), padding='same')(first_conv) first_max = Flatten()(first_max) first_max = Lambda(lambda x: K.l2_normalize(x, axis=1))(first_max) second_conv = Conv2D(96, kernel_size=(8, 8), strides=(32, 32), padding='same')(vgg_model.input) second_max = MaxPool2D(pool_size=(7, 7), strides=(2, 2), padding='same')(second_conv) second_max = Flatten()(second_max) second_max = Lambda(lambda x: K.l2_normalize(x, axis=1))(second_max) merge_one = concatenate([first_max, second_max]) merge_two = concatenate([merge_one, convnet_output], axis=1) emb = Dense(4096)(merge_two) l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=l2_norm_final) return final_model
def ranknet(): vgg_model = VGG19(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) convnet_output = GlobalAveragePooling2D()(vgg_model.output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.5)(convnet_output) convnet_output = Dense(4096, activation='relu')(convnet_output) convnet_output = Dropout(0.5)(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) s1 = MaxPool2D(pool_size=(4, 4), strides=(4, 4), padding='valid')(vgg_model.input) s1 = ZeroPadding2D(padding=(4, 4), data_format=None)(s1) s1 = Conv2D(96, kernel_size=(8, 8), strides=(4, 4), padding='valid')(s1) s1 = ZeroPadding2D(padding=(2, 2), data_format=None)(s1) s1 = MaxPool2D(pool_size=(7, 7), strides=(4, 4), padding='valid')(s1) s1 = Flatten()(s1) s2 = MaxPool2D(pool_size=(8, 8), strides=(8, 8), padding='valid')(vgg_model.input) s2 = ZeroPadding2D(padding=(4, 4), data_format=None)(s2) s2 = Conv2D(96, kernel_size=(8, 8), strides=(4, 4), padding='valid')(s2) s2 = ZeroPadding2D(padding=(1, 1), data_format=None)(s2) s2 = MaxPool2D(pool_size=(3, 3), strides=(2, 2), padding='valid')(s2) s2 = Flatten()(s2) merge_one = concatenate([s1, s2]) merge_one_norm = Lambda(lambda x: K.l2_normalize(x, axis=1))(merge_one) merge_two = concatenate([merge_one_norm, convnet_output], axis=1) emb = Dense(4096)(merge_two) l2_norm_final = Lambda(lambda x: K.l2_normalize(x, axis=1))(emb) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=l2_norm_final) return final_model
def finetune_inceptionv3(base_model, transfer_layer, x_trainable, dropout, fc_layers, num_classes, new_weights=""): if x_trainable != 0: freeze = 0 for layer in base_model.layers[:-x_trainable]: layer.trainable = False freeze = freeze + 1 else: for layer in base_model.layers: layer.trainable = True all_lay = 0 for layer in base_model.layers: all_lay = all_lay + 1 print("Model with {} freezed layers.".format(freeze)) print("All layers {} .".format(all_lay)) x = transfer_layer.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(dropout)(x) predictions = Dense(num_classes, activation='softmax')(x) finetune_model = Model(inputs=base_model.input, outputs=predictions) if new_weights != "": finetune_model.load_weights(new_weights) return finetune_model
def inception(n_retrain_layers=0): K.set_image_data_format('channels_last') base_model = InceptionV3(include_top=False, input_shape=(224, 224, 3)) features = GlobalAveragePooling2D()(base_model.output) model = Model(inputs=base_model.input, outputs=features) model = _set_n_retrain(model, n_retrain_layers) return model
def __init__(self, input_data_shape, num_classes, model_name='resnet50', trainable_layers_amount=1): super(ImageModel, self).__init__() self.num_classes = num_classes self.model_name = model_name self.trainable_layers_amount = trainable_layers_amount self.input_data_shape = input_data_shape if self.model_name == 'resnet50': self.base_model = ResNet50(include_top=False, weights='imagenet', input_tensor=None, input_shape=self.input_data_shape) # Avoid training layers in resnet model. layers = self.base_model.layers print("Layers name") for layer in layers: print(layer.name) layer.trainable = False print("Making layers trainable") for layer in layers[-trainable_layers_amount:]: print(layer.name) layer.trainable = True x0 = Input(shape=self.input_data_shape) x1 = Lambda(preprocess_input, output_shape=self.input_data_shape)(x0) x2 = self.base_model(x1) x3 = GlobalAveragePooling2D()(x2) x4 = Dense(1024, activation='relu')(x3) x5 = Dense(num_classes, activation='softmax', name='softmax')(x4) self.model = Model(inputs=x0, outputs=x5)
def construct_model(self): if self.name == 'inceptionv3': print('{:=^75}'.format('Downloading {}'.format(self.name))) self.base_model = InceptionV3(**params['network_params']) print('{:=^75}'.format('Download Complete')) elif self.name == 'xception': print('{:=^75}'.format('Downloading {}'.format(self.name))) self.base_model = Xception(**params['network_params']) print('{:=^75}'.format('Download Complete')) # 모델 구조 base model -> global average pooling -> dense print('{:=^75}'.format('Adding layers')) self.model = Sequential() self.model.add(self.base_model) self.model.add(GlobalAveragePooling2D()) self.model.add(Dense(params['num_classes'], activation='softmax')) print('{:=^75}'.format('Added layers')) # 지정 경로에 저장 if not os.path.exists('weight_path/'): os.mkdir('weight_path/') self.weight_save_path = os.path.join('weight_path/', self.name + "_weights.h5") print('{:=^75}'.format('Saving weights to {}'.format(self.weight_save_path))) self.model.save_weights(self.weight_save_path) print('{:=^75}'.format('Saved weights'))
def Build(self): # create the input layer for feeding the netowrk inLayer = Input(self.inputShape, self.batchSize) net = ConvBnLRelu(32, kernelSize=3)(inLayer) # 1 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(64, kernelSize=3)(net) # 2 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(128, kernelSize=3)(net) # 3 net = ConvBnLRelu(64, kernelSize=1)(net) # 4 net = ConvBnLRelu(128, kernelSize=3)(net) # 5 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(256, kernelSize=3)(net) # 6 net = ConvBnLRelu(128, kernelSize=1)(net) # 7 net = ConvBnLRelu(256, kernelSize=3)(net) # 8 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(512, kernelSize=3)(net) # 9 net = ConvBnLRelu(256, kernelSize=1)(net) # 10 net = ConvBnLRelu(512, kernelSize=3)(net) # 11 net = ConvBnLRelu(256, kernelSize=1)(net) # 12 net = ConvBnLRelu(512, kernelSize=3)(net) # 13 net = MaxPool2D((2, 2), strides=(2, 2))(net) net = ConvBnLRelu(1024, kernelSize=3)(net) # 14 net = ConvBnLRelu(512, kernelSize=1)(net) # 15 net = ConvBnLRelu(1024, kernelSize=3)(net) # 16 net = ConvBnLRelu(512, kernelSize=1)(net) # 17 net = ConvBnLRelu(1024, kernelSize=3)(net) # 18 # variational encoder output (distributions) mean = Conv2D(filters=self.latentSize, kernel_size=(1, 1), padding='same')(net) mean = GlobalAveragePooling2D()(mean) stddev = Conv2D(filters=self.latentSize, kernel_size=(1, 1), padding='same')(net) stddev = GlobalAveragePooling2D()(stddev) sample = SampleLayer(self.latentConstraints, self.beta, self.latentCapacity, self.randomSample)([mean, stddev]) return Model(inputs=inLayer, outputs=sample)
def createModel(): base_model = ResNet152V2(input_shape=(224, 224, 3), weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) model = Model(inputs=base_model.input, outputs=x) return model
def bypass_image_features_graph_with_gap(image_feature_size, image_top_layer, image_top_layer_dropout_rate): image_size = int(np.sqrt(image_feature_size)) vinput = Input((image_feature_size, 512), name="input_images") outputs = Reshape((image_size, image_size, 512))(vinput) outputs = GlobalAveragePooling2D(name="baseline_gap")(outputs) if image_top_layer: outputs = __add_top_layer(outputs, image_top_layer_dropout_rate) return outputs, vinput
def empty_resnet(): K.set_image_data_format('channels_last') base_model = ResNet50(weights=None, include_top=False, input_shape=(224, 224, 3)) features = GlobalAveragePooling2D()(base_model.output) model = Model(inputs=base_model.input, outputs=features) return model
def Mildnet_all_trainable(): vgg_model = VGG16(weights="imagenet", include_top=False, input_shape=(224, 224, 3)) intermediate_layer_outputs = get_layers_output_by_name(vgg_model, ["block1_pool", "block2_pool", "block3_pool", "block4_pool"]) convnet_output = GlobalAveragePooling2D()(vgg_model.output) for layer_name, output in intermediate_layer_outputs.items(): output = GlobalAveragePooling2D()(output) convnet_output = concatenate([convnet_output, output]) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Dropout(0.6)(convnet_output) convnet_output = Dense(2048, activation='relu')(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=convnet_output) return final_model
def __init__(self, args): self.args = args self.features = net_setting(include_top=False, input_tensor=None, input_shape=(args.CenterCropSize[0], args.CenterCropSize[1], 3)) self.GAP = GlobalAveragePooling2D(name='avg_pool') self.dropout = Dropout(0.5) self.dense = Dense(2, activation='softmax', name='softmax')
def se_net(in_block, depth): x = GlobalAveragePooling2D()(in_block) x = Dense(depth // 16, activation='relu', kernel_initializer=default_init, bias_initializer='zeros')(x) x = Dense(depth, activation='sigmoid', kernel_regularizer=l2_reg)(x) return Multiply()([in_block, x])
def __init__(self, include_top=True, classes=1000, pooling=None): self.include_top = include_top self.pooling = pooling self.Conv2d_1a_3x3 = BasicConv2d(32, 3, 3, strides=(2, 2), padding='valid') self.Conv2d_2a_3x3 = BasicConv2d(32, 3, 3, padding='valid') self.Conv2d_2b_3x3 = BasicConv2d(64, 3, 3) self.maxpool1 = MaxPooling2D((3, 3), strides=(2, 2)) self.Conv2d_3b_1x1 = BasicConv2d(80, 1, 1, padding='valid') self.Conv2d_4a_3x3 = BasicConv2d(192, 3, 3, padding='valid') self.maxpool2 = MaxPooling2D((3, 3), strides=(2, 2)) self.Mixed_5b = InceptionA(192, pool_features=32, name='mixed0', branch_pool_filters=32) self.Mixed_5c = InceptionA(256, pool_features=64, name='mixed1', branch_pool_filters=64) self.Mixed_5d = InceptionA(288, pool_features=64, name='mixed2', branch_pool_filters=64) self.Mixed_6a = InceptionB(288, name='mixed3') self.Mixed_6b = InceptionC(768, channels_7x7=128, name='mixed4') self.Mixed_6c = InceptionC(768, channels_7x7=160, name='mixed5') self.Mixed_6d = InceptionC(768, channels_7x7=160, name='mixed6') self.Mixed_6e = InceptionC(768, channels_7x7=192, name='mixed7') self.Mixed_7a = InceptionD(768, name='mixed8') self.Mixed_7b = InceptionE(1280, name_idx=0) self.Mixed_7c = InceptionE(2048, name_idx=1) self.classifier_GAP = GlobalAveragePooling2D(name='avg_pool') self.classifier_fc = Dense(classes, activation='softmax', name='predictions') self.GAP = GlobalAveragePooling2D() self.GMP = GlobalMaxPooling2D()
def Mildnet_mobilenet(): vgg_model = MobileNet(weights=None, include_top=False, input_shape=(224, 224, 3)) intermediate_layer_outputs = get_layers_output_by_name(vgg_model, ["conv_dw_1_relu", "conv_dw_2_relu", "conv_dw_4_relu", "conv_dw_6_relu", "conv_dw_12_relu"]) convnet_output = GlobalAveragePooling2D()(vgg_model.output) for layer_name, output in intermediate_layer_outputs.items(): output = GlobalAveragePooling2D()(output) convnet_output = concatenate([convnet_output, output]) convnet_output = GlobalAveragePooling2D()(vgg_model.output) convnet_output = Dense(1024, activation='relu')(convnet_output) convnet_output = Dropout(0.5)(convnet_output) convnet_output = Dense(1024, activation='relu')(convnet_output) convnet_output = Lambda(lambda x: K.l2_normalize(x, axis=1))(convnet_output) final_model = tf.keras.models.Model(inputs=vgg_model.input, outputs=convnet_output) return final_model
def f(inputs): squeeze = GlobalAveragePooling2D()(inputs) out_dim = squeeze.get_shape().as_list()[-1] excitation = Dense(units=out_dim / ratio)(squeeze) excitation = Activation("relu")(excitation) excitation = Dense(units=out_dim)(excitation) excitation = Activation("sigmoid")(excitation) excitation = Reshape([1, 1, out_dim])(excitation) scale = Multiply()([inputs, excitation]) return scale
def build_model_xception_avg(lock_base_model: bool): base_model = Xception(input_shape=INPUT_SHAPE, include_top=False, pooling=None, weights=None) if lock_base_model: for layer in base_model.layers: layer.trainable = False x = GlobalAveragePooling2D(name='avg_pool_final')(base_model.layers[-1].output) res = Dense(NB_CLASSES, activation='sigmoid', name='classes', kernel_initializer='zero', kernel_regularizer=l1(1e-5))(x) model = Model(inputs=base_model.inputs, outputs=res) return model