使用pytorch-pix2pix实现边图到真实图片的转换

论文 pix2pix image to image translation

Here is some explanation together with a link to pix2pix’s image traslation paper

Github 项目 pytorch pix2pix

Github pytorch’s implementation, you can simply clone the repo in the command line using:

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

in which a pix2pix network model is ready for us.

数据集 datasets

Construct dataset by:

Create dataset folder:
我们需要根据要求创建数据集的文件夹:

mkdir ./datasets/fire_hydrants
cd ./datasets/fire_hydrants
# requires subdirectories
mkdir A B train test val
cd A
mkdir train test val
cd ../B
mkdir train test val

Formatted data:

按顺序命名图片,例如:0.png,1.png etc。这可以在爬取图片的时候顺便

你也可以编写一个PowerShell脚本(我是在Windows下完成爬取,边界提取和重命名的,然后在Mac上进行训练)来完成这个工作:

# initialize loop variable
$i = 0
# path of your directory
$Path = 'path/to/your/dataset/directory'
$images = Get-ChildItem -Path $Path
# get extension
$Extension = [System.Io.Path]::GetExtension($images[0].Name)
$images | Rename-Item -NewName {
	'{0}.{1}' $script:i, $Extension
	$script:i++
}

Combined A and B
在按照要求对图片进行命名之后,我们通过运行脚本datasets/combine_A_and_B.py将A和B中对应文件夹下(例如,train 中)对应序号的图片进行合并:

python datasets/combine_A_and_B.py --fold_A datasets/fire_hydrants/A
--fold_B datasets/fire_hydrants/B --fold_AB datasets/fire_hydrants

--fold_AB就是输出目录,这个目录下必须要有和A,B同样的子目录(train, test and val)
最终将会得到类似这样的结果:
在这里插入图片描述
真实图像和边图合并在一张图像里面,我们将使用这些图片进行训练和测试。

可视化 Visualization

在终端中输入:

python -m visdom.server

然后可以在浏览器中进入http://localhost:8097,你将会看到整个训练和测试的可视化过程:

  • 损失随时间的变化
    在这里插入图片描述
  • 测试过程以及对应图片的标签:
    在这里插入图片描述
    在这里插入图片描述

训练 Training

Command:

python train.py --dataroot ./datasets/fire_hydrants --name 
fire_hydrant_experiment --model pix2pix --gpu_ids -1 --direction BtoA

P.S. 这里的方向是从边图数据集到真实图像数据集,这也是我们想要做的工作,根据你的数据集存放方式进行设置
Options Explanation:

OptionExplanation
–datarootpath to images (should have subfolders A and B,
both of which contain test, train and val.)
–namename of the experiment
–modelchooses which model to use. [cycle_gan | pix2pix | …]
–directionAtoB or BtoA

./options/base_options.py中给出了每个基本选项的解释。
而在./options/train_options.py./options/test_options.py分别给出了训练和测试的相关选项和解释。

测试 Testing

Command:

python test.py --dataroot ./datasets/fire_hydrants --name hydrants_edge2photo
--model pix2pix --gpu_ids -1 --direction BtoA 

result:
在这里插入图片描述

中间为生成的图片

最后 Last

因为训练集不够大,因此生成的效果很不理想,但我们也可以看到,基本的颜色,高光部分和边缘都可以看出来,如果有一个不错的训练集的话,相信可以得到一个比较理想的效果。

F i n \mathscr{Fin} Fin

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值