From b215fdf93b0bc2c387e8e38ff40361efe05cc49e Mon Sep 17 00:00:00 2001 From: Kelvin Clien <99217852+bulutthecat@users.noreply.github.com> Date: Tue, 27 Jun 2023 12:31:12 -0400 Subject: [PATCH 1/9] Update README.md updated for windows users. --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e59be83..49494f8 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,10 @@ To download pre-trained weights, simply run: ```sh sh scripts/download_model.sh ``` +Or for windows: +``` +.\scripts\download_model.bat +``` If you want to try StyleGAN-Human and the Landscapes HQ (LHQ) dataset, please download weights from these links: [StyleGAN-Human](https://drive.google.com/file/d/1dlFEHbu-WzQWJl7nBBZYcTyo000H9hVm/view?usp=sharing), [LHQ](https://drive.google.com/file/d/16twEf0T9QINAEoMsWefoWiyhcTd-aiWc/view?usp=sharing), and put them under `./checkpoints`. Feel free to try other pretrained StyleGAN. @@ -72,10 +76,14 @@ To start the DragGAN GUI, simply run: ```sh sh scripts/gui.sh ``` +If you are using windows, you can run: +``` +.\scripts\gui.bat +``` This GUI supports editing GAN-generated images. To edit a real image, you need to first perform GAN inversion using tools like [PTI](https://github.com/danielroich/PTI). Then load the new latent code and model weights to the GUI. -You can run DragGAN Gradio demo as well: +You can run DragGAN Gradio demo as well, this is universal for both windows and linux: ```sh python visualizer_drag_gradio.py ``` @@ -84,6 +92,7 @@ python visualizer_drag_gradio.py This code is developed based on [StyleGAN3](https://github.com/NVlabs/stylegan3). Part of the code is borrowed from [StyleGAN-Human](https://github.com/stylegan-human/StyleGAN-Human). +(cheers to the community as well) ## License The code related to the DragGAN algorithm is licensed under [CC-BY-NC](https://creativecommons.org/licenses/by-nc/4.0/). From 35bebb3544a3a4aed1b8502d0fb2d00dfd6bc852 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96rsan=20NUHO=C4=9ELU?= Date: Wed, 28 Jun 2023 19:02:49 +0300 Subject: [PATCH 2/9] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b26a764..2152bf0 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ If you have CUDA graphic card, please follow the requirements of [NVlabs/stylega The usual installation steps involve the following commands, they should set up the correct CUDA version and all the python packages ``` -conda env create python=3.7 -f environment.yml +conda env create -f environment.yml conda activate stylegan3 ``` From 9db0ab922f557418f6384d8e0935d287454f7070 Mon Sep 17 00:00:00 2001 From: Ivan Chernukha Date: Wed, 28 Jun 2023 19:56:28 +0000 Subject: [PATCH 3/9] feat: added simple Dockerfile to quickly run gradio app and instructions; fixed version in pip requirements.txt --- Dockerfile | 27 +++++++++++++++++++++++++++ README.md | 13 +++++++++++++ requirements.txt | 14 +++++++++----- 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..b40f196 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,27 @@ +FROM nvcr.io/nvidia/pytorch:23.05-py3 + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV PYTHONUNBUFFERED 1 + +RUN apt-get update && apt-get install -y --no-install-recommends \ + make \ + pkgconf \ + xz-utils \ + xorg-dev \ + libgl1-mesa-dev \ + libglu1-mesa-dev \ + libxrandr-dev \ + libxinerama-dev \ + libxcursor-dev \ + libxi-dev \ + libxxf86vm-dev + +RUN pip install --upgrade pip + +COPY requirements.txt requirements.txt +RUN pip install -r requirements.txt + +WORKDIR /workspace + +RUN (printf '#!/bin/bash\nexec \"$@\"\n' >> /entry.sh) && chmod a+x /entry.sh +ENTRYPOINT ["/entry.sh"] diff --git a/README.md b/README.md index b26a764..1d175b7 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,19 @@ cat environment.yml | \ grep -v -E 'nvidia|cuda' > environment-no-nvidia.yml && \ conda env create -f environment-no-nvidia.yml conda activate stylegan3 +``` + +## Run Gradio visualizer in Docker + +Provided docker image is based on NGC PyTorch repository. To quickly try out visualizer in Docker, run the following: + +```sh +docker build . -t draggan:latest +docker run -v "$PWD":/workspace/src -it draggan:latest bash +cd src && python visualizer_drag_gradio.py +``` +Now you can open a shared link from Gradio (printed in the terminal console). +Beware the Docker image takes about 25GB of disk space! # On MacOS export PYTORCH_ENABLE_MPS_FALLBACK=1 diff --git a/requirements.txt b/requirements.txt index e71044d..556e127 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,9 +1,13 @@ -torch -torchvision -Ninja -gradio +torch>=2.0.0 +scipy==1.11.0 +Ninja==1.10.2 +gradio>=3.35.2 +imageio-ffmpeg>=0.4.3 huggingface_hub hf_transfer pyopengl imgui -glfw +glfw==2.6.1 +pillow>=9.4.0 +torchvision>=0.15.2 +imageio>=2.9.0 From 67d7a7ca8187bce082f3b27f768fe49959debb1e Mon Sep 17 00:00:00 2001 From: Xingang Pan Date: Thu, 29 Jun 2023 09:37:13 +0800 Subject: [PATCH 4/9] Fix README --- README.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 55b5a29..8fdd6dc 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,9 @@ cat environment.yml | \ grep -v -E 'nvidia|cuda' > environment-no-nvidia.yml && \ conda env create -f environment-no-nvidia.yml conda activate stylegan3 + +# On MacOS +export PYTORCH_ENABLE_MPS_FALLBACK=1 ``` ## Run Gradio visualizer in Docker @@ -78,10 +81,6 @@ cd src && python visualizer_drag_gradio.py Now you can open a shared link from Gradio (printed in the terminal console). Beware the Docker image takes about 25GB of disk space! -# On MacOS -export PYTORCH_ENABLE_MPS_FALLBACK=1 -``` - ## Download pre-trained StyleGAN2 weights To download pre-trained weights, simply run: From 5f392f92ff7d11a8666f02b55ab51e432203477d Mon Sep 17 00:00:00 2001 From: Mert Cobanov Date: Thu, 29 Jun 2023 12:43:52 +0300 Subject: [PATCH 5/9] Update Dockerfile Used --no-cache-dir flag when installing Python packages to avoid caching unnecessary files. Removed the intermediate requirements.txt file after installing the Python packages. --- Dockerfile | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index b40f196..2c6ad40 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,12 +14,13 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ libxinerama-dev \ libxcursor-dev \ libxi-dev \ - libxxf86vm-dev + libxxf86vm-dev \ + && rm -rf /var/lib/apt/lists/* -RUN pip install --upgrade pip +RUN pip install --no-cache-dir --upgrade pip -COPY requirements.txt requirements.txt -RUN pip install -r requirements.txt +COPY requirements.txt . +RUN pip install --no-cache-dir -r requirements.txt WORKDIR /workspace From 07f766c19fe5346af7ffc5be221c93335aa378d3 Mon Sep 17 00:00:00 2001 From: Mert Cobanov Date: Thu, 29 Jun 2023 13:54:27 +0300 Subject: [PATCH 6/9] Open a port from docker container to local --- README.md | 4 ++-- visualizer_drag_gradio.py | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8fdd6dc..7b21594 100644 --- a/README.md +++ b/README.md @@ -75,8 +75,8 @@ Provided docker image is based on NGC PyTorch repository. To quickly try out vis ```sh docker build . -t draggan:latest -docker run -v "$PWD":/workspace/src -it draggan:latest bash -cd src && python visualizer_drag_gradio.py +docker run -p 7860: 7860 -v "$PWD":/workspace/src -it draggan:latest bash +cd src && python visualizer_drag_gradio.py --listen ``` Now you can open a shared link from Gradio (printed in the terminal console). Beware the Docker image takes about 25GB of disk space! diff --git a/visualizer_drag_gradio.py b/visualizer_drag_gradio.py index 42e6c15..ff9abe7 100644 --- a/visualizer_drag_gradio.py +++ b/visualizer_drag_gradio.py @@ -17,6 +17,11 @@ from viz.renderer import Renderer, add_watermark_np parser = ArgumentParser() parser.add_argument('--share', action='store_true',default='True') parser.add_argument('--cache-dir', type=str, default='./checkpoints') +parser.add_argument( + "--listen", + action="store_true", + help="launch gradio with 0.0.0.0 as server name, allowing to respond to network requests", +) args = parser.parse_args() cache_dir = args.cache_dir @@ -863,4 +868,4 @@ with gr.Blocks() as app: gr.close_all() app.queue(concurrency_count=3, max_size=20) -app.launch(share=args.share) +app.launch(share=args.share, server_name="0.0.0.0" if args.listen else "127.0.0.1") From 3347bbb1d6ff714c61ecb4e2058346bae61b368a Mon Sep 17 00:00:00 2001 From: Kevin Unger Date: Thu, 29 Jun 2023 16:18:46 +0200 Subject: [PATCH 7/9] rm bat/sh download script --- scripts/download_model.bat | 23 ----------------------- scripts/download_model.sh | 19 ------------------- 2 files changed, 42 deletions(-) delete mode 100644 scripts/download_model.bat delete mode 100644 scripts/download_model.sh diff --git a/scripts/download_model.bat b/scripts/download_model.bat deleted file mode 100644 index 67101d4..0000000 --- a/scripts/download_model.bat +++ /dev/null @@ -1,23 +0,0 @@ -@echo off -mkdir checkpoints -cd checkpoints - -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://storage.googleapis.com/self-distilled-stylegan/lions_512_pytorch.pkl', 'lions_512_pytorch.pkl')" -ren lions_512_pytorch.pkl stylegan2_lions_512_pytorch.pkl - -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://storage.googleapis.com/self-distilled-stylegan/dogs_1024_pytorch.pkl', 'dogs_1024_pytorch.pkl')" -ren dogs_1024_pytorch.pkl stylegan2_dogs_1024_pytorch.pkl - -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://storage.googleapis.com/self-distilled-stylegan/horses_256_pytorch.pkl', 'horses_256_pytorch.pkl')" -ren horses_256_pytorch.pkl stylegan2_horses_256_pytorch.pkl - -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://storage.googleapis.com/self-distilled-stylegan/elephants_512_pytorch.pkl', 'elephants_512_pytorch.pkl')" -ren elephants_512_pytorch.pkl stylegan2_elephants_512_pytorch.pkl - -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-ffhq-512x512.pkl', 'stylegan2-ffhq-512x512.pkl')" -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-afhqcat-512x512.pkl', 'stylegan2-afhqcat-512x512.pkl')" -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-car-config-f.pkl', 'stylegan2-car-config-f.pkl')" -powershell -Command "(New-Object System.Net.WebClient).DownloadFile('http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-cat-config-f.pkl', 'stylegan2-cat-config-f.pkl')" - -echo "Done" -pause diff --git a/scripts/download_model.sh b/scripts/download_model.sh deleted file mode 100644 index 4d2dccc..0000000 --- a/scripts/download_model.sh +++ /dev/null @@ -1,19 +0,0 @@ -mkdir checkpoints -cd checkpoints - -wget https://storage.googleapis.com/self-distilled-stylegan/lions_512_pytorch.pkl -mv lions_512_pytorch.pkl stylegan2_lions_512_pytorch.pkl - -wget https://storage.googleapis.com/self-distilled-stylegan/dogs_1024_pytorch.pkl -mv dogs_1024_pytorch.pkl stylegan2_dogs_1024_pytorch.pkl - -wget https://storage.googleapis.com/self-distilled-stylegan/horses_256_pytorch.pkl -mv horses_256_pytorch.pkl stylegan2_horses_256_pytorch.pkl - -wget https://storage.googleapis.com/self-distilled-stylegan/elephants_512_pytorch.pkl -mv elephants_512_pytorch.pkl stylegan2_elephants_512_pytorch.pkl - -wget https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-ffhq-512x512.pkl -wget https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-afhqcat-512x512.pkl -wget http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-car-config-f.pkl -wget http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-cat-config-f.pkl From 6888c9d3a926543bc6f3ca1913fc2b8d697d280d Mon Sep 17 00:00:00 2001 From: Kevin Unger Date: Thu, 29 Jun 2023 16:19:25 +0200 Subject: [PATCH 8/9] better checkpoint download --- .gitignore | 2 +- scripts/download_model.py | 78 ++++++++++++++++++++++++++++++++++++ scripts/download_models.json | 10 +++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 scripts/download_model.py create mode 100644 scripts/download_models.json diff --git a/.gitignore b/.gitignore index 24b5204..8b626aa 100644 --- a/.gitignore +++ b/.gitignore @@ -99,7 +99,7 @@ ENV/ [Ll]ib64 [Ll]ocal [Ss]cripts -!scripts\download_model.bat +!scripts/ pyvenv.cfg .venv pip-selfcheck.json diff --git a/scripts/download_model.py b/scripts/download_model.py new file mode 100644 index 0000000..b951b05 --- /dev/null +++ b/scripts/download_model.py @@ -0,0 +1,78 @@ +import os +import sys +import json +import requests +from tqdm import tqdm + +def download_file(url: str, filename: str, download_dir: str): + """Download a file if it does not already exist.""" + + try: + filepath = os.path.join(download_dir, filename) + content_length = int(requests.head(url).headers.get("content-length", 0)) + + # If file already exists and size matches, skip download + if os.path.isfile(filepath) and os.path.getsize(filepath) == content_length: + print(f"{filepath} already exists. Skipping download.") + return + if os.path.isfile(filepath) and os.path.getsize(filepath) != content_length: + print(f"{filepath} already exists but size does not match. Redownloading.") + else: + print(f"Downloading {filename} from {url}") + + # Start download, stream=True allows for progress tracking + response = requests.get(url, stream=True) + + # Check if request was successful + response.raise_for_status() + + # Create progress bar + total_size = int(response.headers.get('content-length', 0)) + progress_bar = tqdm( + total=total_size, + unit='iB', + unit_scale=True, + ncols=70, + file=sys.stdout + ) + + # Write response content to file + with open(filepath, 'wb') as f: + for data in response.iter_content(chunk_size=1024): + f.write(data) + progress_bar.update(len(data)) # Update progress bar + + # Close progress bar + progress_bar.close() + + # Error handling for incomplete downloads + if total_size != 0 and progress_bar.n != total_size: + print("ERROR, something went wrong while downloading") + raise Exception() + + + except Exception as e: + print(f"An error occurred: {e}") + +def main(): + """Main function to download files from URLs in a config file.""" + + # Get JSON config file path + script_dir = os.path.dirname(os.path.realpath(__file__)) + config_file_path = os.path.join(script_dir, "download_models.json") + + # Set download directory + download_dir = "checkpoints" + os.makedirs(download_dir, exist_ok=True) + + # Load URL and filenames from JSON + with open(config_file_path, "r") as f: + config = json.load(f) + + # Download each file specified in config + for url, filename in config.items(): + download_file(url, filename, download_dir) + + +if __name__ == "__main__": + main() diff --git a/scripts/download_models.json b/scripts/download_models.json new file mode 100644 index 0000000..637d553 --- /dev/null +++ b/scripts/download_models.json @@ -0,0 +1,10 @@ +{ + "https://storage.googleapis.com/self-distilled-stylegan/lions_512_pytorch.pkl": "stylegan2_lions_512_pytorch.pkl", + "https://storage.googleapis.com/self-distilled-stylegan/dogs_1024_pytorch.pkl": "stylegan2_dogs_1024_pytorch.pkl", + "https://storage.googleapis.com/self-distilled-stylegan/horses_256_pytorch.pkl": "stylegan2_horses_256_pytorch.pkl", + "https://storage.googleapis.com/self-distilled-stylegan/elephants_512_pytorch.pkl": "stylegan2_elephants_512_pytorch.pkl", + "https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-ffhq-512x512.pkl": "stylegan2-ffhq-512x512.pkl", + "https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-afhqcat-512x512.pkl": "stylegan2-afhqcat-512x512.pkl", + "http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-car-config-f.pkl": "stylegan2-car-config-f.pkl", + "http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-cat-config-f.pkl": "stylegan2-cat-config-f.pkl" +} From 3160c97022b0e54a6988c9484d3a7c7b4ee430fc Mon Sep 17 00:00:00 2001 From: Kevin Unger Date: Thu, 29 Jun 2023 16:28:14 +0200 Subject: [PATCH 9/9] update download instructions --- README.md | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8fdd6dc..9302d63 100644 --- a/README.md +++ b/README.md @@ -84,12 +84,9 @@ Beware the Docker image takes about 25GB of disk space! ## Download pre-trained StyleGAN2 weights To download pre-trained weights, simply run: -```sh -sh scripts/download_model.sh -``` -Or for windows: + ``` -.\scripts\download_model.bat +python scripts/download_model.py ``` If you want to try StyleGAN-Human and the Landscapes HQ (LHQ) dataset, please download weights from these links: [StyleGAN-Human](https://drive.google.com/file/d/1dlFEHbu-WzQWJl7nBBZYcTyo000H9hVm/view?usp=sharing), [LHQ](https://drive.google.com/file/d/16twEf0T9QINAEoMsWefoWiyhcTd-aiWc/view?usp=sharing), and put them under `./checkpoints`.