论文pytorch-CycleGAN-and-pix2pix

github源代码:GitHub - junyanz/pytorch-CycleGAN-and-pix2pix: Image-to-Image Translation in PyTorch

论文详解:http://t.csdn.cn/q9hUS

二、运行代码

1.下载源代码

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

conda env create -f environment.yml #创建conda环境
conda activate pytorch-CycleGAN-and-pix2pix #激活conda环境

        如下图,在conda(pytorch-CycleGAN-and-pix2pix)环境中运行代码。

2.准备数据集

        “prepare_ACDC_datasets.py”将数据集ACDC/night中图片均裁成1:1的.jpg格式,保存在“./zhw/pytorch-CycleGAN-and-pix2pix/datasets/ACDC/day2night”,数据集文件需要如下形式 ,其中B为normal情况,A为night。

 3.train训练

        可以通过visdom在训练过程中查看训练情况和loss损失,在训练前开启visdom,用法如下:

pip install visdom
python -m visdom.server

##如果Error: Address already in use.
lsof -i tcp:8097
kill -9 xxx #xxx被占用的pid号码
lsof -i tcp:8097
python3 -m visdom.server

如果出现'Address already in use'的错误,参考http://t.csdn.cn/0ZWin

点击网址localhost:8097,发现仍然出错tornado,网址无法打开,则参考http://t.csdn.cn/ifO1r

1.在github下载下载visdom-master代码;GitHub - fossasia/visdom: A flexible tool for creating, organizing, and sharing visualizations of live, rich data. Supports Torch and Numpy.2.先在‘visdom-master\py\visdom\server.py’接近文件结尾处注释download_scripts

3.找到visdom-master\py\visdom\static\index.html替换为如下内容html;

4.将下载的static文件夹替换到‘anaconda/envs/环境名/lib/python3.6/site-packages/visdom/static’。static文件夹压缩包见我发布的资源。

<!--

Copyright 2017-present, Facebook, Inc.
All rights reserved.

This source code is licensed under the license found in the
LICENSE file in the root directory of this source tree.

-->

<!doctype html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link rel="shortcut icon" href="favicon.png">

    <!-- Bootstrap & jQuery -->
    <link rel="stylesheet" href={{ static_url("css/bootstrap.min.css") }}>
    <script src={{ static_url("js/jquery.min.js") }}></script>
    <script src={{ static_url("js/bootstrap.min.js") }}></script>

    <link rel="stylesheet" href={{ static_url("css/react-resizable-styles.css") }}>
    <link rel="stylesheet" href={{ static_url("css/react-grid-layout-styles.css") }}>

    <!-- Other deps -->
    <script src={{ static_url("js/react-react.min.js") }}></script>
    <script src={{ static_url("js/react-dom.min.js") }}></script>
    <script src={{ static_url("fonts/layout_bin_packer") }}></script>

    <!-- Mathjax -->
    <script type="text/javascript" async src={{ static_url("js/mathjax-MathJax.js") }}></script>

    <!-- Plotly -->
    <script src={{ static_url("js/plotly-plotly.min.js") }}></script>

    <!-- Custom styles for this template -->
    <script>
    // TODO: this is not great. Should probably be an endpoint with a JSON
    // response or the first thing the socket sends back.
    var ENV_LIST = [
      {% for item in items %}
      '{{escape(item)}}',
      {% end %}
    ];
    var ACTIVE_ENV = '{{escape(active_item)}}';
    var USER = '{{escape(user)}}';

    // Plotly setup
    window.PLOTLYENV = window.PLOTLYENV || {};
    window.PLOTLYENV.BASE_URL = 'https://plot.ly';

    </script>
    <script src={{ static_url("js/main.js") }}></script>
    <link rel="stylesheet" href={{ static_url("css/style.css") }}>

    <title>visdom</title>
    <!-- <link rel="icon" href="http://example.com/favicon.png"> -->
  </head>

  <body>
    <noscript>JS is required</noscript>
    <div id="app"></div>
  </body>
</html>

点击网址,若visdom运行成功则在开始训练后开始如下界面:

        开启visdom后训练,训练结束的模型存放在'checkpoints/FDA/day2night_cyclegan/'。

#!./scripts/train_cyclegan.sh
python train.py --dataroot ./datasets/ACDC/night/day2night/ --name day2night_cyclegan --model cycle_gan

         最后day2night和day2rain模型训练过程loss趋势图如下,是收敛的。

4.test测试

#!./scripts/test_cyclegan.sh
python test.py --dataroot ./datasets/ACDC/night/day2night/ --name day2night_cyclegan --model cycle_gan

        测试结果在'./results/day2night_cyclegan/latest_test/index.html',包括real、fake、rec。realA为A原图,fakeA为B转换为A风格的结果,recA为A转换到B风格再回到A风格的结果F(G(A))。测试结果如下:

5.应用模型

        训练前需要在'./model/base_model.py'line192修改参数。test和apply代码修改不同:

load_filename='%s_net_%s' %(epoch,name) #test
load_filename='%s_net_%s_B' %(epoch,name) #apply:day2night
load_filename='%s_net_%s_A' %(epoch,name) #apply:day2rain

在'./option/test_option.py'需要修改转换风格后图像存储位置'--results='和转换数量'--num_test'。test与apply代码略有不同,结果存放在'./results/day2rain_cyclegan'。文件包含'_A_fake.jpg'为normal的cityscapes转换为night风格的结果,其对应labels存放在'./datasets/cityscapes/testB'。

##day2night
python test.py --dataroot datasets/cityscapes1/testB --name day2night_cyclegan --model test --no_dropout

##day2rain
python test.py --dataroot datasets/cityscapes1/testA --name day2rain_cyclegan --model test --no_dropout

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值