Beispiel #1
0
def BigGAN_render(class_vector, noise_vector, truncation):
    if class_vector.shape[0] == 1:
        class_vector = np.tile(class_vector, [noise_vector.shape[0], 1])
    if noise_vector.shape[0] == 1:
        noise_vector = np.tile(noise_vector, [class_vector.shape[0], 1])
    class_vector = torch.from_numpy(class_vector.astype(np.float32)).to('cuda')
    noise_vector = torch.from_numpy(noise_vector.astype(np.float32)).to('cuda')
    with torch.no_grad():
        output = model(noise_vector, class_vector, truncation)
    imgs = convert_to_images(output.cpu())
    return imgs
Beispiel #2
0
def get_image(class_vector, noise_vector, truncation=0.5):
    class_vector = torch.from_numpy(class_vector)
    noise_vector = torch.from_numpy(noise_vector)

    # If you have a GPU, put everything on cuda
    # noise_vector = noise_vector.to('cuda')
    # class_vector = class_vector.to('cuda')
    # model.to('cuda')

    with torch.no_grad():
        output = model(noise_vector, class_vector, truncation)

    return convert_to_images(output)[0]
Beispiel #3
0
def biggan(inp: List[str], metadata):
    truncation = 0.4
    try:
        class_vector = one_hot_from_names(inp, batch_size=len(inp))
        noise_vector = truncated_noise_sample(truncation=truncation, batch_size=len(inp))
        noise_vector = torch.from_numpy(noise_vector)
        class_vector = torch.from_numpy(class_vector)
        with torch.no_grad():
            output = modelBG(noise_vector, class_vector, truncation)
    except:
        inp = ['cat']
        class_vector = torch.from_numpy(one_hot_from_names(inp, batch_size=len(inp)))
        noise_vector = torch.from_numpy(truncated_noise_sample(truncation=truncation, batch_size=len(inp)))
        with torch.no_grad():
            output = modelBG(noise_vector, class_vector, truncation)

    return convert_to_images(output)[0]
Beispiel #4
0
def generate_image(thing="mushroom",
                   model_name="biggan-deep-512",
                   truncation=0.4):
    "Generate an image of *thing* from the model, save it and return the path"

    if model_name in ["waifu", "celeb"]:
        return generate_waifu(model_name, truncation)

    global img_i
    model = get_model(model_name)

    # Prepare a input
    class_vector = one_hot_from_names([thing], batch_size=1)
    noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1)

    # All in tensors
    noise_vector = torch.from_numpy(noise_vector)
    class_vector = torch.from_numpy(class_vector)

    # If you have a GPU, put everything on cuda
    noise_vector = noise_vector.to('cuda')
    class_vector = class_vector.to('cuda')
    model.to('cuda')

    # Generate an image
    with torch.no_grad():
        output = model(noise_vector, class_vector, truncation)

    # If you have a GPU put back on CPU
    output = output.to('cpu')
    img = convert_to_images(output)
    out = img[0]
    file_name = f"images/{img_i}.png"
    img_i += 1
    os.system("mkdir -p images/")
    out.save(file_name, 'png')
    print(
        f"Generated an image of {thing} in file {file_name} with model {model_name}"
    )
    return file_name
Beispiel #5
0
        pass

        if (i + 1) * batch_size > len(class_vectors):
            torch.cuda.empty_cache()
            break

        #get batch
        noise_vector = noise_vectors[i * batch_size:(i + 1) * batch_size]
        class_vector = class_vectors[i * batch_size:(i + 1) * batch_size]

        # Generate images
        with torch.no_grad():
            output = model(noise_vector, class_vector, truncation)

        output_cpu = output.cpu()
        pil_imgs = convert_to_images(output_cpu)

        #convert to image array and add to frames
        for out in pil_imgs:
            if args.scaling != 1:
                newsize = (img_resolution * args.scaling,
                           img_resolution * args.scaling)
                im = np.array(out.resize(newsize, Image.LANCZOS))
            else:
                im = np.array(out)
            if use_disk == True:
                imageio.imwrite(
                    os.path.join(tmp_folder_path,
                                 str(counter) + ".png"), im)
                counter = counter + 1
            else:
Beispiel #6
0
    saveFile += str('_LargeBatches/')

saveFile += str(str(classnam.capitalize()) + '/')
if super_jitter:
    saveFile += str('SuperJitter' + str(minJ) + '/')
saveFile += str(classnam + '_' + str(x))


ensure_dir(saveFile)

save_as_images(output, saveFile)


#Save pngs as a .gif

imgs = convert_to_images(output)

saveGIF = saveFile + '/anim.gif'

ensure_dir(saveGIF)

imgs[0].save(saveGIF,
               save_all=True,
               append_images=imgs[1:],
               duration=100,
               loop=0)




Beispiel #7
0
    noise_vector = truncated_noise_sample(truncation=truncation,
                                          batch_size=batch_size)

    #noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1)
    # All in tensors
    #noise_vector = torch.from_numpy(np.ones([3, 128]).astype(np.float32)) #
    noise_vector = torch.from_numpy(noise_vector)
    class_vector = torch.from_numpy(class_vector)
    # If you have a GPU, put everything on cuda
    noise_vector = noise_vector.to('cuda')
    class_vector = class_vector.to('cuda')
    model.to('cuda')
    # Generate an image
    with torch.no_grad():
        output = model(noise_vector, class_vector, truncation)
    imgs = convert_to_images(output.cpu())
    #%% 1d interpolation
    truncation = 0.7
    batch_size = 11
    class_vector = one_hot_from_names(['mushroom'] * batch_size, batch_size=1)
    noise_vector = truncated_noise_sample(truncation=truncation, batch_size=1)
    scale_vec = np.arange(-1, 1.1, 0.2)
    noise_vec_scale = scale_vec.reshape([-1, 1]) * noise_vector
    imgs = BigGAN_render(class_vector, noise_vec_scale, truncation=truncation)
    #%
    figh = plt.figure(figsize=[25, 3])
    gs = figh.add_gridspec(1, len(imgs))  # 1d interpolation
    for i, img in enumerate(imgs):
        plt.subplot(gs[i])
        plt.imshow(img)
        plt.axis('off')