-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutils.py
60 lines (52 loc) · 1.41 KB
/
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import torch
import numpy as np
class AvgMeter(object) :
def __init__(self) :
self.reset()
def reset(self) :
self.sum = 0
self.count = 0
def __call__(self, val = None, reset = False) :
if val is not None :
self.sum += val
self.count += 1
result = 0
if self.count > 0 :
result = self.sum / self.count
if reset :
self.reset()
return result
class ImagePool(object) :
def __init__(self, size, device) :
self.size = size
self.bs = 0
self.count = 0
self.buffer = None
self.device = device
def put(self, images: torch.Tensor) :
if self.size == 0 :
return
if self.bs == 0 :
self.bs = images.size(0)
assert self.bs < self.size
else :
assert self.bs == images.size(0)
if self.buffer is None :
self.buffer = torch.zeros(self.size, *images.shape[1:], dtype = images.dtype, device = self.device)
remain_cap = self.size - self.count
if remain_cap >= self.bs :
# append back
self.buffer[self.count: self.count + self.bs] = images.detach()
else :
self.buffer[remain_cap: remain_cap + self.count] = self.buffer[0: self.count]
self.buffer[: self.bs] = images.detach()
self.count = min(self.count + self.bs, self.size)
def available(self) :
return self.count > 0
def sample(self) :
assert self.count > 0
indices = list(range(self.count))
np.random.shuffle(indices)
indices = indices[: self.bs]
images = self.buffer[indices].contiguous()
return images