def cnn_model(model_name, img_size): """ Model definition using Xception net architecture """ input_size = (img_size, img_size, 3) if model_name == "xception": baseModel = Xception(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) elif model_name == "iv3": baseModel = InceptionV3(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) elif model_name == "irv2": baseModel = InceptionResNetV2(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) elif model_name == "resnet": baseModel = ResNet50(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) elif model_name == "nasnet": baseModel = NASNetLarge(weights="imagenet", include_top=False, input_shape=(img_size, img_size, 3)) elif model_name == "ef0": baseModel = EfficientNetB0(input_size, weights="imagenet", include_top=False) elif model_name == "ef5": baseModel = EfficientNetB5(input_size, weights="imagenet", include_top=False) headModel = baseModel.output headModel = GlobalAveragePooling2D()(headModel) headModel = Dense(512, activation="relu", kernel_initializer="he_uniform")(headModel) headModel = Dropout(0.4)(headModel) # headModel = Dense(512, activation="relu", kernel_initializer="he_uniform")( # headModel # ) # headModel = Dropout(0.5)(headModel) headModel = Dropout(0.5)(headModel) predictions = Dense(2, activation="softmax", kernel_initializer="he_uniform")(headModel) model = Model(inputs=baseModel.input, outputs=predictions) for layer in baseModel.layers: layer.trainable = True optimizer = Nadam(lr=0.002, beta_1=0.9, beta_2=0.999, epsilon=1e-08, schedule_decay=0.004) model.compile(loss="categorical_crossentropy", optimizer=optimizer, metrics=["accuracy"]) return model
def main(): # construct the argument parser and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-d", "--dataset", required=True, help="path to input dataset of images") ap.add_argument("-m", "--model", required=True, help="path to output trained model") ap.add_argument("-l", "--label-bin", required=True, help="path to output label binarizer") ap.add_argument("-p", "--plot", required=True, help="path to output accuracy/loss plot") args = vars(ap.parse_args()) # initialize the data and labels print("[INFO] loading images...") # grab the image paths and randomly shuffle them image_path = sorted(list(paths.list_images(args["dataset"] + "/train"))) random.shuffle(image_path) random.seed(42) data, labels = split(image_path) (trainX, valX, trainY, valY) = train_test_split(data, labels, test_size=0.2, random_state=42) # convert the labels from integers to vectors (for 2-class, binary # classification you should use Keras' to_categorical function # instead as the scikit-learn's LabelBinarizer will not return a # vector) lb = LabelBinarizer() trainY = lb.fit_transform(trainY) valY = lb.transform(valY) # construct the image generator for data augmentation aug = ImageDataGenerator(rotation_range=30, width_shift_range=0.1, height_shift_range=0.1, shear_range=0.2, zoom_range=0.2, horizontal_flip=True, fill_mode="nearest") # base_model = InceptionV3(input_shape=(HEIGHT, WIDTH, 3), # weights = 'imagenet', # include_top = False, # pooling = 'avg') # base_model = InceptionResNetV2(input_shape=(HEIGHT, WIDTH, 3), # weights = 'imagenet', # include_top = False, # pooling = 'avg') base_model = Xception(input_shape=(HEIGHT, WIDTH, 3), weights='imagenet', include_top=False, pooling='avg') x = base_model.output x = Dense(1024, activation="relu")(x) x = Dropout(DROPOUT)(x) predictions = Dense(15, activation="softmax")(x) model = Model(inputs=base_model.input, outputs=predictions) # initialize the model and optimizer print("[INFO] training network...") opt = SGD(lr=INIT_LR, momentum=0.9, clipnorm=5.) model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"]) # saver = ModelCheckpoint("output/model.hdf5", verbose=1, # save_best_only=True, monitor="val_acc", # mode="max") # reduce_lr = ReduceLROnPlateau(monitor="loss", factor=0.5, # patience=5, verbose=1, min_lr=0.0001) stopper = EarlyStopping(patience=20, verbose=1, monitor="val_acc", mode="max") clr = CyclicLR(base_lr=INIT_LR, max_lr=0.006, step_size=8 * len(trainX) // BS, mode="triangular2") # lrate = LearningRateScheduler(step_decay, verbose=1) # train the network H = model.fit_generator(aug.flow(trainX, trainY, batch_size=BS), validation_data=(valX, valY), steps_per_epoch=len(trainX) // BS, validation_steps=len(valX) // BS, epochs=EPOCHS, callbacks=[stopper, clr]) ########################## ## EVALUATE THE NETWORK ## ########################## print("[INFO] evaluating network...") image_path = sorted(list(paths.list_images("dataset/test"))) testX, testY = split(image_path) testY = lb.transform(testY) predictions = model.predict(testX, batch_size=BS) print( classification_report(testY.argmax(axis=1), predictions.argmax(axis=1), target_names=lb.classes_)) print("Accuracy: {}".format( accuracy_score(testY.argmax(axis=1), predictions.argmax(axis=1)))) print(confusion_matrix(testY.argmax(axis=1), predictions.argmax(axis=1))) # save the model and label binarizer to disk print("[INFO] serializing network and label binarizer...") model.save(args["model"]) f = open(args["label_bin"], "wb") f.write(pickle.dumps(lb)) f.close() print("[INFO] plotting and saving results...") # plot the training loss and accuracy N = np.arange(0, EPOCHS) if stopper.stopped_epoch == 0 else np.arange( 0, stopper.stopped_epoch + 1) # Se não parou antes então stopper.stopped_epoch será 0 plt.style.use("ggplot") plt.figure() plt.plot(N, H.history["loss"], label="train_loss") plt.plot(N, H.history["val_loss"], label="val_loss") plt.plot(N, H.history["acc"], label="train_acc") plt.plot(N, H.history["val_acc"], label="val_acc") plt.title("Training Loss and Accuracy (Xception)") plt.xlabel("Epoch #") plt.ylabel("Loss/Accuracy") plt.legend() plt.savefig(args["plot"]) plot_confusion_matrix(lb.classes_, testY.argmax(axis=1), predictions.argmax(axis=1)) plt.style.use("ggplot") plt.figure() plt.plot(clr.history['lr'], clr.history['acc']) plt.title("Training Learning Rate and Accuracy (Xception)") plt.xlabel("Learning Rate") plt.ylabel("Accuracy") plt.legend() plt.savefig("output/learning_rate_Xception.png") print("Dropout: {} BS: {}".format(DROPOUT, BS))
width_shift_range=0.2, height_shift_range=0.2, zoom_range=0.2,shear_range=0.2 ) train_generator = train_data_gen.flow_from_directory(directory='assets/train/', target_size=(224,224), batch_size=32, class_mode='categorical') valid_data_gen = ImageDataGenerator(rescale=1./255) valid_generator = valid_data_gen.flow_from_directory(directory='assets/valid/', target_size=(224,224), batch_size=32, class_mode='categorical') base_model = Xception(weights='imagenet', include_top=False,input_shape=(224,224,3)) top_model = load_model('models/Xception/Gru/top_model_512_0.5.hdf5', custom_objects={'top1_loss':top1_loss}) x = base_model.output predictions = top_model(x) model = Model(inputs=base_model.input, outputs=predictions) #for layer in base_model.layers: # layer.trainable = False model.summary() model.compile(optimizer=Adam(lr = 0.00005), loss='categorical_crossentropy',metrics=[top1_loss]) file_path = 'models/Xception/Gru/model_512.hdf5' # train the model on the new data for a few epochs check_point = ModelCheckpoint(file_path, monitor="val_loss", mode="min", save_best_only=True, verbose=1)
def main(args): # ==================================================== # Preparation # ==================================================== # parameters epochs = args.epochs_pre + args.epochs_fine args.dataset_root = os.path.expanduser(args.dataset_root) args.result_root = os.path.expanduser(args.result_root) args.classes = os.path.expanduser(args.classes) # load class names with open(args.classes, 'r') as f: classes = f.readlines() classes = list(map(lambda x: x.strip(), classes)) num_classes = len(classes) # make input_paths and labels input_paths, labels = [], [] for class_name in os.listdir(args.dataset_root): class_root = os.path.join(args.dataset_root, class_name) class_id = classes.index(class_name) for path in os.listdir(class_root): path = os.path.join(class_root, path) if imghdr.what(path) == None: # this is not an image file continue input_paths.append(path) labels.append(class_id) # convert to one-hot-vector format labels = to_categorical(labels, num_classes=num_classes) # convert to numpy array input_paths = np.array(input_paths) # shuffle dataset perm = np.random.permutation(len(input_paths)) labels = labels[perm] input_paths = input_paths[perm] # split dataset for training and validation border = int(len(input_paths) * args.split) train_labels, val_labels = labels[:border], labels[border:] train_input_paths, val_input_paths = input_paths[:border], input_paths[ border:] print("Training on %d images and labels" % (len(train_input_paths))) print("Validation on %d images and labels" % (len(val_input_paths))) # create a directory where results will be saved (if necessary) if os.path.exists(args.result_root) == False: os.makedirs(args.result_root) # ==================================================== # Build a custom Xception # ==================================================== # instantiate pre-trained Xception model # the default input shape is (299, 299, 3) # NOTE: the top classifier is not included base_model = Xception(include_top=False, weights='imagenet', input_shape=(299, 299, 3)) # create a custom top classifier x = base_model.output x = GlobalAveragePooling2D()(x) x = Dense(1024, activation='relu')(x) predictions = Dense(num_classes, activation='softmax')(x) model = Model(inputs=base_model.inputs, outputs=predictions) # ==================================================== # Train only the top classifier # ==================================================== # freeze the body layers for layer in base_model.layers: layer.trainable = False # compile model model.compile(loss=categorical_crossentropy, optimizer=Adam(lr=args.lr_pre), metrics=['accuracy']) # train hist_pre = model.fit_generator( generator=generate_from_paths_and_labels( input_paths=train_input_paths, labels=train_labels, batch_size=args.batch_size_pre), steps_per_epoch=math.ceil( len(train_input_paths) / args.batch_size_pre), epochs=args.epochs_pre, validation_data=generate_from_paths_and_labels( input_paths=val_input_paths, labels=val_labels, batch_size=args.batch_size_pre), validation_steps=math.ceil(len(val_input_paths) / args.batch_size_pre), verbose=1, callbacks=[ ModelCheckpoint( filepath=os.path.join( args.result_root, 'model_pre_ep{epoch}_valloss{val_loss:.3f}.h5'), period=args.snapshot_period_pre, ), ], ) model.save(os.path.join(args.result_root, 'model_pre_final.h5')) # ==================================================== # Train the whole model # ==================================================== # set all the layers to be trainable for layer in model.layers: layer.trainable = True # recompile model.compile(optimizer=Adam(lr=args.lr_fine), loss=categorical_crossentropy, metrics=['accuracy']) # train hist_fine = model.fit_generator( generator=generate_from_paths_and_labels( input_paths=train_input_paths, labels=train_labels, batch_size=args.batch_size_fine), steps_per_epoch=math.ceil( len(train_input_paths) / args.batch_size_fine), epochs=args.epochs_fine, validation_data=generate_from_paths_and_labels( input_paths=val_input_paths, labels=val_labels, batch_size=args.batch_size_fine), validation_steps=math.ceil( len(val_input_paths) / args.batch_size_fine), verbose=1, callbacks=[ ModelCheckpoint( filepath=os.path.join( args.result_root, 'model_fine_ep{epoch}_valloss{val_loss:.3f}.h5'), period=args.snapshot_period_fine, ), ], ) model.save(os.path.join(args.result_root, 'model_fine_final.h5')) model_json = model.to_json() with open(os.path.join(args.result_root, 'model_fine_final.json'), "w") as json_file: json.dump(model_json, json_file) # ==================================================== # Create & save result graphs # ==================================================== # concatinate plot data acc = hist_pre.history['acc'] val_acc = hist_pre.history['val_acc'] loss = hist_pre.history['loss'] val_loss = hist_pre.history['val_loss'] acc.extend(hist_fine.history['acc']) val_acc.extend(hist_fine.history['val_acc']) loss.extend(hist_fine.history['loss']) val_loss.extend(hist_fine.history['val_loss']) # save graph image plt.plot(range(epochs), acc, marker='.', label='acc') plt.plot(range(epochs), val_acc, marker='.', label='val_acc') plt.legend(loc='best') plt.grid() plt.xlabel('epoch') plt.ylabel('acc') plt.savefig(os.path.join(args.result_root, 'acc.png')) plt.clf() plt.plot(range(epochs), loss, marker='.', label='loss') plt.plot(range(epochs), val_loss, marker='.', label='val_loss') plt.legend(loc='best') plt.grid() plt.xlabel('epoch') plt.ylabel('loss') plt.savefig(os.path.join(args.result_root, 'loss.png')) plt.clf() # save plot data as pickle file plot = { 'acc': acc, 'val_acc': val_acc, 'loss': loss, 'val_loss': val_loss, } with open(os.path.join(args.result_root, 'plot.dump'), 'wb') as f: pkl.dump(plot, f)
while True: x_test = test_generator.next() x_previous_test = test_previous_generator.next() yield [x_previous_test[0], x_test[0]], [x_previous_test[1], x_test[1]] # Now we proceed to decapitate the model, and retrain its head on the new data. # In[ ]: from keras.applications.xception import Xception #xception_weights_path = "/home/leila/Code/siamese_traffic_density/pretrained/xception_weights_tf_dim_ordering_tf_kernels_notop.h5" xception_conv_base = Xception(include_top=False, weights="imagenet", input_tensor=None, input_shape=(224, 224, 3), pooling=None, classes=None) xception_conv_base.summary() #xception_conv_base.load_weights(xception_weights_path) # In[ ]: from keras.models import Sequential, Model from keras.layers import Flatten, Dense, Lambda, Input, Multiply, Dot, Add, Concatenate, Average from keras import backend as K import tensorflow as tf def initialize_weights(shape, name=None): """
def extract_Xception(tensor): from keras.applications.xception import Xception, preprocess_input return Xception(weights='imagenet', include_top=False).predict_step(preprocess_input(tensor))
from keras.applications.resnet50 import preprocess_input preprocessing_function = preprocess_input base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "InceptionV3": from keras.applications.inception_v3 import preprocess_input preprocessing_function = preprocess_input base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "Xception": from keras.applications.xception import preprocess_input preprocessing_function = preprocess_input base_model = Xception(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "InceptionResNetV2": from keras.applications.inceptionresnetv2 import preprocess_input preprocessing_function = preprocess_input base_model = InceptionResNetV2(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "MobileNet": from keras.applications.mobilenet import preprocess_input preprocessing_function = preprocess_input base_model = MobileNet(weights='imagenet', include_top=False, input_shape=(HEIGHT, WIDTH, 3)) elif args.model == "DenseNet121": from keras.applications.densenet import preprocess_input
def creatXceptionModel(mode=None, par=None, weights_h5=None, evaluate=False, train=False): ''' creat model for image recognition,location and segmentation :param mode:None,change outsize or common None:default xception mode from keras change outsize:out10,out19,out37,out74;xception output will be change in order to improve pixel position accuracy common:try a generic CNNs network :param par:common mode subparameter Ki:filters, Integer, the dimensionality of the output space (i.e. the number output of filters in the convolution). reduce:Integer,Output size reduce factor loop:Integer,Sub-network repeat times,determine the depth and entropy of the network :param weights_h5:load pre-trained weights h5 file,File naming rules: mode + produce_time +evaluate_score :param evaluate:boolean,Whether to evaluate this model :param train:boolean,Whether to train this model :return:created model ''' global fcnUpTimes global kernelSize global trainModel global segModel global it_size global overArea global batchSize global classNameDic global classNameDic_T # build the network with ImageNet weights inputShape = (imgRows, imgCols, 3) # default if mode == None: from keras.applications.xception import Xception base_model = Xception(weights='imagenet', include_top=False, input_shape=inputShape) print 'you chose default mode' mode = 'default' kernelSize = 10 fcnUpTimes = 30 elif mode == 'common': from xception_common import Xception if par == None: print "common mode par should not be None!" exit(0) base_model = Xception(Ki=par[0], reduce=par[1], loop=par[2], input_shape=inputShape) parStr = str(par[0]) + '_' + str(par[1]) + '_' + str(par[2]) mode += parStr print 'you chose common mode,par is ' + parStr kernelSize = 8 # need adapter with reduce fcnUpTimes = 37 # need adapter with reduce batchSize = 8 # GPU memory limit # outsize changed mode else: import xception_outsize_change from xception_outsize_change import Xception if mode == 'out10': # as same as default mode kernelSize = 10 fcnUpTimes = 30 print 'you chose out10 mode...' if mode == 'out19': xception_outsize_change.out19 = 1 kernelSize = 19 fcnUpTimes = 16 print 'you chose out19 mode...' if mode == 'out37': xception_outsize_change.out19 = 1 xception_outsize_change.out37 = 1 kernelSize = 37 kernelSize = 18 fcnUpTimes = 8 batchSize = 8 # GPU memory limit print 'you chose out37 mode...' if mode == 'out74': xception_outsize_change.out19 = 1 xception_outsize_change.out37 = 1 xception_outsize_change.out74 = 1 kernelSize = 74 fcnUpTimes = 4 batchSize = 3 # GPU memory limit print 'you chose out74 mode...' # base_model = Xception(include_top=False, input_shape=inputShape) base_model = Xception(weights='imagenet', include_top=False, input_shape=inputShape) #it_size = min(kernelSize*fcnUpTimes, imgRows, imgCols) #print "Pixel iteration size is "+str(it_size) #overArea = it_size * it_size it_size = kernelSize print "Pixel iteration size is " + str(it_size) overArea = it_size * it_size # CNNs network x = base_model.output x = Dropout(0.5)(x) x = Conv2D(num_classes, (1, 1), name='fcn')(x) #x = Conv2D(num_classes, (3, 3), padding='same',name='fcn')(x) #x = Conv2D(num_classes, (3, 3), strides=(2, 2), name='fcn')(x) y = x # 2D to point x = GlobalAveragePooling2D()(x) x = Activation('softmax')(x) trainModel = Model(inputs=base_model.input, outputs=x, name='Xception_dishes') # segmentation y = Activation('softmax')(y) y = keras.layers.advanced_activations.ThresholdedReLU( theta=judgeThreshold)(y) #y = UpSampling2D((fcnUpTimes, fcnUpTimes))(y) segModel = Model(inputs=base_model.input, outputs=y, name='Xception_dishes2Fcn') if weights_h5 != None and os.path.exists(weights_h5): print(weights_h5 + " have loaded!") trainModel.load_weights(weights_h5, by_name=True) # let's visualize layer names and layer indices to see how many layers # we should freeze: # for i, layer in enumerate(trainModel.layers): # print(i, layer.name) # exit(0) # set the first 15 layers (up to the last conv block) # to non-trainable (weights will not be updated) # for layer in trainModel.layers[0:311]: #block5 top,mix8 # layer.trainable = False #rgbTo1_layer = trainModel.get_layer(name='rgb2Atomic') #rgbTo1_layer.trainable = False opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) # compile the model with a SGD/momentum optimizer # and a very slow learning rate. sgd = keras.optimizers.SGD(lr=1e-5, decay=1e-5, momentum=0.9, nesterov=True) #-4,-6 # Let's train the model using opt or sgd trainModel.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) trainModel.summary() # this is a generator that will read pictures found in # subfolers of 'data/train', and indefinitely generate # batches of augmented image data train_generator = datagen.flow_from_directory( imgPath + 'train', # this is the target directory target_size=(imgRows, imgCols), # all images will be resized to 150x150 batch_size=batchSize, # save_to_dir=imgPath+'trainSet', save_prefix='gen', save_format='png', shuffle=True, class_mode='categorical') # get train labs info and item name classNameDic = train_generator.class_indices classNameDic_T = dict((v, k) for k, v in classNameDic.items()) print classNameDic_T # this is a similar generator, for validation data validation_generator = datagen.flow_from_directory( imgPath + 'validation', target_size=(imgRows, imgCols), batch_size=batchSize, # save_to_dir=imgPath+'valSet', save_prefix='gen', save_format='png', shuffle=True, class_mode='categorical') if evaluate == True: print 'evaluate...' score = trainModel.evaluate_generator( validation_generator, steps=math.ceil(imgsNumVal / batchSize) * valTimes, workers=4) print(score) if train == True: from keras.callbacks import EarlyStopping early_stopping = EarlyStopping(monitor='val_loss', patience=1) # Fit the model on the batches generated by datagen.flow(). trainModel.fit_generator( generator=train_generator, steps_per_epoch=math.ceil(imgsNumTrain * trainTimes / batchSize), epochs=2, #callbacks=[early_stopping], #validation_data=validation_generator, #validation_steps=math.ceil(imgsNumVal*valTimes/batchSize), workers=4) print 'evaluate...' score = trainModel.evaluate_generator( validation_generator, steps=math.ceil(imgsNumVal / batchSize) * valTimes, workers=4) print(score) #save trained weights timeInfo = time.strftime('%m-%d_%H:%M', time.localtime(time.time())) trainModel.save_weights(mode + '_' + timeInfo + '_' + str(score[1]) + '.h5') return trainModel, segModel
import cv2 import csv import os import numpy as np import tqdm import os os.environ["CUDA_VISIBLE_DEVICES"] = "2" from keras.applications.xception import Xception, preprocess_input # define global variable csv_file = [ '/mnt/disk1/omg_competition/submmit_version/results/omg_TestVideos_WithoutLabels.csv' ] face_video_root = ['/mnt/disk1/omg_competition/omg_Test_faces'] net = Xception(include_top=False, input_shape=(80, 80, 3), pooling='max') verbose = 0 mode = ['test'] savepath = '/mnt/disk1/omg_competition/omg_Test_TMP/face_{}/{}.npz' def feature_extraction(cap): frames = [] while (1): ret, frame = cap.read() if not ret: break frame = cv2.resize(frame, (80, 80), interpolation=cv2.INTER_NEAREST).astype(float) frames.append(frame) frame_num = len(frames) if len(frames) == 0:
input_shape = (int(width), int(width), x_train.shape[-1]) input_tensor = Input(shape=input_shape) if model_name == 'resnet50': base_model = ResNet50(input_tensor=input_tensor, weights=retrain, include_top=False) batch_size = 48 * gpu_number elif model_name == 'inception_v3': base_model = InceptionV3(input_tensor=input_tensor, weights=retrain, include_top=False) batch_size = 48 * gpu_number elif model_name == 'xception': base_model = Xception(input_tensor=input_tensor, weights=retrain, include_top=False) if int(width) > 200: batch_size = 48 * gpu_number else: batch_size = 24 * gpu_number if int(width) > 299 and int(width) < 500: batch_size = 48 * gpu_number elif int(width) >= 500: batch_size = 32 * gpu_number elif model_name == 'inception_resnet': base_model = InceptionResNetV2(input_tensor=input_tensor, weights=None, include_top=False) batch_size = 32 * gpu_number elif model_name == 'nasnet':
num_classes, val_datagen, lock, batch_size=batch_size, shuffle=True) last_set_layer = 96 # value is based on model selected. models_savename = "./models/xception" classnames = pd.read_csv('your/path/...') img_width = 180 img_height = 180 '''Xception Model''' model0 = Xception(include_top=False, weights='imagenet', input_tensor=None, input_shape=(img_width, img_height, 3)) x = model0.output x = GlobalAveragePooling2D(name='avg_pool')(x) x = Dropout(0.2)(x) x = Dense(len(classnames), activation='softmax', name='predictions')(x) model = Model(model0.input, x) #transfer learning #first train the top added layer weights for layer in model0.layers: layer.trainable = False
import tensorflow as tf from keras.applications.xception import Xception # from keras.utils import multi_gpu_model # from keras.models import import numpy as np import datetime num_samples = 100 height = 71 width = 71 num_classes = 100 start = datetime.datetime.now() with tf.device('/gpu:0'): model = Xception(weights=None, input_shape=(height, width, 3), classes=num_classes) model.compile(loss='categorical_crossentropy', optimizer='rmsprop') # Generate dummy data. x = np.random.random((num_samples, height, width, 3)) y = np.random.random((num_samples, num_classes)) model.fit(x, y, epochs=3, batch_size=16) model.save('my_model.h5') end = datetime.datetime.now() time_delta = end - start print('GPU 처리시간 : ', time_delta)
import numpy as np print("Module imports successful.") # import images # full_path = ""~/CS/Hydroponic-Root-Classifier/" test_image = cv.imread("allData/1_a.jpg") print(test_image.shape) preprocess_input(test_image) # doesn't change input dimensions # build pre-trained xception model model = Xception(include_top=False, weights='imagenet', input_tensor=None, input_shape=(256, 256, 3), pooling=None) # expand x to the 4-dimensional tensor (batch_size, image_width, image_height, channels) # required by the Xception model as input test_image = np.expand_dims(test_image, axis=0) # use xception model to make prediction for images predictions = model.predict(test_image, batch_size=1, verbose=0, steps=None) print("Prediction completed.") print(predictions.shape)
def extract_Xception(sefl, tensor): return Xception(weights='imagenet', include_top=False).predict(preprocess_input(tensor))
pred = model.predict([photo, sequence], verbose=0) pred = np.argmax(pred) word = word_for_id(pred, tokenizer) if word is None: break in_text += ' ' + word if word == 'end': break return in_text #path = 'Flicker8k_Dataset/111537222_07e56d5a30.jpg' max_length = 32 tokenizer = load(open("tokenizer.p", "rb")) model = load_model('models/model_9.h5') xception_model = Xception(include_top=False, pooling="avg") photo = extract_features(img_path, xception_model) img = Image.open(img_path) description = generate_desc(model, tokenizer, photo, max_length) print("\n\n") print(description) plt.imshow(img) description += '.' find = RemoveBannedWords(description, bannedWord) print(find) chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
def rgb_to_grayscale_output_shape(input_shape): return input_shape[:-1] nb_val_samples = 5000 img_width = 299 img_height = 299 print("Building model...") input_tensor = Input(shape=(img_width, img_height, 3)) # Creating CNN cnn_model = Xception(weights='imagenet', include_top=False, input_tensor=input_tensor) x = cnn_model.output cnn_bottleneck = GlobalAveragePooling2D()(x) # Creating RNN x = Lambda(rgb_to_grayscale, rgb_to_grayscale_output_shape)(input_tensor) x = Reshape((23, 3887))(x) # 23 timesteps, input dim of each timestep 3887 x = LSTM(2048, return_sequences=True)(x) rnn_output = LSTM(2048)(x) # Merging both cnn bottleneck and rnn's output wise element wise multiplication x = merge([cnn_bottleneck, rnn_output], mode='mul') predictions = Dense(100, activation='softmax')(x) model = Model(input=input_tensor, output=predictions)
base_model = InceptionResNetV2(include_top=include_top, weights=weights, input_tensor=Input(shape=(299, 299, 3))) model = Model(input=base_model.input, output=base_model.get_layer('custom').output) image_size = (299, 299) elif model_name == "mobilenet": base_model = MobileNet(include_top=include_top, weights=weights, input_tensor=Input(shape=(224, 224, 3)), input_shape=(224, 224, 3)) model = Model(input=base_model.input, output=base_model.get_layer('batch_normalization_1').output) image_size = (224, 224) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None print("[INFO] successfully loaded base model and model...") # path to training dataset train_labels = os.listdir(train_path) # encode the labels print("[INFO] encoding labels...") le = LabelEncoder() le.fit([tl for tl in train_labels])
print(X_datas.shape) print(x_train.shape) print(x_test.shape) print(x_val.shape) #print(np.asarray(range(len(uc)))) #print(y_val[0,:]) # ## Adapting the Xception Architecture to Regression Problems # In[ ]: from keras.applications.xception import Xception from keras.models import Model model = Xception(weights='imagenet', include_top=True, input_shape=(299, 299, 3)) x = model.get_layer(index=len(model.layers) - 2).output print(x) x = Dense(1)(x) model = Model(inputs=model.input, outputs=x) model.summary() # **Using RMSprop optimizer, mean absolute error with metrics, and mean square erro with loss** # In[ ]: opt = RMSprop(lr=0.0001)
# [a-z] letter_list = [chr(i) for i in range(97,123)] img_size = (50, 120) #CNN适合在高宽都是偶数的情况,否则需要在边缘补齐, #那么我们也可以把全体图片都resize成这个尺寸(高,宽,通道) input_image = Input(shape=(img_size[0],img_size[1],3)) #具体方案是:直接将验证码输入,做几个卷积层提取特征, #然后把这些提出来的特征连接几个分类器(26分类,因为不区分大小写), #如果需要加入数字就是36分类,微信验证码里没有数字。 #输入图片 #用预训练的Xception提取特征,采用平均池化 base_model = Xception(input_tensor=input_image, weights='imagenet', include_top=False, pooling='avg') #用全连接层把图片特征接上softmax然后26分类,dropout为0.5,激活使用softmax因为是多分类问题。 #ReLU - 用于隐层神经元输出 #Sigmoid - 用于隐层神经元输出 #Softmax - 用于多分类神经网络输出 #Linear - 用于回归神经网络输出(或二分类问题) #对四个字母做26分类 predicts = [Dense(26, activation='softmax')(Dropout(0.5)(base_model.output)) for i in range(4)] #optimizer:优化器的选择可以参考这篇博客https://www.jianshu.com/p/d99b83f4c1a6 #loss:损失函数,这里选去稀疏多类对数损失 #metrics:列表,包含评估模型在训练和测试时的性能的指标,典型用法是metrics=['accuracy']如果要在多输出模型中为不同的输出指定不同的指标,可像该参数传递一个字典,例如metrics={'ouput_a': 'accuracy'} #sample_weight_mode:如果你需要按时间步为样本赋权(2D权矩阵),将该值设为“temporal”。默认为“None”,代表按样本赋权(1D权)。如果模型有多个输出,可以向该参数传入指定sample_weight_mode的字典或列表。在下面fit函数的解释中有相关的参考内容。 #weighted_metrics: metrics列表,在训练和测试过程中,这些metrics将由sample_weight或clss_weight计算并赋权
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
es = EarlyStopping('val_loss', patience=patience, mode="min") msave = Mycbk(model, './' + save_base_name + '/'+filepath) file_dir = './' + save_base_name + '/log/'+ time.strftime('%Y_%m_%d',time.localtime(time.time())) if not os.path.exists(file_dir): os.makedirs(file_dir) tb_log = TensorBoard(log_dir=file_dir) reduce_lr = ReduceLROnPlateau(monitor='val_loss', factor=0.1, patience=4, verbose=0, mode='min', epsilon=-0.95, cooldown=0, min_lr=1e-8) log_cv = CSVLogger('./' + save_base_name + '/' + time.strftime('%Y_%m_%d',time.localtime(time.time())) +'_log.csv', separator=',', append=True) return [es, msave,reduce_lr,tb_log,log_cv] os.environ["CUDA_VISIBLE_DEVICES"] ='0,1' # 按需进行设置,如果只用一张显卡,可以忽略下面的multi_gpu_model(model, gpus=n)及其相关语句 if __name__ == '__main__': Xception_model = Xception(weights=None, include_top=False, input_shape=(299,299,3),pooling='Average') #实际上也可以设置weights='imagenet',不过这样做的话框架判断用户目录下,即~/.keras/models判断是否有对应的权重文件, # 没有的话会执行下载,建议自己另外加载就好。具体参看http://192.168.3.126/Kwong/totem_begin/blob/master/README.md # Xception_model = load_model('xception_weights_tf_dim_ordering_tf_kernels_notop.h5') # 如果加载的是imageNet预训练权重,需要添加另外的预处理方法(去imageNet数据集的均值和标准差) # 详见:http://192.168.3.126/Kwong/totem_begin/blob/master/tricks_in_processing_and_training/README.md top_model_avg = Sequential() top_model_avg.add(Flatten(input_shape=Xception_model.output_shape[1:],name='flatten_Average')) #top_model_avg.add(Dropout(0.5)) top_model_avg.add(BatchNormalization()) top_model_avg.add(Dense(48, name='dense_1', kernel_initializer=initializers.Orthogonal())) top_model_avg.add(advanced_activations.PReLU(alpha_initializer='zeros')) #top_model_avg.add(Dropout(0.5)) top_model_avg.add(BatchNormalization()) top_model_avg.add(Dense(2, name='dense_2', activation="softmax", kernel_initializer=initializers.Orthogonal()))
model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "vgg19": base_model = VGG19(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('fc1').output) image_size = (224, 224) elif model_name == "resnet50": base_model = ResNet50(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('flatten').output) image_size = (224, 224) elif model_name == "inceptionv3": base_model = InceptionV3(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('flatten').output) image_size = (299, 299) elif model_name == "xception": base_model = Xception(weights=weights) model = Model(input=base_model.input, output=base_model.get_layer('avg_pool').output) image_size = (299, 299) else: base_model = None print "[INFO] successfully loaded base model and model..." # path to training dataset train_labels = os.listdir(train_path) # encode the labels print("[INFO] encoding labels...") le = LabelEncoder() le.fit([tl for tl in train_labels])