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

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

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


目录

第1章 Train.py代码

1.1 代码路径

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

第2章 训练代码主要流程

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

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

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

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

(5)加载并打印训练模型结构:model.setup(opt) 

(6)构建web输出结构

(7)设置在train模式:model.train()

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

(10)计算梯度,优化参数:model.optimize_parameters()  

(11)获取输出图片

(12)可视化打印loss

(13)存储网络模型

(14)更新学习率

第3章 代码详解



第1章 Train.py代码

1.1 代码路径

.\pytorch-CycleGAN-and-pix2pix\train

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

--dataroot ./datasets/horse2zebra --name horse2zebra --model cycle_gan --verbose

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

第2章 训练代码主要流程

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

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

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

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

根据选项参数,确定模型的类型。

(5)加载并打印训练模型结构:model.setup(opt) 

预训练模型的由opt参数指定。

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

initialize network with normal
initialize network with normal
initialize network with normal
model [CycleGANModel] was created


---------- Networks initialized -------------
[Network G_A] Total number of parameters : 11.378 M
[Network G_B] Total number of parameters : 11.378 M
[Network D_A] Total number of parameters : 2.765 M
[Network D_B] Total number of parameters : 2.765 M
-----------------------------------------------

[Pytorch系列-73]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - Train.py代码详解_第1张图片

(6)构建web输出结构

visualizer = Visualizer(opt) 

(7)设置在train模式:model.train()

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

[Pytorch系列-73]:生成对抗网络GAN - 图像生成开源项目pytorch-CycleGAN-and-pix2pix - Train.py代码详解_第2张图片

 

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

(10)计算梯度,优化参数:model.optimize_parameters()  

这一步骤是模型训练的最核心的代码:

    def optimize_parameters(self):
        """Calculate losses, gradients, and update network weights; called in every training iteration"""
        # 生成fake图片
        # forward
        self.forward()      # compute fake images and reconstruction images.

        # G_A and G_B
        self.set_requires_grad([self.netD_A, self.netD_B], False)  # Ds require no gradients when optimizing Gs
        self.optimizer_G.zero_grad()  # set G_A and G_B's gradients to zero

        # G网络的反向求导
        self.backward_G()             # calculate gradients for G_A and G_B
        
        # G网络的参数更新
        self.optimizer_G.step()       # update G_A and G_B's weights

        # 
        # D_A and D_B
        self.set_requires_grad([self.netD_A, self.netD_B], True)
        self.optimizer_D.zero_grad()   # set D_A and D_B's gradients to zero

        # D网络的反向求导
        self.backward_D_A()      # calculate gradients for D_A
        self.backward_D_B()      # calculate graidents for D_B
        
        # D网络的参数更新
        self.optimizer_D.step()  # update D_A and D_B's weights

(11)获取输出图片

model.compute_visuals()
visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)

(12)可视化打印loss

losses = model.get_current_losses()

visualizer.print_current_losses(epoch, epoch_iter, losses, t_comp, t_data)

(13)存储网络模型

model.save_networks(save_suffix)

(14)更新学习率

model.update_learning_rate()    

第3章 代码详解

if __name__ == '__main__':
    opt = TrainOptions().parse()   # get training options
    dataset = create_dataset(opt)  # create a dataset given opt.dataset_mode and other options
    dataset_size = len(dataset)    # get the number of images in the dataset.
    print('The number of training images = %d' % dataset_size)

    model = create_model(opt)      # create a model given opt.model and other options
    model.setup(opt)               # regular setup: load and print networks; create schedulers
    visualizer = Visualizer(opt)   # create a visualizer that display/save images and plots
    total_iters = 0                # the total number of training iterations

    for epoch in range(opt.epoch_count, opt.niter + opt.niter_decay + 1):    # outer loop for different epochs; we save the model by , +
        epoch_start_time = time.time()  # timer for entire epoch
        iter_data_time = time.time()    # timer for data loading per iteration
        epoch_iter = 0                  # the number of training iterations in current epoch, reset to 0 every epoch
        visualizer.reset()              # reset the visualizer: make sure it saves the results to HTML at least once every epoch

        for i, data in enumerate(dataset):  # inner loop within one epoch
            iter_start_time = time.time()  # timer for computation per iteration
            if total_iters % opt.print_freq == 0:
                t_data = iter_start_time - iter_data_time

            total_iters += opt.batch_size
            epoch_iter += opt.batch_size
            model.set_input(data)         # unpack data from dataset and apply preprocessing

            model.optimize_parameters()   # calculate loss functions, get gradients, update network weights

            if total_iters % opt.display_freq == 0:   # display images on visdom and save images to a HTML file
                save_result = total_iters % opt.update_html_freq == 0
                model.compute_visuals()
                visualizer.display_current_results(model.get_current_visuals(), epoch, save_result)

            if total_iters % opt.print_freq == 0:    # print training losses and save logging information to the disk
                losses = model.get_current_losses()
                t_comp = (time.time() - iter_start_time) / opt.batch_size
                visualizer.print_current_losses(epoch, epoch_iter, losses, t_comp, t_data)
                if opt.display_id > 0:
                    visualizer.plot_current_losses(epoch, float(epoch_iter) / dataset_size, losses)

            if total_iters % opt.save_latest_freq == 0:   # cache our latest model every  iterations
                print('saving the latest model (epoch %d, total_iters %d)' % (epoch, total_iters))
                save_suffix = 'iter_%d' % total_iters if opt.save_by_iter else 'latest'
                model.save_networks(save_suffix)

            iter_data_time = time.time()
        if epoch % opt.save_epoch_freq == 0:              # cache our model every  epochs
            print('saving the model at the end of epoch %d, iters %d' % (epoch, total_iters))
            model.save_networks('latest')
            model.save_networks(epoch)

        print('End of epoch %d / %d \t Time Taken: %d sec' % (epoch, opt.niter + opt.niter_decay, time.time() - epoch_start_time))
        model.update_learning_rate()                     # update learning rates at the end of every epoch.

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

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

你可能感兴趣的:(人工智能-PyTorch,pytorch,深度学习,python,Train,流程)