hitty commited on
Commit
fca4368
·
1 Parent(s): 079e920

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +264 -263
app.py CHANGED
@@ -1,272 +1,273 @@
1
- # import gradio as gr
2
- # import tensorflow as tf
3
- # import numpy as np
4
- # from model import create_effnetb2_model
5
- # from timeit import default_timer as timer
6
- # from typing import Tuple, Dict
7
- # from tensorflow.keras.applications import EfficientNetB2
8
- # from tensorflow.keras.preprocessing import image
9
- # from tensorflow.keras.applications.efficientnet import preprocess_input
10
- # tf.config.set_visible_devices([], 'GPU')
11
- # # Load the TensorFlow model
12
- # model_path = "./Vegetable/Vegetable.h5" # Replace with the actual path to your saved TensorFlow model
13
- # # model = tf.keras.models.load_model(model_path)
14
-
15
- # # Class names
16
- # class_names = ['Bean', 'Bitter_Gourd', 'Bottle_Gourd', 'Brinjal', 'Broccoli,' 'Cabbage','Capsicum', 'Carrot', 'Cauliflower', 'Cucumber', 'Papaya', 'Potato', 'Pumpkin','Radish','Tomato',]
17
- # # Create EffNetB2 model
18
- # effnetb2= create_effnetb2_model(
19
- # num_classes=15, # len(class_names) would also work
20
- # )
21
- # # Load saved weights
22
- # effnetb2.load_weights(model_path)
23
-
24
- # # Define the predict function
25
- # def predict(image):
26
- # # Preprocess the image
27
- # image = np.array(image)
28
- # image = tf.image.resize(image, (224, 224))
29
- # image = tf.expand_dims(image, axis=0)
30
- # image = image / 255.0
31
-
32
- # # Make the prediction
33
- # prediction = model.predict(image)[0]
34
- # probabilities = tf.nn.softmax(prediction)
35
- # pred_labels_and_probs = {class_names[i]: float(probabilities[i]) for i in range(len(class_names))}
36
-
37
- # return pred_labels_and_probs
38
-
39
- # # Define the Gradio interface
40
- # title = "Vegetable Classifier 🍅🥔🥕🫑"
41
- # description = "A TensorFlow computer vision model to classify images of Vegetables as Tomato, Potato, or cucumber."
42
- # inputs = gr.inputs.Image()
43
- # outputs = gr.outputs.Label(num_top_classes=15)
44
- # interface = gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title=title, description=description)
45
-
46
- # # Launch the Gradio interface
47
- # interface.launch()
48
 
49
- import tensorflow as tf
50
- import tensorflow_hub as hub
51
 
52
- import requests
53
- from PIL import Image
54
- from io import BytesIO
55
 
56
- import matplotlib.pyplot as plt
57
- import numpy as np
58
- import gradio as gr
 
 
 
 
59
 
