chojo12 commited on
Commit
cfb75f3
1 Parent(s): 0deedcf

Upload 8 files

Browse files
Files changed (8) hide show
  1. app.py +120 -0
  2. labels.txt +18 -0
  3. person-1.jpg +0 -0
  4. person-2.jpg +0 -0
  5. person-3.jpg +0 -0
  6. person-4.jpg +0 -0
  7. person-5.jpg +0 -0
  8. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ [204, 87, 92],
21
+ [112, 185, 212],
22
+ [45, 189, 106],
23
+ [234, 123, 67],
24
+ [78, 56, 123],
25
+ [210, 32, 89],
26
+ [90, 180, 56],
27
+ [155, 102, 200],
28
+ [33, 147, 176],
29
+ [255, 183, 76],
30
+ [67, 123, 89],
31
+ [190, 60, 45],
32
+ [134, 112, 200],
33
+ [56, 45, 189],
34
+ [200, 56, 123],
35
+ [87, 92, 204],
36
+ [120, 56, 123],
37
+ [45, 78, 123],
38
+ [156, 200, 56],
39
+ [32, 90, 210],
40
+ [56, 123, 67],
41
+ [180, 56, 123],
42
+ [123, 67, 45],
43
+ [45, 134, 200],
44
+ [67, 56, 123],
45
+ [78, 123, 67],
46
+ [32, 210, 90],
47
+ [45, 56, 189],
48
+ ]
49
+
50
+ labels_list = []
51
+
52
+ with open(r'labels.txt', 'r') as fp:
53
+ for line in fp:
54
+ labels_list.append(line[:-1])
55
+
56
+ colormap = np.asarray(ade_palette())
57
+
58
+ def label_to_color_image(label):
59
+ if label.ndim != 2:
60
+ raise ValueError("Expect 2-D input label")
61
+
62
+ if np.max(label) >= len(colormap):
63
+ raise ValueError("label value too large.")
64
+ return colormap[label]
65
+
66
+ def draw_plot(pred_img, seg):
67
+ fig = plt.figure(figsize=(20, 15))
68
+
69
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
70
+
71
+ plt.subplot(grid_spec[0])
72
+ plt.imshow(pred_img)
73
+ plt.axis('off')
74
+ LABEL_NAMES = np.asarray(labels_list)
75
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
76
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
77
+
78
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
79
+ ax = plt.subplot(grid_spec[1])
80
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
81
+ ax.yaxis.tick_right()
82
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
83
+ plt.xticks([], [])
84
+ ax.tick_params(width=0.0, labelsize=25)
85
+ return fig
86
+
87
+ def sepia(input_img):
88
+ input_img = Image.fromarray(input_img)
89
+
90
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
91
+ outputs = model(**inputs)
92
+ logits = outputs.logits
93
+
94
+ logits = tf.transpose(logits, [0, 2, 3, 1])
95
+ logits = tf.image.resize(
96
+ logits, input_img.size[::-1]
97
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
98
+ seg = tf.math.argmax(logits, axis=-1)[0]
99
+
100
+ color_seg = np.zeros(
101
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
102
+ ) # height, width, 3
103
+ for label, color in enumerate(colormap):
104
+ color_seg[seg.numpy() == label, :] = color
105
+
106
+ # Show image + mask
107
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
108
+ pred_img = pred_img.astype(np.uint8)
109
+
110
+ fig = draw_plot(pred_img, seg)
111
+ return fig
112
+
113
+ demo = gr.Interface(fn=sepia,
114
+ inputs=gr.Image(shape=(400, 600)),
115
+ outputs=['plot'],
116
+ examples=["person-1.jpg", "person-2.jpg", "person-3.jpg", "person-4.jpg","person-5.jpg"],
117
+ allow_flagging='never')
118
+
119
+
120
+ 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