def test_max_neighborhood_sizei_with_arr_out(): arr_in = np.arange(4*4*3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = np.empty((1, 2, 3)).astype(DTYPE) arr_out = lcdnorm3(arr_in, neighborhood, arr_out=arr_out)
def test_stride(): arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lcdnorm3(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 = lcdnorm3(arr_in, neighborhood, stride=2) assert arr_out.shape == (1, 1, 3)
def test_max_neighborhood_size(): arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lcdnorm3(arr_in, neighborhood) assert arr_out.shape == (1, 2, 3)
def test_max_neighborhood_sizei_with_arr_out(): arr_in = np.arange(4 * 4 * 3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = np.empty((1, 2, 3)).astype(DTYPE) arr_out = lcdnorm3(arr_in, neighborhood, arr_out=arr_out)
def test_max_neighborhood_size(): arr_in = np.arange(4*4*3).reshape((4, 4, 3)).astype(DTYPE) neighborhood = (4, 3) arr_out = lcdnorm3(arr_in, neighborhood) assert arr_out.shape == (1, 2, 3)
def test_input_d_1_default(): 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(*arr_in.shape) idx = [[4, 3], [20, 12]] gt = np.array([[0.23064923], [0.20238316]], dtype=DTYPE) lcdnorm3(arr_in, neighborhood, arr_out=arr_out) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lcdnorm3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
def 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([[-0.53213698], [-0.0816204]], dtype=DTYPE) arr_out = lcdnorm3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
def test_input_d_4_default(): 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, 3], [20, 12]] gt = np.array( [[+1.27813682e-01, -9.97862518e-02, -2.48777084e-02, -5.16409911e-02], [-2.00690944e-02, -2.42322776e-02, 7.76741435e-05, -3.73861268e-02]], dtype=DTYPE) lcdnorm3(arr_in, neighborhood, arr_out=arr_out) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lcdnorm3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
def 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([[-0.53213698], [-0.0816204]], dtype=DTYPE) arr_out = lcdnorm3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
def test_input_d_4_default(): 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, 3], [20, 12]] gt = np.array([[+1.27813682e-01, -9.97862518e-02, -2.48777084e-02, -5.16409911e-02], [-2.00690944e-02, -2.42322776e-02, 7.76741435e-05, -3.73861268e-02]], dtype=DTYPE) lcdnorm3(arr_in, neighborhood, arr_out=arr_out) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL) arr_out = lcdnorm3(arr_in, neighborhood) gv = arr_out[idx] assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
def test_smoke_max_neighborhood_size(): np.random.seed(42) arr_in = np.random.randn(4, 4, 3).astype(DTYPE) neighborhood = (4, 3) arr_out = lcdnorm3(arr_in, neighborhood) idx = (0, 1, 2) ref = 0.0123399 res = arr_out[idx] assert (ref - res) < 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