cycleGAN的使用

github地址

数据准备

需要一个root_dir,下面有trainA,trainB两个文件夹
只运行训练py文件可以不需要testA,testB

git clone 从官方原文github克隆到本地来

git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix
cd pytorch-CycleGAN-and-pix2pix

安装依赖

For pip users, please type the command pip install -r requirements.txt
For Conda users, you can create a new Conda environment using conda env create -f environment.yml

更改参数

pycharm - run - edit cofiguration
用pycharm打开后更改train.py输入参数
dataroot:你的root_dir地址
name:本次训练项目名称,会在checkpoint文件夹下保存
model:用的模型,默认cycle_gan

--dataroot your_dir --name RIN_ct_to_mr_cyclegan --model cycle_gan

其他参数可在option文件夹下三个py文件按需更改
这两个加起来是总的训练轮数(上面是初始学习率训练几轮,下面是递减几轮)

可视化设置

conda进入虚拟环境pytorch-CycleGAN-and-pix2pix
python -m visdom.server
如果出现端口占用:
执行命令:netstat -tunlp
然后找到占用8097端口的PID kill -9 PID
完成后出现You can navigate to http://localhost:8097
一直保持这个终端处于开启状态

训练

运行train.py

测试

源代码给了两种测试方法

Example (You need to train models first or download pre-trained models from our website):
    Test a CycleGAN model (both sides):
        python test.py --dataroot ./datasets/maps --name maps_cyclegan --model cycle_gan

    Test a CycleGAN model (one side only):
        python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout

    The option '--model test' is used for generating CycleGAN results only for one side.
    This option will automatically set '--dataset_mode single', which only loads the images from one set.
    On the contrary, using '--model cycle_gan' requires loading and generating results in both directions,
    which is sometimes unnecessary. The results will be saved at ./results/.
    Use '--results_dir <directory_path_to_save_result>' to specify the results directory.

但是第一种我以为是两边同时生成,GA和GB一起调用,但是经过测试只有一边能生成,both sides意思是反向生成rec和idt
所以用第二种作为测试方法
同样的在pycharm - run - edit cofiguration
更改test文件的初始参数

--dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout

新建一个放模型的文件夹,Ga或者Gb,把模型文件改名为latest_net_G.pth
将name后面参数改为这个文件夹
这样就可以运行测试了,但是注意option里的模型要设置成你的pth文件里的模型,否则报错

posted @ 2022-12-27 14:05  梅雨明夏  阅读(431)  评论(0编辑  收藏  举报