Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
README.md
CHANGED
|
@@ -7,3 +7,6 @@ sdk_version: 4.44.1
|
|
| 7 |
license: mit
|
| 8 |
pinned: true
|
| 9 |
---
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
license: mit
|
| 8 |
pinned: true
|
| 9 |
---
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
# DogBreedsClassifier
|
app.py
CHANGED
|
@@ -1,70 +1,79 @@
|
|
| 1 |
-
import os
|
| 2 |
import torch
|
| 3 |
-
import numpy as np
|
| 4 |
import lightning as pl
|
| 5 |
-
import gradio as gr
|
| 6 |
from PIL import Image
|
| 7 |
from torchvision import transforms
|
| 8 |
-
from timeit import default_timer as timer
|
| 9 |
from torch.nn import functional as F
|
| 10 |
|
| 11 |
torch.set_float32_matmul_precision('medium')
|
| 12 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 13 |
-
torch.set_default_device(
|
| 14 |
-
torch.autocast(enabled
|
| 15 |
|
| 16 |
pl.seed_everything(123, workers=True)
|
| 17 |
|
| 18 |
TEST_TRANSFORMS = transforms.Compose([
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
class_labels= [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
|
| 26 |
# Model
|
| 27 |
-
model = torch.jit.load('best_model.pt',map_location=device).to(device)
|
| 28 |
|
| 29 |
|
| 30 |
@torch.no_grad()
|
| 31 |
-
def predict_fn(img:Image):
|
| 32 |
start_time = timer()
|
| 33 |
-
try:
|
| 34 |
-
# img = np.array(img)
|
| 35 |
-
# print(img)
|
| 36 |
img = TEST_TRANSFORMS(img).to(device)
|
| 37 |
# print(type(img),img.shape)
|
| 38 |
logits = model(img.unsqueeze(0))
|
| 39 |
-
probabilities = F.softmax(logits,dim=-1)
|
| 40 |
# print(torch.topk(probabilities,k=2))
|
| 41 |
y_pred = probabilities.argmax(dim=-1).item()
|
| 42 |
confidence = probabilities[0][y_pred].item()
|
| 43 |
predicted_label = class_labels[y_pred]
|
| 44 |
# print(confidence,predicted_label)
|
| 45 |
-
pred_time = round(timer()-start_time,5)
|
| 46 |
-
res = {f
|
| 47 |
-
return (res,pred_time)
|
| 48 |
except Exception as e:
|
| 49 |
-
print(f
|
| 50 |
-
gr.Error(
|
| 51 |
-
return ({
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
|
| 56 |
|
| 57 |
gr.Interface(
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import torch
|
|
|
|
| 3 |
import lightning as pl
|
| 4 |
+
import gradio as gr
|
| 5 |
from PIL import Image
|
| 6 |
from torchvision import transforms
|
| 7 |
+
from timeit import default_timer as timer
|
| 8 |
from torch.nn import functional as F
|
| 9 |
|
| 10 |
torch.set_float32_matmul_precision('medium')
|
| 11 |
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
| 12 |
+
torch.set_default_device(device=device)
|
| 13 |
+
torch.autocast(enabled=True, dtype='float16', device_type='cuda')
|
| 14 |
|
| 15 |
pl.seed_everything(123, workers=True)
|
| 16 |
|
| 17 |
TEST_TRANSFORMS = transforms.Compose([
|
| 18 |
+
transforms.Resize((224, 224)),
|
| 19 |
+
transforms.ToTensor(),
|
| 20 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 21 |
+
])
|
| 22 |
+
class_labels = [
|
| 23 |
+
'Beagle',
|
| 24 |
+
'Boxer',
|
| 25 |
+
'Bulldog',
|
| 26 |
+
'Dachshund',
|
| 27 |
+
'German_Shepherd',
|
| 28 |
+
'Golden_Retriever',
|
| 29 |
+
'Labrador_Retriever',
|
| 30 |
+
'Poodle',
|
| 31 |
+
'Rottweiler',
|
| 32 |
+
'Yorkshire_Terrier',
|
| 33 |
+
]
|
| 34 |
|
| 35 |
|
| 36 |
# Model
|
| 37 |
+
model = torch.jit.load('best_model.pt', map_location=device).to(device)
|
| 38 |
|
| 39 |
|
| 40 |
@torch.no_grad()
|
| 41 |
+
def predict_fn(img: Image):
|
| 42 |
start_time = timer()
|
| 43 |
+
try:
|
| 44 |
+
# img = np.array(img)
|
| 45 |
+
# print(img)
|
| 46 |
img = TEST_TRANSFORMS(img).to(device)
|
| 47 |
# print(type(img),img.shape)
|
| 48 |
logits = model(img.unsqueeze(0))
|
| 49 |
+
probabilities = F.softmax(logits, dim=-1)
|
| 50 |
# print(torch.topk(probabilities,k=2))
|
| 51 |
y_pred = probabilities.argmax(dim=-1).item()
|
| 52 |
confidence = probabilities[0][y_pred].item()
|
| 53 |
predicted_label = class_labels[y_pred]
|
| 54 |
# print(confidence,predicted_label)
|
| 55 |
+
pred_time = round(timer() - start_time, 5)
|
| 56 |
+
res = {f'Title: {predicted_label}': confidence}
|
| 57 |
+
return (res, pred_time)
|
| 58 |
except Exception as e:
|
| 59 |
+
print(f'error:: {e}')
|
| 60 |
+
gr.Error('An error occured 💥!', duration=5)
|
| 61 |
+
return ({'Title ☠️': 0.0}, 0.0)
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
|
| 64 |
gr.Interface(
|
| 65 |
+
fn=predict_fn,
|
| 66 |
+
inputs=gr.Image(type='pil'),
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.Label(num_top_classes=1, label='Predictions'), # what are the outputs?
|
| 69 |
+
gr.Number(label='Prediction time (s)'),
|
| 70 |
+
],
|
| 71 |
+
examples=[
|
| 72 |
+
['examples/' + i]
|
| 73 |
+
for i in os.listdir(os.path.join(os.path.dirname(__file__), 'examples'))
|
| 74 |
+
],
|
| 75 |
+
title='Dog Breeds Classifier 🐈',
|
| 76 |
+
description='CNN-based Architecture for Fast and Accurate DogsBreed Classifier',
|
| 77 |
+
article='Created by muthukamalan.m ❤️',
|
| 78 |
+
cache_examples=True,
|
| 79 |
+
).launch(share=False, debug=False)
|