cycleGAN source Interpretation (III): data read

Source Address: https://github.com/aitorzip/PyTorch-CycleGAN

Read data is relatively straightforward, cycleGAN no pair of data needs, different domains are stored in the two data sets A, B two folders, the interface can be written dataset

fake_A_buffer = ReplayBuffer()
fake_B_buffer = ReplayBuffer()

# Dataset loader
transforms_ = [ transforms.Resize(int(opt.size*1.12), Image.BICUBIC), 
                transforms.RandomCrop(opt.size), 
                transforms.RandomHorizontalFlip(),
                transforms.ToTensor(),
                transforms.Normalize((0.5,0.5,0.5), (0.5,0.5,0.5)) ]
dataloader = DataLoader(ImageDataset(opt.dataroot, transforms_=transforms_, unaligned=True), 
                        batch_size=opt.batchSize, shuffle=True, num_workers=opt.n_cpu)

The above code, defined the first buffer (elaborate later), then the image transform definition of a good, well-defined call ImageDataset (inherited from DataSet) objects, data can be read from the dataloader. The following code is ImageDataset

class ImageDataset(Dataset):
    def __init__(self, root, transforms_=None, unaligned=False, mode='train'):
        self.transform = transforms.Compose(transforms_)
        self.unaligned = unaligned

        self.files_A = sorted(glob.glob(os.path.join(root, '%s/A' % mode) + '/*.*'))
        self.files_B = sorted(glob.glob(os.path.join(root, '%s/B' % mode) + '/*.*'))

    def __getitem__(self, index):
        item_A = self.transform(Image.open(self.files_A[index % len(self.files_A)]))

        if self.unaligned:
            item_B = self.transform(Image.open(self.files_B[random.randint(0, len(self.files_B) - 1)]))
        else:
            item_B = self.transform(Image.open(self.files_B[index % len(self.files_B)]))

        return {'A': item_A, 'B': item_B}

    def __len__(self):
        return max(len(self.files_A), len(self.files_B))

Standards achieved __init__, __getitem__, __len__ three interfaces, but I'm not quite sure here to sort the data and alignment of purpose, alignment can be sequential read, random read is not aligned Finally, with regard to buffer, reference cycleGAN paper, the original words say " Second, to the reduce Oscillation Model [ 15 ], WE Follow  Shrivastava et Al. apos Strategy [ 46 is ] and Update the discrimi nators a History of the using Generated Images the Rather Within last  ones Produced by Latest Generators at The. We the Keep AN Image  Buffer at The Stores that 50 of Previously the Created ImagesRF Royalty Free "

In other words, in order to stabilize the training, the use of false sample history generated to update discriminator, rather than the false samples of the current generation, as to the principle, reference is made to another paper. We look at the code

class ReplayBuffer():
    def __init__(self, max_size=50):
        assert (max_size > 0), 'Empty buffer or trying to create a black hole. Be careful.'
        self.max_size = max_size
        self.data = []

    def push_and_pop(self, data):
        to_return = []
        for element in data.data:
            element = torch.unsqueeze(element, 0)
            if len(self.data) < self.max_size:
                self.data.append(element)
                to_return.append(element)
            else:
                if random.uniform(0,1) > 0.5:
                    i = random.randint(0, self.max_size-1)
                    to_return.append(self.data[i].clone())
                    self.data[i] = element
                else:
                    to_return.append(element)
        return Variable(torch.cat(to_return))

Defines a buffer object that has a data storage table data, the default size is 50, I think it's running process is this: when the data table is not filled, are currently generated false image of each reading, when data table is full, a randomly determined number of random data 1 in the data table, return, and coming up with the current data 2. use the current data

As to why this makes sense, depending on the reference paper

 

 

Guess you like

Origin www.cnblogs.com/wzyuan/p/11899821.html