[go: up one dir, main page]

Menu

[2694c9]: / src / test_ssd.py  Maximize  Restore  History

Download this file

212 lines (158 with data), 7.9 kB

  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
#!/usr/bin/python
#
# This file is part of fluid3d - a software for image registration
#
# Copyright (c) Madrid 2013 Gert Wollny
#
# fluid3d is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with fluid3d; if not, see <http://www.gnu.org/licenses/>.
#
#
import pyopencl as cl
import pyopencl.array as cl_array
import numpy as np
from math import cos
from math import sin
from math import fabs
from math import sqrt
import unittest
from ssd import CostSSD
from cldevice import CLDevice
from OpenCLHelpers import get_cl_devices
class TestSSD(unittest.TestCase):
def setUp(self):
self.nx = 8
self.ny = 8
self.nz = 12
studyx = self.__fill_cos_row(self.nx, 0)
studyy = self.__fill_cos_row(self.ny, 1)
studyz = self.__fill_cos_row(self.nz, 2)
self.study = np.asarray(studyz * studyx * studyy, order='F')
self.reference = np.empty([self.nx,self.ny,self.nz], dtype=np.float32, order='F')
self.reference.fill(1.0)
self.imgformat = cl.ImageFormat(cl.channel_order.INTENSITY, cl.channel_type.FLOAT)
self.expect = np.sum(np.square(np.subtract(self.study, self.reference)))
self.test_g = np.multiply(np.subtract(self.study, self.reference), np.gradient( self.study))
def createImageInContext(self, ctx, img):
return cl.Image(ctx, cl.mem_flags.READ_ONLY |cl.mem_flags.COPY_HOST_PTR,
self.imgformat, None, None, img)
def createImageBufferInContext(self, ctx, img):
nelm = img.shape[0] * img.shape[1] * img.shape[2]
hbuf = np.reshape(img, (1,1, nelm))[0][0]
return cl.Buffer(ctx, cl.mem_flags.READ_ONLY|cl.mem_flags.COPY_HOST_PTR, 4 * nelm, hbuf)
def __run_test_on_device(self, d):
device = CLDevice(d)
study_dev = self.createImageInContext(device.context, self.study)
ref_dev = self.createImageInContext(device.context, self.reference)
ssd = CostSSD(device)
self.__checkClose(ssd.cost(study_dev, ref_dev, None), self.expect, "cost value")
nelm = self.study.shape[0] * self.study.shape[1] * self.study.shape[2]
grad_dev = device.get_rw_buffer(4 * 4 * nelm)
self.__checkClose(ssd.cost(study_dev, ref_dev, grad_dev), self.expect, "cost value")
grad = np.empty(4 * nelm, dtype=np.float32)
cl.enqueue_copy(device.queue, grad, grad_dev).wait()
for z in range(self.nz-1)[1:]:
zi = self.ny * z
for y in range(self.ny-1)[1:]:
yi = (zi + y) * self.nx
for i in range(3):
for x in range(self.nx -1)[1:]:
xi = (yi + x) * 4
self.__checkClose(grad[xi+i], self.test_g[i][x][y][z],
"{0},{1},{2},{3}".format(x,y,z,i), False)
def __run_test_on_device_with_buffers(self, d):
device = CLDevice(d)
study_dev = self.createImageBufferInContext(device.context, self.study)
ref_dev = self.createImageBufferInContext(device.context, self.reference)
ssd = CostSSD(device)
self.__checkClose(ssd.cost_from_buf(self.study.shape, study_dev, ref_dev, None), self.expect , "cost value")
nelm = self.study.shape[0] * self.study.shape[1] * self.study.shape[2]
grad_dev = device.get_rw_buffer(4 * 4 * nelm)
self.__checkClose(ssd.cost_from_buf(self.study.shape, study_dev, ref_dev, grad_dev), self.expect, "cost value" )
grad = np.empty(4 * nelm, dtype=np.float32)
cl.enqueue_copy(device.queue, grad, grad_dev).wait()
errors = [0,0,0]
for z in range(self.nz-1)[1:]:
zi = self.ny * z
for y in range(self.ny-1)[1:]:
yi = (zi + y) * self.nx
for x in range(self.nx -1)[1:]:
xi = (yi + x) * 4
for i in range(3):
self.__checkClose(grad[xi+i], self.test_g[i][x][y][z], "{0},{1},{2},{3}".format(x,y,z,i), False)
errors[i] = errors[i] + 1
l = grad[xi+3]
lt = sqrt(grad[xi] * grad[xi] + grad[xi+1] * grad[xi+1] + grad[xi+2] * grad[xi+2])
self.__checkClose(l, lt, "norm", False)
def __run_test_on_device_with_mixed(self, device):
device = CLDevice(d)
study_dev = self.createImageInContext(device.context, self.study)
ref_dev = self.createImageBufferInContext(device.context, self.reference)
ssd = CostSSD(device)
self.__checkClose(ssd.cost_from_mixed(study_dev, ref_dev, None), self.expect)
nelm = self.study.shape[0] * self.study.shape[1] * self.study.shape[2]
grad_dev = device.get_rw_buffer(4 * 4 * nelm)
self.__checkClose(ssd.cost_from_mixed(study_dev, ref_dev, grad_dev), self.expect)
grad = np.empty(4 * nelm, dtype=np.float32)
event = cl.enqueue_copy(device.queue, grad, grad_dev)
test_g = np.multiply(np.subtract(self.study, self.reference), np.gradient( self.study))
event.wait()
for z in range(self.nz-1)[1:]:
zi = self.ny * z
for y in range(self.ny-1)[1:]:
yi = (zi + y) * self.nx
for x in range(self.nx -1)[1:]:
xi = (yi + x) * 4
for i in range(3):
self.__checkClose(grad[xi+i], test_g[i][x][y][z])
l = grad[xi+3]
lt = sqrt(grad[xi] * grad[xi] + grad[xi+1] * grad[xi+1] + grad[xi+2] * grad[xi+2])
self.__checkClose(l, lt)
def __checkClose(self, a,b, text, throw=True):
if fabs(b) < 1e-4:
if fabs(a)> 1e-4:
print "FAIL: got ", a , " expect ", b , text
if throw:
self.assertLess(fabs(a), 1e-4)
else:
if fabs((a - b) / b) > 1e-4:
print "FAIL: got ", a , " expect ", b , " Q:", a /b, text
if throw:
self.assertLess(fabs((a - b) / b), 1e-4)
def testOnDevices(self):
(devchoices, devhelp, devices) = get_cl_devices()
self.__run_test_on_device(devices[0])
def ytestOnDevicesWithBuffers(self):
(devchoices, devhelp, devices) = get_cl_devices()
self.__run_test_on_device_with_buffers(devices[1])
def __fill_cos_row(self, length, idx):
shape = [1,1,1]
shape[idx] = length
row = np.empty([length], dtype=np.float32)
for i in range(length):
x = (3.1415926 * i) / length
row[i] = 4.0 * cos(x)
return np.reshape(row, shape)
def __grad(self, ix, iy, iz):
coord = [(3.1415926 * ix) / self.nx, (3.1415926 * iy) / self.ny, (3.1415926 * iz) / self.nz]
sx = -64.0 *sin(coord[0]) * 3.1415926 / self.nx
sy = -64.0 *sin(coord[1]) * 3.1415926 / self.ny
sz = -64.0 *sin(coord[2]) * 3.1415926 / self.nz
c = [ cos(x) for x in coord ]
result = [sx * c[1] * c[2], c[0] * sy * c[2], c[0] * c[1] * sz]
return result
def suite():
suite = unittest.makeSuite(TestSSD, 'test')
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')