def out_generated_image(trainer):
        train_ind = [1, 3, 5, 10, 2, 0, 13, 15, 17]
        x = chainer.Variable(np.asarray(train[train_ind]))
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            x1 = model(x)

        test_ind = [3, 2, 1, 18, 4, 8, 11, 17, 61]
        x_test = chainer.Variable(np.asarray(test[test_ind]))
        with chainer.using_config('train', False), chainer.no_backprop_mode():
            x1_test = model(x)

        # draw images from randomly sampled z
        z = chainer.Variable(
            np.random.normal(0, 1, (9, args.dimz)).astype(np.float32))
        x_sampled = model.decode(z)

        epoch = trainer.updater.epoch
        iteration = trainer.updater.iteration
        with summary.reporter(epoch=epoch, iteration=iteration) as r:
            r.image(x.reshape(len(train_ind), 28, 28), 'train', row=3)
            r.image(x1.reshape(len(train_ind), 28, 28),
                    'train_reconstructed',
                    row=3)
            r.image(x_test.reshape(len(test_ind), 28, 28), 'test', row=3)
            r.image(x1_test.reshape(len(test_ind), 28, 28),
                    'test_reconstructed',
                    row=3)
            r.image(x_sampled.reshape(9, 28, 28), 'sampled', row=3)
Esempio n. 2
0
def test_summary_reporter_empty(func_dir):
    with summary.reporter(out=func_dir, epoch=10):
        pass

    meta_filepath = os.path.join(func_dir,
                                 summary.CHAINERUI_ASSETS_METAFILE_NAME)
    assert not os.path.exists(meta_filepath)
Esempio n. 3
0
def test_summary_reporter_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))
        with summary.reporter() as r:
            r.image(img)
        assert len(w) == 1
        assert os.path.exists(meta_filepath)

        summary.set_out(func_dir)
        with summary.reporter() as r:
            r.image(img)
        assert len(w) == 1
Esempio n. 4
0
def test_reporter_audio_unavailable(func_dir):
    mock_checker = MagicMock(return_value=False)
    with patch('chainerui.report.audio_report.check_available', mock_checker):
        with summary.reporter(out=func_dir) as r:
            r.audio(np.zeros(10), 40)

    assert not os.path.exists(
        os.path.join(func_dir, summary.CHAINERUI_ASSETS_METAFILE_NAME))
Esempio n. 5
0
def test_summary_timeout(func_dir, caplog):
    meta_filepath = os.path.join(func_dir,
                                 summary.CHAINERUI_ASSETS_METAFILE_NAME)
    metalock_filepath = meta_filepath + '.lock'

    with filelock.FileLock(metalock_filepath):
        with summary.reporter(out=func_dir, timeout=0.1) as r:
            # test process has already handled meta file,
            # this saving process should be timeout
            r.text('test')

    assert len(caplog.records) == 1
    assert 'is timeout' in caplog.records[0].message
    assert not os.path.exists(meta_filepath)
Esempio n. 6
0
def test_summary_reporter_mix(func_dir):
    img = np.zeros(10 * 3 * 5 * 5, dtype=np.float32).reshape((10, 3, 5, 5))
    img2 = np.copy(img)
    audio = np.random.uniform(-1, 1, 16000)
    audio2 = np.copy(audio)

    with summary.reporter(prefix='with_', out=func_dir, epoch=10) as r:
        r.image(img)
        r.image(img2, 'test_image', subdir='image')
        r.audio(audio, 16000)
        r.audio(audio2, 16000, 'test_audio', subdir='audio')
        r.text('content')
        r.text('content2', 'test_text')

    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 'with_image_0' in metas[0]['images']
    saved_filename = metas[0]['images']['with_image_0']
    assert saved_filename.startswith('with_image_0')
    assert saved_filename.endswith('.png')
    assert 'with_test_image' in metas[0]['images']
    saved_filename1 = metas[0]['images']['with_test_image']
    assert saved_filename1.startswith(os.path.join('image', 'with_test_image'))
    assert saved_filename1.endswith('.png')
    assert 'with_audio_2' in metas[0]['audios']
    saved_filename3 = metas[0]['audios']['with_audio_2']
    assert saved_filename3.startswith('with_audio_2')
    assert saved_filename3.endswith('.wav')
    assert 'with_test_audio' in metas[0]['audios']
    saved_filename4 = metas[0]['audios']['with_test_audio']
    assert saved_filename4.startswith(os.path.join('audio', 'with_test_audio'))
    assert saved_filename4.endswith('.wav')
    assert 'texts' in metas[0]
    assert metas[0]['texts'].get('with_text_4', None) == 'content'
    assert metas[0]['texts'].get('with_test_text', None) == 'content2'

    img3 = np.copy(img)
    img4 = np.copy(img)
    audio3 = np.copy(audio)
    audio4 = np.copy(audio)

    with summary.reporter(prefix='with_', out=func_dir, subdir='sub',
                          epoch=20) as r:
        r.image(img3)
        r.image(img4, 'test_image', subdir='image')
        r.audio(audio3, 44100)
        r.audio(audio4, 44100, 'test_audio', subdir='audio')
        r.text('content')
        r.text('content2', 'test_text')

    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 'with_image_0' in metas2[1]['images']
    saved_filename = metas2[1]['images']['with_image_0']
    assert saved_filename.startswith(os.path.join('sub', 'with_image_0'))
    assert saved_filename.endswith('.png')
    assert 'with_test_image' in metas2[1]['images']
    saved_filename1 = metas2[1]['images']['with_test_image']
    assert saved_filename1.startswith(
        os.path.join('sub', 'image', 'with_test_image'))
    assert saved_filename1.endswith('.png')
    assert 'with_audio_2' in metas2[1]['audios']
    saved_filename2 = metas2[1]['audios']['with_audio_2']
    assert saved_filename2.startswith(os.path.join('sub', 'with_audio_2'))
    assert saved_filename2.endswith('.wav')
    assert 'with_test_audio' in metas2[1]['audios']
    saved_filename3 = metas2[1]['audios']['with_test_audio']
    assert saved_filename3.startswith(
        os.path.join('sub', 'audio', 'with_test_audio'))
    assert saved_filename3.endswith('.wav')
    assert 'texts' in metas2[1]
    assert metas2[1]['texts'].get('with_text_4', None) == 'content'
    assert metas2[1]['texts'].get('with_test_text', None) == 'content2'