GitHub Actions Bot commited on
Commit ·
8aeeee9
1
Parent(s): 404d252
Sync from Github Actions
Browse files- app.py +9 -45
- dataset/__init__.py +1 -0
- dataset/dataset.py +31 -0
- model/__init__.py +0 -0
- model/cnn.py +0 -1
- model/predict.py +30 -0
app.py
CHANGED
|
@@ -1,57 +1,21 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
import torchvision.transforms.functional as TF
|
| 5 |
-
from model.cnn import CNN
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
|
| 12 |
-
translation = {
|
| 13 |
-
"airplane": 0,
|
| 14 |
-
"automobile": 1,
|
| 15 |
-
"bird": 2,
|
| 16 |
-
"cat": 3,
|
| 17 |
-
"deer": 4,
|
| 18 |
-
"dog": 5,
|
| 19 |
-
"frog": 6,
|
| 20 |
-
"horse": 7,
|
| 21 |
-
"ship": 8,
|
| 22 |
-
"truck": 9,
|
| 23 |
-
}
|
| 24 |
-
inverted_translation = {v: k for k, v in translation.items()}
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def predict(image):
|
| 28 |
-
|
| 29 |
-
image = TF.to_tensor(image)
|
| 30 |
-
image_tensor = torch.stack([image])
|
| 31 |
-
|
| 32 |
-
print(image_tensor)
|
| 33 |
-
|
| 34 |
-
result = model(image_tensor)
|
| 35 |
-
result = torch.softmax(result,dim=1)
|
| 36 |
-
result = result[0]
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
dict_results = {}
|
| 40 |
-
|
| 41 |
-
for i in range(len(result)):
|
| 42 |
-
dict_results[inverted_translation[i]] = float(result[i])
|
| 43 |
-
|
| 44 |
-
return dict_results
|
| 45 |
-
|
| 46 |
|
| 47 |
demo = gr.Interface(
|
| 48 |
-
|
| 49 |
title="Image Classifier using CNN ( Cifar-10) ",
|
| 50 |
description="This is a image classifier using a CNN, it was trained on the Cifar-10 dataset ( Kaggle) \n",
|
| 51 |
-
article="The architecture is a CNN",
|
| 52 |
inputs=gr.Image(shape=(32, 32),type="pil"),
|
| 53 |
outputs=gr.Label(),
|
| 54 |
-
examples=["examples/1.png", "examples/2.png", "examples/3.png", "examples/4.png" ],
|
| 55 |
)
|
| 56 |
|
| 57 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from model.predict import predict
|
| 3 |
+
from torchvision.transforms import functional as F
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def custom_predict(image):
|
| 6 |
+
image = F.to_tensor(image)
|
| 7 |
+
return predict(image, get_dictionary=True)
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
demo = gr.Interface(
|
| 12 |
+
custom_predict,
|
| 13 |
title="Image Classifier using CNN ( Cifar-10) ",
|
| 14 |
description="This is a image classifier using a CNN, it was trained on the Cifar-10 dataset ( Kaggle) \n",
|
| 15 |
+
article="The architecture is a CNN, uploaded via Github Actions",
|
| 16 |
inputs=gr.Image(shape=(32, 32),type="pil"),
|
| 17 |
outputs=gr.Label(),
|
| 18 |
+
examples=["examples/1.png", "examples/2.png", "examples/3.png", "examples/4.png" , "examples/5.png"],
|
| 19 |
)
|
| 20 |
|
| 21 |
+
demo.launch()
|
dataset/__init__.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
from dataset.dataset import CifarDataset
|
dataset/dataset.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
from torch.utils.data import Dataset
|
| 3 |
+
from torchvision.io import read_image
|
| 4 |
+
|
| 5 |
+
translation = {'airplane': 0, 'automobile': 1, 'bird': 2, 'cat': 3, 'deer': 4, 'dog': 5, 'frog': 6, 'horse': 7, 'ship': 8, 'truck': 9}
|
| 6 |
+
inverted_translation = {v: k for k, v in translation.items()}
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class CifarDataset(Dataset):
|
| 10 |
+
def __init__(self, file, folder, include_idx=False):
|
| 11 |
+
self.data = pd.read_csv(file)
|
| 12 |
+
self.data['label'] = self.data['label'].map(translation)
|
| 13 |
+
self.folder = folder
|
| 14 |
+
self.include_idx = include_idx
|
| 15 |
+
|
| 16 |
+
def __len__(self):
|
| 17 |
+
return len(self.data)
|
| 18 |
+
|
| 19 |
+
def __getitem__(self, idx):
|
| 20 |
+
id, label = self.data.iloc[idx][0] , self.data.iloc[idx][1]
|
| 21 |
+
image = read_image( f'{self.folder}/{id}.png')
|
| 22 |
+
image = image/255.0
|
| 23 |
+
|
| 24 |
+
if self.include_idx:
|
| 25 |
+
return image, label, id
|
| 26 |
+
else:
|
| 27 |
+
return image, label
|
| 28 |
+
|
| 29 |
+
if __name__ == '__main__':
|
| 30 |
+
dataset = CifarDataset('trainLabels.csv', 'train')
|
| 31 |
+
print(dataset[0][0].type())
|
model/__init__.py
ADDED
|
File without changes
|
model/cnn.py
CHANGED
|
@@ -3,7 +3,6 @@ import torch
|
|
| 3 |
import torch.nn as nn
|
| 4 |
|
| 5 |
|
| 6 |
-
|
| 7 |
class CNN(pl.LightningModule):
|
| 8 |
def __init__(self):
|
| 9 |
super().__init__()
|
|
|
|
| 3 |
import torch.nn as nn
|
| 4 |
|
| 5 |
|
|
|
|
| 6 |
class CNN(pl.LightningModule):
|
| 7 |
def __init__(self):
|
| 8 |
super().__init__()
|
model/predict.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from model.cnn import CNN
|
| 2 |
+
import torch
|
| 3 |
+
from dataset.dataset import inverted_translation
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from torchvision.transforms import functional as F
|
| 6 |
+
|
| 7 |
+
path = "model/epoch=599-step=187800.ckpt"
|
| 8 |
+
model = CNN.load_from_checkpoint(path)
|
| 9 |
+
model.eval()
|
| 10 |
+
|
| 11 |
+
def predict(image, get_dictionary=False):
|
| 12 |
+
|
| 13 |
+
image_tensor = image.view(1, 3, 32, 32)
|
| 14 |
+
result = model(image_tensor)
|
| 15 |
+
result = torch.softmax(result,dim=1)
|
| 16 |
+
result = result[0]
|
| 17 |
+
|
| 18 |
+
if get_dictionary:
|
| 19 |
+
dict_results = {}
|
| 20 |
+
|
| 21 |
+
for i in range(len(result)):
|
| 22 |
+
dict_results[inverted_translation[i]] = float(result[i])
|
| 23 |
+
|
| 24 |
+
return dict_results
|
| 25 |
+
|
| 26 |
+
else:
|
| 27 |
+
best = int(torch.argmax(result))
|
| 28 |
+
return inverted_translation[best]
|
| 29 |
+
|
| 30 |
+
|