60
- #@title Helper functions for loading image (hidden)
61
-
62
- original_image_cache = {}
63
-
64
- def preprocess_image(image):
65
- image = np.array(image)
66
- # reshape into shape [batch_size, height, width, num_channels]
67
- img_reshaped = tf.reshape(image, [1, image.shape[0], image.shape[1], image.shape[2]])
68
- # Use `convert_image_dtype` to convert to floats in the [0,1] range.
69
- image = tf.image.convert_image_dtype(img_reshaped, tf.float32)
70
- return image
71
-
72
- def load_image_from_url(img_url):
73
- """Returns an image with shape [1, height, width, num_channels]."""
74
- user_agent = {'User-agent': 'Colab Sample (https://tensorflow.org)'}
75
- response = requests.get(img_url, headers=user_agent)
76
- image = Image.open(BytesIO(response.content))
77
- image = preprocess_image(image)
78
- return image
79
-
80
- def load_image(image_url, image_size=256, dynamic_size=False, max_dynamic_size=512):
81
- """Loads and preprocesses images."""
82
- # Cache image file locally.
83
- if image_url in original_image_cache:
84
- img = original_image_cache[image_url]
85
- elif image_url.startswith('https://'):
86
- img = load_image_from_url(image_url)
87
- else:
88
- fd = tf.io.gfile.GFile(image_url, 'rb')
89
- img = preprocess_image(Image.open(fd))
90
- original_image_cache[image_url] = img
91
- # Load and convert to float32 numpy array, add batch dimension, and normalize to range [0, 1].
92
- img_raw = img
93
- if tf.reduce_max(img) > 1.0:
94
- img = img / 255.
95
- if len(img.shape) == 3:
96
- img = tf.stack([img, img, img], axis=-1)
97
- if not dynamic_size:
98
- img = tf.image.resize_with_pad(img, image_size, image_size)
99
- elif img.shape[1] > max_dynamic_size or img.shape[2] > max_dynamic_size:
100
- img = tf.image.resize_with_pad(img, max_dynamic_size, max_dynamic_size)
101
- return img, img_raw
102
-
103
-
104
-
105
- image_size = 224
106
- dynamic_size = False
107
-
108
- model_name = "efficientnet_b0"
109
-
110
- model_handle_map = {
111
- "efficientnetv2-s": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/classification/2",
112
- "efficientnetv2-m": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_m/classification/2",
113
- "efficientnetv2-l": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_l/classification/2",
114
- "efficientnetv2-s-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_s/classification/2",
115
- "efficientnetv2-m-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_m/classification/2",
116
- "efficientnetv2-l-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_l/classification/2",
117
- "efficientnetv2-xl-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_xl/classification/2",
118
- "efficientnetv2-b0-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b0/classification/2",
119
- "efficientnetv2-b1-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b1/classification/2",
120
- "efficientnetv2-b2-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b2/classification/2",
121
- "efficientnetv2-b3-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b3/classification/2",
122
- "efficientnetv2-s-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_s/classification/2",
123
- "efficientnetv2-m-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_m/classification/2",
124
- "efficientnetv2-l-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_l/classification/2",
125
- "efficientnetv2-xl-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_xl/classification/2",
126
- "efficientnetv2-b0-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b0/classification/2",
127
- "efficientnetv2-b1-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b1/classification/2",
128
- "efficientnetv2-b2-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b2/classification/2",
129
- "efficientnetv2-b3-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b3/classification/2",
130
- "efficientnetv2-b0": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b0/classification/2",
131
- "efficientnetv2-b1": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b1/classification/2",
132
- "efficientnetv2-b2": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b2/classification/2",
133
- "efficientnetv2-b3": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b3/classification/2",
134
- "efficientnet_b0": "https://tfhub.dev/tensorflow/efficientnet/b0/classification/1",
135
- "efficientnet_b1": "https://tfhub.dev/tensorflow/efficientnet/b1/classification/1",
136
- "efficientnet_b2": "https://tfhub.dev/tensorflow/efficientnet/b2/classification/1",
137
- "efficientnet_b3": "https://tfhub.dev/tensorflow/efficientnet/b3/classification/1",
138
- "efficientnet_b4": "https://tfhub.dev/tensorflow/efficientnet/b4/classification/1",
139
- "efficientnet_b5": "https://tfhub.dev/tensorflow/efficientnet/b5/classification/1",
140
- "efficientnet_b6": "https://tfhub.dev/tensorflow/efficientnet/b6/classification/1",
141
- "efficientnet_b7": "https://tfhub.dev/tensorflow/efficientnet/b7/classification/1",
142
- "bit_s-r50x1": "https://tfhub.dev/google/bit/s-r50x1/ilsvrc2012_classification/1",
143
- "inception_v3": "https://tfhub.dev/google/imagenet/inception_v3/classification/4",
144
- "inception_resnet_v2": "https://tfhub.dev/google/imagenet/inception_resnet_v2/classification/4",
145
- "resnet_v1_50": "https://tfhub.dev/google/imagenet/resnet_v1_50/classification/4",
146
- "resnet_v1_101": "https://tfhub.dev/google/imagenet/resnet_v1_101/classification/4",
147
- "resnet_v1_152": "https://tfhub.dev/google/imagenet/resnet_v1_152/classification/4",
148
- "resnet_v2_50": "https://tfhub.dev/google/imagenet/resnet_v2_50/classification/4",
149
- "resnet_v2_101": "https://tfhub.dev/google/imagenet/resnet_v2_101/classification/4",
150
- "resnet_v2_152": "https://tfhub.dev/google/imagenet/resnet_v2_152/classification/4",
151
- "nasnet_large": "https://tfhub.dev/google/imagenet/nasnet_large/classification/4",
152
- "nasnet_mobile": "https://tfhub.dev/google/imagenet/nasnet_mobile/classification/4",
153
- "pnasnet_large": "https://tfhub.dev/google/imagenet/pnasnet_large/classification/4",
154
- "mobilenet_v2_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/4",
155
- "mobilenet_v2_130_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4",
156
- "mobilenet_v2_140_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/4",
157
- "mobilenet_v3_small_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_100_224/classification/5",
158
- "mobilenet_v3_small_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_075_224/classification/5",
159
- "mobilenet_v3_large_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_100_224/classification/5",
160
- "mobilenet_v3_large_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_075_224/classification/5",
161
- }
162
-
163
- model_image_size_map = {
164
- "efficientnetv2-s": 384,
165
- "efficientnetv2-m": 480,
166
- "efficientnetv2-l": 480,
167
- "efficientnetv2-b0": 224,
168
- "efficientnetv2-b1": 240,
169
- "efficientnetv2-b2": 260,
170
- "efficientnetv2-b3": 300,
171
- "efficientnetv2-s-21k": 384,
172
- "efficientnetv2-m-21k": 480,
173
- "efficientnetv2-l-21k": 480,
174
- "efficientnetv2-xl-21k": 512,
175
- "efficientnetv2-b0-21k": 224,
176
- "efficientnetv2-b1-21k": 240,
177
- "efficientnetv2-b2-21k": 260,
178
- "efficientnetv2-b3-21k": 300,
179
- "efficientnetv2-s-21k-ft1k": 384,
180
- "efficientnetv2-m-21k-ft1k": 480,
181
- "efficientnetv2-l-21k-ft1k": 480,
182
- "efficientnetv2-xl-21k-ft1k": 512,
183
- "efficientnetv2-b0-21k-ft1k": 224,
184
- "efficientnetv2-b1-21k-ft1k": 240,
185
- "efficientnetv2-b2-21k-ft1k": 260,
186
- "efficientnetv2-b3-21k-ft1k": 300,
187
- "efficientnet_b0": 224,
188
- "efficientnet_b1": 240,
189
- "efficientnet_b2": 260,
190
- "efficientnet_b3": 300,
191
- "efficientnet_b4": 380,
192
- "efficientnet_b5": 456,
193
- "efficientnet_b6": 528,
194
- "efficientnet_b7": 600,
195
- "inception_v3": 299,
196
- "inception_resnet_v2": 299,
197
- "mobilenet_v2_100_224": 224,
198
- "mobilenet_v2_130_224": 224,
199
- "mobilenet_v2_140_224": 224,
200
- "nasnet_large": 331,
201
- "nasnet_mobile": 224,
202
- "pnasnet_large": 331,
203
- "resnet_v1_50": 224,
204
- "resnet_v1_101": 224,
205
- "resnet_v1_152": 224,
206
- "resnet_v2_50": 224,
207
- "resnet_v2_101": 224,
208
- "resnet_v2_152": 224,
209
- "mobilenet_v3_small_100_224": 224,
210
- "mobilenet_v3_small_075_224": 224,
211
- "mobilenet_v3_large_100_224": 224,
212
- "mobilenet_v3_large_075_224": 224,
213
- }
214
-
215
- model_handle = model_handle_map[model_name]
216
-
217
-
218
- max_dynamic_size = 512
219
- if model_name in model_image_size_map:
220
- image_size = model_image_size_map[model_name]
221
- dynamic_size = False
222
- print(f"Images will be converted to {image_size}x{image_size}")
223
- else:
224
- dynamic_size = True
225
- print(f"Images will be capped to a max size of {max_dynamic_size}x{max_dynamic_size}")
226
-
227
- labels_file = "https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt"
228
-
229
- #download labels and creates a maps
230
- downloaded_file = tf.keras.utils.get_file("labels.txt", origin=labels_file)
231
-
232
- classes = []
233
-
234
- with open(downloaded_file) as f:
235
- labels = f.readlines()
236
- classes = [l.strip() for l in labels]
237
-
238
-
239
- classifier = hub.load(model_handle)
240
-
241
-
242
- def inference(img):
243
- image, original_image = load_image(img, image_size, dynamic_size, max_dynamic_size)
244
 
