forked from adambielski/GrouPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_splitgconv2d.py
335 lines (265 loc) · 10.6 KB
/
test_splitgconv2d.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
"""
File contains various test for evaluating the functionality of the given
splitgconv2d.py implementations.
"""
# import numpy as np
# ### ---[ Test pytorch implementation ]-----------
# import torch as pt
# from torch.nn.modules.utils import _pair
# from torch.autograd import Variable
# from groupy.gconv.make_gconv_indices import *
# from groupy.gconv.pytorch_gconv.splitgconv2d import gconv2d
# from groupy.gfunc import Z2FuncArray, P4FuncArray
# import groupy.garray.C4_array as c4a
# from PIL import Image
# def test_p4_net_equivariance():
# im = np.random.randn(1, 1, 11, 11)
# inds = make_c4_z2_indices(ksize=3)
# print("make c4_z2 indices: "+str(inds))
# inds_flat = flatten_indices(inds)
# print("inds_flat: "+str(inds_flat))
# check_equivariance(
# im=im,
# layers=[
# gconv2d(g_input='Z2', g_output='C4', in_channels=1, out_channels=1, kernel_size=3, padding=1)
# ],
# input_array=Z2FuncArray,
# output_array=P4FuncArray,
# point_group=c4a,
# )
# def check_equivariance(im, layers, input_array, output_array, point_group):
# # Transform the image
# print("Input: "+str(im), flush=True)
# f = input_array(im)
# print("Network output: "+str(f), flush=True)
# g = point_group.rand()
# gf = g*f # Default g*f
# im1 = gf.v
# # Apply layers to both images
# im = Variable(pt.Tensor(im))
# im1 = Variable(pt.Tensor(im1))
# fmap = im
# fmap1 = im1
# for layer in layers:
# fmap = layer(fmap)
# fmap1 = layer(fmap1)
# # Transform the computed feature maps
# fmap1_garray = output_array(fmap1.data.numpy())
# r_fmap1_data = (g.inv() * fmap1_garray).v
# fmap_data = fmap.data.numpy()
# assert np.allclose(fmap_data, r_fmap1_data, rtol=1e-5, atol=1e-3)
# def test_p4_net_pooling_equivariance():
# out_channels=1
# in_channels=1
# nti=1
# ksize=3
# kernel_size = _pair(ksize)
# print("kernel size:\n "+str(kernel_size))
# w = pt.nn.Parameter(pt.Tensor(out_channels, in_channels, nti, *kernel_size))
# print("w:\n "+str(w))
# inds = make_c4_z2_indices(ksize=ksize)
# print("make c4_z2 indices:\n "+str(inds))
# inds_flat = flatten_indices(inds)
# print("inds_flat:\n "+str(inds_flat))
# inds_reshape = inds.reshape((-1, inds.shape[-1])).astype(np.int32)
# print("inds_reshape:\n "+str(inds_reshape))
# print("inds_reshaped shape:\n "+str(inds_reshape.shape))
# w_indexed = w[:, :, inds_reshape[:, 0].tolist(), inds_reshape[:, 1].tolist(), inds_reshape[:, 2].tolist()]
# print("w_indexed:\n "+str(w_indexed))
# w_indexed = w_indexed.view(w_indexed.size()[0],
# w_indexed.size()[1],
# inds.shape[0],
# inds.shape[1],
# inds.shape[2],
# inds.shape[3])
# print("w_indexed:\n "+str(w_indexed))
# w_transformed = w_indexed.permute(0, 2, 1, 3, 4, 5)
# print("w_tranformed:\n "+str(w_transformed))
# im = pt.randn(in_channels,out_channels,ksize,ksize)
# imT = pt.rot90(im, dims=[2,3])
# layers=[
# gconv2d(g_input='Z2', g_output='C4', in_channels=in_channels, out_channels=out_channels, kernel_size=ksize, padding=1)
# ]
# print("Image:\n "+str(im))
# print("Image.T:\n "+str(imT))
# y = im
# for layer in layers:
# y = layer(y)
# print("y: "+str(y))
# y = pt.mean(y, dim=1)
# yT = imT
# for layer in layers:
# yT = layer(yT)
# print("yT: "+str(yT))
# yT = pt.mean(yT, dim=1)
# print("y_pooled:\n "+str(y))
# print("yT_pooled:\n "+str(yT))
# difference = pt.abs(pt.rot90(y, dims=[1,2])-yT)
# error = pt.sum(difference)
# print("Error:\n "+str(error))
# print("Difference:\n "+str(difference))
# ## ---[ Test tensforflow implementation ]-------
# import tensorflow as tf
# tf.compat.v1.disable_eager_execution()
# import tensorflow.keras.layers as KL
# from tensorflow.keras.models import Model
# from keras_gcnn.layers import GConv2D, GBatchNorm
# from groupy.gconv.tensorflow_gconv.splitgconv2d import gconv2d_util, gconv2d
# from groupy.gfunc.z2func_array import Z2FuncArray
# from groupy.gfunc.p4func_array import P4FuncArray
# from groupy.gfunc.p4mfunc_array import P4MFuncArray
# import groupy.garray.C4_array as C4a
# import groupy.garray.D4_array as D4a
# def check_c4_z2_conv_equivariance():
# out_channels=1
# in_channels=1
# nti=1
# ksize=3
# print("kernel size:\n "+str(ksize))
# im = np.random.randn(in_channels, ksize, ksize, out_channels)
# imT = np.rot90(im, axes=(1,2))
# print("Image: "+str(im))
# print("Image.T: "+str(imT))
# x, y = make_graph('Z2', 'C4', ksize)
# print("x: "+str(x))
# print("y: "+str(y))
# inds = make_c4_z2_indices(ksize=3)
# print("make c4_z2 indices:\n "+str(inds))
# inds_util, inds_shape_util, w_shape = gconv2d_util(h_input='Z2', h_output='C4', in_channels=in_channels, out_channels=out_channels, ksize=ksize)
# print("inds_util:\n "+str(inds_util))
# print("inds_shape_util:\n "+str(inds_shape_util))
# print("w_shape:\n "+str(w_shape))
# w = tf.Variable(tf.compat.v1.truncated_normal(w_shape, stddev=1.))
# # Compute
# input = tf.compat.v1.placeholder(dtype=tf.float32, shape=[out_channels, ksize, ksize, in_channels * nti])
# output = gconv2d(input=input, filter=w, strides=[1, 1, 1, 1], padding='SAME',
# gconv_indices=inds_util, gconv_shape_info=inds_shape_util)
# init = tf.compat.v1.global_variables_initializer()
# sess = tf.compat.v1.Session()
# sess.run(init)
# y = sess.run(output, feed_dict={input: im})
# yT = sess.run(output, feed_dict={input: imT})
# sess.close()
# print("y:\n "+str(y))
# print("yT:\n "+str(yT))
# difference = np.abs(y-yT)
# difference_rot = np.abs(np.rot90(y, axes=(1,2))-yT)
# print("Difference:\n "+str(difference))
# print("difference rot:\n"+str(difference_rot))
# y_pooled = KL.AveragePooling2D(y)
# check_equivariance(im, x, y, Z2FuncArray, P4FuncArray, C4a)
# def make_graph(h_input, h_output, ksize):
# gconv_indices, gconv_shape_info, w_shape = gconv2d_util(
# h_input=h_input, h_output=h_output, in_channels=1, out_channels=1, ksize=3)
# nti = gconv_shape_info[-2]
# x = tf.compat.v1.placeholder(dtype=tf.float32, shape=[None, ksize, ksize, 1 * nti])
# w = tf.Variable(tf.compat.v1.truncated_normal(shape=w_shape, stddev=1.))
# y = gconv2d(input=x, filter=w, strides=[1, 1, 1, 1], padding='SAME',
# gconv_indices=gconv_indices, gconv_shape_info=gconv_shape_info)
# return x, y
# def check_equivariance(im, input, output, input_array, output_array, point_group):
# # Transform the image
# f = input_array(im.transpose((0, 3, 1, 2)))
# g = point_group.rand()
# gf = g * f
# im1 = gf.v.transpose((0, 2, 3, 1))
# # Compute
# init = tf.global_variables_initializer()
# sess = tf.Session()
# sess.run(init)
# yx = sess.run(output, feed_dict={input: im})
# yrx = sess.run(output, feed_dict={input: im1})
# sess.close()
# # Transform the computed feature maps
# fmap1_garray = output_array(yrx.transpose((0, 3, 1, 2)))
# r_fmap1_data = (g.inv() * fmap1_garray).v.transpose((0, 2, 3, 1))
# print (np.abs(yx - r_fmap1_data).sum())
# assert np.allclose(yx, r_fmap1_data, rtol=1e-5, atol=1e-3)
## ---[ Test Keras implementation ]-------
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
import numpy as np
import tensorflow.keras
import tensorflow.python.keras.engine
import tensorflow.keras.layers as KL
from tensorflow.keras.models import Model
import groupy.garray.C4_array as C4a
import groupy.garray.D4_array as D4a
from keras_gcnn.layers import GConv2D
from groupy.gfunc.p4func_array import P4FuncArray
from groupy.gfunc.p4mfunc_array import P4MFuncArray
from groupy.gfunc.z2func_array import Z2FuncArray
def test_c4_z2_conv_equivariance():
out_channels=1
in_channels=1
nti=1
ksize=5
print("kernel size:\n "+str(ksize))
im = np.random.randn(in_channels, ksize, ksize, out_channels)
imT = tf.image.rot90(im, k=1)
print("Image: "+str(im))
print("Image.T: "+str(imT))
x, y = make_graph('Z2', 'C4')
print("x: "+str(x))
print("y: "+str(y))
equivariance_check(im, x, y, Z2FuncArray, P4FuncArray, C4a)
def make_graph(h_input, h_output):
l = GConv2D(1, 3, h_input, h_output)
input_dim = 1
if h_input == 'C4':
input_dim *= 4
elif h_input == 'D4':
input_dim *= 8
l.build([None, None, input_dim])
nti = l.gconv_shape_info[-2]
x = tf.compat.v1.placeholder(tf.float32, [None, 5, 5, 1 * nti])
y = l(x)
return x, y
def make_graph_transposed(in_shape, h_input, h_output):
l = GConv2D(1, 3, h_input, h_output, strides=(2,2), transpose=True)
input_dim = 1
if h_input == 'C4':
input_dim *= 4
elif h_input == 'D4':
input_dim *= 8
l.build([None, None, input_dim])
nti = l.gconv_shape_info[-2]
x = tf.compat.v1.placeholder(tf.float32, [None, 5, 5, 1 * nti])
y = l(x)
return x, y
def equivariance_check(im, input, output, input_array, output_array, point_group):
# Transform the image
im = im
print("Image:\n"+str(im))
imT = im.T
im_rot = np.rot90(im, k=1)
print("Image rot:\n"+str(im_rot))
# Compute
init = tf.compat.v1.global_variables_initializer()
sess = tf.compat.v1.Session()
sess.run(init)
y = sess.run(output, feed_dict={input: im})
y_rot = sess.run(output, feed_dict={input: im_rot})
yT = sess.run(output, feed_dict={input: imT})
sess.close()
differenceT = np.abs(y.T-yT)
difference_rot = np.abs(tf.image.rot90(y, k=1)-y_rot)
print("DifferenceT:\n "+str(differenceT))
print("difference rot:\n"+str(difference_rot))
f = input_array(im.transpose((0, 3, 1, 2)))
g = point_group.rand()
gf = g * f
im1 = gf.v.transpose((0, 2, 3, 1))
# Transform the computed feature maps
fmap1_garray = output_array(yT.transpose((0, 3, 1, 2)))
r_fmap1_data = (g.inv() * fmap1_garray).v.transpose((0, 2, 3, 1))
print(np.abs(y - r_fmap1_data).sum())
assert np.allclose(y, r_fmap1_data, rtol=1e-5, atol=1e-3)
### ---[ Main ]----------------------------------
if __name__=="__main__":
# print("\n"+"="*10+"[ PYTORCH ]"+"="*10+"\n")
# test_p4_net_pooling_equivariance()
# print("\n"+"="*10+"[ TENSORFLOW ]"+"="*10)
# check_c4_z2_conv_equivariance()
print("\n"+"="*10+"[ KERAS ]"+"="*10)
test_c4_z2_conv_equivariance()