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 create_model(model_name): 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=(380, 380, 3), weights='imagenet', include_top=False)) elif model_name == 'efn_b5_p': model = tf.keras.models.Sequential() model.add(efn.EfficientNetB5(input_shape=(456, 456, 3), weights='imagenet', include_top=False)) elif model_name == 'resnet18': model = ResNet([2, 2, 2, 2], input_shape=(224, 224, 3)) elif model_name == 'densenet121_p': model = tf.keras.models.Sequential() model.add(DenseNet121(input_shape=(224, 224, 3), weights='imagenet', include_top=False)) elif model_name == 'densenet201_p': model = tf.keras.models.Sequential() model.add(DenseNet201(input_shape=(224, 224, 3), 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_cnn_model(): model = keras.models.Sequential() pre_trained_model = efn.EfficientNetB5(input_shape=(*IMG_SIZE, 3), include_top=False, weights='noisy-student') # freeze the batch normalisation layers for layer in reversed(pre_trained_model.layers): if isinstance(layer, tf.keras.layers.BatchNormalization): layer.trainable = False else: layer.trainable = True model.add(pre_trained_model) model.add(layers.Dropout(0.25)) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dropout(0.25)) model.add(layers.Dense(5, activation='softmax')) # add metrics metrics = [ tf.keras.metrics.CategoricalAccuracy(name='accuracy'), ] optimizer = tf.keras.optimizers.Adam() loss = tf.keras.losses.CategoricalCrossentropy() model.compile(optimizer=optimizer, loss=loss, metrics=metrics) print(model.summary()) return model
def get_model(nb_classes=4): base_model = efn.EfficientNetB5(weights='imagenet', include_top=False, pooling='avg', input_shape=(IMAGE_SIZE, IMAGE_SIZE, 3)) x = base_model.output predictions = Dense(nb_classes, activation="softmax")(x) return Model(inputs=base_model.input, outputs=predictions)
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, 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_b5(include_top=False, input_shape=None, input_tensor=None, weights="noisy-student"): """ネットワークの作成。""" import efficientnet.tfkeras as efn return efn.EfficientNetB5( include_top=include_top, input_shape=input_shape, input_tensor=input_tensor, weights=weights, )
def build_model2(): effnet = efn.EfficientNetB5(weights=None, include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, 3)) effnet.load_weights('/home/td/桌面/efficientnet-b5_imagenet_1000_notop.h5') for i, layer in enumerate(effnet.layers): if "batch_normalization" in layer.name: effnet.layers[i] = layers.BatchNormalization(groups=32, axis=-1, epsilon=0.00001) model = Sequential() model.add(effnet) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dropout(0.5)) model.add(layers.Dense(5, activation="elu")) model.add(layers.Dense(1, activation="linear")) model.compile(loss='mse', optimizer=Adam(lr=0.0005), metrics=['acc']) return model
def _create_efficientNet_3deconv(self, inp_shape, input_tensor, output_len): eff_net = efn.EfficientNetB5(include_top=True, weights=None, input_tensor=input_tensor, input_shape=[224, 224, 3], pooling=None, classes=output_len) eff_net.layers.pop() inp = eff_net.input x = eff_net.get_layer('top_bn').output x = Deconvolution2D(filters=256, kernel_size=(4, 4), strides=(2, 2), padding='same', activation='relu', kernel_initializer='he_uniform')(x) # 14, 14, 256 x = BatchNormalization()(x) x = Deconvolution2D(filters=256, kernel_size=(4, 4), strides=(2, 2), padding='same', activation='relu', kernel_initializer='he_uniform')(x) # 28, 28, 256 x = BatchNormalization()(x) x = Deconvolution2D(filters=256, kernel_size=(4, 4), strides=(2, 2), padding='same', activation='relu', kernel_initializer='he_uniform')(x) # 56, 56, 256 x = BatchNormalization()(x) out_heatmap = Conv2D(output_len // 2, kernel_size=1, padding='same')(x) eff_net = Model(inp, out_heatmap) eff_net.summary() return eff_net
def model_chooser(self, classes=2, weights=None): print("Model selection started.") name = self.model_name if(name=='C0'): self.model = efn.EfficientNetB0(include_top=True, weights=weights, classes=classes) elif(name=='C1'): self.model = efn.EfficientNetB1(include_top=True, weights=weights, classes=classes) elif(name=='C2'): self.model = efn.EfficientNetB2(include_top=True, weights=weights, classes=classes) elif(name=='C3'): self.model = efn.EfficientNetB3(include_top=True, weights=weights, classes=classes) elif(name=='C4'): self.model = efn.EfficientNetB4(include_top=True, weights=weights, classes=classes) elif(name=='C5'): self.model = efn.EfficientNetB5(include_top=True, weights=weights, classes=classes) if(classes==2): self.model.compile(optimizer="adam", loss="binary_crossentropy", metrics = ['acc']) elif(classes>2): self.model.compile(optimizer="adam", loss="categorical_crossentropy", metrics = ['acc'])
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
if BACKBONE.lower() == "VGG16".lower(): base_model = VGG16(input_shape=(img_height, img_width, 3), include_top=False, weights=None) elif BACKBONE.lower() == "ResNet50V2".lower(): base_model = ResNet50V2(input_shape=(img_height, img_width, 3), include_top=False, weights=None) elif BACKBONE.lower() == "InceptionResNetV2".lower(): base_model = InceptionResNetV2(input_shape=(img_height, img_width, 3), include_top=False, weights=None) elif BACKBONE.lower() == "EfficientNet".lower(): base_model = efn.EfficientNetB5( input_shape=(img_height, img_width, 3), weights=None, include_top=False, ) elif BACKBONE.lower() == "MobileNetV2".lower(): base_model = MobileNetV2(input_shape=(img_height, img_width, 3), include_top=False, weights=None) else: raise NotImplementedError("Unknown backbone \'{}\' ".format()) base_model.trainable = True # define top layers x = base_model.output x = Flatten()(x) x = Dropout(DROPOUT)(x) # this dropout here also seems to help x = Dense(2048)(x)
import time import tensorflow as tf from tensorflow.keras import layers as L import sys import cv2 Threshold=0.285 #学習時における検証用予測結果の2σ+中央値を閾値とする #model building IMAGE_SIZE = [384,384] #model作成(重みと同じモデル) model = tf.keras.Sequential([ efn.EfficientNetB5( input_shape=(*IMAGE_SIZE, 3), weights='imagenet', include_top=False ), L.GlobalAveragePooling2D(), L.Dense(1024, activation = 'relu'), L.Dropout(0.3), L.Dense(512, activation= 'relu'), L.Dropout(0.2), L.Dense(256, activation='relu'), L.Dropout(0.2), L.Dense(128, activation='relu'), L.Dropout(0.1), L.Dense(1, activation='sigmoid') ]) if __name__ == '__main__':
def load_b5_prc(): cnn_net = efn.EfficientNetB5(weights='imagenet',include_top=False,input_shape=(380, 380, 3)) model = build_model(cnn_net) model.load_weights('models/effb5_old_new_preprocess.h5') return model
def load_b5(): cnn_net = efn.EfficientNetB5(weights='imagenet',include_top=False,input_shape=(456, 456, 3)) model = build_model(cnn_net) model.load_weights('models/effnet_b5.h5') return model
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)
def get_model(input_height, input_width, n_ch): """Generates the crack segmentation model as specified in the paper: 'Optimized Deep Encoder-Decoder Methods for Crack Segmentation'. This model uses an EfficientNet B5 backbone. Args: input_height (int): input height of the model input_width (int): input width of the model n_ch (int): number of channels of the input Returns: tensorflow.keras.Model: Crack segmentation model (already compiled) """ filters = (256, 128, 64, 32, 16) backbone = efn.EfficientNetB5( weights="imagenet", input_shape=(input_height, input_width, 3), include_top=False, ) # some adaptions from https://github.com/qubvel/segmentation_models/blob/master/segmentation_models/models/unet.py x = backbone.output skip_connection_layers = ( "block6a_expand_activation", "block4a_expand_activation", "block3a_expand_activation", "block2a_expand_activation", ) # extract skip connections skips = [backbone.get_layer(name=i).output for i in skip_connection_layers] # add a conv after the input to extract some information from the original scale inp = backbone.layers[0].output inp = conv_stack(16, name="conv_inp")(inp) dsvs = [] for i in range(len(filters)): name = f"decoderBlock{i}" if i < len(skips): skip = skips[i] else: skip = inp x = upsample_skip(filters[i], name=name)(x, skip) if i < len(skips): x = ConvBnAct( filters[i], (3, 3), activation="relu", use_bn=True, name=f"{name}preconv", )(x) x = residual_conv_stack(filters[i], name=name)(x) dsvs.append(x) else: x = conv_stack(filters[i], name="inp1" + name)(x) x = Conv2D( filters=1, kernel_size=(3, 3), padding="same", use_bias=True, kernel_initializer=k_init, kernel_regularizer=regularizers.l2(alpha), name="output_conv", )(x) x = Activation("sigmoid", name="main_out")(x) interim_model = Model(inputs=backbone.input, outputs=[ x, ] + dsvs) # add weight decay for layer in interim_model.layers: for attr in ["kernel_regularizer"]: if hasattr(layer, attr): setattr(layer, attr, keras.regularizers.l2(alpha)) inputs = Input(shape=(input_height, input_width, n_ch)) if n_ch == 1: inp = Lambda(lambda x: K.tile(x, (1, 1, 1, 3)))(inputs) else: inp = inputs interim_outputs = interim_model(inp) outputs = [] upscale_factors = [16, 8, 4, 2] for ix, o in enumerate(interim_outputs): if ix == 0: outputs.append(Lambda(lambda x: x, name="main_out")(o)) else: o = deep_supervision_block( o, 1, name="dsv" + str(ix + 1), out_shape=(input_height, input_width), last_layer_name="aux" + str(ix + 1), upscale_factor=upscale_factors[ix - 1], ) outputs.append(o) sgd = SGD(lr=0.001, momentum=0.9, nesterov=False) model = Model(inputs=inputs, outputs=outputs) model.compile(optimizer=sgd, loss=ce_dice_loss, metrics=["accuracy"]) return model
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