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 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 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
# Clip the prediction value y_pred_ls = K.clip(y_pred_ls, epsilon, 1.0 - epsilon) # Calculate cross entropy cross_entropy = -y_true * K.log(y_pred_ls) # Calculate weight that consists of modulating factor and weighting factor weight = alpha * y_true * K.pow((1 - y_pred_ls), gamma) # Calculate focal loss loss = weight * cross_entropy # Sum the losses in mini_batch loss = K.sum(loss, axis=1) return loss return focal_loss base_model = efn.EfficientNetB6(weights='imagenet', input_shape=(512, 512, 3),include_top=False) base_model.trainable = True model = tf.keras.Sequential([ tf.keras.layers.Input((512, 512, 3)), tf.keras.layers.BatchNormalization(renorm=True), base_model, BatchNormalization(), tf.keras.layers.LeakyReLU(), tf.keras.layers.Flatten(), tf.keras.layers.Dense(256), BatchNormalization(), tf.keras.layers.LeakyReLU(),
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
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))
"": int, "DF": int, "VASC": int, "SCC": int, "UNK": int }) validation_data['image'] = validation_data['image'] + '.jpg' val_labels = np.argmax(np.array(validation_data.iloc[:, 1:10]), axis=1) val_images = np.asarray(validation_data.iloc[:, 0]) val_classes = list(validation_data.columns.values[1:10]) print(val_classes) print(val_images) print(val_labels) base_model = efn.EfficientNetB6(input_shape=(256, 256, 3), weights='imagenet', include_top=False, pooling='avg') x = Dropout(0.3)(base_model.output) # adding Droupout layer to the model. prediction_efn = Dense(9, activation='softmax')(x) model = Model(base_model.input, prediction_efn) #compiling the CNN model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) callbacks_save = ModelCheckpoint('best isic.h5', monitor='val_loss', mode='min', save_best_only=True) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.2,
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) if b_name == "7": model = efn.EfficientNetB7(weights=weights) image_size = model.input_shape[1] def read_image(path): try: return preprocess_input( center_crop_and_resize(imread(path)[:, :, :3], image_size=image_size)) except: return None