def define_model(architecture, input_shape=(128, 128, 3), n_classes=4): if architecture == 'ResNet50V2': base_model = ResNet50V2(weights='imagenet', include_top=False, input_shape=input_shape) elif architecture == 'VGG16': base_model = VGG16(weights='imagenet', include_top=False, input_shape=input_shape) elif architecture == 'VGG19': base_model = VGG19(weights='imagenet', include_top=False, input_shape=input_shape) elif architecture == 'DenseNet121': base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=input_shape) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(512, activation='relu')(x) predictions = Dense(n_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) return model
def create_model(): base_model = DenseNet121( weights='imagenet', include_top=False, pooling='avg' ) # Add new fully connected layers for ifcb # dense layer to classify with softmax model_output = base_model.output model_output = Dense(1024, activation='relu')(model_output) model_output = Dense(50, activation='softmax')(model_output) model = Model(inputs=base_model.input, outputs=model_output) # Only train the dense layers, user transfer learning for the rest for layer in model.layers[:-2]: layer.trainable = False for layer in model.layers[-2::]: layer.trainable = True model.compile( optimizer='Adam', loss='categorical_crossentropy', metrics=['categorical_accuracy'], ) return model
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 DenseNet-121 model used in pretraining. """ base_model = DenseNet121(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 get_cbir_results(mode, search_number, dist): global cbir_tree, cbir_data ''' results = get_res(FILEPATH,cbir_data,cbir_tree,search_number) ''' model = DenseNet121(input_shape=(224, 224, 3), weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[-2].output) cbir_image = load_image(mode, FILEPATH, (224, 224)) prediction = model.predict(cbir_image) result = cbir_tree.get_n_nearest_neighbors(prediction, 50) #print(result) results = [[ os.path.join('C:/Users/mdrah/Downloads/ImageClef 2013', cbir_data[tuple(res[1])]), res[0] ] for res in result] results.sort(key=lambda x: x[1]) #print(results) return results
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 modelInit(self): rnet = DenseNet121( input_shape=(224, 224, 3), weights="imagenet", include_top=False, ) rnet.trainable = True model = keras.models.Sequential() model.add(tf.keras.layers.GaussianNoise(30)) model.add( tf.keras.layers.experimental.preprocessing.Rescaling(1. / 255)) # model.add(keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical")) model.add( tf.keras.layers.experimental.preprocessing.RandomContrast(0.99)) model.add(keras.layers.experimental.preprocessing.RandomRotation(0.85)) model.add(rnet) model.add(keras.layers.GlobalAveragePooling2D()) model.add(keras.layers.Dense(len(CLASSES), activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) return model
def get_model(model): output_layer = -1 if model in [ 'VGG16', 'VGG19', 'RESNET50', 'INCEPTION', 'DENSE121', 'XCEPTION' ]: if model == 'VGG19': model = VGG19(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[output_layer].output) elif model == 'VGG16': model = VGG16(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[output_layer].output) elif model == 'RESNET50': model = ResNet50(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[output_layer].output) elif model == 'DENSE121': model = DenseNet121(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[output_layer].output) elif model == 'INCEPTION': model = InceptionV3(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[output_layer].output) elif model == 'XCEPTION': model = InceptionV3(weights='imagenet') model = Model(inputs=model.inputs, outputs=model.layers[output_layer].output) return model else: raise ()
def load_model(): ''' load pretrained Keras model. In this case, our model has been pretrained on Imagenet ''' global model_densenet121 model_densenet121 = DenseNet121(weights='imagenet') model_densenet121._make_predict_function()
def get_dense_encoder(input_shape, weights): ''' Return the Dense architecture to represent the siamese branch ''' model = DenseNet121(weights=weights, input_shape=input_shape, include_top=False) for layer in model.layers: layer.trainable = True return model
def __init__(self, model_name=None): if model_name == 'Xception': base_model = Xception(weights='imagenet') self.preprocess_input = xception.preprocess_input elif model_name == 'VGG19': base_model = VGG19(weights='imagenet') self.preprocess_input = vgg19.preprocess_input elif model_name == 'ResNet50': base_model = ResNet50(weights='imagenet') self.preprocess_input = resnet.preprocess_input elif model_name == 'ResNet101': base_model = ResNet101(weights='imagenet') self.preprocess_input = resnet.preprocess_input elif model_name == 'ResNet152': base_model = ResNet152(weights='imagenet') self.preprocess_input = resnet.preprocess_input elif model_name == 'ResNet50V2': base_model = ResNet50V2(weights='imagenet') self.preprocess_input = resnet_v2.preprocess_input elif model_name == 'ResNet101V2': base_model = ResNet101V2(weights='imagenet') self.preprocess_input = resnet_v2.preprocess_input elif model_name == 'ResNet152V2': base_model = ResNet152V2(weights='imagenet') self.preprocess_input = resnet_v2.preprocess_input elif model_name == 'InceptionV3': base_model = InceptionV3(weights='imagenet') self.preprocess_input = inception_v3.preprocess_input elif model_name == 'InceptionResNetV2': base_model = InceptionResNetV2(weights='imagenet') self.preprocess_input = inception_resnet_v2.preprocess_input elif model_name == 'DenseNet121': base_model = DenseNet121(weights='imagenet') self.preprocess_input = densenet.preprocess_input elif model_name == 'DenseNet169': base_model = DenseNet169(weights='imagenet') self.preprocess_input = densenet.preprocess_input elif model_name == 'DenseNet201': base_model = DenseNet201(weights='imagenet') self.preprocess_input = densenet.preprocess_input elif model_name == 'NASNetLarge': base_model = NASNetLarge(weights='imagenet') self.preprocess_input = nasnet.preprocess_input elif model_name == 'NASNetMobile': base_model = NASNetMobile(weights='imagenet') self.preprocess_input = nasnet.preprocess_input elif model_name == 'MobileNet': base_model = MobileNet(weights='imagenet') self.preprocess_input = mobilenet.preprocess_input elif model_name == 'MobileNetV2': base_model = MobileNetV2(weights='imagenet') self.preprocess_input = mobilenet_v2.preprocess_input else: base_model = VGG16(weights='imagenet') self.preprocess_input = vgg16.preprocess_input self.model = Model(inputs=base_model.input, outputs=base_model.layers[-2].output)
def create_model(self, num_outputs): print('[Dronet] Starting dronet') # input_shape=(224, 224, 3), self.denseNet121 = DenseNet121(include_top=self.include_top, weights=None, classes=num_outputs) print('[Dronet] Done with dronet')
def __init__(self, input_size, weights): input_image = Input(shape=(input_size[0], input_size[1], 3)) if weights == 'imagenet': densenet = DenseNet121(input_tensor=input_image, include_top=False, weights='imagenet', pooling=None) print('Successfully loaded imagenet backend weights') else: densenet = DenseNet121(input_tensor=input_image, include_top=False, weights=None, pooling=None) if weights: densenet.load_weights(weights) print('Loaded backend weigths: ' + weights) self.feature_extractor = densenet
def create_DenseNet121_model(input_shape, num_classes): model = tf.keras.Sequential([ DenseNet121(include_top=False, weights='imagenet', input_shape=input_shape), GlobalAveragePooling2D(), Dense(num_classes), Activation("softmax") ]) model.summary() return model
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 cifar_model(x_test, model_name='MobileNetV2', num_classes=10): # Load model from tensorlfow include_top = False weights = 'imagenet' input_tensor = Input(x_test.shape[1:], dtype='float16') input_shape = x_test.shape[1:] pooling = None classes = num_classes if model_name == 'MobileNetV2': KerasModel = MobileNetV2(include_top=include_top, weights=weights, input_tensor=input_tensor, input_shape=input_shape, pooling=pooling, classes=classes) elif model_name == 'ResNet50V2': KerasModel = ResNet50V2(include_top=include_top, weights=weights, input_tensor=input_tensor, input_shape=input_shape, pooling=pooling, classifier_activation='softmax', classes=classes) elif model_name == 'ResNet50': KerasModel = ResNet50(include_top=include_top, weights=weights, input_tensor=input_tensor, input_shape=input_shape, pooling=pooling, classifier_activation='softmax', classes=classes) elif model_name == 'DenseNet121': KerasModel = DenseNet121(include_top=include_top, weights=weights, input_tensor=input_tensor, input_shape=input_shape, pooling=pooling, classes=classes) inputs = KerasModel.input output = KerasModel.output x = GlobalAveragePooling2D()(output) x = Flatten()(x) x = Dense(num_classes, kernel_initializer='he_normal')(x) outputs = Softmax(dtype='float32')(x) # Instantiate model. model = Model(inputs=inputs, outputs=outputs) return model
def pretrainded_model(type: str, trainable=False): with strategy.scope(): if type == 'VGG16': pretrained_model = VGG16(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif type == 'VGG19': pretrained_model = VGG19(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif type == 'DenseNet121': pretrained_model = DenseNet121(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif type == 'DenseNet169': pretrained_model = DenseNet169(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3]) elif type == 'DenseNet201': pretrained_model = DenseNet201(weights='imagenet', include_top=False, input_shape=[*IMAGE_SIZE, 3]) pretrained_model.trainable = trainable model = Sequential([ # To a base pretrained on ImageNet to extract features from images... pretrained_model, # ... attach a new head to act as a classifier. Flatten(), Dense(256, activation='relu'), BatchNormalization(), Dropout(0.2), Dense(256, activation='relu'), BatchNormalization(), Dropout(0.2), Dense(256, activation='relu'), BatchNormalization(), Dropout(0.2), Dense(256, activation='relu'), BatchNormalization(), Dropout(0.2), Dense(256, activation='relu'), BatchNormalization(), Dropout(0.2), tf.keras.layers.Dense(len(CLASSES), activation='softmax', use_bias=False) ]) return model
def call(self, img): # Input # x = DenseNet121(include_top=self.include_top, weights=None, classes = 10) (img) # model_d = DenseNet121(weights='imagenet', include_top=False, input_shape=(128, 128, 3)) model_d = DenseNet121(include_top=self.include_top, weights=None, classes=10)(img) if self.include_top: model_d = tf.keras.layers.Activation('relu')(model_d) model_d = self.dense0(model_d) model_d = self.dense1(model_d) model_d = self.dense2(model_d) return model_d '''
def create_network(): net = DenseNet121(include_top=False, weights="imagenet", input_shape=(160, 160, 3)) # don't train until conv4 blocks for l in net.layers: if "conv4" in l.name: break l.trainable = False x = GlobalAveragePooling2D()(net.layers[-1].output) x = Dense(176, activation="softmax")(x) return Model(net.inputs, x)
def densenetModel(): densenet = DenseNet121(include_top=False, input_shape=input_shape) model = Sequential([ densenet, GlobalAveragePooling2D(), Dropout(0.5), Dense(numClasses, activation='sigmoid') ]) model.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.00005), metrics=['accuracy']) return model
def DenseNet121model(no_classes, shape): """ DenseNet121 """ base_model = DenseNet121(include_top=False, weights='imagenet', input_shape=shape) base_model.trainable = False inputs = Input(shape=shape) x = base_model(inputs, training=False) x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(no_classes, activation='softmax', name='predictions')(x) model = Model(inputs, outputs=predictions) return model
def call(self, img): print("img===============", img) # Input x = DenseNet121(include_top=self.include_top, weights=None, classes=10)(img) if self.include_top: x = tf.keras.layers.Activation('relu')(x) # x = tf.keras.layers.Dropout(0.5)(x) x = self.dense0(x) x = self.dense1(x) gate_pose = self.dense2(x) # phi_rel = self.dense_phi_rel(x) # gate_pose = tf.concat([gate_pose, phi_rel], 1) return gate_pose else: return x
def create_densenet121_model(num_classes: int): """ Function to create a DenseNet121 model pre-trained with custom FC Layers. If the "advanced" command line argument is selected, adds an extra convolutional layer with extra filters to support larger images. :param num_classes: The number of classes (labels). :return: The DenseNet121 model. """ # Reconfigure single channel input into a greyscale 3 channel input img_input = Input(shape=(config.DENSE_NET_IMG_SIZE['HEIGHT'], config.DENSE_NET_IMG_SIZE['WIDTH'], 1)) img_conc = Concatenate()([img_input, img_input, img_input]) # Generate a DenseNet121 model with pre-trained ImageNet weights, input as given above, excluded fully connected layers. model_base = DenseNet121(include_top=False, weights="imagenet", input_tensor=img_conc) # Add fully connected layers model = Sequential() # Start with base model consisting of convolutional layers model.add(model_base) # Flatten layer to convert each input into a 1D array (no parameters in this layer, just simple pre-processing). model.add(Flatten()) fully_connected = Sequential(name="Fully_Connected") # Fully connected layers. fully_connected.add(Dropout(0.2, seed=config.RANDOM_SEED, name="Dropout_1")) fully_connected.add(Dense(units=512, activation='relu', name='Dense_1')) # fully_connected.add(Dropout(0.2, name="Dropout_2")) fully_connected.add(Dense(units=32, activation='relu', name='Dense_2')) # Final output layer that uses softmax activation function (because the classes are exclusive). if num_classes == 2: fully_connected.add(Dense(1, activation='sigmoid', kernel_initializer="random_uniform", name='Output')) else: fully_connected.add( Dense(num_classes, activation='softmax', kernel_initializer="random_uniform", name='Output')) model.add(fully_connected) # Print model details if running in debug mode. if config.verbose_mode: print("CNN Model used:") print(model.summary()) print("Fully connected layers:") print(fully_connected.summary()) return model
def Annotation_model(vocab_size, max_len): base_model = DenseNet121(input_shape=(224, 224, 3), weights='imagenet') for layer in base_model.layers: layer.trainable = False feature = base_model.layers[-2].output final_model = Dense(vocab_size, activation='sigmoid')(feature) model = Model(inputs=base_model.input, outputs=final_model) print(model.summary()) return model
def buildTunerModel(self, img_size): """ This function builds a base DenseNet-121 model for keras tuner Parameters: img_size (int): the size of input images (img_size, img_size). Returns: model (class): the DenseNet-121 model used for keras tuner. """ base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(img_size, img_size, 3)) base_model.load_weights(self.weights, by_name=True) return base_model
def get_encoder_model(name, in_shape, pooling): if name == "InceptionV3": model = InceptionV3(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet50": model = ResNet50(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet50V2": model = ResNet50V2(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet101": model = ResNet101(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet101V2": model = ResNet101V2(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "ResNet152": model = ResNet152(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "InceptionResNetV2": model = InceptionResNetV2(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) elif name == "DenseNet121": model = DenseNet121(include_top=False, input_shape=in_shape, weights=None, pooling=pooling) else: raise ValueError("model " + name + " not found") return model
def build_DenseNet121(IMG_SIZE=224, channels=3, trainable_layers=10): base = DenseNet121(weights=None, include_top=False, input_shape=(IMG_SIZE, IMG_SIZE, channels)) base.trainable = True ''' for layer in base.layers[:-trainable_layers]: layer.trainable = False for layer in base.layers[-trainable_layers:]: layer.trainable = True ''' model = models.Sequential() model.add(base) model.add(layers.GlobalAveragePooling2D()) model.add(layers.Dense(256, activation='relu')) model.add(layers.Dense(2, activation='softmax')) return model
def make_densenet(inputshape): base_model = DenseNet121( input_tensor=Input(inputshape), include_top=False,) base_model.trainable= False inputs = Input(inputshape) x = densenet.preprocess_input(inputs) x = base_model(x, training=False) x = GlobalAveragePooling2D()(x) x = Dropout(0.3)(x) outputs = Dense(1, activation='sigmoid')(x) model = Model(inputs, outputs) return model
def __init__(self, maxBin, maxBin2): """ must have self.model, self.params, and self._name """ self._name = 'DenseNet121-imagenet' self.model = DenseNet121(include_top=True, weights="imagenet", input_shape=(224, 224, 3), classes=1000) self.params = { 'age hidden layer': 512, 'gender hidden layer': 1024, 'ethnicity hidden layer': 1024, } self.model = addOutputLayers(self.model, maxBin, maxBin2, self.params) self.model.compile(optimizer='Adam', loss=losses, metrics=['accuracy'])
def build_model(acti, opti, lr): # # 2. 모델구성 resnet = DenseNet121(include_top=False, input_shape=(42, 42, 3)) inputs = Input(shape=(x_train.shape[1], 1, 1), name='input') a = Conv2D(3, kernel_size=(1, 1))(inputs) a = UpSampling2D(size=(1, 42))(a) x = resnet(a) x = Flatten()(x) x = Dense(16, activation=acti)(x) outputs = Dense(1)(x) model = Model(inputs=inputs, outputs=outputs) model.summary() # 3. 컴파일 훈련 model.compile(loss='mse', optimizer=opti(learning_rate=lr), metrics='mae') return model
def get_model(): # Create Model.......................................... # Input layer baseModel = DenseNet121(weights="imagenet", include_top=False, input_tensor=Input(shape=(224, 224, 3))) for layer in baseModel.layers: layer.trainable = False x = baseModel.output # x = AveragePooling2D(pool_size=(3,3), name='avg_pool')(x) # LSTM layer x = Reshape((49, 1024))(x) x = ((LSTM(1024, activation="relu", return_sequences=True, trainable=False)))(x) x = BatchNormalization()(x) # x = Dropout(0.5)(x) # FC layer x = Flatten(name="flatten")(x) # fc1 layer x = Dense(units=4096, activation='relu')(x) x = BatchNormalization()(x) # x = Dropout(0.5)(x) # fc2 layer x = Dense(units=4096, activation='relu')(x) x = BatchNormalization()(x) # x = Dropout(0.5)(x) # Output layer output = Dense(units=3, activation='softmax')(x) model = Model(inputs=baseModel.input, outputs=output) opt = RMSprop(lr=0.01, clipvalue=100) model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=["accuracy"]) return model