programofktw commited on
Commit
1ad748d
1 Parent(s): 845dfd7

assinment2 complete

Browse files
assignment2/app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # "nvidia/segformer-b5-finetuned-ade-640-640"
12
+ # 참조코드
13
+ "mattmdjaga/segformer_b2_clothes"
14
+ )
15
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
16
+ # "nvidia/segformer-b5-finetuned-ade-640-640"
17
+ # 참조 모델
18
+ "mattmdjaga/segformer_b2_clothes"
19
+ )
20
+
21
+
22
+ def ade_palette():
23
+ """ADE20K palette that maps each class to RGB values."""
24
+ return [
25
+ [204, 87, 92],
26
+ [112, 185, 212],
27
+ [45, 189, 106],
28
+ [234, 123, 67],
29
+ [78, 56, 123],
30
+ [210, 32, 89],
31
+ [90, 180, 56],
32
+ [155, 102, 200],
33
+ [33, 147, 176],
34
+ [255, 183, 76],
35
+ [67, 123, 89],
36
+ [190, 60, 45],
37
+ [134, 112, 200],
38
+ [56, 45, 189],
39
+ [200, 56, 123],
40
+ [87, 92, 204],
41
+ [120, 56, 123],
42
+ [45, 78, 123]
43
+ ]
44
+
45
+ labels_list = []
46
+
47
+ with open(r'labels.txt', 'r') as fp:
48
+ for line in fp:
49
+ labels_list.append(line[:-1])
50
+
51
+ colormap = np.asarray(ade_palette())
52
+
53
+ def label_to_color_image(label):
54
+ if label.ndim != 2:
55
+ raise ValueError("Expect 2-D input label")
56
+
57
+ if np.max(label) >= len(colormap):
58
+ raise ValueError("label value too large.")
59
+ return colormap[label]
60
+
61
+ def draw_plot(pred_img, seg):
62
+ fig = plt.figure(figsize=(20, 15))
63
+
64
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
65
+
66
+ plt.subplot(grid_spec[0])
67
+ plt.imshow(pred_img)
68
+ plt.axis('off')
69
+ LABEL_NAMES = np.asarray(labels_list)
70
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
71
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
72
+
73
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
74
+ ax = plt.subplot(grid_spec[1])
75
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
76
+ ax.yaxis.tick_right()
77
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
78
+ plt.xticks([], [])
79
+ ax.tick_params(width=0.0, labelsize=25)
80
+ return fig
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
+ demo = gr.Interface(fn=sepia,
109
+ inputs=gr.Image(shape=(400, 600)),
110
+ outputs=['plot'],
111
+ examples=["ADE_val_00000001.jpeg", "ADE_val_00001159.jpg", "ADE_val_00001248.jpg", "ADE_val_00001472.jpg"],
112
+ allow_flagging='never')
113
+
114
+
115
+ demo.launch()
assignment2/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
assignment2/requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ tensorflow
4
+ numpy
5
+ Image
6
+ matplotlib