mmocr/tests/test_models/test_ocr_encoder.py
LvTianlei 8f377f2fa2
[Feature] Add Satrn (#405)
* Add SATRN

* Create satrn_small_academic.py

* Update README.md

* change config name

* Update mmocr/models/textrecog/backbones/shallow_cnn.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update configs/textrecog/satrn/satrn_academic.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update configs/textrecog/satrn/satrn_small.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update shallow_cnn.py

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update test_ocr_encoder.py

* change keep_aspect_ratio=False

* Update transformer_layer.py

* Update configs/textrecog/satrn/satrn_small.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update configs/textrecog/satrn/satrn_academic.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update mmocr/models/textrecog/layers/transformer_layer.py

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update transformer_layer.py

* Apply suggestions from code review

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>

* Update transformer_layer.py

* update satrn readme

* add satrn to ocr.py

* add satrn_sm and fix configs

* add a test for config

* add copyright info

* use mmocr registry

Co-authored-by: Tong Gao <gaotongxiao@gmail.com>
2021-08-19 22:02:58 +08:00

65 lines
1.7 KiB
Python

# Copyright (c) OpenMMLab. All rights reserved.
import pytest
import torch
from mmocr.models.textrecog.encoders import (BaseEncoder, SAREncoder,
SatrnEncoder, TFEncoder)
def test_sar_encoder():
with pytest.raises(AssertionError):
SAREncoder(enc_bi_rnn='bi')
with pytest.raises(AssertionError):
SAREncoder(enc_do_rnn=2)
with pytest.raises(AssertionError):
SAREncoder(enc_gru='gru')
with pytest.raises(AssertionError):
SAREncoder(d_model=512.5)
with pytest.raises(AssertionError):
SAREncoder(d_enc=200.5)
with pytest.raises(AssertionError):
SAREncoder(mask='mask')
encoder = SAREncoder()
encoder.init_weights()
encoder.train()
feat = torch.randn(1, 512, 4, 40)
img_metas = [{'valid_ratio': 1.0}]
with pytest.raises(AssertionError):
encoder(feat, img_metas * 2)
out_enc = encoder(feat, img_metas)
assert out_enc.shape == torch.Size([1, 512])
def test_transformer_encoder():
tf_encoder = TFEncoder()
tf_encoder.init_weights()
tf_encoder.train()
feat = torch.randn(1, 512, 1, 25)
out_enc = tf_encoder(feat)
print('hello', out_enc.size())
assert out_enc.shape == torch.Size([1, 512, 1, 25])
def test_satrn_encoder():
satrn_encoder = SatrnEncoder()
satrn_encoder.init_weights()
satrn_encoder.train()
feat = torch.randn(1, 512, 8, 25)
out_enc = satrn_encoder(feat)
assert out_enc.shape == torch.Size([1, 512, 8, 25])
def test_base_encoder():
encoder = BaseEncoder()
encoder.init_weights()
encoder.train()
feat = torch.randn(1, 256, 4, 40)
out_enc = encoder(feat)
assert out_enc.shape == torch.Size([1, 256, 4, 40])