Spaces:
Runtime error
Runtime error
Commit
·
bb2feab
1
Parent(s):
cf6d28d
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,83 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
from timeit import default_timer as timer
|
| 7 |
-
from typing import Tuple, Dict
|
| 8 |
-
|
| 9 |
-
#class names
|
| 10 |
-
with open('class_names.txt', "r") as f:
|
| 11 |
-
class_names = [car.strip() for car in f.readlines()]
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
#model and transforms preparation
|
| 15 |
-
effnetb0_weights = models.EfficientNet_B0_Weights.DEFAULT
|
| 16 |
-
effnetb0 = torchvision.models.efficientnet_b0(weights = effnetb0_weights)
|
| 17 |
-
effnetb0_transforms = effnetb0_weights.transforms()
|
| 18 |
-
|
| 19 |
-
#freeze params
|
| 20 |
-
for param in effnetb0.parameters():
|
| 21 |
-
param.requires_grad = False
|
| 22 |
-
|
| 23 |
-
#change classifier
|
| 24 |
-
effnetb0.classifier = nn.Sequential(
|
| 25 |
-
nn.Dropout(p=.2),
|
| 26 |
-
nn.Linear(in_features = 1280,
|
| 27 |
-
out_features = 196)
|
| 28 |
-
)
|
| 29 |
-
|
| 30 |
-
#load saved weights
|
| 31 |
-
effnetb0.load_state_dict(torch.load('stanford_cars/pretrained_effnetb0_stanford_cars_20_percent.pth.pth'),
|
| 32 |
-
map_location=torch.device("cpu"))
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
#predict function
|
| 36 |
-
|
| 37 |
-
def predict(img) -> Tuple[Dict, float]:
|
| 38 |
-
|
| 39 |
-
start_time = timer()
|
| 40 |
-
|
| 41 |
-
#put model into eval mode
|
| 42 |
-
effnetb0.eval()
|
| 43 |
-
|
| 44 |
-
with torch.inference_mode():
|
| 45 |
-
pred_logits = effnetb0(img.unsqueeze(0))
|
| 46 |
-
pred_probs = torch.softmax(pred_logits, dim = 1)
|
| 47 |
-
|
| 48 |
-
# Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio's output parameter)
|
| 49 |
-
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
|
| 50 |
-
|
| 51 |
-
end_time = timer()
|
| 52 |
-
|
| 53 |
-
time = round(end_time - start_time, 5)
|
| 54 |
-
|
| 55 |
-
return pred_labels_and_probs, time
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
#gradio app
|
| 59 |
-
|
| 60 |
-
title = 'effnetb0'
|
| 61 |
-
description = 'Pretrained effnetb0 model on stanford cars dataset'
|
| 62 |
-
|
| 63 |
-
#create example list
|
| 64 |
-
example_list = [["examples/" + example] for example in os.listdir("examples")]
|
| 65 |
-
|
| 66 |
-
# Create Gradio interface
|
| 67 |
-
demo = gr.Interface(
|
| 68 |
-
fn=predict,
|
| 69 |
-
inputs=gr.Image(type="pil"),
|
| 70 |
-
outputs=[
|
| 71 |
-
gr.Label(num_top_classes=5, label="Predictions"),
|
| 72 |
-
gr.Number(label="Prediction time (s)"),
|
| 73 |
-
],
|
| 74 |
-
examples=example_list,
|
| 75 |
-
title=title,
|
| 76 |
-
description=description
|
| 77 |
-
|
| 78 |
-
)
|
| 79 |
-
|
| 80 |
-
# Launch the app!
|
| 81 |
-
demo.launch()
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|