Return an image from the pool. Parameters: images: the latest generated images from the generator Returns images from the buffer. By 50/100, the buffer will return input images. By 50/100, the buffer will return images previously stored in the buffer,
(self, images)
| 21 | self.images = [] |
| 22 | |
| 23 | def query(self, images): |
| 24 | """Return an image from the pool. |
| 25 | |
| 26 | Parameters: |
| 27 | images: the latest generated images from the generator |
| 28 | |
| 29 | Returns images from the buffer. |
| 30 | |
| 31 | By 50/100, the buffer will return input images. |
| 32 | By 50/100, the buffer will return images previously stored in the buffer, |
| 33 | and insert the current images to the buffer. |
| 34 | """ |
| 35 | if self.pool_size == 0: # if the buffer size is 0, do nothing |
| 36 | return images |
| 37 | return_images = [] |
| 38 | for image in images: |
| 39 | image = torch.unsqueeze(image.data, 0) |
| 40 | if self.num_imgs < self.pool_size: # if the buffer is not full; keep inserting current images to the buffer |
| 41 | self.num_imgs = self.num_imgs + 1 |
| 42 | self.images.append(image) |
| 43 | return_images.append(image) |
| 44 | else: |
| 45 | p = random.uniform(0, 1) |
| 46 | if p > 0.5: # by 50% chance, the buffer will return a previously stored image, and insert the current image into the buffer |
| 47 | random_id = random.randint(0, self.pool_size - 1) # randint is inclusive |
| 48 | tmp = self.images[random_id].clone() |
| 49 | self.images[random_id] = image |
| 50 | return_images.append(tmp) |
| 51 | else: # by another 50% chance, the buffer will return the current image |
| 52 | return_images.append(image) |
| 53 | return_images = torch.cat(return_images, 0) # collect all the images and return |
| 54 | return return_images |