Download model.py from DaviNeg/Identificador_de_Formas_Geometricas: direct link, hf CLI and curl.
- Browser
- Download file 1.37 kB
-
https://huggingface.co/DaviNeg/Identificador_de_Formas_Geometricas/resolve/main/model.py
- Command line
-
hf download hf://DaviNeg/Identificador_de_Formas_Geometricas/model.py
-
curl -L -o model.py https://huggingface.co/DaviNeg/Identificador_de_Formas_Geometricas/resolve/main/model.py
1.37 kB
| """Arquitetura e pré-processamento do classificador de formas.""" | |
| import torch | |
| from torch import nn | |
| from torchvision import transforms | |
| from huggingface_hub import PyTorchModelHubMixin | |
| CLASSES = ("circulo", "triangulo", "quadrado") | |
| IMAGE_SIZE = 64 | |
| class ShapeMLP(nn.Module, PyTorchModelHubMixin): | |
| """MLP para uma imagem (B, 1, 64, 64), com saída em logits (B, 3).""" | |
| def __init__( | |
| self, | |
| input_dim: int = 4096, | |
| hidden_dims: tuple[int, int, int] = (512, 256, 128), | |
| num_classes: int = 3, | |
| dropout_p: float = 0.4, | |
| ) -> None: | |
| super().__init__() | |
| h1, h2, h3 = hidden_dims | |
| self.model = nn.Sequential( | |
| nn.Linear(input_dim, h1), nn.BatchNorm1d(h1), nn.ReLU(), nn.Dropout(dropout_p), | |
| nn.Linear(h1, h2), nn.BatchNorm1d(h2), nn.ReLU(), nn.Dropout(dropout_p), | |
| nn.Linear(h2, h3), nn.BatchNorm1d(h3), nn.ReLU(), nn.Dropout(dropout_p), | |
| nn.Linear(h3, num_classes), | |
| ) | |
| def forward(self, x: torch.Tensor) -> torch.Tensor: | |
| return self.model(x.reshape(x.size(0), -1)) | |
| def evaluation_transform(): | |
| """Mesmo pré-processamento usado em validação e teste.""" | |
| return transforms.Compose([ | |
| transforms.Resize((IMAGE_SIZE, IMAGE_SIZE)), | |
| transforms.ToTensor(), | |
| transforms.Normalize(mean=(0.5,), std=(0.5,)), | |
| ]) | |