def alloc_random_uniform_filterbank(n_filters, height, width, channels, dtype, rseed, normalize=True): """ Generate the same weights as are generated by pythor3 """ if height != width: raise ValueError('filters must be square') if channels is None: filter_shape = [n_filters, height, width] else: filter_shape = [n_filters, height, width, channels] rng = np.random.RandomState(rseed) foobar.append_randomstate('alloc_random_uniform_filterbank', rng) fb_data = rng.uniform(size=filter_shape) # normalize each filter in the bank if needed if normalize: # TODO: vectorize these computations, do all at once. for fidx, filt in enumerate(fb_data): # normalization here means zero-mean, unit-L2norm filt -= filt.mean() filt_norm = np.sqrt((filt * filt).sum()) assert filt_norm != 0 filt /= filt_norm fb_data[fidx] = filt foobar.append_ndarray_signature(fb_data, 'alloc_random_uniform_filterbank') return fb_data.astype(dtype)
def fb_whitened_projections(patches, pwfX, n_filters, rseed, dtype): """ pwfX is the output of patch_whitening_filterbank_X with reshape=False M, and fb will be reshaped to match elements of patches """ M, P, patches_cn = pwfX if patches_cn.ndim != 2: raise TypeError('wrong shape for pwfX args, should be flattened', patches_cn.shape) rng = np.random.RandomState(rseed) foobar.append_randomstate('fb_whitened_projections', rng) D = rng.randn(n_filters, patches_cn.shape[1]) D = D / (np.sqrt((D ** 2).sum(axis=1))[:, None] + 1e-20) fb = dot_f32(D, P) fb.shape = (n_filters,) + patches.shape[1:] M.shape = patches.shape[1:] M = M.astype(dtype) fb = fb.astype(dtype) if fb.size == 0: raise ValueError('filterbank had size 0') foobar.append_ndarray_signature(M, 'fb_whitened_projections M') foobar.append_ndarray_signature(fb, 'fb_whitened_projections fb') return M, fb
def fb_whitened_projections(patches, pwfX, n_filters, rseed, dtype): """ pwfX is the output of patch_whitening_filterbank_X with reshape=False M, and fb will be reshaped to match elements of patches """ M, P, patches_cn = pwfX if patches_cn.ndim != 2: raise TypeError('wrong shape for pwfX args, should be flattened', patches_cn.shape) rng = np.random.RandomState(rseed) foobar.append_randomstate('fb_whitened_projections', rng) D = rng.randn(n_filters, patches_cn.shape[1]) D = D / (np.sqrt((D**2).sum(axis=1))[:, None] + 1e-20) fb = dot_f32(D, P) fb.shape = (n_filters, ) + patches.shape[1:] M.shape = patches.shape[1:] M = M.astype(dtype) fb = fb.astype(dtype) if fb.size == 0: raise ValueError('filterbank had size 0') foobar.append_ndarray_signature(M, 'fb_whitened_projections M') foobar.append_ndarray_signature(fb, 'fb_whitened_projections fb') return M, fb
def fb_whitened_patches(patches, pwfX, n_filters, rseed, dtype): """ pwfX is the output of patch_whitening_filterbank_X with reshape=False M, and fb will be reshaped to match elements of patches """ M, P, patches_cn = pwfX rng = np.random.RandomState(rseed) foobar.append_randomstate('fb_whitened_patches', rng) d_elems = rng.randint(len(patches_cn), size=n_filters) D = dot_f64(patches_cn[d_elems] - M, P) D = D / (np.sqrt((D ** 2).sum(axis=1))[:, None] + 1e-20) fb = dot_f32(D, P) fb.shape = (n_filters,) + patches.shape[1:] M.shape = patches.shape[1:] M = M.astype(dtype) fb = fb.astype(dtype) if fb.size == 0: raise ValueError('filterbank had size 0') foobar.append_ndarray_signature(M, 'fb_whitened_patches M') foobar.append_ndarray_signature(fb, 'fb_whitened_patches fb') return M, fb
def fb_whitened_patches(patches, pwfX, n_filters, rseed, dtype): """ pwfX is the output of patch_whitening_filterbank_X with reshape=False M, and fb will be reshaped to match elements of patches """ M, P, patches_cn = pwfX rng = np.random.RandomState(rseed) foobar.append_randomstate('fb_whitened_patches', rng) d_elems = rng.randint(len(patches_cn), size=n_filters) D = dot_f64(patches_cn[d_elems] - M, P) D = D / (np.sqrt((D**2).sum(axis=1))[:, None] + 1e-20) fb = dot_f32(D, P) fb.shape = (n_filters, ) + patches.shape[1:] M.shape = patches.shape[1:] M = M.astype(dtype) fb = fb.astype(dtype) if fb.size == 0: raise ValueError('filterbank had size 0') foobar.append_ndarray_signature(M, 'fb_whitened_patches M') foobar.append_ndarray_signature(fb, 'fb_whitened_patches fb') return M, fb
def random_patches(images, N, R, C, rng, channel_major=False, memlimit=None): """Return a stack of N image patches (channel major version)""" def N_with_memlimit(): if memlimit is not None: # -- memlimit in bytes sizelimit = memlimit / images.dtype.itemsize return min(N, sizelimit // (R * C * iF)) else: return N if channel_major: n_imgs, iF, iR, iC = images.shape N = N_with_memlimit() rval = np.empty((N, iF, R, C), dtype=images.dtype) else: n_imgs, iR, iC, iF = images.shape N = N_with_memlimit() rval = np.empty((N, R, C, iF), dtype=images.dtype) foobar.append_trace('random_patches dims', *rval.shape) foobar.append_randomstate('random_patches rng', rng) srcs = rng.randint(n_imgs, size=N) if R > iR or C > iC: raise InvalidDescription('cannot extract patches', (R, C)) roffsets = rng.randint(iR - R + 1, size=N) coffsets = rng.randint(iC - C + 1, size=N) # TODO: this can be done with one advanced index right? for rv_i, src_i, ro, co in zip(rval, srcs, roffsets, coffsets): if channel_major: rv_i[:] = images[src_i, :, ro: ro + R, co : co + C] else: rv_i[:] = images[src_i, ro: ro + R, co : co + C] foobar.append_ndarray_signature(rval, 'random_patches rval') return rval
def random_patches(images, N, R, C, rng, channel_major=False, memlimit=None): """Return a stack of N image patches (channel major version)""" def N_with_memlimit(): if memlimit is not None: # -- memlimit in bytes sizelimit = memlimit / images.dtype.itemsize return min(N, sizelimit // (R * C * iF)) else: return N if channel_major: n_imgs, iF, iR, iC = images.shape N = N_with_memlimit() rval = np.empty((N, iF, R, C), dtype=images.dtype) else: n_imgs, iR, iC, iF = images.shape N = N_with_memlimit() rval = np.empty((N, R, C, iF), dtype=images.dtype) foobar.append_trace('random_patches dims', *rval.shape) foobar.append_randomstate('random_patches rng', rng) srcs = rng.randint(n_imgs, size=N) if R > iR or C > iC: raise InvalidDescription('cannot extract patches', (R, C)) roffsets = rng.randint(iR - R + 1, size=N) coffsets = rng.randint(iC - C + 1, size=N) # TODO: this can be done with one advanced index right? for rv_i, src_i, ro, co in zip(rval, srcs, roffsets, coffsets): if channel_major: rv_i[:] = images[src_i, :, ro:ro + R, co:co + C] else: rv_i[:] = images[src_i, ro:ro + R, co:co + C] foobar.append_ndarray_signature(rval, 'random_patches rval') return rval