File size: 2,123 Bytes
65602f1
 
 
 
4027e4e
65602f1
 
 
 
 
 
 
 
 
 
 
4027e4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
65602f1
 
4027e4e
65602f1
4027e4e
65602f1
 
 
 
4027e4e
 
65602f1
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
import torch
import torch.nn as nn
from torchvision import models,transforms
from PIL import Image
import torch.nn.functional as f
import gradio as gr
from torchvision.transforms import transforms


# model=models.resnet18(pretrained=True)
# model.fc=nn.Linear(model.fc.in_features,10)
t=transforms.Compose([  transforms.ToTensor(),
                        transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5)),
                        transforms.RandomHorizontalFlip(0.5),
                        transforms.RandomRotation(10),
                        ])
class_name=["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship","truck"]

class CIFAR_Module(nn.Module):
    def __init__(self,in_channel):
        self.in_channel=in_channel
        super(CIFAR_Module,self).__init__()
        self.con1=nn.Conv2d(in_channel,6*in_channel,5)
        self.pool1=nn.MaxPool2d(5,stride=2)
        self.con2=nn.Conv2d(6*in_channel,16*in_channel,5)
        self.pool2=nn.MaxPool2d(5,stride=2)
        self.flat=nn.Flatten()
        self.fc1=nn.Linear(192,100*in_channel)
        self.fc2=nn.Linear(100*in_channel,40*in_channel)
        self.fc3=nn.Linear(40*in_channel,10)
    def forward(self,x):
        x=self.con1(x)
        x=f.relu(x)
        x=self.pool1(x)
        x=f.relu(x)
        x=self.con2(x)
        x=f.relu(x)
        x=self.pool2(x)
        x=self.flat(x)
        x=self.fc1(x)
        x=f.relu(x)
        x=self.fc2(x)
        x=f.relu(x)
        x=self.fc3(x)
        return x


model=CIFAR_Module(3)
model.load_state_dict(torch.load("model.pth",weights_only=True))
model.eval()


print(model)

def predict(image):
    image=image.resize((32,32))
    image=t(image).unsqueeze(0)
    with torch.no_grad():
        output=model(image)
        _,predicted=torch.max(output,1)
        print(output)
        predicted_class=class_name[predicted.item()-1]
        return predicted_class

interface=gr.Interface(
    fn=predict,
    inputs=gr.Image(type="pil"),
    outputs="text",
    title="cifar dataset prediction",
    description="upload an image to get its class"
)

interface.launch(share=True)