def build_backbone_net_graph(input_tensor, architecture, weights=None): """ Build basic feature extraction networks. :param input_tensor: Input of the basic networks, should be a tensor or tf.keras.layers.Input :param architecture: The architecture name of the basic network. :param weights: Whether download and initialize weights from the pre-trained weights, could be either 'imagenet', (pre-training on ImageNet) 'noisy-student', 'None' (random initialization), or the path to the weights file to be loaded。 :return: Efficient Model and corresponding endpoints. """ assert architecture in ['efficientnet-b0', 'efficientnet-b1', 'efficientnet-b2', 'efficientnet-b3', 'efficientnet-b4', 'efficientnet-b5', 'efficientnet-b7', 'efficientnet-b7', 'efficientnet-l2'] if architecture == 'efficientnet-b0': return efn.EfficientNetB0(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b1': return efn.EfficientNetB1(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b2': return efn.EfficientNetB2(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b3': return efn.EfficientNetB3(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b4': return efn.EfficientNetB4(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b5': return efn.EfficientNetB5(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b6': return efn.EfficientNetB6(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-b7': return efn.EfficientNetB7(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) elif architecture == 'efficientnet-l2': return efn.EfficientNetL2(include_top=False, weights=weights, input_tensor=input_tensor, input_shape=[None, None, 3]) else: raise ValueError("Argument architecture should in " "[efficientnet-b0, efficientnet-b1, " "efficientnet-b2, efficientnet-b3, efficientnet-b4, efficientnet-b5, " "efficientnet-b7, efficientnet-b7, efficientnet-l2] " "but get %s" % architecture)
def effnet_retinanet(num_classes, backbone='EfficientNetB0', inputs=None, modifier=None, **kwargs): """ Constructs a retinanet model using a resnet backbone. Args num_classes: Number of classes to predict. backbone: Which backbone to use (one of ('resnet50', 'resnet101', 'resnet152')). inputs: The inputs to the network (defaults to a Tensor of shape (None, None, 3)). modifier: A function handler which can modify the backbone before using it in retinanet (this can be used to freeze backbone layers for example). Returns RetinaNet model with a ResNet backbone. """ # choose default input if inputs is None: if keras.backend.image_data_format() == 'channels_first': inputs = keras.layers.Input(shape=(3, None, None)) else: # inputs = keras.layers.Input(shape=(224, 224, 3)) inputs = keras.layers.Input(shape=(None, None, 3)) # get last conv layer from the end of each block [28x28, 14x14, 7x7] if backbone == 'EfficientNetB0': model = efn.EfficientNetB0(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB1': model = efn.EfficientNetB1(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB2': model = efn.EfficientNetB2(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB3': model = efn.EfficientNetB3(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB4': model = efn.EfficientNetB4(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB5': model = efn.EfficientNetB5(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB6': model = efn.EfficientNetB6(input_tensor=inputs, include_top=False, weights=None) elif backbone == 'EfficientNetB7': model = efn.EfficientNetB7(input_tensor=inputs, include_top=False, weights=None) else: raise ValueError('Backbone (\'{}\') is invalid.'.format(backbone)) layer_outputs = ['block4a_expand_activation', 'block6a_expand_activation', 'top_activation'] layer_outputs = [ model.get_layer(name=layer_outputs[0]).output, # 28x28 model.get_layer(name=layer_outputs[1]).output, # 14x14 model.get_layer(name=layer_outputs[2]).output, # 7x7 ] # create the densenet backbone model = keras.Model(inputs=inputs, outputs=layer_outputs, name=model.name) # invoke modifier if given if modifier: model = modifier(model) # create the full model return retinanet.retinanet(inputs=inputs, num_classes=num_classes, backbone_layers=model.outputs, **kwargs)
def get_efficientnet_model(model_version): if model_version == "B0": return efn.EfficientNetB0(weights='imagenet') elif model_version == "B1": return efn.EfficientNetB1(weights='imagenet') elif model_version == "B2": return efn.EfficientNetB2(weights='imagenet') elif model_version == "B3": return efn.EfficientNetB3(weights='imagenet') elif model_version == "B4": return efn.EfficientNetB4(weights='imagenet') elif model_version == "B5": return efn.EfficientNetB5(weights='imagenet') elif model_version == "B6": return efn.EfficientNetB6(weights='imagenet') elif model_version == "B7": return efn.EfficientNetB7(weights='imagenet') else: return efn.EfficientNetB0(weights='imagenet')
def __init__(self): # Create the base model from the pre-trained EfficientNet model_full = efn.EfficientNetB6(input_shape=(self.IMG_SIZE, self.IMG_SIZE, 3), include_top=False, weights="imagenet") model_full.trainable = False self.model = tf.keras.Model( model_full.inputs, model_full.get_layer(self.LAYER_NAME).output) self.model.trainable = False
def __init__(self, hparams): super(InputEmbedding, self).__init__() self.hparams = hparams if hparams.base_model_name == 'InceptionV3': base_model = tf.keras.applications.InceptionV3(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'InceptionResNetV2': base_model = tf.keras.applications.InceptionResNetV2( include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB0': base_model = efn.EfficientNetB0(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB1': base_model = efn.EfficientNetB1(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB2': base_model = efn.EfficientNetB2(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB3': base_model = efn.EfficientNetB3(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB4': base_model = efn.EfficientNetB4(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB5': base_model = efn.EfficientNetB5(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB6': base_model = efn.EfficientNetB6(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] elif hparams.base_model_name == 'EfficientNetB7': base_model = efn.EfficientNetB7(include_top=False, weights='imagenet') base_model_layers = [layer.name for layer in base_model.layers] assert hparams.end_point in base_model_layers, "no {} layer in {}".format( hparams.end_point, hparams.base_model_name) conv_tower_output = base_model.get_layer(hparams.end_point).output self.conv_model = tf.keras.models.Model(inputs=base_model.input, outputs=conv_tower_output) self.conv_out_shape = self.conv_model.predict( np.array([np.zeros(hparams.image_shape)])).shape self.encode_cordinate = EncodeCordinate( input_shape=self.conv_out_shape)
def get_efficientnet(self): models_dict ={ 'b0': efn.EfficientNetB0(input_shape=self.shape,weights=None,include_top=False), 'b1': efn.EfficientNetB1(input_shape=self.shape,weights=None,include_top=False), 'b2': efn.EfficientNetB2(input_shape=self.shape,weights=None,include_top=False), 'b3': efn.EfficientNetB3(input_shape=self.shape,weights=None,include_top=False), 'b4': efn.EfficientNetB4(input_shape=self.shape,weights=None,include_top=False), 'b5': efn.EfficientNetB5(input_shape=self.shape,weights=None,include_top=False), 'b6': efn.EfficientNetB6(input_shape=self.shape,weights=None,include_top=False), 'b7': efn.EfficientNetB7(input_shape=self.shape,weights=None,include_top=False) } return models_dict[self.model_class]
def create_model(model_name, input_shape=(IMG_SIZE, IMG_SIZE, 3)): if model_name == 'efn_b4': model = efn.EfficientNetB4(weights=None, classes=4) elif model_name == 'efn_b4_p': model = tf.keras.models.Sequential() model.add( efn.EfficientNetB4(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'efn_b5_p': model = tf.keras.models.Sequential() model.add( efn.EfficientNetB5(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'efn_b6_p': model = tf.keras.models.Sequential() model.add( efn.EfficientNetB6(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'efn_b7_p': model = tf.keras.models.Sequential() model.add( efn.EfficientNetB7(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'densenet121_p': model = tf.keras.models.Sequential() model.add( DenseNet121(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'densenet201_p': model = tf.keras.models.Sequential() model.add( DenseNet201(input_shape=input_shape, weights='imagenet', include_top=False)) elif model_name == 'inceptionResV2_p': model = tf.keras.models.Sequential() model.add( InceptionResNetV2(input_shape=input_shape, weights='imagenet', include_top=False)) if model_name.split('_')[-1] == 'p': model.add(GlobalAveragePooling2D()) #model.add(Dense(128, activation='relu')) #model.add(Dense(64, activation='relu')) model.add(Dense(4, activation='softmax')) model.summary() return model
def create_b6(include_top=False, input_shape=None, input_tensor=None, weights="noisy-student"): """ネットワークの作成。""" import efficientnet.tfkeras as efn return efn.EfficientNetB6( include_top=include_top, input_shape=input_shape, input_tensor=input_tensor, weights=weights, )
def create_base_model(base_model_name, pretrained=True, IMAGE_SIZE=[300, 300]): if pretrained is False: weights = None else: weights = "imagenet" if base_model_name == 'B0': base = efn.EfficientNetB0(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B1': base = efn.EfficientNetB1(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B2': base = efn.EfficientNetB2(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B3': base = efn.EfficientNetB3(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B4': base = efn.EfficientNetB4(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B5': base = efn.EfficientNetB5(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B6': base = efn.EfficientNetB6(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif base_model_name == 'B7': base = efn.EfficientNetB7(weights=weights, include_top=False, input_shape=[*IMAGE_SIZE, 3]) base = remove_dropout(base) base.trainable = True return base
def create_model(model, img_height, img_width, classes): if model == 'densenet': model = tf.keras.Sequential( [DenseNet121(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(classes, activation='softmax')] ) elif model == 'efficientnetB7': model = tf.keras.Sequential( [efn.EfficientNetB7(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(classes, activation='softmax')] ) elif model == 'efficientnetB6': model = tf.keras.Sequential( [efn.EfficientNetB6(input_shape=(img_height, img_width, 3), weights='imagenet', include_top=False), tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(classes, activation='softmax')] ) model.summary() weights = model.get_weights() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model, weights
PYTHONHASHSEED = 0 DATA_PATH = '/home/alex/Car_Classification/input/' MODEL_PATH = '/home/alex/Car_Classification/model/' sample_submission = pd.read_csv(DATA_PATH + "sample-submission.csv") # Step - увеличение размера изображения # Увеличим размер изображения и уменьшим уровень аугментации input_shape_step4 = (IMG_SIZE_STEP4, IMG_SIZE_STEP4, IMG_CHANNELS) # Пересобираем модель под новый input_shape base_model = efn.EfficientNetB6( weights=None, # Веса не подгружаем, т.к. будем загружать уже обученные include_top= False, # Выходной слой (голову) будем менять т.к. у нас другие классы и их количество input_shape=input_shape_step4) model = M.Sequential() model.add(base_model) model.add(L.GlobalAveragePooling2D(), ) model.add(L.Dense(256, activation='relu')) model.add(L.BatchNormalization()) model.add(L.Dropout(0.25)) model.add(L.Dense(CLASS_NUM, activation='softmax')) while not os.path.isfile(model_name := input('Input name of model file: ')): print(model_name, "File not exist!") model = keras.models.load_model(model_name)
test_datagen = ImageDataGenerator(rescale=1. / 255) test_generator = test_datagen.flow_from_dataframe(dataframe=df, directory=sys.argv[-1], x_col="Id", y_col=None, shuffle=False, class_mode=None, target_size=(IMG_SIZE, IMG_SIZE), batch_size=BATCH_SIZE, seed=RANDOM_SEED) #Model base_model = efn.EfficientNetB6(weights='imagenet', include_top=False, input_shape=input_shape) base_model.trainable = False model = M.Sequential() model.add(base_model) model.add(L.GlobalAveragePooling2D(), ) model.add(L.Dense(CLASS_NUM, activation='softmax')) model.compile(loss="categorical_crossentropy", optimizer=optimizers.Adam(lr=0.001), metrics=["accuracy"]) model.load_weights('best_model.hdf5') #Prediction predictions = model.predict_generator(test_generator, steps=len(test_generator),
def get_model(arch="b3", pretrained="imagenet", image_size=(128, 128, 3)): image_input = tf.keras.layers.Input(shape=image_size, dtype='float32', name='image_input') if arch.startswith("b2"): base_model = efn.EfficientNetB2(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b3"): base_model = efn.EfficientNetB3(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b4"): base_model = efn.EfficientNetB4(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b5"): base_model = efn.EfficientNetB5(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b6"): base_model = efn.EfficientNetB6(weights=pretrained, input_shape=image_size, include_top=False) elif arch.startswith("b7"): base_model = efn.EfficientNetB7(weights=pretrained, input_shape=image_size, include_top=False) else: raise ValueError("Unknown arch!") base_model.trainable = True tmp = base_model(image_input) hidden_dim = base_model.output_shape[-1] tmp = tf.keras.layers.GlobalAveragePooling2D()(tmp) tmp = tf.keras.layers.Dropout(0.5)(tmp) if arch.endswith("g"): prediction_0 = tf.keras.layers.Dense(CLASS_COUNTS[0], activation='softmax', name="root", dtype='float32')(SELayer( hidden_dim, 8)(tmp)) prediction_1 = tf.keras.layers.Dense(CLASS_COUNTS[1], activation='softmax', name="vowel", dtype='float32')(SELayer( hidden_dim, 8)(tmp)) prediction_2 = tf.keras.layers.Dense(CLASS_COUNTS[2], activation='softmax', name="consonant", dtype='float32')(SELayer( hidden_dim, 8)(tmp)) else: prediction_0 = tf.keras.layers.Dense(CLASS_COUNTS[0], activation='softmax', name="root", dtype='float32')(tmp) prediction_1 = tf.keras.layers.Dense(CLASS_COUNTS[1], activation='softmax', name="vowel", dtype='float32')(tmp) prediction_2 = tf.keras.layers.Dense(CLASS_COUNTS[2], activation='softmax', name="consonant", dtype='float32')(tmp) prediction = tf.keras.layers.Concatenate(axis=-1)( [prediction_0, prediction_1, prediction_2]) return tf.keras.Model(image_input, prediction)
target_size=(IMG_SIZE, IMG_SIZE), batch_size=BATCH_SIZE, class_mode='categorical', shuffle=True, seed=RANDOM_SEED, subset='validation') # set as validation data # # Построение модели # За основу берем сеть EfficientB6 в реализации https://github.com/qubvel/efficientnet # # base_model = efn.EfficientNetB6( weights='imagenet', # Подгружаем веса imagenet include_top= False, # Выходной слой (голову) будем менять т.к. у нас другие классы input_shape=input_shape) # Заморозим веса imagenet в базовой модели, чтобы она работала в качестве feature extractor # и наша голова обучалась делать классификацию на наши 10 классов base_model.trainable = False # Устанавливаем "голову" в минималистическо классической конфигурации model = M.Sequential() model.add(base_model) model.add(L.GlobalAveragePooling2D(), ) model.add(L.Dense(256, activation='relu')) model.add(L.BatchNormalization())
def main(args): class_names = sorted(os.listdir(r"/home/nvme/data/train/train")) N_classes = len(class_names) base_model = efn.EfficientNetB6(weights='imagenet', include_top=False, input_shape=(224, 224, 3)) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # let's add a fully-connected layer x = Dense(4096, activation='relu')(x) # and a logistic layer -- let's say we have 200 classes predictions = Dense(N_classes, activation='softmax')(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) adam = Adam(lr=0.0001) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) # example of progressively loading images from file original_dir = "/home/nvme/data/train/train" validation_split = 0.2 batch_size = 16 # all data in train_dir and val_dir which are alias to original_data. (both dir is temporary directory) # don't clear base_dir, because this directory holds on temp directory. base_dir, train_dir, val_dir = split_utils.train_valid_split( original_dir, validation_split, seed=1) # generator for train data train_datagen = ImageDataGenerator(rescale=1. / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) train_gen = train_datagen.flow_from_directory(train_dir, class_mode="categorical", target_size=(224, 224), batch_size=batch_size, shuffle=True, seed=4266) # generator for validation data val_datagen = ImageDataGenerator(rescale=1. / 255) val_gen = val_datagen.flow_from_directory(val_dir, class_mode="categorical", target_size=(224, 224), batch_size=batch_size, shuffle=True, seed=4528) epochs = args.epochs class_weights = { 0: 65, 1: 42, 2: 5, 3: 1, 4: 4, 5: 1, 6: 169, 7: 27, 8: 13, 9: 115, 10: 2, 11: 56, 12: 70, 13: 42, 14: 11, 15: 4, 16: 7 } model.fit_generator(train_gen, steps_per_epoch=train_gen.samples // batch_size, validation_data=val_gen, validation_steps=val_gen.samples // batch_size, epochs=epochs #,class_weight = class_weights ) for layer in model.layers[:658]: layer.trainable = False for layer in model.layers[658:]: layer.trainable = True # SGD(lr=0.0001, momentum=0.9) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy']) model.fit_generator(train_gen, steps_per_epoch=train_gen.samples // batch_size, validation_data=val_gen, validation_steps=val_gen.samples // batch_size, epochs=epochs) model.save('efficientnet.h5') datagen_test = ImageDataGenerator(rescale=1. / 255.) test_gen = datagen_test.flow_from_directory( '/home/nvme/data/test', #class_mode = "categorical", target_size=(224, 224), batch_size=1, shuffle=False) pred = model.predict_generator(test_gen, verbose=1) p = np.argmax(pred, axis=1) predictions = [class_names[k] for k in p] a = np.arange(len(predictions)) d = {'Id': a, 'Category': predictions} df = pd.DataFrame(d) file_name = args.file df.to_csv(file_name, index=None, header=True)
def build(name, width, height, depth, n_classes, reg=0.8): """ Args: name: name of the network width: width of the images height: height of the images depth: number of channels of the images reg: regularization value """ # If Keras backend is TensorFlow inputShape = (height, width, depth) chanDim = -1 # If Keras backend is Theano if K.image_data_format() == "channels_first": inputShape = (depth, height, width) chanDim = 1 # Define the base model architecture if name == 'EfficientNetB0': base_model = efn.EfficientNetB0(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'EfficientNetB1': base_model = efn.EfficientNetB1(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'EfficientNetB2': base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'EfficientNetB3': base_model = efn.EfficientNetB3(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'EfficientNetB4': base_model = efn.EfficientNetB4(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'EfficientNetB5': base_model = efn.EfficientNetB5(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'EfficientNetB6': base_model = efn.EfficientNetB6(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'ResNet50': base_model = ResNet50(weights='imagenet', include_top=False, input_shape=inputShape) elif name == 'DenseNet121': base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=inputShape) #x1 = GlobalMaxPooling2D()(base_model.output) # Compute the max pooling of the base model output #x2 = GlobalAveragePooling2D()(base_model.output) # Compute the average pooling of the base model output #x3 = Flatten()(base_model.output) # Flatten the base model output #x = Concatenate(axis=-1)([x1, x2, x3]) x = GlobalAveragePooling2D()(base_model.output) x = Dropout(0.5)(x) """ # First Dense => Relu => BN => DO fc_layer_1 = Dense(512, kernel_regularizer=l2(reg))(x) activation_1 = Activation('relu')(fc_layer_1) batch_norm_1 = BatchNormalization(axis=-1)(activation_1) dropout_1 = Dropout(0.5)(batch_norm_1) # First Dense => Relu => BN => DO fc_layer_2 = Dense(256, kernel_regularizer=l2(reg))(dropout_1) activation_2 = Activation('relu')(fc_layer_2) batch_norm_2 = BatchNormalization(axis=-1)(activation_2) dropout_2 = Dropout(0.5)(batch_norm_2) # Add the output layer output = Dense(n_classes, kernel_regularizer=l2(reg), activation='softmax')(dropout_2) """ output = Dense(n_classes, kernel_regularizer=l2(reg), activation='softmax')(x) # Create the model model = Model(inputs=base_model.inputs, outputs=output) return model
def get_model(config): # model = globals().get(config.MODEL.NAME)(1) print('model name:', config.MODEL.NAME) model_name = config.MODEL.NAME input_shape = (config.DATA.IMG_H, config.DATA.IMG_W, 3) pretrained_weight = config.MODEL.WEIGHT if pretrained_weight == 'None': pretrained_weight = None if 'EfficientNet' in model_name: ##keras.application if 'B7' in model_name: encoder = efn.EfficientNetB7(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B0' in model_name: encoder = efn.EfficientNetB0(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B1' in model_name: encoder = efn.EfficientNetB1(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B2' in model_name: encoder = efn.EfficientNetB2(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B3' in model_name: encoder = efn.EfficientNetB3(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B4' in model_name: encoder = efn.EfficientNetB4(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B5' in model_name: encoder = efn.EfficientNetB5(input_shape=input_shape, weights=pretrained_weight, include_top=False) elif 'B6' in model_name: encoder = efn.EfficientNetB6(input_shape=input_shape, weights=pretrained_weight, include_top=False) model = tf.keras.Sequential([ encoder, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(len(CLASSES), activation='softmax') ]) else: ##https://github.com/qubvel/classification_models #['resnet18', 'resnet34', 'resnet50', 'resnet101', 'resnet152', 'seresnet18', 'seresnet34', 'seresnet50', # 'seresnet101', 'seresnet152', 'seresnext50', 'seresnext101', 'senet154', 'resnet50v2', 'resnet101v2', # 'resnet152v2', 'resnext50', 'resnext101', 'vgg16', 'vgg19', # 'densenet121', 'densenet169', 'densenet201', # 'inceptionresnetv2', 'inceptionv3', 'xception', 'nasnetlarge', 'nasnetmobile', 'mobilenet', 'mobilenetv2'] base_model, preprocess_input = Classifiers.get(model_name) base_model = base_model(input_shape=input_shape, weights=pretrained_weight, include_top=False) x = tf.keras.layers.GlobalAveragePooling2D()(base_model.output) output = tf.keras.layers.Dense( len(CLASSES), activation=config.MODEL.OUTPUT_ACTIVATION)(x) # if 'focal' in config.LOSS.NAME: # if 'categorical_focal_loss' == config.LOSS.NAME: # else: # output = tf.keras.layers.Dense(len(CLASSES), activation='sigmoid')(x) # else: # output = tf.keras.layers.Dense(len(CLASSES), activation='softmax')(x) model = tf.keras.models.Model(inputs=[base_model.input], outputs=[output]) return model