def sample_vae(vae, dirs, count): z_size = vae.z_dim batch_size = count z = np.random.normal(size=(batch_size, z_size)) samples = vae.decode(z) input_dim = samples.shape[1:] n = batch_size plt.figure(figsize=(20, 6), tight_layout=False) plt.title('VAE samples') for i in range(n): ax = plt.subplot(3, n, i + 1) plt.imshow(samples[i].reshape(input_dim[0], input_dim[1], input_dim[2])) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if 0 == i: ax.set_title("Random") data = {} orig = [] opts = {'cfg': cfg, 'categorical': False} records = gather_records(cfg, dirs, opts, verbose=True) collate_records(records, data, opts) keys = list(data.keys()) keys = shuffle(keys)[0:batch_size] for key in keys: filename = data[key]['image_path'] img_arr = load_scaled_image_arr(filename, cfg) orig.append(img_arr) orig = np.array(orig) recon = vae.decode(vae.encode(orig)) for i in range(n): ax = plt.subplot(3, n, n + i + 1) plt.imshow(orig[i].reshape(input_dim[0], input_dim[1], input_dim[2])) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if 0 == i: ax.set_title("Real") ax = plt.subplot(3, n, (2 * n) + i + 1) plt.imshow(recon[i].reshape(input_dim[0], input_dim[1], input_dim[2])) ax.get_xaxis().set_visible(False) ax.get_yaxis().set_visible(False) if 0 == i: ax.set_title("Reconstructed") plt.savefig("samples_vae.png") plt.show()
def load_tubs(cfg, tub_names, kl, aux_name=None, pilot=False): opts = { 'cfg' : cfg} opts['categorical'] = False opts['keras_pilot'] = kl # This will be needed for generator opts['continuous'] = False gen_records = {} records = gather_records(cfg, tub_names, verbose=True) collate_records(records, gen_records, opts) # These options should be part of the KerasPilot class if type(kl.model.output) is list: opts['model_out_shape'] = (2, 1) else: opts['model_out_shape'] = kl.model.output.shape if type(kl.model.input) is list: opts['model_in_shape'] = (2, 1) else: opts['model_in_shape'] = kl.model.input.shape opts['has_imu'] = type(kl) is KerasIMU opts['has_bvh'] = type(kl) is KerasBehavioral opts['img_out'] = type(kl) is KerasLatent opts['vae_out'] = type(kl) is KerasVAE train_gen = vae_generator(cfg, gen_records, cfg.BATCH_SIZE, isTrainSet=True, aug=False, aux=aux_name, pilot=pilot) val_gen = vae_generator(cfg, gen_records, cfg.BATCH_SIZE, isTrainSet=False, aug=False, aux=aux_name, pilot=pilot) num_train = 0 num_val = 0 for key, _record in gen_records.items(): if _record['train'] == True: num_train += 1 else: num_val += 1 steps = num_train // cfg.BATCH_SIZE val_steps = num_val // cfg.BATCH_SIZE print( "Num/Steps: {} {}".format( num_train, steps ) ) print( "Val/Steps: {} {}".format( num_val, val_steps ) ) return train_gen, val_gen, steps, val_steps
def make_generator(cfg, tub_names, verbose=True): opts = {'cfg': cfg} opts['categorical'] = False opts['continuous'] = False gen_records = {} records = gather_records(cfg, tub_names, verbose=True) collate_records(records, gen_records, opts) train_gen = vae_generator(cfg, gen_records, cfg.BATCH_SIZE, isTrainSet=True, aug=False) val_gen = vae_generator(cfg, gen_records, cfg.BATCH_SIZE, isTrainSet=False, aug=False) return train_gen, val_gen, gen_records
def test_train_TrainTestSplit_continuous(tub_path): # Check whether the Train-Test splitting is working correctly when a dataset is extended. initial_records = 100 # Setup the test data t = create_sample_tub(tub_path, records=initial_records) assert t is not None # Import the configuration import donkeycar.templates.cfg_complete as cfg # Initial Setup gen_records = {} opts = {'categorical': False} opts['cfg'] = cfg # Perform the initial split print() print("Initial split of {} records to {} Test-Train split...".format( initial_records, opts['cfg'].TRAIN_TEST_SPLIT)) print() records = gather_records(cfg, tub_path, opts, verbose=True) assert len(records) == initial_records collate_records(records, gen_records, opts) ratio = calculate_TrainTestSplit(gen_records) assert ratio == cfg.TRAIN_TEST_SPLIT # Add some more records and recheck the ratio (only the NEW records should be added) additional_records = 200 print() print( "Added an extra {} records, aiming for overall {} Test-Train split...". format(additional_records, opts['cfg'].TRAIN_TEST_SPLIT)) print() create_sample_tub(tub_path, records=additional_records) records = gather_records(cfg, tub_path, opts, verbose=True) assert len(records) == (initial_records + additional_records) collate_records(records, gen_records, opts) ratio = calculate_TrainTestSplit(gen_records) assert ratio == cfg.TRAIN_TEST_SPLIT
def test_train_TrainTestSplit_simple(tub_path): # Check whether the Train-Test splitting is working correctly on a dataset. initial_records = 100 # Setup the test data t = create_sample_tub(tub_path, records=initial_records) assert t is not None # Import the configuration import donkeycar.templates.cfg_complete as cfg # Initial Setup opts = {'categorical': False} opts['cfg'] = cfg orig_TRAIN_TEST_SPLIT = cfg.TRAIN_TEST_SPLIT records = gather_records(cfg, tub_path, opts, verbose=True) assert len(records) == initial_records # Attempt a 50:50 split gen_records = {} cfg.TRAIN_TEST_SPLIT = 0.5 print() print("Testing a {} Test-Train split...".format( opts['cfg'].TRAIN_TEST_SPLIT)) print() collate_records(records, gen_records, opts) ratio = calculate_TrainTestSplit(gen_records) assert ratio == cfg.TRAIN_TEST_SPLIT # Attempt a split based on config file (reset of the record set) gen_records = {} cfg.TRAIN_TEST_SPLIT = orig_TRAIN_TEST_SPLIT print() print("Testing a {} Test-Train split...".format( opts['cfg'].TRAIN_TEST_SPLIT)) print() collate_records(records, gen_records, opts) ratio = calculate_TrainTestSplit(gen_records) assert ratio == cfg.TRAIN_TEST_SPLIT
except FileNotFoundError: cfg = dk.load_config("config.py") # retry in the current directory tub_names = preprocessFileList(args.file) input_shape = (cfg.IMAGE_W, cfg.IMAGE_H, cfg.IMAGE_DEPTH) # Code for multiple inputs: http://digital-thinking.de/deep-learning-combining-numerical-and-text-features-in-deep-neural-networks/ aux_out = 0 if args.aux is not None: aux_out = 7 # need to get number of aux outputs from data opts = {'cfg': cfg} opts['categorical'] = False opts['continuous'] = False gen_records = {} records = gather_records(cfg, tub_names, verbose=True) collate_records(records, gen_records, opts) train_gen = vae_generator(cfg, gen_records, cfg.BATCH_SIZE, isTrainSet=True, aug=False, aux=args.aux, pilot=True) for X, y in train_gen: print("X {} {}".format(type(X[0]), X[0].shape)) img = y['main_output'][0] print("main {} min/max/avg: {}/{}/{}".format(img.shape, np.min(img), np.max(img), np.mean(img))) if 'aux_output' in y: