def train(model_config, model_name): token = '{}_{}_{}'.format('spv-cnn', model_config, model_name) checkpoint_dir = tempfile.mkdtemp(prefix=token + '_', dir=config.DIR_MODEL) path_loss_plot = os.path.join(checkpoint_dir, 'LOSS_{}.png'.format(token)) checkpoint_path = os.path.join( checkpoint_dir, 'check_gen{epoch:02d}_loss{val_loss:.2f}.hdf5') model_path = os.path.join(config.DIR_MODEL, 'MODEL_{}.hdf5'.format(token)) tee = Tee(os.path.join(config.DIR_LOG, 'LOG_{}.logg'.format(token)), 'w') # noqa: F841 # ## preproc X, Y = load_label(config.DIR_DATA) X = transform_channel(X, orig_mode='channels_first') # ## split data X_train, Y_train, X_valid, Y_valid = split_data(X, Y, ratio=0.9) X_train, Y_train = data_augmentation(X_train, Y_train) # ## small down dataset # X, Y, X_out, Y_out = split_data(X, Y, ratio=0.5) # standardize train data # orig_shape = X_train.shape # tmp_X_train = X_train.copy() # tmp_X_train.shape = (orig_shape[0], orig_shape[1]*orig_shape[2]*orig_shape[3]) # scaler = StandardScaler().fit(tmp_X_train) # tmp_X_train = scaler.transform(tmp_X_train) # tmp_X_train.shape = orig_shape # X_train = tmp_X_train if model_config == 'vgg16': model = keras.applications.vgg16.VGG16(include_top=False, weights='imagenet', input_tensor=None, input_shape=(3, 32, 32)) batch_size = 8 else: func_get_custom_model = getattr(model_configs, model_config) model, batch_size = func_get_custom_model(10, inputs=(32, 32, 3)) model.summary() model.fit(X_train, Y_train, batch_size=batch_size, epochs=40, validation_data=(X_valid, Y_valid), callbacks=[ ModelCheckpoint(checkpoint_path, monitor='val_loss'), ModelCheckpoint(model_path, save_best_only=True, monitor='val_loss', mode='min'), EarlyStopping(monitor='val_loss', patience=3, mode='min'), PlotLosses(output_img=path_loss_plot) ])
def train(model_config, model_name): token = '{}_{}_{}'.format('st-cnn', model_config, model_name) checkpoint_dir = tempfile.mkdtemp(prefix=token + '_', dir=config.DIR_MODEL) model_path = os.path.join(config.DIR_MODEL, 'MODEL_{}.hdf5'.format(token)) tee = Tee(os.path.join(config.DIR_LOG, 'LOG_{}.logg'.format(token)), 'w') # noqa: F841 # ## preproc # label data preproc LX, LY = load_label(config.DIR_DATA) LX = transform_channel(LX, orig_mode='channels_first') LX, LY, LX_valid, LY_valid = split_data(LX, LY, ratio=0.8) LX_valid, LY_valid = data_augmentation(LX_valid, LY_valid) # unlabel data preproc UX = load_unlabel(config.DIR_DATA) UX = transform_channel(UX, orig_mode='channels_first') # pretrain_model spv_token = '{}_{}_{}'.format('spv-cnn', model_config, model_name) pretrain_model_path = os.path.join(config.DIR_MODEL, 'MODEL_{}.hdf5'.format(spv_token)) if os.path.exists(pretrain_model_path): model, batch_size = _create_model(model_config, path_restore=pretrain_model_path) model.summary() else: model, batch_size = _create_model(model_config) model.summary() model.fit(LX, LY, batch_size=batch_size, epochs=5, validation_data=(LX_valid, LY_valid), callbacks=[ EarlyStopping(monitor='val_loss', patience=3, mode='min'), ]) # ## self-training num_self_train = 10 num_epochs = 10 patience = 1 relable_score = 0.92 path_best_checkpoint = None for st_round in range(1, 1 + num_self_train): print('\n\n----- Round {} -----\n\n'.format(st_round)) round_token = token + '_round{}'.format(st_round) path_loss_plot = os.path.join(checkpoint_dir, 'LOSS_{}.png'.format(round_token)) checkpoint_path = os.path.join( checkpoint_dir, 'check_round{}'.format(st_round) + '_gen{epoch:02d}_loss{val_loss:.2f}.hdf5') # restore model if path_best_checkpoint is not None: model, batch_size = _create_model( model_config, path_restore=path_best_checkpoint) # add predicted unlabel data above relable_score X_train, Y_train = LX, LY ux, uy = _select_unlabeled_above_relable(UX, model, relable_score) if ux is not None: num_add_unlabeled = ux.shape[0] print 'add unlabeled data: ', num_add_unlabeled X_train = np.concatenate((X_train, ux), axis=0) Y_train = np.concatenate((Y_train, uy), axis=0) X_train, Y_train = data_augmentation(X_train, Y_train) model.fit( X_train, Y_train, batch_size=batch_size, epochs=num_epochs, validation_data=(LX_valid, LY_valid), callbacks=[ ModelCheckpoint(checkpoint_path, monitor='val_loss'), EarlyStopping(monitor='val_loss', patience=patience, mode='min'), PlotLosses(output_img=path_loss_plot) ], verbose=1, ) del model # select best model checkpoints = filter(lambda x: '_loss' in x, os.listdir(checkpoint_dir)) best_checkpoint = sorted( checkpoints, key=lambda x: float((x.split('_loss')[1]).replace('.hdf5', '')))[0] print 'best checkpoint right now: ', best_checkpoint path_best_checkpoint = os.path.join(checkpoint_dir, best_checkpoint) copyfile(path_best_checkpoint, model_path)
import pickle import math from shutil import copyfile import time from common import load_label, load_unlabel from common import split_data from common import transform_channel from common import data_augmentation from keras.models import load_model # label data preproc folder = os.path.join(PATH,'data') LX,LY = load_label(folder) LX = transform_channel(LX,orig_mode="channels_first") LX,LY,X_valid,Y_valid = split_data(LX,LY,ratio=0.9) # unlabel data preproc UX = load_unlabel(folder) UX = transform_channel(UX,orig_mode="channels_first") # load model from models_supervised_cnn import YCNet3 model_input = os.path.join(PATH,'model','model_cnn_gen15_loss1.07_acc67.6.hdf5') # path or None if os.path.isfile(model_input): model,batch_size = YCNet3(10, inputs=(32,32,3), file_load_weights=model_input) else:
def train(model_config, model_name): token = '{}_{}_{}'.format('ae-cnn', model_config, model_name) checkpoint_dir = tempfile.mkdtemp(prefix=token + '_', dir=config.DIR_MODEL) path_loss_plot = os.path.join(checkpoint_dir, 'LOSS_{}.png'.format(token)) checkpoint_path = os.path.join( checkpoint_dir, 'check_gen{epoch:02d}_loss{val_loss:.2f}.hdf5') model_path = os.path.join(config.DIR_MODEL, 'MODEL_{}.hdf5'.format(token)) tee = Tee(os.path.join(config.DIR_LOG, 'LOG_{}.logg'.format(token)), 'w') # noqa: F841 # ## preproc # label data preproc LX, LY = load_label(config.DIR_DATA) LX = transform_channel(LX, orig_mode='channels_first') LX, LY, X_valid, Y_valid = split_data(LX, LY, ratio=0.9) # unlabel data preproc UX = load_unlabel(config.DIR_DATA) UX = transform_channel(UX, orig_mode='channels_first') func_get_aec = getattr(ae_classifier_configs, model_config) autoencoder_classifier = func_get_aec(10, inputs=(32, 32, 3)) # pretrain autoencoder train_ae_X = np.concatenate((LX, UX), axis=0) train_ae_X, _ = data_augmentation(train_ae_X, np.ones((train_ae_X.shape[0], 1))) normal_train_ae_X = np.asarray(train_ae_X, dtype='float32') / 255.0 normal_X_valid = np.asarray(X_valid, dtype='float32') / 255.0 ae, batch_ae = autoencoder_classifier.get_autoencoder() ae.summary() ae.fit( train_ae_X, normal_train_ae_X, batch_size=batch_ae, epochs=5, validation_data=(X_valid, normal_X_valid), verbose=1, ) # autoencoder_classifier.freeze_ae_layers() # train train_X, train_Y = data_augmentation(LX, LY) ae_classifier, batch_ae_classifier = autoencoder_classifier.get_ae_classifier( ) ae_classifier.summary() ae_classifier.fit(train_X, train_Y, batch_size=batch_ae_classifier, epochs=60, validation_data=(X_valid, Y_valid), verbose=1, callbacks=[ ModelCheckpoint(checkpoint_path, monitor='val_loss'), ModelCheckpoint(model_path, save_best_only=True, monitor='val_loss', mode='min'), EarlyStopping(monitor='val_loss', patience=3, mode='min'), PlotLosses(output_img=path_loss_plot) ])