[Pytorch系列-69]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - test.py代码详解

作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客

本文网址:[Pytorch系列-66]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - pix2pix test代码详解_文火冰糖(王文兵)的博客-CSDN博客


目录

第1章 pix2pix测试代码

1.1 代码路径

1.2 关键命令行参数

第2章 测试代码主要流程

第3章 代码详解


第1章 pix2pix测试代码

1.1 代码路径

.\pytorch-CycleGAN-and-pix2pix\test

1.2 关键命令行参数(以pix2pix为例)

-dataroot ./datasets/facades --direction BtoA --model pix2pix --name facades_pix2pix --verbose

其中 --verbose:表示打印网络架构

第2章 测试代码主要流程

(1)获取命令行参数:opt = TestOptions().parse() 

(2)设置test模式下命令命令行参数

(3)创建数据集:dataset = create_dataset(opt) 

(4)创建模型pix2pix模型: model = create_model(opt) 

(5)加载预预训练模型参数:model.setup(opt) 

预训练模型的位置有opt参数指定。

print(model.model_names)
print(model.visual_names)

[Network G] Total number of parameters : 54.414 M
-----------------------------------------------
['G']
['real_A', 'fake_B', 'real_B']

备注:

  • pix2pix的test,只有G网络,没有D网络。
  • 在pix2pix网络中,有三个关键点的图片:['real_A', 'fake_B', 'real_B'],其中real_A为输入图片,fake_B为创作生成的图片,real_B为与创作对应的真实图片,用它与创作图片进行比较和验证,以确定输出图片是否精准。

(6)构建web输出结构

(7)设置在评估模式:model.eval()

(8)读取数据集:for i, data in enumerate(dataset):

(9)unpack成对数据:model.set_input(data)

(10)根据输入图片,生成创作图片:model.test() , =》 调用foward() 进行前向预测

而前向预测只有G网络,没有D网络:

    def forward(self):
        """Run forward pass; called by both functions <optimize_parameters> and <test>."""
        self.fake_B = self.netG(self.real_A)  # G(A)

(11)获取输出图片:visuals = model.get_current_visuals(),图片包括:real_A(输入图片), fake_B(创作图片), real_B (用于人工比较)这三部分。

(12)存储图片:save_images  =》 pytorch-CycleGAN-and-pix2pix\results\facades_pix2pix\test_latest\images\xxx

(13)存储Website:webpage.save()   =》pytorch-CycleGAN-and-pix2pix\results\facades_pix2pix\test_latest\index.htlm

第3章 代码详解

   # 获取命令行参数
    opt = TestOptions().parse()  # get test options
    
    # hard-code some parameters for test
    opt.num_threads = 0   # test code only supports num_threads = 1
    opt.batch_size = 1    # test code only supports batch_size = 1
    opt.serial_batches = True  # disable data shuffling; comment this line if results on randomly chosen images are needed.
    opt.no_flip = True    # no flip; comment this line if results on flipped images are needed.
    opt.display_id = -1   # no visdom display; the test code saves the results to a HTML file.

    # 创建数据集
    dataset = create_dataset(opt)  # create a dataset given opt.dataset_mode and other options
    
    # 创建模型
    model = create_model(opt)      # create a model given opt.model and other options

    # 加载预训练模型
    model.setup(opt)               # regular setup: load and print networks; create schedulers

    # 构建web输出
    # create a website
    web_dir = os.path.join(opt.results_dir, opt.name, '{}_{}'.format(opt.phase, opt.epoch))  # define the website directory
    if opt.load_iter > 0:  # load_iter is 0 by default
        web_dir = '{:s}_iter{:d}'.format(web_dir, opt.load_iter)
    print('creating web directory', web_dir)
    webpage = html.HTML(web_dir, 'Experiment = %s, Phase = %s, Epoch = %s' % (opt.name, opt.phase, opt.epoch))

    # test with eval mode. This only affects layers like batchnorm and dropout.
    # For [pix2pix]: we use batchnorm and dropout in the original pix2pix. You can experiment it with and without eval() mode.
    # For [CycleGAN]: It should not affect CycleGAN as CycleGAN uses instancenorm without dropout.

    # 设置在评估模式
    if opt.eval:
        model.eval()

    # 读取数据集数据
    for i, data in enumerate(dataset):
        if i >= opt.num_test:  # only apply our model to opt.num_test images.
            break
            
        # unpack成对数据
        model.set_input(data)  # unpack data from data loader
        
        # 生成图片
        model.test()           # run inference
        
        # 获取输出图片:real_A(输入图片), fake_B(创作图片), real_B (用于人工比较)
        visuals = model.get_current_visuals()  # get image results
        
        # 获取存放图片的路径
        img_path = model.get_image_paths()     # get image paths
        
        # 每运行5次,打印一次提示
        if i % 5 == 0:  # save images to an HTML file
            print('processing (%04d)-th image... %s' % (i, img_path))
            
        #存储图片
        save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize)
    
    # 图片信息存放到web中,用于可视化显示
    webpage.save()  # save the HTML

