示例#1
0
def load(level_name):
    opt.input_name = level_name
    real = read_level(opt, None, replace_tokens).to(opt.device)
    # Remove the sky layer
    real1 = real[:, :2]
    real2 = real[:, 3:]
    real = torch.cat((real1, real2), dim=1)
    return real
示例#2
0
# Init game specific inputs
replace_tokens = {}
sprite_path = opt.game + '/sprites'
if opt.game == 'mario':
    opt.ImgGen = MarioLevelGen(sprite_path)
    replace_tokens = MARIO_REPLACE_TOKENS
    downsample = special_mario_downsampling
elif opt.game == 'mariokart':
    opt.ImgGen = MariokartLevelGen(sprite_path)
    replace_tokens = MARIOKART_REPLACE_TOKENS
    downsample = special_mariokart_downsampling
else:
    NameError("name of --game not recognized. Supported: mario, mariokart")

real = read_level(opt, None, replace_tokens).to(opt.device)

################################################################################
# MarioVAE

N, C, H_real, W_real = real.shape
print(f'real {real.shape}')

# architecture
PATCH_SIZE = 7
LATENT_SIZE = 7

# training
BATCH_SIZE = 1
EPOCHS = 200
# MAX_LOSS = 300
示例#3
0
def generate_mario_samples(opt_m):

    # Generate many samples for all mario levels for large scale evaluation
    level_names = [f for f in os.listdir("./input") if f.endswith('.txt')]
    level_names.sort()

    # Directory with saved runs
    run_dir_m = "/home/awiszus/Project/TOAD-GAN/wandb/"

    for generator_level in range(0, len(level_names)):
        # New "input" mario level
        opt_m.input_name = level_names[generator_level]

        # New "output" folder
        if generator_level == 0:
            opt_m.out_ = run_dir_m + "run-20200605_101920-21l6f6ke"  # level 1 (1-1)
        elif generator_level == 1:
            opt_m.out_ = run_dir_m + "run-20200609_141816-2cxsre2o"  # level 2 (1-2)
        elif generator_level == 2:
            opt_m.out_ = run_dir_m + "run-20200609_144802-30asxofq"  # level 3 (1-3)
        elif generator_level == 3:
            opt_m.out_ = run_dir_m + "run-20200616_082954-20htkyzv"  # level 4 (2-1)
        elif generator_level == 4:
            opt_m.out_ = run_dir_m + "run-20200616_074234-19xr2e3o"  # level 5 (3-1)
        elif generator_level == 5:
            opt_m.out_ = run_dir_m + "run-20200616_093747-2ulvs4fh"  # level 6 (3-3)
        elif generator_level == 6:
            opt_m.out_ = run_dir_m + "run-20200616_102830-flwggm0z"  # level 7 (4-1)
        elif generator_level == 7:
            opt_m.out_ = run_dir_m + "run-20200616_114258-1uwt2v80"  # level 8 (4-2)
        elif generator_level == 8:
            opt_m.out_ = run_dir_m + "run-20200618_072750-3mfpkr81"  # level 9 (5-1)
        elif generator_level == 9:
            opt_m.out_ = run_dir_m + "run-20200618_093240-3aeol9gd"  # level 10 (5-3)
        elif generator_level == 10:
            opt_m.out_ = run_dir_m + "run-20200618_125519-3r0bngi4"  # level 11 (6-1)
        elif generator_level == 11:
            opt_m.out_ = run_dir_m + "run-20200618_131406-2ai6f3cl"  # level 12 (6-2)
        elif generator_level == 12:
            opt_m.out_ = run_dir_m + "run-20200618_133803-bxjlb36v"  # level 13 (6-3)
        elif generator_level == 13:
            opt_m.out_ = run_dir_m + "run-20200619_074635-14l988af"  # level 14 (7-1)
        elif generator_level == 14:
            opt_m.out_ = run_dir_m + "run-20200619_094438-lo7f1hqb"  # level 15 (8-1)

        # Read level according to input arguments
        real_m = read_level(opt_m, None, MARIO_REPLACE_TOKENS).to(opt_m.device)

        # Load TOAD-GAN for current level
        generators_m, noise_maps_m, reals_m, noise_amplitudes_m = load_trained_pyramid(opt_m)

        # Set in_s and scales
        if opt_m.gen_start_scale == 0:  # starting in lowest scale
            in_s_m = None
            m_scale_v = 1.0
            m_scale_h = 200 / real_m.shape[-1]  # normalize all levels to length 16x200
        else:  # if opt.gen_start_scale > 0
            # Only works with default level size if no in_s is provided (should not be reached)
            in_s_m = reals_m[opt_m.gen_start_scale]
            m_scale_v = 1.0
            m_scale_h = 1.0

        # Prefix for folder structure
        prefix_m = 'arbitrary'

        # Define directory
        s_dir_name_m = "%s_random_samples_v%.5f_h%.5f_start%d" % (
            prefix_m, m_scale_v, m_scale_h, opt_m.gen_start_scale)

        # Generate samples
        generate_samples(generators_m, noise_maps_m, reals_m, noise_amplitudes_m, opt_m, in_s=in_s_m,
                         scale_v=m_scale_v, scale_h=m_scale_h, current_scale=opt_m.gen_start_scale,
                         gen_start_scale=opt_m.gen_start_scale, num_samples=1000, render_images=False,
                         save_tensors=False, save_dir=s_dir_name_m)

        # For embedding experiment, copy levels to easy access folder
        samples_dir = opt_m.out_ + '/' + s_dir_name_m + '/txt'
        newpath = "./input/umap_images/baselines/" + opt_m.input_name[:-4]
        os.makedirs(newpath, exist_ok=True)
        for f in tqdm(os.listdir(samples_dir)):
            if f.endswith('.txt'):
                copyfile(os.path.join(samples_dir, f), os.path.join(newpath, f))
