-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
40 lines (33 loc) · 891 Bytes
/
utils.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
34
35
36
37
38
39
40
#utils
import tensorflow as tf
import random
def convert2float(image):
"""
将0~255转换到-1~1
"""
image = tf.image.convert_image_dtype(image, dtype=tf.float32)
return (image/127.5) - 1.0
class ImagePool:
"""
在线样本池:
在线存储50个样本,有50%概率选择样本池中样本训练,50%概率选择当前样本训练
"""
def __init__(self, pool_size):
self.pool_size = pool_size
self.images = []
def query(self, image):
if self.pool_size == 0:
return image
if len(self.images) < self.pool_size:
self.images.append(image)
return image
else:
p = random.random()
if p > 0.5:
# use old image
random_id = random.randrange(0, self.pool_size)
tmp = self.images[random_id].copy()
self.images[random_id] = image.copy()
return tmp
else:
return image