def generate_data(model, batch_size): train_gen = SequenceFromDisk('train', batch_size, (299, 299), preprocessfunc=preprocess_input) valid_gen = SequenceFromDisk('validation', batch_size, (299, 299), preprocessfunc=preprocess_input) x_train = model.predict_generator(train_gen, use_multiprocessing=True, workers=8, verbose=1) x_valid = model.predict_generator(valid_gen, use_multiprocessing=True, workers=8, verbose=1) y_train = np.load(join(paths['data'], 'labels_train.npy')) y_valid = np.load(join(paths['data'], 'labels_validation.npy')) return x_train, x_valid, y_train, y_valid
iaa.Fliplr(.5), sometimes(iaa.Affine(rotate=(-20, 20))), sometimes(iaa.AddToHueAndSaturation((-20, 20))), sometimes(iaa.GaussianBlur((0, 2.))), sometimes(iaa.ContrastNormalization((.5, 1.5), per_channel=True)), sometimes(iaa.Sharpen(alpha=(0, 1.), lightness=(.75, 1.5))), sometimes(iaa.Emboss(alpha=(0, 1.), strength=(0, 2.))), sometimes(iaa.Crop(px=(5, 15))), ]) return seq.augment_images(batch) for net in networks.keys(): print('Loading training data for {}...'.format(net)) img_size = 299 val_gen = SequenceFromDisk('validation', 128, (img_size, img_size), augment) #Load the trained network from model_build path_dict = paths with tf.device('/cpu:0'): model = load_network(net) model = multi_gpu_model(model) opt = SGD(lr=1e-4, momentum=.9) model.compile(loss='binary_crossentropy', optimizer=opt) cp = MultiGPUCheckpoint(join(paths['models'], '{}_val.h5'.format(net)), monitor='loss') model.fit_generator(val_gen, epochs=num_epochs, use_multiprocessing=True,
from keras.utils import multi_gpu_model from sklearn.metrics import f1_score import keras.backend as K import numpy as np import tensorflow as tf net_filenames = [ 'models/densenet201_val.h5', 'models/inceptionresnetv2_val.h5', 'models/inceptionv3_val.h5', 'models/resnet50_val.h5', 'models/xception_val.h5', ] print('Creating test generator...') test_gen = SequenceFromDisk('test', 128, (299, 299), preprocess_input) print('Running predictions...') preds = [] for net in net_filenames: with tf.device('/cpu:0'): model = load_model(net, compile=False) model = multi_gpu_model(model) p = model.predict_generator(test_gen, use_multiprocessing=True, workers=8, verbose=1) preds.append(p) K.clear_session() preds = np.stack(preds, axis=-1)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- from fashion_code.generators import SequenceFromDisk from keras.applications.xception import preprocess_input from keras.models import load_model from sklearn.metrics import f1_score model = load_model('models/xception.h5') model.compile('adam', 'binary_crossentropy') val_gen = SequenceFromDisk('validation', 128, (299, 299), preprocess_input) labels = val_gen.get_all_labels() preds = model.predict_generator(val_gen, verbose=1) print(f1_score(labels, preds > .2, average='micro'))
def train_model(args): # Some variables batch_size = args.batch_size * args.gpus chpt = args.continue_from_chpt epochs = args.epochs lr_red = args.lr_reduction img_size = (299, 299) loss = 'binary_crossentropy' optimizer = Adam(decay=1e-6) use_multiprocessing = not args.windows workers = 0 if args.windows else 8 if args.gcp: path_dict = GCP_paths else: path_dict = paths # Create and compile model if chpt: model = load_model(join(paths['models'], chpt)) else: model = build_model(num_classes) if args.gpus > 1: model = multi_gpu_model(model, args.gpus) model.compile(optimizer=optimizer, loss=loss) # Create data generators if args.gcp: train_gen = SequenceFromGCP('validation', batch_size, img_size, preprocessfunc=preprocess_input) if args.create_submission: test_gen = SequenceFromGCP('test', batch_size, img_size, preprocessfunc=preprocess_input) else: test_gen = None else: train_gen = SequenceFromDisk('validation', batch_size, img_size, preprocessfunc=preprocess_input) if args.create_submission: test_gen = SequenceFromDisk('test', batch_size, img_size, preprocessfunc=preprocess_input) else: test_gen = None # Fit model save_file = join(paths['models'], args.save_filename) mc = ModelCheckpoint(save_file, monitor='loss', save_best_only=True) block_indices = [] for i, layer in enumerate(model.layers): if 'mixed' in layer.name and len(block_indices) < 8: block_indices.append(i) block_indices.extend([248, 279]) block_indices = block_indices[::-1] uc = Finetuning(block_indices, lr_reduction=lr_red) train_steps = args.train_steps or len(train_gen) model.fit_generator( train_gen, epochs=epochs, steps_per_epoch=train_steps, use_multiprocessing=use_multiprocessing, workers=workers, # This callback does validation, checkpointing and # submission creation callbacks=[uc, mc], verbose=1) '''
def train_model(NetworkConstr, aug_fun, preprocess_fun, args): # Some variables batch_size = args.batch_size chpt = args.continue_from_chpt epochs = args.epochs img_size = (299, 299) loss = 'binary_crossentropy' optimizer = Adam(decay=1e-6) use_multiprocessing = not args.windows workers = 0 if args.windows else 8 if args.gcp: path_dict = GCP_paths else: path_dict = paths # Create and compile model if chpt: model = load_model(join(path_dict['models'], chpt)) else: model = build_model(NetworkConstr, num_classes) if args.multi_gpu: model = multi_gpu_model(model, cpu_relocation=True) model.compile(optimizer=optimizer, loss=loss) # Create data generators if args.gcp: train_gen = SequenceFromGCP('train', batch_size, img_size, preprocessfunc=preprocess_fun) valid_gen = SequenceFromGCP('validation', batch_size, img_size, preprocessfunc=preprocess_fun) if args.create_submission: test_gen = SequenceFromGCP('test', batch_size, img_size, preprocessfunc=preprocess_fun) else: test_gen = None else: train_gen = SequenceFromDisk('train', batch_size, img_size, preprocessfunc=aug_fun) valid_gen = SequenceFromDisk('validation', batch_size, img_size, preprocessfunc=preprocess_fun) if args.create_submission: test_gen = SequenceFromDisk('test', batch_size, img_size, preprocessfunc=preprocess_fun) else: test_gen = None # Fit model if args.multi_gpu: pm = MultiGPUCheckpoint(join(path_dict['models'], args.save_filename), verbose=1) else: pm = ModelCheckpoint(join(path_dict['models'], args.save_filename), monitor='val_loss', save_best_only=True) train_steps = args.train_steps or len(train_gen) model.fit_generator(train_gen, epochs=epochs, steps_per_epoch=train_steps, validation_data=valid_gen, use_multiprocessing=use_multiprocessing, workers=workers, callbacks=[pm], verbose=1)
# TODO: add random forest classifier transformer = nn_transformer() transformer.fit(x_train, y_train, validation_data=(x_valid, y_valid), epochs=epochs, verbose=1) transformer.save(join(paths['models'], save_filename)) valid_preds = transformer.predict( x_valid, batch_size=batch_size, verbose=1) > threshold score = f1_score(y_valid, valid_preds, average='micro') print('F1 score on validation: {:.4f}'.format(score)) if args.create_submission: print('Creating submission...') test_gen = SequenceFromDisk('test', batch_size, (299, 299), preprocessfunc=preprocess_input) initial_preds = model.predict_generator(test_gen, use_multiprocessing=True, workers=8, verbose=1) y_pred = transformer.predict(initial_preds, batch_size=batch_size, verbose=1) create_submission(y_pred, save_filename) sys.exit(0)
sometimes(iaa.AddToHueAndSaturation((-20, 20))), sometimes(iaa.GaussianBlur((0, 2.))), sometimes(iaa.ContrastNormalization((.5, 1.5), per_channel=True)), sometimes(iaa.Sharpen(alpha=(0, 1.), lightness=(.75, 1.5))), sometimes(iaa.Emboss(alpha=(0, 1.), strength=(0, 2.))), sometimes(iaa.Crop(px=(5, 15))), ]) return seq.augment_images(batch) network = {'xfv3', 'xfv2'} for net in network: print('Loading training data for {}...'.format(net)) img_size = 200 val_gen = SequenceFromDisk('validation', 64, (img_size, img_size)) #Load the trained network from model_build #path_dict = paths #with tf.device('/cpu:0'): # model = load_network(net) #model = multi_gpu_model(model) # opt = SGD(lr=1e-4, momentum=.9) # model.compile(loss='binary_crossentropy', optimizer=opt) # cp = MultiGPUCheckpoint(join(paths['models'], '{}_val.h5'.format(net)), # monitor='loss') # model.fit_generator(val_gen, # epochs=num_epochs,