-
Notifications
You must be signed in to change notification settings - Fork 27
/
imagepool.py
31 lines (30 loc) · 1.1 KB
/
imagepool.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
import mxnet as mx
from mxnet import nd
import numpy as np
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
ret_imgs = []
for i in range(images.shape[0]):
image = nd.expand_dims(images[i], axis=0)
if self.num_imgs < self.pool_size:
self.num_imgs = self.num_imgs + 1
self.images.append(image)
ret_imgs.append(image)
else:
p = nd.random_uniform(0, 1, shape=(1,)).asscalar()
if p > 0.5:
random_id = nd.random_uniform(0, self.pool_size - 1, shape=(1,)).astype(np.uint8).asscalar()
tmp = self.images[random_id].copy()
self.images[random_id] = image
ret_imgs.append(tmp)
else:
ret_imgs.append(image)
ret_imgs = nd.concat(*ret_imgs, dim=0)
return ret_imgs