学习CycleGAN-遇到的问题

①pip报错   更新OpenSSL(不确定有没有用)   +    删除pip文件夹(D:\anzhuanglujing\anaconda\anaconda3\Lib\site-packages依赖包中的) 
重新在pycharm中下载(不确定有没有用)+    pip 的时候需要指定镜像源 并加上信任该镜像源  
例pip install boost -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
几个常用:python -m pip install --upgrade pip
pip install -r D:\wangyr\yunse\pytorch-CycleGAN-and-pix2pix-master\requirements.txt
pip install visdom==0.1.8.8 -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

②git clone https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix需要下载git

报错wget  需要单独下载wget解压并放到
D:\anzhuanglujing\git\Git\mingw64\bin下

未解决问题fatal: unable to access 'https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix/': 
Failed to connect to github.com port 443: Timed out
代码通过网页直接下载 

③bash ./datasets/download_cyclegan_dataset.sh [apple2orange, orange2apple, summer2winter_yosemite, winter2summer_yosemite, 
horse2zebra, zebra2horse, monet2photo, style_monet, style_cezanne, style_ukiyoe, style_vangogh, sat2map, map2sat, 
cityscapes_photo2label, cityscapes_label2photo, facades_photo2label, facades_label2photo, iphone2dslr_flower]
需要在git bash里 先进入到D:\wangyr\yunse\pytorch-CycleGAN-and-pix2pix-master文件夹里(只能一层一层cd)


④下载数据集 bash ./datasets/download_cyclegan_dataset.sh horse2zebra             './datasets/horse2zebra.zip' saved(好像先下载再自动解压成文件夹)
下载预训练模型 bash ./scripts/download_cyclegan_model.sh horse2zebra                  './checkpoints/horse2zebra_pretrained/latest_net_G.pth' saved

⑤数据集:/dataset为您的数据集创建一个数据集文件夹。
在数据集文件夹内创建子文件夹testA,testB,trainA,和trainB。将要从a转换为b的任何图像(cat2dog)放在testA文件夹中,
要将要从b转换为a的图像(dog2cat)放在testB文件夹中,并对trainA和trainB文件夹执行相同的操作。
预训练模型:或将您自己的预训练模型添加到 ./checkpoints/{NAME}_pretrained/latest_net_G.pt

python train.py --dataroot ./datasets/horse2zebra --name horse2zebra --model cycle_gan

⑥将--dataroot和--name更改为您自己的数据集路径和模型名称。使用--gpu_ids 0,1,..  在多个GPU上进行训练,并更改批大小。   使用CPU时选-1
我发现16个批量适合4台V100,可以在90秒内完成一个历元的训练。
一旦您的模型经过训练,将最后一个检查点复制为测试模型可以自动检测的格式:
Use cp ./checkpoints/horse2zebra/latest_net_G_A.pth ./checkpoints/horse2zebra/latest_net_G.pth 
if you want to transform images from class A to class B and 
cp ./checkpoints/horse2zebra/latest_net_G_B.pth ./checkpoints/horse2zebra/latest_net_G.pth 
if you want to transform images from class B to class A.

上面的将最后一个检查点复制为测试模型可以自动检测的格式没做

⑦(python test.py --dataroot ./datasets/horse2zebra --name horse2zebra --model cycle_gan)
python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra_pretrained --model test --no_dropout
选项--model test仅用于生成一侧的CycleGAN结果。此选项将自动将--dataset_mode设置为single,仅加载一组图像。
相反,使用--model cycle_gan需要在两个方向上加载和生成结果,有时这是不必要的。结果将保存在./results/。
使用--results_dir {directory_path_to_save_result}指定结果目录。
对于您自己的实验,您可能需要指定--netG,--norm和--no_dropout以匹配训练后的模型的生成器体系结构。

