Arekku21 commited on
Commit
ae98750
1 Parent(s): 4e78428

Upload 9 files

Browse files
app.py ADDED
@@ -0,0 +1,110 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+
5
+ import torch
6
+ import torchvision.models as models
7
+ from torch import nn
8
+
9
+ from model import *
10
+
11
+ from albumentations import (
12
+ HorizontalFlip, VerticalFlip, IAAPerspective, ShiftScaleRotate, CLAHE, RandomRotate90,
13
+ Transpose, ShiftScaleRotate, Blur, OpticalDistortion, GridDistortion, HueSaturationValue,
14
+ IAAAdditiveGaussianNoise, GaussNoise, MotionBlur, MedianBlur, IAAPiecewiseAffine, RandomResizedCrop,
15
+ IAASharpen, IAAEmboss, RandomBrightnessContrast, Flip, OneOf, Compose, Normalize, Cutout, CoarseDropout, ShiftScaleRotate, CenterCrop, Resize, Rotate,
16
+ ShiftScaleRotate, CenterCrop, Crop, Resize, Rotate, RandomShadow, RandomSizedBBoxSafeCrop,
17
+ ChannelShuffle, MotionBlur,Lambda,SmallestMaxSize
18
+ )
19
+
20
+ from albumentations.pytorch import ToTensorV2
21
+
22
+ # Your Albumentations transformations
23
+ test_transforms = Compose([
24
+ Resize(224, 224),
25
+ Normalize(mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)),
26
+ ToTensorV2(),
27
+ ])
28
+
29
+ def load_cub200_classes():
30
+ """
31
+ This function loads the classes from the classes.txt file and returns a dictionary
32
+ """
33
+ with open("classes.txt", encoding="utf-8") as f:
34
+ classes = f.read().splitlines()
35
+
36
+ # convert classes to dictionary separating the lines by the first space
37
+ classes = {int(line.split(" ")[0]) : line.split(" ")[1] for line in classes}
38
+
39
+ # return the classes dictionary
40
+ return classes
41
+
42
+ def load_model():
43
+ """
44
+ This function loads the trained model and returns it
45
+ """
46
+ model = models.resnet50(pretrained=True)
47
+
48
+ # Freeze the initial layers up to a certain point (e.g., layer 4)
49
+ freeze_layers = 4
50
+ for idx, (name, param) in enumerate(model.named_parameters()):
51
+ if idx < freeze_layers:
52
+ param.requires_grad = False
53
+
54
+ # Replace the final fully connected layer for the new task
55
+ num_ftrs = model.fc.in_features
56
+ model.fc = nn.Linear(num_ftrs, 200)
57
+
58
+ # Add dropout layers
59
+ model = nn.Sequential(
60
+ model,
61
+ nn.Dropout(0.8), # Adjust the dropout rate as needed
62
+ nn.Linear(200, 200) # Add additional fully connected layer
63
+ )
64
+
65
+ # Load the state dictionary from the file
66
+ state_dict = torch.load("resnet50_7511.pt",map_location=torch.device('cpu'))
67
+
68
+ # Load the state dictionary into the model object
69
+ model.load_state_dict(state_dict)
70
+
71
+ # # Load actual model
72
+ # model = torch.load("resnet18.pth", map_location=torch.device('cpu'))
73
+
74
+ # set the model to evaluation mode
75
+ model.eval()
76
+
77
+ # return the model
78
+ return model
79
+
80
+ def predict_image(image):
81
+ """
82
+ This function takes an image as input and returns the class label
83
+ """
84
+ # load the model
85
+ model = load_model()
86
+ # model.eval()
87
+ # load the classes
88
+ classes = load_cub200_classes()
89
+
90
+ # Apply Albumentations transformations
91
+ transformed_image = test_transforms(image=image)['image']
92
+
93
+ # Convert the image to a PyTorch tensor
94
+ tensor_image = transformed_image.unsqueeze(0)
95
+
96
+ # Make prediction
97
+ with torch.no_grad():
98
+ output = model(tensor_image)
99
+
100
+ # Assuming the output is a tensor representing class probabilities
101
+ probabilities = torch.nn.functional.softmax(output[0], dim=0).numpy()
102
+
103
+ # Get the class with the highest probability
104
+ predicted_class = np.argmax(probabilities)
105
+
106
+ # Return the class label
107
+ return "Predicted Class: " + classes[predicted_class+1]
108
+
109
+ # create a gradio interface
110
+ gr.Interface(fn=predict_image, inputs="image", outputs="text").launch()
classes.txt ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 1 001.Black_footed_Albatross
2
+ 2 002.Laysan_Albatross
3
+ 3 003.Sooty_Albatross
4
+ 4 004.Groove_billed_Ani
5
+ 5 005.Crested_Auklet
6
+ 6 006.Least_Auklet
7
+ 7 007.Parakeet_Auklet
8
+ 8 008.Rhinoceros_Auklet
9
+ 9 009.Brewer_Blackbird
10
+ 10 010.Red_winged_Blackbird
11
+ 11 011.Rusty_Blackbird
12
+ 12 012.Yellow_headed_Blackbird
13
+ 13 013.Bobolink
14
+ 14 014.Indigo_Bunting
15
+ 15 015.Lazuli_Bunting
16
+ 16 016.Painted_Bunting
17
+ 17 017.Cardinal
18
+ 18 018.Spotted_Catbird
19
+ 19 019.Gray_Catbird
20
+ 20 020.Yellow_breasted_Chat
21
+ 21 021.Eastern_Towhee
22
+ 22 022.Chuck_will_Widow
23
+ 23 023.Brandt_Cormorant
24
+ 24 024.Red_faced_Cormorant
25
+ 25 025.Pelagic_Cormorant
26
+ 26 026.Bronzed_Cowbird
27
+ 27 027.Shiny_Cowbird
28
+ 28 028.Brown_Creeper
29
+ 29 029.American_Crow
30
+ 30 030.Fish_Crow
31
+ 31 031.Black_billed_Cuckoo
32
+ 32 032.Mangrove_Cuckoo
33
+ 33 033.Yellow_billed_Cuckoo
34
+ 34 034.Gray_crowned_Rosy_Finch
35
+ 35 035.Purple_Finch
36
+ 36 036.Northern_Flicker
37
+ 37 037.Acadian_Flycatcher
38
+ 38 038.Great_Crested_Flycatcher
39
+ 39 039.Least_Flycatcher
40
+ 40 040.Olive_sided_Flycatcher
41
+ 41 041.Scissor_tailed_Flycatcher
42
+ 42 042.Vermilion_Flycatcher
43
+ 43 043.Yellow_bellied_Flycatcher
44
+ 44 044.Frigatebird
45
+ 45 045.Northern_Fulmar
46
+ 46 046.Gadwall
47
+ 47 047.American_Goldfinch
48
+ 48 048.European_Goldfinch
49
+ 49 049.Boat_tailed_Grackle
50
+ 50 050.Eared_Grebe
51
+ 51 051.Horned_Grebe
52
+ 52 052.Pied_billed_Grebe
53
+ 53 053.Western_Grebe
54
+ 54 054.Blue_Grosbeak
55
+ 55 055.Evening_Grosbeak
56
+ 56 056.Pine_Grosbeak
57
+ 57 057.Rose_breasted_Grosbeak
58
+ 58 058.Pigeon_Guillemot
59
+ 59 059.California_Gull
60
+ 60 060.Glaucous_winged_Gull
61
+ 61 061.Heermann_Gull
62
+ 62 062.Herring_Gull
63
+ 63 063.Ivory_Gull
64
+ 64 064.Ring_billed_Gull
65
+ 65 065.Slaty_backed_Gull
66
+ 66 066.Western_Gull
67
+ 67 067.Anna_Hummingbird
68
+ 68 068.Ruby_throated_Hummingbird
69
+ 69 069.Rufous_Hummingbird
70
+ 70 070.Green_Violetear
71
+ 71 071.Long_tailed_Jaeger
72
+ 72 072.Pomarine_Jaeger
73
+ 73 073.Blue_Jay
74
+ 74 074.Florida_Jay
75
+ 75 075.Green_Jay
76
+ 76 076.Dark_eyed_Junco
77
+ 77 077.Tropical_Kingbird
78
+ 78 078.Gray_Kingbird
79
+ 79 079.Belted_Kingfisher
80
+ 80 080.Green_Kingfisher
81
+ 81 081.Pied_Kingfisher
82
+ 82 082.Ringed_Kingfisher
83
+ 83 083.White_breasted_Kingfisher
84
+ 84 084.Red_legged_Kittiwake
85
+ 85 085.Horned_Lark
86
+ 86 086.Pacific_Loon
87
+ 87 087.Mallard
88
+ 88 088.Western_Meadowlark
89
+ 89 089.Hooded_Merganser
90
+ 90 090.Red_breasted_Merganser
91
+ 91 091.Mockingbird
92
+ 92 092.Nighthawk
93
+ 93 093.Clark_Nutcracker
94
+ 94 094.White_breasted_Nuthatch
95
+ 95 095.Baltimore_Oriole
96
+ 96 096.Hooded_Oriole
97
+ 97 097.Orchard_Oriole
98
+ 98 098.Scott_Oriole
99
+ 99 099.Ovenbird
100
+ 100 100.Brown_Pelican
101
+ 101 101.White_Pelican
102
+ 102 102.Western_Wood_Pewee
103
+ 103 103.Sayornis
104
+ 104 104.American_Pipit
105
+ 105 105.Whip_poor_Will
106
+ 106 106.Horned_Puffin
107
+ 107 107.Common_Raven
108
+ 108 108.White_necked_Raven
109
+ 109 109.American_Redstart
110
+ 110 110.Geococcyx
111
+ 111 111.Loggerhead_Shrike
112
+ 112 112.Great_Grey_Shrike
113
+ 113 113.Baird_Sparrow
114
+ 114 114.Black_throated_Sparrow
115
+ 115 115.Brewer_Sparrow
116
+ 116 116.Chipping_Sparrow
117
+ 117 117.Clay_colored_Sparrow
118
+ 118 118.House_Sparrow
119
+ 119 119.Field_Sparrow
120
+ 120 120.Fox_Sparrow
121
+ 121 121.Grasshopper_Sparrow
122
+ 122 122.Harris_Sparrow
123
+ 123 123.Henslow_Sparrow
124
+ 124 124.Le_Conte_Sparrow
125
+ 125 125.Lincoln_Sparrow
126
+ 126 126.Nelson_Sharp_tailed_Sparrow
127
+ 127 127.Savannah_Sparrow
128
+ 128 128.Seaside_Sparrow
129
+ 129 129.Song_Sparrow
130
+ 130 130.Tree_Sparrow
131
+ 131 131.Vesper_Sparrow
132
+ 132 132.White_crowned_Sparrow
133
+ 133 133.White_throated_Sparrow
134
+ 134 134.Cape_Glossy_Starling
135
+ 135 135.Bank_Swallow
136
+ 136 136.Barn_Swallow
137
+ 137 137.Cliff_Swallow
138
+ 138 138.Tree_Swallow
139
+ 139 139.Scarlet_Tanager
140
+ 140 140.Summer_Tanager
141
+ 141 141.Artic_Tern
142
+ 142 142.Black_Tern
143
+ 143 143.Caspian_Tern
144
+ 144 144.Common_Tern
145
+ 145 145.Elegant_Tern
146
+ 146 146.Forsters_Tern
147
+ 147 147.Least_Tern
148
+ 148 148.Green_tailed_Towhee
149
+ 149 149.Brown_Thrasher
150
+ 150 150.Sage_Thrasher
151
+ 151 151.Black_capped_Vireo
152
+ 152 152.Blue_headed_Vireo
153
+ 153 153.Philadelphia_Vireo
154
+ 154 154.Red_eyed_Vireo
155
+ 155 155.Warbling_Vireo
156
+ 156 156.White_eyed_Vireo
157
+ 157 157.Yellow_throated_Vireo
158
+ 158 158.Bay_breasted_Warbler
159
+ 159 159.Black_and_white_Warbler
160
+ 160 160.Black_throated_Blue_Warbler
161
+ 161 161.Blue_winged_Warbler
162
+ 162 162.Canada_Warbler
163
+ 163 163.Cape_May_Warbler
164
+ 164 164.Cerulean_Warbler
165
+ 165 165.Chestnut_sided_Warbler
166
+ 166 166.Golden_winged_Warbler
167
+ 167 167.Hooded_Warbler
168
+ 168 168.Kentucky_Warbler
169
+ 169 169.Magnolia_Warbler
170
+ 170 170.Mourning_Warbler
171
+ 171 171.Myrtle_Warbler
172
+ 172 172.Nashville_Warbler
173
+ 173 173.Orange_crowned_Warbler
174
+ 174 174.Palm_Warbler
175
+ 175 175.Pine_Warbler
176
+ 176 176.Prairie_Warbler
177
+ 177 177.Prothonotary_Warbler
178
+ 178 178.Swainson_Warbler
179
+ 179 179.Tennessee_Warbler
180
+ 180 180.Wilson_Warbler
181
+ 181 181.Worm_eating_Warbler
182
+ 182 182.Yellow_Warbler
183
+ 183 183.Northern_Waterthrush
184
+ 184 184.Louisiana_Waterthrush
185
+ 185 185.Bohemian_Waxwing
186
+ 186 186.Cedar_Waxwing
187
+ 187 187.American_Three_toed_Woodpecker
188
+ 188 188.Pileated_Woodpecker
189
+ 189 189.Red_bellied_Woodpecker
190
+ 190 190.Red_cockaded_Woodpecker
191
+ 191 191.Red_headed_Woodpecker
192
+ 192 192.Downy_Woodpecker
193
+ 193 193.Bewick_Wren
194
+ 194 194.Cactus_Wren
195
+ 195 195.Carolina_Wren
196
+ 196 196.House_Wren
197
+ 197 197.Marsh_Wren
198
+ 198 198.Rock_Wren
199
+ 199 199.Winter_Wren
200
+ 200 200.Common_Yellowthroat
cub_classification_resnet.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:621c61bf81ea6e924af6ce58411b4bb470823449c979bb3a17eedf9cc63caf62
3
+ size 95999162
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ numpy
4
+ albumentations
resnet.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1ab6a64117652a080b15b8e206e989de1c4c854da5e1c877032584a80dd836a8
3
+ size 102540270
resnet101.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fa9d223819049f287a048585b803e89cb06afeef159dd0f5f90d88df03f8629d
3
+ size 172320698
resnet18.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:6ae575d4c22919fd951dc28d865993d0e6366d9a7f5b9ca0250ed45d79e52ac2
3
+ size 45371066
resnet50_7511.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2ce0d050b523cc67485a6bc94e7fe57c74ffec4622d51a500c7f0554c56988d2
3
+ size 96152908
resnet_classification.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:7cbfd21d1fbf2bdae9246a55e7a33c6427311a2bad0f2a41185528dac702024e
3
+ size 96022698