def buildBaseModel(self, img_size): """ This function builds a EfficientNet-B2 model which includes a global average pooling layer and a dense layer with sigmoid activation function. Parameters: img_size (int): the size of input images (img_size, img_size). Returns: model (class): the base Efficient-B2 model that can be used later in training. """ base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils, input_shape=(img_size, img_size, 3)) x = base_model.output x = layers.GlobalAveragePooling2D()(x) predictions = layers.Dense(1, activation='sigmoid', name='last')(x) model = Model(inputs=base_model.input, outputs=predictions) model.load_weights(self.weights) return model
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 buildNihModel(self, img_size, label_len): """ This function builds a base DenseNet-121 model for pretraining with the NIH dataset. Parameters: img_size (int): the size of input images (img_size, img_size). label_len (int): the length of the labels from the NIH dataset. Returns: model (class): the EfficientNet-B2 model used in pretraining. """ base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3)) x = base_model.output x = layers.GlobalAveragePooling2D()(x) predictions = layers.Dense(label_len, activation='sigmoid', name='last')(x) model = Model(inputs=base_model.input, outputs=predictions) if not self.weights == 'imagenet': model.load_weights(self.weights) return model
def create_model_b2(): efficient_net = efn.EfficientNetB2(weights='imagenet', include_top=False, input_shape=(100, 100, 3), pooling='max') return tf.keras.Sequential( [ efficient_net, tf.keras.layers.Dense(2000, activation='relu'), tf.keras.layers.Dense(4000, activation='sigmoid') ])
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 efficientnetb2(self, input_shape, **kwagrs): # x_input = tf.keras.Input(shape=input_shape, name="triplet") m = efn.EfficientNetB2(input_shape=(256, 256, 3), weights=None) # remove the output layer, leave the feature extraction part m_fe = tf.keras.Model(inputs=m.inputs, outputs=m.layers[-2].output) output = tf.keras.layers.Dense(1, activation="sigmoid")(m_fe.output) m = tf.keras.Model(inputs=m_fe.inputs, outputs=output) return m
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 effnet_model_b2(input_shape, classes): base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, input_shape=input_shape) x = base_model.output x1 = keras.layers.GlobalAveragePooling2D()(x) x2 = keras.layers.GlobalMaxPooling2D()(x) x = keras.layers.concatenate([x1, x2]) x = keras.layers.Dropout(0.2)(x) predictions = keras.layers.Dense(classes, activation="softmax")(x) return keras.models.Model(inputs=base_model.input, outputs=predictions)
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_b2(include_top=False, input_shape=None, input_tensor=None, weights="noisy-student"): """ネットワークの作成。""" import efficientnet.tfkeras as efn return efn.EfficientNetB2( include_top=include_top, input_shape=input_shape, input_tensor=input_tensor, weights=weights, )
def create_model(input_shape, output_shape) -> tf.keras.Model: model = efn.EfficientNetB2(input_shape=input_shape, weights='noisy-student', include_top=False) model = tf.keras.Sequential([ Dataset.normalize_layer(input_shape), tf.keras.layers.experimental.preprocessing.RandomFlip(), tf.keras.layers.experimental.preprocessing.RandomTranslation(0.2, 0.2), tf.keras.layers.experimental.preprocessing.RandomRotation(0.3), tf.keras.layers.experimental.preprocessing.RandomContrast(0.35), model, tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(output_shape) ]) return model
def getEffTFModel(self, n=0): modelInput = tf.keras.Input(batch_input_shape=(None, 5, self.config['net_size'], self.config['net_size'], 3)) modelInput0, modelInput1, modelInput2, modelInput3, modelInput4 = tf.split(modelInput, [1, 1, 1, 1, 1], 1) x0 = tf.squeeze(tf.keras.layers.Lambda(lambda x0: x0)(modelInput0)) x1 = tf.squeeze(tf.keras.layers.Lambda(lambda x1: x1)(modelInput1)) x2 = tf.squeeze(tf.keras.layers.Lambda(lambda x2: x2)(modelInput2)) x3 = tf.squeeze(tf.keras.layers.Lambda(lambda x3: x3)(modelInput3)) x4 = tf.squeeze(tf.keras.layers.Lambda(lambda x4: x4)(modelInput4)) net = '' if n % 10 == 0: net = efn.EfficientNetB0(include_top=False, weights='imagenet', input_shape=(self.config['net_size'], self.config['net_size'], 3), pooling='avg') elif n % 10 == 1: net = efn.EfficientNetB1(include_top=False, weights='imagenet', input_shape=(self.config['net_size'], self.config['net_size'], 3), pooling='avg') elif n % 10 == 2: net = efn.EfficientNetB2(include_top=False, weights='imagenet', input_shape=(self.config['net_size'], self.config['net_size'], 3), pooling='avg') elif n % 10 == 3: net = efn.EfficientNetB3(include_top=False, weights='imagenet', input_shape=(self.config['net_size'], self.config['net_size'], 3), pooling='avg') elif n % 10 == 4: net = efn.EfficientNetB4(include_top=False, weights='imagenet', input_shape=(self.config['net_size'], self.config['net_size'], 3), pooling='avg') activation = tf.keras.layers.LeakyReLU() self.config['rnn_size'] = 256 ret0 = net(x0) ret0 = Dense(self.config['rnn_size'], activation=activation)(ret0) ret0 = tf.expand_dims(ret0, axis=1) ret0 = self.transformer(ret0) ret1 = net(x1) ret1 = Dense(self.config['rnn_size'], activation=activation)(ret1) ret1 = tf.expand_dims(ret1, axis=1) ret1 = self.transformer(ret1) ret2 = net(x2) ret2 = Dense(self.config['rnn_size'], activation=activation)(ret2) ret2 = tf.expand_dims(ret2, axis=1) ret2 = self.transformer(ret2) ret3 = net(x3) ret3 = Dense(self.config['rnn_size'], activation=activation)(ret3) ret3 = tf.expand_dims(ret3, axis=1) ret3 = self.transformer(ret3) ret4 = net(x4) ret4 = Dense(self.config['rnn_size'], activation=activation)(ret4) ret4 = tf.expand_dims(ret4, axis=1) ret4 = self.transformer(ret4) ret = tf.concat([ret0, ret1, ret2, ret3, ret4], axis=1) print(ret) x = tf.keras.layers.Dense(self.config['rnn_size'], activation=activation)(ret) model = tf.keras.Model(modelInput, x) return model
def create_model_efficientnet(self): # Import Efficientnet import efficientnet.tfkeras as efn input_tensor = Input(shape=self.input_shape) base_model = efn.EfficientNetB2(include_top=False, input_tensor=input_tensor) x = base_model(input_tensor) x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) out = Dense(self.output_shape, activation="sigmoid")(x) model = Model(input_tensor, out) model.compile(optimizer=Adam(self.learning_rate), loss=binary_crossentropy, metrics=['acc']) return model
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 buildTunerModel(self, img_size): """ This function builds a base EfficientNet-B2 model for keras tuner Parameters: img_size (int): the size of input images (img_size, img_size). Returns: model (class): the EfficientNet-B2 model used for keras tuner. """ base_model = efn.EfficientNetB2(weights='imagenet', include_top=False, backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils, input_shape=(img_size, img_size, 3)) base_model.load_weights(self.weights, by_name=True) return base_model
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 buildDropModel(self, img_size, dropout): """ This function builds a EfficientNet-B2 model with dropout layer. Parameters: img_size (int): the size of input images (img_size, img_size). dropout (float): the drop out rate for the dropout layer. Must be less than 1. Returns: model (class): the EfficientNet-B2 model with dropout layer. """ base_model = efn.EfficientNetB2(weights=None, include_top=False, input_shape=(img_size, img_size, 3)) x = base_model.output x = layers.GlobalAveragePooling2D()(x) x = layers.Dropout(dropout)(x) predictions = layers.Dense(1, activation='sigmoid', name='last')(x) model = Model(inputs=base_model.input, outputs=predictions) model.load_weights(self.weights) return model
def efficient_net_b3(input_shape) -> Model: inputs = Input(shape=input_shape) base_model = efn.EfficientNetB2(weights=None, include_top=False, input_tensor=inputs, pooling=None, classes=None) for layer in base_model.layers: layer.trainable = True # a = tail_block(base_model.output, "root") # b = tail_block(base_model.output, "vowel") # c = tail_block(base_model.output, "consonant") x = GeneralizedMeanPool2D('gem')(base_model.output) a = Dense(512)(x) b = Dense(512)(x) c = Dense(512)(x) head_root = Dense(168, activation='softmax', name='root')(a) head_vowel = Dense(11, activation='softmax', name='vowel')(b) head_consonant = Dense(7, activation='softmax', name='consonant')(c) return Model(inputs=inputs, outputs=[head_root, head_vowel, head_consonant])
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)
class_mode='categorical', batch_size=1024, classes=labels, target_size=(image_size, image_size))) # In[18]: # # Change the model to the one that you're training import efficientnet.tfkeras as efn import tensorflow.keras as keras base_model = efn.EfficientNetB2(include_top=False, weights='imagenet', input_shape=(224, 224, 3), backend=keras.backend, layers=keras.layers, models=keras.models, utils=keras.utils) x = keras.layers.GlobalAveragePooling2D(name='avg_pool')(base_model.output) x = keras.layers.Dropout(0.125)(x) predictions = keras.layers.Dense(len(labels), activation="sigmoid", name='last')(x) model = keras.models.Model(inputs=base_model.input, outputs=predictions) # model.summary() print('......finish loading model') # In[ ]: from tensorflow.keras import optimizers
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
print("Started worker rank=%d local_rank=%d size=%d" % (hvd.rank(), hvd.local_rank(), hvd.size())) config = tf.ConfigProto() config.gpu_options.allow_growth = True config.gpu_options.visible_device_list = str(hvd.local_rank()) K.set_session(tf.Session(config=config)) #device_id = hvd.local_rank() device_id = 0 batch_size = 16 * hvd.size() backbone = efn.EfficientNetB2( input_shape=(224, 224, 3), weights='imagenet', include_top=False, ) layers = [ backbone, L.Conv2D(256, 3), L.BatchNormalization(), L.Activation('relu'), L.Conv2D(256, 3), L.BatchNormalization(), L.Activation('relu'), L.Conv2D(256, 3), L.BatchNormalization(), L.Activation('relu'), L.Conv2D(1, 1),
def __create_model(self): inputs = tf.keras.layers.Input(shape=(self.im_height, self.im_width, 3)) if self.backbond == "MOBILENET_V2": feature = MobileNetV2(input_shape=(224, 224, 3), weights="imagenet", include_top=False)(inputs) feature = tf.keras.layers.GlobalAveragePooling2D()(feature) feature = tf.keras.layers.Dropout(0.5)(feature) feature = tf.keras.layers.Dense(256, activation='relu')(feature) feature = tf.keras.layers.Dropout(0.2)(feature) elif self.backbond == "EFFICIENT_NET_B0": efn_backbond = efn.EfficientNetB0(weights='imagenet', include_top=False, input_shape=(self.im_height, self.im_width, 3)) efn_backbond.trainable = True feature = efn_backbond(inputs) feature = tf.keras.layers.GlobalAveragePooling2D()(feature) feature = tf.keras.layers.Dropout(0.5)(feature) feature = tf.keras.layers.Dense(256, activation='relu')(feature) feature = tf.keras.layers.Dropout(0.2)(feature) elif self.backbond == "EFFICIENT_NET_B2": efn_backbond = efn.EfficientNetB2(weights='imagenet', include_top=False, input_shape=(self.im_height, self.im_width, 3)) efn_backbond.trainable = True feature = efn_backbond(inputs) feature = tf.keras.layers.GlobalAveragePooling2D()(feature) feature = tf.keras.layers.Dropout(0.5)(feature) feature = tf.keras.layers.Dense(256, activation='relu')(feature) feature = tf.keras.layers.Dropout(0.2)(feature) elif self.backbond == "EFFICIENT_NET_B3": efn_backbond = efn.EfficientNetB3(weights='imagenet', include_top=False, input_shape=(self.im_height, self.im_width, 3)) efn_backbond.trainable = True feature = efn_backbond(inputs) feature = tf.keras.layers.Flatten()(feature) feature = tf.keras.layers.Dropout(0.5)(feature) feature = tf.keras.layers.Dense(1024, activation='relu')(feature) feature = tf.keras.layers.Dropout(0.2)(feature) elif self.backbond == "EFFICIENT_NET_B4": efn_backbond = efn.EfficientNetB4(weights='imagenet', include_top=False, input_shape=(self.im_height, self.im_width, 3)) efn_backbond.trainable = True feature = efn_backbond(inputs) feature = tf.keras.layers.Flatten()(feature) feature = tf.keras.layers.Dropout(0.5)(feature) feature = tf.keras.layers.Dense(1024, activation='relu')(feature) feature = tf.keras.layers.Dropout(0.2)(feature) else: raise ValueError( 'No such arch!... Please check the backend in config file') outputs = tf.keras.layers.Dense(15, name='landmarks', activation="sigmoid")(feature) model = tf.keras.Model(inputs=inputs, outputs=outputs) # print(model.summary()) # exit(0) def landmark_loss(alpha=0.8, beta=0.2): def landmark_loss_func(target, pred): coor_x_t = target[:][:, ::2] coor_y_t = target[:, 1:][:, ::2] coor_x_p = pred[:][:, ::2] coor_y_p = pred[:, 1:][:, ::2] ra1_t = tf.math.atan2((coor_y_t[:, 1] - coor_y_t[:, 0]), (coor_x_t[:, 1] - coor_x_t[:, 0] + 1e-5)) ra1_p = tf.math.atan2((coor_y_p[:, 1] - coor_y_p[:, 0]), (coor_x_p[:, 1] - coor_x_p[:, 0] + 1e-5)) ra2_t = tf.math.atan2((coor_y_t[:, 2] - coor_y_t[:, 1]), (coor_x_t[:, 2] - coor_x_t[:, 1] + 1e-5)) ra2_p = tf.math.atan2((coor_y_p[:, 2] - coor_y_p[:, 1]), (coor_x_p[:, 2] - coor_x_p[:, 1] + 1e-5)) la1_t = tf.math.atan2( (coor_y_t[:, -2] - coor_y_t[:, -1]), (coor_x_t[:, -2] - coor_x_t[:, -1] + 1e-5)) la1_p = tf.math.atan2( (coor_y_p[:, -2] - coor_y_p[:, -1]), (coor_x_p[:, -2] - coor_x_p[:, -1] + 1e-5)) la2_t = tf.math.atan2( (coor_y_t[:, -3] - coor_y_t[:, -2]), (coor_x_t[:, -3] - coor_x_t[:, -2] + 1e-5)) la2_p = tf.math.atan2( (coor_y_p[:, -3] - coor_y_p[:, -2]), (coor_x_p[:, -3] - coor_x_p[:, -2] + 1e-5)) angle_loss = tf.math.reduce_mean(( (ra1_t - ra1_p) / (8 * np.pi))**+((ra2_t - ra2_p) / (8 * np.pi))**2 + ((la1_t - la1_p) / (8 * np.pi))**2 + ((la2_t - la2_p) / (8 * np.pi))**2) bce_loss = tf.keras.losses.binary_crossentropy(target, pred) lm_loss = alpha * bce_loss + beta * angle_loss return lm_loss return landmark_loss_func loss_func = None if self.loss_func == "binary_crossentropy": loss_func = "binary_crossentropy" elif self.loss_func == "landmark_loss": loss_func = landmark_loss() else: print("Unknown loss function:", self.loss_func) exit(1) model.compile(optimizer=optimizers.Adam(self.learning_rate), loss=loss_func) 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
shuffle=False, target_size=(260, 260)) ##x,y = test_generator.class_indices.next() print(test_generator.filenames) ##train_generator. ##print(train_generator.labels) # confirm the iterator works batchX, batchy = train_generator.next() print('Batch shape=%s, min=%.3f, max=%.3f' % (batchX.shape, batchX.min(), batchX.max())) # Build the model. IMG_SHAPE = (260, 260, 3) model_eff = efn.EfficientNetB2(input_shape=IMG_SHAPE, weights='imagenet') ##model = efn.EfficientNetB0(input_shape=IMG_SHAPE,include_top=False) ##global_average_layer = tf.keras.layers.GlobalAveragePooling2D() prediction_layer = tf.keras.layers.Dense(2, activation='sigmoid') model = tf.keras.Sequential([ model_eff, ##global_average_layer, prediction_layer ]) ##https://stackoverflow.com/questions/42606207/keras-custom-decision-threshold-for-precision-and-recall def precision_threshold(threshold=0.5): def precision(y_true, y_pred): """Precision metric.
def load_backbone(backbone_type="resnet50", backbone_outputs=('C3', 'C4', 'C5', 'P6', 'P7'), num_features=256): global BACKBONE_LAYERS inputs = Input((None, None, 3), name='images') if backbone_type.lower() == 'resnet50': preprocess = BackBonePreProcess(rgb=False, mean_shift=True, normalize=0)(inputs) model = ResNet50(input_tensor=preprocess, include_top=False) elif backbone_type.lower() == 'resnet50v2': preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=2)(inputs) resnet50v2, _ = Classifiers.get('resnet50v2') model = resnet50v2(input_tensor=preprocess, include_top=False, weights='imagenet') elif backbone_type.lower() == "resnet101v2": preprocess = BackBonePreProcess(rgb=True, mean_shift=False, normalize=2)(inputs) model = ResNet101V2(input_tensor=preprocess, include_top=False, backend=tf.keras.backend, layers=tf.keras.layers, models=tf.keras.models, utils=tf.keras.utils) elif backbone_type.lower() == 'resnext50': preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=2)(inputs) model = ResNeXt50(input_tensor=preprocess, include_top=False) elif backbone_type.lower() == "seresnet50": preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=3)(inputs) seresnet50, _ = Classifiers.get('seresnet50') model = seresnet50(input_tensor=preprocess, original_input=inputs, include_top=False, weights='imagenet') elif backbone_type.lower() == "seresnet34": preprocess = BackBonePreProcess(rgb=True, mean_shift=False, normalize=0)(inputs) seresnet34, _ = Classifiers.get('seresnet34') model = seresnet34(input_tensor=preprocess, original_input=inputs, include_top=False, weights='imagenet') elif backbone_type.lower() == "seresnext50": preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=3)(inputs) seresnext50, _ = Classifiers.get('seresnext50') model = seresnext50(input_tensor=preprocess, original_input=inputs, include_top=False, weights='imagenet') elif backbone_type.lower() == "vgg16": preprocess = BackBonePreProcess(rgb=False, mean_shift=True, normalize=0)(inputs) model = VGG16(input_tensor=preprocess, include_top=False) elif backbone_type.lower() == "mobilenet": preprocess = BackBonePreProcess(rgb=False, mean_shift=False, normalize=2)(inputs) model = MobileNet(input_tensor=preprocess, include_top=False, alpha=1.0) elif backbone_type.lower() == 'efficientnetb2': preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=3)(inputs) model = efn.EfficientNetB2(input_tensor=preprocess, include_top=False, weights='imagenet') elif backbone_type.lower() == 'efficientnetb3': preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=3)(inputs) model = efn.EfficientNetB3(input_tensor=preprocess, include_top=False, weights='imagenet') elif backbone_type.lower() == 'efficientnetb4': preprocess = BackBonePreProcess(rgb=True, mean_shift=True, normalize=3)(inputs) model = efn.EfficientNetB4(input_tensor=preprocess, include_top=False, weights='imagenet') else: raise NotImplementedError( f"backbone_type은 {BACKBONE_LAYERS.keys()} 중에서 하나가 되어야 합니다.") model.trainable = False # Block Layer 가져오기 features = [] for key, layer_name in BACKBONE_LAYERS[backbone_type.lower()].items(): if key in backbone_outputs: layer_tensor = model.get_layer(layer_name).output features.append(Identity(name=key)(layer_tensor)) if backbone_type.lower() == "mobilenet": # Extra Layer for Feature Extracting Z6 = ZeroPadding2D(((0, 1), (0, 1)), name=f'P6_zeropadding')(features[-1]) P6 = Conv2D(num_features, (3, 3), strides=(2, 2), padding='valid', activation='relu', name=f'P6_conv')(Z6) if 'P6' in backbone_outputs: features.append(Identity(name='P6')(P6)) G6 = GroupNormalization(name=f'P6_norm')(P6) Z7 = ZeroPadding2D(((0, 1), (0, 1)), name=f'P7_zeropadding')(G6) P7 = Conv2D(num_features, (3, 3), strides=(2, 2), padding='valid', activation='relu', name=f'P7_conv')(Z7) if 'P7' in backbone_outputs: features.append(Identity(name=f'P7')(P7)) else: P6 = Conv2D(num_features, (3, 3), strides=(2, 2), padding='same', activation='relu', name=f'P6_conv')(features[-1]) if 'P6' in backbone_outputs: features.append(Identity(name=f'P6')(P6)) G6 = GroupNormalization(name=f'P6_norm')(P6) P7 = Conv2D(num_features, (3, 3), strides=(2, 2), padding='same', activation='relu', name=f'P7_conv')(G6) if 'P7' in backbone_outputs: features.append(Identity(name=f'P7')(P7)) return Model(inputs, features, name=backbone_type)
def getEfficientNetModel(): model = efn.EfficientNetB2(include_top=True, weights=None, input_shape=(img_size, img_size, 1), classes=3) return model
cache_dir = os.path.expanduser(os.path.join('~', '.keras', 'models')) weight_path = os.path.join( cache_dir, '{}_{}_{}.h5'.format(model.name, md5_hash, K.image_data_format())) if os.path.exists(weight_path): model.load_weights(weight_path) else: #path = get_file(fname, origin=origin, extract=True, md5_hash=md5_hash, cache_dir=cache_dir) path = 'yourmodelpath' checkpoint_file = os.path.join(path, 'model.ckpt') print(checkpoint_file) load_weights_from_tf_checkpoint(model, checkpoint_file, background_label) model.save_weights(weight_path) model = efn.EfficientNetB2(input_shape=(128, 256, 3), weights=None, include_top=False) load_pretrained_weights( model, 'efficientnet-b2', origin= 'https://storage.googleapis.com/cloud-tpu-checkpoints/efficientnet/advprop/efficientnet-b2.tar.gz', md5_hash='6fdf8c24a374f0b5fcd8211af623dcd3', background_label=False, cache_dir='C:\\Users\\zhangzheng\\.keras\\models\\datasets\\')
def create_model(l2, num_classes, num_ctrl_classes): ############## # BRANCH MODEL ############## regul = regularizers.l2(l2) # optim = Adam(lr=lr) # kwargs = {'kernel_regularizer': regul} base_model = efn.EfficientNetB2(input_shape=(network_shape[0], network_shape[1], 3), weights='imagenet', include_top=False) input_tensor = Input(shape=network_shape, dtype=K.floatx()) conv1 = Conv2D(32, kernel_size=(3, 3), strides=(2, 2), activation='relu', use_bias=False, padding='same', input_shape=(input_shape[0] + 6, input_shape[1] + 6, input_shape[2])) layers = [] layers.append(input_tensor) layers.append(conv1) layers[2:] = base_model.layers[2:] new_model = copy_model_graph(layers, base_model, input_tensor) weights = base_model.layers[1].get_weights() weight0 = weights[0] w = np.concatenate((weight0, weight0), axis=2) w = w / 2.0 weights[0] = w # weights.append(np.zeros((64),dtype='float32')) new_model.layers[1].set_weights(weights) inp = Input(shape=input_shape, dtype='uint8') # 384x384x6 x = Lambda(augment)(inp) for layer in new_model.layers: if type(layer) is Conv2D: layer.kernel_regularizer = regul x = new_model(x) x = GlobalMaxPooling2D()(x) x = BatchNormalization()(x) x = Dropout(rate=0.5)(x) x = Flatten()(x) x = Dense(512, use_bias=False, kernel_initializer='he_normal')(x) x = BatchNormalization()(x) encoder_model = Model(inputs=inp, outputs=x) # softmax model for training encoder output_softmax = Dense(num_classes, use_bias=False, activation='softmax')(x) softmax_model = Model(inputs=inp, outputs=output_softmax) ################# # COMPARE MODEL # ################# mid = 32 xa_inp = Input(shape=encoder_model.output_shape[1:]) xb_inp = Input(shape=encoder_model.output_shape[1:]) x1 = Lambda(lambda x: x[0] * x[1])([xa_inp, xb_inp]) x2 = Lambda(lambda x: x[0] + x[1])([xa_inp, xb_inp]) x3 = Lambda(lambda x: x[0] - x[1])([xa_inp, xb_inp]) x4 = Lambda(lambda x: K.square(x))(x3) head = Concatenate()([x1, x2, x3, x4]) head = Reshape((4, encoder_model.output_shape[1], 1), name='reshape1')(head) # Per feature NN with shared weight is implemented using CONV2D with appropriate stride. head = Conv2D(mid, (4, 1), activation='relu', padding='valid')(head) head = Reshape((encoder_model.output_shape[1], mid, 1))(head) head = Conv2D(1, (1, mid), activation='linear', padding='valid')(head) head = Flatten()(head) compare_model = Model([xa_inp, xb_inp], head) # process encoding from control # compare the current features to all controls features_controls = Input( shape=[num_ctrl_classes, encoder_model.output_shape[1]]) fs = Lambda(lambda x: tf.unstack(x, axis=1))(features_controls) # def create_mask(features_controls): # # Use a function with a Keras Lambda layer wrapper to resolve a tensorflow issue. # # https://stackoverflow.com/questions/50715928/valueerror-output-tensors-to-a-model-must-be-the-output-of-a-tensorflow-layer # max_abs_features = K.max(K.abs(features_controls), axis=2) # mask = tf.greater(max_abs_features, K.epsilon()) # mask = tf.expand_dims(tf.expand_dims(tf.dtypes.cast(mask, K.floatx()), axis=-1), axis=-1) # return mask # mask = Lambda(create_mask)(features_controls) comps = [] for f in fs: comp = compare_model([x, f]) comps.append(comp) c = Concatenate()(comps) c = Reshape((num_ctrl_classes, encoder_model.output_shape[1], 1))(c) # c = Lambda(lambda x: tf.math.multiply(x[0], x[1]))([c, mask]) # compare = Lambda(compare_features)([x, features_controls]) # Per feature NN with shared weight is implemented using CONV2D with appropriate stride. compare = Conv2D(mid, (num_ctrl_classes, 1), activation='relu', padding='valid')(c) compare = Reshape((encoder_model.output_shape[1], mid, 1))(compare) compare = Conv2D(1, (1, mid), activation='linear', padding='valid')(compare) compare = Flatten(name='flatten2')(compare) feature_model = Model(inputs=[inp, features_controls], outputs=compare) label = Input(shape=(num_classes, )) output_arcface = ArcFace(num_classes, regularizer=regul)([compare, label]) arcface_model = Model([inp, features_controls, label], output_arcface) output_cosface = CosFace(num_classes, regularizer=regul)([compare, label]) cosface_model = Model([inp, features_controls, label], output_cosface) return encoder_model, softmax_model, feature_model, arcface_model, cosface_model