Download modeling_captcha.py from jtrecenti/captcha: direct link, hf CLI and curl.
- Browser
- Download file 2.42 kB
-
https://huggingface.co/jtrecenti/captcha/resolve/main/modeling_captcha.py
- Command line
-
hf download hf://jtrecenti/captcha/modeling_captcha.py
-
curl -L -o modeling_captcha.py https://huggingface.co/jtrecenti/captcha/resolve/main/modeling_captcha.py
2.42 kB
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| from transformers import PreTrainedModel, PretrainedConfig | |
| class CaptchaConfig(PretrainedConfig): | |
| model_type = "captcha" # Importante para o Hugging Face reconhecer | |
| def __init__(self, input_dim=(40, 110), output_ndigits=5, output_vocab_size=10, vocab=None, **kwargs): | |
| super().__init__(**kwargs) | |
| self.input_dim = input_dim | |
| self.output_ndigits = output_ndigits | |
| self.output_vocab_size = output_vocab_size | |
| self.vocab = vocab if vocab else [str(i) for i in range(10)] | |
| class CaptchaModel(PreTrainedModel): | |
| config_class = CaptchaConfig | |
| model_type = "captcha" # Importante para o Hugging Face reconhecer | |
| def __init__(self, config): | |
| super().__init__(config) | |
| self.vocab = config.vocab | |
| self.output_ndigits = config.output_ndigits | |
| self.output_vocab_size = config.output_vocab_size | |
| self.batchnorm0 = nn.BatchNorm2d(3) | |
| self.conv1 = nn.Conv2d(3, 32, kernel_size=3) | |
| self.batchnorm1 = nn.BatchNorm2d(32) | |
| self.conv2 = nn.Conv2d(32, 64, kernel_size=3) | |
| self.batchnorm2 = nn.BatchNorm2d(64) | |
| self.conv3 = nn.Conv2d(64, 64, kernel_size=3) | |
| self.batchnorm3 = nn.BatchNorm2d(64) | |
| self.dropout1 = nn.Dropout(0.25) | |
| self.dropout2 = nn.Dropout(0.5) | |
| def calc_dim(x): | |
| for _ in range(3): | |
| x = (x - 2) // 2 | |
| return x | |
| conv_h = calc_dim(config.input_dim[0]) | |
| conv_w = calc_dim(config.input_dim[1]) | |
| fc1_in_features = conv_h * conv_w * 64 | |
| self.fc1 = nn.Linear(fc1_in_features, 200) | |
| self.batchnorm_dense = nn.BatchNorm1d(200) | |
| self.fc2 = nn.Linear(200, self.output_vocab_size * self.output_ndigits) | |
| def forward(self, pixel_values): | |
| x = self.batchnorm0(pixel_values) | |
| x = F.relu(self.batchnorm1(F.max_pool2d(self.conv1(x), 2))) | |
| x = F.relu(self.batchnorm2(F.max_pool2d(self.conv2(x), 2))) | |
| x = F.relu(self.batchnorm3(F.max_pool2d(self.conv3(x), 2))) | |
| x = torch.flatten(x, start_dim=1) | |
| x = self.dropout1(x) | |
| x = F.relu(self.batchnorm_dense(self.fc1(x))) | |
| x = self.dropout2(x) | |
| logits = self.fc2(x) | |
| logits = logits.view(-1, self.output_ndigits, self.output_vocab_size) | |
| return logits | |