File size: 5,181 Bytes
cd921bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d34cc8
cd921bb
 
 
 
 
 
 
 
 
 
474404c
cd921bb
 
f27bd8f
cd921bb
 
 
 
 
 
3d34cc8
 
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import csv
from pathlib import Path

import torch
import torch.nn as nn
from datasets import Dataset, Image
from huggingface_hub import PyTorchModelHubMixin
from torch import Tensor
from torch.utils.data import DataLoader


class DetectionHeads(nn.Module):
    def __init__(self, input_dim: int, class_num: int):
        super().__init__()
        self.heads = nn.ModuleList(
            [
                nn.Sequential(
                    nn.Linear(input_dim, 64),
                    nn.GELU(),
                    nn.Linear(64, 32),
                    nn.GELU(),
                    nn.Linear(32, 16),
                    nn.GELU(),
                    nn.Linear(16, 8),
                    nn.GELU(),
                )
                for _ in range(4)
            ]
        )
        self.proj = nn.Linear(8, class_num)

    def forward(self, x: Tensor) -> Tensor:
        # x: (batch_size, input_dim)
        # output: (batch_size, 4, class_num)
        y = torch.stack([self.proj(self.heads[i](x)) for i in range(4)], dim=1)
        return y


class Baseline2024(nn.Module, PyTorchModelHubMixin):
    def __init__(
        self,
        class_num: int = 26 + 10 + 3,
        n_channels: int = 32,
        p_dropout: float = 0.95,
    ):
        super().__init__()
        self.act = nn.GELU()
        self.pool2d = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
        self.pool1d = nn.MaxPool2d(kernel_size=(1, 2), stride=(1, 2), padding=0)

        self.conv1 = nn.Conv2d(3, n_channels, kernel_size=3, stride=1, padding=1)
        self.bn1 = nn.BatchNorm2d(n_channels)
        self.conv2 = nn.Conv2d(
            n_channels, n_channels * 2, kernel_size=3, stride=1, padding=1
        )
        self.bn2 = nn.BatchNorm2d(n_channels * 2)
        self.conv3 = nn.Conv2d(
            n_channels * 2, n_channels * 4, kernel_size=3, stride=1, padding=1
        )
        self.bn3 = nn.BatchNorm2d(n_channels * 4)
        self.conv4 = nn.Conv2d(
            n_channels * 4, n_channels * 8, kernel_size=3, stride=1, padding=1
        )
        self.bn4 = nn.BatchNorm2d(n_channels * 8)
        self.conv5 = nn.Conv2d(
            n_channels * 8, n_channels * 16, kernel_size=3, stride=1, padding=1
        )
        self.bn5 = nn.BatchNorm2d(n_channels * 16)
        self.conv6 = nn.Conv2d(
            n_channels * 16, n_channels * 32, kernel_size=3, stride=1, padding=1
        )
        self.bn6 = nn.BatchNorm2d(n_channels * 32)

        self.flatten = nn.Flatten()
        self.dropout = nn.Dropout(p_dropout)
        self.heads = DetectionHeads(n_channels * 32, class_num)

    def forward(self, x: Tensor) -> Tensor:
        # x: (batch_size, 3, 30, 108)
        # output: (batch_size, 4, class_num)
        x = self.conv1(x)
        x = self.act(x)
        x = self.pool2d(x)
        x = self.bn1(x)

        x = self.conv2(x)
        x = self.act(x)
        x = self.pool2d(x)
        x = self.bn2(x)

        x = self.conv3(x)
        x = self.act(x)
        x = self.pool2d(x)
        x = self.bn3(x)

        x = self.conv4(x)
        x = self.act(x)
        x = self.pool2d(x)
        x = self.bn4(x)

        x = self.conv5(x)
        x = self.act(x)
        x = self.pool1d(x)
        x = self.bn5(x)

        x = self.conv6(x)
        x = self.act(x)
        x = self.pool1d(x)
        x = self.bn6(x)

        x = self.flatten(x)
        x = self.dropout(x)
        x = self.heads(x)
        return x


char_dict = {
    "0": 0,
    "1": 1,
    "2": 2,
    "3": 3,
    "4": 4,
    "5": 5,
    "6": 6,
    "7": 7,
    "8": 8,
    "9": 9,
    "-": 10,
    "+": 11,
    "=": 12,
    "a": 13,
    "b": 14,
    "c": 15,
    "d": 16,
    "e": 17,
    "f": 18,
    "g": 19,
    "h": 20,
    "i": 21,
    "j": 22,
    "k": 23,
    "l": 24,
    "m": 25,
    "n": 26,
    "o": 27,
    "p": 28,
    "q": 29,
    "r": 30,
    "s": 31,
    "t": 32,
    "u": 33,
    "v": 34,
    "w": 35,
    "x": 36,
    "y": 37,
    "z": 38,
}

char_dict_rev = {v: k for k, v in char_dict.items()}


def tensor_to_text(tensor: torch.Tensor) -> str:
    text = ""
    for i in tensor:
        text += char_dict_rev[torch.argmax(i).item()]
    return text


def tensors_to_texts(tensors: torch.Tensor) -> list[str]:
    texts = []
    for tensor in tensors:
        texts.append(tensor_to_text(tensor))
    return texts


if __name__ == "__main__":
    model = Baseline2024.from_pretrained("./")
    dir = Path("/tmp/data/test-data")
    captchas = [str(captcha) for captcha in dir.glob("*.jpg")]

    dataset = (
        Dataset.from_dict({"image": captchas, "path": captchas})
        .cast_column("image", Image())
        .with_format("torch")
    )
    loader = DataLoader(dataset, batch_size=16)

    model.eval()

    submission = "submission.csv"
    with open(submission, "w") as f, torch.no_grad():
        writer = csv.writer(f)
        writer.writerow(["filename", "text"])

        for batch in loader:
            image = batch["image"].float() / 255.0
            output = model(image)
            texts = tensors_to_texts(output)
            for i, text in enumerate(texts):
                file = Path(batch["path"][i]).name
                writer.writerow([file, text])