Ejemplo n.º 1
0
def test_caption_random_features(train_params: str, translate_params: str):
    # generate random names
    source_list = [
        ''.join(random.choice(string.ascii_uppercase) for _ in range(4))
        for i in range(15)
    ]
    prefix = "tmp_caption_random"
    use_features = True
    with tmp_img_captioning_dataset(source_list,
                                    prefix,
                                    train_max_length=_LINE_MAX_LENGTH,
                                    dev_max_length=_LINE_MAX_LENGTH,
                                    test_max_length=_TEST_MAX_LENGTH,
                                    use_features=use_features) as data:
        # Test model configuration, including the output equivalence of batch and no-batch decoding
        translate_params_batch = translate_params + " --batch-size 2"

        # Ignore return values (perplexity and BLEU) for integration test
        run_train_captioning(train_params=train_params,
                             translate_params=translate_params,
                             translate_params_equiv=translate_params_batch,
                             train_source_path=data['source'],
                             train_target_path=data['target'],
                             dev_source_path=data['validation_source'],
                             dev_target_path=data['validation_target'],
                             test_source_path=data['test_source'],
                             test_target_path=data['test_target'],
                             max_seq_len=_LINE_MAX_LENGTH + 1,
                             work_dir=data['work_dir'])
Ejemplo n.º 2
0
def test_caption_random_features(train_params: str, translate_params: str):
    # generate random names
    source_list = [''.join(random.choice(string.ascii_uppercase) for _ in range(4)) for i in range(15)]
    prefix = "tmp_caption_ramdom"
    use_features = True
    with tmp_img_captioning_dataset(source_list,
                                    prefix,
                                    train_max_length=_LINE_MAX_LENGTH,
                                    dev_max_length=_LINE_MAX_LENGTH,
                                    test_max_length=_TEST_MAX_LENGTH,
                                    use_features=use_features) as data:
        # Test model configuration, including the output equivalence of batch and no-batch decoding
        translate_params_batch = translate_params + " --batch-size 2"

        # Ignore return values (perplexity and BLEU) for integration test
        run_train_captioning(train_params=train_params,
                             translate_params=translate_params,
                             translate_params_equiv=translate_params_batch,
                             train_source_path=data['source'],
                             train_target_path=data['target'],
                             dev_source_path=data['validation_source'],
                             dev_target_path=data['validation_target'],
                             test_source_path=data['test_source'],
                             test_target_path=data['test_target'],
                             max_seq_len=_LINE_MAX_LENGTH + 1,
                             work_dir=data['work_dir'])
Ejemplo n.º 3
0
def test_caption_random_features(layer: str):
    source_image_size = (3, 20, 20)
    batch_size = 8
    extract_params = "--source-image-size {s1} {s2} {s3} --batch-size {batch_size} " \
                     "--image-encoder-layer {layer}".format(s1=source_image_size[0],
                                                            s2=source_image_size[1],
                                                            s3=source_image_size[2],
                                                            batch_size=batch_size,
                                                            layer=layer)

    # generate random names
    source_list = [
        ''.join(random.choice(string.ascii_uppercase) for _ in range(4)) for i
        in range(8)]
    prefix = "tmp_features"
    use_features = False
    with tmp_img_captioning_dataset(source_list,
                                    prefix,
                                    train_max_length=1,
                                    dev_max_length=1,
                                    test_max_length=1,
                                    use_features=use_features) as data:
        source_files = [data["source"], data["validation_source"],
                        data["test_source"]]
        run_extract_features_captioning(source_image_size=source_image_size,
                                        batch_size=batch_size,
                                        extract_params=extract_params,
                                        source_files=source_files,
                                        image_root=data['work_dir'])
