Files
rapidocr/python/rapidocr_torch/modeling/heads/cls_head.py
T
Joker1212 9a8a4a3916 suport torch inference (#320)
* feat: add rapidocr_torch inference

* ci: fix push workflow
2025-02-08 09:52:34 +08:00

26 lines
647 B
Python

import os, sys
import torch
import torch.nn as nn
import torch.nn.functional as F
class ClsHead(nn.Module):
"""
Class orientation
Args:
params(dict): super parameters for build Class network
"""
def __init__(self, in_channels, class_dim, **kwargs):
super(ClsHead, self).__init__()
self.pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Linear(
in_channels,
class_dim,
bias=True)
def forward(self, x):
x = self.pool(x)
x = torch.reshape(x, shape=[x.shape[0], x.shape[1]])
x = self.fc(x)
x = F.softmax(x, dim=1)
return x