-
Notifications
You must be signed in to change notification settings - Fork 2
/
image_pool.py
33 lines (29 loc) · 990 Bytes
/
image_pool.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import random
import numpy as np
import torch
from torch.autograd import Variable
from collections import deque
class ImagePool():
def __init__(self, pool_size):
self.pool_size = pool_size
self.sample_size = pool_size
if self.pool_size > 0:
self.num_imgs = 0
self.images = deque()
def add(self, images):
if self.pool_size == 0:
return images
for image in images.data:
image = torch.unsqueeze(image, 0)
if self.num_imgs < self.pool_size:
self.num_imgs = self.num_imgs + 1
self.images.append(image)
else:
self.images.popleft()
self.images.append(image)
def query(self):
if len(self.images) > self.sample_size:
return_images = list(random.sample(self.images, self.sample_size))
else:
return_images = list(self.images)
return torch.cat(return_images, 0)