Ejemplo n.º 1
0
def main():
    if len(sys.argv) < 7:
        print(
            'Usage: python3 sample.py [MODEL_PATH] [DATA_PATH] [SPLIT_INDEX] [OUT_PATH] [RECIPE_ID] [NUM_SAMPLES]'
        )
        exit()
    model_path = os.path.abspath(sys.argv[1])
    data_path = os.path.abspath(sys.argv[2])
    split_index = int(sys.argv[3])
    out_path = os.path.abspath(sys.argv[4])
    recipe_id = sys.argv[5]
    num_samples = int(sys.argv[6])

    util.create_dir(out_path)
    saved_model = torch.load(model_path)

    data = GANstronomyDataset(data_path, split=opts.TVT_SPLIT)
    data.set_split_index(split_index)
    data_loader = DataLoader(data,
                             batch_size=1,
                             shuffle=False,
                             sampler=SequentialSampler(data))

    num_classes = data.num_classes()
    G = Generator(opts.LATENT_SIZE, opts.EMBED_SIZE).to(opts.DEVICE)
    G.load_state_dict(saved_model['G_state_dict'])
    G.eval()

    embs = None

    for ibatch, data_batch in enumerate(data_loader):
        with torch.no_grad():
            recipe_ids, recipe_embs, img_ids, imgs, classes, _, _ = data_batch
            batch_size, recipe_embs, imgs = util.get_variables3(
                recipe_ids, recipe_embs, img_ids, imgs)
            if recipe_ids[0] == recipe_id:
                embs = recipe_embs
                break

    assert embs is not None

    z = torch.randn(num_samples, opts.LATENT_SIZE).to(opts.DEVICE)
    imgs_gen = G(z, embs.expand(num_samples, opts.EMBED_SIZE)).detach()
    for i in range(num_samples):
        img_gen = imgs_gen[i]
        save_results(out_path, recipe_id, img_gen, i)
Ejemplo n.º 2
0
def main():
    if len(sys.argv) < 7:
        print('Usage: python3 interp.py [MODEL_PATH] [DATA_PATH] [SPLIT_INDEX] [OUT_PATH] [RECIPE_ID] [NUM_DIV]')
        exit()
    model_path = os.path.abspath(sys.argv[1])
    data_path = os.path.abspath(sys.argv[2])
    split_index = int(sys.argv[3])
    out_path = os.path.abspath(sys.argv[4])
    recipe_id = sys.argv[5]
    num_div = int(sys.argv[6])
    
    util.create_dir(out_path)
    saved_model = torch.load(model_path)

    data = GANstronomyDataset(data_path, split=opts.TVT_SPLIT)
    data.set_split_index(split_index)
    data_loader = DataLoader(data, batch_size=1, shuffle=False, sampler=SequentialSampler(data))

    num_classes = data.num_classes()
    G = Generator(opts.LATENT_SIZE, opts.EMBED_SIZE).to(opts.DEVICE)
    G.load_state_dict(saved_model['G_state_dict'])
    G.eval()

    embs = None

    for ibatch, data_batch in enumerate(data_loader):
        with torch.no_grad():
            recipe_ids, recipe_embs, img_ids, imgs, classes, _, _ = data_batch
            batch_size, recipe_embs, imgs  = util.get_variables3(recipe_ids, recipe_embs, img_ids, imgs)
            if recipe_ids[0] == recipe_id:
                embs = recipe_embs
                break

    assert embs is not None
    z1 = torch.randn(1, opts.LATENT_SIZE).to(opts.DEVICE)
    z2 = torch.randn(1, opts.LATENT_SIZE).to(opts.DEVICE)
    for a in np.linspace(0.0, 1.0, num_div + 1):
        a = torch.tensor(a, dtype=torch.float)
        a = Variable(a.type(FloatTensor)).to(opts.DEVICE)
        z = (1.0 - a) * z1 + a * z2
        img_gen = G(z, embs).detach()[0]
        save_results(out_path, recipe_id, img_gen, a)
Ejemplo n.º 3
0
def main():
    if len(sys.argv) < 5:
        print(
            'Usage: python3 test.py [MODEL_PATH] [DATA_PATH] [SPLIT_INDEX] [OUT_PATH]'
        )
        exit()
    model_path = os.path.abspath(sys.argv[1])
    data_path = os.path.abspath(sys.argv[2])
    split_index = int(sys.argv[3])
    out_path = os.path.abspath(sys.argv[4])

    util.create_dir(out_path)
    saved_model = torch.load(model_path)

    data = GANstronomyDataset(data_path, split=opts.TVT_SPLIT)
    data.set_split_index(split_index)
    data_loader = DataLoader(data,
                             batch_size=opts.BATCH_SIZE,
                             shuffle=False,
                             sampler=SequentialSampler(data))

    G = Generator(opts.LATENT_SIZE, opts.EMBED_SIZE).to(opts.DEVICE)
    G.load_state_dict(saved_model['G_state_dict'])
    G.eval()

    all_ingrs = util.load_ingredients()

    for ibatch, data_batch in enumerate(data_loader):
        with torch.no_grad():
            recipe_ids, recipe_embs, img_ids, imgs, classes, _, _ = data_batch
            batch_size, recipe_embs, imgs, = util.get_variables3(
                recipe_ids, recipe_embs, img_ids, imgs)
            z = torch.randn(batch_size, opts.LATENT_SIZE).to(opts.DEVICE)
            imgs_gen = G(z, recipe_embs)
            imgs, imgs_gen = imgs.detach(), imgs_gen.detach()
            for iexample in range(batch_size):
                save_results(all_ingrs, imgs[iexample], imgs_gen[iexample],
                             img_ids[iexample], recipe_ids[iexample], out_path)