python-doctr 1.0.1__py3-none-any.whl → 1.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- doctr/cli/__init__.py +0 -0
- doctr/cli/main.py +506 -0
- doctr/datasets/__init__.py +2 -0
- doctr/datasets/coco_text.py +4 -2
- doctr/datasets/cord.py +2 -1
- doctr/datasets/datasets/base.py +12 -24
- doctr/datasets/datasets/pytorch.py +14 -4
- doctr/datasets/detection.py +2 -1
- doctr/datasets/doc_artefacts.py +2 -1
- doctr/datasets/funsd.py +2 -1
- doctr/datasets/generator/base.py +5 -4
- doctr/datasets/generator/pytorch.py +4 -5
- doctr/datasets/ic03.py +3 -2
- doctr/datasets/ic13.py +4 -2
- doctr/datasets/iiit5k.py +2 -1
- doctr/datasets/iiithws.py +4 -2
- doctr/datasets/imgur5k.py +4 -2
- doctr/datasets/layout.py +123 -0
- doctr/datasets/mjsynth.py +4 -2
- doctr/datasets/ocr.py +2 -1
- doctr/datasets/orientation.py +2 -1
- doctr/datasets/recognition.py +2 -1
- doctr/datasets/sroie.py +2 -1
- doctr/datasets/svhn.py +2 -1
- doctr/datasets/svt.py +3 -2
- doctr/datasets/synthtext.py +2 -1
- doctr/datasets/table_structure.py +97 -0
- doctr/datasets/utils.py +14 -5
- doctr/datasets/vocabs.py +2 -0
- doctr/datasets/wildreceipt.py +4 -2
- doctr/file_utils.py +3 -1
- doctr/io/__init__.py +1 -0
- doctr/io/elements.py +252 -260
- doctr/io/exporters.py +1275 -0
- doctr/io/image/base.py +3 -2
- doctr/io/image/pytorch.py +5 -3
- doctr/models/__init__.py +2 -0
- doctr/models/_utils.py +56 -8
- doctr/models/builder.py +532 -61
- doctr/models/classification/__init__.py +2 -0
- doctr/models/classification/magc_resnet/pytorch.py +1 -1
- doctr/models/classification/mobilenet/pytorch.py +6 -6
- doctr/models/classification/predictor/pytorch.py +6 -2
- doctr/models/classification/resnet/pytorch.py +5 -5
- doctr/models/classification/starnet/__init__.py +1 -0
- doctr/models/classification/starnet/pytorch.py +202 -0
- doctr/models/classification/textnet/pytorch.py +6 -5
- doctr/models/classification/vgg/pytorch.py +3 -2
- doctr/models/classification/vip/layers/pytorch.py +4 -4
- doctr/models/classification/vip/pytorch.py +4 -4
- doctr/models/classification/vit/pytorch.py +3 -3
- doctr/models/classification/vit_det/__init__.py +1 -0
- doctr/models/classification/vit_det/layers/__init__.py +1 -0
- doctr/models/classification/vit_det/layers/pytorch.py +215 -0
- doctr/models/classification/vit_det/pytorch.py +346 -0
- doctr/models/classification/zoo.py +3 -0
- doctr/models/detection/_utils/base.py +23 -7
- doctr/models/detection/core.py +33 -18
- doctr/models/detection/differentiable_binarization/base.py +6 -4
- doctr/models/detection/differentiable_binarization/pytorch.py +3 -3
- doctr/models/detection/fast/base.py +5 -1
- doctr/models/detection/fast/pytorch.py +3 -3
- doctr/models/detection/linknet/base.py +5 -1
- doctr/models/detection/linknet/pytorch.py +3 -3
- doctr/models/detection/predictor/pytorch.py +3 -2
- doctr/models/detection/zoo.py +2 -2
- doctr/models/factory/hub.py +56 -41
- doctr/models/kie_predictor/base.py +16 -1
- doctr/models/kie_predictor/pytorch.py +41 -10
- doctr/models/layout/__init__.py +2 -0
- doctr/models/layout/lw_detr/__init__.py +1 -0
- doctr/models/layout/lw_detr/base.py +309 -0
- doctr/models/layout/lw_detr/layers/__init__.py +1 -0
- doctr/models/layout/lw_detr/layers/pytorch.py +785 -0
- doctr/models/layout/lw_detr/pytorch.py +838 -0
- doctr/models/layout/predictor/__init__.py +1 -0
- doctr/models/layout/predictor/pytorch.py +82 -0
- doctr/models/layout/zoo.py +90 -0
- doctr/models/modules/layers/pytorch.py +139 -3
- doctr/models/predictor/base.py +137 -15
- doctr/models/predictor/pytorch.py +150 -8
- doctr/models/preprocessor/pytorch.py +77 -22
- doctr/models/reading_order/__init__.py +1 -0
- doctr/models/reading_order/base.py +667 -0
- doctr/models/recognition/crnn/pytorch.py +5 -7
- doctr/models/recognition/master/pytorch.py +6 -7
- doctr/models/recognition/parseq/pytorch.py +4 -4
- doctr/models/recognition/predictor/pytorch.py +1 -1
- doctr/models/recognition/sar/pytorch.py +1 -1
- doctr/models/recognition/utils.py +8 -10
- doctr/models/recognition/viptr/pytorch.py +3 -5
- doctr/models/recognition/vitstr/pytorch.py +5 -5
- doctr/models/recognition/zoo.py +5 -6
- doctr/models/table_structure/__init__.py +2 -0
- doctr/models/table_structure/predictor/__init__.py +1 -0
- doctr/models/table_structure/predictor/pytorch.py +80 -0
- doctr/models/table_structure/tablecenternet/__init__.py +2 -0
- doctr/models/table_structure/tablecenternet/base.py +485 -0
- doctr/models/table_structure/tablecenternet/pytorch.py +522 -0
- doctr/models/table_structure/zoo.py +82 -0
- doctr/models/utils/pytorch.py +314 -15
- doctr/models/zoo.py +90 -2
- doctr/transforms/modules/base.py +247 -82
- doctr/transforms/modules/pytorch.py +212 -73
- doctr/utils/common_types.py +21 -1
- doctr/utils/data.py +4 -2
- doctr/utils/fonts.py +55 -16
- doctr/utils/geometry.py +135 -18
- doctr/utils/metrics.py +350 -18
- doctr/utils/multithreading.py +5 -2
- doctr/utils/reconstitution.py +696 -132
- doctr/utils/visualization.py +62 -3
- doctr/version.py +1 -1
- {python_doctr-1.0.1.dist-info → python_doctr-1.1.0.dist-info}/METADATA +67 -27
- python_doctr-1.1.0.dist-info/RECORD +179 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.1.0.dist-info}/WHEEL +1 -1
- python_doctr-1.1.0.dist-info/entry_points.txt +2 -0
- python_doctr-1.0.1.dist-info/RECORD +0 -149
- {python_doctr-1.0.1.dist-info → python_doctr-1.1.0.dist-info}/licenses/LICENSE +0 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.1.0.dist-info}/top_level.txt +0 -0
- {python_doctr-1.0.1.dist-info → python_doctr-1.1.0.dist-info}/zip-safe +0 -0
|
@@ -152,7 +152,7 @@ def magc_resnet31(pretrained: bool = False, **kwargs: Any) -> ResNet:
|
|
|
152
152
|
>>> out = model(input_tensor)
|
|
153
153
|
|
|
154
154
|
Args:
|
|
155
|
-
pretrained:
|
|
155
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
156
156
|
**kwargs: keyword arguments of the ResNet architecture
|
|
157
157
|
|
|
158
158
|
Returns:
|
|
@@ -137,7 +137,7 @@ def mobilenet_v3_small(pretrained: bool = False, **kwargs: Any) -> mobilenetv3.M
|
|
|
137
137
|
>>> out = model(input_tensor)
|
|
138
138
|
|
|
139
139
|
Args:
|
|
140
|
-
pretrained:
|
|
140
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
141
141
|
**kwargs: keyword arguments of the MobileNetV3 architecture
|
|
142
142
|
|
|
143
143
|
Returns:
|
|
@@ -160,7 +160,7 @@ def mobilenet_v3_small_r(pretrained: bool = False, **kwargs: Any) -> mobilenetv3
|
|
|
160
160
|
>>> out = model(input_tensor)
|
|
161
161
|
|
|
162
162
|
Args:
|
|
163
|
-
pretrained:
|
|
163
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
164
164
|
**kwargs: keyword arguments of the MobileNetV3 architecture
|
|
165
165
|
|
|
166
166
|
Returns:
|
|
@@ -187,7 +187,7 @@ def mobilenet_v3_large(pretrained: bool = False, **kwargs: Any) -> mobilenetv3.M
|
|
|
187
187
|
>>> out = model(input_tensor)
|
|
188
188
|
|
|
189
189
|
Args:
|
|
190
|
-
pretrained:
|
|
190
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
191
191
|
**kwargs: keyword arguments of the MobileNetV3 architecture
|
|
192
192
|
|
|
193
193
|
Returns:
|
|
@@ -213,7 +213,7 @@ def mobilenet_v3_large_r(pretrained: bool = False, **kwargs: Any) -> mobilenetv3
|
|
|
213
213
|
>>> out = model(input_tensor)
|
|
214
214
|
|
|
215
215
|
Args:
|
|
216
|
-
pretrained:
|
|
216
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
217
217
|
**kwargs: keyword arguments of the MobileNetV3 architecture
|
|
218
218
|
|
|
219
219
|
Returns:
|
|
@@ -240,7 +240,7 @@ def mobilenet_v3_small_crop_orientation(pretrained: bool = False, **kwargs: Any)
|
|
|
240
240
|
>>> out = model(input_tensor)
|
|
241
241
|
|
|
242
242
|
Args:
|
|
243
|
-
pretrained:
|
|
243
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
244
244
|
**kwargs: keyword arguments of the MobileNetV3 architecture
|
|
245
245
|
|
|
246
246
|
Returns:
|
|
@@ -266,7 +266,7 @@ def mobilenet_v3_small_page_orientation(pretrained: bool = False, **kwargs: Any)
|
|
|
266
266
|
>>> out = model(input_tensor)
|
|
267
267
|
|
|
268
268
|
Args:
|
|
269
|
-
pretrained:
|
|
269
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
270
270
|
**kwargs: keyword arguments of the MobileNetV3 architecture
|
|
271
271
|
|
|
272
272
|
Returns:
|
|
@@ -37,6 +37,9 @@ class OrientationPredictor(nn.Module):
|
|
|
37
37
|
self,
|
|
38
38
|
inputs: list[np.ndarray],
|
|
39
39
|
) -> list[list[int] | list[float]]:
|
|
40
|
+
if len(inputs) == 0:
|
|
41
|
+
return [[], [], []]
|
|
42
|
+
|
|
40
43
|
# Dimension check
|
|
41
44
|
if any(input.ndim != 3 for input in inputs):
|
|
42
45
|
raise ValueError("incorrect input shape: all inputs are expected to be multi-channel 2D images.")
|
|
@@ -47,13 +50,14 @@ class OrientationPredictor(nn.Module):
|
|
|
47
50
|
|
|
48
51
|
processed_batches = self.pre_processor(inputs)
|
|
49
52
|
_params = next(self.model.parameters())
|
|
50
|
-
self.model, processed_batches = set_device_and_dtype(
|
|
53
|
+
self.model, processed_batches = set_device_and_dtype( # type: ignore[assignment]
|
|
51
54
|
self.model, processed_batches, _params.device, _params.dtype
|
|
52
55
|
)
|
|
53
56
|
predicted_batches = [self.model(batch) for batch in processed_batches]
|
|
54
57
|
# confidence
|
|
55
58
|
probs = [
|
|
56
|
-
torch.max(torch.softmax(batch, dim=1), dim=1).values.cpu().detach().numpy()
|
|
59
|
+
torch.max(torch.softmax(batch.float(), dim=1), dim=1).values.cpu().detach().numpy()
|
|
60
|
+
for batch in predicted_batches
|
|
57
61
|
]
|
|
58
62
|
# Postprocess predictions
|
|
59
63
|
predicted_batches = [out_batch.argmax(dim=1).cpu().detach().numpy() for out_batch in predicted_batches]
|
|
@@ -247,7 +247,7 @@ def resnet18(pretrained: bool = False, **kwargs: Any) -> TVResNet:
|
|
|
247
247
|
>>> out = model(input_tensor)
|
|
248
248
|
|
|
249
249
|
Args:
|
|
250
|
-
pretrained:
|
|
250
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
251
251
|
**kwargs: keyword arguments of the ResNet architecture
|
|
252
252
|
|
|
253
253
|
Returns:
|
|
@@ -274,7 +274,7 @@ def resnet31(pretrained: bool = False, **kwargs: Any) -> ResNet:
|
|
|
274
274
|
>>> out = model(input_tensor)
|
|
275
275
|
|
|
276
276
|
Args:
|
|
277
|
-
pretrained:
|
|
277
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
278
278
|
**kwargs: keyword arguments of the ResNet architecture
|
|
279
279
|
|
|
280
280
|
Returns:
|
|
@@ -306,7 +306,7 @@ def resnet34(pretrained: bool = False, **kwargs: Any) -> TVResNet:
|
|
|
306
306
|
>>> out = model(input_tensor)
|
|
307
307
|
|
|
308
308
|
Args:
|
|
309
|
-
pretrained:
|
|
309
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
310
310
|
**kwargs: keyword arguments of the ResNet architecture
|
|
311
311
|
|
|
312
312
|
Returns:
|
|
@@ -332,7 +332,7 @@ def resnet34_wide(pretrained: bool = False, **kwargs: Any) -> ResNet:
|
|
|
332
332
|
>>> out = model(input_tensor)
|
|
333
333
|
|
|
334
334
|
Args:
|
|
335
|
-
pretrained:
|
|
335
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
336
336
|
**kwargs: keyword arguments of the ResNet architecture
|
|
337
337
|
|
|
338
338
|
Returns:
|
|
@@ -364,7 +364,7 @@ def resnet50(pretrained: bool = False, **kwargs: Any) -> TVResNet:
|
|
|
364
364
|
>>> out = model(input_tensor)
|
|
365
365
|
|
|
366
366
|
Args:
|
|
367
|
-
pretrained:
|
|
367
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
368
368
|
**kwargs: keyword arguments of the ResNet architecture
|
|
369
369
|
|
|
370
370
|
Returns:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .pytorch import *
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
# Copyright (C) 2021-2026, Mindee.
|
|
2
|
+
|
|
3
|
+
# This program is licensed under the Apache License 2.0.
|
|
4
|
+
# See LICENSE or go to <https://opensource.org/licenses/Apache-2.0> for full license details.
|
|
5
|
+
|
|
6
|
+
# Credits: architecture adapted from "Rewrite the Stars" (https://github.com/ma-xu/Rewrite-the-Stars) as
|
|
7
|
+
# used by TableCenterNet (https://github.com/dreamy-xay/TableCenterNet).
|
|
8
|
+
|
|
9
|
+
from copy import deepcopy
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import torch
|
|
13
|
+
from torch import nn
|
|
14
|
+
from torch.nn.init import trunc_normal_
|
|
15
|
+
|
|
16
|
+
from doctr.datasets import VOCABS
|
|
17
|
+
|
|
18
|
+
from ...modules.layers import DropPath
|
|
19
|
+
from ...utils import load_pretrained_params
|
|
20
|
+
|
|
21
|
+
__all__ = ["StarNet", "starnet_s3"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
default_cfgs: dict[str, dict[str, Any]] = {
|
|
25
|
+
"starnet_s3": {
|
|
26
|
+
"mean": (0.694, 0.695, 0.693),
|
|
27
|
+
"std": (0.299, 0.296, 0.301),
|
|
28
|
+
"input_shape": (3, 32, 32),
|
|
29
|
+
"classes": list(VOCABS["french"]),
|
|
30
|
+
"url": "https://github.com/mindee/doctr/releases/download/v1.0.1/starnet_s3-a413415c.pt",
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class ConvBN(nn.Sequential):
|
|
36
|
+
"""A convolution optionally followed by batch-norm (the conv keeps its bias, as in the reference)."""
|
|
37
|
+
|
|
38
|
+
def __init__(
|
|
39
|
+
self,
|
|
40
|
+
in_planes: int,
|
|
41
|
+
out_planes: int,
|
|
42
|
+
kernel_size: int = 1,
|
|
43
|
+
stride: int = 1,
|
|
44
|
+
padding: int = 0,
|
|
45
|
+
dilation: int = 1,
|
|
46
|
+
groups: int = 1,
|
|
47
|
+
with_bn: bool = True,
|
|
48
|
+
):
|
|
49
|
+
super().__init__()
|
|
50
|
+
self.add_module("conv", nn.Conv2d(in_planes, out_planes, kernel_size, stride, padding, dilation, groups))
|
|
51
|
+
if with_bn:
|
|
52
|
+
bn = nn.BatchNorm2d(out_planes, momentum=0.1)
|
|
53
|
+
nn.init.constant_(bn.weight, 1)
|
|
54
|
+
nn.init.constant_(bn.bias, 0)
|
|
55
|
+
self.add_module("bn", bn)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class Block(nn.Module):
|
|
59
|
+
"""StarNet block: depth-wise conv, the element-wise "star" multiplication of two linear branches,
|
|
60
|
+
a projection and a residual connection."""
|
|
61
|
+
|
|
62
|
+
def __init__(self, dim: int, mlp_ratio: int = 4, drop_path: float = 0.0):
|
|
63
|
+
super().__init__()
|
|
64
|
+
self.dwconv = ConvBN(dim, dim, 7, 1, 3, groups=dim, with_bn=True)
|
|
65
|
+
self.f1 = ConvBN(dim, mlp_ratio * dim, 1, with_bn=False)
|
|
66
|
+
self.f2 = ConvBN(dim, mlp_ratio * dim, 1, with_bn=False)
|
|
67
|
+
self.g = ConvBN(mlp_ratio * dim, dim, 1, with_bn=True)
|
|
68
|
+
self.dwconv2 = ConvBN(dim, dim, 7, 1, 3, groups=dim, with_bn=False)
|
|
69
|
+
self.act = nn.ReLU6()
|
|
70
|
+
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity()
|
|
71
|
+
|
|
72
|
+
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
|
73
|
+
identity = x
|
|
74
|
+
x = self.dwconv(x)
|
|
75
|
+
x1, x2 = self.f1(x), self.f2(x)
|
|
76
|
+
x = self.act(x1) * x2
|
|
77
|
+
x = self.dwconv2(self.g(x))
|
|
78
|
+
return identity + self.drop_path(x)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class StarNet(nn.Sequential):
|
|
82
|
+
"""Implements the StarNet architecture from `"Rewrite the Stars" <https://arxiv.org/abs/2403.19967>`_.
|
|
83
|
+
|
|
84
|
+
Args:
|
|
85
|
+
base_dim: channel width of the first stage (doubles every stage)
|
|
86
|
+
depths: number of blocks in each of the four stages
|
|
87
|
+
mlp_ratio: expansion ratio of the star-multiplication branches
|
|
88
|
+
drop_path_rate: maximum stochastic-depth rate (linearly scaled across blocks)
|
|
89
|
+
stem_dim: number of channels produced by the stem
|
|
90
|
+
num_classes: number of output classes of the classification head
|
|
91
|
+
include_top: whether to add the classification head
|
|
92
|
+
cfg: the configuration dict of the model
|
|
93
|
+
"""
|
|
94
|
+
|
|
95
|
+
def __init__(
|
|
96
|
+
self,
|
|
97
|
+
base_dim: int = 32,
|
|
98
|
+
depths: tuple[int, int, int, int] = (2, 2, 8, 4),
|
|
99
|
+
mlp_ratio: int = 4,
|
|
100
|
+
drop_path_rate: float = 0.0,
|
|
101
|
+
stem_dim: int = 32,
|
|
102
|
+
num_classes: int = 1000,
|
|
103
|
+
include_top: bool = True,
|
|
104
|
+
cfg: dict[str, Any] | None = None,
|
|
105
|
+
) -> None:
|
|
106
|
+
stem = nn.Sequential(ConvBN(3, stem_dim, kernel_size=3, stride=2, padding=1), nn.ReLU6())
|
|
107
|
+
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, sum(depths))]
|
|
108
|
+
|
|
109
|
+
channels = [stem_dim]
|
|
110
|
+
stages: list[nn.Module] = []
|
|
111
|
+
in_channel = stem_dim
|
|
112
|
+
cur = 0
|
|
113
|
+
for i_layer, depth in enumerate(depths):
|
|
114
|
+
embed_dim = base_dim * 2**i_layer
|
|
115
|
+
channels.append(embed_dim)
|
|
116
|
+
down_sampler = ConvBN(in_channel, embed_dim, 3, 2, 1)
|
|
117
|
+
in_channel = embed_dim
|
|
118
|
+
blocks = [Block(in_channel, mlp_ratio, dpr[cur + i]) for i in range(depth)]
|
|
119
|
+
cur += depth
|
|
120
|
+
stages.append(nn.Sequential(down_sampler, *blocks))
|
|
121
|
+
|
|
122
|
+
_layers: list[nn.Module] = [stem, *stages]
|
|
123
|
+
if include_top:
|
|
124
|
+
_layers.append(
|
|
125
|
+
nn.Sequential(
|
|
126
|
+
nn.BatchNorm2d(in_channel, momentum=0.1),
|
|
127
|
+
nn.AdaptiveAvgPool2d(1),
|
|
128
|
+
nn.Flatten(1),
|
|
129
|
+
nn.Linear(in_channel, num_classes),
|
|
130
|
+
)
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
super().__init__(*_layers)
|
|
134
|
+
self.channels = channels # [stem_dim, base_dim, base_dim*2, base_dim*4, base_dim*8]
|
|
135
|
+
self.cfg = cfg
|
|
136
|
+
|
|
137
|
+
self.apply(self._init_weights)
|
|
138
|
+
|
|
139
|
+
@staticmethod
|
|
140
|
+
def _init_weights(m: nn.Module) -> None:
|
|
141
|
+
if isinstance(m, (nn.Linear, nn.Conv2d)):
|
|
142
|
+
trunc_normal_(m.weight, std=0.02)
|
|
143
|
+
if m.bias is not None:
|
|
144
|
+
nn.init.constant_(m.bias, 0)
|
|
145
|
+
elif isinstance(m, (nn.LayerNorm, nn.BatchNorm2d)):
|
|
146
|
+
nn.init.constant_(m.bias, 0)
|
|
147
|
+
nn.init.constant_(m.weight, 1.0)
|
|
148
|
+
|
|
149
|
+
def from_pretrained(self, path_or_url: str, **kwargs: Any) -> None:
|
|
150
|
+
"""Load pretrained parameters onto the model
|
|
151
|
+
|
|
152
|
+
Args:
|
|
153
|
+
path_or_url: the path or URL to the model parameters (checkpoint)
|
|
154
|
+
**kwargs: additional arguments to be passed to `doctr.models.utils.load_pretrained_params`
|
|
155
|
+
"""
|
|
156
|
+
load_pretrained_params(self, path_or_url, **kwargs)
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _starnet(
|
|
160
|
+
arch: str,
|
|
161
|
+
pretrained: bool,
|
|
162
|
+
ignore_keys: list[str] | None = None,
|
|
163
|
+
**kwargs: Any,
|
|
164
|
+
) -> StarNet:
|
|
165
|
+
kwargs["num_classes"] = kwargs.get("num_classes", len(default_cfgs[arch]["classes"]))
|
|
166
|
+
kwargs["classes"] = kwargs.get("classes", default_cfgs[arch]["classes"])
|
|
167
|
+
|
|
168
|
+
_cfg = deepcopy(default_cfgs[arch])
|
|
169
|
+
_cfg["num_classes"] = kwargs["num_classes"]
|
|
170
|
+
_cfg["classes"] = kwargs["classes"]
|
|
171
|
+
kwargs.pop("classes")
|
|
172
|
+
|
|
173
|
+
# Build the model
|
|
174
|
+
model = StarNet(**kwargs)
|
|
175
|
+
# Load pretrained parameters
|
|
176
|
+
if pretrained:
|
|
177
|
+
# The number of classes is not the same as the number of classes in the pretrained model =>
|
|
178
|
+
# remove the last layer weights
|
|
179
|
+
_ignore_keys = ignore_keys if kwargs["num_classes"] != len(default_cfgs[arch]["classes"]) else None
|
|
180
|
+
model.from_pretrained(default_cfgs[arch]["url"], ignore_keys=_ignore_keys)
|
|
181
|
+
|
|
182
|
+
model.cfg = _cfg
|
|
183
|
+
|
|
184
|
+
return model
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def starnet_s3(pretrained: bool = False, **kwargs: Any) -> StarNet:
|
|
188
|
+
"""StarNet-S3 from `"Rewrite the Stars" <https://arxiv.org/abs/2403.19967>`_.
|
|
189
|
+
|
|
190
|
+
>>> import torch
|
|
191
|
+
>>> from doctr.models import starnet_s3
|
|
192
|
+
>>> model = starnet_s3(pretrained=False)
|
|
193
|
+
>>> out = model(torch.rand((1, 3, 32, 32), dtype=torch.float32))
|
|
194
|
+
|
|
195
|
+
Args:
|
|
196
|
+
pretrained: boolean, True if model is pretrained
|
|
197
|
+
**kwargs: keyword arguments of the StarNet architecture
|
|
198
|
+
|
|
199
|
+
Returns:
|
|
200
|
+
A StarNet-S3 model
|
|
201
|
+
"""
|
|
202
|
+
return _starnet("starnet_s3", pretrained, ignore_keys=["5.3.weight", "5.3.bias"], **kwargs)
|
|
@@ -14,7 +14,7 @@ from doctr.datasets import VOCABS
|
|
|
14
14
|
from ...modules.layers import FASTConvLayer
|
|
15
15
|
from ...utils import conv_sequence_pt, load_pretrained_params
|
|
16
16
|
|
|
17
|
-
__all__ = ["textnet_tiny", "textnet_small", "textnet_base"]
|
|
17
|
+
__all__ = ["TextNet", "textnet_tiny", "textnet_small", "textnet_base"]
|
|
18
18
|
|
|
19
19
|
default_cfgs: dict[str, dict[str, Any]] = {
|
|
20
20
|
"textnet_tiny": {
|
|
@@ -48,6 +48,7 @@ class TextNet(nn.Sequential):
|
|
|
48
48
|
|
|
49
49
|
Args:
|
|
50
50
|
stages (list[dict[str, list[int]]]): list of dictionaries containing the parameters of each stage.
|
|
51
|
+
input_shape (tuple[int, int, int], optional): Input shape of the model. Defaults to (3, 32, 32).
|
|
51
52
|
include_top (bool, optional): Whether to include the classifier head. Defaults to True.
|
|
52
53
|
num_classes (int, optional): Number of output classes. Defaults to 1000.
|
|
53
54
|
cfg (dict[str, Any], optional): Additional configuration. Defaults to None.
|
|
@@ -63,7 +64,7 @@ class TextNet(nn.Sequential):
|
|
|
63
64
|
) -> None:
|
|
64
65
|
_layers: list[nn.Module] = [
|
|
65
66
|
*conv_sequence_pt(
|
|
66
|
-
in_channels=3, out_channels=64,
|
|
67
|
+
in_channels=3, out_channels=64, act=True, bn=True, kernel_size=3, stride=2, padding=(1, 1)
|
|
67
68
|
),
|
|
68
69
|
*[
|
|
69
70
|
nn.Sequential(*[
|
|
@@ -143,7 +144,7 @@ def textnet_tiny(pretrained: bool = False, **kwargs: Any) -> TextNet:
|
|
|
143
144
|
>>> out = model(input_tensor)
|
|
144
145
|
|
|
145
146
|
Args:
|
|
146
|
-
pretrained:
|
|
147
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
147
148
|
**kwargs: keyword arguments of the TextNet architecture
|
|
148
149
|
|
|
149
150
|
Returns:
|
|
@@ -190,7 +191,7 @@ def textnet_small(pretrained: bool = False, **kwargs: Any) -> TextNet:
|
|
|
190
191
|
>>> out = model(input_tensor)
|
|
191
192
|
|
|
192
193
|
Args:
|
|
193
|
-
pretrained:
|
|
194
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
194
195
|
**kwargs: keyword arguments of the TextNet architecture
|
|
195
196
|
|
|
196
197
|
Returns:
|
|
@@ -237,7 +238,7 @@ def textnet_base(pretrained: bool = False, **kwargs: Any) -> TextNet:
|
|
|
237
238
|
>>> out = model(input_tensor)
|
|
238
239
|
|
|
239
240
|
Args:
|
|
240
|
-
pretrained:
|
|
241
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
241
242
|
**kwargs: keyword arguments of the TextNet architecture
|
|
242
243
|
|
|
243
244
|
Returns:
|
|
@@ -9,12 +9,13 @@ from typing import Any
|
|
|
9
9
|
|
|
10
10
|
from torch import nn
|
|
11
11
|
from torchvision.models import vgg as tv_vgg
|
|
12
|
+
from torchvision.models.vgg import VGG
|
|
12
13
|
|
|
13
14
|
from doctr.datasets import VOCABS
|
|
14
15
|
|
|
15
16
|
from ...utils import load_pretrained_params
|
|
16
17
|
|
|
17
|
-
__all__ = ["vgg16_bn_r"]
|
|
18
|
+
__all__ = ["VGG", "vgg16_bn_r"]
|
|
18
19
|
|
|
19
20
|
|
|
20
21
|
default_cfgs: dict[str, dict[str, Any]] = {
|
|
@@ -92,7 +93,7 @@ def vgg16_bn_r(pretrained: bool = False, **kwargs: Any) -> tv_vgg.VGG:
|
|
|
92
93
|
>>> out = model(input_tensor)
|
|
93
94
|
|
|
94
95
|
Args:
|
|
95
|
-
pretrained
|
|
96
|
+
pretrained: If True, returns a model pre-trained on ImageNet
|
|
96
97
|
**kwargs: keyword arguments of the VGG architecture
|
|
97
98
|
|
|
98
99
|
Returns:
|
|
@@ -63,11 +63,11 @@ class PatchEmbed(nn.Module):
|
|
|
63
63
|
self.embed_dim = embed_dim
|
|
64
64
|
self.proj = nn.Sequential(
|
|
65
65
|
*conv_sequence_pt(
|
|
66
|
-
in_channels, embed_dim // 2, kernel_size=3, stride=2, padding=1, bias=False, bn=True,
|
|
66
|
+
in_channels, embed_dim // 2, kernel_size=3, stride=2, padding=1, bias=False, bn=True, act=False
|
|
67
67
|
),
|
|
68
68
|
nn.GELU(),
|
|
69
69
|
*conv_sequence_pt(
|
|
70
|
-
embed_dim // 2, embed_dim, kernel_size=3, stride=2, padding=1, bias=False, bn=True,
|
|
70
|
+
embed_dim // 2, embed_dim, kernel_size=3, stride=2, padding=1, bias=False, bn=True, act=False
|
|
71
71
|
),
|
|
72
72
|
nn.GELU(),
|
|
73
73
|
)
|
|
@@ -240,10 +240,10 @@ class OverlappedSpatialReductionAttention(nn.Module):
|
|
|
240
240
|
groups=dim,
|
|
241
241
|
bias=False,
|
|
242
242
|
bn=True,
|
|
243
|
-
|
|
243
|
+
act=False,
|
|
244
244
|
),
|
|
245
245
|
nn.GELU(),
|
|
246
|
-
*conv_sequence_pt(dim, dim, kernel_size=1, groups=dim, bias=False, bn=True,
|
|
246
|
+
*conv_sequence_pt(dim, dim, kernel_size=1, groups=dim, bias=False, bn=True, act=False),
|
|
247
247
|
)
|
|
248
248
|
else:
|
|
249
249
|
self.sr = nn.Identity() # type: ignore[assignment]
|
|
@@ -23,7 +23,7 @@ from .layers import (
|
|
|
23
23
|
SqueezeLayer,
|
|
24
24
|
)
|
|
25
25
|
|
|
26
|
-
__all__ = ["vip_tiny", "vip_base"]
|
|
26
|
+
__all__ = ["VIPNet", "vip_tiny", "vip_base"]
|
|
27
27
|
|
|
28
28
|
default_cfgs: dict[str, dict[str, Any]] = {
|
|
29
29
|
"vip_tiny": {
|
|
@@ -247,7 +247,7 @@ def vip_tiny(pretrained: bool = False, **kwargs: Any) -> VIPNet:
|
|
|
247
247
|
https://github.com/cxfyxl/VIPTR/blob/main/modules/VIPTRv2.py)
|
|
248
248
|
|
|
249
249
|
Args:
|
|
250
|
-
pretrained:
|
|
250
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
251
251
|
**kwargs: optional arguments
|
|
252
252
|
|
|
253
253
|
Returns:
|
|
@@ -276,7 +276,7 @@ def vip_base(pretrained: bool = False, **kwargs: Any) -> VIPNet:
|
|
|
276
276
|
https://github.com/cxfyxl/VIPTR/blob/main/modules/VIPTRv2.py)
|
|
277
277
|
|
|
278
278
|
Args:
|
|
279
|
-
pretrained:
|
|
279
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
280
280
|
**kwargs: optional arguments
|
|
281
281
|
|
|
282
282
|
Returns:
|
|
@@ -309,7 +309,7 @@ def _vip(
|
|
|
309
309
|
|
|
310
310
|
Args:
|
|
311
311
|
arch: architecture key
|
|
312
|
-
pretrained:
|
|
312
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
313
313
|
ignore_keys: layer keys to ignore
|
|
314
314
|
**kwargs: arguments passed to VIPNet
|
|
315
315
|
|
|
@@ -15,7 +15,7 @@ from doctr.models.modules.vision_transformer import PatchEmbedding
|
|
|
15
15
|
|
|
16
16
|
from ...utils import load_pretrained_params
|
|
17
17
|
|
|
18
|
-
__all__ = ["vit_s", "vit_b"]
|
|
18
|
+
__all__ = ["VisionTransformer", "vit_s", "vit_b"]
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
default_cfgs: dict[str, dict[str, Any]] = {
|
|
@@ -150,7 +150,7 @@ def vit_s(pretrained: bool = False, **kwargs: Any) -> VisionTransformer:
|
|
|
150
150
|
>>> out = model(input_tensor)
|
|
151
151
|
|
|
152
152
|
Args:
|
|
153
|
-
pretrained:
|
|
153
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
154
154
|
**kwargs: keyword arguments of the VisionTransformer architecture
|
|
155
155
|
|
|
156
156
|
Returns:
|
|
@@ -180,7 +180,7 @@ def vit_b(pretrained: bool = False, **kwargs: Any) -> VisionTransformer:
|
|
|
180
180
|
>>> out = model(input_tensor)
|
|
181
181
|
|
|
182
182
|
Args:
|
|
183
|
-
pretrained:
|
|
183
|
+
pretrained: If True, returns a model pre-trained on our classification dataset
|
|
184
184
|
**kwargs: keyword arguments of the VisionTransformer architecture
|
|
185
185
|
|
|
186
186
|
Returns:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .pytorch import *
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from .pytorch import *
|