Others · 2023年6月27日 0

DragGAN快速上手|在windows中运行DragGAN

DragGAN模型本质上是给各种GAN开发的一种交互式图像操作方法,实现了通过鼠标拉伸图像即可自动生成新图像的神奇功能。 

代码:https://github.com/XingangPan/DragGAN

论文链接:https://arxiv.org/abs/2305.10973

项目主页:https://vcai.mpi-inf.mpg.de/projects/DragGAN/

这种全新的控制方法非常灵活、强大且简单,有手就行,只需在图像上「拖动」想改变的位置点(操纵点),就能合成你想要的图像。

下面说一下如何在windows中使用它。

一 、安装python环境

我们使用的是anaconda,去官方下载https://www.anaconda.com/

下载下载,直接安装就行

二、下载代码

直接去项目主页下载zip包,下载完成后解压到一个盘的根目录下,https://github.com/XingangPan/DragGAN

下载git,使用git下载代码会比较慢甚至无法下载,最好是开梯子

git clone https://github.com/XingangPan/DragGAN.git

三、创建虚拟环境

打开anaconda

创建一个虚拟环境

启动terminal

切换到代码所在分区
d:
进入代码所在目录
dir DragGAN
安装代码依赖

但是这里有个坑,这个文件里的依赖不够全
把以下内容加到requirements.txt文件中
scipy==1.11.0
PyOpenGL==3.1.7
glfw==2.6.0
imgui==2.0.0
然后安装依赖

如果太慢
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple


直接安装torch可能会报错或者,无法启动,下载指定版本比较好
首先是安装torch,开梯子的方法就是通过v2ray,qv2ray等工具启用系统代理
pip install torch==2.0.1+cu118 --extra-index-url https://download.pytorch.org/whl/cu118

安装ninja依赖

https://github.com/ninja-build/ninja/releases/

下载windows版,放到环境变量指定的目录下

四、下载模型

这个项目的脚本里写了可用的模型,提供了下载脚本,但是这个脚本是linux shell,在windows下无法运行

在项目主目录创建目录checkpoints 

因为部分文件的特殊限制,需要使用linux shell的wget命令下载,有两个可选方案

第一个就是在windows中安装wget,https://eternallybored.org/misc/wget/ 下载x64版本exe,放到draggan的checkpoints目录

然后确保shell当前所在的目录是draggan的checkpoint目录

wget.exe 

确保程序没有问题,开始下载模型

把下面下载到的模型全部放到checkpoints 

下载这些模型文件放到checkpoints然后改名
wget.exe https://storage.googleapis.com/self-distilled-stylegan/lions_512_pytorch.pkl
ren lions_512_pytorch.pkl stylegan2_lions_512_pytorch.pkl

wget.exe https://storage.googleapis.com/self-distilled-stylegan/dogs_1024_pytorch.pkl
ren dogs_1024_pytorch.pkl stylegan2_dogs_1024_pytorch.pkl

wget.exe https://storage.googleapis.com/self-distilled-stylegan/horses_256_pytorch.pkl
ren horses_256_pytorch.pkl stylegan2_horses_256_pytorch.pkl

wget.exe https://storage.googleapis.com/self-distilled-stylegan/elephants_512_pytorch.pkl
ren elephants_512_pytorch.pkl stylegan2_elephants_512_pytorch.pkl


nvidia的模型下载
wget.exe https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-ffhq-512x512.pkl
wget.exe https://api.ngc.nvidia.com/v2/models/nvidia/research/stylegan2/versions/1/files/stylegan2-afhqcat-512x512.pkl
wget.exe http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-car-config-f.pkl
wget.exe http://d36zk2xti64re0.cloudfront.net/stylegan2/networks/stylegan2-cat-config-f.pkl
wget.exe https://huggingface.co/turx2/eargx/resolve/2e6cc2d7a3cb9ff02cb13f73adaef67fc7477003/stylegan_human_v2_512.pkl

当然也可以直接到我的网盘下载,只提供百度网盘版本。

链接:https://pan.baidu.com/s/16Mr-BEPKssQ4JsHRhAiDLQ 
提取码:iqzk 

启动DragGAN

一切准备就绪,启动服务的同时,加载了多个模型

注意,要在创建的虚拟环境启动的情况下才能顺利启动。


python visualizer_drag.py checkpoints/stylegan2_lions_512_pytorch.pkl checkpoints/stylegan2-ffhq-512x512.pkl checkpoints/stylegan2-afhqcat-512x512.pkl checkpoints/stylegan2-car-config-f.pkl checkpoints/stylegan2_dogs_1024_pytorch.pkl checkpoints/stylegan2_horses_256_pytorch.pkl checkpoints/stylegan2-cat-config-f.pkl checkpoints/stylegan2_elephants_512_pytorch.pkl checkpoints/stylegan_human_v2_512.pkl checkpoints/stylegan2-lhq-256x256.pkl

自行体验