PsychicFireSong commited on
Commit
9fae6ff
·
1 Parent(s): c82a904

Add application files for Gradio Space

Browse files
Files changed (4) hide show
  1. app.py +73 -0
  2. labels.txt +198 -0
  3. models/final_model_best.pth +3 -0
  4. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from torchvision import transforms
4
+ import timm
5
+ from PIL import Image
6
+ import os
7
+
8
+ # 1. Load Labels
9
+ with open('labels.txt', 'r') as f:
10
+ labels = [line.strip() for line in f.readlines()]
11
+
12
+ # 2. Model Definition
13
+ def get_model(num_classes=200, model_path='models/final_model_best.pth'):
14
+ """Initializes and loads the pre-trained ConvNeXt V2 Large model."""
15
+ model = timm.create_model(
16
+ 'convnextv2_large.fcmae_ft_in22k_in1k',
17
+ pretrained=False,
18
+ num_classes=num_classes,
19
+ drop_path_rate=0.2
20
+ )
21
+ if not os.path.exists(model_path):
22
+ print(f"Error: Model file not found at {model_path}.")
23
+ return None
24
+ try:
25
+ model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
26
+ model.eval()
27
+ print("Model loaded successfully.")
28
+ return model
29
+ except Exception as e:
30
+ print(f"An error occurred while loading the model: {e}")
31
+ return None
32
+
33
+ model = get_model()
34
+
35
+ # 3. Image Transformations
36
+ transform = transforms.Compose([
37
+ transforms.Resize((224, 224)),
38
+ transforms.ToTensor(),
39
+ transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
40
+ ])
41
+
42
+ # 4. Prediction Function
43
+ def predict(image):
44
+ """Takes a PIL image and returns a dictionary of top 3 predictions."""
45
+ if model is None:
46
+ return {"Error": "Model is not loaded. Please check the logs for errors."}
47
+
48
+ image = transform(image).unsqueeze(0)
49
+ with torch.no_grad():
50
+ outputs = model(image)
51
+ probabilities = torch.nn.functional.softmax(outputs, dim=1)[0]
52
+
53
+ # Get top 3 predictions
54
+ top3_prob, top3_indices = torch.topk(probabilities, 3)
55
+
56
+ confidences = {labels[i]: float(p) for i, p in zip(top3_indices, top3_prob)}
57
+
58
+ return confidences
59
+
60
+ # 5. Gradio Interface
61
+ title = "Bird Species Classifier"
62
+ description = "Upload an image of a bird to classify it into one of 200 species. This model is a ConvNeXt V2 Large, fine-tuned on a dataset of 200 bird species."
63
+
64
+ iface = gr.Interface(
65
+ fn=predict,
66
+ inputs=gr.Image(type="pil", label="Upload Bird Image"),
67
+ outputs=gr.Label(num_top_classes=3, label="Predictions"),
68
+ title=title,
69
+ description=description,
70
+ )
71
+
72
+ if __name__ == "__main__":
73
+ iface.launch()
labels.txt ADDED
@@ -0,0 +1,198 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Black_footed_Albatross
2
+ Laysan_Albatross
3
+ Sooty_Albatross
4
+ Groove_billed_Ani
5
+ Crested_Auklet
6
+ Least_Auklet
7
+ Parakeet_Auklet
8
+ Rhinoceros_Auklet
9
+ Brewer_Blackbird
10
+ Red_winged_Blackbird
11
+ Rusty_Blackbird
12
+ Yellow_headed_Blackbird
13
+ Bobolink
14
+ Indigo_Bunting
15
+ Lazuli_Bunting
16
+ Painted_Bunting
17
+ Cardinal
18
+ Spotted_Catbird
19
+ Gray_Catbird
20
+ Yellow_breasted_Chat
21
+ Eastern_Towhee
22
+ Chuck_will_Widow
23
+ Brandt_Cormorant
24
+ Red_faced_Cormorant
25
+ Pelagic_Cormorant
26
+ Bronzed_Cowbird
27
+ Shiny_Cowbird
28
+ Brown_Creeper
29
+ American_Crow
30
+ Fish_Crow
31
+ Black_billed_Cuckoo
32
+ Mangrove_Cuckoo
33
+ Yellow_billed_Cuckoo
34
+ Gray_crowned_Rosy_Finch
35
+ Purple_Finch
36
+ Northern_Flicker
37
+ Acadian_Flycatcher
38
+ Great_Crested_Flycatcher
39
+ Least_Flycatcher
40
+ Olive_sided_Flycatcher
41
+ Scissor_tailed_Flycatcher
42
+ Vermilion_Flycatcher
43
+ Yellow_bellied_Flycatcher
44
+ Frigatebird
45
+ Northern_Fulmar
46
+ Gadwall
47
+ American_Goldfinch
48
+ European_Goldfinch
49
+ Boat_tailed_Grackle
50
+ Eared_Grebe
51
+ Horned_Grebe
52
+ Pied_billed_Grebe
53
+ Western_Grebe
54
+ Blue_Grosbeak
55
+ Evening_Grosbeak
56
+ Pine_Grosbeak
57
+ Rose_breasted_Grosbeak
58
+ Pigeon_Guillemot
59
+ California_Gull
60
+ Glaucous_winged_Gull
61
+ Heermann_Gull
62
+ Herring_Gull
63
+ Ivory_Gull
64
+ Ring_billed_Gull
65
+ Slaty_backed_Gull
66
+ Western_Gull
67
+ Anna_Hummingbird
68
+ Ruby_throated_Hummingbird
69
+ Rufous_Hummingbird
70
+ Green_Violetear
71
+ Long_tailed_Jaeger
72
+ Pomarine_Jaeger
73
+ Blue_Jay
74
+ Florida_Jay
75
+ Green_Jay
76
+ Dark_eyed_Junco
77
+ Tropical_Kingbird
78
+ Gray_Kingbird
79
+ Belted_Kingfisher
80
+ Green_Kingfisher
81
+ Pied_Kingfisher
82
+ Ringed_Kingfisher
83
+ White_breasted_Kingfisher
84
+ Red_legged_Kittiwake
85
+ Horned_Lark
86
+ Pacific_Loon
87
+ Mallard
88
+ Western_Meadowlark
89
+ Hooded_Merganser
90
+ Red_breasted_Merganser
91
+ Mockingbird
92
+ Nighthawk
93
+ White_breasted_Nuthatch
94
+ Baltimore_Oriole
95
+ Hooded_Oriole
96
+ Orchard_Oriole
97
+ Scott_Oriole
98
+ Ovenbird
99
+ Brown_Pelican
100
+ White_Pelican
101
+ Western_Wood_Pewee
102
+ Sayornis
103
+ American_Pipit
104
+ Horned_Puffin
105
+ Common_Raven
106
+ White_necked_Raven
107
+ American_Redstart
108
+ Geococcyx
109
+ Loggerhead_Shrike
110
+ Great_Grey_Shrike
111
+ Baird_Sparrow
112
+ Black_throated_Sparrow
113
+ Brewer_Sparrow
114
+ Chipping_Sparrow
115
+ Clay_colored_Sparrow
116
+ House_Sparrow
117
+ Field_Sparrow
118
+ Fox_Sparrow
119
+ Grasshopper_Sparrow
120
+ Harris_Sparrow
121
+ Henslow_Sparrow
122
+ Le_Conte_Sparrow
123
+ Lincoln_Sparrow
124
+ Nelson_Sharp_tailed_Sparrow
125
+ Savannah_Sparrow
126
+ Seaside_Sparrow
127
+ Song_Sparrow
128
+ Tree_Sparrow
129
+ Vesper_Sparrow
130
+ White_crowned_Sparrow
131
+ White_throated_Sparrow
132
+ Cape_Glossy_Starling
133
+ Bank_Swallow
134
+ Barn_Swallow
135
+ Cliff_Swallow
136
+ Tree_Swallow
137
+ Scarlet_Tanager
138
+ Summer_Tanager
139
+ Artic_Tern
140
+ Black_Tern
141
+ Caspian_Tern
142
+ Common_Tern
143
+ Elegant_Tern
144
+ Forsters_Tern
145
+ Least_Tern
146
+ Green_tailed_Towhee
147
+ Brown_Thrasher
148
+ Sage_Thrasher
149
+ Black_capped_Vireo
150
+ Blue_headed_Vireo
151
+ Philadelphia_Vireo
152
+ Red_eyed_Vireo
153
+ Warbling_Vireo
154
+ White_eyed_Vireo
155
+ Yellow_throated_Vireo
156
+ Bay_breasted_Warbler
157
+ Black_and_white_Warbler
158
+ Black_throated_Blue_Warbler
159
+ Blue_winged_Warbler
160
+ Canada_Warbler
161
+ Cape_May_Warbler
162
+ Cerulean_Warbler
163
+ Chestnut_sided_Warbler
164
+ Golden_winged_Warbler
165
+ Hooded_Warbler
166
+ Kentucky_Warbler
167
+ Magnolia_Warbler
168
+ Mourning_Warbler
169
+ Myrtle_Warbler
170
+ Nashville_Warbler
171
+ Orange_crowned_Warbler
172
+ Palm_Warbler
173
+ Pine_Warbler
174
+ Prairie_Warbler
175
+ Prothonotary_Warbler
176
+ Swainson_Warbler
177
+ Tennessee_Warbler
178
+ Wilson_Warbler
179
+ Worm_eating_Warbler
180
+ Yellow_Warbler
181
+ Northern_Waterthrush
182
+ Louisiana_Waterthrush
183
+ Bohemian_Waxwing
184
+ Cedar_Waxwing
185
+ American_Three_toed_Woodpecker
186
+ Pileated_Woodpecker
187
+ Red_bellied_Woodpecker
188
+ Red_cockaded_Woodpecker
189
+ Red_headed_Woodpecker
190
+ Downy_Woodpecker
191
+ Bewick_Wren
192
+ Cactus_Wren
193
+ Carolina_Wren
194
+ House_Wren
195
+ Marsh_Wren
196
+ Rock_Wren
197
+ Winter_Wren
198
+ Common_Yellowthroat
models/final_model_best.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:8e3d2fdf2326c16b778d163ceba96c51b69407ea4d4234f80093c2507af19fe2
3
+ size 787062709
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ torch
2
+ torchvision
3
+ gradio
4
+ timm
5
+ numpy
6
+ tqdm
7
+ Pillow