if args.resume: print('resume from checkpoint') model = keras.models.load_model(save_file) else: print('train from start') model = models.Sequential() if '121' in args_model: base_model = DenseNet121(weights=None, include_top=False, input_shape=(32, 32, 3), pooling='avg') elif '169' in args_model: base_model = DenseNet169(weights=None, include_top=False, input_shape=(32, 32, 3), pooling='avg') elif '201' in args_model: base_model = DenseNet201(weights=None, include_top=False, input_shape=(32, 32, 3), pooling='avg') model.add(base_model) #model.add(layers.Flatten()) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(128, activation='relu')) #model.add(layers.Dropout(0.5)) #model.add(layers.BatchNormalization()) #model.add(layers.Dense(64, activation='relu')) #model.add(layers.Dropout(0.5))
def densenet_169(weights=densenet169_path): weights = weights pretrained_model = DenseNet169(weights=weights, include_top=False) return (pretrained_model)
#!/usr/bin/env python from keras.preprocessing.image import load_img from keras.preprocessing.image import img_to_array from keras.applications.densenet import preprocess_input from keras.applications.densenet import decode_predictions from keras.applications.densenet import DenseNet169 # iteration count _iter = 1 """ Main """ if __name__ == '__main__': # load the model model = DenseNet169() # load an image from file image = load_img('mug.jpg', target_size=(224, 224)) # convert the image pixels to a numpy array image = img_to_array(image) # reshape data for the model image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2])) # prepare the image for the VGG model image = preprocess_input(image) # predict the probability across all output classes for i in range(_iter): raw_input('{} iteration, press any key to perform...'.format(str(i))) yhat = model.predict(image) # return if not iter if not _iter: exit()
from keras.applications.mobilenet import preprocess_input preprocessing_function = preprocess_input base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "DenseNet121": from keras.applications.densenet import preprocess_input preprocessing_function = preprocess_input base_model = DenseNet121(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "DenseNet169": from keras.applications.densenet import preprocess_input preprocessing_function = preprocess_input base_model = DenseNet169(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "DenseNet201": from keras.applications.densenet import preprocess_input preprocessing_function = preprocess_input base_model = DenseNet201(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "NASNetLarge": from keras.applications.nasnet import preprocess_input preprocessing_function = preprocess_input base_model = NASNetLarge(weights='imagenet', include_top=True, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "NASNetMobile": from keras.applications.nasnet import preprocess_input
def TransferNet(input_shape: Tuple[int, int], num_classes: int, feature_extractor: FeatureExtractor = None, dense_layers: int = 3, dropout_rate: float = 0.3) -> Model: """ Exploits a pre-trained model as feature extractor, feeding its output into a fully-connected NN. The feature extractor model is NOT fine-tuned for the specific task. Dropout and batch normalization are used throughout the trainable portion of the network. :param input_shape: Shape of the input tensor as a 2-dimensional int tuple :param num_classes: Number of classes for the final FC layer :param feature_extractor: FeatureExtractor instance representing which pre-trained model to use as feature extractor :param dense_layers: Number of layers for the FC NN :param dropout_rate: Dropout rate :return: a Keras model """ adam_opt = Adam(lr=0.1) model_input = Input(shape=input_shape) # load pre-trained model on ImageNet if feature_extractor == FeatureExtractor.Dense121: fe_model = DenseNet121(weights="imagenet", include_top=False, input_tensor=model_input) elif feature_extractor == FeatureExtractor.Dense169: fe_model = DenseNet169(weights="imagenet", include_top=False, input_tensor=model_input) elif feature_extractor == FeatureExtractor.Dense201: fe_model = DenseNet201(weights="imagenet", include_top=False, input_tensor=model_input) elif feature_extractor == FeatureExtractor.NASNetLarge: fe_model = NASNetLarge(weights="imagenet", include_top=False, input_tensor=model_input) else: # default: NASNetMobile fe_model = NASNetMobile(weights="imagenet", include_top=False, input_tensor=model_input) fe_model = Model(input=model_input, output=fe_model.output, name="FeatureExtractor") fe_model.compile(loss=keras.losses.categorical_crossentropy, optimizer=adam_opt, metrics=["accuracy"]) # get handles to the model (input, output tensors) fe_input = fe_model.get_layer(index=0).input fe_output = fe_model.get_layer(index=-1).output # freeze layers for _, layer in enumerate(fe_model.layers): layer.trainable = False # final fully-connected layers dense = Flatten()(fe_output) dense = BatchNormalization()(dense) dense = Dropout(rate=dropout_rate)(dense) num_units = 128 for i in range(1, dense_layers + 1): dense = Dense(units=int(num_units / i), activation="relu")(dense) dense = BatchNormalization()(dense) dense = Dropout(rate=dropout_rate)(dense) output_dense = Dense(units=num_classes, activation="softmax")(dense) model = Model(input=fe_input, output=output_dense, name="TransferNet") model.compile(loss=keras.losses.categorical_crossentropy, optimizer=adam_opt, metrics=["accuracy"]) return model
weights=None, include_top=False) batch_size = 32 * gpu_number elif model_name == 'nasnet': base_model = NASNetLarge(input_tensor=input_tensor, weights=None, include_top=False) batch_size = 24 * gpu_number elif model_name == 'densenet121': base_model = DenseNet121(input_tensor=input_tensor, weights=None, include_top=False) batch_size = 48 * gpu_number elif model_name == 'densenet169': base_model = DenseNet169(input_tensor=input_tensor, weights=None, include_top=False) batch_size = 48 * gpu_number elif model_name == 'densenet201': base_model = DenseNet201(input_tensor=input_tensor, weights=None, include_top=False) batch_size = 32 * gpu_number elif model_name == 'seresnext': base_model = SEResNeXt(input_tensor=input_tensor, num_classes=num_classes, include_top=False).model batch_size = 4 * gpu_number elif model_name == 'seresnet50': base_model = SEResNet50(input_shape=input_shape, include_top=False) batch_size = 32 * gpu_number