def test_stride(): arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) arr_fb = np.ones((4, 3, 3, 5)).astype(DTYPE) arr_out = fbcorr3(arr_in, arr_fb, stride=2) assert arr_out.shape == (1, 1, 5)
def test_stride(): arr_in = np.arange(4*4*3).reshape((4, 4, 3)).astype(DTYPE) arr_fb = np.ones((4, 3, 3, 5)).astype(DTYPE) arr_out = fbcorr3(arr_in, arr_fb, stride=2) assert arr_out.shape == (1, 1, 5)
def test_constant_arr_in_arange_fb(): a = 0.987 inh, inw, ind = 6, 7, 4 fbh, fbw, fbd, fn = 2, 3, 4, 4 arr_in = a * np.ones(inh*inw*ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.arange(fbh*fbw*fbd*fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn alpha = a * fbh * fbw * ind beta = 0.5 * (fn * (ind - 1) + fn * ind * (fbw - 1) + fn * ind * fbw * (fbh - 1)) ref = np.zeros((outh, outw, outd), dtype=DTYPE) for k in xrange(outd): ref[:, :, k] = alpha * (k + beta) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_arange_arr_in_and_fb(): inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 1, 5, 3, 8 arr_in = np.arange(inh*inw*ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.arange(fbh*fbw*fbd*fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn alpha = 0.5 * fbh * fbw * ind ref = np.zeros((outh, outw, outd), dtype=DTYPE) for i in xrange(outh): for j in xrange(outw): for k in xrange(outd): ref[i, j, k] = alpha * ( 1. / 3. * (3 * i + 2 * fbh - 1) * (inw * ind ** 2 * fn * fbw * (fbh - 1)) \ + ind * inw * (2 * i + fbh - 1) * (0.5 * ind * fn * (fbw - 1) + \ 0.5 * fn * (ind - 1) + k) \ + 1. / 3. * (3 * j + 2 * fbw - 1) * (ind ** 2 * fn * (fbw - 1)) \ + ind * (2 * j + fbw - 1) * (0.5 * ind * fn * fbw * (fbh - 1) + 0.5 * fn * (ind - 1) + k) \ + fn * (ind - 1) * (0.5 * fbw * (fbh - 1) + 0.5 * ind * (fbw - 1) \ + 1. / 3. * (2 * ind - 1)) + (ind - 1) * k) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_arange_arr_in_and_fb(): inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 1, 5, 3, 8 arr_in = np.arange(inh * inw * ind).reshape((inh, inw, ind)).astype(DTYPE) arr_fb = np.arange(fbh * fbw * fbd * fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn alpha = 0.5 * fbh * fbw * ind ref = np.zeros((outh, outw, outd), dtype=DTYPE) for i in xrange(outh): for j in xrange(outw): for k in xrange(outd): ref[i, j, k] = alpha * ( 1. / 3. * (3 * i + 2 * fbh - 1) * (inw * ind ** 2 * fn * fbw * (fbh - 1)) \ + ind * inw * (2 * i + fbh - 1) * (0.5 * ind * fn * (fbw - 1) + \ 0.5 * fn * (ind - 1) + k) \ + 1. / 3. * (3 * j + 2 * fbw - 1) * (ind ** 2 * fn * (fbw - 1)) \ + ind * (2 * j + fbw - 1) * (0.5 * ind * fn * fbw * (fbh - 1) + 0.5 * fn * (ind - 1) + k) \ + fn * (ind - 1) * (0.5 * fbw * (fbh - 1) + 0.5 * ind * (fbw - 1) \ + 1. / 3. * (2 * ind - 1)) + (ind - 1) * k) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_max_filter_size(): a = 1.23 arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) arr_fb = a * np.ones((4, 3, 3, 5)).astype(DTYPE) arr_out = fbcorr3(arr_in, arr_fb) assert arr_out.shape == (1, 2, 5)
def test_input_d_1(): arr_in = np.zeros((2, 3, 1), dtype=DTYPE) arr_fb = np.zeros((1, 1, 1, 4), dtype=DTYPE) arr_in[:] = np.arange(np.prod(arr_in.shape)).reshape(arr_in.shape) arr_fb[:] = np.arange(np.prod(arr_fb.shape)).reshape(arr_fb.shape) gt = np.array([[[0., 0., 0., 0.], [0., 1., 2., 3.], [0., 2., 4., 6.]], [[0., 3., 6., 9.], [0., 4., 8., 12.], [0., 5., 10., 15.]]], dtype=DTYPE) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL)
def test_max_filter_size(): a = 1.23 arr_in = np.arange(4*4*3).reshape((4, 4, 3)).astype(DTYPE) arr_fb = a * np.ones((4, 3, 3, 5)).astype(DTYPE) arr_out = fbcorr3(arr_in, arr_fb) assert arr_out.shape == (1, 2, 5)
def test_one_dot(): arr_in = np.zeros((3, 3, 4), dtype=DTYPE) arr_fb = np.zeros((3, 3, 4, 8), dtype=DTYPE) arr_in[:] = np.arange(np.prod(arr_in.shape)).reshape(arr_in.shape) arr_fb[:] = np.arange(np.prod(arr_fb.shape)).reshape(arr_fb.shape) gt = np.array([[[ 119280., 119910., 120540., 121170., 121800., 122430., 123060., 123690.]]], dtype=DTYPE) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL)
def test_one_dot(): arr_in = np.zeros((3, 3, 4), dtype=DTYPE) arr_fb = np.zeros((3, 3, 4, 8), dtype=DTYPE) arr_in[:] = np.arange(np.prod(arr_in.shape)).reshape(arr_in.shape) arr_fb[:] = np.arange(np.prod(arr_fb.shape)).reshape(arr_fb.shape) gt = np.array([[[ 119280., 119910., 120540., 121170., 121800., 122430., 123060., 123690. ]]], dtype=DTYPE) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL)
def test_input_d_1(): arr_in = np.zeros((2, 3, 1), dtype=DTYPE) arr_fb = np.zeros((1, 1, 1, 4), dtype=DTYPE) arr_in[:] = np.arange(np.prod(arr_in.shape)).reshape(arr_in.shape) arr_fb[:] = np.arange(np.prod(arr_fb.shape)).reshape(arr_fb.shape) gt = np.array([[[ 0., 0., 0., 0.], [ 0., 1., 2., 3.], [ 0., 2., 4., 6.]], [[ 0., 3., 6., 9.], [ 0., 4., 8., 12.], [ 0., 5., 10., 15.]]], dtype=DTYPE) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL) arr_out = fbcorr3(arr_in, arr_fb) assert_allclose(arr_out, gt, rtol=RTOL, atol=ATOL)
def test_smoke_stride(): np.random.seed(42) arr_in = np.random.randn(4, 4, 3).astype(DTYPE) arr_fb = np.ones((4, 3, 3, 5)).astype(DTYPE) arr_out = fbcorr3(arr_in, arr_fb) idx = (0, 1, 3) ref = -2.80072999 res = arr_out[idx] assert (ref - res) < ATOL
def test_arr_out_3d(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) arr_fb = np.zeros((1, 1, 4, 8), dtype=DTYPE) arr_out = np.zeros((20, 30, 8), dtype=DTYPE) arr_in[:] = np.arange(np.prod(arr_in.shape)).reshape(arr_in.shape) arr_fb[:] = np.arange(np.prod(arr_fb.shape)).reshape(arr_fb.shape) idx = [2, 3], [10, 12] gt = np.zeros((len(idx), arr_out.shape[-1]), dtype=DTYPE) for i in xrange(gt.shape[0]): for k in xrange(gt.shape[1]): gt[i, k] = get_fbcorr_element(arr_in, arr_fb, idx[0][i], idx[1][i], k) arr_out = fbcorr3(arr_in, arr_fb) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
def test_random_arr_in_and_arr_fb(): inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 3, 5, 3, 8 arr_in = np.random.randn(inh * inw * ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.random.randn(fbh * fbw * fbd * fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn ref = np.zeros((outh, outw, outd), dtype=DTYPE) for i in xrange(outh): for j in xrange(outw): for k in xrange(outd): ref[i, j, k] = get_fbcorr_element(arr_in, arr_fb, i, j, k) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_constant_arr_in_random_arr_fb(): a = 4.827 inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 3, 5, 3, 8 arr_in = a * np.ones(inh * inw * ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.random.randn(fbh * fbw * fbd * fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn ref = np.zeros((outh, outw, outd), dtype=DTYPE) for k in xrange(arr_fb.shape[-1]): constant = a * arr_fb[..., k].sum() ref[..., k] = constant res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_constant_arr_in_random_arr_fb(): a = 4.827 inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 3, 5, 3, 8 arr_in = a * np.ones(inh*inw*ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.random.randn(fbh*fbw*fbd*fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn ref = np.zeros((outh, outw, outd), dtype=DTYPE) for k in xrange(arr_fb.shape[-1]): constant = a * arr_fb[..., k].sum() ref[..., k] = constant res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_random_arr_in_and_arr_fb(): inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 3, 5, 3, 8 arr_in = np.random.randn(inh*inw*ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.random.randn(fbh*fbw*fbd*fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn ref = np.zeros((outh, outw, outd), dtype=DTYPE) for i in xrange(outh): for j in xrange(outw): for k in xrange(outd): ref[i, j, k] = get_fbcorr_element(arr_in, arr_fb, i, j, k) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_constant_arr_in_and_fb(): a = 1.234 b = 2.345 inh, inw, ind = 4, 5, 6 fbh, fbw, fbd, fn = 2, 2, 6, 4 arr_in = a * np.ones(inh * inw * ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = b * np.ones(fbh * fbw * fbd * fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn constant = a * b * fbh * fbw * fbd ref = constant * np.ones(outh * outw * outd).reshape( (outh, outw, outd)).astype(DTYPE) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_constant_arr_in_and_fb(): a = 1.234 b = 2.345 inh, inw, ind = 4, 5, 6 fbh, fbw, fbd, fn = 2, 2, 6, 4 arr_in = a * np.ones(inh*inw*ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = b * np.ones(fbh*fbw*fbd*fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn constant = a * b * fbh * fbw * fbd ref = constant * np.ones(outh*outw*outd).reshape( (outh, outw, outd)).astype(DTYPE) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_arange_arr_in_constant_fb(): b = 2.345 inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 1, 5, 3, 8 arr_in = np.arange(inh * inw * ind).reshape((inh, inw, ind)).astype(DTYPE) arr_fb = b * np.ones(fbh * fbw * fbd * fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn alpha = 0.5 * b * fbh * fbw * ind ref = np.zeros((outh, outw, outd), dtype=DTYPE) for i in xrange(outh): for j in xrange(outw): ref[i, j, :] = alpha * (ind - 1 + ind * (2 * j + fbw - 1) + ind * inw * (2 * i + fbh - 1)) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_arange_arr_in_constant_fb(): b = 2.345 inh, inw, ind = 8, 7, 3 fbh, fbw, fbd, fn = 1, 5, 3, 8 arr_in = np.arange(inh*inw*ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = b * np.ones(fbh*fbw*fbd*fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn alpha = 0.5 * b * fbh * fbw * ind ref = np.zeros((outh, outw, outd), dtype=DTYPE) for i in xrange(outh): for j in xrange(outw): ref[i, j, :] = alpha * (ind - 1 + ind * (2 * j + fbw - 1) + ind * inw * (2 * i + fbh - 1)) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def test_constant_arr_in_arange_fb(): a = 0.987 inh, inw, ind = 6, 7, 4 fbh, fbw, fbd, fn = 2, 3, 4, 4 arr_in = a * np.ones(inh * inw * ind).reshape( (inh, inw, ind)).astype(DTYPE) arr_fb = np.arange(fbh * fbw * fbd * fn).reshape( (fbh, fbw, fbd, fn)).astype(DTYPE) outh, outw, outd = (inh - fbh + 1), (inw - fbw + 1), fn alpha = a * fbh * fbw * ind beta = 0.5 * (fn * (ind - 1) + fn * ind * (fbw - 1) + fn * ind * fbw * (fbh - 1)) ref = np.zeros((outh, outw, outd), dtype=DTYPE) for k in xrange(outd): ref[:, :, k] = alpha * (k + beta) res = fbcorr3(arr_in, arr_fb) assert_allclose(res, ref, rtol=RTOL, atol=ATOL)
def _get_feature_map(self, tmp_in, layer_idx, op_idx, kwargs, op_params, op_name): if op_name == 'lnorm': inker_shape = kwargs['inker_shape'] outker_shape = kwargs['outker_shape'] remove_mean = kwargs['remove_mean'] stretch = kwargs['stretch'] threshold = kwargs['threshold'] # SLM PLoS09 / FG11 constraints: assert inker_shape == outker_shape tmp_out = lcdnorm3(tmp_in, inker_shape, contrast=remove_mean, stretch=stretch, threshold=threshold) elif op_name == 'fbcorr': max_out = kwargs['max_out'] min_out = kwargs['min_out'] fbkey = layer_idx, op_idx if fbkey not in self.filterbanks: initialize = op_params['initialize'] if isinstance(initialize, np.ndarray): fb = initialize if len(fb.shape) == 3: fb = fb[..., np.newaxis] else: filter_shape = list(initialize['filter_shape']) generate = initialize['generate'] n_filters = initialize['n_filters'] fb_shape = [n_filters] + filter_shape + [tmp_in.shape[-1]] # generate filterbank data method_name, method_kwargs = generate assert method_name == 'random:uniform' rseed = method_kwargs.get('rseed', None) rng = np.random.RandomState(rseed) fb = rng.uniform(size=fb_shape) for fidx in xrange(n_filters): filt = fb[fidx] # zero-mean, unit-l2norm filt -= filt.mean() filt_norm = np.linalg.norm(filt) assert filt_norm != 0 filt /= filt_norm fb[fidx] = filt fb = np.ascontiguousarray(np.rollaxis(fb, 0, 4)).astype(DTYPE) self.filterbanks[fbkey] = fb print fb.shape fb = self.filterbanks[fbkey] # -- filter assert tmp_in.dtype == np.float32 tmp_out = fbcorr3(tmp_in, fb) # -- activation min_out = -np.inf if min_out is None else min_out max_out = +np.inf if max_out is None else max_out # insure that the type is right before calling numexpr min_out = np.array([min_out], dtype=tmp_in.dtype) max_out = np.array([max_out], dtype=tmp_in.dtype) # call numexpr tmp_out = ne.evaluate('where(tmp_out < min_out, min_out, tmp_out)') tmp_out = ne.evaluate('where(tmp_out > max_out, max_out, tmp_out)') assert tmp_out.dtype == tmp_in.dtype elif op_name == 'lpool': ker_shape = kwargs['ker_shape'] order = kwargs['order'] stride = kwargs['stride'] tmp_out = lpool3(tmp_in, ker_shape, order=order, stride=stride) else: raise ValueError("operation '%s' not understood" % op_name) assert tmp_out.dtype == tmp_in.dtype assert tmp_out.dtype == np.float32 return tmp_out
def test_error_arr_in_ndim(): arr_in = np.zeros((1, 4, 4, 4), dtype=DTYPE) arr_fb = np.zeros((1, 1, 4, 8), dtype=DTYPE) fbcorr3(arr_in, arr_fb)
def test_error_arr_fb_d_too_large(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) arr_fb = np.zeros((1, 1, 5, 8), dtype=DTYPE) fbcorr3(arr_in, arr_fb)
def test_error_arr_fb_wrong_dtype(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) arr_fb = np.zeros((1, 1, 5, 8), dtype='float64') fbcorr3(arr_in, arr_fb)
def test_error_too_few_filters(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) arr_fb = np.zeros((3, 3, 4, 1), dtype=DTYPE) fbcorr3(arr_in, arr_fb)
def test_error_arr_fb_w_too_big(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) arr_fb = np.zeros((1, 31, 4, 8), dtype=DTYPE) fbcorr3(arr_in, arr_fb)
def test_error_arr_fb_ndim(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) arr_fb = np.zeros((3, 3, 4), dtype=DTYPE) fbcorr3(arr_in, arr_fb)