Spaces:
Runtime error
Runtime error
File size: 4,838 Bytes
2366e36 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# Copyright (c) OpenMMLab. All rights reserved.
import math
import pytest
import torch
from mmocr.models.textrecog.decoders import (ABILanguageDecoder,
ABIVisionDecoder, BaseDecoder,
NRTRDecoder, ParallelSARDecoder,
ParallelSARDecoderWithBS,
SequentialSARDecoder)
from mmocr.models.textrecog.decoders.sar_decoder_with_bs import DecodeNode
def _create_dummy_input():
feat = torch.rand(1, 512, 4, 40)
out_enc = torch.rand(1, 512)
tgt_dict = {'padded_targets': torch.LongTensor([[1, 1, 1, 1, 36]])}
img_metas = [{'valid_ratio': 1.0}]
return feat, out_enc, tgt_dict, img_metas
def test_base_decoder():
decoder = BaseDecoder()
with pytest.raises(NotImplementedError):
decoder.forward_train(None, None, None, None)
with pytest.raises(NotImplementedError):
decoder.forward_test(None, None, None)
def test_parallel_sar_decoder():
# test parallel sar decoder
decoder = ParallelSARDecoder(num_classes=37, padding_idx=36, max_seq_len=5)
decoder.init_weights()
decoder.train()
feat, out_enc, tgt_dict, img_metas = _create_dummy_input()
with pytest.raises(AssertionError):
decoder(feat, out_enc, tgt_dict, [], True)
with pytest.raises(AssertionError):
decoder(feat, out_enc, tgt_dict, img_metas * 2, True)
out_train = decoder(feat, out_enc, tgt_dict, img_metas, True)
assert out_train.shape == torch.Size([1, 5, 36])
out_test = decoder(feat, out_enc, tgt_dict, img_metas, False)
assert out_test.shape == torch.Size([1, 5, 36])
def test_sequential_sar_decoder():
# test parallel sar decoder
decoder = SequentialSARDecoder(
num_classes=37, padding_idx=36, max_seq_len=5)
decoder.init_weights()
decoder.train()
feat, out_enc, tgt_dict, img_metas = _create_dummy_input()
with pytest.raises(AssertionError):
decoder(feat, out_enc, tgt_dict, [])
with pytest.raises(AssertionError):
decoder(feat, out_enc, tgt_dict, img_metas * 2)
out_train = decoder(feat, out_enc, tgt_dict, img_metas, True)
assert out_train.shape == torch.Size([1, 5, 36])
out_test = decoder(feat, out_enc, tgt_dict, img_metas, False)
assert out_test.shape == torch.Size([1, 5, 36])
def test_parallel_sar_decoder_with_beam_search():
with pytest.raises(AssertionError):
ParallelSARDecoderWithBS(beam_width='beam')
with pytest.raises(AssertionError):
ParallelSARDecoderWithBS(beam_width=0)
feat, out_enc, tgt_dict, img_metas = _create_dummy_input()
decoder = ParallelSARDecoderWithBS(
beam_width=1, num_classes=37, padding_idx=36, max_seq_len=5)
decoder.init_weights()
decoder.train()
with pytest.raises(AssertionError):
decoder(feat, out_enc, tgt_dict, [])
with pytest.raises(AssertionError):
decoder(feat, out_enc, tgt_dict, img_metas * 2)
out_test = decoder(feat, out_enc, tgt_dict, img_metas, train_mode=False)
assert out_test.shape == torch.Size([1, 5, 36])
# test decodenode
with pytest.raises(AssertionError):
DecodeNode(1, 1)
with pytest.raises(AssertionError):
DecodeNode([1, 2], ['4', '3'])
with pytest.raises(AssertionError):
DecodeNode([1, 2], [0.5])
decode_node = DecodeNode([1, 2], [0.7, 0.8])
assert math.isclose(decode_node.eval(), 1.5)
def test_transformer_decoder():
decoder = NRTRDecoder(num_classes=37, padding_idx=36, max_seq_len=5)
decoder.init_weights()
decoder.train()
out_enc = torch.rand(1, 25, 512)
tgt_dict = {'padded_targets': torch.LongTensor([[1, 1, 1, 1, 36]])}
img_metas = [{'valid_ratio': 1.0}]
tgt_dict['padded_targets'] = tgt_dict['padded_targets']
out_train = decoder(None, out_enc, tgt_dict, img_metas, True)
assert out_train.shape == torch.Size([1, 5, 36])
out_test = decoder(None, out_enc, tgt_dict, img_metas, False)
assert out_test.shape == torch.Size([1, 5, 36])
def test_abi_language_decoder():
decoder = ABILanguageDecoder(max_seq_len=25)
logits = torch.randn(2, 25, 90)
result = decoder(
feat=None, out_enc=logits, targets_dict=None, img_metas=None)
assert result['feature'].shape == torch.Size([2, 25, 512])
assert result['logits'].shape == torch.Size([2, 25, 90])
def test_abi_vision_decoder():
model = ABIVisionDecoder(
in_channels=128, num_channels=16, max_seq_len=10, use_result=None)
x = torch.randn(2, 128, 8, 32)
result = model(x, None)
assert result['feature'].shape == torch.Size([2, 10, 128])
assert result['logits'].shape == torch.Size([2, 10, 90])
assert result['attn_scores'].shape == torch.Size([2, 10, 8, 32])
|