def test_nasnet_variable_input_channels(): input_shape = (1, None, None) if K.image_data_format() == 'channels_first' else ( None, None, 1) model = applications.NASNetMobile(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 1056) model = applications.NASNetLarge(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 4032) input_shape = (4, None, None) if K.image_data_format() == 'channels_first' else ( None, None, 4) model = applications.NASNetMobile(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 1056) model = applications.NASNetLarge(weights=None, include_top=False, input_shape=input_shape) assert model.output_shape == (None, None, None, 4032)
def save_bottlebeck_features(): train_datagen = ImageDataGenerator(rescale=1. / 255) test_datagen = ImageDataGenerator(rescale=1. / 255) # build the NASNetLarge network model = applications.NASNetLarge(include_top=False, weights='imagenet') train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( train_generator, aug_factor * nb_classes * nb_train_samples // batch_size) train_labels = np.tile(np.repeat(np.arange(nb_classes), nb_train_samples), aug_factor) np.savez(bn_train_path, data=bottleneck_features_train, label=train_labels) test_generator = test_datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( test_generator, nb_classes * nb_validation_samples // batch_size) validation_labels = np.repeat(np.arange(nb_classes), nb_validation_samples) np.savez(bn_validation_path, data=bottleneck_features_validation, label=validation_labels)
def save_bottlebeck_features(): datagen = ImageDataGenerator(rescale=1. / 255) # build the VGG16 network model = applications.NASNetLarge(include_top=False, weights='imagenet') generator = datagen.flow_from_directory(train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_train = model.predict_generator( generator, nb_train_samples // batch_size) np.save('bottleneck_features_train.npy', bottleneck_features_train) generator = datagen.flow_from_directory(validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=None, shuffle=False) bottleneck_features_validation = model.predict_generator( generator, nb_validation_samples // batch_size) np.save('bottleneck_features_validation.npy', bottleneck_features_validation)
def test_nasnet_pooling(): model = applications.NASNetMobile(weights=None, include_top=False, pooling='avg') assert model.output_shape == (None, 1056) model = applications.NASNetLarge(weights=None, include_top=False, pooling='avg') assert model.output_shape == (None, 4032)
def get_imagenet_architecture(architecture, variant, size, alpha, output_layer, include_top=False, weights='imagenet'): from keras import applications, Model if include_top: assert output_layer == 'last' if size == 'auto': size = get_image_size(architecture, variant, size) shape = (size, size, 3) if architecture == 'densenet': if variant == 'auto': variant = 'densenet-121' if variant == 'densenet-121': model = applications.DenseNet121(weights=weights, include_top=include_top, input_shape=shape) elif variant == 'densenet-169': model = applications.DenseNet169(weights=weights, include_top=include_top, input_shape=shape) elif variant == 'densenet-201': model = applications.DenseNet201(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'inception-resnet-v2': model = applications.InceptionResNetV2(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'mobilenet': model = applications.MobileNet(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha) elif architecture == 'mobilenet-v2': model = applications.MobileNetV2(weights=weights, include_top=include_top, input_shape=shape, alpha=alpha) elif architecture == 'nasnet': if variant == 'auto': variant = 'large' if variant == 'large': model = applications.NASNetLarge(weights=weights, include_top=include_top, input_shape=shape) else: model = applications.NASNetMobile(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'resnet-50': model = applications.ResNet50(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'vgg-16': model = applications.VGG16(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'vgg-19': model = applications.VGG19(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'xception': model = applications.Xception(weights=weights, include_top=include_top, input_shape=shape) elif architecture == 'inception-v3': model = applications.InceptionV3(weights=weights, include_top=include_top, input_shape=shape) if output_layer != 'last': try: if isinstance(output_layer, int): layer = model.layers[output_layer] else: layer = model.get_layer(output_layer) except Exception: raise VergeMLError('layer not found: {}'.format(output_layer)) model = Model(inputs=model.input, outputs=layer.output) return model
def model_app(arch, input_tensor): """Loads the appropriate convolutional neural network (CNN) model Args: arch: String key for model to be loaded. input_tensor: Keras tensor to use as image input for the model. Returns: model: The specified Keras Model instance with ImageNet weights loaded and without the top classification layer. """ # function that loads the appropriate model if arch == 'Xception': model = applications.Xception(weights='imagenet', include_top=False, input_tensor=input_tensor) print('Xception loaded') elif arch == 'VGG16': model = applications.VGG16(weights='imagenet', include_top=False, input_tensor=input_tensor) print('VGG16 loaded') elif arch == 'VGG19': model = applications.VGG19(weights='imagenet', include_top=False, input_tensor=input_tensor) print('VGG19 loaded') elif arch == 'ResNet50': model = applications.ResNet50(weights='imagenet', include_top=False, input_tensor=input_tensor) print('ResNet50 loaded') elif arch == 'InceptionV3': model = applications.InceptionV3(weights='imagenet', include_top=False, input_tensor=input_tensor) print('InceptionV3 loaded') elif arch == 'InceptionResNetV2': model = applications.InceptionResNetV2(weights='imagenet', include_top=False, input_tensor=input_tensor) print('InceptionResNetV2 loaded') elif arch == 'MobileNet': model = applications.MobileNet(input_shape=(224, 224, 3), weights='imagenet', include_top=False, input_tensor=input_tensor) print('MobileNet loaded') elif arch == 'DenseNet121': model = applications.DenseNet121(weights='imagenet', include_top=False, input_tensor=input_tensor) print('DenseNet121 loaded') elif arch == 'NASNetLarge': model = applications.NASNetLarge(weights='imagenet', include_top=False, input_tensor=input_tensor) print('NASNetLarge loaded') elif arch == 'MobileNetV2': model = applications.MobileNetV2(input_shape=(224, 224, 3), weights='imagenet', include_top=False, input_tensor=input_tensor) print('MobileNetV2 loaded') else: print('Invalid model selected') model = False return model
def test_nasnet(): model = applications.NASNetMobile(weights=None) assert model.output_shape == (None, 1000) model = applications.NASNetLarge(weights=None) assert model.output_shape == (None, 1000)
def train(train_path, val_path, test_path, batch_size=32, epochs=50, network='InceptionResNetV2', data_augmentation=True, mode='finetune', optimizer='Adadelta', fc=1, classes=5, gpu=1): ''' Inputs: train_path: data path for train set (data should be stored like train/DR, train/Normal) val_path: data path for validation set test_path: data path for test set batch_size: data sizes per step epochs: loop counts over whole train set network: { 'InceptionResNetV2': fine-tune mode will train last 2 inception blocks 'DenseNet201': fine-tune mode will train last Dense block 'InceptionV3': fine-tune mode will train last 2 inception blocks 'Xception' 'NASNet' 'MobileNetV2' 'ResNet50': According to https://arxiv.org/pdf/1805.08974.pdf, it is most suitable for transfer learning? } data_augmentation: whether to do data augmentation or not mode: { 'retrain': randomly initialize all layers and retrain the whole model 'finetune': train specified layers 'transfer' train fc layer(s) } optimizer: { 'Adadelta' 'RMSprop' } fc: { 1: only one fc layer at last 2: include two fc layers at last } classes: category counts ''' if mode == 'retrain': include_top = False weights = None pooling = 'avg' else: include_top = False weights = 'imagenet' pooling = 'avg' if network == 'DenseNet201': from keras.applications.densenet import preprocess_input img_width, img_height = 224, 224 base_model = applications.DenseNet201(include_top=include_top, weights=weights, pooling=pooling) # train last Dense Block if mode == 'finetune': trainable = False for layer in base_model.layers: if layer.name == 'conv5_block1_0_bn': trainable = True layer.trainable = trainable if network == 'Xception': from keras.applications.xception import preprocess_input img_width, img_height = 299, 299 base_model = applications.Xception(include_top=include_top, weights=weights, pooling=pooling) if network == 'InceptionV3': from keras.applications.inception_v3 import preprocess_input img_width, img_height = 299, 299 base_model = applications.InceptionV3(include_top=include_top, weights=weights, pooling=pooling) # train top 2 inception blocks if mode == 'finetune': for layer in base_model.layers[:249]: layer.trainable = False for layer in base_model.layers[249:]: #print(layer.name) layer.trainable = True if network == 'InceptionResNetV2': from keras.applications.inception_resnet_v2 import preprocess_input img_width, img_height = 299, 299 base_model = applications.InceptionResNetV2(include_top=include_top, weights=weights, pooling=pooling) # train top 1 inception blocks if mode == 'finetune': trainable = True for layer in base_model.layers: #print(layer.name) if layer.name == 'conv2d_9': trainable = False if layer.name == 'conv2d_201': trainable = True layer.trainable = trainable if network == 'NASNet': from keras.applications.nasnet import preprocess_input img_width, img_height = 331, 331 base_model = applications.NASNetLarge(include_top=include_top, weights=weights, pooling=pooling) if network == 'MoblieNetV2': from keras.applications.mobilenetv2 import preprocess_input img_width, img_height = 224, 224 base_model = applications.MobileNetV2(include_top=include_top, weights=weights, pooling=pooling) if network == 'ResNet50': from keras.applications.resnet50 import preprocess_input img_width, img_height = 224, 224 base_model = applications.ResNet50(include_top=include_top, weights=weights, pooling=pooling) bottleneck = base_model.output if fc == 2: bottleneck = Dense( 512, activation='relu', kernel_regularizer=keras.regularizers.l2(l=0.001))(bottleneck) predictions = Dense( classes, kernel_regularizer=keras.regularizers.l2(l=0.001), activation='softmax', bias_regularizer=keras.regularizers.l2(l=0.001))(bottleneck) model = Model(inputs=base_model.input, outputs=predictions) if mode == 'transfer': # train only the top layers (which were randomly initialized) # freeze all convolutional layers for layer in base_model.layers: layer.trainable = False if mode == 'retrain': # train a complete model for layer in base_model.layers: layer.trainable = True if optimizer == 'Adadelta': opt = optimizers.Adadelta() if optimizer == 'Adam': opt = optimizers.Adam() if optimizer == 'RMSprop': opt = optimizers.RMSprop(lr=0.005, rho=0.9, epsilon=1.0, decay=0.94) if gpu > 1: batch_size *= gpu model = multi_gpu_model(model, gpus=gpu) model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy']) if data_augmentation: # Initialize the train and test generators with data Augumentation train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, horizontal_flip=True, fill_mode="nearest", zoom_range=0.3, width_shift_range=0.3, height_shift_range=0.3, rotation_range=30) val_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) else: train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) val_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) train_generator = train_datagen.flow_from_directory( train_path, target_size=(img_height, img_width), batch_size=batch_size, class_mode="categorical") validation_generator = val_datagen.flow_from_directory( val_path, target_size=(img_height, img_width), class_mode="categorical") test_generator = val_datagen.flow_from_directory(test_path, target_size=(img_height, img_width), class_mode="categorical") checkpoint = ModelCheckpoint("{}_{}_{}.h5".format(network, mode, optimizer), monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) early = EarlyStopping(monitor='val_acc', min_delta=0, patience=10, verbose=1, mode='auto') model.fit_generator(train_generator, epochs=epochs, validation_data=validation_generator, callbacks=[checkpoint, early]) score = model.evaluate_generator(test_generator) print(score)
input_shape=(256, 256, 3)) elif args.base_model == 'densenet169': base_model = applications.DenseNet169(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'densenet201': base_model = applications.DenseNet201(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'nasnetmobile': base_model = applications.NASNetMobile(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) elif args.base_model == 'nasnetlarge': base_model = applications.NASNetLarge(weights='imagenet', include_top=False, input_shape=(256, 256, 3)) else: raise ValueError( "Model you entered is not present in the model zoo thats offered") if args.bottleneck_tensorname is None: # Taking the last tensor (Just before the softmax layer) BOTTLENECK_TENSOR_NAME = base_model.layers[-1].name else: BOTTLENECK_TENSOR_NAME = args.bottleneck_tensorname ## load data LABEL_LENGTH = len(glob(args.train + '/*')) # BOTTLENECK_TENSOR_NAME = 'activation_31'