Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to generate image without GPU? #54

Closed
lz1998 opened this issue Feb 28, 2021 · 4 comments
Closed

How to generate image without GPU? #54

lz1998 opened this issue Feb 28, 2021 · 4 comments

Comments

@lz1998
Copy link

lz1998 commented Feb 28, 2021

How to generate image without GPU?

@nurpax
Copy link
Contributor

nurpax commented Mar 9, 2021

Generating images on the CPU is possible but not with default settings.

Here's how you will need to modify generate.py to enable CPU generation:

diff --git a/generate.py b/generate.py
index f7f9619..0e487ed 100755
--- a/generate.py
+++ b/generate.py
@@ -79,7 +79,7 @@ def generate_images(
     """
 
     print('Loading networks from "%s"...' % network_pkl)
-    device = torch.device('cuda')
+    device = torch.device('cpu')
     with dnnlib.util.open_url(network_pkl) as f:
         G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
 
@@ -116,7 +116,7 @@ def generate_images(
     for seed_idx, seed in enumerate(seeds):
         print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
         z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
-        img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode)
+        img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode, force_fp32=True)
         img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
         PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save(f'{outdir}/seed{seed:04d}.png')
 

Basically, run it on the CPU device and use only fp32.

Note that there is a marked performance difference between running on the CPU. On my machine, generating an image takes roughly 20 ms whereas the same code on the CPU takes 1500 ms, ie., 75 times slower.

@nurpax nurpax closed this as completed Mar 9, 2021
@lz1998
Copy link
Author

lz1998 commented Apr 3, 2021

Generating images on the CPU is possible but not with default settings.

Here's how you will need to modify generate.py to enable CPU generation:

diff --git a/generate.py b/generate.py
index f7f9619..0e487ed 100755
--- a/generate.py
+++ b/generate.py
@@ -79,7 +79,7 @@ def generate_images(
     """
 
     print('Loading networks from "%s"...' % network_pkl)
-    device = torch.device('cuda')
+    device = torch.device('cpu')
     with dnnlib.util.open_url(network_pkl) as f:
         G = legacy.load_network_pkl(f)['G_ema'].to(device) # type: ignore
 
@@ -116,7 +116,7 @@ def generate_images(
     for seed_idx, seed in enumerate(seeds):
         print('Generating image for seed %d (%d/%d) ...' % (seed, seed_idx, len(seeds)))
         z = torch.from_numpy(np.random.RandomState(seed).randn(1, G.z_dim)).to(device)
-        img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode)
+        img = G(z, label, truncation_psi=truncation_psi, noise_mode=noise_mode, force_fp32=True)
         img = (img.permute(0, 2, 3, 1) * 127.5 + 128).clamp(0, 255).to(torch.uint8)
         PIL.Image.fromarray(img[0].cpu().numpy(), 'RGB').save(f'{outdir}/seed{seed:04d}.png')
 

Basically, run it on the CPU device and use only fp32.

Note that there is a marked performance difference between running on the CPU. On my machine, generating an image takes roughly 20 ms whereas the same code on the CPU takes 1500 ms, ie., 75 times slower.

Why not add an param to choose generate with GPU or CPU? Can I create a PR?

@CrackerHax
Copy link

Basically, run it on the CPU device and use only fp32.

What about projecting images in latent space?

@CrackerHax
Copy link

CrackerHax commented Jul 29, 2021

Basically, run it on the CPU device and use only fp32.

What about projecting images in latent space?

project.py line 98ish

        # Synth images from opt_w.
        w_noise = torch.randn_like(w_opt) * w_noise_scale
        ws = (w_opt + w_noise).repeat([1, G.mapping.num_ws, 1])
     -   synth_images = G.synthesis(ws, noise_mode='const')
     +   synth_images = G.synthesis(ws, noise_mode='const', force_fp32=True)

line 163:

    # Load networks.
    print('Loading networks from "%s"...' % network_pkl)
   - device = torch.device('cuda')
   + device = torch.device('cpu')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants