DDingcheol commited on
Commit
67274f8
•
1 Parent(s): ee994f2

Upload 9 files

Browse files
Files changed (9) hide show
  1. README.md +12 -0
  2. app.py +109 -0
  3. labels.txt +18 -0
  4. person-1.jpg +0 -0
  5. person-2.jpg +0 -0
  6. person-3.jpg +0 -0
  7. person-4.jpg +0 -0
  8. person-5.jpg +0 -0
  9. requirements.txt +6 -0
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Task M28yhtd Segmentation2
3
+ emoji: 📉
4
+ colorFrom: pink
5
+ colorTo: pink
6
+ sdk: gradio
7
+ sdk_version: 4.1.1
8
+ app_file: app.py
9
+ pinned: false
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from matplotlib import gridspec
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+ from PIL import Image
7
+ import tensorflow as tf
8
+ from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
9
+
10
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
11
+ "mattmdjaga/segformer_b2_clothes"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "mattmdjaga/segformer_b2_clothes"
15
+ )
16
+
17
+ def ade_palette():
18
+ """ADE20K palette that maps each class to RGB values."""
19
+ return [
20
+ [255, 0, 0],
21
+ [255, 187, 0],
22
+ [255, 228, 0],
23
+ [29, 219, 22],
24
+ [178, 204, 255],
25
+ [1, 0, 255],
26
+ [165, 102, 255],
27
+ [217, 65, 197],
28
+ [116, 116, 116],
29
+ [204, 114, 61],
30
+ [206, 242, 121],
31
+ [61, 183, 204],
32
+ [94, 94, 94],
33
+ [196, 183, 59],
34
+ [246, 246, 246],
35
+ [209, 178, 255],
36
+ [0, 87, 102]
37
+ ]
38
+
39
+ labels_list = []
40
+
41
+ with open(r'labels.txt', 'r') as fp:
42
+ for line in fp:
43
+ labels_list.append(line[:-1])
44
+
45
+ colormap = np.asarray(ade_palette())
46
+
47
+ def label_to_color_image(label):
48
+ if label.ndim != 2:
49
+ raise ValueError("Expect 2-D input label")
50
+
51
+ if np.max(label) >= len(colormap):
52
+ raise ValueError("label value too large.")
53
+ return colormap[label]
54
+
55
+ def draw_plot(pred_img, seg):
56
+ fig = plt.figure(figsize=(20, 15))
57
+
58
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
59
+
60
+ plt.subplot(grid_spec[0])
61
+ plt.imshow(pred_img)
62
+ plt.axis('off')
63
+ LABEL_NAMES = np.asarray(labels_list)
64
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
65
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
66
+
67
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
68
+ ax = plt.subplot(grid_spec[1])
69
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
70
+ ax.yaxis.tick_right()
71
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
72
+ plt.xticks([], [])
73
+ ax.tick_params(width=0.0, labelsize=25)
74
+ return fig
75
+
76
+ def sepia(input_img):
77
+ input_img = Image.fromarray(input_img)
78
+
79
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
80
+ outputs = model(**inputs)
81
+ logits = outputs.logits
82
+
83
+ logits = tf.transpose(logits, [0, 2, 3, 1])
84
+ logits = tf.image.resize(
85
+ logits, input_img.size[::-1]
86
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
87
+ seg = tf.math.argmax(logits, axis=-1)[0]
88
+
89
+ color_seg = np.zeros(
90
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
91
+ ) # height, width, 3
92
+ for label, color in enumerate(colormap):
93
+ color_seg[seg.numpy() == label, :] = color
94
+
95
+ # Show image + mask
96
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
97
+ pred_img = pred_img.astype(np.uint8)
98
+
99
+ fig = draw_plot(pred_img, seg)
100
+ return fig
101
+
102
+ demo = gr.Interface(fn=sepia,
103
+ inputs=gr.Image(shape=(400, 600)),
104
+ outputs=['plot'],
105
+ examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg", "person-5.jpg"],
106
+ allow_flagging='never')
107
+
108
+
109
+ demo.launch()
labels.txt ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Background
2
+ Hat
3
+ Hair
4
+ Sunglasses
5
+ Upper-clothes
6
+ Skirt
7
+ Pants
8
+ Dress
9
+ Belt
10
+ Left-shoe
11
+ Right-shoe
12
+ Face
13
+ Left-leg
14
+ Right-leg
15
+ Left-arm
16
+ Right-arm
17
+ Bag
18
+ Scarf
person-1.jpg ADDED
person-2.jpg ADDED
person-3.jpg ADDED
person-4.jpg ADDED
person-5.jpg ADDED
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ tensorflow
4
+ numpy
5
+ Image
6
+ matplotlib