def test_neighborhood_order_2_stride_2(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) neighborhood = 5, 5 arr_out = np.zeros((8, 13, 4), dtype=DTYPE) np.random.seed(42) arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape) order = 2 stride = 2 idx = [[4, 3], [10, 6]] gt = np.array([[4.81159449, 5.68032312, 5.07941389, 6.04614496], [4.87372255, 4.47074938, 4.07878876, 4.43441534]], dtype=DTYPE) lpool3(arr_in, neighborhood, order=order, stride=stride, arr_out=arr_out) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lpool3(arr_in, neighborhood, order=order, stride=stride) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_strides_self(): arr_in = lena() / 1. arr_in = arr_in[:12, :12] arr_in.shape = arr_in.shape[:2] + (1,) neighborhood = 3, 3 for stride in xrange(1, 12): arr_out = lpool3(arr_in, neighborhood) gt = arr_out[::stride, ::stride] gv = lpool3(arr_in, neighborhood, stride=stride) assert_array_equal(gv, gt)
def test_strides_self(): arr_in = lena() / 1. arr_in = arr_in[:12, :12] arr_in.shape = arr_in.shape[:2] + (1, ) neighborhood = 3, 3 for stride in xrange(1, 12): arr_out = lpool3(arr_in, neighborhood) gt = arr_out[::stride, ::stride] gv = lpool3(arr_in, neighborhood, stride=stride) assert_array_equal(gv, gt)
def test_neighborhood_max_size(): arr_in = np.arange(4*4*3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lpool3(arr_in, neighborhood) assert arr_out.shape == (1, 2, 3)
def test_stride(): arr_in = np.arange(4*4*3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lpool3(arr_in, neighborhood, stride=2) assert arr_out.shape == (1, 1, 3)
def test_stride(): arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lpool3(arr_in, neighborhood, stride=2) assert arr_out.shape == (1, 1, 3)
def test_neighborhood_max_size(): arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lpool3(arr_in, neighborhood) assert arr_out.shape == (1, 2, 3)
def test_default_input_d_1(): arr_in = np.zeros((20, 30, 1), dtype=DTYPE) neighborhood = 5, 5 arr_out = np.zeros((16, 26, 1), dtype=DTYPE) np.random.seed(42) arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape) idx = [[4, 8], [20, 12]] gt = np.array([[-3.59586287], [9.16217232]], dtype=DTYPE) lpool3(arr_in, neighborhood, arr_out=arr_out) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lpool3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_neighborhood_5(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) neighborhood = 5, 5 arr_out = np.zeros((16, 26, 4), dtype=DTYPE) np.random.seed(42) arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape) idx = [[4, 8], [20, 12]] gt = np.array([[7.93315649, -0.24066222, 0.64046526, -3.51567388], [7.14803362, -2.99622989, 9.5001564, 11.99116325]], dtype=DTYPE) lpool3(arr_in, neighborhood, arr_out=arr_out) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lpool3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_default_lena(): arr_in = lena().astype(np.float32) / 1. arr_in.shape = arr_in.shape[:2] + (1, ) idx = [[4, 2], [4, 2]] neighborhood = 3, 3 gt = np.array([[1442.], [1455.]], dtype=DTYPE) arr_out = lpool3(arr_in, neighborhood) assert_equals(arr_out.ndim, arr_in.ndim) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_test_lena_npy_array(): arr_in = lena()[::32, ::32].astype(DTYPE) arr_in.shape = arr_in.shape[:2] + (1, ) neighborhood = 3, 3 idx = [[4, 2], [4, 2]] gt = np.array([[1280.99987793], [992.]], dtype=DTYPE) arr_out = lpool3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_neighborhood_5_order_2(): arr_in = np.zeros((20, 30, 4), dtype=DTYPE) neighborhood = 5, 5 arr_out = np.zeros((16, 26, 4), dtype=DTYPE) np.random.seed(42) arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape) order = 2 idx = [[4, 8], [20, 12]] gt = np.array([[5.22081137, 4.9204731, 3.75381684, 4.892416], [5.60951042, 4.28514147, 4.77592659, 5.77252817]], dtype=DTYPE) lpool3(arr_in, neighborhood, order=order, arr_out=arr_out) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lpool3(arr_in, neighborhood, order=order) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_test_lena_npy_array(): arr_in = lena()[::32, ::32].astype(DTYPE) arr_in.shape = arr_in.shape[:2] + (1,) neighborhood = 3, 3 idx = [[4, 2], [4, 2]] gt = np.array([[1280.99987793], [992.]], dtype=DTYPE) arr_out = lpool3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_default_lena(): arr_in = lena().astype(np.float32) / 1. arr_in.shape = arr_in.shape[:2] + (1,) idx = [[4, 2], [4, 2]] neighborhood = 3, 3 gt = np.array([[1442.], [1455.]], dtype=DTYPE) arr_out = lpool3(arr_in, neighborhood) assert_equals(arr_out.ndim, arr_in.ndim) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_default_lena_non_C_contiguous(): arr_in = lena() / 1. arr_in.shape = arr_in.shape[:2] + (1, ) neighborhood = 3, 3 arr_in = np.asfortranarray(arr_in) idx = [[4, 2], [4, 2]] gt = np.array([[1442.], [1455.]], dtype=DTYPE) arr_out = lpool3(arr_in, neighborhood) assert_equals(arr_out.ndim, arr_in.ndim) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def test_neighborhood_max_size_smoke_test(): np.random.seed(42) arr_in = np.random.randn(4, 4, 3).astype(DTYPE) neighborhood = (4, 3) arr_out = lpool3(arr_in, neighborhood) idx = (0, 1, 2) ref = -2.59025908 res = arr_out[idx] assert (ref - res) < ATOL
def test_default_lena_non_C_contiguous(): arr_in = lena() / 1. arr_in.shape = arr_in.shape[:2] + (1,) neighborhood = 3, 3 arr_in = np.asfortranarray(arr_in) idx = [[4, 2], [4, 2]] gt = np.array([[1442.], [1455.]], dtype=DTYPE) arr_out = lpool3(arr_in, neighborhood) assert_equals(arr_out.ndim, arr_in.ndim) gv = arr_out[idx] assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
def slm(arr_in): assert arr_in.ndim == 3 inh, inw, ind = arr_in.shape fbs = [] fbd = arr_in.shape[-1] for nf in nfs: fbshape = nb + (fbd, nf) print 'generating', fbshape, 'filterbank' fb = np.random.randn(*fbshape).astype('f') fbs += [fb] fbd = nf #for nf in nfs: for fb in fbs: n1 = lcdnorm3(arr_in, nb, threshold=1.0) f1 = fbcorr(n1, fb) p1 = lpool3(f1, nb, order=2, stride=2) arr_in = p1 return p1
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