def combined_plots(n_test, n_top, output_dir, plot_var, n_dims=4, n_constituents=20): sample_topo = make_sample(n_constituents, 'qcd-topo', 'top-topo', n_test, n_top, n_dims, adjust_weights=True) sample_UFO = make_sample(n_constituents, 'qcd-UFO', 'BSM', n_test, n_top, n_dims, adjust_weights=True) samples = [sample_UFO, sample_topo] plot_distributions(samples, output_dir, plot_var, sig_bins=200, bkg_bins=400, sig_tag='top') sys.exit()
if args.valid_cuts == '': args.valid_cuts = valid_cuts else: args.valid_cuts = valid_cuts + '& (' + args.valid_cuts + ')' inputs = {'scalars': scalars, 'images': [], 'others': others} validation(args.output_dir, args.results_in, args.plotting, args.n_valid, data_files, inputs, args.valid_cuts, args.sep_bkg, args.runDiffPlots) elif args.results_in != '': print('\nOption --results_in not matching any file --> aborting\n') if args.results_in != '': sys.exit() # MODEL CREATION AND MULTI-GPU DISTRIBUTION n_gpus = min(args.n_gpus, len(tf.config.experimental.list_physical_devices('GPU'))) train_batch_size = max(1, n_gpus) * args.batch_size valid_batch_size = max(1, n_gpus) * max(args.batch_size, int(5e3)) sample = make_sample(data_files[0], [0, 1], input_data, args.n_tracks, args.n_classes)[0] model = create_model(args.n_classes, sample, args.NN_type, args.FCN_neurons, CNN, args.l2, args.dropout, train_data, n_gpus) # ARGUMENTS AND VARIABLES SUMMARY args.scaling = args.scaling == 'ON' and scalars != [] args.t_scaling = args.t_scaling == 'ON' and 'tracks' in scalars + images if args.NN_type == 'CNN': print('\nCNN ARCHITECTURE:') for shape in [ shape for shape in CNN if shape in [sample[key].shape[1:] for key in sample] ]: print(format(str(shape), '>8s') + ':', str(CNN[shape])) print('\nPROGRAM ARGUMENTS:') print(tabulate(vars(args).items(), tablefmt='psql'))
num_workers=world.workers) else: data_test = dataloader(mode="test", transform=tran, folder=args.evalFolder) data_test = DataLoader(data_test, batch_size=world.batch_size, shuffle=False, num_workers=world.workers) print(">EVAL FOLDER:", args.evalFolder) if world.verbose: print("loaded data") # sample data and show configs data_sample = data[0] img = utils.make_sample(data_sample) utils.show_config() # initialize model and restore training process m = ITrackerModel() if world.verbose: print("initialized model") epoch_before = 0 best = 1e10 if world.doLoad: saved = utils.load_checkpoint(world.weight_file) if saved: print('>Loading checkpoint for epoch %05d with loss %.5f ...' % (saved['epoch'], saved['best_prec1'])) state = saved['state_dict'] try:
from keras.models import load_model from utils import files_for_train, generate_train_file, make_sample epochs = 3 input_length = 128 batch_size = 256 input_path = "/home/biot/projects/Audio_generator/data/" output_path = "/home/biot/projects/Audio_generator/generated_music/" # input_path = "/root/inspiron/Audio_generator/data/" # output_path = "/root/inspiron/Audio_generator/generated_music/" sample_length = 120000 model = load_model('music_gen_model_5.h5') make_sample(model=model, input_path=input_path, output_path=output_path, sample_length=sample_length, epoch=epochs, input_length=input_length, extension='mp3')
'images': images if args.images == 'ON' else [], 'tracks': [], 'scalars': scalars if args.scalars == 'ON' else [] } all_var = { **train_var, 'others': others } scalars = train_var['scalars'] # ARCHITECTURE SELECTION AND MULTI-GPU DISTRIBUTION n_gpus = min(args.n_gpus, len(tf.config.experimental.list_physical_devices('GPU'))) devices = ['/gpu:0', '/gpu:1', '/gpu:2', '/gpu:3'] tf.debugging.set_log_device_placement(False) strategy = tf.distribute.MirroredStrategy(devices=devices[:n_gpus]) sample, _ = make_sample(data_file, all_var, [0, 1], args.n_tracks, args.n_classes) with strategy.scope(): if tf.__version__ >= '2.1.0': tf.keras.mixed_precision.experimental.set_policy('mixed_float16') model = multi_CNN(args.n_classes, args.NN_type, sample, args.l2, args.dropout, args.alpha, args.CNN_neurons, args.FCN_neurons, **train_var) model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # SAMPLES SIZES AND APPLIED CUTS ON PHYSICS VARIABLES sample_size = len(h5py.File(data_file, 'r')['eventNumber']) args.n_train = [0, min(sample_size, args.n_train) ] if args.cross_valid == 'OFF' else [0, 0]
train_var = {'images' :images if args.images =='ON' else [], 'tracks':[], 'scalars':scalars if args.scalars=='ON' else []} other_var = ['eventNumber', 'p_TruthType', 'p_iffTruth', 'p_LHTight', 'p_LHMedium', 'p_LHLoose', 'p_eta', 'p_et_calo','p_LHValue'] total_var = {**train_var, 'others':other_var}; scalars = train_var['scalars'] # MULTI-GPU DISTRIBUTION n_gpus = min(args.n_gpus, len(tf.config.experimental.list_physical_devices('GPU'))) devices = ['/gpu:0', '/gpu:1', '/gpu:2', '/gpu:3'] tf.debugging.set_log_device_placement(False) strategy = tf.distribute.MirroredStrategy(devices=devices[:n_gpus]) with strategy.scope(): if tf.__version__ >= '2.1.0' and len(total_var['images']) > 1: tf.keras.mixed_precision.experimental.set_policy('mixed_float16') sample, _ = make_sample(data_file, total_var, [0,1], args.n_tracks, args.n_classes) func_args = (args.n_classes, args.NN_type, sample, args.l2, args.dropout, CNN, args.FCN_neurons) model = multi_CNN(*func_args, **train_var) print('\nNEURAL NETWORK ARCHITECTURE'); model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # SAMPLES SIZES AND APPLIED CUTS ON PHYSICS VARIABLES sample_size = len(h5py.File(data_file, 'r')['eventNumber']) args.n_train = [0, min(sample_size, args.n_train)] if args.cross_valid == 'OFF' else [0,0] args.n_valid = [args.n_train[-1], min(args.n_train[-1]+args.n_valid, sample_size )] if args.cross_valid == 'OFF' and args.n_folds > 1: args.n_valid = args.n_train #args.valid_cuts += ' & (sample["p_et_calo"] >= 20)' #args.valid_cuts += ' & (sample["p_et_calo"] > 4.5) & (sample["p_et_calo"] < 20)' #args.valid_cuts += ' & (abs(sample["p_eta"] > 0.6)'
tf.compat.v1.logging.ERROR) #Suppressing Tensorflow warnings seed = None if args.n_epochs > 0 else 0 model = VariationalAutoEncoder(args.FC_layers, args.n_dims * args.n_constituents, seed=seed) args.model_in = args.output_dir + '/' + args.model_in args.model_out = args.output_dir + '/' + args.model_out args.scaler_in = args.output_dir + '/' + args.scaler_in args.scaler_out = args.output_dir + '/' + args.scaler_out if args.model_in != args.output_dir + '/': if not os.path.isfile(args.model_in): sys.exit() print('\nLoading pre-trained weights from:', args.model_in) sys.stdout = open(os.devnull, 'w') #Suppressing screen output sample = make_sample(args.n_constituents, bkg_data, OoD_data, n_dims=args.n_dims) dataset = make_datasets(sample, sample) train_model(model, dataset.take(1), dataset.take(1)) sys.stdout = sys.__stdout__ #Resuming screen output model.load_weights(args.model_in) if args.scaling == 'ON' and os.path.isfile(args.scaler_in): print('\nLoading scaler transform from:', args.scaler_in) scaler = pickle.load(open(args.scaler_in, 'rb')) args.output_dir += '/plots' for path in list( accumulate([folder + '/' for folder in args.output_dir.split('/')])): try: os.mkdir(path) except FileExistsError: pass
print( "\noption [--results_in] was given but no matching file found in the right path, aborting.." ) print("results_in file =", args.output_dir + '/' + args.results_in, '\n') if args.results_in != '': sys.exit() # MULTI-GPU DISTRIBUTION n_gpus = min(args.n_gpus, len(tf.config.experimental.list_physical_devices('GPU'))) devices = ['/gpu:0', '/gpu:1', '/gpu:2', '/gpu:3'] tf.debugging.set_log_device_placement(False) strategy = tf.distribute.MirroredStrategy(devices=devices[:n_gpus]) with strategy.scope(): if tf.__version__ >= '2.1.0' and len(variables['images']) >= 1: tf.keras.mixed_precision.experimental.set_policy('mixed_float16') sample, _ = make_sample(args.data_file, variables, [0, 1], args.n_tracks, args.n_classes) func_args = (args.n_classes, args.NN_type, sample, args.l2, args.dropout, CNN, args.FCN_neurons) model = multi_CNN(*func_args, **train_var) print('\nNEURAL NETWORK ARCHITECTURE') model.summary() model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) # ARGUMENTS AND VARIABLES TABLES args.scaler_in = args.scaler_in if '.pkl' in args.scaler_in else '' args.model_in = args.model_in if '.h5' in args.model_in else '' args.results_in = args.results_in if '.h5' in args.results_in else '' args.NN_type = 'FCN' if train_var['images'] == [] else args.NN_type args.scaling = (args.scaling == 'ON' and scalars != [])
tf.compat.v1.logging.ERROR) #suppress Tensorflow warnings for path in list( accumulate([folder + '/' for folder in args.output_dir.split('/')])): try: os.mkdir(path) except FileExistsError: pass print('\nTRAINING SAMPLE:') sig_bins, bkg_bins = 200, 600 log = args.weight_type != 'flat_pt' train_sample = make_sample(args.n_train, args.n_W, args.n_dims, metrics, train_var, args.n_constituents, bkg_cuts, sig_cuts, bkg='qcd', sig='W') train_sample = { key: utils.shuffle(train_sample[key], random_state=0) for key in train_sample } train_sample = reweight_sample(train_sample, sig_bins, bkg_bins, args.weight_type) var_distributions(train_sample, args.output_dir, sig_bins, bkg_bins, var='pt',