⑧cd D:\wangyr\yunse\pytorch-CycleGAN-and-pix2pix-master
python train.py --dataroot ./datasets/horse2zebra --name horse2zebra1 --model cycle_gan --gpu_ids -1
python test.py --dataroot datasets/horse2zebra/testA --name horse2zebra1 --model test --no_dropout

官方给的使用方法,我把位置加到环境变量里了,不用cd到这个位置了。

⑨同时存在Python2和Python3 遇到了点问题

pip install visdom时默认使用python2 
python3 -m pip install visdom无事发生
暂时删掉环境变量C:\Python27\ArcGIS10.2\Scripts 无用
py -3 pip install visdom'py' 不是内部或外部命令,也不是可运行的程序或批处理文件。
将C:\Python27\ArcGIS10.2\python.exe和pythonw.exe改成了python2和pythonw2
输入Python显示This Python interpreter is in a conda environment, but the environment hasnot been activated. 网上找的,but我用了没用的方法们

激活
步骤一命令行:conda info --envs   获取Anaconda的地址
!!!!!!!!!!步骤二命令行:conda activate D:\anzhuanglujing\anaconda\anaconda3  把这里的C:\ProgramData\Anaconda3换成步骤一中的base  * 的这个地址。
步骤三重新查看:python 失败了 
管理员模式打开powershell,输入conda init 命令。之后在cmd中,键入conda activate,即可正常激活conda环境。(成功了但每次打开终端都需要重新conda activate)
成功pip install visdom 

⑩python -m visdom.server启动服务失败
找到D:\anzhuanglujing\anaconda\anaconda3\Lib\site-packages\tornado\platform\asyncio.py添加
if sys.platform == 'win32':
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy()) 
找到D:\anzhuanglujing\anaconda\anaconda3\Lib\site-packages\visdom\server.py添加如下代码
import asyncio
asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

python -m visdom.server
或后台运行nohup python -m visdom.server &(win不可用,Ubuntu可用)

⑪UserWarning: Detected call of `lr_scheduler.step()` before `optimizer.step()`. 
In PyTorch 1.1.0 and later, you should call them in the opposite order: `optimizer.step()` before `lr_scheduler.step()`.  
Failure to do this will result in PyTorch skipping the first value of the learning rate schedule. 
See more details at https://pytorch.org/docs/stable/optim.html#how-to-adjust-learning-rate
改成先optimizer优化后Ir_scheduler调试
把train.py43行换到78行(epoch最后)
运行了horse2zebra3 四个epoch后死机(重启又好了)

⑫PyTorch中数据读取的一个重要接口是torch.utils.data.DataLoader,该接口定义在dataloader.py脚本中,只要是用PyTorch
来训练模型基本都会用到该接口,该接口主要用来将自定义的数据读取接口的输出或者PyTorch已有的数据读取接口的输入
按照batch size封装成Tensor,后续只需要再包装成Variable即可作为模型的输入,因此该接口有点承上启下的作用,比较重要。
DataLoader类 先看看__init__中的几个重要的输入:1、dataset,这个就是PyTorch已有的数据读取接口
(比如torchvision.datasets.ImageFolder)或者自定义的数据接口的输出,该输出要么是torch.utils.data.Dataset类的对象,
要么是继承自torch.utils.data.Dataset类的自定义类的对象。2、batch_size,根据具体情况设置即可。
3、shuffle,一般在训练数据中会采用。4、num_workers,从注释可以看出这个参数必须大于等于0,0的话表示数据导入在主进程中进行,
其他大于0的数表示通过多个进程来导入数据,可以加快数据导入速度。
在__init__中,RandomSampler类表示随机采样且不重复,所以起到的就是shuffle的作用。BatchSampler类则是把batch size个
RandomSampler类对象封装成一个,这样就实现了随机选取一个batch的目的。(是截取的不知道哪位写的话,当时好像代码里用到了这个接口搜了一下,不知道用不用得上就先抄在笔记上了)

⑬安装gdal https://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal下载  
放到D:\anzhuanglujing\anaconda\anaconda3\Scripts
cd D:\anzhuanglujing\anaconda\anaconda3\Scripts
pip install GDAL-3.2.2-cp38-cp38-win_amd64.whl

