def test_summary_image(func_dir): img = np.zeros(10 * 3 * 5 * 5, dtype=np.float32).reshape((10, 3, 5, 5)) summary.image(img, out=func_dir, epoch=10) meta_filepath = os.path.join(func_dir, summary.CHAINERUI_ASSETS_METAFILE_NAME) assert os.path.exists(meta_filepath) with open(meta_filepath, 'r') as f: metas = json.load(f) assert len(metas) == 1 assert 'timestamp' in metas[0] assert 'epoch' in metas[0] assert metas[0]['epoch'] == 10 assert 'images' in metas[0] assert 'image' in metas[0]['images'] saved_filename = metas[0]['images']['image'] assert saved_filename.startswith('image_') assert saved_filename.endswith('.png') img2 = np.zeros(10 * 3 * 5 * 5, dtype=np.float32).reshape((10, 3, 5, 5)) summary.image(img2, 'test', out=func_dir, subdir='image', epoch=20) with open(meta_filepath, 'r') as f: metas2 = json.load(f) assert len(metas2) == 2 assert 'timestamp' in metas2[1] assert 'epoch' in metas2[1] assert metas2[1]['epoch'] == 20 assert 'images' in metas2[1] assert 'test' in metas2[1]['images'] saved_filename2 = metas2[1]['images']['test'] assert saved_filename != saved_filename2 assert saved_filename2.startswith(os.path.join('image', 'test_')) assert saved_filename2.endswith('.png')
def test_summary_image_unavailable(func_dir): mock_checker = MagicMock(return_value=False) with patch('chainerui.report.image_report.check_available', mock_checker): summary.image(np.zeros(10), out=func_dir) assert not os.path.exists( os.path.join(func_dir, summary.CHAINERUI_ASSETS_METAFILE_NAME))
def generate_images(trainer): gen = trainer.updater.gen xp = gen.xp z = xp.asarray(gen.make_hidden(n_samples)) with chainer.using_config('train', False): with chainer.using_config('enable_backprop', False): x = gen(z) summary.image(x, name=image_name_format.format(trainer), row=n_rows)
def make_image(trainer): np.random.seed(seed) n_images = rows * cols xp = gen.xp z = Variable(xp.asarray(gen.make_hidden(n_images))) with chainer.using_config('train', False): x = gen(z) x = chainer.cuda.to_cpu(x.data) summary.image(x, row=rows) np.random.seed()
def test_image_error(self): img = np.zeros(10) with self.assertRaises(ValueError) as e: summary.image(img, 'test', batched=False) assert 'must be 2 or 3' in str(e.exception) img = np.zeros(10).reshape(2, 5) with self.assertRaises(ValueError) as e: summary.image(img, 'test') assert 'must be 3 or 4' in str(e.exception)
def test_image_hw(self): observer = summary.chainerui_image_observer img = np.zeros(5*10).reshape((5, 10)) img = chainer.Variable(img) summary.image(img, 'test', batched=False) assert len(observer.observation) == 1 key = summary.CHAINERUI_IMAGE_PREFIX+'/test' assert key in observer.observation assert np.allclose(observer.observation[key]['image'], img.data)
def test_image_chw(self): observer = summary.chainerui_image_observer img = np.zeros(3*5*5).reshape((3, 5, 5)) img = chainer.Variable(img) summary.image(img, 'test', ch_axis=0, batched=False) assert len(observer.observation) == 1 key = summary.CHAINERUI_IMAGE_PREFIX+'/test' assert key in observer.observation expected_img = np.zeros(5*5*3).reshape((5, 5, 3)) assert np.allclose(observer.observation[key]['image'], expected_img) assert 'mode' not in observer.observation[key]
def test_image_hwc_hsv(self): observer = summary.chainerui_image_observer img = np.zeros(5*5*3).reshape((5, 5, 3)) img = chainer.Variable(img) summary.image(img, 'test', batched=False, ch_axis=-1, mode='HSV') assert len(observer.observation) == 1 key = summary.CHAINERUI_IMAGE_PREFIX+'/test' assert key in observer.observation assert np.allclose(observer.observation[key]['image'], img.data) assert 'mode' in observer.observation[key] assert observer.observation[key]['mode'] == 'hsv'
def test_summary_image_without_output_path(func_dir): summary._chainerui_asset_observer.default_output_path = func_dir meta_filepath = os.path.join(func_dir, summary.CHAINERUI_ASSETS_METAFILE_NAME) with warnings.catch_warnings(record=True) as w: warnings.simplefilter('always') img = np.zeros(10 * 3 * 5 * 5, dtype=np.float32).reshape((10, 3, 5, 5)) summary.image(img) assert len(w) == 1 assert os.path.exists(meta_filepath) summary.set_out(func_dir) summary.image(img) assert len(w) == 1
def test_image(self): img = np.zeros(750).reshape((10, 5, 5, 3)) with self.assertRaises(IndexError) as e: summary.image(img, 'test', ch_axis=-1) assert 'out of range' in str(e.exception) reporter = chainer.Reporter() observer = summary.chainerui_image_observer reporter.add_observer(summary.CHAINERUI_IMAGE_PREFIX, observer) with reporter.scope(observer.observation): summary.image(img, 'test', ch_axis=-1) assert len(observer.observation) == 1 key = summary.CHAINERUI_IMAGE_PREFIX+'/test' assert key in observer.observation img2 = np.zeros(750).reshape((10, 5, 5, 3)) img2[0, 0, 0, 1] = 1 with reporter.scope(observer.observation): summary.image(img2, 'test', ch_axis=-1) assert len(observer.observation) == 1 assert key in observer.observation assert np.allclose(observer.observation[key]['array'], img2) assert 'row' not in observer.observation[key] assert 'mode' not in observer.observation[key] img3 = np.zeros(750).reshape((10, 5, 5, 3)) img3[0, 0, 0, 2] = 1 img3 = chainer.Variable(img3) with reporter.scope(observer.observation): summary.image(img3, row=5, ch_axis=-1, mode='hsv') assert len(observer.observation) == 2 none_key = summary.CHAINERUI_IMAGE_PREFIX+'/0' assert none_key in observer.observation assert np.allclose(observer.observation[none_key]['array'], img3.data) assert observer.observation[none_key]['row'] == 5 assert observer.observation[none_key]['mode'] == 'hsv' img4 = np.zeros(750).reshape((10, 3, 5, 5)) img4[0, 0, 1, 0] = 1 with reporter.scope(observer.observation): summary.image(img4, 'test') assert len(observer.observation) == 2 expected_img4 = img4.transpose(0, 2, 3, 1) assert np.allclose(observer.observation[key]['array'], expected_img4) assert 'row' not in observer.observation[key] assert 'mode' not in observer.observation[key]
def test_image_bhw_no_name(self): observer = summary.chainerui_image_observer # unstuck img = np.zeros(10*5*5).reshape((10, 5, 5)) summary.image(img) assert len(observer.observation) == 1 key = summary.CHAINERUI_IMAGE_PREFIX+'/0' assert key in observer.observation expected_img = np.zeros(10*5*5).reshape((50, 5)) assert np.allclose(observer.observation[key]['image'], expected_img) # stuck img2 = np.zeros(10*5*5).reshape((10, 5, 5)) img2[0, 0, 0] = 10 summary.image(img2, row=2) assert len(observer.observation) == 1 expected_img2 = np.zeros(10*5*5).reshape((10, 25)) expected_img2[0, 0] = 10 assert np.allclose(observer.observation[key]['image'], expected_img2)
def test_image_bhwc(self): observer = summary.chainerui_image_observer # unstuck img = np.zeros(10*5*5*3).reshape((10, 5, 5, 3)) summary.image(img, 'test', ch_axis=-1) assert len(observer.observation) == 1 key = summary.CHAINERUI_IMAGE_PREFIX+'/test' assert key in observer.observation expected_img = np.zeros(10*3*5*5).reshape((50, 5, 3)) assert np.allclose(observer.observation[key]['image'], expected_img) # stuck img2 = np.zeros(10*5*5*3).reshape((10, 5, 5, 3)) img2[0, 0, 0, 0] = 10 summary.image(img2, 'test2', ch_axis=-1, row=2) assert len(observer.observation) == 2 key2 = summary.CHAINERUI_IMAGE_PREFIX+'/test2' assert key2 in observer.observation expected_img2 = np.zeros(10*5*5*3).reshape((10, 25, 3)) expected_img2[0, 0, 0] = 10 assert np.allclose(observer.observation[key2]['image'], expected_img2)
def make_image(trainer): np.random.seed(seed) xp = enc.xp w_in = 256 w_out = 256 in_ch = 12 out_ch = 3 in_all = np.zeros((n_images, in_ch, w_in, w_in)).astype("i") gt_all = np.zeros((n_images, out_ch, w_out, w_out)).astype("f") gen_all = np.zeros((n_images, out_ch, w_out, w_out)).astype("f") for it in range(n_images): batch = updater.get_iterator('test').next() batchsize = len(batch) x_in = xp.zeros((batchsize, in_ch, w_in, w_in)).astype("f") t_out = xp.zeros((batchsize, out_ch, w_out, w_out)).astype("f") for i in range(batchsize): x_in[i,:] = xp.asarray(batch[i][0]) t_out[i,:] = xp.asarray(batch[i][1]) x_in = Variable(x_in) z = enc(x_in) x_out = dec(z) in_all[it,:] = x_in.data.get()[0,:] gt_all[it,:] = t_out.get()[0,:] gen_all[it,:] = x_out.data.get()[0,:] summary.image(gen_all, name='gen', row=rows) x = np.ones((n_images, 3, w_in, w_in)).astype(np.uint8)*255 x[:,0,:,:] = 0 for i in range(12): x[:,0,:,:] += np.uint8(15*i*in_all[:,i,:,:]) summary.image(x, name='in', row=rows, mode='HSV') summary.image(gt_all, name='gt', row=rows)