File size: 2,917 Bytes
06088b6
 
 
c2fc618
06088b6
2426537
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06088b6
 
8c92bed
c2fc618
2426537
c2fc618
 
 
06088b6
 
2426537
 
 
 
 
 
 
 
12bc61f
2426537
 
 
 
 
 
 
 
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import streamlit as st
import torch
from PIL import Image
import os

import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
    
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, 5)
        self.conv2 = nn.Conv2d(32, 64, 5)
        self.conv3 = nn.Conv2d(64, 128, 5)
        self.conv4 = nn.Conv2d(128, 256, 5)
        self.conv5 = nn.Conv2d(256, 512, 5)

        self.fc1 = None
        self.fc2 = nn.Linear(512, 128)
        self.fc3 = nn.Linear(128, 64)
        self.fc4 = nn.Linear(64, 2)

    def forward(self, x):
        x = x.float()
        """ x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2)
        x = F.relu(self.conv3(x))
        x = F.relu(self.conv4(x))
        x = F.max_pool2d(x, 2)
        x = F.relu(self.conv5(x))
        x = F.max_pool2d(x, 2) """
        
        x = F.max_pool2d(F.relu(self.conv1(x)), 2)
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = F.max_pool2d(F.relu(self.conv3(x)), 2)
        x = F.max_pool2d(F.relu(self.conv4(x)), 2)
        x = F.max_pool2d(F.relu(self.conv5(x)), 2)

        #x = x.view(x.size(0), -1)
        x = torch.flatten(x, 1)
        
        if self.fc1 is None:
            self.fc1 = nn.Linear(x.shape[1], 512).to(x.device)
        
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        x = self.fc4(x)
        return x
    
    
def classify(model, img, trans=None, classes=[], device=torch.device("cpu")):
    try:
        model = model.eval()
        img = img.convert("RGB")
        img = trans(img)
        img = img.unsqueeze(0)
        img = img.to(device)

        output = model(img)
        _, pred = torch.max(output, 1)
        procent = torch.sigmoid(output)
        
        return f"It {classes[pred.item()].replace('_', ' ')}, I'm {procent[0][pred[0]]*100:.2f}% sure"
    except Exception:
        return "Something went wrong😕, please notify the developer with the following message: " + str(Exception)

st.title("Pizza & Not Pizza")

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
checkpoint = torch.load("best.pth.tar", map_location=device)
model = checkpoint["model"]
classes = checkpoint["classes"]
tran = checkpoint["transform"]

# upload image
uploaded_file = st.file_uploader("Choose an image...", type="jpg")
taking_picture = st.camera_input("Take a picture...")

if uploaded_file is not None:
    img = Image.open(uploaded_file)
    st.image(img, caption="Uploaded Image.", use_column_width=True)
    label = classify(model, img, tran, classes, device)
    st.write(label)
    
elif taking_picture is not None:
    img = Image.open(taking_picture)
    st.image(img, caption="Uploaded Image.", use_column_width=True)
    label = classify(model, img, tran, classes, device)
    st.write(label)

else:
    pass