示例#4
0
def main():
    """ Main Training funtion. Parses inputs, inits logger, trains, and then generates some samples. """

    # torch.autograd.set_detect_anomaly(True)

    # Logger init
    logger.remove()
    logger.add(sys.stdout,
               colorize=True,
               format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | " +
               "<level>{level}</level> | " +
               "<light-black>{file.path}:{line}</light-black> | " +
               "{message}")

    # Parse arguments
    opt = get_arguments().parse_args()
    opt = post_config(opt)

    # Init wandb
    run = wandb.init(project="mario",
                     tags=get_tags(opt),
                     config=opt,
                     dir=opt.out)
    opt.out_ = run.dir

    # Init game specific inputs
    replace_tokens = {}
    sprite_path = opt.game + '/sprites'
    if opt.game == 'mario':
        opt.ImgGen = MarioLevelGen(sprite_path)
        replace_tokens = MARIO_REPLACE_TOKENS
        downsample = special_mario_downsampling
    elif opt.game == 'mariokart':
        opt.ImgGen = MariokartLevelGen(sprite_path)
        replace_tokens = MARIOKART_REPLACE_TOKENS
        downsample = special_mariokart_downsampling
    else:
        NameError("name of --game not recognized. Supported: mario, mariokart")

    # Read level according to input arguments
    real = read_level(opt, None, replace_tokens).to(opt.device)

    # Train!
    generators, noise_maps, reals, noise_amplitudes = train(real, opt)

    # Generate Samples of same size as level
    logger.info("Finished training! Generating random samples...")
    in_s = None
    generate_samples(generators,
                     noise_maps,
                     reals,
                     noise_amplitudes,
                     opt,
                     in_s=in_s)

    # Generate samples of smaller size than level
    logger.info("Generating arbitrary sized random samples...")
    scale_v = 0.8  # Arbitrarily chosen scales
    scale_h = 0.4
    real_down = downsample(1, [[scale_v, scale_h]], real, opt.token_list)
    real_down = real_down[0]
    # necessary for correct input shape
    in_s = torch.zeros(real_down.shape, device=opt.device)
    generate_samples(generators,
                     noise_maps,
                     reals,
                     noise_amplitudes,
                     opt,
                     in_s=in_s,
                     scale_v=scale_v,
                     scale_h=scale_h,
                     save_dir="arbitrary_random_samples")
示例#5
0
        opt.ImgGen = MarioLevelGen(sprite_path)
        opt.gen_start_scale = 0  # Forced for this experiment

        generate_mario_samples(opt)

    elif opt.seed_mariokart_road:
        # Code to make mario kart seeded road examples
        opt.game = 'mariokart'
        sprite_path = opt.game + '/sprites'
        opt.ImgGen = MariokartLevelGen(sprite_path)
        replace_tokens = MARIOKART_REPLACE_TOKENS
        downsample = special_mariokart_downsampling
        opt.gen_start_scale = 0  # Forced for this experiment

        # Load level
        real = read_level(opt, None, replace_tokens).to(opt.device)
        # Load generator
        generators, noise_maps, reals, noise_amplitudes = load_trained_pyramid(opt)

        # Define paths to seed road image(s)
        seed_road_images = ['mariokart/seed_road/seed_road.png']
        # seed_road_images = ['mariokart/seed_road/MNIST_examples/eights/sample_%d.png' % im for im in range(20)]

        for i, img_path in enumerate(seed_road_images):
            # Read and convert seed road image
            seed_road_img = plt.imread(img_path)
            opt.seed_road = torch.Tensor(1 - seed_road_img[:, :, 0])

            # Scales have to be fitting with seed road image (preferably make seed road the size of scale 0 directly!)
            scale_0_h = reals[0].shape[-1] / reals[-1].shape[-1]
            scale_0_v = reals[0].shape[-2] / reals[-1].shape[-2]