245
 
246
- input_shape = image.shape
247
- warmup_input = tf.random.uniform(input_shape, 0, 1.0)
248
- warmup_logits = classifier(warmup_input).numpy()
249
 
250
- # Run model on image
251
- probabilities = tf.nn.softmax(classifier(image)).numpy()
252
 
253
- top_5 = tf.argsort(probabilities, axis=-1, direction="DESCENDING")[0][:5].numpy()
254
- np_classes = np.array(classes)
255
 
256
- # Some models include an additional 'background' class in the predictions, so
257
- # we must account for this when reading the class labels.
258
- includes_background_class = probabilities.shape[1] == 1001
259
- result = {}
260
- for i, item in enumerate(top_5):
261
- class_index = item if includes_background_class else item + 1
262
- line = f'({i+1}) {class_index:4} - {classes[class_index]}: {probabilities[0][top_5][i]}'
263
- result[classes[class_index]] = probabilities[0][top_5][i].item()
264
- return result
265
-
266
- title="efficientnet_b0"
267
- description="Gradio Demo for efficientnet_b0: Imagenet (ILSVRC-2012-CLS) classification with EfficientNet-B0. To use it, simply upload your image or click on one of the examples to load them. Read more at the links below"
268
-
269
- article = "<p style='text-align: center'><a href='https://tfhub.dev/google/efficientnet/b0/classification/1' target='_blank'>Tensorflow Hub</a></p>"
270
- # examples=[['apple1.jpg']]
271
-
272
- gr.Interface(inference,gr.inputs.Image(type="filepath"),"label",title=title,description=description,article=article).launch(enable_queue=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
 
 
2
 
3
+ # import tensorflow as tf
4
+ # import tensorflow_hub as hub
 
5
 
6
+ # import requests
7
+ # from PIL import Image
8
+ # from io import BytesIO
9
+
10
+ # import matplotlib.pyplot as plt
11
+ # import numpy as np
12
+ # import gradio as gr
13
 
14
+ # #@title Helper functions for loading image (hidden)
15
+
16
+ # original_image_cache = {}
17
+
18
+ # def preprocess_image(image):
19
+ # image = np.array(image)
20
+ # # reshape into shape [batch_size, height, width, num_channels]
21
+ # img_reshaped = tf.reshape(image, [1, image.shape[0], image.shape[1], image.shape[2]])
22
+ # # Use `convert_image_dtype` to convert to floats in the [0,1] range.
23
+ # image = tf.image.convert_image_dtype(img_reshaped, tf.float32)
24
+ # return image
25
+
26
+ # def load_image_from_url(img_url):
27
+ # """Returns an image with shape [1, height, width, num_channels]."""
28
+ # user_agent = {'User-agent': 'Colab Sample (https://tensorflow.org)'}
29
+ # response = requests.get(img_url, headers=user_agent)
30
+ # image = Image.open(BytesIO(response.content))
31
+ # image = preprocess_image(image)
32
+ # return image
33
+
34
+ # def load_image(image_url, image_size=256, dynamic_size=False, max_dynamic_size=512):
35
+ # """Loads and preprocesses images."""
36
+ # # Cache image file locally.
37
+ # if image_url in original_image_cache:
38
+ # img = original_image_cache[image_url]
39
+ # elif image_url.startswith('https://'):
40
+ # img = load_image_from_url(image_url)
41
+ # else:
42
+ # fd = tf.io.gfile.GFile(image_url, 'rb')
43
+ # img = preprocess_image(Image.open(fd))
44
+ # original_image_cache[image_url] = img
45
+ # # Load and convert to float32 numpy array, add batch dimension, and normalize to range [0, 1].
46
+ # img_raw = img
47
+ # if tf.reduce_max(img) > 1.0:
48
+ # img = img / 255.
49
+ # if len(img.shape) == 3:
50
+ # img = tf.stack([img, img, img], axis=-1)
51
+ # if not dynamic_size:
52
+ # img = tf.image.resize_with_pad(img, image_size, image_size)
53
+ # elif img.shape[1] > max_dynamic_size or img.shape[2] > max_dynamic_size:
54
+ # img = tf.image.resize_with_pad(img, max_dynamic_size, max_dynamic_size)
55
+ # return img, img_raw
56
+
57
+
58
+
59
+ # image_size = 224
60
+ # dynamic_size = False
61
+
62
+ # model_name = "efficientnet_b0"
63
+
64
+ # model_handle_map = {
65
+ # "efficientnetv2-s": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/classification/2",
66
+ # "efficientnetv2-m": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_m/classification/2",
67
+ # "efficientnetv2-l": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_l/classification/2",
68
+ # "efficientnetv2-s-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_s/classification/2",
69
+ # "efficientnetv2-m-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_m/classification/2",
70
+ # "efficientnetv2-l-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_l/classification/2",
71
+ # "efficientnetv2-xl-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_xl/classification/2",
72
+ # "efficientnetv2-b0-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b0/classification/2",
73
+ # "efficientnetv2-b1-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b1/classification/2",
74
+ # "efficientnetv2-b2-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b2/classification/2",
75
+ # "efficientnetv2-b3-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b3/classification/2",
76
+ # "efficientnetv2-s-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_s/classification/2",
77
+ # "efficientnetv2-m-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_m/classification/2",
78
+ # "efficientnetv2-l-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_l/classification/2",
79
+ # "efficientnetv2-xl-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_xl/classification/2",
80
+ # "efficientnetv2-b0-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b0/classification/2",
81
+ # "efficientnetv2-b1-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b1/classification/2",
82
+ # "efficientnetv2-b2-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b2/classification/2",
83
+ # "efficientnetv2-b3-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b3/classification/2",
84
+ # "efficientnetv2-b0": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b0/classification/2",
85
+ # "efficientnetv2-b1": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b1/classification/2",
86
+ # "efficientnetv2-b2": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b2/classification/2",
87
+ # "efficientnetv2-b3": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b3/classification/2",
88
+ # "efficientnet_b0": "https://tfhub.dev/tensorflow/efficientnet/b0/classification/1",
89
+ # "efficientnet_b1": "https://tfhub.dev/tensorflow/efficientnet/b1/classification/1",
90
+ # "efficientnet_b2": "https://tfhub.dev/tensorflow/efficientnet/b2/classification/1",
91
+ # "efficientnet_b3": "https://tfhub.dev/tensorflow/efficientnet/b3/classification/1",
92
+ # "efficientnet_b4": "https://tfhub.dev/tensorflow/efficientnet/b4/classification/1",
93
+ # "efficientnet_b5": "https://tfhub.dev/tensorflow/efficientnet/b5/classification/1",
94
+ # "efficientnet_b6": "https://tfhub.dev/tensorflow/efficientnet/b6/classification/1",
95
+ # "efficientnet_b7": "https://tfhub.dev/tensorflow/efficientnet/b7/classification/1",
96
+ # "bit_s-r50x1": "https://tfhub.dev/google/bit/s-r50x1/ilsvrc2012_classification/1",
97
+ # "inception_v3": "https://tfhub.dev/google/imagenet/inception_v3/classification/4",
98
+ # "inception_resnet_v2": "https://tfhub.dev/google/imagenet/inception_resnet_v2/classification/4",
99
+ # "resnet_v1_50": "https://tfhub.dev/google/imagenet/resnet_v1_50/classification/4",
100
+ # "resnet_v1_101": "https://tfhub.dev/google/imagenet/resnet_v1_101/classification/4",
101
+ # "resnet_v1_152": "https://tfhub.dev/google/imagenet/resnet_v1_152/classification/4",
102
+ # "resnet_v2_50": "https://tfhub.dev/google/imagenet/resnet_v2_50/classification/4",
103
+ # "resnet_v2_101": "https://tfhub.dev/google/imagenet/resnet_v2_101/classification/4",
104
+ # "resnet_v2_152": "https://tfhub.dev/google/imagenet/resnet_v2_152/classification/4",
105
+ # "nasnet_large": "https://tfhub.dev/google/imagenet/nasnet_large/classification/4",
106
+ # "nasnet_mobile": "https://tfhub.dev/google/imagenet/nasnet_mobile/classification/4",
107
+ # "pnasnet_large": "https://tfhub.dev/google/imagenet/pnasnet_large/classification/4",
108
+ # "mobilenet_v2_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/4",
109
+ # "mobilenet_v2_130_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4",
110
+ # "mobilenet_v2_140_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/4",
111
+ # "mobilenet_v3_small_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_100_224/classification/5",
112
+ # "mobilenet_v3_small_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_075_224/classification/5",
113
+ # "mobilenet_v3_large_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_100_224/classification/5",
114
+ # "mobilenet_v3_large_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_075_224/classification/5",
115
+ # }
116
+
117
+ # model_image_size_map = {
118
+ # "efficientnetv2-s": 384,
119
+ # "efficientnetv2-m": 480,
120
+ # "efficientnetv2-l": 480,
121
+ # "efficientnetv2-b0": 224,
122
+ # "efficientnetv2-b1": 240,
123
+ # "efficientnetv2-b2": 260,
124
+ # "efficientnetv2-b3": 300,
125
+ # "efficientnetv2-s-21k": 384,
126
+ # "efficientnetv2-m-21k": 480,
127
+ # "efficientnetv2-l-21k": 480,
128
+ # "efficientnetv2-xl-21k": 512,
129
+ # "efficientnetv2-b0-21k": 224,
130
+ # "efficientnetv2-b1-21k": 240,
131
+ # "efficientnetv2-b2-21k": 260,
132
+ # "efficientnetv2-b3-21k": 300,
133
+ # "efficientnetv2-s-21k-ft1k": 384,
134
+ # "efficientnetv2-m-21k-ft1k": 480,
135
+ # "efficientnetv2-l-21k-ft1k": 480,
136
+ # "efficientnetv2-xl-21k-ft1k": 512,
137
+ # "efficientnetv2-b0-21k-ft1k": 224,
138
+ # "efficientnetv2-b1-21k-ft1k": 240,
139
+ # "efficientnetv2-b2-21k-ft1k": 260,
140
+ # "efficientnetv2-b3-21k-ft1k": 300,
141
+ # "efficientnet_b0": 224,
142
+ # "efficientnet_b1": 240,
143
+ # "efficientnet_b2": 260,
144
+ # "efficientnet_b3": 300,
145
+ # "efficientnet_b4": 380,
146
+ # "efficientnet_b5": 456,
147
+ # "efficientnet_b6": 528,
148
+ # "efficientnet_b7": 600,
149
+ # "inception_v3": 299,
150
+ # "inception_resnet_v2": 299,
151
+ # "mobilenet_v2_100_224": 224,
152
+ # "mobilenet_v2_130_224": 224,
153
+ # "mobilenet_v2_140_224": 224,
154
+ # "nasnet_large": 331,
155
+ # "nasnet_mobile": 224,
156
+ # "pnasnet_large": 331,
157
+ # "resnet_v1_50": 224,
158
+ # "resnet_v1_101": 224,
159
+ # "resnet_v1_152": 224,
160
+ # "resnet_v2_50": 224,
161
+ # "resnet_v2_101": 224,
162
+ # "resnet_v2_152": 224,
163
+ # "mobilenet_v3_small_100_224": 224,
164
+ # "mobilenet_v3_small_075_224": 224,
165
+ # "mobilenet_v3_large_100_224": 224,
166
+ # "mobilenet_v3_large_075_224": 224,
167
+ # }
168
+
169
+ # model_handle = model_handle_map[model_name]
170
+
171
+
172
+ # max_dynamic_size = 512
173
+ # if model_name in model_image_size_map:
174
+ # image_size = model_image_size_map[model_name]
175
+ # dynamic_size = False
176
+ # print(f"Images will be converted to {image_size}x{image_size}")
177
+ # else:
178
+ # dynamic_size = True
179
+ # print(f"Images will be capped to a max size of {max_dynamic_size}x{max_dynamic_size}")
180
+
181
+ # labels_file = "https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt"
182
+
183
+ # #download labels and creates a maps
184
+ # downloaded_file = tf.keras.utils.get_file("labels.txt", origin=labels_file)
185
+
186
+ # classes = []
187
+
188
+ # with open(downloaded_file) as f:
189
+ # labels = f.readlines()
190
+ # classes = [l.strip() for l in labels]
191
+
192
+
193
+ # classifier = hub.load(model_handle)
194
+
195
+
196
+ # def inference(img):
197
+ # image, original_image = load_image(img, image_size, dynamic_size, max_dynamic_size)
198
 
199
 
200
+ # input_shape = image.shape
201
+ # warmup_input = tf.random.uniform(input_shape, 0, 1.0)
202
+ # warmup_logits = classifier(warmup_input).numpy()
203
 
204
+ # # Run model on image
205
+ # probabilities = tf.nn.softmax(classifier(image)).numpy()
206
 
207
+ # top_5 = tf.argsort(probabilities, axis=-1, direction="DESCENDING")[0][:5].numpy()
208
+ # np_classes = np.array(classes)
209
 
210
+ # # Some models include an additional 'background' class in the predictions, so
211
+ # # we must account for this when reading the class labels.
212
+ # includes_background_class = probabilities.shape[1] == 1001
213
+ # result = {}
214
+ # for i, item in enumerate(top_5):
215
+ # class_index = item if includes_background_class else item + 1
216
+ # line = f'({i+1}) {class_index:4} - {classes[class_index]}: {probabilities[0][top_5][i]}'
217
+ # result[classes[class_index]] = probabilities[0][top_5][i].item()
218
+ # return result
219
+
220
+ # title="efficientnet_b0"
221
+ # description="Gradio Demo for efficientnet_b0: Imagenet (ILSVRC-2012-CLS) classification with EfficientNet-B0. To use it, simply upload your image or click on one of the examples to load them. Read more at the links below"
222
+
223
+ # article = "<p style='text-align: center'><a href='https://tfhub.dev/google/efficientnet/b0/classification/1' target='_blank'>Tensorflow Hub</a></p>"
224
+ # # examples=[['apple1.jpg']]
225
+
226
+ # gr.Interface(inference,gr.inputs.Image(type="filepath"),"label",title=title,description=description,article=article).launch(enable_queue=True)
227
+
228
+
229
+
230
+
231
+
232
+
233
+
234
+
235
+
236
+
237
+
238
+
239
+
240
+ import gradio as gr
241
+ from PIL import Image
242
+ import tensorflow as tf
243
+ from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
244
+
245
+ # Load pre-trained model
246
+ model = tf.keras.applications.ResNet50(weights='imagenet')
247
+
248
+ # Define prediction function
249
+ def classify_image(image):
250
+ # Preprocess image
251
+ img_array = tf.keras.preprocessing.image.img_to_array(image)
252
+ img_array = tf.expand_dims(img_array, 0)
253
+ img_array = preprocess_input(img_array)
254
+
255
+ # Make prediction
256
+ predictions = model.predict(img_array)
257
+ decoded_predictions = decode_predictions(predictions, top=3)[0]
258
+
259
+ # Format predictions
260
+ results = []
261
+ for pred in decoded_predictions:
262
+ label = pred[1]
263
+ confidence = pred[2] * 100
264
+ results.append(f'Class: {label}, Confidence: {confidence:.2f}%')
265
+
266
+ return results
267
+
268
+ # Create image classification interface
269
+ image_input = gr.inputs.Image()
270
+ label_output = gr.outputs.Textbox()
271
+
272
+ interface = gr.Interface(fn=classify_image, inputs=image_input, outputs=label_output, title='Image Classification App')
273
+ interface.launch()