-
Notifications
You must be signed in to change notification settings - Fork 0
/
Archive.py
101 lines (80 loc) · 3.25 KB
/
Archive.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import numpy as np
class Cell():
# item in archive
def __init__(self, idx, restore, frame, embedding=None, score=-np.inf, proxies=14):
self.visits = 0
self.idx = idx
self.restore = restore
self.embedding = embedding
self.score = score
self.frame = frame
self.checkpoint_proxies = []
self.i_p = 0
self.num_proxies = proxies
def add_proxy(self, frame, restore):
if len(self.checkpoint_proxies) <= self.num_proxies:
self.checkpoint_proxies.append((frame, restore))
self.i_p += 1
else:
if self.i_p >= self.num_proxies:
self.i_p = 0
self.checkpoint_proxies[self.i_p] = (frame, restore)
self.i_p += 1
class Archive():
def __init__(self):
# idx | cell
self.cells = {}
def __iter__(self):
return iter(self.cells)
def init_archive(self, start_info):
self.cells = {}
# start cell
self.cells[start_info[0]] = Cell(start_info[0],start_info[1],start_info[2],
start_info[3], score=0)
class CellSeletor():
# select starting cells
def __init__(self, archive):
self.archive = archive
def select_cells(self, amount, best_score):
keys = []
weights = []
for key in self.archive.cells:
if key == None: # done cell
weights.append(0.0)
else:
w_visits = 1/(np.sqrt(self.archive.cells[key].visits)+1)
w_score = np.maximum(self.archive.cells[key].score / (best_score+1), 0.075)
weights.append(w_visits * w_score)
keys.append(key)
indexes = np.random.choice(range(len(weights)),size=amount,p=weights/np.sum(weights))
selected_cells = []
for i in indexes:
selected_cells.append(self.archive.cells[keys[i]])
return selected_cells
def select_proxies(self, amount, best_score):
# select checkpoint from which to sample proxies
# sample based on checkpoint visits
weights = []
keys = []
for key in self.archive.cells:
w_visits = 1/(np.sqrt(self.archive.cells[key].visits)+1)
w_score = np.maximum(self.archive.cells[key].score / (best_score+1), 0.075)
weights.append(w_visits * w_score)
#weights.append(1/(np.sqrt(self.archive.cells[key].visits)+1))
keys.append(key)
cell_keys = np.random.choice(range(len(weights)),size=amount,p=weights/np.sum(weights))
proxies = []
for key in cell_keys:
cell = self.archive.cells[key]
cell_proxies = cell.checkpoint_proxies
num_proxies = len(cell_proxies)
if num_proxies == 0:
proxies.append((cell.frame, cell.restore))
else:
proxy_idx = np.random.choice(range(num_proxies), size=1)[0]
proxies.append(cell.checkpoint_proxies[proxy_idx])
return proxies