File size: 2,423 Bytes
c29c374 | 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 | 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
|