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

Can not use Stylegan3 to generate images. #142

Open
zydjohnHotmail opened this issue Mar 22, 2022 · 7 comments
Open

Can not use Stylegan3 to generate images. #142

zydjohnHotmail opened this issue Mar 22, 2022 · 7 comments

Comments

@zydjohnHotmail
Copy link

Hello:
I tried to follow the instruction to install Stylegan3 and generate some images. I am using Python 3.9.7 on Windows 10.
First, I used conda to install all the packages, but I found the requirement.yml seems not correct.
For my Windows 10 environment.
If you install pytorch (version 1.9.1), the default pytorch package is without cuda toolkit. And ninja package seem not working either. So I have to delete pytorch and ninja from the requiement.yml, then install them after I activate the environment. However, I still have issue when I tried to generate some images.
Then, I decided to simply use pip to install all necessary packages one by one, starting from ninja, then pytorch with cuda, then other packages.
Now, I have installed all necessary packages:

(stylegan3-main) F:\Python\stylegan3-main>pip list
Package Version


certifi 2021.10.8
charset-normalizer 2.0.12
click 8.0.4
colorama 0.4.4
cycler 0.11.0
fonttools 4.31.2
glfw 2.5.1
idna 3.3
imageio 2.16.1
imageio-ffmpeg 0.4.5
imgui 1.4.1
kiwisolver 1.4.0
matplotlib 3.5.1
ninja 1.10.2.3
numpy 1.22.3
packaging 21.3
Pillow 9.0.1
pip 22.0.4
PyOpenGL 3.1.6
pyparsing 3.0.7
pyspng 0.1.0
python-dateutil 2.8.2
requests 2.27.1
scipy 1.8.0
setuptools 57.4.0
six 1.16.0
torch 1.11.0+cu113
torchaudio 0.11.0+cu113
torchvision 0.12.0+cu113
tqdm 4.63.0
typing_extensions 4.1.1
urllib3 1.26.9

Then I created one folder: F:\Python\stylegan3-main\out
Finally, I run the command to generate the images:

