MCPcopy Create free account
hub / github.com/NVIDIA/FastPhotoStyle / SegReMapping

Class SegReMapping

process_stylization_ade20k_ssn.py:43–121  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41
42
43class SegReMapping:
44 def __init__(self, mapping_name, min_ratio=0.02):
45 self.label_mapping = np.load(mapping_name)
46 self.min_ratio = min_ratio
47
48 def cross_remapping(self, cont_seg, styl_seg):
49 cont_label_info = []
50 new_cont_label_info = []
51 for label in np.unique(cont_seg):
52 cont_label_info.append(label)
53 new_cont_label_info.append(label)
54
55 style_label_info = []
56 new_style_label_info = []
57 for label in np.unique(styl_seg):
58 style_label_info.append(label)
59 new_style_label_info.append(label)
60
61 cont_set_diff = set(cont_label_info) - set(style_label_info)
62 # Find the labels that are not covered by the style
63 # Assign them to the best matched region in the style region
64 for s in cont_set_diff:
65 cont_label_index = cont_label_info.index(s)
66 for j in range(self.label_mapping.shape[0]):
67 new_label = self.label_mapping[j, s]
68 if new_label in style_label_info:
69 new_cont_label_info[cont_label_index] = new_label
70 break
71 new_cont_seg = cont_seg.copy()
72 for i,current_label in enumerate(cont_label_info):
73 new_cont_seg[(cont_seg == current_label)] = new_cont_label_info[i]
74
75 cont_label_info = []
76 for label in np.unique(new_cont_seg):
77 cont_label_info.append(label)
78 styl_set_diff = set(style_label_info) - set(cont_label_info)
79 valid_styl_set = set(style_label_info) - set(styl_set_diff)
80 for s in styl_set_diff:
81 style_label_index = style_label_info.index(s)
82 for j in range(self.label_mapping.shape[0]):
83 new_label = self.label_mapping[j, s]
84 if new_label in valid_styl_set:
85 new_style_label_info[style_label_index] = new_label
86 break
87 new_styl_seg = styl_seg.copy()
88 for i,current_label in enumerate(style_label_info):
89 # print("%d -> %d" %(current_label,new_style_label_info[i]))
90 new_styl_seg[(styl_seg == current_label)] = new_style_label_info[i]
91
92 return new_cont_seg, new_styl_seg
93
94 def self_remapping(self, seg):
95 init_ratio = self.min_ratio
96 # Assign label with small portions to label with large portion
97 new_seg = seg.copy()
98 [h,w] = new_seg.shape
99 n_pixels = h*w
100 # First scan through what are the available labels and their sizes

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected