def get_b1_backbone(): backbone = efn.EfficientNetB1(input_shape=(128, 128, 3), include_top=False, weights='imagenet') backbone_output = concatenate([ GlobalAveragePooling2D()(backbone.output), GlobalMaxPooling2D()(backbone.output), ]) return backbone, backbone_output
def effcient1_unet(input_shape, filters=512, out_dim=4, act="sigmoid"): model = efn.EfficientNetB1(include_top=False, input_shape=input_shape, weights=None) blockid = [ 'block2a_expand_activation', 'block3a_expand_activation', 'block4a_expand_activation', 'block6a_expand_activation', 'block7b_add' ] block_outputs = [] for i in range(len(blockid)): blockout = model.get_layer(blockid[i]).output block_outputs.append(blockout) input = model.input inputc = Conv2D(16, (3, 3), padding="same", activation="relu")(input) x = block_outputs.pop() for i in range(len(blockid) - 1): x = transconv_block(x, int(filters / (2**i)), 3, 2, skip=block_outputs.pop()) x = transconv_block(x, 16, 3, 2, skip=inputc) output_layer = Conv2D(out_dim, (1, 1), padding="same", activation=act)(x) model = Model(input, output_layer) return model
def get_model(model='b2', shape=(320,320)): K.clear_session() h,w = shape if model == 'b0': base_model = efn.EfficientNetB0(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) elif model == 'b1': base_model = efn.EfficientNetB1(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) elif model == 'b2': base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) elif model == 'b3': base_model = efn.EfficientNetB3(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) elif model == 'b4': base_model = efn.EfficientNetB4(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) elif model == 'b5': base_model = efn.EfficientNetB5(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) elif model == 'b6': base_model = efn.EfficientNetB6(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) else: base_model = efn.EfficientNetB7(weights='imagenet', include_top=False, pooling='avg', input_shape=(h, w, 3)) x = base_model.output y_pred = Dense(4, activation='sigmoid')(x) return Model(inputs=base_model.input, outputs=y_pred)
def get_efficientnet_model( model_name='efficientnetb0', input_shape=(224, 224, 3), input_tensor=None, include_top=True, classes=1000, weights='imagenet', ): layer_names = [ 'block3a_expand_activation', #C2 'block4a_expand_activation', #C3 'block6a_expand_activation', #C4 'top_activation' #C5 ] Args = { 'input_shape': input_shape, 'weights': weights, 'include_top': include_top, 'input_tensor': input_tensor } if model_name == 'efficientnetb0': backbone = efn.EfficientNetB0(**Args) elif model_name == 'efficientnetb1': backbone = efn.EfficientNetB1(**Args) elif model_name == 'efficientnetb2': backbone = efn.EfficientNetB2(**Args) elif model_name == 'efficientnetb3': backbone = efn.EfficientNetB3(**Args) elif model_name == 'efficientnetb4': backbone = efn.EfficientNetB4(**Args) elif model_name == 'efficientnetb5': backbone = efn.EfficientNetB5(**Args) elif model_name == 'efficientnetb6': backbone = efn.EfficientNetB6(**Args) elif model_name == 'efficientnetb7': backbone = efn.EfficientNetB7(**Args) else: raise ValueError('No such model {}'.format(model_name)) several_layers = [] several_layers.append(backbone.get_layer(layer_names[0]).output) several_layers.append(backbone.get_layer(layer_names[1]).output) several_layers.append(backbone.get_layer(layer_names[2]).output) several_layers.append(backbone.get_layer(layer_names[3]).output) model = keras.models.Model(inputs=[backbone.input], outputs=several_layers) return model
def _setup_base_model(self): base_model = efn.EfficientNetB1(weights='imagenet', include_top=False) # fix the feature extraction part of the model for layer in base_model.layers: if isinstance(layer, BatchNormalization): layer.trainable = True else: layer.trainable = False return base_model
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.models.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_model(): base_model = efn.EfficientNetB1(weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) output_values = [Dense(N_TOTAL_BARS, activation='softmax')(x)] for _ in range(N_TOTAL_BARS): output_values.append(Dense(1, activation=lambda z: K.clip(z, 0, 1))(x)) model = Model(inputs=base_model.input, outputs=output_values) return model
def construct_mlp(input_size, num_classes, num_frames, dropout_size=0.5, ef_mode=4, l2_reg=1e-5): """ Construct a MLP model for urban sound tagging. Parameters ---------- num_frames input_size num_classes dropout_size ef_mode l2_reg Returns ------- model """ # Add hidden layers from keras.layers import Flatten, Conv1D, Conv2D, GlobalMaxPooling1D, GlobalAveragePooling1D, LSTM, Concatenate, GlobalAveragePooling2D, LeakyReLU import efficientnet.keras as efn if ef_mode == 0: base_model = efn.EfficientNetB0(weights='noisy-student', include_top=False, pooling='avg') elif ef_mode == 1: base_model = efn.EfficientNetB1(weights='noisy-student', include_top=False, pooling='avg') elif ef_mode == 2: base_model = efn.EfficientNetB2(weights='noisy-student', include_top=False, pooling='avg') elif ef_mode == 3: base_model = efn.EfficientNetB3(weights='noisy-student', include_top=False, pooling='avg') elif ef_mode == 4: base_model = efn.EfficientNetB4(weights='noisy-student', include_top=False, pooling='avg') #imagenet or weights='noisy-student' elif ef_mode == 5: base_model = efn.EfficientNetB5(weights='noisy-student', include_top=False, pooling='avg') elif ef_mode == 6: base_model = efn.EfficientNetB6(weights='noisy-student', include_top=False, pooling='avg') elif ef_mode == 7: base_model = efn.EfficientNetB7(weights='noisy-student', include_top=False, pooling='avg') input1 = Input(shape=input_size, dtype='float32', name='input') input2 = Input(shape=(num_frames,85), dtype='float32', name='input2') #1621 y = TimeDistributed(base_model)(input1) y = TimeDistributed(Dropout(dropout_size))(y) y = Concatenate()([y, input2]) y = TimeDistributed(Dense(num_classes, activation='sigmoid', kernel_regularizer=regularizers.l2(l2_reg)))(y) y = AutoPool1D(axis=1, name='output')(y) m = Model(inputs=[input1, input2], outputs=y) m.summary() m.name = 'urban_sound_classifier' return m
def get_model_effnet(img_shape, img_input, weights, effnet_version): if effnet_version == 'B0': effnet = efn.EfficientNetB0(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) elif effnet_version == 'B1': effnet = efn.EfficientNetB1(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) elif effnet_version == 'B2': effnet = efn.EfficientNetB2(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) elif effnet_version == 'B3': effnet = efn.EfficientNetB3(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) elif effnet_version == 'B4': effnet = efn.EfficientNetB4(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) elif effnet_version == 'B5': effnet = efn.EfficientNetB5(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) elif effnet_version == 'B6': effnet = efn.EfficientNetB6(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) else: effnet = efn.EfficientNetB7(include_top=False, input_tensor=img_input, weights=weights, pooling=None, input_shape=img_shape) return effnet
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 build_model(input_shape, args): D = args.d F = args.f V = args.v input_tensor = Input(shape=input_shape) if args.tf == "in": base_model = InceptionV3(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = in_pi elif args.tf == "inr": base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = inr_pi elif args.tf == "vg": base_model = VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = vg_pi elif args.tf == "xc": base_model = Xception(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = xc_pi elif args.tf == "re": base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = re_pi elif args.tf == "de": base_model = DenseNet121(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = de_pi elif args.tf == "mo": base_model = MobileNet(weights='imagenet', include_top=False, input_tensor=input_tensor) #pi = mo_pi elif args.tf.find("ef") > -1: if args.tf == "ef0": base_model = efn.EfficientNetB0(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef1": base_model = efn.EfficientNetB1(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef2": base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef3": base_model = efn.EfficientNetB3(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef4": base_model = efn.EfficientNetB4(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef5": base_model = efn.EfficientNetB5(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef6": base_model = efn.EfficientNetB6(weights='imagenet', include_top=False, input_tensor=input_tensor) elif args.tf == "ef7": base_model = efn.EfficientNetB7(weights='imagenet', include_top=False, input_tensor=input_tensor) else: print("unknown network type:", args.tf) exit() x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(F, activation='relu')(x) if D > 0: x = Dropout(D)(x) pred = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=pred) layer_num = len(base_model.layers) for layer in base_model.layers[:int(layer_num * V)]: layer.trainable = False return model #, pi
msg = "Accuracy on test set: {0:.1%} ({1} / {2})" print(msg.format(acc, correct_sum, num_test)) if show_confusion_matrix: print("Confusion Matrix:") plot_test_confusion_matrix(cls_pred=cls_pred, labels_test=labels_test, classes=classes) return float(acc) #%% model = efn.EfficientNetB1(include_top=False, input_shape=(128, 128, 1), weights=None) #weights=None #weights='imagenet' x = model.output x = GlobalAveragePooling2D()(x) x = Dropout(0.7)(x) predictions = Dense(3, activation="softmax")(x) model_final = Model(inputs=model.input, outputs=predictions) model_final.summary()
def get_backbone(name): """ Chooses a backbone/ base network. Args: name: the name of the base network. Returns: backbone: the Keras model of the chosen network. """ if name == 'EfficientNetB0': backbone = efn.EfficientNetB0(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB1': backbone = efn.EfficientNetB1(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB2': backbone = efn.EfficientNetB2(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB3': backbone = efn.EfficientNetB3(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB4': backbone = efn.EfficientNetB4(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB5': backbone = efn.EfficientNetB5(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB6': backbone = efn.EfficientNetB6(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'EfficientNetB7': backbone = efn.EfficientNetB7(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'VGG16': backbone = VGG16(weights=c.WEIGHTS, include_top=c.INCLUDE_TOP, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'ResNet50': backbone = ResNet50(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'InceptionV3': backbone = InceptionV3(include_top=c.INCLUDE_TOP, weights=c.WEIGHTS, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) elif name == 'DenseNet201': backbone = DenseNet201(weights=c.WEIGHTS, include_top=c.INCLUDE_TOP, input_shape=c.INPUT_SHAPE, pooling=c.POOLING) else: backbone = None try: backbone.trainable = True return backbone except Exception as e: print(str(e))
from efficientnet.keras import center_crop_and_resize, preprocess_input from keras.applications.imagenet_utils import decode_predictions #import multiprocessing weights = os.getenv("WEIGHTS", "noisy-student") b_name = os.getenv("B", "2") model = None if b_name == "0": model = efn.EfficientNetB0(weights=weights) if b_name == "1": model = efn.EfficientNetB1(weights=weights) if b_name == "2": model = efn.EfficientNetB2(weights=weights) if b_name == "3": model = efn.EfficientNetB3(weights=weights) if b_name == "4": model = efn.EfficientNetB4(weights=weights) if b_name == "5": model = efn.EfficientNetB5(weights=weights) if b_name == "6": model = efn.EfficientNetB6(weights=weights)