forked from yhenon/keras-spp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_roi_pooling.py
78 lines (58 loc) · 2.97 KB
/
test_roi_pooling.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
import numpy as np
from keras.layers import Input
from keras.models import Model
from RoiPooling import RoiPooling
import keras.backend as K
dim_ordering = K.image_dim_ordering()
assert dim_ordering in {'tf', 'th'}, 'dim_ordering must be in {tf, th}'
pooling_regions = [1, 2, 4]
num_rois = 2
num_channels = 3
if dim_ordering == 'tf':
in_img = Input(shape=(None, None, num_channels))
elif dim_ordering == 'th':
in_img = Input(shape=(num_channels, None, None))
in_roi = Input(shape=(num_rois, 4))
out_roi_pool = RoiPooling(pooling_regions, num_rois)([in_img, in_roi])
model = Model([in_img, in_roi], out_roi_pool)
model.summary()
model.compile(loss='mse', optimizer='sgd')
for img_size in [8, 16, 32]:
if dim_ordering == 'th':
X_img = np.random.rand(1, num_channels, img_size, img_size)
row_length = [float(X_img.shape[2]) / i for i in pooling_regions]
col_length = [float(X_img.shape[3]) / i for i in pooling_regions]
elif dim_ordering == 'tf':
X_img = np.random.rand(1, img_size, img_size, num_channels)
row_length = [float(X_img.shape[1]) / i for i in pooling_regions]
col_length = [float(X_img.shape[2]) / i for i in pooling_regions]
X_roi = np.array([[0, 0, img_size / 1, img_size / 1],
[0, 0, img_size / 2, img_size / 2]])
X_roi = np.reshape(X_roi, (1, num_rois, 4))
Y = model.predict([X_img, X_roi])
for roi in range(num_rois):
if dim_ordering == 'th':
X_curr = X_img[0, :, X_roi[0, roi, 0]:X_roi[0, roi, 2], X_roi[0, roi, 1]:X_roi[0, roi, 3]]
row_length = [float(X_curr.shape[1]) / i for i in pooling_regions]
col_length = [float(X_curr.shape[2]) / i for i in pooling_regions]
elif dim_ordering == 'tf':
X_curr = X_img[0, X_roi[0, roi, 0]:X_roi[0, roi, 2], X_roi[0, roi, 1]:X_roi[0, roi, 3], :]
row_length = [float(X_curr.shape[0]) / i for i in pooling_regions]
col_length = [float(X_curr.shape[1]) / i for i in pooling_regions]
idx = 0
for pool_num, num_pool_regions in enumerate(pooling_regions):
for ix in range(num_pool_regions):
for jy in range(num_pool_regions):
for cn in range(num_channels):
x1 = int(round(ix * col_length[pool_num]))
x2 = int(round(ix * col_length[pool_num] + col_length[pool_num]))
y1 = int(round(jy * row_length[pool_num]))
y2 = int(round(jy * row_length[pool_num] + row_length[pool_num]))
if dim_ordering == 'th':
m_val = np.max(X_curr[cn, y1:y2, x1:x2])
elif dim_ordering == 'tf':
m_val = np.max(X_curr[y1:y2, x1:x2, cn])
np.testing.assert_almost_equal(
m_val, Y[0, roi, idx], decimal=6)
idx += 1
print('Passed roi pooling test')