osanseviero HF staff commited on
Commit
9578c5b
1 Parent(s): d74784d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import tensorflow as tf
4
+ import tensorflow_hub as hub
5
+
6
+ ckpt_type = '1k'
7
+ tf_hub_url = 'gs://cloud-tpu-checkpoints/efficientnet/v2/hub/efficientnetv2-s/classification'
8
+
9
+ m = hub.KerasLayer(tf_hub_url, trainable=False)
10
+ m.build([None, 224, 224, 3]) # Batch input shape.
11
+
12
+ def get_imagenet_labels(filename):
13
+ labels = []
14
+ with open(filename, 'r') as f:
15
+ for line in f:
16
+ labels.append(line.split('\t')[1][:-1]) # split and remove line break.
17
+ return labels
18
+
19
+ classes = get_imagenet_labels("imagenet1k_labels.txt")
20
+
21
+ def classify(image):
22
+ image = tf.keras.preprocessing.image.img_to_array(image)
23
+ image = (image - 128.) / 128.
24
+ logits = m(tf.expand_dims(image, 0), False)
25
+ pred = tf.keras.layers.Softmax()(logits)
26
+ idx = tf.argsort(logits[0])[::-1][0].numpy()
27
+ return classes[idx]
28
+
29
+ title = "Interactive demo: EfficientNetV2"
30
+ description = "Demo for Google's EfficientNetV2. EfficientNetV2 (accepted at ICML 2021) consists of convolutional neural networks that aim for fast training speed for relatively small-scale datasets, such as ImageNet1k."
31
+ article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2104.00298'>EfficientNetV2: Smaller Models and Faster Training</a> | <a href='https://github.com/google/automl/tree/master/efficientnetv2'>Github Repo</a> | <a href='https://ai.googleblog.com/2021/09/toward-fast-and-accurate-neural.html'>Blog Post</a></p>"
32
+
33
+ iface = gr.Interface(fn=classify,
34
+ inputs=gr.inputs.Image(label="image"),
35
+ outputs='text',
36
+ title=title,
37
+ description=description,
38
+ enable_queue=True,
39
+ examples=[['panda.jpeg'], ["llamas.jpeg"], ["hot dog.png"]],
40
+ article=article)
41
+
42
+ iface.launch()