Ejemplo n.º 4
0
def test_caption_random_features(layer: str):
    source_image_size = (3, 20, 20)
    batch_size = 8
    extract_params = "--source-image-size {s1} {s2} {s3} --batch-size {batch_size} " \
                     "--image-encoder-layer {layer}".format(s1=source_image_size[0],
                                                            s2=source_image_size[1],
                                                            s3=source_image_size[2],
                                                            batch_size=batch_size,
                                                            layer=layer)

    # generate random names
    source_list = [
        ''.join(random.choice(string.ascii_uppercase) for _ in range(4))
        for i in range(8)
    ]
    prefix = "tmp_features"
    use_features = False
    with tmp_img_captioning_dataset(source_list,
                                    prefix,
                                    train_max_length=1,
                                    dev_max_length=1,
                                    test_max_length=1,
                                    use_features=use_features) as data:
        source_files = [
            data["source"], data["validation_source"], data["test_source"]
        ]
        run_extract_features_captioning(source_image_size=source_image_size,
                                        batch_size=batch_size,
                                        extract_params=extract_params,
                                        source_files=source_files,
                                        image_root=data['work_dir'])
Ejemplo n.º 5
0
def test_get_training_image_text_data_iters():
    # Test images
    source_list = ['1', '2', '3', '4', '100']
    prefix = "tmp_corpus"
    use_feature_loader = False
    preload_features = False
    train_max_length = 30
    dev_max_length = 30
    expected_mean = 1.0
    expected_std = 1.0
    test_max_length = 30
    batch_size = 5
    if use_feature_loader:
        source_image_size = _FEATURE_SHAPE
    else:
        source_image_size = _CNN_INPUT_IMAGE_SHAPE
    with tmp_img_captioning_dataset(source_list, prefix, train_max_length,
                                    dev_max_length, test_max_length,
                                    use_feature_loader) as data:
        # tmp common vocab
        vcb = vocab.build_from_paths([data['target'], data['target']])

        train_iter, val_iter, config_data, data_info = data_io.get_training_image_text_data_iters(
            source_root=data['work_dir'],
            source=data['source'],
            target=data['target'],
            validation_source_root=data['work_dir'],
            validation_source=data['validation_source'],
            validation_target=data['validation_target'],
            vocab_target=vcb,
            vocab_target_path=None,
            batch_size=batch_size,
            batch_by_words=False,
            batch_num_devices=1,
            source_image_size=source_image_size,
            fill_up="replicate",
            max_seq_len_target=train_max_length,
            bucketing=False,
            bucket_width=10,
            use_feature_loader=use_feature_loader,
            preload_features=preload_features)
        assert isinstance(train_iter, data_io.ParallelSampleIter)
        assert isinstance(val_iter, data_io.ParallelSampleIter)
        assert isinstance(config_data, data_io.DataConfig)
        assert isinstance(data_info.sources[0], data_io.FileListReader)
        assert data_info.target == data['target']
        assert data_info.source_vocabs is None
        assert data_info.target_vocab is None
        assert config_data.data_statistics.max_observed_len_source == 0
        assert config_data.data_statistics.max_observed_len_target == train_max_length - 1
        assert np.isclose(config_data.data_statistics.length_ratio_mean,
                          expected_mean)
        assert np.isclose(config_data.data_statistics.length_ratio_std,
                          expected_std)

        assert train_iter.batch_size == batch_size
        assert val_iter.batch_size == batch_size
        assert train_iter.default_bucket_key == (0, train_max_length)
        assert val_iter.default_bucket_key == (0, dev_max_length)
        assert train_iter.dtype == 'float32'

        # test some batches
        bos_id = vcb[C.BOS_SYMBOL]
        expected_first_target_symbols = np.full((batch_size, ),
                                                bos_id,
                                                dtype='float32')
        for epoch in range(2):
            while train_iter.iter_next():
                batch = train_iter.next()
                assert len(batch.data) == 2
                assert len(batch.label) == 1
                assert batch.bucket_key in train_iter.buckets
                source = batch.data[0].asnumpy()
                target = batch.data[1].asnumpy()
                label = batch.label[0].asnumpy()
                assert source.shape[0] == target.shape[0] == label.shape[
                    0] == batch_size
                # target first symbol should be BOS
                assert np.array_equal(target[:, 0],
                                      expected_first_target_symbols)
                # label first symbol should be 2nd target symbol
                assert np.array_equal(label[:, 0], target[:, 1])
                # each label sequence contains one EOS symbol
                assert np.sum(label == vcb[C.EOS_SYMBOL]) == batch_size
            train_iter.reset()