(stylegan3-main) F:\Python\stylegan3-main>python gen_images.py --outdir=out --trunc=1 --seeds=2 --network=https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-r-afhqv2-512x512.pkl
Loading networks from "https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-r-afhqv2-512x512.pkl"...
Generating image for seed 2 (0/1) ...
Setting up PyTorch plugin "bias_act_plugin"... Failed!
Traceback (most recent call last):
File "F:\Python\stylegan3-main\lib\site-packages\torch\utils\cpp_extension.py", line 1740, in _run_ninja_build
subprocess.run(
File "C:\Program Files\Python39\lib\subprocess.py", line 528, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['ninja', '-v']' returned non-zero exit status 1.

I always failed with F:\Python\stylegan3-main\lib\site-packages\torch\utils\cpp_extension.py
Any suggestions?
By the way, is there any working Stylegan3 or Stylegan2 pre-trained model I can use in Windows 10 environment?
My Windows 10 version is: Version 21H2 (OS Build 19044.1586). I think most of the people on Windows 10 are using this version, except some people already migrated to Windows 11.
I am using Python 3.9.7 (x64) on Windows 10, as I believe most of the python packages work in Python 3.9; only some of them work in Python 3.10.
So, my issue could represent quite a number of people are using Windows.
Thanks,

@PDillis
Copy link

PDillis commented Mar 24, 2022

Forgive me if this is obvious or redundant, but there are some hints/guides on making the network compile and run in Windows. For example, the Requirements tells you what to do with VS and what to add to your PATH. Troubleshooting also has some good pointers, as well as some final things to try if nothing else works.

Since it's the "bias_act_plugin" that fails, most likely it has to do with errors during the installation of VSCode. For example, @nipponjo and @felkoh in issue #124 note that you shouldn't have two VS versions installed and that you should remove one (I've also used the 2019 version before and I haven't had issues with it either).

For the package installations, there have been some fixes/additions, for example @edstoica's PR #116, where you change cudatoolkit>=11.1 to cudatoolkit>=11.1 in environment.yml. @siddharthksah's PR #111 also adds psutil==5.9.0 to the file. Lastly, @SetZero's PR #80 adds conda-forge to the channels, so perhaps these changes will also help you, should

@zydjohnHotmail
Copy link
Author

Hello:
I asked one person who had experience to help me out. But he didn't find anything wrong. I have tried all the advices you listed, but none of them works. My environment fully meet all software requirements listed in environment.yml.
The only issue seems to be that I have to install torch+cuda after the environment is setup. I suggest that you can change the instruction, to indicate that you have to re-install torch+cuda again by using pip, not conda, which has performance issue.

However, I doubted about the hardware issue. My PC has one NVIDIA GEFORCE GT1030 graphic card. This card seems to be able to install cuda toolkit, but NVIDIA didn't even list it as supported product for cuda programming.
However, since my PC has 16-core CPU and 128GB RAM, I want to know if there is any parameter, I can set, so that generating images don't use GPU?
Please advise!
Thanks,

@PDillis
Copy link

PDillis commented Mar 25, 2022

@nurpax found out how to do this in StyleGAN2-ADA: you just need to set force_fp32=True, along with changing the device to use the CPU. @JCBrouwer summarized this in a more pythonic way. I just tested the latter in StyleGAN3 and it still works.

First, you could either hard set the device to just use the 'cpu' or add an option with click, which is what I did:

device = torch.device('cuda') if torch.cuda.is_available() and device == 'cuda' else torch.device('cpu')

or just do device=torch.device('cpu'). Then after you load normally your Generator, you do the following:

if device.type == 'cpu':
    import functools
    G.forward = functools.partial(G.forward, force_fp32=True)

That's it. There's no need to compile the custom CUDA kernels, and it just goes straight to the image synthesis, assuming that's what you have next. Note that it will take a lot longer, which depends on the specifications of your machine.

@zydjohnHotmail
Copy link
Author

Hello:
I changed gen_images.py, so it looks like:
device=torch.device('cpu')
And I run it, I think it works.
I found a cat image in out folder.
However, I don't know if it is correct or not, please let me know.
By the way, how I can use one pre-trained model with a source image and generate a target image?
Please advise!
Thanks,
seed0002

@zydjohnHotmail
Copy link
Author

Hi,
I forgot to show the command I run:
(stylegan3) F:\Python\GAN3\stylegan3>python gen_images.py --outdir=out --trunc=1 --seeds=2 --network=https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-r-afhqv2-512x512.pkl
Loading networks from "https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-r-afhqv2-512x512.pkl"...
Generating image for seed 2 (0/1) ...

(stylegan3) F:\Python\GAN3\stylegan3>dir out
Volume in drive F is Disk8T
Volume Serial Number is 5030-0BF0

Directory of F:\Python\GAN3\stylegan3\out

03/26/2022 10:19 PM

.
03/26/2022 10:19 PM ..
03/26/2022 10:19 PM 518,833 seed0002.png
1 File(s) 518,833 bytes
2 Dir(s) 5,015,626,403,840 bytes free

(stylegan3) F:\Python\GAN3\stylegan3>

@PDillis
Copy link

PDillis commented Mar 27, 2022

Yeah, the image looks good, so you should see how to generate images with the presented parameters. --help shows every option available.

Regarding generating a target image, that's called projection and it has been part of StyleGAN since StyleGAN2, so check those repositories for code. You won't be able to get perfect projection because well, that's not the point of StyleGAN in general unless you force it beyond what the Generator should normally be able to synthesize.

@zydjohnHotmail
Copy link
Author

Hello:
I can generate quite a lot of images now, as I download all Stylegan3 models, and save them in my local disk, the speed is OK, as my CPU is rather powerful.
However, I can't figure out how to generate a target image with a source image. Give me an idea in this repo, where I can find the python code for the projection?
The following is the output from help:
(stylegan3) F:\Python\GAN3\stylegan3>python gen_images.py --help
Usage: gen_images.py [OPTIONS]

Generate images using pretrained network pickle.

Examples:

Generate an image using pre-trained AFHQv2 model ("Ours" in Figure 1, left).

python gen_images.py --outdir=out --trunc=1 --seeds=2
--network=https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-r-afhqv2-512x512.pkl

Generate uncurated images with truncation using the MetFaces-U dataset

python gen_images.py --outdir=out --trunc=0.7 --seeds=600-605
--network=https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan3/versions/1/files/stylegan3-t-metfacesu-1024x1024.pkl

Options:
--network TEXT Network pickle filename [required]
--seeds PARSE_RANGE List of random seeds (e.g., '0,1,4-6')
[required]
--trunc FLOAT Truncation psi [default: 1]
--class INTEGER Class label (unconditional if not specified)
--noise-mode [const|random|none]
Noise mode [default: const]
--translate VEC2 Translate XY-coordinate (e.g. '0.3,1')
[default: 0,0]
--rotate ANGLE Rotation angle in degrees [default: 0]
--outdir DIR Where to save the output images [required]
--help Show this message and exit.

(stylegan3) F:\Python\GAN3\stylegan3>
Please advise!
Thanks,

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

2 participants