-
Notifications
You must be signed in to change notification settings - Fork 5
/
reconnector.py
221 lines (169 loc) · 6.54 KB
/
reconnector.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
# Reconnect convolution blocks inside NN:
# helper func to swap
def swap(weights, fixed, num1, num2):
# w[:,:,fixed, num1] <=> w[:,:,fixed, num2]
conv1vals = weights[:, :, fixed, num1]
# print("conv1vals.shape", conv1vals.shape)
conv2vals = weights[:, :, fixed, num2]
# print("conv2vals.shape", conv2vals.shape)
weights[:, :, fixed, num2] = conv1vals
weights[:, :, fixed, num1] = conv2vals
return weights
def setrand(weights, fixed, num1, num2):
# To have a comparable baseline (comparable in terms of the strength of the effect)
# w[:,:,fixed, num1] = RANDOM
# w[:,:,fixed, num2] = RANDOM
conv1vals = weights[:, :, fixed, num1]
#print("conv1vals.shape", conv1vals.shape)
conv2vals = weights[:, :, fixed, num2]
#print("conv2vals.shape", conv2vals.shape)
rand_a = np.random.randn(*conv1vals.shape)
rand_b = np.random.randn(*conv2vals.shape)
#print("rand_a.shape", rand_a.shape)
#print("rand_b.shape", rand_a.shape)
weights[:, :, fixed, num1] = rand_a
weights[:, :, fixed, num2] = rand_b
return weights
# reconnection of conv filters in existing net
def reconnect(net, tensor_name="128x128/Conv0_up/weight", percent_change=10, DO_ALL=True):
weights = get_tensor(net, tensor_name)
res_first = weights.shape[2]
res = weights.shape[3]
possible = list(range(res))
to_select = int((res / 100.0) * percent_change)
print("reconnecting", tensor_name, "weights.shape", weights.shape, " ... selected", to_select, "from", res)
select = np.random.choice(possible, to_select, replace=False)
odds = []
evens = []
for i in range(len(select)):
if i % 2 == 0:
evens.append(i)
else:
odds.append(i)
# print(select)
# print(odds)
# print(evens)
equalizer = min(len(odds), len(evens))
evens = evens[0:equalizer]
odds = odds[0:equalizer]
AS = select[odds]
BS = select[evens]
# print(AS)
# print(BS)
if DO_ALL:
NUMS = list(range(res_first))
else:
how_many = int(res_first/4)
NUMS = list(np.random.choice(list(range(res_first)), how_many))
for first in NUMS:
for idx in range(len(AS)):
weights = swap(weights, first, AS[idx], BS[idx])
set_tensor(net, tensor_name, weights)
return net
# reconnection of conv filters in existing net
def reconnect_simulate_random_weights(net, tensor_name="128x128/Conv0_up/weight", percent_change=10, DO_ALL=True):
weights = get_tensor(net, tensor_name)
res_first = weights.shape[2]
res = weights.shape[3]
possible = list(range(res))
to_select = int((res / 100.0) * percent_change)
print("randomizing", tensor_name, "weights.shape", weights.shape, " ... selected", to_select, "from", res)
select = np.random.choice(possible, to_select, replace=False)
odds = []
evens = []
for i in range(len(select)):
if i % 2 == 0:
evens.append(i)
else:
odds.append(i)
#print("select",select)
#print("odds",odds)
#print("evens",evens)
equalizer = min(len(odds), len(evens))
evens = evens[0:equalizer]
odds = odds[0:equalizer]
AS = select[odds]
BS = select[evens]
#print("AS",AS)
#print("BS",BS)
if DO_ALL:
NUMS = list(range(res_first))
else:
how_many = int(res_first/4)
NUMS = list(np.random.choice(list(range(res_first)), how_many))
for first in NUMS:
for idx in range(len(AS)):
weights = setrand(weights, first, AS[idx], BS[idx])
set_tensor(net, tensor_name, weights)
return net
def dgb_get_res(net, tensor_name):
weights = get_tensor(net, tensor_name)
res = weights.shape[3]
return res
# reconnection of conv filters in existing net
# this function always does the same order (and as such makes reproducible plots for different strengths etc.)
def reconnect_DIRECT_ORDER(net, FIXED_ORDER, tensor_name="128x128/Conv0_up/weight", DO_ALL=True):
weights = get_tensor(net, tensor_name)
res = weights.shape[2]
print("weights.shape", weights.shape, "reconnecting in total", len(FIXED_ORDER))
select = FIXED_ORDER
odds = []
evens = []
for i in range(len(select)):
if i % 2 == 0:
evens.append(i)
else:
odds.append(i)
equalizer = min(len(odds), len(evens))
evens = evens[0:equalizer]
odds = odds[0:equalizer]
AS = select[odds]
BS = select[evens]
if DO_ALL:
NUMS = list(range(res))
else:
assert False # further not deterministic fucn
for first in NUMS:
for idx in range(len(AS)):
weights = swap(weights, first, AS[idx], BS[idx])
set_tensor(net, tensor_name, weights)
return net
def get_tensor_OVERRIDE(net, target_tensor):
np_arr = net.get_var(target_tensor)
return np_arr
original_weights_reconnect_specific = {}
def get_tensor(net, target_tensor):
global original_weights_reconnect_specific
"""
# first restore net
for tensor_key in original_weights_reconnect_specific.keys():
#print("---reloading original values for", target_tensor, "from original_weights_reconnect_specific; keys:", original_weights_reconnect_specific.keys())
orig_val = original_weights_reconnect_specific[tensor_key]
net.set_var(tensor_key, orig_val )
"""
np_arr = net.get_var(target_tensor)
if target_tensor not in original_weights_reconnect_specific:
#print("---saving current version of ", target_tensor," into original_weights_reconnect_specific; keys:", original_weights_reconnect_specific.keys())
# first time getting it
original_weights_reconnect_specific[target_tensor] = np.copy( np_arr )
"""
else:
#print("---getting original version of the ", target_tensor)
I_WANT_TO_RELOAD = False
if I_WANT_TO_RELOAD:
np_arr = np.copy( original_weights_reconnect_specific[target_tensor] )
else:
np_arr = original_weights_reconnect_specific[target_tensor]
"""
return np_arr
def restore_net(net):
for tensor_key in original_weights_reconnect_specific.keys():
#print("---reloading original values for", target_tensor, "from original_weights_reconnect_specific; keys:", original_weights_reconnect_specific.keys())
orig_val = original_weights_reconnect_specific[tensor_key]
net.set_var(tensor_key, orig_val )
return net
def set_tensor(net, target_tensor, np_arr):
net.set_var(target_tensor, np_arr)
return net
# editednet = reconnect(changedGs, target_tensor, percent_change)