HangenYuu commited on
Commit
0a2ae0a
1 Parent(s): fcd6502

added first version of app.py

Browse files
Files changed (3) hide show
  1. app.py +36 -3
  2. classes.txt +101 -0
  3. labels.txt +101 -0
app.py CHANGED
@@ -1,7 +1,40 @@
1
  import gradio as gr
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  iface.launch()
 
1
  import gradio as gr
2
+ import timm
3
+ import torch
4
+ import torchvision.transforms as transforms
5
 
6
+ inference_model = timm.create_model('swin_large_patch4_window7_224', pretrained=False, num_classes=101)
7
+ inference_model.load_state_dict(torch.load('model.pth'))
8
+ inference_model.eval()
9
 
10
+ with open('labels.txt', 'r') as f:
11
+ idx_to_class = [s.strip() for s in f.readlines()]
12
+
13
+ preprocess = transforms.Compose([
14
+ transforms.Resize(256),
15
+ transforms.CenterCrop(224),
16
+ transforms.ToTensor(),
17
+ transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
18
+ ])
19
+
20
+ def inference(input_image):
21
+ input_tensor = preprocess(input_image)
22
+ input_batch = input_tensor.unsqueeze(0)
23
+
24
+ if torch.cuda.is_available():
25
+ input_batch = input_batch.to('cuda')
26
+ inference_model.to('cuda')
27
+
28
+ with torch.inference_mode():
29
+ output = inference_model(input_batch)
30
+
31
+ probabilities = torch.nn.functional.softmax(output[0], dim=0)
32
+ top5_prob, top5_catid = torch.topk(probabilities, 5)
33
+ # Label:probability
34
+ result = {idx_to_class[int(idx)]:val.item() for val, idx in zip(top5_prob.cpu(), top5_catid.cpu())}
35
+
36
+ return result
37
+ iface = gr.Interface(fn=inference,
38
+ inputs=gr.Image(type="pil"),
39
+ outputs=gr.Label(num_top_classes=5))
40
  iface.launch()
classes.txt ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ apple_pie
2
+ baby_back_ribs
3
+ baklava
4
+ beef_carpaccio
5
+ beef_tartare
6
+ beet_salad
7
+ beignets
8
+ bibimbap
9
+ bread_pudding
10
+ breakfast_burrito
11
+ bruschetta
12
+ caesar_salad
13
+ cannoli
14
+ caprese_salad
15
+ carrot_cake
16
+ ceviche
17
+ cheesecake
18
+ cheese_plate
19
+ chicken_curry
20
+ chicken_quesadilla
21
+ chicken_wings
22
+ chocolate_cake
23
+ chocolate_mousse
24
+ churros
25
+ clam_chowder
26
+ club_sandwich
27
+ crab_cakes
28
+ creme_brulee
29
+ croque_madame
30
+ cup_cakes
31
+ deviled_eggs
32
+ donuts
33
+ dumplings
34
+ edamame
35
+ eggs_benedict
36
+ escargots
37
+ falafel
38
+ filet_mignon
39
+ fish_and_chips
40
+ foie_gras
41
+ french_fries
42
+ french_onion_soup
43
+ french_toast
44
+ fried_calamari
45
+ fried_rice
46
+ frozen_yogurt
47
+ garlic_bread
48
+ gnocchi
49
+ greek_salad
50
+ grilled_cheese_sandwich
51
+ grilled_salmon
52
+ guacamole
53
+ gyoza
54
+ hamburger
55
+ hot_and_sour_soup
56
+ hot_dog
57
+ huevos_rancheros
58
+ hummus
59
+ ice_cream
60
+ lasagna
61
+ lobster_bisque
62
+ lobster_roll_sandwich
63
+ macaroni_and_cheese
64
+ macarons
65
+ miso_soup
66
+ mussels
67
+ nachos
68
+ omelette
69
+ onion_rings
70
+ oysters
71
+ pad_thai
72
+ paella
73
+ pancakes
74
+ panna_cotta
75
+ peking_duck
76
+ pho
77
+ pizza
78
+ pork_chop
79
+ poutine
80
+ prime_rib
81
+ pulled_pork_sandwich
82
+ ramen
83
+ ravioli
84
+ red_velvet_cake
85
+ risotto
86
+ samosa
87
+ sashimi
88
+ scallops
89
+ seaweed_salad
90
+ shrimp_and_grits
91
+ spaghetti_bolognese
92
+ spaghetti_carbonara
93
+ spring_rolls
94
+ steak
95
+ strawberry_shortcake
96
+ sushi
97
+ tacos
98
+ takoyaki
99
+ tiramisu
100
+ tuna_tartare
101
+ waffles
labels.txt ADDED
@@ -0,0 +1,101 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Apple pie
2
+ Baby back ribs
3
+ Baklava
4
+ Beef carpaccio
5
+ Beef tartare
6
+ Beet salad
7
+ Beignets
8
+ Bibimbap
9
+ Bread pudding
10
+ Breakfast burrito
11
+ Bruschetta
12
+ Caesar salad
13
+ Cannoli
14
+ Caprese salad
15
+ Carrot cake
16
+ Ceviche
17
+ Cheesecake
18
+ Cheese plate
19
+ Chicken curry
20
+ Chicken quesadilla
21
+ Chicken wings
22
+ Chocolate cake
23
+ Chocolate mousse
24
+ Churros
25
+ Clam chowder
26
+ Club sandwich
27
+ Crab cakes
28
+ Creme brulee
29
+ Croque madame
30
+ Cup cakes
31
+ Deviled eggs
32
+ Donuts
33
+ Dumplings
34
+ Edamame
35
+ Eggs benedict
36
+ Escargots
37
+ Falafel
38
+ Filet mignon
39
+ Fish and chips
40
+ Foie gras
41
+ French fries
42
+ French onion soup
43
+ French toast
44
+ Fried calamari
45
+ Fried rice
46
+ Frozen yogurt
47
+ Garlic bread
48
+ Gnocchi
49
+ Greek salad
50
+ Grilled cheese sandwich
51
+ Grilled salmon
52
+ Guacamole
53
+ Gyoza
54
+ Hamburger
55
+ Hot and sour soup
56
+ Hot dog
57
+ Huevos rancheros
58
+ Hummus
59
+ Ice cream
60
+ Lasagna
61
+ Lobster bisque
62
+ Lobster roll sandwich
63
+ Macaroni and cheese
64
+ Macarons
65
+ Miso soup
66
+ Mussels
67
+ Nachos
68
+ Omelette
69
+ Onion rings
70
+ Oysters
71
+ Pad thai
72
+ Paella
73
+ Pancakes
74
+ Panna cotta
75
+ Peking duck
76
+ Pho
77
+ Pizza
78
+ Pork chop
79
+ Poutine
80
+ Prime rib
81
+ Pulled pork sandwich
82
+ Ramen
83
+ Ravioli
84
+ Red velvet cake
85
+ Risotto
86
+ Samosa
87
+ Sashimi
88
+ Scallops
89
+ Seaweed salad
90
+ Shrimp and grits
91
+ Spaghetti bolognese
92
+ Spaghetti carbonara
93
+ Spring rolls
94
+ Steak
95
+ Strawberry shortcake
96
+ Sushi
97
+ Tacos
98
+ Takoyaki
99
+ Tiramisu
100
+ Tuna tartare
101
+ Waffles