EverJun2 commited on
Commit
3dee48a
ยท
verified ยท
1 Parent(s): 3fa30da

Upload 8 files

Browse files
Files changed (9) hide show
  1. .gitattributes +3 -0
  2. app.py +147 -0
  3. labels.txt +19 -0
  4. requirements.txt +6 -0
  5. test-1.jpg +3 -0
  6. test-2.jpg +3 -0
  7. test-3.jpg +3 -0
  8. test-4.jpg +0 -0
  9. test-5.jpg +0 -0
.gitattributes CHANGED
@@ -33,3 +33,6 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ test-1.jpg filter=lfs diff=lfs merge=lfs -text
37
+ test-2.jpg filter=lfs diff=lfs merge=lfs -text
38
+ test-3.jpg filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from matplotlib import gridspec
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from PIL import Image
6
+ import torch
7
+ from transformers import AutoImageProcessor, AutoModelForSemanticSegmentation
8
+
9
+ MODEL_ID = "jonathandinu/face-parsing"
10
+ processor = AutoImageProcessor.from_pretrained(MODEL_ID)
11
+ model = AutoModelForSemanticSegmentation.from_pretrained(MODEL_ID)
12
+
13
+ def ade_palette():
14
+ """ADE20K palette that maps each class to RGB values."""
15
+ return [
16
+ [204, 87, 92],[112, 185, 212],[45, 189, 106],[234, 123, 67],[78, 56, 123],[210, 32, 89],
17
+ [90, 180, 56],[155, 102, 200],[33, 147, 176],[255, 183, 76],[67, 123, 89],[190, 60, 45],
18
+ [134, 112, 200],[56, 45, 189],[200, 56, 123],[87, 92, 204],[120, 56, 123],[45, 78, 123], [200, 32, 123]
19
+ ]
20
+
21
+ labels_list = []
22
+ with open("labels.txt", "r", encoding="utf-8") as fp:
23
+ for line in fp:
24
+ labels_list.append(line.rstrip("\n"))
25
+
26
+ colormap = np.asarray(ade_palette(), dtype=np.uint8)
27
+
28
+ def label_to_color_image(label):
29
+ if label.ndim != 2:
30
+ raise ValueError("Expect 2-D input label")
31
+ if np.max(label) >= len(colormap):
32
+ raise ValueError("label value too large.")
33
+ return colormap[label]
34
+
35
+ def draw_plot(pred_img, seg_np):
36
+ fig = plt.figure(figsize=(20, 15))
37
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
38
+
39
+ plt.subplot(grid_spec[0])
40
+ plt.imshow(pred_img)
41
+ plt.axis('off')
42
+
43
+ LABEL_NAMES = np.asarray(labels_list)
44
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
45
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
46
+
47
+ unique_labels = np.unique(seg_np.astype("uint8"))
48
+ ax = plt.subplot(grid_spec[1])
49
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
50
+ ax.yaxis.tick_right()
51
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
52
+ plt.xticks([], [])
53
+ ax.tick_params(width=0.0, labelsize=25)
54
+ return fig
55
+
56
+ def run_inference(input_img):
57
+ # input: numpy array from gradio -> PIL
58
+ img = Image.fromarray(input_img.astype(np.uint8)) if isinstance(input_img, np.ndarray) else input_img
59
+ if img.mode != "RGB":
60
+ img = img.convert("RGB")
61
+
62
+ inputs = processor(images=img, return_tensors="pt")
63
+ with torch.no_grad():
64
+ outputs = model(**inputs)
65
+ logits = outputs.logits # (1, C, h/4, w/4)
66
+
67
+ # resize to original
68
+ upsampled = torch.nn.functional.interpolate(
69
+ logits, size=img.size[::-1], mode="bilinear", align_corners=False
70
+ )
71
+ seg = upsampled.argmax(dim=1)[0].cpu().numpy().astype(np.uint8) # (H,W)
72
+
73
+ # colorize & overlay
74
+ color_seg = colormap[seg] # (H,W,3)
75
+ pred_img = (np.array(img) * 0.5 + color_seg * 0.5).astype(np.uint8)
76
+
77
+ fig = draw_plot(pred_img, seg)
78
+ return fig
79
+
80
+ def run_inference(input_img):
81
+ # input: numpy array from gradio -> PIL
82
+ img = Image.fromarray(input_img.astype(np.uint8)) if isinstance(input_img, np.ndarray) else input_img
83
+ if img.mode != "RGB":
84
+ img = img.convert("RGB")
85
+
86
+ inputs = processor(images=img, return_tensors="pt")
87
+ with torch.no_grad():
88
+ outputs = model(**inputs)
89
+ logits = outputs.logits # (1, C, h/4, w/4)
90
+
91
+ # resize to original
92
+ upsampled = torch.nn.functional.interpolate(
93
+ logits, size=img.size[::-1], mode="bilinear", align_corners=False
94
+ )
95
+ seg = upsampled.argmax(dim=1)[0].cpu().numpy().astype(np.uint8) # (H,W)
96
+
97
+ # colorize & overlay
98
+ color_seg = colormap[seg] # (H,W,3)
99
+ pred_img = (np.array(img) * 0.5 + color_seg * 0.5).astype(np.uint8)
100
+
101
+ fig = draw_plot(pred_img, seg)
102
+ return fig
103
+
104
+ with gr.Blocks(title="๐ŸŽจ ๋จธ์‹ ๋Ÿฌ๋‹ 6์ฐจ ๊ณผ์ œ", theme=gr.themes.Base(
105
+ primary_hue="blue", # GitHub ํŒŒ๋ž€์ƒ‰ ๊ณ„์—ด ๋ฒ„ํŠผ
106
+ secondary_hue="slate", # ํšŒ์ƒ‰ ํฌ์ธํŠธ
107
+ neutral_hue="gray", # ๋ฐฐ๊ฒฝ ํ†ค
108
+ text_size=gr.themes.sizes.text_md,
109
+ font=["JetBrains Mono", "sans-serif"], # GitHub ๋А๋‚Œ ํฐํŠธ
110
+ radius_size=gr.themes.sizes.radius_sm
111
+ )) as demo:
112
+ theme=gr.themes.Glass()
113
+ gr.Markdown("""
114
+ # โญ Face Parsing Demo
115
+ ์–ผ๊ตด ๊ฐ ๋ถ€์œ„๋ฅผ ์ž๋™์œผ๋กœ ๊ตฌ๋ถ„ํ•˜๋Š” Image Segmentation ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.
116
+
117
+ **ํ™œ์šฉ ๋ชจ๋ธ:** `jonathandinu/face-parsing`
118
+ **์ปดํ“จํ„ฐ๊ณตํ•™์ „๊ณต 202111570 ์กฐํ•ญ์ค€**
119
+
120
+ ---
121
+
122
+ ๐Ÿ‘ ์—…๋กœ๋“œํ•œ ์–ผ๊ตด ์ด๋ฏธ์ง€๋ฅผ ๋ถ„์„ํ•˜์—ฌ, ๋จธ๋ฆฌ์นด๋ฝยทํ”ผ๋ถ€ยท๋ˆˆยท์ž… ๋“ฑ์˜ ์–ผ๊ตด ์˜์—ญ์„ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ƒ‰์ƒ์œผ๋กœ ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค.\n
123
+ ๐Ÿ‘ ๋ณธ ๋ชจ๋ธ์€ ์œ ๋ช…์ธ์‚ฌ๋“ค์˜ ์–ผ๊ตด๋กœ ์ด๋ฃจ์–ด์ง„ CelebAMask-HQ dataset ์„ ํ™œ์šฉํ•˜์—ฌ ํ•™์Šต๋œ ๋ชจ๋ธ์ž…๋‹ˆ๋‹ค.\n
124
+ """)
125
+
126
+ gr.Markdown("""
127
+ ๐Ÿ‘ ํƒ์ง€ํ•  ์ˆ˜ ์žˆ๋Š” ์˜์—ญ์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. \n
128
+ background / skin / nose / eye_g / l_eye / r_eye / l_brow / r_brow / l_ear / r_ear / mouth / u_lip / l_lip / hair / hat / ear_r / neck_l / neck / cloth
129
+ """)
130
+
131
+ gr.Interface(
132
+ fn=run_inference,
133
+ inputs=gr.Image(type="numpy", label="Input Image"),
134
+ outputs=gr.Plot(label="Overlay + Legend"),
135
+ examples=[
136
+ "test-1.jpg",
137
+ "test-2.jpg",
138
+ "test-3.jpg",
139
+ "test-4.jpg",
140
+ "test-5.jpg"
141
+ ],
142
+ flagging_mode="never",
143
+ cache_examples=False,
144
+ )
145
+
146
+ if __name__ == "__main__":
147
+ demo.launch()
labels.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ background
2
+ skin
3
+ nose
4
+ eye_g
5
+ l_eye
6
+ r_eye
7
+ l_brow
8
+ r_brow
9
+ l_ear
10
+ r_ear
11
+ mouth
12
+ u_lip
13
+ l_lip
14
+ hair
15
+ hat
16
+ ear_r
17
+ neck_l
18
+ neck
19
+ cloth
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers>=4.41.0
3
+ gradio>=4.0.0
4
+ Pillow
5
+ numpy
6
+ matplotlib
test-1.jpg ADDED

Git LFS Details

  • SHA256: 879ffc0e0f07ecac0e5993882e3a6f3f73ab935205a2a2d492371d6f07305c4c
  • Pointer size: 132 Bytes
  • Size of remote file: 1.35 MB
test-2.jpg ADDED

Git LFS Details

  • SHA256: 1e046854c18c6534c8f075a4df20b4712f2980418f0c7d0fc0f894be5d3a74c6
  • Pointer size: 132 Bytes
  • Size of remote file: 3.3 MB
test-3.jpg ADDED

Git LFS Details

  • SHA256: 928bf800814986e113ff779c6417939319267d3c9aa170ad8bef997652257abd
  • Pointer size: 132 Bytes
  • Size of remote file: 1.47 MB
test-4.jpg ADDED
test-5.jpg ADDED