Spaces:
Sleeping
Sleeping
Boboiazumi
commited on
Upload 3 files
Browse files- app.py +43 -0
- cnn.py +47 -0
- cnn_model.pth +3 -0
app.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import cnn
|
4 |
+
from torchvision import transforms
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
transform = transforms.Compose([
|
8 |
+
transforms.Resize((128, 128)),
|
9 |
+
transforms.ToTensor()
|
10 |
+
])
|
11 |
+
|
12 |
+
model = cnn.CNN(2)
|
13 |
+
model = model.to("cpu")
|
14 |
+
model.load_state_dict(torch.load("cnn_model.pth", weights_only=True, map_location="cpu"))
|
15 |
+
model.eval()
|
16 |
+
|
17 |
+
label = ["Kucing", "Anjing"]
|
18 |
+
|
19 |
+
def inference(image):
|
20 |
+
image = transform(image).unsqueeze(0)
|
21 |
+
|
22 |
+
with torch.no_grad():
|
23 |
+
output = model(image)
|
24 |
+
output = torch.nn.functional.softmax(output, dim=1)
|
25 |
+
|
26 |
+
predicted_class = torch.argmax(output, dim=1).item()
|
27 |
+
score = output[0][predicted_class]
|
28 |
+
|
29 |
+
return f'Ini adalah {label[predicted_class]} dengan kecocokan sebesar {score * 100}'
|
30 |
+
|
31 |
+
with gr.Blocks() as demo:
|
32 |
+
with gr.Row():
|
33 |
+
with gr.Column():
|
34 |
+
inputs = gr.Image(type="pil")
|
35 |
+
with gr.Column():
|
36 |
+
btn = gr.Button("Cek")
|
37 |
+
pred = gr.Text(label="Prediction")
|
38 |
+
|
39 |
+
btn.click(fn=inference, inputs=inputs, outputs=pred)
|
40 |
+
|
41 |
+
|
42 |
+
|
43 |
+
demo.queue().launch()
|
cnn.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from torch import nn
|
2 |
+
|
3 |
+
class CNN(nn.Module):
|
4 |
+
def __init__(self, num_classes):
|
5 |
+
super(CNN, self).__init__()
|
6 |
+
|
7 |
+
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
|
8 |
+
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
|
9 |
+
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
|
10 |
+
|
11 |
+
self.pool = nn.MaxPool2d(kernel_size=2, stride=2)
|
12 |
+
|
13 |
+
self.bn1 = nn.BatchNorm2d(32)
|
14 |
+
self.bn2 = nn.BatchNorm2d(64)
|
15 |
+
self.bn3 = nn.BatchNorm2d(128)
|
16 |
+
|
17 |
+
self.fc1 = nn.Linear(128 * 16 * 16, 512)
|
18 |
+
self.fc2 = nn.Linear(512, 256)
|
19 |
+
self.fc3 = nn.Linear(256, 128)
|
20 |
+
self.fc4 = nn.Linear(128 ,num_classes)
|
21 |
+
|
22 |
+
self.dropout = nn.Dropout(0.5)
|
23 |
+
|
24 |
+
def forward(self, x):
|
25 |
+
x = nn.functional.relu(self.conv1(x))
|
26 |
+
x = self.bn1(x)
|
27 |
+
x = self.pool(x)
|
28 |
+
|
29 |
+
x = nn.functional.relu(self.conv2(x))
|
30 |
+
x = self.bn2(x)
|
31 |
+
x = self.pool(x)
|
32 |
+
|
33 |
+
x = nn.functional.relu(self.conv3(x))
|
34 |
+
x = self.bn3(x)
|
35 |
+
x = self.pool(x)
|
36 |
+
|
37 |
+
x = x.view(-1, 128 * 16 * 16)
|
38 |
+
|
39 |
+
x = nn.functional.relu(self.fc1(x))
|
40 |
+
|
41 |
+
x = nn.functional.relu(self.fc2(x))
|
42 |
+
x = self.dropout(x)
|
43 |
+
|
44 |
+
x = nn.functional.relu(self.fc3(x))
|
45 |
+
x = self.dropout(x)
|
46 |
+
x = self.fc4(x)
|
47 |
+
return x
|
cnn_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:981a38ba3f9444a5d72edef7526ac510dc593fe5c907e0bfb0a2a58ee43549a6
|
3 |
+
size 68154689
|