Ahsen Khaliq commited on
Commit
fa0722e
·
1 Parent(s): 9301f50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -16
app.py CHANGED
@@ -10,24 +10,36 @@ import PIL
10
  import io
11
  import matplotlib.pyplot as plt
12
 
13
- def download_image(url):
14
- resp = requests.get(url)
15
- resp.raise_for_status()
16
- return PIL.Image.open(io.BytesIO(resp.content))
17
 
18
  from keras_cv_attention_models import convnext
19
 
20
- mm = convnext.ConvNeXtBase()
21
-
22
- downloaded_image = download_image(
23
- "https://www.popsci.com/uploads/2021/09/21/Tortoise-on-ground-surrounded-by-plants.jpg?auto=webp"
24
- )
25
 
26
- downloaded_image_np = np.array(downloaded_image)
27
-
28
- img = downloaded_image_np
29
- imm = keras.applications.imagenet_utils.preprocess_input(img, mode='torch')
30
- image_input = tf.expand_dims(tf.image.resize(imm, mm.input_shape[1:3]), 0)
31
 
32
- pred = mm(image_input)
33
- pred_np = pred.numpy()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  import io
11
  import matplotlib.pyplot as plt
12
 
 
 
 
 
13
 
14
  from keras_cv_attention_models import convnext
15
 
16
+ import gradio as gr
 
 
 
 
17
 
18
+ mm = convnext.ConvNeXtBase()
 
 
 
 
19
 
20
+ def(img):
21
+ img = img
22
+ imm = keras.applications.imagenet_utils.preprocess_input(img, mode='torch')
23
+ image_input = tf.expand_dims(tf.image.resize(imm, mm.input_shape[1:3]), 0)
24
+
25
+ pred = mm(image_input)
26
+ pred_np = pred.numpy()
27
+
28
+ pred_names = keras.applications.imagenet_utils.decode_predictions(pred.numpy())[0]
29
+
30
+ result = {}
31
+
32
+ for i in range(5):
33
+ result[pred_names[i][1]] = int(100*pred_names[i][2]).item()
34
+
35
+ return result
36
+
37
+ inputs = gr.inputs.Image(type='numpy')
38
+ outputs = gr.outputs.Label(type="confidences",num_top_classes=5)
39
+
40
+ title = "MOBILENET V2"
41
+ description = "Gradio demo for MOBILENET V2, Efficient networks optimized for speed and memory, with residual blocks. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below."
42
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/1801.04381'>MobileNetV2: Inverted Residuals and Linear Bottlenecks</a> | <a href='https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenet.py'>Github Repo</a></p>"
43
+
44
+
45
+ gr.Interface(inference, inputs, outputs, title=title, description=description, article=article, analytics_enabled=False).launch()