Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
iperov
GitHub Repository: iperov/deepfacelab
Path: blob/master/merger/MergerConfig.py
628 views
1
import numpy as np
2
import copy
3
4
from facelib import FaceType
5
from core.interact import interact as io
6
7
8
class MergerConfig(object):
9
TYPE_NONE = 0
10
TYPE_MASKED = 1
11
TYPE_FACE_AVATAR = 2
12
####
13
14
TYPE_IMAGE = 3
15
TYPE_IMAGE_WITH_LANDMARKS = 4
16
17
def __init__(self, type=0,
18
sharpen_mode=0,
19
blursharpen_amount=0,
20
**kwargs
21
):
22
self.type = type
23
24
self.sharpen_dict = {0:"None", 1:'box', 2:'gaussian'}
25
26
#default changeable params
27
self.sharpen_mode = sharpen_mode
28
self.blursharpen_amount = blursharpen_amount
29
30
def copy(self):
31
return copy.copy(self)
32
33
#overridable
34
def ask_settings(self):
35
s = """Choose sharpen mode: \n"""
36
for key in self.sharpen_dict.keys():
37
s += f"""({key}) {self.sharpen_dict[key]}\n"""
38
io.log_info(s)
39
self.sharpen_mode = io.input_int ("", 0, valid_list=self.sharpen_dict.keys(), help_message="Enhance details by applying sharpen filter.")
40
41
if self.sharpen_mode != 0:
42
self.blursharpen_amount = np.clip ( io.input_int ("Choose blur/sharpen amount", 0, add_info="-100..100"), -100, 100 )
43
44
def toggle_sharpen_mode(self):
45
a = list( self.sharpen_dict.keys() )
46
self.sharpen_mode = a[ (a.index(self.sharpen_mode)+1) % len(a) ]
47
48
def add_blursharpen_amount(self, diff):
49
self.blursharpen_amount = np.clip ( self.blursharpen_amount+diff, -100, 100)
50
51
#overridable
52
def get_config(self):
53
d = self.__dict__.copy()
54
d.pop('type')
55
return d
56
57
#overridable
58
def __eq__(self, other):
59
#check equality of changeable params
60
61
if isinstance(other, MergerConfig):
62
return self.sharpen_mode == other.sharpen_mode and \
63
self.blursharpen_amount == other.blursharpen_amount
64
65
return False
66
67
#overridable
68
def to_string(self, filename):
69
r = ""
70
r += f"sharpen_mode : {self.sharpen_dict[self.sharpen_mode]}\n"
71
r += f"blursharpen_amount : {self.blursharpen_amount}\n"
72
return r
73
74
mode_dict = {0:'original',
75
1:'overlay',
76
2:'hist-match',
77
3:'seamless',
78
4:'seamless-hist-match',
79
5:'raw-rgb',
80
6:'raw-predict'}
81
82
mode_str_dict = { mode_dict[key] : key for key in mode_dict.keys() }
83
84
mask_mode_dict = {0:'full',
85
1:'dst',
86
2:'learned-prd',
87
3:'learned-dst',
88
4:'learned-prd*learned-dst',
89
5:'learned-prd+learned-dst',
90
6:'XSeg-prd',
91
7:'XSeg-dst',
92
8:'XSeg-prd*XSeg-dst',
93
9:'learned-prd*learned-dst*XSeg-prd*XSeg-dst'
94
}
95
96
97
ctm_dict = { 0: "None", 1:"rct", 2:"lct", 3:"mkl", 4:"mkl-m", 5:"idt", 6:"idt-m", 7:"sot-m", 8:"mix-m" }
98
ctm_str_dict = {None:0, "rct":1, "lct":2, "mkl":3, "mkl-m":4, "idt":5, "idt-m":6, "sot-m":7, "mix-m":8 }
99
100
class MergerConfigMasked(MergerConfig):
101
102
def __init__(self, face_type=FaceType.FULL,
103
default_mode = 'overlay',
104
mode='overlay',
105
masked_hist_match=True,
106
hist_match_threshold = 238,
107
mask_mode = 4,
108
erode_mask_modifier = 0,
109
blur_mask_modifier = 0,
110
motion_blur_power = 0,
111
output_face_scale = 0,
112
super_resolution_power = 0,
113
color_transfer_mode = ctm_str_dict['rct'],
114
image_denoise_power = 0,
115
bicubic_degrade_power = 0,
116
color_degrade_power = 0,
117
**kwargs
118
):
119
120
super().__init__(type=MergerConfig.TYPE_MASKED, **kwargs)
121
122
self.face_type = face_type
123
if self.face_type not in [FaceType.HALF, FaceType.MID_FULL, FaceType.FULL, FaceType.WHOLE_FACE, FaceType.HEAD ]:
124
raise ValueError("MergerConfigMasked does not support this type of face.")
125
126
self.default_mode = default_mode
127
128
#default changeable params
129
if mode not in mode_str_dict:
130
mode = mode_dict[1]
131
132
self.mode = mode
133
self.masked_hist_match = masked_hist_match
134
self.hist_match_threshold = hist_match_threshold
135
self.mask_mode = mask_mode
136
self.erode_mask_modifier = erode_mask_modifier
137
self.blur_mask_modifier = blur_mask_modifier
138
self.motion_blur_power = motion_blur_power
139
self.output_face_scale = output_face_scale
140
self.super_resolution_power = super_resolution_power
141
self.color_transfer_mode = color_transfer_mode
142
self.image_denoise_power = image_denoise_power
143
self.bicubic_degrade_power = bicubic_degrade_power
144
self.color_degrade_power = color_degrade_power
145
146
def copy(self):
147
return copy.copy(self)
148
149
def set_mode (self, mode):
150
self.mode = mode_dict.get (mode, self.default_mode)
151
152
def toggle_masked_hist_match(self):
153
if self.mode == 'hist-match':
154
self.masked_hist_match = not self.masked_hist_match
155
156
def add_hist_match_threshold(self, diff):
157
if self.mode == 'hist-match' or self.mode == 'seamless-hist-match':
158
self.hist_match_threshold = np.clip ( self.hist_match_threshold+diff , 0, 255)
159
160
def toggle_mask_mode(self):
161
a = list( mask_mode_dict.keys() )
162
self.mask_mode = a[ (a.index(self.mask_mode)+1) % len(a) ]
163
164
def add_erode_mask_modifier(self, diff):
165
self.erode_mask_modifier = np.clip ( self.erode_mask_modifier+diff , -400, 400)
166
167
def add_blur_mask_modifier(self, diff):
168
self.blur_mask_modifier = np.clip ( self.blur_mask_modifier+diff , 0, 400)
169
170
def add_motion_blur_power(self, diff):
171
self.motion_blur_power = np.clip ( self.motion_blur_power+diff, 0, 100)
172
173
def add_output_face_scale(self, diff):
174
self.output_face_scale = np.clip ( self.output_face_scale+diff , -50, 50)
175
176
def toggle_color_transfer_mode(self):
177
self.color_transfer_mode = (self.color_transfer_mode+1) % ( max(ctm_dict.keys())+1 )
178
179
def add_super_resolution_power(self, diff):
180
self.super_resolution_power = np.clip ( self.super_resolution_power+diff , 0, 100)
181
182
def add_color_degrade_power(self, diff):
183
self.color_degrade_power = np.clip ( self.color_degrade_power+diff , 0, 100)
184
185
def add_image_denoise_power(self, diff):
186
self.image_denoise_power = np.clip ( self.image_denoise_power+diff, 0, 500)
187
188
def add_bicubic_degrade_power(self, diff):
189
self.bicubic_degrade_power = np.clip ( self.bicubic_degrade_power+diff, 0, 100)
190
191
def ask_settings(self):
192
s = """Choose mode: \n"""
193
for key in mode_dict.keys():
194
s += f"""({key}) {mode_dict[key]}\n"""
195
io.log_info(s)
196
mode = io.input_int ("", mode_str_dict.get(self.default_mode, 1) )
197
198
self.mode = mode_dict.get (mode, self.default_mode )
199
200
if 'raw' not in self.mode:
201
if self.mode == 'hist-match':
202
self.masked_hist_match = io.input_bool("Masked hist match?", True)
203
204
if self.mode == 'hist-match' or self.mode == 'seamless-hist-match':
205
self.hist_match_threshold = np.clip ( io.input_int("Hist match threshold", 255, add_info="0..255"), 0, 255)
206
207
s = """Choose mask mode: \n"""
208
for key in mask_mode_dict.keys():
209
s += f"""({key}) {mask_mode_dict[key]}\n"""
210
io.log_info(s)
211
self.mask_mode = io.input_int ("", 1, valid_list=mask_mode_dict.keys() )
212
213
if 'raw' not in self.mode:
214
self.erode_mask_modifier = np.clip ( io.input_int ("Choose erode mask modifier", 0, add_info="-400..400"), -400, 400)
215
self.blur_mask_modifier = np.clip ( io.input_int ("Choose blur mask modifier", 0, add_info="0..400"), 0, 400)
216
self.motion_blur_power = np.clip ( io.input_int ("Choose motion blur power", 0, add_info="0..100"), 0, 100)
217
218
self.output_face_scale = np.clip (io.input_int ("Choose output face scale modifier", 0, add_info="-50..50" ), -50, 50)
219
220
if 'raw' not in self.mode:
221
self.color_transfer_mode = io.input_str ( "Color transfer to predicted face", None, valid_list=list(ctm_str_dict.keys())[1:] )
222
self.color_transfer_mode = ctm_str_dict[self.color_transfer_mode]
223
224
super().ask_settings()
225
226
self.super_resolution_power = np.clip ( io.input_int ("Choose super resolution power", 0, add_info="0..100", help_message="Enhance details by applying superresolution network."), 0, 100)
227
228
if 'raw' not in self.mode:
229
self.image_denoise_power = np.clip ( io.input_int ("Choose image degrade by denoise power", 0, add_info="0..500"), 0, 500)
230
self.bicubic_degrade_power = np.clip ( io.input_int ("Choose image degrade by bicubic rescale power", 0, add_info="0..100"), 0, 100)
231
self.color_degrade_power = np.clip ( io.input_int ("Degrade color power of final image", 0, add_info="0..100"), 0, 100)
232
233
io.log_info ("")
234
235
def __eq__(self, other):
236
#check equality of changeable params
237
238
if isinstance(other, MergerConfigMasked):
239
return super().__eq__(other) and \
240
self.mode == other.mode and \
241
self.masked_hist_match == other.masked_hist_match and \
242
self.hist_match_threshold == other.hist_match_threshold and \
243
self.mask_mode == other.mask_mode and \
244
self.erode_mask_modifier == other.erode_mask_modifier and \
245
self.blur_mask_modifier == other.blur_mask_modifier and \
246
self.motion_blur_power == other.motion_blur_power and \
247
self.output_face_scale == other.output_face_scale and \
248
self.color_transfer_mode == other.color_transfer_mode and \
249
self.super_resolution_power == other.super_resolution_power and \
250
self.image_denoise_power == other.image_denoise_power and \
251
self.bicubic_degrade_power == other.bicubic_degrade_power and \
252
self.color_degrade_power == other.color_degrade_power
253
254
return False
255
256
def to_string(self, filename):
257
r = (
258
f"""MergerConfig {filename}:\n"""
259
f"""Mode: {self.mode}\n"""
260
)
261
262
if self.mode == 'hist-match':
263
r += f"""masked_hist_match: {self.masked_hist_match}\n"""
264
265
if self.mode == 'hist-match' or self.mode == 'seamless-hist-match':
266
r += f"""hist_match_threshold: {self.hist_match_threshold}\n"""
267
268
r += f"""mask_mode: { mask_mode_dict[self.mask_mode] }\n"""
269
270
if 'raw' not in self.mode:
271
r += (f"""erode_mask_modifier: {self.erode_mask_modifier}\n"""
272
f"""blur_mask_modifier: {self.blur_mask_modifier}\n"""
273
f"""motion_blur_power: {self.motion_blur_power}\n""")
274
275
r += f"""output_face_scale: {self.output_face_scale}\n"""
276
277
if 'raw' not in self.mode:
278
r += f"""color_transfer_mode: {ctm_dict[self.color_transfer_mode]}\n"""
279
r += super().to_string(filename)
280
281
r += f"""super_resolution_power: {self.super_resolution_power}\n"""
282
283
if 'raw' not in self.mode:
284
r += (f"""image_denoise_power: {self.image_denoise_power}\n"""
285
f"""bicubic_degrade_power: {self.bicubic_degrade_power}\n"""
286
f"""color_degrade_power: {self.color_degrade_power}\n""")
287
288
r += "================"
289
290
return r
291
292
293
class MergerConfigFaceAvatar(MergerConfig):
294
295
def __init__(self, temporal_face_count=0,
296
add_source_image=False):
297
super().__init__(type=MergerConfig.TYPE_FACE_AVATAR)
298
self.temporal_face_count = temporal_face_count
299
300
#changeable params
301
self.add_source_image = add_source_image
302
303
def copy(self):
304
return copy.copy(self)
305
306
#override
307
def ask_settings(self):
308
self.add_source_image = io.input_bool("Add source image?", False, help_message="Add source image for comparison.")
309
super().ask_settings()
310
311
def toggle_add_source_image(self):
312
self.add_source_image = not self.add_source_image
313
314
#override
315
def __eq__(self, other):
316
#check equality of changeable params
317
318
if isinstance(other, MergerConfigFaceAvatar):
319
return super().__eq__(other) and \
320
self.add_source_image == other.add_source_image
321
322
return False
323
324
#override
325
def to_string(self, filename):
326
return (f"MergerConfig {filename}:\n"
327
f"add_source_image : {self.add_source_image}\n") + \
328
super().to_string(filename) + "================"
329
330
331