def test_PoolingWindows_project(self): im = torch.rand((1, 1, 256, 256), dtype=torch.float32) pw = pooling.PoolingWindows(.5, im.shape[2:]) pooled = pw(im) pw.project(pooled) pw = pooling.PoolingWindows(.5, im.shape[2:], num_scales=3) pooled = pw(im) pw.project(pooled)
def test_PoolingWindows_caching(self, tmp_path): im = torch.rand((1, 1, 256, 256), dtype=torch.float32) # first time we save, second we load pw = pooling.PoolingWindows(.8, im.shape[-2:], num_scales=2, cache_dir=tmp_path) pw = pooling.PoolingWindows(.8, im.shape[-2:], num_scales=2, cache_dir=tmp_path)
def test_PoolingWindows_nonsquare(self): # test PoolingWindows with weirdly-shaped iamges im = torch.rand((1, 1, 256, 256), dtype=torch.float32) for sh in [(256, 128), (256, 127), (256, 125), (125, 125), (127, 125)]: tmp = im[..., :sh[0], :sh[1]] pw = pooling.PoolingWindows(.9, tmp.shape[-2:]) pw(tmp)
def test_PoolingWindows_cosine(self, num_scales, transition_region_width): im = torch.rand((1, 1, 256, 256), dtype=torch.float32) pw = pooling.PoolingWindows( .5, im.shape[2:], num_scales=num_scales, transition_region_width=transition_region_width, window_type='cosine', ) pw(im)
def test_PoolingWindows_plotting(self): im = torch.rand((1, 1, 256, 256), dtype=torch.float32) pw = pooling.PoolingWindows(.8, im.shape[-2:], num_scales=2) pw.plot_window_areas() pw.plot_window_widths() for i in range(2): pw.plot_window_areas('pixels', i) pw.plot_window_widths('pixels', i) fig = po.imshow(im) pw.plot_windows(fig.axes[0]) plt.close('all')
def test_PoolingWindows(self, num_scales): im = torch.rand((1, 1, 256, 256), dtype=torch.float32) pw = pooling.PoolingWindows(.5, im.shape[2:], num_scales=num_scales, window_type='gaussian', std_dev=1) pw(im) # we only support std_dev=1 with pytest.raises(Exception): pooling.PoolingWindows(.5, im.shape[2:], num_scales=num_scales, window_type='gaussian', std_dev=2) with pytest.raises(Exception): pooling.PoolingWindows(.5, im.shape[2:], num_scales=num_scales, window_type='gaussian', std_dev=.5)
def test_reweighting(self, num_scales, input_fmt): pw = pooling.PoolingWindows(.5, (256, 256), num_scales=num_scales) im = {(i, ): torch.rand((1, 1, 256 // 2**i, 256 // 2**i), dtype=torch.float32) for i in range(num_scales)} if input_fmt == 'dict': pw(im, weights=torch.ones(num_scales, 1, 1, 1, 1)) elif input_fmt == 'tensor': for i in range(num_scales): pw(im[(i, )], idx=i, weights=torch.ones(num_scales, 1, 1, 1, 1))
def test_PoolingWindows_sep(self): # test the window and pool function separate of the forward function im = torch.rand((1, 1, 256, 256), dtype=torch.float32) pw = pooling.PoolingWindows(.5, im.shape[2:]) pw.pool(pw.window(im))