forked from Samsung/veles.znicz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cutter.py
257 lines (211 loc) · 9.11 KB
/
cutter.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
# -*- coding: utf-8 -*-
"""
.. invisible:
_ _ _____ _ _____ _____
| | | | ___| | | ___/ ___|
| | | | |__ | | | |__ \ `--.
| | | | __|| | | __| `--. \
\ \_/ / |___| |___| |___/\__/ /
\___/\____/\_____|____/\____/
Created on Aug 4, 2014
Cutter unit.
███████████████████████████████████████████████████████████████████████████████
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
███████████████████████████████████████████████████████████████████████████████
"""
from __future__ import division
import numpy
from zope.interface import implementer
from veles.accelerated_units import IOpenCLUnit, ICUDAUnit, INumpyUnit
import veles.error as error
from veles.memory import reshape
import veles.znicz.nn_units as nn_units
from veles.units import Unit
class CutterBase(Unit):
hide_from_registry = True
def __init__(self, workflow, padding, *args, **kwargs):
super(CutterBase, self).__init__(workflow, *args, **kwargs)
self.padding = padding
self.demand("input")
@property
def padding(self):
return self._padding
@padding.setter
def padding(self, value):
if value is None:
raise ValueError("padding may not be None")
if type(value) not in (tuple, list):
raise TypeError("padding must either of type tuple or list")
if len(value) != 4:
raise ValueError(
"padding must be of length 4: (left, top, right, bottom)")
self._padding = value
def create_stuff(self, prefix):
setattr(self, "_%s_origin" % prefix, (
self.padding[0] * self.input.shape[3] * self.input.itemsize,
self.padding[1], 0))
self._region = (
self.output_shape[2] * self.output_shape[3] * self.input.itemsize,
self.output_shape[1], self.input.shape[0])
setattr(self, "_%s_row_pitch" % prefix, (
self.input.shape[2] * self.input.shape[3] * self.input.itemsize))
setattr(self, "_%s_slice_pitch" % prefix,
self.input.sample_size * self.input.itemsize)
setattr(self, "_%s_slice_height" % prefix,
self.input.shape[1])
@implementer(IOpenCLUnit, ICUDAUnit, INumpyUnit)
class Cutter(nn_units.Forward, CutterBase):
MAPPING = {"cutter"}
"""Cuts rectangular area from an input.
Must be assigned before initialize():
input
Updates after run():
output
Creates within initialize():
output
Attributes:
input: input as batch of samples.
output: output as batch of samples.
"""
def __init__(self, workflow, **kwargs):
super(Cutter, self).__init__(workflow, **kwargs)
self.exports.append("padding")
def initialize(self, device, **kwargs):
if not self.input or len(self.input.shape) != 4:
raise error.BadFormatError(
"input should be assigned and have shape of 4: "
"(n_samples, sy, sx, n_channels)")
if self.padding[0] < 0 or self.padding[1] < 0:
raise error.BadFormatError(
"padding[0], padding[1] should not be less than zero")
super(Cutter, self).initialize(device=device, **kwargs)
shape = list(self.input.shape)
shape[2] -= self.padding[0] + self.padding[2]
shape[1] -= self.padding[1] + self.padding[3]
if shape[2] <= 0 or shape[1] <= 0:
raise error.BadFormatError("Resulted output shape is empty")
self.output_shape = shape
if not self.output:
self.output.reset(numpy.zeros(self.output_shape, self.input.dtype))
else:
assert self.output.shape == self.output_shape
for vec in self.input, self.output:
vec.initialize(self.device)
self.create_stuff("src")
def ocl_init(self):
pass
def cuda_init(self):
pass
def ocl_run(self):
"""Forward propagation from batch on OpenCL.
"""
self.unmap_vectors(self.output, self.input)
self.device.queue_.copy_buffer_rect(
self.input.devmem, self.output.devmem,
self._src_origin, (0, 0, 0), self._region,
self._src_row_pitch, self._src_slice_pitch,
need_event=False)
def cuda_run(self):
"""Forward propagation from batch on CUDA.
"""
self.unmap_vectors(self.output, self.input)
self.input.devmem.memcpy_3d_async(
self._src_origin, (0, 0, 0), self._region,
self._src_row_pitch, self._src_slice_height,
dst=self.output.devmem)
def numpy_run(self):
"""Forward propagation from batch on CPU only.
"""
self.output.map_invalidate()
self.input.map_read()
out = reshape(self.output.mem, self.output_shape)
inp = self.input.mem
out[:, :, :, :] = inp[
:, self.padding[1]:self.padding[1] + self.output_shape[1],
self.padding[0]:self.padding[0] + self.output_shape[2], :]
@implementer(IOpenCLUnit, ICUDAUnit, INumpyUnit)
class GDCutter(nn_units.GradientDescentBase, CutterBase):
"""Gradient descent for Cutter.
"""
MAPPING = {"cutter"}
def initialize(self, device, **kwargs):
if not self.input or len(self.input.shape) != 4:
raise error.BadFormatError(
"input should be assigned and have shape of 4: "
"(n_samples, sy, sx, n_channels)")
if self.padding[0] < 0 or self.padding[1] < 0:
raise error.BadFormatError(
"padding[0], padding[1] should not be less than zero")
if not self.err_output:
raise error.BadFormatError("err_output should be assigned")
super(GDCutter, self).initialize(device=device, **kwargs)
sh = list(self.input.shape)
sh[2] -= self.padding[0] + self.padding[2]
sh[1] -= self.padding[1] + self.padding[3]
if sh[2] <= 0 or sh[1] <= 0:
raise error.BadFormatError("Resulted output shape is empty")
output_size = int(numpy.prod(sh))
if self.err_output.size != output_size:
raise error.BadFormatError(
"Computed err_output size differs from an assigned one")
self.output_shape = sh
if not self.err_input:
self.err_input.reset(numpy.zeros_like(self.input.mem))
else:
assert self.err_input.shape == self.input.shape
self.init_vectors(self.err_output, self.err_input)
self.create_stuff("dst")
def ocl_init(self):
self.sources_["cutter"] = {}
self.build_program(
{}, "%s_%s_%s" %
(self.__class__.__name__,
"x".join(str(x) for x in self.err_input.shape),
"x".join(str(x) for x in self.padding)),
dtype=self.err_input.dtype)
self.assign_kernel("clear_err_input")
self.set_args(self.err_input)
def cuda_init(self):
pass
def ocl_run(self):
"""Backward propagation from batch on OpenCL.
"""
self.unmap_vectors(self.err_output, self.err_input)
self.execute_kernel([self.err_input.size], None)
self.device.queue_.copy_buffer_rect(
self.err_output.devmem, self.err_input.devmem,
(0, 0, 0), self._dst_origin, self._region,
0, 0, self._dst_row_pitch, self._dst_slice_pitch,
need_event=False)
def cuda_run(self):
"""Backward propagation from batch on OpenCL.
"""
self.unmap_vectors(self.err_output, self.err_input)
self.err_input.devmem.memset32_async()
self.err_output.devmem.memcpy_3d_async(
(0, 0, 0), self._dst_origin, self._region,
0, 0, self._dst_row_pitch, self._dst_slice_height,
dst=self.err_input.devmem)
def numpy_run(self):
"""Forward propagation from batch on CPU only.
"""
self.err_input.map_invalidate()
self.err_output.map_read()
out = reshape(self.err_output.mem, self.output_shape)
inp = self.err_input.mem
inp[:] = 0
inp[:, self.padding[1]:self.padding[1] + self.output_shape[1],
self.padding[0]:self.padding[0] + self.output_shape[2], :] = out