def main(): args = parse_args() exp = Experiment(args.exp_name, args, mode=args.mode) if args.cfg is None: cfg_path = exp.cfg_path else: cfg_path = args.cfg cfg = Config(cfg_path) exp.set_cfg(cfg, override=False) device = torch.device('cpu') if not torch.cuda.is_available() or args.cpu else torch.device('cuda') runner = Runner(cfg, exp, device, view=args.view, resume=args.resume, deterministic=args.deterministic) if args.mode == 'train': try: runner.train() except KeyboardInterrupt: logging.info('Training interrupted.') runner.eval(epoch=args.epoch or exp.get_last_checkpoint_epoch(), save_predictions=args.save_predictions)
def main() -> None: args = parse_args() exp = Experiment(args.exp_name, args, mode=args.mode) if args.cfg is None: cfg_path = exp.cfg_path else: cfg_path = args.cfg cfg = Config(cfg_path) exp.set_cfg(cfg, override=False) device = ( torch.device("cpu") if not torch.cuda.is_available() else torch.device("cuda") ) runner = Runner(cfg, exp, device, resume=args.resume) if args.mode == "train": try: runner.train() except KeyboardInterrupt: logging.info("Training interrupted.")
def do_experiment(arg_tuple): # run the experiment exp = Experiment(arg_tuple.run_accession, arg_tuple.fastq_ftp, arg_tuple.fastq_md5) # acquire a lock on the result file (needed since this function is concurrently executed) lock = FileLock(result_file + '.lock') with lock: # append the experiment result as a row to the tab-separated values file row = exp.run_accession + '\t' + exp.fastq_dl_success + '\t' + exp.cortex_conversion_success + '\t' + \ exp.kraken_file_success + '\t' + exp.brack_file_success + '\n' open(result_file, "a").write(row)
def face_experiment(): ''' applies NMF to the faces dataset and generates an image ''' config = yaml.safe_load(open('./config/dev.yml')) experiment_config = yaml.safe_load(open('./experiments/face.yml')) solvers = experiment_config['solver_list'] config['dataset'] = 'face' r = experiment_config['r'] config['r'] = r name = 'face' X, ground_truth = get_data(config) #if ground_truth is not -1: # W = ground_truth[0] # H = ground_truth[1] print('Data loaded, rank of X: ', np.linalg.matrix_rank(X)) experiment = Experiment(config, X, experiment_config) experiment() images = np.zeros((r, 19, 19)) for solver in experiment.solvers: W = solver.solution[0] W /= np.max(W, axis=0) W = 1 - W for i in range(r): images[i, :, :] = np.reshape(W[:, i], (19, 19)) d = 0.05 plt.subplots_adjust(wspace=d, hspace=d) fig, ax = plt.subplots(4, 4) fig.set_figheight(8) fig.set_figwidth(8) for m in range(4): for n in range(4): ax[m, n].imshow(images[4 * m + n, :, :], cmap='gray', vmin=0, vmax=1) ax[m, n].set_xticks([]) ax[m, n].set_yticks([]) fig.savefig('./experiments/' + name + '/' + solver.name + '.pgf', bbox_inches='tight') fig.savefig('./experiments/' + name + '/' + solver.name + '.pdf', bbox_inches='tight') return 0
# for debugging purposes if args.smoke_test: start_date = '2021-01-01' end_date = '2021-02-15' random_repeats = 1 full_scale = False # create experiment object experiment_info = f'{name}-{country}-{area}' experiment = Experiment( experiment_info=experiment_info, start_date=start_date, end_date=end_date, random_repeats=random_repeats, cpu_count=cpu_count, full_scale=full_scale, condensed_summary=condensed_summary, continued_run=continued_run, verbose=verbose, ) max_days = (pd.to_datetime(end_date) - pd.to_datetime(start_date)).days m = [ # standard tracing measures ComplianceForAllMeasure(t_window=Interval(0.0, TO_HOURS * max_days), p_compliance=0.0), SocialDistancingForSmartTracing( t_window=Interval(0.0, TO_HOURS * max_days), p_stay_home=1.0, smart_tracing_isolation_duration=TO_HOURS * 14.0),
data = { 'single-attn-social': [], 'single-attn-course': [], 'dual-attn-social': [], 'dual-attn-course': [], 'single-contri-social': [], 'dual-contri-social': [], 'single-contri-course': [], 'dual-contri-course': [] } for p in probs: total_posts = [] for s in pops: print(s, ' students experiment, join chat prob ', p) a, b = p exp = Experiment(num_steps) def non_overload_course(a): return a.posts_read + a.chats_read def non_overload_social(a): return a.posts_social_read + a.chats_social_read def course_contributions_made(a): return a.contrib_post + a.contrib_chat def social_contributions_made(a): return a.contrib_social_post + a.contrib_social_chat agent_reporters = {} agent_reporters['non_overload_course'] = non_overload_course
# set simulation and intervention dates start_date = calibration_start_dates[country][area] end_date = calibration_lockdown_dates[country]['end'] measure_start_date = calibration_lockdown_dates[country]['start'] measure_window_in_hours = dict() measure_window_in_hours['start'] = (pd.to_datetime(measure_start_date) - pd.to_datetime(start_date)).days * TO_HOURS measure_window_in_hours['end'] = (pd.to_datetime(end_date) - pd.to_datetime(start_date)).days * TO_HOURS # create experiment object experiment_info = f'{name}-{country}-{area}' experiment = Experiment( experiment_info=experiment_info, start_date=start_date, end_date=end_date, random_repeats=random_repeats, cpu_count=cpu_count, full_scale=full_scale, verbose=verbose, ) # Load calibrated parameters up to `maxBOiters` iterations of BO unique_calibration_params = get_unique_calibration_params( country=country, area=area, multi_beta_calibration=False, maxiters=maxBOiters) unique_calibration_params = [unique_calibration_params[-1]] for iteration, calibrated_params in unique_calibration_params: print(iteration - 20, calibrated_params)
from lib.experiment import Experiment from lib import scheduler, utils experiment = Experiment(N=1000, M=5000, t_max=10000, beta_scheduler=scheduler.ConstantBetaScheduler(0.5), algorithm="Metropolis", batch_size=None, use_gpu=False) errors, energies, x = experiment.run() utils.plot_errors_energies(errors, energies)
def __init__(self): self.experiment = Experiment(underscore(self.__class__.__name__)) self._save_agent_state()
def main(): """ The main routine. """ # Fix random seeds for reproducibility - these are themselves generated from random.org # From https://keras.io/getting-started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development os.environ['PYTHONHASHSEED'] = '0' np.random.seed(91) rn.seed(95) session_conf = tf.ConfigProto(intra_op_parallelism_threads=1, inter_op_parallelism_threads=1) tf.set_random_seed(47) sess = tf.Session(graph=tf.get_default_graph(), config=session_conf) k.set_session(sess) # Enable simple logging logging.basicConfig(level=logging.INFO, format='%(message)s') # Parse command line arguments args = parseargs() # Create run folder output_directory = create_output_folder(args.output) # Write arguments to file with open(output_directory + 'arguments.txt', 'a') as arguments_file: for arg in vars(args): arguments_file.write( str(arg) + ': ' + str(getattr(args, arg)) + '\n') ############## # Prepare data print('') data = Data(incidences_file=args.incidences, specifications_file=args.specifications, plot_data=args.plotData, output_directory=output_directory) data.state(message='Raw data') data.filter_cases(cases_file=args.cases) data.state(message='Filtered SEER*Stat cases from ASCII') # Determine inputs, filter, and pre process them data.apply_data_pipeline(pipelines.data_pipeline_full, args.oneHotEncoding) data.state( message= 'Remove irrelevant, combined, post-diagnosis, and treatment attributes' ) data.create_target(args.task) data.state(message='Create target label indicating cancer survival for ' + args.task) encodings = data.finalize() data.state(message='Remove inputs with constant values') ############### # Prepare model model = Model(model_type=args.model, task=args.task, input_dim=(len(data.frame.columns) - 1), encodings=encodings, mlp_layers=args.mlpLayers, mlp_width=args.mlpWidth, mlp_dropout=args.mlpDropout, mlp_emb_neurons=args.mlpEmbNeurons, svm_gamma=args.svmGamma, svm_c=args.svmC, logr_c=args.logrC) if args.plotData: model.plot_model(output_directory) ################ # Carry out task experiment = Experiment(model=model, data=data, task=args.task, valid_ratio=0.1, test_ratio=0.1, model_type=args.model, encodings=encodings, encode_categorical_inputs=args.oneHotEncoding, plot_results=args.plotResults, output_directory=output_directory) experiment.train(mlp_epochs=args.mlpEpochs) results_validate = experiment.validate() # Write validation results to file with open(output_directory + 'results_validate.txt', 'a') as results_file: for res in results_validate: results_file.write(res + '\n') # Only test final model, do not use for tuning if args.test: results_test = experiment.test() # Write validation results to file with open(output_directory + 'results_test.txt', 'a') as results_file: for res in results_test: results_file.write(res + '\n') ################### # Input importance if args.importance: importance = experiment.importance(encodings=encodings) # Write importance results to file with open(output_directory + 'results_importance.txt', 'a') as results_file: for (column, rel) in importance: results_file.write(column + '=' + str(rel) + '\n')
def peharz_experiment(): ''' runs the peharz experiment and generates its plots ''' # load experiment config file config = yaml.safe_load(open('./config/dev.yml')) config['clip'] = False # otherwise methods diverge? experiment_config = yaml.safe_load(open('./experiments/peharz.yml')) name = 'peharz' solvers = experiment_config['solver_list'] # generate data n = experiment_config['n'] m = experiment_config['m'] r = experiment_config['r'] l0 = np.array([0.4, 0.5, 0.6, 0.7, 0.8, 0.9]) X, W, H = generate_synthetic_data(n, m, r, l0) l0_axis = np.array( [Solver.get_nonzeros(H[:, :, i]) for i in range(len(l0))]) print('Data generated, rank of X: ', np.linalg.matrix_rank(X[:, :, 0])) accuracy = np.zeros((len(l0), len(solvers))) total = [ np.zeros((len(experiment_config['solver_list']), 0)) for feature in experiment_config['plot'] ] for i in range(len(l0)): # generate experiment object config['project_l0'] = l0_axis[i] experiment = Experiment(config, X[:, :, i], experiment_config) #print([solver.name for solver in experiment.solvers]) experiment.run() summary = experiment.summary #summary = experiment.get_summary() for i, feature in enumerate(experiment_config['plot']): a = summary[feature] a = np.array(a).reshape((len(a), 1)) total[i] = np.hstack((total[i], a)) print(total) # plotting for i, feature in enumerate(experiment_config['plot']): fig = plt.figure(figsize=(4, 4)) ax0 = fig.add_subplot(111) #color = ['r', 'g', 'b', 'cyan', 'k'] ax0.set_xlabel('$\ell_0 (H_o )$') for j in range(total[i].shape[0]): ax0.plot(l0_axis, total[i][j, :], color=COLORS[j], label=LABELS[solvers[j]], linestyle='--', markersize=15, marker='.') ax0.yaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.xaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.get_yaxis().set_tick_params(which='both', direction='in') ax0.get_xaxis().set_tick_params(which='both', direction='in') ax0.grid() ax0.set_ylabel(Y_LABELS[feature]) #ax0.legend() #ax0.set_xscale('log') #ax0.set_yscale('log') s = '_' + str(n) + '_' + str(m) + '_' + str(r) fig.savefig('./experiments/' + name + '/' + feature + s + '.pgf', bbox_inches='tight') fig.savefig('./experiments/' + name + '/' + feature + s + '.pdf', bbox_inches='tight')
def classic_experiment(): ''' ''' config = yaml.safe_load(open('./config/dev.yml')) config['dataset'] = 'face' experiment_config = yaml.safe_load(open('./experiments/classic.yml')) name = 'classic' solvers = experiment_config['solver_list'] X, _ = get_data(config) experiment = Experiment(config, X, experiment_config) experiment() fig = plt.figure(figsize=(6, 6)) ax0 = fig.add_subplot(111) ax0.set_xlabel('iteration') ax0.set_ylabel('Relative error') for i, solver in enumerate(experiment.solvers): x_axis = np.array(solver.output['iteration']) y_axis = np.array(solver.output['rel_error']) ax0.plot(x_axis, y_axis, color=COLORS[i], label=solvers[i], linestyle='--', markersize=8, marker='.') ax0.yaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.xaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.get_yaxis().set_tick_params(which='both', direction='in') ax0.get_xaxis().set_tick_params(which='both', direction='in') ax0.grid() ax0.set_ylim([0.1, 0.3]) ax0.legend() #ax0.set_ylabel(Y_LABELS[feature]) #ax0.set_xscale('log') #ax0.set_yscale('log') #s = '_' + str(n) + '_' + str(m) + '_' + str(r) fig.savefig('./experiments/' + name + '/' + 'graph_iter.pgf', bbox_inches='tight') fig.savefig('./experiments/' + name + '/' + 'graph_iter.pdf', bbox_inches='tight') fig = plt.figure(figsize=(6, 6)) ax0 = fig.add_subplot(111) ax0.set_xlabel('time [s]') ax0.set_ylabel('Relative error') for i, solver in enumerate(experiment.solvers): x_axis = np.array(solver.output['time']) y_axis = np.array(solver.output['rel_error']) ax0.plot(x_axis, y_axis, color=COLORS[i], label=solvers[i], linestyle='--', markersize=8, marker='.') ax0.yaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.xaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.get_yaxis().set_tick_params(which='both', direction='in') ax0.get_xaxis().set_tick_params(which='both', direction='in') ax0.grid() ax0.set_ylim([0.1, 0.3]) ax0.legend() #ax0.set_ylabel(Y_LABELS[feature]) #ax0.set_xscale('log') #ax0.set_yscale('log') #s = '_' + str(n) + '_' + str(m) + '_' + str(r) fig.savefig('./experiments/' + name + '/' + 'graph_time.pgf', bbox_inches='tight') fig.savefig('./experiments/' + name + '/' + 'graph_time.pdf', bbox_inches='tight')
def complexity_experiment(): ''' tries to compare the complexity of iterations ''' # load experiment config file config = yaml.safe_load(open('./config/dev.yml')) config['clip'] = False # otherwise methods diverge? experiment_config = yaml.safe_load(open('./experiments/complexity.yml')) name = 'complexity' solvers = experiment_config['solver_list'] # generate data n = np.arange(190, 290, 10) m = np.arange(190, 290, 10) r = [5, 10, 15] l0 = [0.7] threshold = 0.2 iterations = np.zeros((len(r), len(n), len(solvers))) for i in range(len(n)): for j in range(len(r)): X, W, H = generate_synthetic_data(n[i], m[i], r[j], l0) print('Data generated, rank of X: ', np.linalg.matrix_rank(X[:, :, 0])) experiment = Experiment(config, X[:, :, i], experiment_config) experiment.run() for k, solver in enumerate(experiment.solvers): iterations_ = solver.output['iteration'] rel_error = solver.output['rel_error'] index_list = np.where(np.array(rel_error) < threshold)[0] if len(index_list) > 0: index = index_list[0] iterations[j, i, k] = iterations_[index] else: iterations[j, i, k] = iterations_[-1] fig = plt.figure(figsize=(6, 6)) ax0 = fig.add_subplot(111) #color = ['r', 'g', 'b', 'cyan', 'k'] ax0.set_xlabel('Size of $X$') ax0.set_ylabel('Iterations until relative error $< 0.3$') for i in range(len(r)): for j in range(len(solvers)): ax0.plot(n * m, iterations[i, :, j], color=COLORS[j], label=solvers[j], linestyle='--', markersize=15, marker='.') ax0.yaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.xaxis.set_major_formatter(FormatStrFormatter('%g')) ax0.get_yaxis().set_tick_params(which='both', direction='in') ax0.get_xaxis().set_tick_params(which='both', direction='in') ax0.grid() #ax0.set_ylabel(Y_LABELS[feature]) ax0.legend() #ax0.set_xscale('log') #ax0.set_yscale('log') #s = '_' + str(n) + '_' + str(m) + '_' + str(r) fig.savefig('./experiments/' + name + '/' + 'graph.pgf', bbox_inches='tight') fig.savefig('./experiments/' + name + '/' + 'graph.pdf', bbox_inches='tight')
import argparse import yaml from lib.experiment import Experiment def parse_args(): parser = argparse.ArgumentParser() parser.add_argument("--config", required=True) args = parser.parse_args() return args def read_yaml(path): with open(path, "r") as f: config = yaml.load(f) return config if __name__ == "__main__": args = parse_args() config = read_yaml(args.config) exper = Experiment(config) exper.run()
start_date = calibration_start_dates[country][area] end_date = calibration_lockdown_dates[country]['end'] if debugmode: random_repeats = 2 end_date = pd.to_datetime( calibration_start_dates[country][area]) + pd.to_timedelta(1, unit='D') end_date = end_date.strftime('%Y-%m-%d') # create experiment object experiment_info = f'{name}-{country}-{area}' experiment = Experiment( experiment_info=experiment_info, start_date=start_date, end_date=end_date, random_repeats=random_repeats, full_scale=None, verbose=verbose, ) summary_paths = [] for exp, expparams in exps.items(): calibrated_params = get_calibrated_params(country=country, area=area, multi_beta_calibration=False, maxiters=maxBOiters) calibrated_params['beta_site'] = expparams[ 'beta_scaling'] * calibrated_params['beta_site'] simulation_info = options_to_str( exp=exp, beta_scaling=expparams['beta_scaling'])
def main(args): sid = args.sid RND_STATE = 1234 BATCH_SIZE = 48 IMG_SIZE = 280 n_classes = 1 learning_rate = 2e-4 efficientnet_b = 0 cv_folds = 5 seed_everything(RND_STATE + sid) IMG_PATH_2019_TRAIN = r"input/2019_train" DF_PATH_2019_TRAIN = r"input/trainLabels19_unique.csv" IMG_PATH_2015_TRAIN = r"input/2015_train" DF_PATH_2015_TRAIN = r"input/trainLabels15.csv" IMG_PATH_2015_TEST = r"input/2015_test" DF_PATH_2015_TEST = r"input/testLabels15.csv" IMG_PATH_MESSIDOR = r"input/messidor1_jpg" DF_PATH_MESSIDOR = r"input/messidor1_labels_adjudicated.csv" df_train = pd.read_csv(DF_PATH_2019_TRAIN) X_2019_train = df_train.id_code.values X_2019_train = IMG_PATH_2019_TRAIN + "/" + X_2019_train + ".jpg" y_2019_train = df_train.diagnosis.values.astype(np.float32) df_train_2015_train = pd.read_csv(DF_PATH_2015_TRAIN) X_2015_train = df_train_2015_train.image.values X_2015_train = IMG_PATH_2015_TRAIN + "/" + X_2015_train + ".jpg" y_2015_train = df_train_2015_train.level.values.astype(np.float32) df_train_2015_test = pd.read_csv(DF_PATH_2015_TEST) X_2015_test = df_train_2015_test.image.values X_2015_test = IMG_PATH_2015_TEST + "/" + X_2015_test + ".jpg" y_2015_test = df_train_2015_test.level.values.astype(np.float32) # df_messidor = pd.read_csv(DF_PATH_MESSIDOR) # df_messidor = df_messidor[df_messidor.adjudicated_dr_grade > -1] # X_messidor = df_messidor.image.values # X_messidor = IMG_PATH_MESSIDOR + "/" + X_messidor + ".jpg" # y_messidor = df_messidor.adjudicated_dr_grade.values.astype(np.float32) normalize = [[0.43823998, 0.29557559, 0.20054542], [0.27235733, 0.19562355, 0.16674458]] img_size = (IMG_SIZE, IMG_SIZE) transform_train = albumentations.Compose([ albumentations.RandomCrop(*img_size), albumentations.HueSaturationValue(hue_shift_limit=7), albumentations.RandomBrightnessContrast(), albumentations.ShiftScaleRotate(shift_limit=0, scale_limit=(-0.05, 0.15), interpolation=cv2.INTER_CUBIC), albumentations.HorizontalFlip(), albumentations.VerticalFlip(), albumentations.Blur(), albumentations.Normalize(*normalize, p=1), ToTensorV2(), ]) transform_validation = albumentations.Compose([ albumentations.CenterCrop(*img_size), albumentations.Normalize(*normalize, p=1), ToTensorV2(), ]) skf9 = StratifiedKFold(n_splits=cv_folds, random_state=RND_STATE, shuffle=True) for split_id, (tra9, tes9) in enumerate(skf9.split(X_2019_train, y_2019_train)): if split_id != sid: continue X_aptos_train, X_aptos_valid = X_2019_train[tra9], X_2019_train[tes9] y_aptos_train, y_aptos_valid = y_2019_train[tra9], y_2019_train[tes9] X_train = np.concatenate([ X_aptos_train, # X_messidor, X_2015_train, X_2015_test, ]) y_train = np.concatenate([ y_aptos_train, # y_messidor, y_2015_train, y_2015_test, ]) X_valid = np.concatenate([ X_aptos_valid, ]) y_valid = np.concatenate([ y_aptos_valid, ]) print("train/validation set size: {}/{}".format( len(y_train), len(y_valid))) dataset_train = ImageDataset( files=X_train, labels=y_train, transform=transform_train, buffer_size= 100, # lower this value if out-of-memory is thrown <<<<<<<<<<<<<<<<<<<< image_size=IMG_SIZE) dataset_valid = ImageDataset(files=X_valid, labels=y_valid, transform=transform_validation, buffer_size=0, image_size=IMG_SIZE, size_is_min=True) # sampling weight for inputs of each class weights = np.array([1, 5, 5, 10, 10]) weights = calc_sampler_weights(y_train, weights) # increase probability of selecting aptos 2019 train images by 5 times weights[:y_aptos_train.shape[0]] *= 5 dataloader_train = DataLoader( dataset_train, batch_size=BATCH_SIZE, num_workers=4, # shuffle=True, sampler=WeightedRandomSampler(weights, 45000, True), pin_memory=True, drop_last=True, ) dataloader_valid = DataLoader( dataset_valid, batch_size=BATCH_SIZE, num_workers=4, shuffle=False, pin_memory=True, drop_last=False, ) _, train_val_ids = train_test_split(list(range(len(X_train))), test_size=0.1, stratify=y_train, random_state=RND_STATE) train_val_sampler = SubsetRandomSampler(train_val_ids) dataloader_train_eval = DataLoader( dataset_train, batch_size=BATCH_SIZE, num_workers=4, sampler=train_val_sampler, pin_memory=True, drop_last=False, ) model = EfficientNet(b=efficientnet_b, in_channels=3, in_spatial_shape=IMG_SIZE, n_classes=n_classes, activation=nn.LeakyReLU(0.001), bias=False, drop_connect_rate=0.2, dropout_rate=None, bn_epsilon=1e-3, bn_momentum=0.01, pretrained=True, progress=False) device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") print("device ? : ", device) model.to(device) optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=0.1 * learning_rate) # optimizer = optim.RMSprop(model.parameters(), # lr=learning_rate, # momentum=0.9, # alpha=0.9, # weight_decay=0.1 * learning_rate) # criterion = nn.CrossEntropyLoss() criterion = nn.SmoothL1Loss() eval_metrics = [ ("loss", criterion, {}), ("f1_score", pytorch_f1, { "average": "macro" }), # ("classwise_f1", pytorch_f1, {"average": None}), ("qwk", qw_kappa, {}) ] scheduler = None s = ( "{epoch}:{step}/{max_epoch} | {loss_train:.4f} / {loss_valid:.4f}" " | {f1_score_train:.4f} / {f1_score_valid:.4f}" # " | {classwise_f1_train}/{classwise_f1_valid}" " | {qwk_train:.4f} / {qwk_valid:.4f} | {time_delta}") exp = Experiment(dl_train=dataloader_train, dl_train_val=dataloader_train_eval, dl_validation=dataloader_valid, model=model, optimizer=optimizer, criterion=criterion, device=device, max_epoch=20, metrics=eval_metrics, target_metric="qwk", format_str=s, scheduler=scheduler, load_path=None, save_path="save/b%d_%dpx/%d" % (efficientnet_b, IMG_SIZE, split_id), evaluate_freq=3) exp.run()