Pytorch-cyclegan-and-pix2pix: What is the motivation for setting up ImagePool? & What is the role of the query function?

Created on 15 Oct 2018  ·  2Comments  ·  Source: junyanz/pytorch-CycleGAN-and-pix2pix

When I read the source code, I had a question about ImagePool and found this problem in this issues-75 What's the purpose of ImagePool? issue.but from the source code, I still did not understand the motivation and role of using ImagePool in the source code, so I have the following problems:

  1. What is the motivation and role of using ImagePool in source code? Is there any source for this idea or can the paper be referenced?
  2. What is the role of the query member function in the ImagePool class? Why do you want to introduce random number operations in functions?
    I sincerely hope that I can get detailed answers, I am grateful!
    Here is the code for ImagePool:
import random
import torch

# the size of image buffer that stores previously generated images
class ImagePool():
    def __init__(self, pool_size):
        self.pool_size = pool_size
        if self.pool_size > 0:
            self.num_imgs = 0
            self.images = []

    def query(self, images):
        if self.pool_size == 0:
            return images
        return_images = []
        for image in images:
            image = torch.unsqueeze(image.data, 0)
            if self.num_imgs < self.pool_size:
                self.num_imgs = self.num_imgs + 1
                self.images.append(image)
                return_images.append(image)
            else:
                p = random.uniform(0, 1)
                if p > 0.5:
                    random_id = random.randint(0, self.pool_size - 1)  # randint is inclusive
                    tmp = self.images[random_id].clone()
                    self.images[random_id] = image
                    return_images.append(tmp)
                else:
                    return_images.append(image)
        return_images = torch.cat(return_images, 0)
        return return_images

Most helpful comment

It was introduced by this paper. See Sec 2.3 for more details.

All 2 comments

@junyanz @SsnL @taesung89

It was introduced by this paper. See Sec 2.3 for more details.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

filmo picture filmo  ·  3Comments

ShaniGam picture ShaniGam  ·  4Comments

nootfly picture nootfly  ·  4Comments

wjx2 picture wjx2  ·  3Comments

HectorAnadon picture HectorAnadon  ·  4Comments