Spaces:
Paused
Paused
Commit
·
c2951b3
1
Parent(s):
b9968f6
Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,81 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
|
|
|
|
|
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
#Import libraries
|
3 |
+
import torch
|
4 |
+
import torchvision.models as models
|
5 |
+
import json
|
6 |
|
7 |
+
#Import User Defined libraries
|
8 |
+
from neural_network_model import initialize_existing_models, build_custom_models, set_parameter_requires_grad
|
9 |
+
from utilities import process_image, get_input_args_predict
|
10 |
+
|
11 |
+
def predict(image_path, model, topk=5):
|
12 |
+
|
13 |
+
|
14 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
15 |
+
model.to(device)
|
16 |
+
model.eval()
|
17 |
+
|
18 |
+
tensor_img = torch.FloatTensor(process_image(image_path))
|
19 |
+
tensor_img = tensor_img.unsqueeze(0)
|
20 |
+
tensor_img = tensor_img.to(device)
|
21 |
+
log_ps = model(tensor_img)
|
22 |
+
result = log_ps.topk(topk)
|
23 |
+
if torch.cuda.is_available(): #gpu Move it from gpu to cpu for numpy
|
24 |
+
ps = torch.exp(result[0].data).cpu().numpy()[0]
|
25 |
+
idxs = result[1].data.cpu().numpy()[0]
|
26 |
+
else: #cpu Keep it on cpu for nump
|
27 |
+
ps = torch.exp(result[0].data).numpy()[0]
|
28 |
+
idxs = result[1].data.numpy()[0]
|
29 |
+
|
30 |
+
return (ps, idxs)
|
31 |
+
|
32 |
+
def process_input(image_path):
|
33 |
+
#0. Get user inputs
|
34 |
+
#in_arg = vars(get_input_args_predict())
|
35 |
+
#print("User arguments/hyperparameters or default used are as below")
|
36 |
+
#print(in_arg)
|
37 |
+
in_arg = {}
|
38 |
+
in_arg['gpu'] = 'gpu'
|
39 |
+
in_arg['save_dir'] = 'checkpoint-densenet121.pth'
|
40 |
+
in_arg['path'] = image_path
|
41 |
+
in_arg['top_k'] = 5
|
42 |
+
|
43 |
+
#1. Get device for prediction and Load model from checkpoint along with some other information
|
44 |
+
if in_arg['gpu'] == 'gpu' and torch.cuda.is_available():
|
45 |
+
device = torch.device("cuda")
|
46 |
+
checkpoint = torch.load(in_arg['save_dir'])
|
47 |
+
else:
|
48 |
+
device = "cpu"
|
49 |
+
checkpoint = torch.load(in_arg['save_dir'], map_location = device)
|
50 |
+
#print(f"Using {device} device for predicting/inference")
|
51 |
+
|
52 |
+
if checkpoint['arch_type'] == 'existing':
|
53 |
+
model_ft, input_size = initialize_existing_models(checkpoint['arch'], checkpoint['arch_type'], len(checkpoint['class_to_idx']),
|
54 |
+
checkpoint['feature_extract'], checkpoint['hidden_units'], use_pretrained=False)
|
55 |
+
elif checkpoint['arch_type'] == 'custom':
|
56 |
+
model_ft = build_custom_models(checkpoint['arch'], checkpoint['arch_type'], len(checkpoint['class_to_idx']), checkpoint['feature_extract'],
|
57 |
+
checkpoint['hidden_units'], use_pretrained=True)
|
58 |
+
else:
|
59 |
+
#print("Nothing to predict")
|
60 |
+
exit()
|
61 |
+
|
62 |
+
|
63 |
+
model_ft.class_to_idx = checkpoint['class_to_idx']
|
64 |
+
model_ft.gpu_or_cpu = checkpoint['gpu_or_cpu']
|
65 |
+
model_ft.load_state_dict(checkpoint['state_dict'])
|
66 |
+
model_ft.to(device)
|
67 |
+
|
68 |
+
#Predict
|
69 |
+
# Get the prediction by passing image and other user preferences through the model
|
70 |
+
probs, idxs = predict(image_path = in_arg['path'], model = model_ft, topk = in_arg['top_k'])
|
71 |
+
|
72 |
+
# Swap class to index mapping with index to class mapping and then map the classes to flower category labels using the json file
|
73 |
+
idx_to_class = {v: k for k, v in model_ft.class_to_idx.items()}
|
74 |
+
with open('cat_to_name.json','r') as f:
|
75 |
+
cat_to_name = json.load(f)
|
76 |
+
names = list(map(lambda x: cat_to_name[f"{idx_to_class[x]}"],idxs))
|
77 |
+
|
78 |
+
return names, probs
|
79 |
+
|
80 |
+
iface = gr.Interface(fn=process_input, inputs=gr.inputs.Image(type='pil'), outputs=gr.outputs.Label(num_top_classes=3))
|
81 |
iface.launch()
|