charlesnchr commited on
Commit
ba9a83a
1 Parent(s): 32cfd33

First commit, test

Browse files
Files changed (4) hide show
  1. app.py +99 -0
  2. examples/dogcat.jpeg +0 -0
  3. requirements.txt +3 -0
  4. tf_model.h5 +3 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ''' ----------------------------------------
2
+ * Creation Time : Sun Aug 28 21:38:58 2022
3
+ * Last Modified : Sun Aug 28 21:41:36 2022
4
+ * Author : Charles N. Christensen
5
+ * Github : github.com/charlesnchr
6
+ ----------------------------------------'''
7
+
8
+ from turtle import title
9
+ import gradio as gr
10
+ from huggingface_hub import from_pretrained_keras
11
+ import tensorflow as tf
12
+ import numpy as np
13
+ from PIL import Image
14
+ import io
15
+ import base64
16
+
17
+
18
+ model = tf.keras.models.load_model("./tf_model.h5")
19
+
20
+
21
+ def predict(image):
22
+ img = np.array(image)
23
+ original_shape = img.shape[:2]
24
+
25
+ im = tf.image.resize(img, (128, 128))
26
+ im = tf.cast(im, tf.float32) / 255.0
27
+ pred_mask = model.predict(im[tf.newaxis, ...])
28
+
29
+
30
+ # take the best performing class for each pixel
31
+ # the output of argmax looks like this [[1, 2, 0], ...]
32
+ pred_mask_arg = tf.argmax(pred_mask, axis=-1)
33
+
34
+
35
+ # convert the prediction mask into binary masks for each class
36
+ binary_masks = {}
37
+
38
+ # when we take tf.argmax() over pred_mask, it becomes a tensor object
39
+ # the shape becomes TensorShape object, looking like this TensorShape([128])
40
+ # we need to take get shape, convert to list and take the best one
41
+
42
+ rows = pred_mask_arg[0][1].get_shape().as_list()[0]
43
+ cols = pred_mask_arg[0][2].get_shape().as_list()[0]
44
+
45
+ for cls in range(pred_mask.shape[-1]):
46
+
47
+ binary_masks[f"mask_{cls}"] = np.zeros(shape = (pred_mask.shape[1], pred_mask.shape[2])) #create masks for each class
48
+
49
+ for row in range(rows):
50
+
51
+ for col in range(cols):
52
+
53
+ if pred_mask_arg[0][row][col] == cls:
54
+
55
+ binary_masks[f"mask_{cls}"][row][col] = 1
56
+ else:
57
+ binary_masks[f"mask_{cls}"][row][col] = 0
58
+
59
+ mask = binary_masks[f"mask_{cls}"]
60
+ mask *= 255
61
+
62
+ mask = np.array(Image.fromarray(mask).convert("L"))
63
+ mask = tf.image.resize(mask[..., tf.newaxis], original_shape)
64
+ mask = tf.cast(mask, tf.uint8)
65
+ mask = mask.numpy().squeeze()
66
+
67
+ return mask
68
+
69
+
70
+ title = '<h1 style="text-align: center;">Segment Pets</h1>'
71
+
72
+ description = """
73
+ ## About
74
+ This space demonstrates the use of a semantic segmentation model to segment pets and classify them
75
+ according to the pixels.
76
+ ## 🚀 To run
77
+ Upload a pet image and hit submit or select one from the given examples
78
+ """
79
+
80
+ inputs = gr.inputs.Image(label="Upload a pet image", type = 'pil', optional=False)
81
+ outputs = [
82
+ gr.outputs.Image(label="Segmentation")
83
+ # , gr.outputs.Textbox(type="auto",label="Pet Prediction")
84
+ ]
85
+
86
+ examples = [
87
+ "./examples/dogcat.jpeg",
88
+ ]
89
+
90
+
91
+
92
+ interface = gr.Interface(fn=predict,
93
+ inputs=inputs,
94
+ outputs=outputs,
95
+ title = title,
96
+ description=description,
97
+ examples=examples
98
+ )
99
+ interface.launch()
examples/dogcat.jpeg ADDED
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ huggingface_hub
2
+ tensorflow
3
+ pillow
tf_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0258ea75c11d977fae78f747902e48541c5e6996d3d5c700175454ffeb42aa0f
3
+ size 63661584