Spaces:
Sleeping
Sleeping
Upload 6 files
Browse files
custom_classifier/__init__.py
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
from .configuration import CustomModelConfig
|
2 |
+
from .model import CustomClassifier
|
custom_classifier/__pycache__/__init__.cpython-311.pyc
ADDED
Binary file (311 Bytes). View file
|
|
custom_classifier/__pycache__/configuration.cpython-311.pyc
ADDED
Binary file (1.12 kB). View file
|
|
custom_classifier/__pycache__/model.cpython-311.pyc
ADDED
Binary file (6.22 kB). View file
|
|
custom_classifier/configuration.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import PretrainedConfig
|
2 |
+
|
3 |
+
class CustomModelConfig(PretrainedConfig):
|
4 |
+
model_type = "custom"
|
5 |
+
input_size = (3, 128, 128)
|
6 |
+
num_classes = 2
|
7 |
+
|
8 |
+
def __init__(self, **kwargs):
|
9 |
+
super().__init__(**kwargs)
|
10 |
+
self.input_size = kwargs.get("input_size", self.input_size)
|
11 |
+
self.num_classes = kwargs.get("num_classes", self.num_classes)
|
custom_classifier/model.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torch import nn
|
3 |
+
from transformers import PreTrainedModel
|
4 |
+
from .configuration import CustomModelConfig
|
5 |
+
from torchvision import transforms
|
6 |
+
from PIL import Image
|
7 |
+
import sys
|
8 |
+
|
9 |
+
class CustomModel(nn.Module):
|
10 |
+
def __init__(self, input_shape, num_classes):
|
11 |
+
super(CustomModel, self).__init__()
|
12 |
+
|
13 |
+
self.conv_layers = nn.Sequential(
|
14 |
+
nn.Conv2d(in_channels=input_shape[0], out_channels=32, kernel_size=3, padding=1),
|
15 |
+
nn.ReLU(),
|
16 |
+
nn.BatchNorm2d(32),
|
17 |
+
nn.MaxPool2d(kernel_size=2),
|
18 |
+
nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, padding=1),
|
19 |
+
nn.ReLU(),
|
20 |
+
nn.BatchNorm2d(64),
|
21 |
+
nn.MaxPool2d(kernel_size=2),
|
22 |
+
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, padding=1),
|
23 |
+
nn.ReLU(),
|
24 |
+
nn.BatchNorm2d(128),
|
25 |
+
nn.MaxPool2d(kernel_size=2),
|
26 |
+
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=3, padding=1),
|
27 |
+
nn.ReLU(),
|
28 |
+
nn.BatchNorm2d(128),
|
29 |
+
nn.MaxPool2d(kernel_size=2)
|
30 |
+
)
|
31 |
+
|
32 |
+
self.fc_layers = nn.Sequential(
|
33 |
+
nn.Flatten(),
|
34 |
+
nn.Dropout(0.5),
|
35 |
+
nn.Linear(128 * (input_shape[1] // 16) * (input_shape[2] // 16), 512),
|
36 |
+
nn.ReLU(),
|
37 |
+
nn.BatchNorm1d(512),
|
38 |
+
nn.Dropout(0.5),
|
39 |
+
nn.Linear(512, num_classes)
|
40 |
+
)
|
41 |
+
|
42 |
+
def forward(self, x):
|
43 |
+
x = self.conv_layers(x)
|
44 |
+
x = self.fc_layers(x)
|
45 |
+
return x
|
46 |
+
|
47 |
+
class CustomClassifier(PreTrainedModel):
|
48 |
+
config_class = CustomModelConfig
|
49 |
+
|
50 |
+
|
51 |
+
def __init__(self, config):
|
52 |
+
super().__init__(config)
|
53 |
+
self.model = CustomModel(config.input_size, config.num_classes)
|
54 |
+
self.preprocess = transforms.Compose([
|
55 |
+
transforms.Resize((128, 128)),
|
56 |
+
transforms.ToTensor(),
|
57 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
58 |
+
])
|
59 |
+
self.classes = ['cat', 'dog']
|
60 |
+
|
61 |
+
def forward(self, x):
|
62 |
+
try:
|
63 |
+
x = Image.open(x).convert("RGB")
|
64 |
+
except Exception as e:
|
65 |
+
raise Exception(f"Error: Unable to load image file {x}. Check if the file exists or is in the right format. Details: {e}")
|
66 |
+
|
67 |
+
x = self.preprocess(x).unsqueeze(0)
|
68 |
+
|
69 |
+
|
70 |
+
return self.model(x)
|
71 |
+
|
72 |
+
def predict(self, x, get_class=False):
|
73 |
+
self.eval()
|
74 |
+
with torch.no_grad():
|
75 |
+
outputs = self.forward(x)
|
76 |
+
probabilities = torch.nn.functional.softmax(outputs, dim=1)
|
77 |
+
if not get_class:
|
78 |
+
return {
|
79 |
+
"cat": round(probabilities[0][0].item(), 3),
|
80 |
+
"dog": round(probabilities[0][1].item(), 3)
|
81 |
+
}
|
82 |
+
else:
|
83 |
+
|
84 |
+
return self.classes[probabilities.argmax(dim=1).item()]
|
85 |
+
|
86 |
+
|
87 |
+
|