⑭(unicode error) 'unicodeescape' codec can't decode bytes in position 2332-2333: truncated \uXXXX escape
解决:代码中
“““中的文字”””
也不能有\

⑮Traceback (most recent call last):
  File "train.py", line 52, in <module>
    model.optimize_parameters()   # calculate loss functions, get gradients, update network weights计算损失函数 得到梯度 更新网络权重
  File "D:\wangyr\yunse\pytorch-CycleGAN-and-pix2pix-master\models\cycle_gan_model.py", line 190, in optimize_parameters
    self.backward_G()             # calculate gradients for G_A and G_B
  File "D:\wangyr\yunse\pytorch-CycleGAN-and-pix2pix-master\models\cycle_gan_model.py", line 162, in backward_G
    self.idt_A = self.netG_A(self.real_B)
  File "D:\anzhuanglujing\anaconda\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\wangyr\yunse\pytorch-CycleGAN-and-pix2pix-master\models\networks.py", line 382, in forward
    return self.model(input)
  File "D:\anzhuanglujing\anaconda\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\anzhuanglujing\anaconda\anaconda3\lib\site-packages\torch\nn\modules\container.py", line 100, in forward
    input = module(input)
  File "D:\anzhuanglujing\anaconda\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "D:\anzhuanglujing\anaconda\anaconda3\lib\site-packages\torch\nn\modules\conv.py", line 345, in forward
    return self.conv2d_forward(input, self.weight)
  File "D:\anzhuanglujing\anaconda\anaconda3\lib\site-packages\torch\nn\modules\conv.py", line 341, in conv2d_forward
    return F.conv2d(input, weight, self.bias, self.stride,
RuntimeError: [enforce fail at ..\c10\core\CPUAllocator.cpp:72] data. DefaultCPUAllocator: not enough memory: you tried to allocate 822083584 bytes. Buy new RAM!

buy就bye,好吧,换了个电脑跑

最后是跑通了,正在写论文中……

⑯整体步骤conda activate D:\anzhuanglujing\anaconda\anaconda3或者 source activate wyr(切换环境)

python -m visdom.server(确保没被占用,被占用了就kill一下)

python train.py --dataroot ./datasets/datasetone --name yunse2 --model cycle_gan --gpu_ids -1 --n_epochs 200 --n_epochs_decay 200(限定训练200个)

待补充

  • 8
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
cyclegan-pytorch-master是一个基于PyTorch深度学习框架实现的CycleGAN模型CycleGAN是一种无监督的图像转换方法,它可以在两个不同领域的图像之间进行双向转换。 具体而言,cyclegan-pytorch-master中的代码实现了CycleGAN中的生成器和判别器网络,以及训练循环和测试函数。生成器网络负责将输入图像从一个领域转换到另一个领域,而判别器网络则用于区分生成的图像和真实的图像。生成器和判别器使用卷积神经网络结构进行建模,可以通过训练过程不断优化网络参数。 在训练循环中,通过最小化生成图像和真实图像之间的差异,使得生成器能够逐渐学习到领域之间的映射关系。同时,判别器也在不断优化中,使其能够更准确地区分生成的图像和真实的图像。通过交替训练生成器和判别器,CycleGAN可以实现两个领域之间的双向图像转换。 此外,在cyclegan-pytorch-master中还包含了测试函数,可将训练好的模型应用于新的图像转换任务。测试函数可以加载已训练好的生成器模型,并将输入图像转换到另一个领域中。通过这种方式,用户可以在训练过的模型上进行图像转换,实现各种有趣的应用,如狗到猫的转换、夏天到冬天的转换等。 总而言之,cyclegan-pytorch-master是一个实现了CycleGAN模型的PyTorch代码库,可用于图像领域之间的双向转换任务,并提供了训练循环和测试函数来支持模型训练和应用。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值