def test_sklearn_repeated(): """Test two runs with the same random seed give the same results.""" result_dir = project_dir + '/results/tests/' config1 = {'model_name': 'SklearnModel', 'best': False, 'debug': False, 'random_seed': 42, 'result_dir': result_dir + '1', 'data_dir': project_dir + '/data', 'max_iter': 10, 'tol': 0, 'C': 1, 'R': 5, 'K': 3, 'method': 'mean', 'n_samples': 1, 'solver': 'saga'} # Train model1. model1 = make_model(config1) model1.train() # Train model2. config2 = copy.deepcopy(config1) model2 = make_model(config2) model2.train() # Make sure they give same results. assert (model1.lr.intercept_ == model2.lr.intercept_).all() assert (model1.lr.coef_ == model2.lr.coef_).all() assert (model1.lr.n_iter_ == model2.lr.n_iter_).all()
def define_models(self): n_latent_scales = 2 n_scales = 1 + int(np.round(np.log2( self.img_shape[0]))) - self.bottleneck_factor n_filters = 32 self.enc_up_pass = models.make_model( "enc_up", models.enc_up, n_scales=n_scales - self.box_factor, n_filters=n_filters * 2**self.box_factor) self.enc_down_pass = models.make_model("enc_down", models.enc_down, n_scales=n_scales - self.box_factor, n_latent_scales=n_latent_scales) self.dec_up_pass = models.make_model("dec_up", models.dec_up, n_scales=n_scales, n_filters=n_filters) self.dec_down_pass = models.make_model("dec_down", models.dec_down, n_scales=n_scales, n_latent_scales=n_latent_scales) self.dec_params = models.make_model("dec_params", models.dec_parameters)
def main(_): # config = flags.FLAGS.__flags.copy() # fixed_params must be a string to be passed in the shell, let's use JSON config.fixed_params = json.loads(config.fixed_params) high_res_protein_feature_filenames = sorted( glob.glob(os.path.join(config.data_dir, "*protein_features.npz"))) high_res_grid_feature_filenames = sorted( glob.glob(os.path.join(config.data_dir, "*residue_features.npz"))) validation_end = int( len(high_res_protein_feature_filenames) * (1. - config.test_fraction)) train_end = validation_start = int(validation_end * (1. - config.validation_fraction)) if not config.mode == 'infer' and not config.mode == 'test': train_data = SparseGenerator() train_data.load_data(high_res_protein_feature_filenames[:train_end], high_res_grid_feature_filenames[:train_end]) validation_data = SparseGenerator() validation_data.load_data( high_res_protein_feature_filenames[ validation_start:validation_end], high_res_grid_feature_filenames[validation_start:validation_end]) elif config.mode == 'test': test_data = SparseGenerator() test_data.load_data( high_res_protein_feature_filenames[validation_end:], high_res_grid_feature_filenames[validation_end:]) if config.fullsearch: pass # Some code for HP search ... elif config.dry_run: model = make_model(config) else: model = make_model(config) if config.mode == 'infer': high_res_protein_feature_filenames = sorted( glob.glob( os.path.join(config.pdb_folder, "*protein_features.npz"))) high_res_grid_feature_filenames = sorted( glob.glob( os.path.join(config.pdb_folder, "*residue_features.npz"))) infer_data = SparseGenerator() infer_data.load_data(high_res_protein_feature_filenames, high_res_grid_feature_filenames) model.infer(infer_data, config.residue_index) else: if config.mode == 'test': model.test(test_data) elif config.mode == 'train': model.train(train_data, validation_data) model.save('end')
def train(): logger.info('Initializing....') cudnn.enable = True cudnn.benchmark = True # torch.manual_seed(1) # torch.cuda.manual_seed(1) write_config_into_log(cfg) logger.info('Building model......') if cfg.pretrained: model = make_model(cfg) model.load_param(cfg) logger.info('Loaded pretrained model from {0}'.format(cfg.pretrained)) else: model = make_model(cfg) model.cuda() model = torch.nn.DataParallel(model) optimizer = torch.optim.Adam( [{ 'params': model.module.base.parameters(), 'lr': cfg.get_lr(0)[0] }, { 'params': model.module.classifiers.parameters(), 'lr': cfg.get_lr(0)[1] }], weight_decay=cfg.weight_decay) celoss = nn.CrossEntropyLoss().cuda() softloss = SoftLoss() sp_kd_loss = SP_KD_Loss() criterions = [celoss, softloss, sp_kd_loss] train_loader, val_loader = make_dataloader(cfg) logger.info('Begin training......') for epoch in range(cfg.start_epoch, cfg.max_epoch): train_one_epoch(train_loader, val_loader, model, criterions, optimizer, epoch, cfg) total_acc = test(cfg, val_loader, model, epoch) with open(cfg.test_log, 'a+') as f: f.write('Epoch {0}: Acc is {1:.4f}\n'.format(epoch, total_acc)) torch.save(obj=model.state_dict(), f=os.path.join( cfg.snapshot_dir, 'ep{}_acc{:.4f}.pth'.format(epoch, total_acc))) logger.info('Model saved')
def predictor(q): model = make_model(network, (None, None, channels)) model.load_weights('trained_models/' + weights) while True: f, image = q.get() if image is None: break preds = [] for tta in [None, 'hflip', 'vflip', 'hflip+vflip']: ttas = [] if tta: ttas = tta.split("+") img = do_tta(image, ttas) pred = model.predict(np.expand_dims(img, axis=0), batch_size=1, verbose=0)[0] pred = undo_tta(pred, ttas) preds.append(pred) mask = np.average(np.array(preds), axis=0) all_masks_dir = "all_masks" os.makedirs(all_masks_dir, exist_ok=True) model_mask_dir = os.path.join(all_masks_dir, out_dir) os.makedirs(model_mask_dir, exist_ok=True) cv2.imwrite(os.path.join(model_mask_dir, f + ".png"), mask * 255) del model K.clear_session()
def main(model_config, save_outputs, output_dir, data_config, seed, small_run, dataset_type, entry, device): # Load the model model = make_model(**model_config) model.sinkhorn_opt.to(device) from tensorboardX import SummaryWriter from datetime import datetime # Load the data train, test = load_data(dorn_mode=False) dataset = train if dataset_type == "train" else test eval_fn = lambda input_, device: model.evaluate( input_["rgb"], input_["crop"][0, :], input_["depth_cropped"], torch.ones_like(input_["depth_cropped"]), device) init_randomness(seed) if entry is None: print("Evaluating the model on {}.".format(data_config["data_name"])) evaluate_model_on_dataset(eval_fn, dataset, small_run, device, save_outputs, output_dir) else: print("Evaluating {}".format(entry)) model.sinkhorn_opt.writer = SummaryWriter(log_dir=os.path.join("runs", datetime.now().strftime('%b%d'), datetime.now().strftime('%H-%M-%S_') + \ "densedepth_hist_match_wass")) evaluate_model_on_data_entry(eval_fn, dataset, entry, device, save_outputs, output_dir)
def run_experiment(cfg_dict): device = utils.get_device() expand_cfg(cfg_dict) wandb.login() with wandb.init(project=cfg_dict['project_name'], config=cfg_dict, notes=cfg_dict['run_description']) as wandb_run: cfg = wandb_run.config model = models.make_model(**cfg.model).to(device) model = model.apply(models.init_weights) trainloader = data.make_loader(**cfg.train_dataset) testloader = data.make_loader(**cfg.test_dataset) samples = [testloader.dataset[i][0] for i in range(8)] if wandb_run.name: filename = wandb_run.name else: filename = "checkpoint_" + datetime.date.today().strftime("%d%m%Y") save_path = os.path.join(cfg.save_dir, filename + "_best.pt") train = trainer.Trainer(save_path, device, model, trainloader, testloader, samples, **cfg.trainer) train.train()
def main(model_config, save_outputs, output_dir, data_config, seed, small_run, dataset_type, entry): # Load the model model = make_model(**model_config) # model.sid_obj.to(device) from tensorboardX import SummaryWriter from datetime import datetime # Load the data train, test = load_data(dorn_mode=False) dataset = train if dataset_type == "train" else test eval_fn = lambda input_, device: model.evaluate( input_["rgb"], input_["crop"][0, :], input_["depth_cropped"], input_[ "rawdepth_cropped"], input_["mask_cropped"], torch.ones_like(input_["depth_cropped"])) init_randomness(seed) if entry is None: print("Evaluating the model on {}.".format(data_config["data_name"])) evaluate_model_on_dataset(eval_fn, dataset, small_run, None, save_outputs, output_dir) else: print("Evaluating {}".format(entry)) evaluate_model_on_data_entry(eval_fn, dataset, entry, None, save_outputs, output_dir)
def main(mode, test_data_path, model_path, log_path): from input import max_visits_count, sample_iterator from models import make_model conf = SparkConf().set("spark.sql.shuffle.partitions", "4096") \ .set("spark.python.worker.memory", "2g") \ .set("spark.memory.storageFraction", "0.1") \ .set("spark.ui.showConsoleProgress", "false") sc = SparkContext(conf=conf) sqlContext = SQLContext(sc) model = make_model(mode) if os.path.isfile(model_path): print 'Loading model from: %s' % model_path model.load_weights(model_path) test_combined_df = sqlContext.read.parquet(test_data_path) training_generator = sample_iterator(test_combined_df, mode, loop=False) cm = np.zeros((max_visits_count, max_visits_count), dtype=int) test_size = 0 for X, Y in training_generator: P = model.predict_on_batch(X) cm += confusion_matrix(np.argmax(P, 1), np.argmax(Y, 1), range(max_visits_count)) test_size += X.shape[0] print 'Confusion Matrix:' print cm print '-' * 20 print 'Accuracy: %s' % (cm.diagonal().sum() / float(cm.sum())) print 'Samples: %s' % test_size plot_confusion_matrix(cm, log_path + '.png')
def query(cfg, query_index): # device num_gpus = 0 if cfg.DEVICE == 'cuda': os.environ['CUDA_VISIBLE_DEVICES'] = cfg.DEVICE_ID num_gpus = len(cfg.DEVICE_ID.split(',')) print("Using {} GPUs.\n".format(num_gpus)) device = torch.device(cfg.DEVICE) # data train_loader, query_loader, gallery_loader, num_classes = make_loader(cfg) # model model = make_model(cfg, num_classes=num_classes) model.load_state_dict( torch.load( os.path.join(cfg.OUTPUT_DIR, model.__class__.__name__ + '_best.pth'))) if num_gpus > 1: model = nn.DataParallel(model) queryer = Queryer(model=model, query_loader=query_loader, gallery_loader=gallery_loader, device=device) queryer.query(idx=query_index) print('Done.')
def __init__(self): super(DeepFool, self).__init__() self.num_label = args.df_num_label self.overshot = args.df_overshot # initialize net if args.dataset in ['mnist', 'cifar10', 'svhn']: num_classes = 10 elif args.dataset in ['cifar100']: num_classes = 100 elif args.dataset in ['imagenet']: num_classes = 1000 else: raise NotImplementedError kwargs = {'num_classes': num_classes} if args.arch in ['LiuNet']: kwargs['num_group'] = args.num_group self.net = make_model(args.dataset, args.arch, **kwargs) # set dropout probability (if there are any dropout layers inside self.net) for m in self.net.modules(): if isinstance(m, DropoutFreeze): m.p = args.dropout_p log.info(self.net) # misc self.eps = 5e-6 if args.dataset == 'mnist' else 1e-5 # protect norm against nan self.not_done = None
def main(): """ Main process. """ args = parse_cli_args() config = TrainConfig() train_ds = Dataset(args.train_path) valid_ds = Dataset(args.valid_path) model = make_model() optimizer = getattr(optim, config.optimizer_name)(model.parameters(), lr=config.learning_rate) training = Training( train_ds, valid_ds, model, optimizer, config.batch_size, config.epochs, ) training.train()
def main(model_config, dataset_type, save_outputs, output_dir, data_config, seed, small_run, device): # Load the model model = make_model(**model_config) model.eval() model.to(device) model.sid_obj.to(device) # Load the data train, test = load_data(dorn_mode=True) dataset = test if dataset_type == "test" else train print( list((name, entry.shape) for name, entry in dataset[0].items() if isinstance(entry, torch.Tensor))) init_randomness(seed) eval_fn = lambda input_, device: model.evaluate( input_["bgr"].to(device), input_["bgr_orig"].to(device), input_["crop"] [0, :], input_["depth_cropped"].to(device), input_["depth"].to(device), torch.ones_like(input_["depth_cropped"]).to(device), device) print("Evaluating the model on {} ({})".format(data_config["data_name"], dataset_type)) evaluate_model_on_dataset(eval_fn, dataset, small_run, device, save_outputs, output_dir)
def main(model_config, save_outputs, output_dir, data_config, seed, small_run, entry, device): # Load the model model = make_model(**model_config) # model.sid_obj.to(device) # print(model) model.to(device) from tensorboardX import SummaryWriter from datetime import datetime # model.writer = SummaryWriter(log_dir=os.path.join("runs", # datetime.now().strftime('%b%d'), # datetime.now().strftime('%H-%M-%S_') + \ # "dorn_sinkhorn_opt")) # Load the data dataset = load_data(dorn_mode=True) eval_fn = lambda input_, device: model.evaluate( input_["rgb_cropped"].to(device), input_["rgb_cropped_orig"].to( device), input_["spad"].to(device), input_["mask_orig"].to(device), input_["depth_cropped_orig"].to(device), device) init_randomness(seed) if entry is None: print("Evaluating the model on {}.".format(data_config["data_name"])) evaluate_model_on_dataset(eval_fn, dataset, small_run, device, save_outputs, output_dir) else: print("Evaluating {}".format(entry)) evaluate_model_on_data_entry(eval_fn, dataset, entry, device, save_outputs, output_dir)
def main(): number_suggestions = args.number_suggestions length_suggestions = args.length_suggestions with open("config_ptb.yaml", 'r') as configfile: config = yaml.load(configfile) tf.logging.set_verbosity(config['general']['logging']) model_name = config['general']['model_name'] tf.logging.info('Initializing the model: {}'.format(model_name)) model = make_model(config) if config['train']: tf.logging.info('Training {}'.format(model_name)) model.train() if config['predict']: primer_words = args.primer_words.lower().split() assert primer_words, 'At least one primer word must be provided!' prediction = model.predict(primer_words, number_suggestions, length_suggestions) for i in range(len(prediction)): tf.logging.info('Predicted sentence {}: {}'.format( i + 1, prediction[i])) if config['test']: tf.logging.info('Testing') model.test()
def train_lightgbm(x_train, train_labels, x_test, orig_test): """ read imputed features trains NUM light gbm model and saves to an pickle object call submission_lgbm to predict and create submission dataframe """ train_labels = train_labels['Col2'] num_lgbm_ensemble = 17 lgb_forests = [] for i in range(num_lgbm_ensemble): print("training LGBC model {}".format(i)) params = { 'n_estimators': 17, 'max_depth': 7, 'learning_rate': 0.01, 'random_state': i, 'colsample_bytree': 0.1, 'reg_lambda': 15, 'reg_alpha': 10 } lgbc = models.make_model(params=params, model_name='light_gbm') lgbc.fit(x_train, train_labels) lgb_forests.append(lgbc) model_file_path = os.path.join(MODEL_PATH, "lgb", "lgb_forest.pkl") pickle.dump(lgb_forests, open(model_file_path, 'wb')) evaluation.submission_lgbm(model_file_path, x_test, orig_test, submission_name='submission_lgb.csv')
def main(model_config, dataset_type, save_outputs, output_dir, data_config, seed, small_run, entry, device): # Load the model model = make_model(**model_config) # model.sid_obj.to(device) # print(model) model.to(device) # Load the data _, val, test = load_data() dataset = test if dataset_type == "test" else val init_randomness(seed) if entry is None: print("Evaluating the model on {} ({})".format(data_config["data_name"], dataset_type)) evaluate_model_on_dataset(model, dataset, small_run, device, save_outputs, output_dir) else: print("Evaluating {}".format(entry)) evaluate_model_on_data_entry(model, dataset, entry, device)
def main(model_config, save_outputs, output_dir, data_config, seed, small_run, entry, device): # Load the model model = make_model(**model_config) model.eval() model.to(device) model.sid_obj.to(device) # Load the data dataset = load_data(dorn_mode=True) eval_fn = lambda input_, device: model.evaluate( input_["rgb_cropped"].to(device), input_["rgb_cropped_orig"].to( device), input_["depth_cropped_orig"].to(device), input_[ "mask_orig"].to(device), device) init_randomness(seed) if entry is None: print("Evaluating the model on {}.".format(data_config["data_name"])) evaluate_model_on_dataset(eval_fn, dataset, small_run, device, save_outputs, output_dir) else: print("Evaluating {}".format(entry)) evaluate_model_on_data_entry(eval_fn, dataset, entry, device, save_outputs, output_dir)
def make_cv_on_current_set(data_stuff, indices_to_use, config): '''On current set, perform cv and extract model, score and best iteration ''' history = [] N_inputs, input_shape = get_input_shapes(data_stuff, config) #for cv_train_idx, cv_val_idx in pu_cv_splitter(indices_to_use, data_stuff[1]): def compute_fold(ARG): cv_train_idx, cv_val_idx = ARG train = slice_data(data_stuff, cv_train_idx, N_inputs) val = slice_data(data_stuff, cv_val_idx, N_inputs) model, metric = make_model(input_shape, data_stuff[1].shape, config['model_params']) logger.info('Model build') result = model.fit(train[0], train[1], sample_weight=train[2], validation_data=val[:2], **config['training_cfg']) return result.history, metric # history.append(result.history) splits = pu_cv_splitter(indices_to_use, data_stuff[1]) if args.nproc <= 1: history = [compute_fold(ARG) for ARG in splits] else: p = Pool(args.nproc) history = p.map(compute_fold, splits) del p metric = history[0][1] history = [ARG[0] for ARG in history] #====== Average =============== cv_av, cv_std = make_av_std(history, metric) cv_val_av, cv_val_std = make_av_std(history, 'val_%s' % metric) best_cv_idx = cv_val_av.argmax() #====== test =========== logger.info('Making Final model') train_stuff = slice_data(data_stuff, indices_to_use, N_inputs) model, metric = make_model(input_shape, data_stuff[1].shape, config['model_params']) logger.info('Model build') saver = SaveSelected(best_cv_idx) result = model.fit(train_stuff[0], train_stuff[1], sample_weight=train_stuff[2], callbacks=[saver], **config['training_cfg']) saver.reset() return { 'train': (cv_av, cv_std), 'val': (cv_val_av, cv_val_std), 'it': best_cv_idx, 'model': model }
def train(cfg): # output output_dir = cfg.OUTPUT_DIR if os.path.exists(output_dir): raise KeyError("Existing path: ", output_dir) else: os.makedirs(output_dir) with open(os.path.join(output_dir, 'config.yaml'), 'w') as f_out: print(cfg, file=f_out) # logger logger = make_logger("project", output_dir, 'log') # device num_gpus = 0 if cfg.DEVICE == 'cuda': os.environ['CUDA_VISIBLE_DEVICES'] = cfg.DEVICE_ID num_gpus = len(cfg.DEVICE_ID.split(',')) logger.info("Using {} GPUs.\n".format(num_gpus)) cudnn.benchmark = True device = torch.device(cfg.DEVICE) # data train_loader, query_loader, gallery_loader, num_classes = make_loader(cfg) # model model = make_model(cfg, num_classes=num_classes) if num_gpus > 1: model = nn.DataParallel(model) # solver criterion = make_loss(cfg, num_classes) optimizer = make_optimizer(cfg, model) scheduler = make_scheduler(cfg, optimizer) # do_train trainer = Trainer(model=model, optimizer=optimizer, criterion=criterion, logger=logger, scheduler=scheduler, device=device) trainer.run(start_epoch=0, total_epoch=cfg.SOLVER.MAX_EPOCHS, train_loader=train_loader, query_loader=query_loader, gallery_loader=gallery_loader, print_freq=cfg.SOLVER.PRINT_FREQ, eval_period=cfg.SOLVER.EVAL_PERIOD, out_dir=output_dir) print('Done.')
def test_gan_steps(self): self.skipTest('legacy') ds_train, ds_val, ds_info = data.load_datasets(self.args) gan = models.make_model(self.args, ds_info['channels']) img = next(iter(ds_train)) disc_vals = gan.disc_grad(img) gen_vals = gan.gen_grad(img) self.assertIsInstance(disc_vals, dict) self.assertIsInstance(gen_vals, dict) for k, v in list(disc_vals.items()) + list(gen_vals.items()): tf.debugging.assert_shapes([(v, [])])
def compute_fold(ARG): cv_train_idx, cv_val_idx = ARG train = slice_data(data_stuff, cv_train_idx, N_inputs) val = slice_data(data_stuff, cv_val_idx, N_inputs) model, metric = make_model(input_shape, data_stuff[1].shape, config['model_params']) logger.info('Model build') result = model.fit(train[0], train[1], sample_weight=train[2], validation_data=val[:2], **config['training_cfg']) return result.history, metric
def test(cfg, flip): # device num_gpus = 0 if cfg.DEVICE == 'cuda': os.environ['CUDA_VISIBLE_DEVICES'] = cfg.DEVICE_ID num_gpus = len(cfg.DEVICE_ID.split(',')) print("Using {} GPUs.\n".format(num_gpus)) cudnn.benchmark = True device = torch.device(cfg.DEVICE) # data train_loader, query_loader, gallery_loader, num_classes = make_loader(cfg) # model model = make_model(cfg, num_classes=num_classes) model.load_state_dict( torch.load( os.path.join(cfg.OUTPUT_DIR, model.__class__.__name__ + '_best.pth'))) if num_gpus > 1: model = nn.DataParallel(model) model = model.to(device) evaluator = Evaluator(model) # Results cmc, mAP = evaluator.evaluate(query_loader, gallery_loader) ranks = [1, 5, 10] print("Results ----------") print("mAP: {:.1%}".format(mAP)) print("CMC curve") for r in ranks: print("Rank-{:<3}: {:.1%}".format(r, cmc[r - 1])) print("------------------\n") # Results with flip if flip: print("Results with flip --------------") query_flip_loader, gallery_flip_loader = make_loader_flip(cfg) cmc, mAP = evaluator.evaluate_flip(query_loader, gallery_loader, query_flip_loader, gallery_flip_loader) print("Results ----------") print("mAP: {:.1%}".format(mAP)) print("CMC curve") for r in ranks: print("Rank-{:<3}: {:.1%}".format(r, cmc[r - 1])) print("------------------\n") print('Done')
def test_sklearn_max_iter(): """Test that a SklearnModel run with 2 episodes of max_iter, gives the same results as when run with one episode of 2*max_iter, with the same starting seed. Note this does not work for sag and saga algorithms due to the internal state of the optimisation not being saved between runs.""" result_dir = project_dir + '/results/tests/' config1 = {'model_name': 'SklearnModel', 'best': False, 'debug': False, 'random_seed': 42, 'result_dir': result_dir + '1', 'data_dir': project_dir + '/data', 'max_iter': 10, 'tol': 0, 'C': 1, 'R': 5, 'K': 3, 'method': 'mean', 'n_samples': 1, 'solver': 'newton-cg'} # Train model1 twice. model1 = make_model(config1) model1.train() model1.train() # Train model2 once (but with twice the iterations). config2 = copy.deepcopy(config1) config2['max_iter'] = 20 model2 = make_model(config2) model2.train() # Make sure they give same results. assert (model1.lr.intercept_ == model2.lr.intercept_).all() assert (model1.lr.coef_ == model2.lr.coef_).all()
def define_models(self): n_latent_scales = 2 n_scales = 1 + int(np.round(np.log2(self.img_shape[0]))) - 2 n_filters = 32 redux = 2 self.enc_up_pass = models.make_model("enc_up", models.enc_up, n_scales=n_scales - redux, n_filters=n_filters * 2**redux) self.enc_down_pass = models.make_model("enc_down", models.enc_down, n_scales=n_scales - redux, n_latent_scales=n_latent_scales) self.dec_up_pass = models.make_model("dec_up", models.dec_up, n_scales=n_scales, n_filters=n_filters) self.dec_down_pass = models.make_model("dec_down", models.dec_down, n_scales=n_scales, n_latent_scales=n_latent_scales) self.dec_params = models.make_model("dec_params", models.dec_parameters)
def __init__(self, dataset, arch, no_grad=True, **kwargs): super(StandardModel, self).__init__() # init cnn model self.cnn = make_model(dataset, arch, **kwargs) self.cnn.to(device) # init cnn model meta-information self.mean = torch.FloatTensor(self.cnn.mean).view(1, 3, 1, 1).to(device) self.std = torch.FloatTensor(self.cnn.std).view(1, 3, 1, 1).to(device) self.input_space = self.cnn.input_space # 'RGB' or 'GBR' self.input_range = self.cnn.input_range # [0, 1] or [0, 255] self.input_size = self.cnn.input_size self.no_grad = no_grad
def train_xg_boost(x_train, train_labels, x_test, orig_test): train_labels = train_labels['Col2'] n_estimators = 2 max_depth = 2 params = {'n_estimators': n_estimators, 'max_depth': max_depth} xgb = models.make_model(params, model_name='xg_boost') model_file_path = os.path.join(MODEL_PATH, "xgb", "xgb.pkl") print(model_file_path) xgb.fit(x_train, pd.DataFrame(train_labels, columns=['Col2']).values.ravel()) pickle.dump(xgb, open(model_file_path, 'wb')) evaluation.submission_default(xgb, x_test, orig_test, submission_name='submission_xgb.csv')
def get_confusion_matrix(): gen = ImageDataGenerator() test_input_path = '../input/valid/' classes = [str(i) for i in range(80)] test_generator = gen.flow_from_directory(test_input_path, model_image_size, shuffle=False, classes=classes, batch_size=pred_batch, class_mode='categorical') y_test = test_generator.classes # X_valid, labels=load_valid(valid_df,299) import pickle # with open('cache/X_valid_299.pkl','wb') as fout: # pickle.dump(X_valid,fout) # with open('cache/y_valid_299.pkl','wb') as fout: # pickle.dump(labels,fout) # with open('cache/X_valid_299.pkl','rb') as fout: # X_valid=pickle.load(fout) # with open('cache/y_valid_299.pkl','rb') as fout: # labels=pickle.load(fout) # X_valid=np.array(X_valid) # X_valid=X_valid/255 # X_valid=preprocess_input(X_valid) # model=get_incept3_model("adam",0.5,0.001,173) model = make_model('adam', 0.5, 0.001, 0, 173, 96) # model=senet(img_size=64) # model.load_weights(filepath="weights/senet_01_1.524.h5") model.load_weights( filepath= "weights/6-optimizer,adam-dropout,0.5-lr,0.0001-tune_layer,(140,213,126)__000- Acc_0.937.h5" ) y_pred = model.predict_generator( test_generator, steps=test_generator.samples // pred_batch + 1, verbose=1) # y_pred=model.predict(X_valid,batch_size=pred_batch) # y_test=labels print("y_test", y_test[0]) print(y_pred.shape) print(np.argmax(y_pred[0])) valid_preds = get_argmax(y_pred) print(accuracy_score(y_test, valid_preds)) print(classification_report(y_test, valid_preds)) cf = confusion_matrix(y_test, valid_preds) print(cf)
def main(model_config, save_outputs, output_dir, data_config, seed, small_run, device): # Load the model model = make_model(**model_config) model.eval() model.to(device) # model.sid_obj.to(device) # Load the data dataset = load_data() init_randomness(seed) print("Evaluating the model on {}".format(data_config["data_name"])) evaluate_model_on_dataset(model, dataset, small_run, device, save_outputs, output_dir)
def run(args): # Setup strategy = setup(args) # Data ds_train, ds_val, ds_info = load_datasets(args) # Models with strategy.scope(): model = make_model(args, ds_info['channels']) if args.model == 'gan': fid_model = fid.FID(args.debug) else: fid_model = None # Train train(args, model, ds_train, ds_val, ds_info, fid_model)
def main(_): config = flags.FLAGS.__flags.copy() # fixed_params must be a string to be passed in the shell, let's use JSON config["fixed_params"] = json.loads(config["fixed_params"]) if config['fullsearch']: print('Hyperparameter search not implemented yet') else: model = make_model(config) if config['infer']: # Some code for inference ... model.infer() elif config['test']: model.test() else: # Some code for training ... model.train()
def main(): if args.crop_size: print('Using crops of shape ({}, {})'.format(args.crop_size, args.crop_size)) else: print('Using full size images') all_ids = np.array(generate_ids(args.data_dirs, args.clahe)) np.random.seed(args.seed) kfold = KFold(n_splits=args.n_folds, shuffle=True) splits = [s for s in kfold.split(all_ids)] folds = [int(f) for f in args.fold.split(",")] for fold in folds: encoded_alias = encode_params(args.clahe, args.preprocessing_function, args.stretch_and_mean) city = "all" if args.city: city = args.city.lower() best_model_file = '{}/{}_{}_{}.h5'.format(args.models_dir, encoded_alias, city, args.network) channels = 8 if args.ohe_city: channels = 12 model = make_model(args.network, (None, None, channels)) if args.weights is None: print('No weights passed, training from scratch') else: print('Loading weights from {}'.format(args.weights)) model.load_weights(args.weights, by_name=True) freeze_model(model, args.freeze_till_layer) optimizer = RMSprop(lr=args.learning_rate) if args.optimizer: if args.optimizer == 'rmsprop': optimizer = RMSprop(lr=args.learning_rate) elif args.optimizer == 'adam': optimizer = Adam(lr=args.learning_rate) elif args.optimizer == 'sgd': optimizer = SGD(lr=args.learning_rate, momentum=0.9, nesterov=True) train_ind, test_ind = splits[fold] train_ids = all_ids[train_ind] val_ids = all_ids[test_ind] if args.city: val_ids = [id for id in val_ids if args.city in id[0]] train_ids = [id for id in train_ids if args.city in id[0]] print('Training fold #{}, {} in train_ids, {} in val_ids'.format(fold, len(train_ids), len(val_ids))) masks_gt = get_groundtruth(args.data_dirs) if args.clahe: template = 'CLAHE-MUL-PanSharpen/MUL-PanSharpen_{id}.tif' else: template = 'MUL-PanSharpen/MUL-PanSharpen_{id}.tif' train_generator = MULSpacenetDataset( data_dirs=args.data_dirs, wdata_dir=args.wdata_dir, clahe=args.clahe, batch_size=args.batch_size, image_ids=train_ids, masks_dict=masks_gt, image_name_template=template, seed=args.seed, ohe_city=args.ohe_city, stretch_and_mean=args.stretch_and_mean, preprocessing_function=args.preprocessing_function, crops_per_image=args.crops_per_image, crop_shape=(args.crop_size, args.crop_size), random_transformer=RandomTransformer(horizontal_flip=True, vertical_flip=True), ) val_generator = MULSpacenetDataset( data_dirs=args.data_dirs, wdata_dir=args.wdata_dir, clahe=args.clahe, batch_size=1, image_ids=val_ids, image_name_template=template, masks_dict=masks_gt, seed=args.seed, ohe_city=args.ohe_city, stretch_and_mean=args.stretch_and_mean, preprocessing_function=args.preprocessing_function, shuffle=False, crops_per_image=1, crop_shape=(1280, 1280), random_transformer=None ) best_model = ModelCheckpoint(filepath=best_model_file, monitor='val_dice_coef_clipped', verbose=1, mode='max', save_best_only=False, save_weights_only=True) model.compile(loss=make_loss(args.loss_function), optimizer=optimizer, metrics=[dice_coef, binary_crossentropy, ceneterline_loss, dice_coef_clipped]) def schedule_steps(epoch, steps): for step in steps: if step[1] > epoch: print("Setting learning rate to {}".format(step[0])) return step[0] print("Setting learning rate to {}".format(steps[-1][0])) return steps[-1][0] callbacks = [best_model, EarlyStopping(patience=20, verbose=1, monitor='val_dice_coef_clipped', mode='max')] if args.schedule is not None: steps = [(float(step.split(":")[0]), int(step.split(":")[1])) for step in args.schedule.split(",")] lrSchedule = LearningRateScheduler(lambda epoch: schedule_steps(epoch, steps)) callbacks.insert(0, lrSchedule) if args.clr is not None: clr_params = args.clr.split(',') base_lr = float(clr_params[0]) max_lr = float(clr_params[1]) step = int(clr_params[2]) mode = clr_params[3] clr = CyclicLR(base_lr=base_lr, max_lr=max_lr, step_size=step, mode=mode) callbacks.append(clr) steps_per_epoch = len(all_ids) / args.batch_size + 1 if args.steps_per_epoch: steps_per_epoch = args.steps_per_epoch model.fit_generator( train_generator, steps_per_epoch=steps_per_epoch, epochs=args.epochs, validation_data=val_generator, validation_steps=len(val_ids), callbacks=callbacks, max_queue_size=30, verbose=1, workers=args.num_workers) del model K.clear_session() gc.collect()