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 if args.mode != "predict": base_model = NASNetLarge(weights='imagenet', include_top=True, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "NASNetMobile": from keras.applications.nasnet import preprocess_input preprocessing_function = preprocess_input if args.mode != "predict": base_model = NASNetMobile(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) else: ValueError("The model you requested is not supported in Keras") if args.mode == "train": print("\n***** Begin training *****") print("Dataset -->", DATASET_NAME) print("Model -->", args.model) print("Resize Height -->", args.resize_height) print("Resize Width -->", args.resize_width) print("Num Epochs -->", args.num_epochs) print("Batch Size -->", args.batch_size) print("Data Augmentation:") print("\tVertical Flip -->", args.v_flip)
y_train3 = [] for i in range(len(predictions)): if predictions[i] > 0.5 and y_train[i] == 0: x_train3.append(X_train[i]) y_train3.append(y_train[i]) elif predictions[i] <= 0.5 and y_train[i] == 1: x_train3.append(X_train[i]) y_train3.append(y_train[i]) while (len(x_train3) <= len(X_train)): index = int(random.uniform(0, len(X_train))) x_train3.append(X_train[index]) y_train3.append(y_train[index]) x_train3 = np.array(x_train3) y_train3 = np.array(y_train3) base_model = NASNetMobile(weights='imagenet', include_top=False, input_shape=(dim, dim, 3)) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) #x = Flatten()(x) # let's add a fully-connected layer x = Dropout(0.25)(x) x = Dense(100, activation='relu')(x) x = Dropout(0.25)(x) #x = Dense(2, activation='softmax') # and a logistic layer -- let's say we have 200 classes predictions = Dense(1, activation='sigmoid')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions)
def get_test_neural_net(type): model = None if type == 'mobilenet_small': from keras.applications.mobilenet import MobileNet model = MobileNet((128, 128, 3), depth_multiplier=1, alpha=0.25, include_top=True, weights='imagenet') elif type == 'mobilenet': from keras.applications.mobilenet import MobileNet model = MobileNet((224, 224, 3), depth_multiplier=1, alpha=1.0, include_top=True, weights='imagenet') elif type == 'mobilenet_v2': from keras.applications.mobilenetv2 import MobileNetV2 model = MobileNetV2((224, 224, 3), depth_multiplier=1, alpha=1.4, include_top=True, weights='imagenet') elif type == 'resnet50': from keras.applications.resnet50 import ResNet50 model = ResNet50(input_shape=(224, 224, 3), include_top=True, weights='imagenet') elif type == 'inception_v3': from keras.applications.inception_v3 import InceptionV3 model = InceptionV3(input_shape=(299, 299, 3), include_top=True, weights='imagenet') elif type == 'inception_resnet_v2': from keras.applications.inception_resnet_v2 import InceptionResNetV2 model = InceptionResNetV2(input_shape=(299, 299, 3), include_top=True, weights='imagenet') elif type == 'xception': from keras.applications.xception import Xception model = Xception(input_shape=(299, 299, 3), include_top=True, weights='imagenet') elif type == 'densenet121': from keras.applications.densenet import DenseNet121 model = DenseNet121(input_shape=(224, 224, 3), include_top=True, weights='imagenet') elif type == 'densenet169': from keras.applications.densenet import DenseNet169 model = DenseNet169(input_shape=(224, 224, 3), include_top=True, weights='imagenet') elif type == 'densenet201': from keras.applications.densenet import DenseNet201 model = DenseNet201(input_shape=(224, 224, 3), include_top=True, weights='imagenet') elif type == 'nasnetmobile': from keras.applications.nasnet import NASNetMobile model = NASNetMobile(input_shape=(224, 224, 3), include_top=True, weights='imagenet') elif type == 'nasnetlarge': from keras.applications.nasnet import NASNetLarge model = NASNetLarge(input_shape=(331, 331, 3), include_top=True, weights='imagenet') elif type == 'vgg16': from keras.applications.vgg16 import VGG16 model = VGG16(input_shape=(224, 224, 3), include_top=False, pooling='avg', weights='imagenet') elif type == 'vgg19': from keras.applications.vgg19 import VGG19 model = VGG19(input_shape=(224, 224, 3), include_top=False, pooling='avg', weights='imagenet') elif type == 'multi_io': model = get_custom_multi_io_model() elif type == 'multi_model_layer_1': model = get_custom_model_with_other_model_as_layer() elif type == 'multi_model_layer_2': model = get_small_model_with_other_model_as_layer() return model
# # # save features for VGG16 at 3 different input scales # from keras.applications.vgg16 import VGG16 # from keras.applications.vgg16 import preprocess_input # model = VGG16(weights='imagenet', include_top=False) # # for n in [224,128,64]: # input_shape = (n,n,3) # new_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape)) # features = utils.features_from_image(all_logos, model, new_preprocess) # utils.save_features('vgg16_logo_features_{}.hdf5'.format(n), features, brand_map, input_shape) from keras.applications.nasnet import NASNetMobile from keras.applications.nasnet import preprocess_input model_out = NASNetMobile(weights='imagenet', include_top=False) input_shape = (224, 224, 3) new_preprocess = lambda x: preprocess_input(utils.pad_image( x, input_shape)) features = utils.features_from_image(all_logos, model, new_preprocess) utils.save_features('NASNet_logo_features_{}.hdf5'.format(224), features, brand_map, input_shape) # from keras.applications.nasnet import NASNetLarge # from keras.applications.nasnet import preprocess_input # model_out = NASNetLarge(weights='imagenet', include_top=False) # input_shape = (331, 331, 3) # # new_preprocess = lambda x: preprocess_input(utils.pad_image(x, input_shape)) # features = utils.features_from_image(all_logos, model, new_preprocess)
image_size = 224 model_name = "nasnet.h5" test_file_stats = "testmetrics_nasnet.txt" ############################## def auc(y_true, y_pred): auc = tf.metrics.auc(y_true, y_pred)[1] K.get_session().run(tf.local_variables_initializer()) return auc #DECLARE MODEL weights='imagenet', include_top=false... base_model = NASNetMobile( weights='imagenet', include_top=False, input_shape=( image_size, image_size, 3)) #imports the resnet model and discards the last 1000 neuron layer. x = base_model.output #x=GlobalAveragePooling2D()(x) x = Flatten()(x) #x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results. x = Dense(1024, activation='relu')(x) #dense layer 2 x = Dropout(0.5)(x) #x=Dense(512,activation='relu')(x) #dense layer 3 preds = Dense(2, activation='sigmoid')(x) #final layer with sigmoid activation #specify the inputs #specify the outputs model = Model(inputs=base_model.input, outputs=preds)
def InitialiazeModel(head_only,weights,model_name,lr): if model_name == 'VGG19': from keras.applications.vgg19 import VGG19 base_model = VGG19(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'VGG16': from keras.applications.vgg16 import VGG16 base_model = VGG16(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'MobileNet': from keras.applications.mobilenet import MobileNet base_model = MobileNet(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'ResNet50': from keras.applications.resnet50 import ResNet50 base_model = ResNet50(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'Xception': from keras.applications.xception import Xception base_model = Xception(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'InceptionV3': from keras.applications.inception_v3 import InceptionV3 base_model = InceptionV3(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'InceptionResNetV2': from keras.applications.inception_resnet_v2 import InceptionResNetV2 base_model = InceptionResNetV2(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'NASNetLarge': from keras.applications.nasnet import NASNetLarge base_model = NASNetLarge(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') elif model_name == 'NASNetMobile': from keras.applications.nasnet import NASNetMobile base_model = NASNetMobile(include_top=False, weights='imagenet', input_shape=(PICS_WIDTH, PICS_HEIGHT, 3), pooling = 'avg') else: raise ValueError('Network name is undefined') if head_only: for lay in base_model.layers: lay.trainable = False #manipulated = Input(shape=(1,)) x = base_model.output #x = concatenate([x, manipulated]) x = Dense(NUM_CATEGS, activation='softmax', name='predictions')(x) #model = Model([base_model.input,manipulated], x) model = Model(base_model.input, x) #print(model.summary()) if weights != '': model.load_weights(weights) MODEL_OPTIMIZER = optimizers.SGD(lr=lr, momentum=0.9, nesterov=True) model.compile(loss=MODEL_LOSS, optimizer=MODEL_OPTIMIZER, metrics=[MODEL_METRIC]) return model
def Load_NasNetMobile(self): return NASNetMobile(weights='imagenet', include_top=False), set[ActiveModel]
def chosen_model(choice): global base_model if choice == 19: model_exist() else: while (1): print() print( 'Transfer Learning? - Will use pre-trained model with imagenet weights' ) print('y') print('n') weights_wanted = input() if weights_wanted.upper() != 'Y' and weights_wanted.upper() != 'N': print('ERROR: Please enter a valid choice') else: break if choice == 1: print('Selected Model = Xception') if weights_wanted.upper() == 'Y': base_model = Xception(weights='imagenet', include_top=False) else: base_model = Xception(weights=None, include_top=False) if choice == 2: print('Selected Model = VGG16') if weights_wanted.upper() == 'Y': base_model = VGG16(weights='imagenet', include_top=False) else: base_model = VGG16(weights=None, include_top=False) if choice == 3: print('Selected Model = VGG19') if weights_wanted.upper() == 'Y': base_model = VGG19(weights='imagenet', include_top=False) else: base_model = VGG19(weights=None, include_top=False) if choice == 4: print('Selected Model = ResNet50') if weights_wanted.upper() == 'Y': base_model = ResNet50(weights='imagenet', include_top=False) else: base_model = ResNet50(weights=None, include_top=False) if choice == 5: print('Selected Model = ResNet101') if weights_wanted.upper() == 'Y': base_model = ResNet101(weights='imagenet', include_top=False) else: base_model = ResNet101(weights=None, include_top=False) if choice == 6: print('Selected Model = ResNet152') if weights_wanted.upper() == 'Y': base_model = ResNet152(weights='imagenet', include_top=False) else: base_model = ResNet152(weights=None, include_top=False) if choice == 7: print('Selected Model = ResNet50V2') if weights_wanted.upper() == 'Y': base_model = ResNet50V2(weights='imagenet', include_top=False) else: base_model = ResNet50V2(weights=None, include_top=False) if choice == 8: print('Selected Model = ResNet101V2') if weights_wanted.upper() == 'Y': base_model = ResNet101V2(weights='imagenet', include_top=False) else: base_model = ResNet101V2(weights=None, include_top=False) if choice == 9: print('Selected Model = ResNet152V2') if weights_wanted.upper() == 'Y': base_model = ResNet152V2(weights='imagenet', include_top=False) else: base_model = ResNet152V2(weights=None, include_top=False) if choice == 10: print('Selected Model = InceptionV3') if weights_wanted.upper() == 'Y': base_model = InceptionV3(weights='imagenet', include_top=False) else: base_model = InceptionV3(weights=None, include_top=False) if choice == 11: print('Selected Model = InceptionResNetV2') if weights_wanted.upper() == 'Y': base_model = InceptionResNetV2(weights='imagenet', include_top=False) else: base_model = InceptionResNetV2(weights=None, include_top=False) if choice == 12: print('Selected Model = MobileNet') if weights_wanted.upper() == 'Y': base_model = MobileNet(weights='imagenet', include_top=False) else: base_model = MobileNet(weights=None, include_top=False) if choice == 13: print('Selected Model = MobileNetV2') if weights_wanted.upper() == 'Y': base_model = MobileNetV2(weights='imagenet', include_top=False) else: base_model = MobileNetV2(weights=None, include_top=False) if choice == 14: print('Selected Model = DenseNet121') if weights_wanted.upper() == 'Y': base_model = DenseNet121(weights='imagenet', include_top=False) else: base_model = DenseNet121(weights=None, include_top=False) if choice == 15: print('Selected Model = DenseNet169') if weights_wanted.upper() == 'Y': base_model = DenseNet169(weights='imagenet', include_top=False) else: base_model = DenseNet169(weights=None, include_top=False) if choice == 16: print('Selected Model = DenseNet201') if weights_wanted.upper() == 'Y': base_model = DenseNet201(weights='imagenet', include_top=False) else: base_model = DenseNet201(weights=None, include_top=False) if choice == 17: print('Selected Model = NASNetLarge') if weights_wanted.upper() == 'Y': base_model = NASNetLarge(weights='imagenet', include_top=False) else: base_model = NASNetLarge(weights=None, include_top=False) if choice == 18: print('Selected Model = NASNetMobile') if weights_wanted.upper() == 'Y': base_model = NASNetMobile(weights='imagenet', include_top=False) else: base_model = NASNetMobile(weights=None, include_top=False) CLASSES = len(os.listdir('data/train')) print('Number of Classes = {}'.format(CLASSES)) x = base_model.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(0.4)(x) predictions = Dense(CLASSES, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) for layer in base_model.layers: layer.trainable = False model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) training(model)
def train(job_id, args): batch_size = args.batchsize lr = args.lr momentum = args.momentum # dimensions of our images. img_width, img_height = args.imgwidth, args.imgheight train_data_dir = os.path.join(args.basedir, args.dataset, 'train') validation_data_dir = os.path.join(args.basedir, args.dataset, 'validation') nb_train_samples = args.numoftrainings nb_validation_samples = args.numofvalidations epochs = MAX_EPOCH if args.dataset == 'food101': classes = 101 elif args.dataset == 'food172': classes = 172 elif args.dataset == 'food191': classes = 191 elif args.dataset == 'uec100': classes = 100 elif args.dataset == 'uec256': classes = 256 else: # aggregated dataset classes = 668 if args.model == 'xception': # 299, 299, 3 base_model = Xception(include_top=True, weights='imagenet', input_shape=(img_height, img_width, 3)) elif args.model == 'inceptionresnet': # 299, 299, 3 base_model = InceptionResNetV2(include_top=True, weights='imagenet', input_shape=(img_height, img_width, 3)) elif args.model == 'nasnetlarge': # 331, 331, 3 base_model = NASNetLarge(include_top=True, weights='imagenet', input_shape=(img_height, img_width, 3)) elif args.model == 'nasnetmobile': # 224, 224, 3 base_model = NASNetMobile(include_top=True, weights='imagenet', input_shape=(img_height, img_width, 3)) elif args.model == 'densenet169': # 224, 224, 3 base_model = DenseNet169(weights='imagenet', input_shape=(img_height, img_width, 3)) elif args.model == 'se-resnet': base_model = SEResNet101(weights='imagenet') elif args.model == 'se-inceptionresnet': base_model = SEInceptionResNetV2(weights='imagenet') elif args.model == 'shufflenet': base_model = ShuffleNet(input_shape=(img_height, img_width, 3), groups=args.group, bottleneck_ratio=args.bn_ratio) elif args.model == 'mobilenet': # 224, 224, 3 base_model = MobileNet(input_shape=(img_height, img_width, 3), weights='imagenet', alpha=args.alpha) elif args.model == 'mobilenetv2': # 224, 224, 3 base_model = MobileNetV2(input_shape=(img_height, img_width, 3), weights='imagenet', alpha=args.alpha) elif args.model == 'dpn': base_model = DPN92((img_height, img_width, 3), weights='imagenet') elif args.model == 'resnet50': base_model = ResNet50(include_top=True, weights='imagenet', input_shape=(img_height, img_width, 3)) elif args.model == 'resnet152': base_model = ResNet152( include_top=True, weights=(args.pretrain_weights if args.pretrain_weights else None), input_shape=(img_height, img_width, 3)) elif args.model == 'resnext': base_model = ResNext(include_top=True, input_shape=(img_height, img_width, 3)) else: base_model = None base_model.layers.pop() if args.model == 'mobilenet': base_model.layers.pop() base_model.layers.pop() x = Conv2D(classes, (1, 1), padding='same', name='conv_preds')(base_model.layers[-1].output) x = Activation('softmax', name='act_softmax')(x) predictions = Reshape((classes, ), name='reshape_2')(x) elif args.model == 'shufflenet': base_model.layers.pop() predictions = Dense(classes, activation='softmax')(base_model.layers[-1].output) else: predictions = Dense(classes, activation='softmax')(base_model.layers[-1].output) model = Model(input=base_model.input, output=[predictions]) # fine-tune model with previous weights if args.finetune_weights != '': model.load_weights(args.finetune_weights) model.summary() if float(args.weight_decay) != 0: for layer in model.layers: if hasattr(layer, 'kernel_regularizer'): layer.kernel_regularizer = regularizers.l2(args.weight_decay) if hasattr(layer, 'bias_regularizer'): layer.bias_regularizer = regularizers.l2(args.weight_decay) for layer in model.layers: if hasattr(layer, 'kernel_regularizer'): print(layer.name, layer.kernel_regularizer.l2) sgd = optimizers.SGD(lr=lr, momentum=momentum, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) # store model architecture # model_json = model.to_json() # with open("vgg19_nolastpooling_model_256.json", "w") as json_file: # json_file.write(model_json) # this is the augmentation configuration we will use for training if 'dpn' in args.model or 'resne' in args.model: # or 'shufflenet' in args.model: train_datagen = ImageDataGenerator( preprocessing_function=preprocess_input, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) test_datagen = ImageDataGenerator( preprocessing_function=preprocess_input) else: train_datagen = ImageDataGenerator(rescale=1. / 255, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True) # this is the augmentation configuration we will use for testing: # only rescaling test_datagen = ImageDataGenerator(rescale=1. / 255) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') # store class indices print train_generator.class_indices np.save('class_indices/' + args.dataset + '.npy', train_generator.class_indices) validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical') # print K.get_value(model.optimizer.lr) if args.checkpoint == '': prefix = args.basedir + '/' + args.dataset + '/models/' + args.model else: prefix = args.checkpoint + '/' + args.model if 'mobilenet' in args.model: prefix += '-' + str(args.alpha) callbacks = [ #EarlyStopping(monitor='val_loss', patience=5, verbose=1), ModelCheckpoint(prefix + '-{epoch:02d}-{val_acc:.2f}.h5', monitor='val_loss', save_best_only=True, save_weights_only=True, verbose=1), CSVLogger(prefix + '_log.csv', append=True, separator=','), # LearningRateScheduler(get_lr), LearningRateReducer(patience=5, reduce_rate=0.1, reduce_nb=3), ] if args.initialepoch == -1: train_history = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size, callbacks=callbacks) else: # resume training train_history = model.fit_generator( train_generator, steps_per_epoch=nb_train_samples // batch_size, epochs=epochs, validation_data=validation_generator, validation_steps=nb_validation_samples // batch_size, callbacks=callbacks, initial_epoch=args.initialepoch) loss = train_history.history['loss'] return min(loss)
sometimes( iaa.ElasticTransformation(alpha=(0.5, 3.5), sigma=0.25) ), # move pixels locally around (with random strengths) sometimes(iaa.PiecewiseAffine(scale=( 0.01, 0.05))), # sometimes move parts of the image around sometimes(iaa.PerspectiveTransform(scale=(0.01, 0.1))) ], random_order=True) ], random_order=True) return seq train = CreateModel(NASNetMobile(input_shape=(96, 96, 3), classes=2, include_top=False), batch_size=256, size=(96, 96, 3), batch_size_val=256, save_model="models/dataset-3/", name_model="nasnet_02_2", multi_check=False, class_mode="categorical", epochs=30, imagenet="models/dataset-3/nasnet_02/model.hdf5") train.set_train_generator( "/home/yannis/Developpement/ppd-GAN-for-medical-imaging/data/dataset-3/split_train/train" ) train.set_val_generator( "/home/yannis/Developpement/ppd-GAN-for-medical-imaging/data/dataset-3/split_train/val"
#model from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D, Average, Input, Concatenate, GlobalMaxPooling2D from keras.applications.xception import Xception from keras.applications.nasnet import NASNetMobile from keras.models import Model from keras.optimizers import Adam from keras.utils.vis_utils import plot_model MODEL_PLOT_FILE = "model_plot.png" IMAGE_SIZE = 224 input_shape = (IMAGE_SIZE, IMAGE_SIZE, 3) inputs = Input(input_shape) xception = Xception(include_top=False, input_shape=input_shape)(inputs) nas_net = NASNetMobile(include_top=False, input_shape=input_shape)(inputs) outputs = Concatenate(axis=-1)( [GlobalAveragePooling2D()(xception), GlobalAveragePooling2D()(nas_net)]) outputs = Dropout(0.5)(outputs) outputs = Dense(1, activation='sigmoid')(outputs) model = Model(inputs, outputs) model.compile(optimizer=Adam(lr=0.0001, decay=0.00001), loss='binary_crossentropy', metrics=['accuracy']) model.summary() plot_model(model, to_file=MODEL_PLOT_FILE, show_shapes=True, show_layer_names=True)
def nasnet_mobile(weights=nasnet_path): weights = weights pretrained_model = NASNetMobile(weights=weights, include_top=False) return (pretrained_model)
from datasequence import DataSequence import pandas as pd MODEL_NAME = "nasnet_lamem_model.h5" # This is a custom loss function (Euclidean Loss) that was used in the MemNet paper, and is specialized for regression. def euc_dist_keras(y_true, y_pred): return K.sqrt(K.sum(K.square(y_true - y_pred), axis=-1, keepdims=True)) # Here, we initialize the "NASNetMobile" model type and customize the final feature regressor layer. # NASNet is a neural network architecture developed by Google. # This architecture is specialized for transfer learning, and was discovered via Neural Architecture Search. # NASNetMobile is a smaller version of NASNet. model = NASNetMobile() model = Model( model.input, Dense(1, activation='linear', kernel_initializer='normal')(model.layers[-2].output)) # This model will use the "Adam" optimizer. model.compile("adam", euc_dist_keras) model.summary() # Here, we read the label files provided with the LaMem dataset. train_pd = pd.read_csv("splits/train_1.txt") test_pd = pd.read_csv("splits/test_1.txt") # The batch size is set to 32.
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
print([x_train.shape,x_valid.shape,x_test.shape, y_train.shape,y_valid.shape,y_test.shape]) del images,labels,cat_labels # creating bottleneck features x_train=np.array([misc.imresize(x_train[i],(224,224,3)) \ for i in range(0,len(x_train))]).astype('float32') x_valid=np.array([misc.imresize(x_valid[i],(224,224,3)) \ for i in range(0,len(x_valid))]).astype('float32') x_test=np.array([misc.imresize(x_test[i],(224,224,3)) \ for i in range(0,len(x_test))]).astype('float32') x_train=nnpi(x_train) x_valid=nnpi(x_valid) x_test=nnpi(x_test) fn="../input/keras-applications-weights/nasnet_mobile_no_top.h5" nasnet_base_model=NASNetMobile(weights=fn,include_top=False) x_train=nasnet_base_model.predict(x_train) x_valid=nasnet_base_model.predict(x_valid) x_test=nasnet_base_model.predict(x_test) def nasnet_model(): model=Sequential() model.add(GlobalAveragePooling2D(input_shape=x_train.shape[1:])) model.add(Dense(2048)) model.add(LeakyReLU(alpha=.02)) model.add(Dropout(.5)) model.add(Dense(256)) model.add(LeakyReLU(alpha=.02)) model.add(Dropout(.5)) model.add(Dense(33,activation='softmax')) model.compile(loss='categorical_crossentropy',
def Build_model(self): ############# Feature Extraction Network ################## if self.Backbone_model == "VGG16": Backbone = VGG16(input_shape=(352,352,3), include_top=False, weights='imagenet') Stages=[Backbone.get_layer("block5_conv3").output, Backbone.get_layer("block4_conv3").output, Backbone.get_layer("block3_conv3").output] # # # # # # # # # # # # # # # # # # # # # # # # # # # # elif self.Backbone_model == "ResNet50": Backbone = ResNet50(input_shape=(352,352,3), include_top=False, weights='imagenet') Stages=[Backbone.output, Backbone.get_layer("conv4_block6_out").output, Backbone.get_layer("conv3_block4_out").output, Backbone.get_layer("conv2_block3_out").output] # # # # # # # # # # # # # # # # # # # # # # # # # # # # elif self.Backbone_model == "NASNetMobile": Backbone = NASNetMobile(input_shape=(352,352,3), include_top=False, weights=None) if os.path.exists('./NASNet-mobile-no-top.h5'): pass else: urlretrieve('https://github.com/titu1994/Keras-NASNet/releases/download/v1.2/NASNet-mobile-no-top.h5', './NASNet-mobile-no-top.h5') Backbone.load_weights('./NASNet-mobile-no-top.h5') Stages=[Backbone.output, Backbone.get_layer("activation_131").output, Backbone.get_layer("activation_72").output, Backbone.get_layer("activation_13").output] # # # # # # # # # # # # # # # # # # # # # # # # # # # # elif self.Backbone_model == "NASNetLarge": Backbone = NASNetLarge(input_shape=(352,352,3), include_top=False, weights=None) if os.path.exists('./NASNet-large-no-top.h5'): pass else: urlretrieve('https://github.com/titu1994/Keras-NASNet/releases/download/v1.2/NASNet-large-no-top.h5', './NASNet-large-no-top.h5') Backbone.load_weights('./NASNet-large-no-top.h5') Stages=[Backbone.output, Backbone.get_layer("activation_319").output, Backbone.get_layer("activation_260").output, Backbone.get_layer("activation_201").output] # # # # # # # # # # # # # # # # # # # # # # # # # # # # else: raise ValueError("The name of the Backbone_model must be one of the following options: VGG16, ResNet50, NASNetMobile, NASNetLarge") # # # # # # # # # # # # # # # # # # # # # # # # # # # # ExtractedFeatures = [] Number_of_Filters = [192,128,96,64] for Stage,Num in zip(Stages,Number_of_Filters): MAG_output=MAG_Module(Stage, Num) extr=Conv2D(Num, (1, 1), padding='same')(MAG_output) ExtractedFeatures.append(extr) #Extracted features from the Feature Extraction Network ############### Feature Integration Network ################## z = BilinearUpsampling(output_size=(ExtractedFeatures[0]._keras_shape[1]*2, ExtractedFeatures[0]._keras_shape[2]*2))(ExtractedFeatures[0]) for i in range(len(ExtractedFeatures)-1): z = AMI_Module(z, ExtractedFeatures[i+1], ExtractedFeatures[i+1]._keras_shape[-1]) z = BilinearUpsampling(output_size=(z._keras_shape[1]*2,z._keras_shape[2]*2))(z) z = Conv2D(64, (3, 3), padding='same', strides=(1,1))(z) z = BatchNormalization(axis=-1)(z) z = Activation('relu')(z) z = BilinearUpsampling(output_size=(352,352))(z) z = Conv2D(2, (1, 1), padding='same')(z) z = Activation('softmax')(z) self.model = Model(Backbone.input,z) self.model.compile(optimizer=SGD(lr=self.learning_rate, momentum=0.9), loss=Sharpenning_Loss(self.Lambda), metrics=["accuracy"]) if self.show_ModelSummary == True: self.model.summary()
y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) if args.resume: print('resume from checkpoint') message = job_name + ' b_end' send_signal.send(args.node, 10002, message) model = keras.models.load_model(save_file) message = job_name + ' c_end' send_signal.send(args.node, 10002, message) else: print('train from start') model = models.Sequential() base_model = NASNetMobile(weights=None, include_top=False, input_shape=(32, 32, 3), pooling=None) #base_model.summary() #pdb.set_trace() 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)) #model.add(layers.BatchNormalization())
class Model_Nas: def __init__(self, args, load=False): self.ckpt = args.pre_train self.model = "NasNet" self.args = args self.class_num = args.class_num self.lr = args.lr self.epoch = args.epoch self.c = args.n_color self.is_online = args.online self.batch_size = args.batch_size self.save_dir = args.save self.sess = None self.is_test = load # self.mode = args.processing_mode self.callbacks = [] self.init_callbacks() def init_callbacks(self): self.callbacks.append( ModelCheckpoint(filepath=self.save_dir + self.model + '_best_weights.h5', verbose=1, monitor='val_categorical_accuracy', mode='auto', save_best_only=True)) self.callbacks.append( TensorBoard( log_dir=self.args.save, write_images=True, write_graph=True, )) self.callbacks.append( EarlyStopping( # patience=self.args.early_stopping patience=1000)) # self.callbacks.append( # ReduceLROnPlateau( # monitor='val_loss', # factor=0.5, # patience=5, # min_lr=1e-5 # ) # ) def custom_schedule(epochs): if epochs <= 5: lr = 1e-3 elif epochs <= 50: lr = 5e-4 elif epochs <= 100: lr = 2.5e-4 elif epochs <= 500: lr = 1e-4 elif epochs <= 700: lr = 5e-5 else: lr = 1e-5 return lr self.callbacks.append(LearningRateScheduler(custom_schedule)) def train(self, training_images, training_labels, validation_images, validation_labels): self.model = NASNetMobile(classes=2, include_top=True, weights=None) self.model.trainable = True self.model.compile(optimizer=Adam(lr=0.0001, beta_1=0.1), loss='categorical_crossentropy', metrics=['categorical_accuracy']) train_datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') val_datagen = ImageDataGenerator(rotation_range=40, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode='nearest') steps = int(np.size(training_images, 0) // self.batch_size) val_steps = int(np.size(validation_images, 0) // self.batch_size) self.model.fit_generator( generator=train_datagen.flow(x=training_images, y=training_labels, batch_size=self.batch_size), epochs=self.args.epoch, steps_per_epoch=steps, validation_steps=val_steps, verbose=1, callbacks=self.callbacks, validation_data=val_datagen.flow(x=validation_images, y=validation_labels, batch_size=self.batch_size))