|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
from typing import Sequence, Any, Optional |
|
|
|
import torch |
|
from pytorch_lightning.utilities.types import STEP_OUTPUT |
|
from torch import Tensor |
|
|
|
from strhub.models.base import CrossEntropySystem |
|
from strhub.models.utils import init_weights |
|
from .model import ViTSTR as Model |
|
|
|
|
|
class ViTSTR(CrossEntropySystem): |
|
|
|
def __init__(self, charset_train: str, charset_test: str, max_label_length: int, |
|
batch_size: int, lr: float, warmup_pct: float, weight_decay: float, |
|
img_size: Sequence[int], patch_size: Sequence[int], embed_dim: int, num_heads: int, |
|
**kwargs: Any) -> None: |
|
super().__init__(charset_train, charset_test, batch_size, lr, warmup_pct, weight_decay) |
|
self.save_hyperparameters() |
|
self.max_label_length = max_label_length |
|
|
|
self.model = Model(img_size=img_size, patch_size=patch_size, depth=12, mlp_ratio=4, qkv_bias=True, |
|
embed_dim=embed_dim, num_heads=num_heads, num_classes=len(self.tokenizer) - 2) |
|
|
|
self.model.head.apply(init_weights) |
|
|
|
@torch.jit.ignore |
|
def no_weight_decay(self): |
|
return {'model.' + n for n in self.model.no_weight_decay()} |
|
|
|
def forward(self, images: Tensor, max_length: Optional[int] = None) -> Tensor: |
|
max_length = self.max_label_length if max_length is None else min(max_length, self.max_label_length) |
|
logits = self.model.forward(images, max_length + 2) |
|
|
|
|
|
logits = logits[:, 1:] |
|
return logits |
|
|
|
def training_step(self, batch, batch_idx) -> STEP_OUTPUT: |
|
images, labels = batch |
|
loss = self.forward_logits_loss(images, labels)[1] |
|
self.log('loss', loss) |
|
return loss |
|
|