hyo37009 commited on
Commit
e695942
·
1 Parent(s): ae79914
.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
.idea/MyImageSegmentation.iml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$">
5
+ <excludeFolder url="file://$MODULE_DIR$/venv" />
6
+ </content>
7
+ <orderEntry type="inheritedJdk" />
8
+ <orderEntry type="sourceFolder" forTests="false" />
9
+ </component>
10
+ <component name="PyDocumentationSettings">
11
+ <option name="format" value="GOOGLE" />
12
+ <option name="myDocStringFormat" value="Google" />
13
+ </component>
14
+ </module>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (MyImageSegmentation)" project-jdk-type="Python SDK" />
4
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/MyImageSegmentation.iml" filepath="$PROJECT_DIR$/.idea/MyImageSegmentation.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
18
+ def ade_palette():
19
+ """ADE20K palette that maps each class to RGB values."""
20
+ return [
21
+ [255, 0, 0],
22
+ [255, 94, 0],
23
+ [255, 187, 0],
24
+ [255, 228, 0],
25
+ [171, 242, 0],
26
+ [29, 219, 22],
27
+ [0, 216, 255],
28
+ [0, 84, 255],
29
+ [1, 0, 255],
30
+ [95, 0, 255],
31
+ [255, 0, 221],
32
+ [255, 0, 127],
33
+ [152, 0, 0],
34
+ [153, 112, 0],
35
+ [107, 153, 0],
36
+ [0, 51, 153],
37
+ [63, 0, 153],
38
+ [153, 0, 133]
39
+ ]
40
+
41
+
42
+ labels_list = []
43
+
44
+ with open(r'labels.txt', 'r') as fp:
45
+ for line in fp:
46
+ labels_list.append(line[:-1])
47
+
48
+ colormap = np.asarray(ade_palette())
49
+
50
+
51
+ def label_to_color_image(label):
52
+ if label.ndim != 2:
53
+ raise ValueError("Expect 2-D input label")
54
+
55
+ if np.max(label) >= len(colormap):
56
+ raise ValueError("label value too large.")
57
+ return colormap[label]
58
+
59
+
60
+ def draw_plot(pred_img, seg):
61
+ fig = plt.figure(figsize=(20, 15))
62
+
63
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
64
+
65
+ plt.subplot(grid_spec[0])
66
+ plt.imshow(pred_img)
67
+ plt.axis('off')
68
+ LABEL_NAMES = np.asarray(labels_list)
69
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
70
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
71
+
72
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
73
+ ax = plt.subplot(grid_spec[1])
74
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
75
+ ax.yaxis.tick_right()
76
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
77
+ plt.xticks([], [])
78
+ ax.tick_params(width=0.0, labelsize=25)
79
+ return fig
80
+
81
+
82
+ def sepia(input_img):
83
+ input_img = Image.fromarray(input_img)
84
+
85
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
86
+ outputs = model(**inputs)
87
+ logits = outputs.logits
88
+
89
+ logits = tf.transpose(logits, [0, 2, 3, 1])
90
+ logits = tf.image.resize(
91
+ logits, input_img.size[::-1]
92
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
93
+ seg = tf.math.argmax(logits, axis=-1)[0]
94
+
95
+ color_seg = np.zeros(
96
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
97
+ ) # height, width, 3
98
+ for label, color in enumerate(colormap):
99
+ color_seg[seg.numpy() == label, :] = color
100
+
101
+ # Show image + mask
102
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
103
+ pred_img = pred_img.astype(np.uint8)
104
+
105
+ fig = draw_plot(pred_img, seg)
106
+ return fig
107
+
108
+
109
+ demo = gr.Interface(fn=sepia,
110
+ inputs=gr.Image(shape=(400, 600)),
111
+ outputs=['plot'],
112
+ examples=["person-1.jpg",
113
+ "person-2.jpg",
114
+ "person-3.jpg",
115
+ "person-4.jpg",
116
+ "person-5.jpg"],
117
+ allow_flagging='never')
118
+
119
+ 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