from keras.models import Sequential from keras.layers import Dense, Activation, Dropout, Flatten, Conv2D, MaxPooling2D from keras.layers.normalization import BatchNormalization import numpy as np from keras.models import Sequential from keras.models import Model from keras.callbacks import ModelCheckpoint, LearningRateScheduler, EarlyStopping, ReduceLROnPlateau, TensorBoard from keras import optimizers, losses, activations, models from keras.layers import Convolution2D, Dense, Input, Flatten, Dropout, MaxPooling2D, BatchNormalization, GlobalAveragePooling2D, Concatenate from keras import applications input_shape = (150, 300, 3) nclass = 9 base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(150, 300, 3)) base_model.trainable = True add_model = Sequential() add_model.add(base_model) add_model.add(GlobalAveragePooling2D()) add_model.add(Dropout(0.5)) add_model.add(Dense(4096 * 2, activation='relu')) add_model.add(Dense(4096 * 2, activation='relu')) add_model.add(Dense(4096 * 2, activation='relu')) add_model.add(Dense(4096, activation='relu'))
def mySpatialModelChannelTest(model_name,spatial_size, nb_classes, channels, channel_first=True, weights_path=None, lr=0.005, decay=1e-6, momentum=0.9,plot_model=True): input_tensor = Input(shape=(channels, spatial_size, spatial_size)) input_shape = (channels, spatial_size, spatial_size) base_model=None predictions=None data_dim=1024 base_model = kerasApp.ResNet50(include_top=False, input_tensor=input_tensor, input_shape=input_shape, weights=None, classes=nb_classes, pooling=None) x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(nb_classes, activation='softmax')(x) # 训练模型 model = Model(inputs=base_model.input, outputs=predictions) print_shape(model,model_name) base_model = kerasApp.VGG16(include_top=False, input_tensor=input_tensor, input_shape=input_shape, weights=None, classes=nb_classes, pooling=None) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) # add a global spatial average pooling layer x = Dense(1024, activation='relu')(x) # let's add a fully-connected layer predictions = Dense(nb_classes, activation='softmax')(x) # 训练模型 model = Model(inputs=base_model.input, outputs=predictions) print_shape(model, model_name) base_model = kerasApp.VGG19(include_top=False, input_tensor=input_tensor, input_shape=input_shape, weights=None, classes=2, pooling='avg') print_shape(base_model, model_name) base_model = kerasApp.InceptionV3(weights=None, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) print_shape(base_model, model_name) base_model = kerasApp.InceptionResNetV2(weights=None, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) predictions = Dense(nb_classes, activation='softmax')(x) # 训练模型 model = Model(inputs=base_model.input, outputs=predictions) print_shape(model, model_name) #channel last input_tensor_Xception = Input(shape=( spatial_size, spatial_size,channels)) input_shape__Xception = (spatial_size, spatial_size,channels) base_model = kerasApp.Xception(weights=None, include_top=False, pooling=None, input_shape=input_shape__Xception, classes=nb_classes) print_shape(base_model, model_name) base_model = kerasApp.DenseNet121(weights=None, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) print_shape(base_model, model_name) base_model = kerasApp.DenseNet169(weights=None, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) print_shape(base_model, model_name) base_model = kerasApp.DenseNet201(weights=None, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) print_shape(base_model, model_name) input_shape = (channels, spatial_size, spatial_size) base_model = kerasApp.MobileNet(weights=None, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes)
def test_inceptionv3_notop(): model = applications.InceptionV3(weights=None, include_top=False) assert model.output_shape == (None, None, None, 2048)
from keras.models import Sequential, Model from keras.layers import Dropout, Flatten, Dense, GlobalAveragePooling2D, Input, Conv2D, MaxPool2D from keras import backend as k from keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping import time img_width, img_height = 256, 256 train_data_dir = "data/train" validation_data_dir = "data/val" nb_train_samples = 129 nb_validation_samples = 21 batch_size = 16 epochs = 50 input_layer = Input(shape=(256, 256, 3)) model = applications.InceptionV3(include_top=False, weights='imagenet', input_tensor=input_layer, pooling=None) """ Layer (type) Output Shape Param # ================================================================= input_1 (InputLayer) (None, 256, 256, 3) 0 _________________________________________________________________ block1_conv1 (Conv2D) (None, 256, 256, 64) 1792 _________________________________________________________________ block1_conv2 (Conv2D) (None, 256, 256, 64) 36928 _________________________________________________________________ block1_pool (MaxPooling2D) (None, 128, 128, 64) 0 _________________________________________________________________ block2_conv1 (Conv2D) (None, 128, 128, 128) 73856 _________________________________________________________________ block2_conv2 (Conv2D) (None, 128, 128, 128) 147584
# data/test/RETARGETTED - RETARGETTED test samples # data/test/NATURAL - natural test samples # data/test/DIBR - DIBR test samples # data/test/SCREENSHOTS - SCREENSHOT test samples img_width, img_height = 139, 139 # Resolution of inputs train_data_dir = "data/sampletrain" # Folder of train samples validation_data_dir = "data/sampletest" # Folder of validation samples nb_train_samples = 10 # Number of train samples nb_validation_samples = 6 # Number of validation samples batch_size = 4 # Batch size epochs = 4 # Maximum number of epochs # Load VGG16 model=applications.InceptionV3(weights="imagenet", include_top=False, input_shape=(img_width, img_height, 3)) # Freeze first 15 layers for layer in model.layers[:45]: layer.trainable = False for layer in model.layers[45:]: layer.trainable = True # Attach additional layers x = model.output x = Flatten()(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) x = Dense(1024, activation="relu")(x) x = Dropout(0.5)(x) predictions = Dense(4, activation="softmax")(x) # 4-way softmax classifier at the end
import numpy as np import keras import tensorflow as tf from keras.utils import multi_gpu_model from keras import models from keras import layers, optimizers from keras import applications import json, os, requests list_of_models = [ applications.MobileNet(weights="imagenet", include_top=False, input_shape=(224, 224, 3)), applications.InceptionV3(weights="imagenet", include_top=False, input_shape=(224, 224, 3)), applications.ResNet50(weights="imagenet", include_top=False, input_shape=(224, 224, 3)), ] def loadModelToZmk(filePath): url = 'http://localhost:8000/api/v1/models' param = {'filePath': filePath} res = requests.post(url, param) res = json.loads(res.text) return res
def main(): """The main function""" args = get_args() # read args if args["fineTuningRate"] != -1: if args["architecture"] == "Xception": model = applications.Xception(weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "VGG16": model = applications.VGG16(weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "VGG19": model = applications.VGG19(weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "ResNet50": model = applications.ResNet50(weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "InceptionV3": model = applications.InceptionV3(weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "MobileNet": model = applications.MobileNet(weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) # calculate how much layers won't be retrained according on fineTuningRate parameter n_layers = len(model.layers) last_layers = n_layers - int(n_layers * (args["fineTuningRate"] / 100.)) for layer in model.layers[:last_layers]: layer.trainable = False else: # without transfer learning if args["architecture"] == "Xception": model = applications.Xception(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "VGG16": model = applications.VGG16(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "VGG19": model = applications.VGG19(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "ResNet50": model = applications.ResNet50(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "InceptionV3": model = applications.InceptionV3(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif args["architecture"] == "MobileNet": model = applications.MobileNet(weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) for layer in model.layers: layer.trainable = True # Initiate the train and test generators with data Augumentation train_datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=True, fill_mode="nearest", zoom_range=0.3, width_shift_range=0.3, height_shift_range=0.3, rotation_range=30) train_generator = train_datagen.flow_from_directory( TRAIN_DATA_DIR, target_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=BATCH_SIZE, shuffle=True, class_mode="categorical") test_datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=True, fill_mode="nearest", zoom_range=0.3, width_shift_range=0.3, height_shift_range=0.3, rotation_range=30) validation_generator = test_datagen.flow_from_directory( VALIDATION_DATA_DIR, target_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=BATCH_SIZE, shuffle=True, class_mode="categorical") # Adding custom Layers new_custom_layers = model.output new_custom_layers = Flatten()(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) new_custom_layers = Dropout(0.5)(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) try: num_classes = train_generator.num_class except: num_classes = train_generator.num_classes predictions = Dense(num_classes, activation="softmax")(new_custom_layers) # creating the final model model_final = Model(inputs=model.input, outputs=predictions) # compile the model model_final.compile(loss="categorical_crossentropy", optimizer=optimizers.SGD(lr=LEARNING_RATE, momentum=0.9), metrics=["accuracy"]) # select .h5 filename if args["fineTuningRate"] == 0: file_name = args["architecture"] + \ '_transfer_learning' elif args["fineTuningRate"] == -1: file_name = args["architecture"] + \ '_without_transfer_learning' else: file_name = args["architecture"] + \ '_fine_tunning_' + str(args["fineTuningRate"]) # Save the model according to the conditions checkpoint = ModelCheckpoint("../models_checkpoints/" + file_name + ".h5", monitor='val_acc', verbose=1, save_best_only=True, save_weights_only=False, mode='auto', period=1) # Train the model model_final.fit_generator( train_generator, steps_per_epoch=train_generator.samples // BATCH_SIZE, epochs=EPOCHS, callbacks=[checkpoint], validation_data=validation_generator, validation_steps=validation_generator.samples // BATCH_SIZE) print "Total time to train: %s" % (time.time() - START_TIME) validation_generator = ImageDataGenerator( rescale=1. / 255).flow_from_directory(VALIDATION_DATA_DIR, batch_size=1, shuffle=False, target_size=(IMG_HEIGHT, IMG_WIDTH), class_mode="categorical") make_confusion_matrix_and_plot(validation_generator, file_name, model_final)
len_train = np.int(np.ceil(len(train_set) * perc_train)) np.random.seed(10) train_idx = np.random.choice(idx, len_train, replace=False) test_idx = np.setdiff1d(idx, train_idx) return (train_idx, test_idx) train_idx, test_idx = sampling_dataset(train_set=list_train_pics, perc_train=0.7) ####Importing all the models#################################################### model_vgg16 = applications.VGG16(include_top=False, weights='imagenet') model_resnet = applications.ResNet50(include_top=False, weights='imagenet') model_xception = applications.Xception(include_top=False, weights='imagenet') model_inception = applications.InceptionV3(include_top=False, weights='imagenet') model_vgg19 = applications.VGG19(include_top=False, weights='imagenet') ###Creating an array of images in train and test and doing preprocessing########## def import_images_in_array(list_train_pics, path, index_file, shape): image_list = [] if len(index_file) > 0: for img_idx in index_file: image_name = list_train_pics[img_idx] temp_img = image.load_img(path + image_name, target_size=shape) temp_img = image.img_to_array(temp_img) image_list.append(temp_img) image_list = np.array(image_list) image_list = image_list / 255.0
intra_op_parallelism_threads=NUM_PARALLEL_EXEC_UNITS, inter_op_parallelism_threads=1 ) session = tf.Session(config=config) K.set_session(session) #MKL and OpenMP os.environ["OMP_NUM_THREADS"] = str(NUM_PARALLEL_EXEC_UNITS) os.environ["KMP_BLOCKTIME"] = "1" os.environ["KMP_SETTINGS"] = "1" os.environ["KMP_AFFINITY"]= "granularity=fine,verbose,compact,1,0" # Initialize InceptionV3 with transfer learning base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(WIDTH, HEIGHT,3)) # add a global spatial average pooling layer x = base_model.output x = GlobalAveragePooling2D()(x) # and a dense layer x = Dense(1024, activation='relu')(x) predictions = Dense(len(train_flow.class_indices), activation='softmax')(x) # this is the model we will train model = Model(inputs=base_model.input, outputs=predictions) # first: train only the top layers (which were randomly initialized) # i.e. freeze all convolutional InceptionV3 layers
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
gains = line.split('\t')[3] # string type val_labels[img_ID] = [float(x) for x in gains.split(' ') ] # convert to float type print('Data is ready. {} sub-images for training, {} for validation.'. format(len(train_img_IDs), len(val_img_IDs))) if NETWORK == 'Hierarchy-1': conv_layers_names = ['conv2d_1'] elif NETWORK == 'Hierarchy-2': conv_layers_names = ['conv2d_1', 'conv2d_2'] elif NETWORK == 'Hierarchy-3': conv_layers_names = ['conv2d_1', 'conv2d_2', 'conv2d_3'] # load pre-trained weights in Inception-V3 inception_model = applications.InceptionV3() # a dictionary records the layer name and layer weights in Inception-V3 inception_layers = { layer.name: layer for layer in inception_model.layers } inception_weights = dict() for layer_name in conv_layers_names: inception_weights[layer_name] = inception_layers[ layer_name].get_weights() K.clear_session() # create a model and initialize with inception_weights model = model_builder(level=args.level, input_shape=(*PATCH_SIZE, 3)) model_layers = {layer.name: layer for layer in model.layers} for layer_name in conv_layers_names:
batch_size = 200 #ResNet50,VGG16,InceptionV3 shape = (50, 50) #models=["VGG16","VGG19","InceptionV3","ResNet50","AlexNet"] models = ["ResNet50"] for modelname in models: if modelname == "VGG16": base = applications.VGG16(include_top=False, input_shape=(*shape, 3), weights='imagenet') elif modelname == "VGG19": base = applications.VGG19(include_top=False, input_shape=(*shape, 3), weights='imagenet') elif modelname == "InceptionV3": base = applications.InceptionV3(include_top=False, input_shape=(*shape, 3), weights='imagenet') elif modelname == "ResNet50": base = applications.ResNet50(include_top=False, input_shape=(*shape, 3), weights='imagenet') print(modelname) model = Model(input=base.input, output=base.layers[-1].output) #model.summary();import sys;sys.exit() dirList = ['训练', '验证', '测试'] datagen = ImageDataGenerator() #(rescale=1.0 / 255) root_path = './features/' data_path = 'data' if not os.path.exists(root_path): os.makedirs(root_path) for path in dirList:
img_height = 224 img_width = 224 num_classes = 2 trdata = ImageDataGenerator() traindata = trdata.flow_from_directory( directory="../../data/TNSCUI2020_train/classification_ds/train_imgmsk/", target_size=(224, 224)) tsdata = ImageDataGenerator() testdata = tsdata.flow_from_directory( directory="../../data/TNSCUI2020_train/classification_ds/valid_imgmsk/", target_size=(224, 224)) base_model = applications.InceptionV3(weights=None, include_top=False, input_shape=(img_height, img_width, 3)) # 'imagenet' x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(64, activation='relu', name='1024featuresgooglenet', kernel_regularizer=l2(0.00001))(x) x = Dropout(0.5)(x) predictions = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) # Freeze the layers except the last 3 layers #for layer in model.layers[:-3]: # layer.trainable = False
def test_inceptionv3_pooling(): model = applications.InceptionV3(weights=None, include_top=False, pooling='avg') assert model.output_shape == (None, 2048)
def model_main(result_sds, project_id, result_dir, train_data_dir, validation_data_dir, nb_train_samples, nb_validation_samples, input_shape, img_width, img_height, epochs, batch_size): # 通过train_data_dir下的文件夹数目得到分类数量 l = os.listdir(train_data_dir) l.remove('.DS_Store') num_classes = len(l) if num_classes < 2: raise Exception('classes should be more than 1, put your ' 'different classes images file into ' 'different folder') # load the inception_v3 network base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=input_shape) # build the top of cnn network top_model = Sequential() top_model.add(Flatten(input_shape=base_model.output_shape[1:])) # top_model.add(Dense(256, activation='relu')) top_model.add(Dropout(0.5)) # binary class if num_classes == 2: top_model.add(Dense(1, activation='sigmoid')) model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) for layer in model.layers[:-2]: layer.trainable = False model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy', custom_metrcis.matthews_correlation, custom_metrcis.precision, custom_metrcis.recall, custom_metrcis.fmeasure, ]) else: top_model.add(Dense(num_classes, activation='softmax')) model = Model(inputs=base_model.input, outputs=top_model(base_model.output)) for layer in model.layers[:-2]: layer.trainable = False model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy']) # this is the augmentation configuration we will use for training train_datagen = ImageDataGenerator( rescale=1. / 255, 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) if num_classes == 2: class_mode = 'binary' else: class_mode = 'categorical' train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=class_mode) validation_generator = test_datagen.flow_from_directory( validation_data_dir, target_size=(img_width, img_height), batch_size=batch_size, class_mode=class_mode) # callback to save metrics batch_print_callback = LambdaCallback(on_epoch_begin= lambda epoch, logs: logger_service.log_epoch_begin( epoch, logs, result_sds, project_id), on_epoch_end= lambda epoch, logs: logger_service.log_epoch_end( epoch, logs, result_sds, project_id), on_batch_end= lambda batch, logs: logger_service.log_batch_end( batch, logs, result_sds, project_id) ) # checkpoint to save best weight best_checkpoint = MyModelCheckpoint( os.path.abspath(os.path.join(result_dir, 'best.hdf5')), save_weights_only=True, verbose=1, save_best_only=True) # checkpoint to save latest weight general_checkpoint = MyModelCheckpoint( os.path.abspath(os.path.join(result_dir, 'latest.hdf5')), save_weights_only=True, verbose=1) 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=[batch_print_callback, best_checkpoint, general_checkpoint], ) # model.save_weights('first_try.h5') config = model.get_config() logger_service.log_train_end(result_sds, model_config=config, # score=score, history=history.history) keras_saved_model.save_model(result_dir, model) return {'history': history.history}
def inception(input_shape) : return applications.InceptionV3(include_top=False, input_shape=input_shape, weights='imagenet')
def creat_net(train_generator, validation_generator, batch_size, image_lengh, image_width): NB_IV3_LAYERS_TO_FREEZE = 172 # 冻结层的数量 base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(image_lengh, image_width, 3)) for layer in base_model.layers[:NB_IV3_LAYERS_TO_FREEZE]: layer.trainable = False for layer in base_model.layers[NB_IV3_LAYERS_TO_FREEZE:]: layer.trainable = True x = base_model.output x = GlobalAveragePooling2D()(x) x = tf.keras.layers.Dropout(0.4)(x) x = Dense(1024, activation='relu')(x) #new FC layer, random init x = tf.keras.layers.Dropout(0.4)(x) x = Dense(4, activation='softmax')(x) #new softmax layer model = Model(base_model.layers[0].input, x) model.compile(optimizer=optimizers.SGD(lr=0.0001, momentum=0.9), loss='categorical_crossentropy', metrics=['accuracy']) #保存最优模型 filepath = './模型/InceptionV3_weights-improvement-{epoch:02d}-{val_accuracy:.2f}.hdf5' checkpoint = callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max') early = callbacks.EarlyStopping(monitor='val_loss', min_delta=0, patience=0, verbose=0, mode='auto', baseline=None, restore_best_weights=False) model.fit_generator(train_generator, epochs=100, steps_per_epoch=1707 // batch_size, validation_data=validation_generator, validation_steps=264 // batch_size, callbacks=[checkpoint]) #Reduce]) #绘制误差和准确率曲线 loss = model.history.history['loss'] val_loss = model.history.history['val_loss'] epoches = range(1, len(loss) + 1) acc = model.history.history['accuracy'] val_acc = model.history.history['val_accuracy'] plt.subplot(121) plt.plot(epoches, loss, 'bo', label='training_loss') plt.plot(epoches, val_loss, 'r', label='validation_loss') plt.xlabel('epoches') plt.ylabel('loss') plt.title('losses of train and val') plt.legend() plt.subplot(122) plt.plot(epoches, acc, 'bo', label='training_acc') plt.plot(epoches, val_acc, 'r', label='validation_acc') plt.xlabel('epoches') plt.ylabel('acc') plt.title('accuracy of train and val') plt.legend() plt.show()
def train(args): """ Performs training. """ train_dir = args.train_folder nb_train_samples = utils.get_nb_files(args.train_folder) nb_classes = utils.get_labels(args.train_folder, args.validation_folder) base_architecture = args.base_architecture # Define base layer if base_architecture == 'VGG16': model = applications.VGG16(weights='imagenet', include_top=False) elif base_architecture == 'VGG19': model = applications.VGG19(weights='imagenet', include_top=False) elif base_architecture == 'InceptionV3': model = applications.InceptionV3(weights='imagenet', include_top=False) elif base_architecture == 'ResNet50': model = applications.ResNet50(weights='imagenet', include_top=False) data_gen = ImageDataGenerator(rescale = 1./255) data_generator = data_gen.flow_from_directory( train_dir, target_size=(img_height, img_width), batch_size=16, class_mode='categorical' ) predictions = model.predict_generator(data_generator, val_samples=1000) predictions = np.squeeze(predictions) # np.savez('inception_features', predictions=predictions) fig, (ax1, ax2) = plt.subplots(1, 2) # The 1st subplot is the silhouette plot # The silhouette coefficient can range from -1, 1 but in this example all # lie within [-0.1, 1] ax1.set_xlim([-0.1, 1]) # The (n_clusters+1)*10 is for inserting blank space between silhouette # plots of individual clusters, to demarcate them clearly. # Reshape the array. nsamples, nx, ny = predictions.shape res_predictions = predictions.reshape((nsamples, nx*ny)) n_clusters = int(args.n_clusters) ax1.set_ylim([0, len(res_predictions) + (n_clusters + 1) * 10]) clusterer = KMeans(n_clusters=n_clusters, random_state=0) kmeans = clusterer.fit_predict(res_predictions) silhouette_avg = silhouette_score(res_predictions, kmeans) print("For n_clusters =", n_clusters, "The average silhouette_score is :", silhouette_avg) # Compute the silhouette scores for each sample sample_silhouette_values = silhouette_samples(res_predictions, kmeans) y_lower = 10 for i in range(n_clusters): # Aggregate the silhouette scores for samples belonging to # cluster i, and sort them ith_cluster_silhouette_values = \ sample_silhouette_values[kmeans == i] ith_cluster_silhouette_values.sort() size_cluster_i = ith_cluster_silhouette_values.shape[0] y_upper = y_lower + size_cluster_i color = cm.spectral(float(i) / n_clusters) ax1.fill_betweenx(np.arange(y_lower, y_upper), 0, ith_cluster_silhouette_values, facecolor=color, edgecolor=color, alpha=0.7) # Label the silhouette plots with their cluster numbers at the middle ax1.text(-0.05, y_lower + 0.5 * size_cluster_i, str(i)) # Compute the new y_lower for next plot y_lower = y_upper + 10 # 10 for the 0 samples ax1.set_title("The silhouette plot for the various clusters.") ax1.set_xlabel("The silhouette coefficient values") ax1.set_ylabel("Cluster label") # 2nd Plot showing the actual clusters formed colors = cm.spectral(kmeans.astype(float) / n_clusters) ax2.scatter(res_predictions[:, 0], res_predictions[:, 1], marker='.', s=30, lw=0, alpha=0.7, c=colors, edgecolor='k') # Labeling the clusters centers = clusterer.cluster_centers_ # Draw white circles at cluster centers ax2.scatter(centers[:, 0], centers[:, 1], marker='o', c="white", alpha=1, s=200, edgecolor='k') for i, c in enumerate(centers): ax2.scatter(c[0], c[1], marker='$%d$' % i, alpha=1, s=50, edgecolor='k') ax2.set_title("The visualization of the clustered data.") ax2.set_xlabel("Feature space for the 1st feature") ax2.set_ylabel("Feature space for the 2nd feature") plt.show()
train_generator = train_datagen.flow_from_directory( train_dir, target_size=(img_height, img_width), batch_size=batch_size, ) validation_generator = test_datagen.flow_from_directory( validation_dir, target_size=(img_height, img_width), batch_size=batch_size, ) # pl = tf.placeholder(tf.float32, shape=(img_height, img_width, 3)) base_model = applications.InceptionV3(weights='imagenet', include_top=False, input_tensor=input_tensor) x = GlobalAveragePooling2D()(base_model.output) # x = Dropout(.4)(x) # x = Flatten()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(nb_classes, init='glorot_uniform', W_regularizer=l2(.0005), activation='softmax')(x) model = Model(base_model.input, predictions) # model = load_model(filepath='./model4.29-0.69.hdf5') opt = SGD(lr=.01, momentum=.9)
#data_x_flat /= 255 data_x = data_x.astype(float) / 255 '''data_x_train = data_x[5000:10000] data_y_train = data_y[5000:10000] data_x_val = data_x[10001:11000] data_y_val = data_y[10001:11000]''' #print(grayscale_batch.shape) # (64, 224, 224) #data_x_train = np.repeat(data_x_train[..., np.newaxis], 3, -1) #data_x_val = np.repeat(data_x_val[..., np.newaxis], 3, -1) #print(rgb_batch.shape) # (64, 224, 224, 3) data_x_train = data_x[:26400] model_inc = applications.InceptionV3(weights='imagenet', include_top=False) pred_list = [] for row in tqdm(data_x_train): row = np.repeat(row[..., np.newaxis], 3, -1) #row = np.expand_dims(row, axis = 0) pred = model_inc.predict(row[np.newaxis, :]) pred_list.append(pred) train_data_feat = np.array(pred_list) file_name_x = 'train_data_feat' np.save(file_name_x, train_data_feat) data_x_val = data_x[26401:]
X_train = X_train / 255 X_test = X_test / 255 print("Done Normalizing!!!") # Converting the class labels into binary class matrices y_train = keras.utils.to_categorical(y_train, num_classes=6) y_test = keras.utils.to_categorical(y_test, num_classes=6) # Splitting 15% of training dataset into CV dataset X_train, X_CV, y_train, y_CV = train_test_split(X_train, y_train, test_size=0.15, random_state=0) res = applications.InceptionV3(input_shape=(150, 150, 3), weights='imagenet', include_top=False) res.trainable = False print('inception pre trained model is loaded ....') model = Sequential([ res, Flatten(), Dense(400, activation='tanh'), Dropout(0.5), BatchNormalization(), Dense(6, activation='softmax') ]) early_stopping_callback = keras.callbacks.EarlyStopping(monitor='val_loss', patience=3)
def val(): # batch_size = 14 # build the network modelvgg = applications.InceptionV3(weights=None, include_top=False, input_shape=(img_width, img_height, 3)) modelvgg2 = applications.InceptionV3(weights=None, include_top=False, input_shape=(img_width, img_height, 3)) print('Model loaded.') model = GP.build_global_attention_pooling_model_cascade_attention( [modelvgg, modelvgg2], class_num) model.load_weights( 'weights1/weights-gatp-two-stream-inception_v3-006-0.9080.hdf5') single_model = model if gpu_count > 1: model = multi_gpu_model(model, gpus=gpu_count) model.compile( loss=[ 'categorical_crossentropy', 'categorical_crossentropy', 'categorical_crossentropy', 'categorical_crossentropy', 'mean_squared_error', 'mean_squared_error', GP.rank_loss, GP.cross_network_similarity_loss, GP.rank_loss, GP.rank_loss, ], # optimizer=optimizers.SGD(lr=0.0001, momentum=0.9, decay=0.0, nesterov=False), optimizer=optimizers.RMSprop(lr=0.00001, rho=0.9, epsilon=1e-08, decay=0.000001), metrics={ 'output_1': ['accuracy', 'top_k_categorical_accuracy'], 'output_2': ['accuracy', 'top_k_categorical_accuracy'], 'output_3': ['accuracy', 'top_k_categorical_accuracy'], # 'output_4': ['accuracy', 'top_k_categorical_accuracy'], 'output_5': ['accuracy', 'top_k_categorical_accuracy'], }) test_datagen = MultiLabelImageDataGenerator(input_num=2, label_num=4, constraint_num=2, extra_constraints=[2, 2, 2, 2]) validation_generator = test_datagen.flow_from_directory( test_data_dir, target_size=(img_height, img_width), batch_size=batch_size, shuffle=False, interpolation='bilinear', class_mode='categorical') result = model.evaluate_generator(validation_generator) print(result)
warnings.warn("Early stopping requires %s available!" % self.monitor, RuntimeWarning) if current < self.value: if self.verbose > 0: print("Epoch %05d: early stopping THR" % epoch) self.model.stop_training = True early_stopping = EarlyStopping(monitor='val_loss', patience=10, mode='auto') # top_model_weights_path = 'bottleneck_fc_model.h5' ### Creating InceptionV3 model # We now create the InceptionV3 model without the final fully-connected layers (setting `include_top=False`) and loading the ImageNet weights (by setting `weights ='imagenet`) from keras.applications.inception_v3 import InceptionV3 model = applications.InceptionV3(include_top=False, weights='imagenet') applications.InceptionV3(include_top=False, weights='imagenet').summary() type(applications.InceptionV3(include_top=False, weights='imagenet').summary()) ### Training and running images on InceptionV3 # We first create the generator. The generator is an iterator that generates batches of images when requested using e.g. `flow( )`. datagen = ImageDataGenerator(rescale=1. / 255) generator = datagen.flow_from_directory( train_data_dir, target_size=(img_width, img_height), batch_size=batch_size,
def train_gatp_two_stream(): # batch_size = 14 # build the network modelvgg = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3)) modelvgg2 = applications.InceptionV3(weights='imagenet', include_top=False, input_shape=(img_width, img_height, 3)) # modelvgg.load_weights('inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5') modelvgg2.load_weights( 'inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5') print('Model loaded.') model = GP.build_global_attention_pooling_model_cascade_attention( [modelvgg, modelvgg2], class_num) single_model = model if gpu_count > 1: model = multi_gpu_model(model, gpus=gpu_count) model.compile( loss=[ 'categorical_crossentropy', 'categorical_crossentropy', 'categorical_crossentropy', 'categorical_crossentropy', 'mean_squared_error', 'mean_squared_error', GP.rank_loss, GP.cross_network_similarity_loss, GP.rank_loss, GP.rank_loss, ], # optimizer=optimizers.SGD(lr=0.0001, momentum=0.9, decay=0.0, nesterov=False), optimizer=optimizers.RMSprop(lr=0.00001, rho=0.9, epsilon=1e-08, decay=0.000001), metrics={ 'output_1': ['accuracy', 'top_k_categorical_accuracy'], 'output_2': ['accuracy', 'top_k_categorical_accuracy'], 'output_3': ['accuracy', 'top_k_categorical_accuracy'], # 'output_4': ['accuracy', 'top_k_categorical_accuracy'], 'output_5': ['accuracy', 'top_k_categorical_accuracy'], }) # prepare data augmentation configuration train_datagen = MultiLabelImageDataGenerator( input_num=2, label_num=4, constraint_num=2, extra_constraints=[2, 2, 2, 2]) test_datagen = MultiLabelImageDataGenerator(input_num=2, label_num=4, constraint_num=2, extra_constraints=[2, 2, 2, 2]) train_generator = train_datagen.flow_from_directory( train_data_dir, target_size=(img_height, img_width), batch_size=batch_size, class_mode='categorical', interpolation='bilinear', shuffle=True) validation_generator = test_datagen.flow_from_directory( test_data_dir, target_size=(img_height, img_width), batch_size=batch_size, shuffle=False, interpolation='bilinear', class_mode='categorical') file_path = "weights" + use_gpu_num + \ "/weights-gatp-two-stream-inception_v3-{epoch:03d}-{val_output_5_acc:.4f}.hdf5" checkpoint = my_callbacks.ModelCheckpoint(file_path, single_model, monitor='val_output_5_acc', verbose=1, save_best_only=True, save_weights_only=True, mode='max') callbacks_list = [checkpoint] # fine-tune the model model.fit_generator( train_generator, steps_per_epoch=(train_generator.n // batch_size), epochs=epochs, validation_data=validation_generator, validation_steps=math.ceil( float(validation_generator.n) // batch_size), callbacks=callbacks_list, ) result = model.evaluate_generator(validation_generator) print(result)
def mySpatialModel(model_name,spatial_size, nb_classes, channels, channel_first=True, weights_path=None, lr=0.005, decay=1e-6, momentum=0.9,plot_model=True): input_tensor = Input(shape=(channels, spatial_size, spatial_size)) input_shape = (channels, spatial_size, spatial_size) base_model=None predictions=None data_dim=1024 if model_name=='ResNet50': base_model = kerasApp.ResNet50(include_top=False, input_tensor=input_tensor, input_shape=input_shape, weights=weights_path, classes=nb_classes, pooling=None) x = base_model.output # 添加自己的全链接分类层 method 1 #x = Flatten()(x) #predictions = Dense(nb_classes, activation='softmax')(x) #method 2 x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name=='VGG16': base_model = kerasApp.VGG16(include_top=False, input_tensor=input_tensor, input_shape=input_shape, weights=weights_path, classes=nb_classes, pooling=None) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) # add a global spatial average pooling layer x = Dense(1024, activation='relu')(x) # let's add a fully-connected layer predictions = Dense(nb_classes, activation='softmax')(x) # and a logistic layer model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'VGG19': base_model = kerasApp.VGG19(include_top=False, input_tensor=input_tensor, input_shape=input_shape, weights=weights_path ,classes=2, pooling=None) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name=='InceptionV3': base_model = kerasApp.InceptionV3(weights=weights_path, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name=='InceptionResNetV2': base_model = kerasApp.InceptionResNetV2(weights=weights_path, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) data_dim = 1536 predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'Xception': input_shape_xception = (spatial_size, spatial_size,channels) base_model = kerasApp.Xception(weights=weights_path, include_top=False, pooling="avg", input_shape=input_shape_xception, classes=nb_classes) x = base_model.output predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'DenseNet121': base_model = kerasApp.DenseNet121(weights=weights_path, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'DenseNet169': base_model = kerasApp.DenseNet169(weights=weights_path, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'DenseNet201': input_tensor = Input(shape=( spatial_size, spatial_size,channels)) input_shape = (spatial_size, spatial_size,channels) base_model = kerasApp.DenseNet201(weights=weights_path, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) elif model_name == 'MobileNet': base_model = kerasApp.MobileNet(weights=weights_path, include_top=False, pooling=None, input_shape=input_shape, classes=nb_classes) x = base_model.output # 添加自己的全链接分类层 x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) x = Dense(1024, activation='relu')(x) x = Dense(512, activation='relu')(x) data_dim=512 predictions = Dense(nb_classes, activation='softmax')(x) model = Model(inputs=base_model.input, outputs=predictions) else: print("this model--["+model_name+"]-- doesnt exist!") # 冻结base_model所有层,这样就可以正确获得bottleneck特征 for layer in base_model.layers: layer.trainable = True # 训练模型 model = Model(inputs=base_model.input, outputs=predictions) print('-------------当前base_model模型[' + model_name + "]-------------------\n") print('base_model层数目:' + str(len(base_model.layers))) print('model模型层数目:' + str(len(model.layers))) featureLayer=model.layers[len(model.layers)-2] print(featureLayer.output_shape) print("data_dim:" + str(featureLayer.output_shape[1])) print("---------------------------------------------\n") sgd = SGD(lr=lr, decay=decay, momentum=momentum, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy']) # 绘制模型 #if plot_model: # plot_model(model, to_file=model_name+'.png', show_shapes=True) return model
training_set = train_datagen.flow_from_directory(train_data_dir, target_size=(width, height), batch_size=64, class_mode='categorical') test_set = test_datagen.flow_from_directory(test_data_dir, target_size=(width, height), batch_size=64, class_mode='categorical') if K.image_dim_ordering() == 'th': input_tensor = Input(shape=(3, 299, 299)) else: input_tensor = Input(shape=(299, 299, 3)) model = applications.InceptionV3(input_tensor=input_tensor, weights="imagenet", include_top=False) model.summary() for layer in model.layers[:-1]: layer.trainable = False output = model.output output = AveragePooling2D((2, 2), padding='valid', name='avg_pool')(output) output = Dropout(0.4)(output) output = Convolution2D(2048, (4, 4), activation='relu')(output) output = Flatten()(output) output = Dense(units=128, activation='relu')(output) output = Dense(units=2, activation='softmax')(output) new_model = Model(inputs=model.input, outputs=output)
def test_inceptionv3(): model = applications.InceptionV3(weights=None) assert model.output_shape == (None, 1000)
def select_model_params(self, num_classes): if self.fine_tuning_rate.value != -1: if self.architecture.value == "Xception": self.app = 0 model = applications.Xception( weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "VGG16": self.app = 1 model = applications.VGG16( weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "VGG19": self.app = 2 model = applications.VGG19( weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "ResNet50": self.app = 3 model = applications.ResNet50( weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "InceptionV3": self.app = 4 model = applications.InceptionV3( weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "MobileNet": self.app = 5 model = applications.MobileNet( weights="imagenet", include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) for layer in model.layers[:int(len(model.layers) * (self.fine_tuning_rate.value / 100.0))]: layer.trainable = False else: # without transfer learning if self.architecture.value == "Xception": self.app = 0 model = applications.Xception( weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "VGG16": self.app = 1 model = applications.VGG16( weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "VGG19": self.app = 2 model = applications.VGG19( weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "ResNet50": self.app = 3 model = applications.ResNet50( weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "InceptionV3": self.app = 4 model = applications.InceptionV3( weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) elif self.architecture.value == "MobileNet": self.app = 5 model = applications.MobileNet( weights=None, include_top=False, input_shape=(IMG_WIDTH, IMG_HEIGHT, 3)) for layer in model.layers: layer.trainable = True # Adding custom Layers new_custom_layers = model.output new_custom_layers = Flatten()(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) new_custom_layers = Dropout(0.5)(new_custom_layers) new_custom_layers = Dense(1024, activation="relu")(new_custom_layers) predictions = Dense(num_classes, activation="softmax")(new_custom_layers) # creating the final model model = Model(inputs=model.input, outputs=predictions) return model