----------------- Options ---------------
             aspect_ratio: 1.0                           
               batch_size: 1                             
          checkpoints_dir: ./checkpoints                 
                crop_size: 256                           
                 dataroot: ./datasets/facades                [default: None]
             dataset_mode: aligned                       
                direction: BtoA                              [default: AtoB]
          display_winsize: 256                           
                    epoch: latest                        
                     eval: False                         
                  gpu_ids: 0                             
                init_gain: 0.02                          
                init_type: normal                        
                 input_nc: 3                             
                  isTrain: False                             [default: None]
                load_iter: 0                                 [default: 0]
                load_size: 256                           
         max_dataset_size: inf                           
                    model: pix2pix                           [default: test]
               n_layers_D: 3                             
                     name: facades_pix2pix                   [default: experiment_name]
                      ndf: 64                            
                     netD: basic                         
                     netG: unet_256                      
                      ngf: 64                            
               no_dropout: False                         
                  no_flip: False                         
                     norm: batch                         
                    ntest: inf                           
                 num_test: 50                            
              num_threads: 4                             
                output_nc: 3                             
                    phase: test                          
               preprocess: resize_and_crop               
              results_dir: ./results/                    
           serial_batches: False                         
                   suffix:                               
                  verbose: True                              [default: False]
----------------- End -------------------
dataset [AlignedDataset] was created
initialize network with normal
model [Pix2PixModel] was created
loading the model from ./checkpoints\facades_pix2pix\latest_net_G.pth
---------- Networks initialized -------------
DataParallel(
  (module): UnetGenerator(
    (model): UnetSkipConnectionBlock(
      (model): Sequential(
        (0): Conv2d(3, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
        (1): UnetSkipConnectionBlock(
          (model): Sequential(
            (0): LeakyReLU(negative_slope=0.2, inplace=True)
            (1): Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
            (2): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
            (3): UnetSkipConnectionBlock(
              (model): Sequential(
                (0): LeakyReLU(negative_slope=0.2, inplace=True)
                (1): Conv2d(128, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                (2): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                (3): UnetSkipConnectionBlock(
                  (model): Sequential(
                    (0): LeakyReLU(negative_slope=0.2, inplace=True)
                    (1): Conv2d(256, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                    (2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                    (3): UnetSkipConnectionBlock(
                      (model): Sequential(
                        (0): LeakyReLU(negative_slope=0.2, inplace=True)
                        (1): Conv2d(512, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                        (2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                        (3): UnetSkipConnectionBlock(
                          (model): Sequential(
                            (0): LeakyReLU(negative_slope=0.2, inplace=True)
                            (1): Conv2d(512, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                            (2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                            (3): UnetSkipConnectionBlock(
                              (model): Sequential(
                                (0): LeakyReLU(negative_slope=0.2, inplace=True)
                                (1): Conv2d(512, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                                (2): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                                (3): UnetSkipConnectionBlock(
                                  (model): Sequential(
                                    (0): LeakyReLU(negative_slope=0.2, inplace=True)
                                    (1): Conv2d(512, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                                    (2): ReLU(inplace=True)
                                    (3): ConvTranspose2d(512, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                                    (4): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                                  )
                                )
                                (4): ReLU(inplace=True)
                                (5): ConvTranspose2d(1024, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                                (6): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                                (7): Dropout(p=0.5, inplace=False)
                              )
                            )
                            (4): ReLU(inplace=True)
                            (5): ConvTranspose2d(1024, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                            (6): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                            (7): Dropout(p=0.5, inplace=False)
                          )
                        )
                        (4): ReLU(inplace=True)
                        (5): ConvTranspose2d(1024, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                        (6): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                        (7): Dropout(p=0.5, inplace=False)
                      )
                    )
                    (4): ReLU(inplace=True)
                    (5): ConvTranspose2d(1024, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                    (6): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
                  )
                )
                (4): ReLU(inplace=True)
                (5): ConvTranspose2d(512, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
                (6): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
              )
            )
            (4): ReLU(inplace=True)
            (5): ConvTranspose2d(256, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False)
            (6): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
          )
        )
        (2): ReLU(inplace=True)
        (3): ConvTranspose2d(128, 3, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1))
        (4): Tanh()
      )
    )
  )
)
[Network G] Total number of parameters : 54.414 M
-----------------------------------------------
['G']
['real_A', 'fake_B', 'real_B']

creating web directory ./results/facades_pix2pix\test_latest
C:\ProgramData\Anaconda3\envs\pytorch-gpu-os\lib\site-packages\torchvision\transforms\transforms.py:287: UserWarning: Argument interpolation should be of type InterpolationMode instead of int. Please, use InterpolationMode enum.
  warnings.warn(
processing (0000)-th image... ['./datasets/facades\\test\\1.jpg']
processing (0005)-th image... ['./datasets/facades\\test\\103.jpg']
processing (0010)-th image... ['./datasets/facades\\test\\12.jpg']
processing (0015)-th image... ['./datasets/facades\\test\\17.jpg']
processing (0020)-th image... ['./datasets/facades\\test\\21.jpg']
processing (0025)-th image... ['./datasets/facades\\test\\26.jpg']
processing (0030)-th image... ['./datasets/facades\\test\\30.jpg']
processing (0035)-th image... ['./datasets/facades\\test\\35.jpg']
processing (0040)-th image... ['./datasets/facades\\test\\4.jpg']
processing (0045)-th image... ['./datasets/facades\\test\\44.jpg']


作者主页(文火冰糖的硅基工坊):文火冰糖(王文兵)的博客_文火冰糖的硅基工坊_CSDN博客

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/122075510

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
你可以使用 PyTorch-CycleGAN-and-pix2pix 库来使用预训练好的模型。下面是一个简单的步骤示例: 1. 首先,确保你已经安装了 PyTorch-CycleGAN-and-pix2pix 库。你可以使用以下命令安装: ``` pip install git+https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix ``` 2. 下载预训练模型。你可以在 CycleGANpix2pix 的模型网页(https://github.com/junyanz/pytorch-CycleGAN-and-pix2pix#model-checkpoints)上找到预训练模型的链接。下载并解压缩模型文件夹。 3. 创建一个配置文件。在模型文件夹中,复制并重命名 `test_opt.txt.example` 文件为 `test_opt.txt`。该文件用于配置测试参数。 4. 配置测试参数。打开 `test_opt.txt` 文件,并根据你的需求修改参数。重要的参数包括 `dataroot`(数据集的路径)和 `name`(模型名称)。 5. 运行测试脚本。使用以下命令运行测试脚本: ``` python test.py --dataroot ./path/to/dataset --name pretrained_model_name --model test_model_name ``` 确保将 `./path/to/dataset` 替换为你的数据集路径,`pretrained_model_name` 替换为你下载的预训练模型文件夹的名称,`test_model_name` 替换为你想要使用的测试模型的名称(如 `cycle_gan` 或 `pix2pix`)。 6. 查看结果。测试完成后,生成的结果将保存在模型文件夹中的 `results` 子文件夹中。 请注意,这只是一个基本的示例,你可能需要根据你的具体情况进行更多的配置和调整。你可以参考 PyTorch-CycleGAN-and-pix2pix 库的文档以获取更多详细信息和用法示例。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

文火冰糖的硅基工坊

你的鼓励是我前进的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值