File size: 1,584 Bytes
831f00d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import torch.nn as nn
import torchvision.transforms as transforms
from PIL import Image
import gradio as gr


class ModifiedLargeNet(nn.Module):
    def __init__(self):
        super(ModifiedLargeNet, self).__init__()
        self.name = "modified_large"
        self.fc1 = nn.Linear(128 * 128 * 3, 256)
        self.fc2 = nn.Linear(256, 128)
        self.fc3 = nn.Linear(128, 3)  # 3 classes: Rope, Hammer, Other

    def forward(self, x):
        x = x.view(-1, 128 * 128 * 3)
        x = torch.relu(self.fc1(x))
        x = torch.relu(self.fc2(x))
        x = self.fc3(x)
        return x


model = ModifiedLargeNet()
model.load_state_dict(torch.load("modified_large_net.pt", map_location=torch.device("cpu")))
model.eval()


transform = transforms.Compose([
    transforms.Resize((128, 128)),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
])


def predict(image):

    image = transform(image).unsqueeze(0) 
    with torch.no_grad():
        outputs = model(image)
        probabilities = torch.softmax(outputs, dim=1).numpy()[0]
        classes = ["Rope", "Hammer", "Other"]
        return {cls: float(prob) for cls, prob in zip(classes, probabilities)}

interface = gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),  
    outputs=gr.Label(num_top_classes=3),  
    title="Mechanical Tools Classifier",
    description="Upload an image of a tool to classify it as 'Rope', 'Hammer', or 'Other'.",
)

if __name__ == "__main__":
    interface.launch()