def backward(self, bottom_data, bottom_diff, top_data, top_diff): padded_ratio = Array.zeros(1, bottom_data.shape[1] + self.size - 1, bottom_data.shape[2], bottom_data.shape[3]) accum_ratio = Array.zeros(1, 1, bottom_data.shape[2], bottom_data.shape[3]) accum_ratio_times_bottom = Array.zeros(1, 1, bottom_data.shape[2], bottom_data.shape[3]) cache_ratio_value = 2.0 * self.apha * self.beta / self.size bottom_diff = np.pow(self.scale, -self.beta) bottom_diff *= top_diff inverse_pre_pad = self.size - (self.size + 1) / 2 for n in range(bottom_data.shape[0]): padded_ratio[0, inverse_pre_pad] = top_diff[n] * top_data[n] padded_ratio[0, inverse_pre_pad] /= self.scale[n] accum_ratio.fill(0) for c in range(self.size - 1): accum_ratio += padded_ratio[0, c] for c in range(bottom_data.shape[1]): accum_ratio += padded_ratio[0, c + self.size - 1] accum_ratio_times_bottom += bottom_data[n, c] * accum_ratio bottom_data[n, c] += -cache_ratio_value * \ accum_ratio_times_bottom accum_ratio += -1 * padded_ratio[0, c]
def test_simple(self): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) for n in range(5): for c in range(channels): bottom[n, c] = Array.array([[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_size = 2 param.pooling_param.stride = 1 layer = PoolingLayer(param) actual_shape = layer.get_top_shape(bottom) actual = Array.zeros(actual_shape, np.int32) layer.setup(bottom, actual) layer.forward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( actual[n, c], np.array([[9, 5, 5, 8], [9, 5, 5, 8]]).astype(np.int32)) bottom = Array.zeros_like(bottom) for n in range(5): for c in range(channels): actual[n, c] = Array.array([[1, 1, 1, 1], [1, 1, 1, 1]]).astype(np.int32) layer.backward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( bottom[n, c], np.array([[0, 0, 2, 0, 0], [2, 0, 0, 0, 2], [0, 0, 2, 0, 0]]).astype(np.int32))
def test_gradient(self): for kernel_h in range(3, 5): for kernel_w in range(3, 5): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) bottom_diff = Array.zeros_like(bottom) for n in range(5): for c in range(channels): bottom[n, c] = Array.array([[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_h = kernel_h param.pooling_param.kernel_w = kernel_w param.pooling_param.stride = 2 param.pooling_param.pad = 1 layer = PoolingLayer(param) top_shape = layer.get_top_shape(bottom) top = Array.zeros(top_shape, np.int32) top_diff = Array.zeros_like(top) checker = GradientChecker(1e-4, 1e-2) checker.check_gradient_exhaustive(layer, bottom, bottom_diff, top, top_diff)
def test_gradient(self): for kernel_h in range(3, 5): for kernel_w in range(3, 5): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) bottom_diff = Array.zeros_like(bottom) for n in range(5): for c in range(channels): bottom[n, c] = Array.array( [[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_h = kernel_h param.pooling_param.kernel_w = kernel_w param.pooling_param.stride = 2 param.pooling_param.pad = 1 layer = PoolingLayer(param) top_shape = layer.get_top_shape(bottom) top = Array.zeros(top_shape, np.int32) top_diff = Array.zeros_like(top) checker = GradientChecker(1e-4, 1e-2) checker.check_gradient_exhaustive(layer, bottom, bottom_diff, top, top_diff)
def _forward_test(self, param, in_shape): conv_param = param.convolution_param in_batch = Array.rand(*in_shape).astype(np.float32) * 255 conv = ConvLayer(param) top_shape = conv.get_top_shape(in_batch) expected_conv = NaiveConv(conv_param) actual = Array.zeros(top_shape, np.float32) expected = Array.zeros(top_shape, np.float32) conv.setup(in_batch, actual) conv.forward(in_batch, actual) expected_conv(in_batch, conv.weights, conv.bias, expected) self._check(actual, expected)
def _forward_test(self, param, in_shape): conv_param = param.convolution_param in_batch = Array.rand(*in_shape).astype(np.float32) * 255 conv = ConvLayer(param) top_shape = conv.get_top_shape(in_batch) expected_conv = NaiveConv(conv_param) actual = Array.zeros(top_shape, np.float32) expected = Array.zeros(top_shape, np.float32) conv.setup(in_batch, actual) conv.forward(in_batch, actual) expected_conv(in_batch, conv.weights, conv.bias, expected) self._check(actual, expected)
def test_simple(self): a = Array.rand(256, 256).astype(np.float32) * 255 actual_mask = Array.zeros((254, 254), np.float32) actual = Array.zeros((254, 254), np.float32) actual.fill(float('-inf')) expected_mask = Array.zeros((254, 254), np.float32) expected = Array.zeros((254, 254), np.float32) expected.fill(float('-inf')) max_pool(a, actual, actual_mask, (2, 2)) py_max_pool(a, expected, expected_mask, (2, 2), (1, 1), (0, 0)) self._check(actual, expected) self._check(actual_mask, expected_mask)
def test_simple(self): a = Array.rand(256, 256).astype(np.float32) * 255 actual_mask = Array.zeros((254, 254), np.float32) actual = Array.zeros((254, 254), np.float32) actual.fill(float('-inf')) expected_mask = Array.zeros((254, 254), np.float32) expected = Array.zeros((254, 254), np.float32) expected.fill(float('-inf')) max_pool(a, actual, actual_mask, (2, 2)) py_max_pool(a, expected, expected_mask, (2, 2), (1, 1), (0, 0)) self._check(actual, expected) self._check(actual_mask, expected_mask)
def test_nonzero_slope(self): bottom = Array.rand(256, 256).astype(np.float32) * 255 actual = Array.zeros(bottom.shape, np.float32) relu(bottom, bottom, actual, 2.4) expected = np.clip(bottom, 0.0, float('inf')) + \ 2.4 * np.clip(bottom, float('-inf'), 0.0) self._check(actual, expected)
def forward(self, bottom, top): # initialize scale to constant value self.scale.fill(self.k) padded_square = Array.zeros((bottom.shape[1] + self.size - 1, bottom.shape[2], bottom.shape[3]), bottom.dtype) alpha_over_size = self.alpha / self.size for n in range(bottom.shape[0]): padded_square[self.pre_pad:bottom.shape[1] + self.pre_pad] = \ np.square(bottom[n]) for c in range(self.size): self.scale[n] += alpha_over_size * padded_square[c] # for c in range(1, bottom.shape[1]): # self.scale[n, c] = self.scale[n, c - 1] + \ # alpha_over_size * padded_square[c + self.size - 1] - \ # alpha_over_size * padded_square[c - 1] print('asdfasdf', c) self.scale[n, 1:] = self.scale[n, :-1] + \ alpha_over_size * padded_square[self.size:self.size + bottom.shape[1] - 1] - \ alpha_over_size * padded_square[:bottom.shape[1] - 1] top[:] = np.power(self.scale, -self.beta) * bottom
def test_nonzero_slope(self): bottom = Array.rand(256, 256).astype(np.float32) * 255 actual = Array.zeros(bottom.shape, np.float32) relu(bottom, bottom, actual, 2.4) expected = np.clip(bottom, 0.0, float('inf')) + \ 2.4 * np.clip(bottom, float('-inf'), 0.0) self._check(actual, expected)
def test_no_padding(self): a = (Array.rand(256, 256) * 255).astype(np.float32) weights = (Array.rand(5, 5) * 2).astype(np.float32) actual = Array.zeros((254, 254), np.float32) convolve(a, weights, actual, (0, 0), (1, 1)) expected = signal.convolve(a, np.fliplr(np.flipud(weights)), mode='same')[2:, 2:] self._check(actual, expected)
def test_no_padding(self): a = (Array.rand(256, 256) * 255).astype(np.float32) weights = (Array.rand(5, 5) * 2).astype(np.float32) actual = Array.zeros((254, 254), np.float32) convolve(a, weights, actual, (0, 0), (1, 1)) expected = signal.convolve(a, np.fliplr(np.flipud(weights)), mode='same')[2:, 2:] self._check(actual, expected)
def test_simple(self): a = (Array.rand(256, 256) * 255).astype(np.float32) weights = (Array.rand(3, 3) * 2).astype(np.float32) actual = Array.zeros(a.shape, np.float32) convolve(a, weights, actual, (1, 1), (1, 1)) expected = signal.convolve(a, np.fliplr(np.flipud(weights)), mode='same') self._check(actual, expected)
def test_simple(self): a = (Array.rand(256, 256) * 255).astype(np.float32) weights = (Array.rand(3, 3) * 2).astype(np.float32) actual = Array.zeros(a.shape, np.float32) convolve(a, weights, actual, (1, 1), (1, 1)) expected = signal.convolve(a, np.fliplr(np.flipud(weights)), mode='same') self._check(actual, expected)
def test_simple(self): channels = 12 height = 3 width = 5 bottom = Array.zeros((5, channels, height, width), np.int32) for n in range(5): for c in range(channels): bottom[n, c] = Array.array( [[1, 2, 5, 2, 3], [9, 4, 1, 4, 8], [1, 2, 5, 2, 3]]).astype(np.int32) param = self.layer[5] param.pooling_param.kernel_size = 2 param.pooling_param.stride = 1 layer = PoolingLayer(param) actual_shape = layer.get_top_shape(bottom) actual = Array.zeros(actual_shape, np.int32) layer.setup(bottom, actual) layer.forward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( actual[n, c], np.array([ [9, 5, 5, 8], [9, 5, 5, 8] ]).astype(np.int32)) bottom = Array.zeros_like(bottom) for n in range(5): for c in range(channels): actual[n, c] = Array.array( [[1, 1, 1, 1], [1, 1, 1, 1]]).astype(np.int32) layer.backward(bottom, actual) for n in range(5): for c in range(channels): np.testing.assert_array_equal( bottom[n, c], np.array([[0, 0, 2, 0, 0], [2, 0, 0, 0, 2], [0, 0, 2, 0, 0]]).astype(np.int32))
def test_forward_simple(self): channels = 12 height = 3 width = 5 bottom = Array.rand( 5, channels, height, width).astype(np.float32) bottom = bottom * 256 - 128 layer = ReluLayer(self.layer[3]) actual = Array.zeros(layer.get_top_shape(bottom), np.float32) layer.forward(bottom, actual) expected = np.clip(bottom, 0.0, float('inf')).astype(np.float32) np.testing.assert_allclose(actual, expected)
def __init__(self, layer_param): super(InnerProductLayer, self).__init__(layer_param) param = self.layer_param.inner_product_param self.num_output = param.num_output self.bias_term = param.bias_term if self.bias_term: self.bias = Array.zeros(self.num_output) filler = param.bias_filler if filler.type == 'constant': self.bias.fill(filler.value) else: raise Exception("Filler not implemented for bias filler \ type {}".format(filler.type))
def test_backward_simple(self): channels = 12 height = 3 width = 5 bottom = Array.rand( 5, channels, height, width).astype(np.float32) bottom = bottom * 256 - 128 top_diff = Array.rand( 5, channels, height, width).astype(np.float32) top_diff = top_diff * 256 - 128 top = np.zeros(top_diff.shape, np.float32) actual = Array.zeros(bottom.shape, np.float32) layer = ReluLayer(self.layer[3]) layer.backward(bottom, actual, top, top_diff) expected = np.multiply(top_diff, np.greater(bottom, Array.zeros(bottom.shape, np.float32))) np.testing.assert_allclose(actual, expected)
def __call__(self, *args): output = Array.zeros(self.out_shape, args[0].dtype) self._c_function(args[0], output) return output
def add_blob(self, blob, shape): self.blobs[blob] = Array.zeros(shape, np.float32)
def test_simple(self): bottom = Array.rand(256, 256).astype(np.float32) * 255 actual = Array.zeros(bottom.shape, np.float32) relu(bottom, bottom, actual, 0.0) expected = np.clip(bottom, 0.0, float('inf')) self._check(actual, expected)
def test_simple(self): bottom = Array.rand(256, 256).astype(np.float32) * 255 actual = Array.zeros(bottom.shape, np.float32) relu(bottom, bottom, actual, 0.0) expected = np.clip(bottom, 0.0, float('inf')) self._check(actual, expected)