Spaces:
Sleeping
Sleeping
initial commit
Browse files- .gitignore +3 -0
- app.py +22 -0
- examples/093e6044-5809-44d6-a93f-f06a6702f20d.jpg +0 -0
- examples/0ab9373f-4d97-456a-9b9d-60b4d05d102e.jpg +0 -0
- examples/332b50b9-ab27-42ac-9118-ae32b3458d97.jpg +0 -0
- examples/357fbd95-ff50-4d90-9487-531595f02a9e.jpg +0 -0
- examples/b100a944-0e67-4e4d-9ccc-c6c63fb99c8e.jpg +0 -0
- image_classifier.py +34 -0
- models/trained_model.pth +3 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.DS_Store
|
2 |
+
/venv/
|
3 |
+
__pycache__
|
app.py
ADDED
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from image_classifier import ImageClassifier
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
classifier = ImageClassifier('models/trained_model.pth')
|
6 |
+
examples = [
|
7 |
+
'examples/0ab9373f-4d97-456a-9b9d-60b4d05d102e.jpg',
|
8 |
+
'examples/093e6044-5809-44d6-a93f-f06a6702f20d.jpg',
|
9 |
+
'examples/332b50b9-ab27-42ac-9118-ae32b3458d97.jpg',
|
10 |
+
'examples/357fbd95-ff50-4d90-9487-531595f02a9e.jpg',
|
11 |
+
'examples/b100a944-0e67-4e4d-9ccc-c6c63fb99c8e.jpg'
|
12 |
+
]
|
13 |
+
|
14 |
+
def predict(image):
|
15 |
+
image = Image.fromarray(image.astype('uint8'), 'RGB')
|
16 |
+
return classifier.classify_image(image)
|
17 |
+
|
18 |
+
|
19 |
+
image = gr.inputs.Image(shape=(192,192))
|
20 |
+
label = gr.outputs.Label()
|
21 |
+
intf = gr.Interface(fn=predict, inputs=image, outputs=label, examples=examples)
|
22 |
+
intf.launch(inline=False)
|
examples/093e6044-5809-44d6-a93f-f06a6702f20d.jpg
ADDED
![]() |
examples/0ab9373f-4d97-456a-9b9d-60b4d05d102e.jpg
ADDED
![]() |
examples/332b50b9-ab27-42ac-9118-ae32b3458d97.jpg
ADDED
![]() |
examples/357fbd95-ff50-4d90-9487-531595f02a9e.jpg
ADDED
![]() |
examples/b100a944-0e67-4e4d-9ccc-c6c63fb99c8e.jpg
ADDED
![]() |
image_classifier.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from torchvision import models, transforms
|
3 |
+
from PIL import Image
|
4 |
+
|
5 |
+
CLASS_NAMES = ['apple', 'bread', 'fried_chicken', 'hamburger', 'pizza', 'popcorn', 'salad', 'steak', 'taco']
|
6 |
+
|
7 |
+
class ImageClassifier:
|
8 |
+
|
9 |
+
def __init__(self, model_path, device='cpu'):
|
10 |
+
self.transform = transforms.Compose([
|
11 |
+
transforms.Resize(256),
|
12 |
+
transforms.CenterCrop(224),
|
13 |
+
transforms.ToTensor(),
|
14 |
+
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
|
15 |
+
])
|
16 |
+
|
17 |
+
self.model = models.resnet50(weights='ResNet50_Weights.DEFAULT')
|
18 |
+
|
19 |
+
# Adjust the last layer to match the number of classes
|
20 |
+
num_ftrs = self.model.fc.in_features
|
21 |
+
self.model.fc = torch.nn.Linear(num_ftrs, len(CLASS_NAMES))
|
22 |
+
|
23 |
+
# Load the saved model
|
24 |
+
self.model.load_state_dict(torch.load(model_path, map_location=torch.device(device)))
|
25 |
+
self.model.eval() # Set the model to evaluation mode
|
26 |
+
|
27 |
+
def classify_image(self, image):
|
28 |
+
image = self.transform(image).unsqueeze(0) # Add batch dimension
|
29 |
+
|
30 |
+
# Perform inference
|
31 |
+
with torch.no_grad():
|
32 |
+
output = self.model(image)
|
33 |
+
_, predicted = torch.max(output, 1)
|
34 |
+
return CLASS_NAMES[predicted.item()]
|
models/trained_model.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6e39765d1161e22f416790ca8a8857141eaed8752565fe009fd91c4583b9f7bb
|
3 |
+
size 94405309
|