Ejemplo n.º 6
0
def test_get_training_image_text_data_iters():
    # Test images
    source_list = ['1', '2', '3', '4', '100']
    prefix = "tmp_corpus"
    use_feature_loader = False
    preload_features = False
    train_max_length = 30
    dev_max_length = 30
    expected_mean = 1.0
    expected_std = 1.0
    test_max_length = 30
    batch_size = 5
    if use_feature_loader:
        source_image_size = _FEATURE_SHAPE
    else:
        source_image_size = _CNN_INPUT_IMAGE_SHAPE
    with tmp_img_captioning_dataset(source_list,
                                    prefix,
                                    train_max_length,
                                    dev_max_length,
                                    test_max_length,
                                    use_feature_loader) as data:
        # tmp common vocab
        vcb = vocab.build_from_paths([data['target'], data['target']])

        train_iter, val_iter, config_data, data_info = data_io.get_training_image_text_data_iters(source_root=data['work_dir'],
                                                                                                  source=data['source'],
                                                                                                  target=data['target'],
                                                                                                  validation_source_root=data['work_dir'],
                                                                                                  validation_source=data['validation_source'],
                                                                                                  validation_target=data['validation_target'],
                                                                                                  vocab_target=vcb,
                                                                                                  vocab_target_path=None,
                                                                                                  batch_size=batch_size,
                                                                                                  batch_by_words=False,
                                                                                                  batch_num_devices=1,
                                                                                                  source_image_size=source_image_size,
                                                                                                  fill_up="replicate",
                                                                                                  max_seq_len_target=train_max_length,
                                                                                                  bucketing=False,
                                                                                                  bucket_width=10,
                                                                                                  use_feature_loader=use_feature_loader,
                                                                                                  preload_features=preload_features)
        assert isinstance(train_iter, data_io.ParallelSampleIter)
        assert isinstance(val_iter, data_io.ParallelSampleIter)
        assert isinstance(config_data, data_io.DataConfig)
        assert isinstance(data_info.sources[0], data_io.FileListReader)
        assert data_info.target == data['target']
        assert data_info.source_vocabs is None
        assert data_info.target_vocab is None
        assert config_data.data_statistics.max_observed_len_source == 0
        assert config_data.data_statistics.max_observed_len_target == train_max_length - 1
        assert np.isclose(config_data.data_statistics.length_ratio_mean, expected_mean)
        assert np.isclose(config_data.data_statistics.length_ratio_std, expected_std)

        assert train_iter.batch_size == batch_size
        assert val_iter.batch_size == batch_size
        assert train_iter.default_bucket_key == (0, train_max_length)
        assert val_iter.default_bucket_key == (0, dev_max_length)
        assert train_iter.dtype == 'float32'

        # test some batches
        bos_id = vcb[C.BOS_SYMBOL]
        expected_first_target_symbols = np.full((batch_size,), bos_id, dtype='float32')
        for epoch in range(2):
            while train_iter.iter_next():
                batch = train_iter.next()
                assert len(batch.data) == 2
                assert len(batch.label) == 1
                assert batch.bucket_key in train_iter.buckets
                source = batch.data[0].asnumpy()
                target = batch.data[1].asnumpy()
                label = batch.label[0].asnumpy()
                assert source.shape[0] == target.shape[0] == label.shape[0] == batch_size
                # target first symbol should be BOS
                assert np.array_equal(target[:, 0], expected_first_target_symbols)
                # label first symbol should be 2nd target symbol
                assert np.array_equal(label[:, 0], target[:, 1])
                # each label sequence contains one EOS symbol
                assert np.sum(label == vcb[C.EOS_SYMBOL]) == batch_size
            train_iter.reset()