mirror of
https://github.com/iperov/DeepFaceLab.git
synced 2025-03-12 20:42:45 -07:00
5.XSeg) data_dst/src mask for XSeg trainer - fetch.bat Copies faces containing XSeg polygons to aligned_xseg\ dir. Useful only if you want to collect labeled faces and reuse them in other fakes. Now you can use trained XSeg mask in the SAEHD training process. It’s mean default ‘full_face’ mask obtained from landmarks will be replaced with the mask obtained from the trained XSeg model. use 5.XSeg.optional) trained mask for data_dst/data_src - apply.bat 5.XSeg.optional) trained mask for data_dst/data_src - remove.bat Normally you don’t need it. You can use it, if you want to use ‘face_style’ and ‘bg_style’ with obstructions. XSeg trainer : now you can choose type of face XSeg trainer : now you can restart training in “override settings” Merger: XSeg-* modes now can be used with all types of faces. Therefore old MaskEditor, FANSEG models, and FAN-x modes have been removed, because the new XSeg solution is better, simpler and more convenient, which costs only 1 hour of manual masking for regular deepfake.
99 lines
3.4 KiB
Python
99 lines
3.4 KiB
Python
import os
|
|
import pickle
|
|
from functools import partial
|
|
from pathlib import Path
|
|
|
|
import cv2
|
|
import numpy as np
|
|
|
|
from core.interact import interact as io
|
|
from core.leras import nn
|
|
|
|
|
|
class XSegNet(object):
|
|
VERSION = 1
|
|
|
|
def __init__ (self, name,
|
|
resolution=256,
|
|
load_weights=True,
|
|
weights_file_root=None,
|
|
training=False,
|
|
place_model_on_cpu=False,
|
|
run_on_cpu=False,
|
|
optimizer=None,
|
|
data_format="NHWC",
|
|
raise_on_no_model_files=False):
|
|
|
|
self.resolution = resolution
|
|
self.weights_file_root = Path(weights_file_root) if weights_file_root is not None else Path(__file__).parent
|
|
|
|
nn.initialize(data_format=data_format)
|
|
tf = nn.tf
|
|
|
|
with tf.device ('/CPU:0'):
|
|
#Place holders on CPU
|
|
self.input_t = tf.placeholder (nn.floatx, nn.get4Dshape(resolution,resolution,3) )
|
|
self.target_t = tf.placeholder (nn.floatx, nn.get4Dshape(resolution,resolution,1) )
|
|
|
|
# Initializing model classes
|
|
with tf.device ('/CPU:0' if place_model_on_cpu else '/GPU:0'):
|
|
self.model = nn.XSeg(3, 32, 1, name=name)
|
|
self.model_weights = self.model.get_weights()
|
|
|
|
model_name = f'{name}_{resolution}'
|
|
|
|
self.model_filename_list = [ [self.model, f'{model_name}.npy'] ]
|
|
|
|
if training:
|
|
if optimizer is None:
|
|
raise ValueError("Optimizer should be provided for training mode.")
|
|
|
|
self.opt = optimizer
|
|
self.opt.initialize_variables (self.model_weights, vars_on_cpu=place_model_on_cpu)
|
|
self.model_filename_list += [ [self.opt, f'{model_name}_opt.npy' ] ]
|
|
else:
|
|
with tf.device ('/CPU:0' if run_on_cpu else '/GPU:0'):
|
|
_, pred = self.model(self.input_t)
|
|
|
|
def net_run(input_np):
|
|
return nn.tf_sess.run ( [pred], feed_dict={self.input_t :input_np})[0]
|
|
self.net_run = net_run
|
|
|
|
# Loading/initializing all models/optimizers weights
|
|
for model, filename in self.model_filename_list:
|
|
do_init = not load_weights
|
|
|
|
if not do_init:
|
|
model_file_path = self.weights_file_root / filename
|
|
do_init = not model.load_weights( model_file_path )
|
|
if do_init and raise_on_no_model_files:
|
|
raise Exception(f'{model_file_path} does not exists.')
|
|
|
|
if do_init:
|
|
model.init_weights()
|
|
|
|
def get_resolution(self):
|
|
return self.resolution
|
|
|
|
def flow(self, x):
|
|
return self.model(x)
|
|
|
|
def get_weights(self):
|
|
return self.model_weights
|
|
|
|
def save_weights(self):
|
|
for model, filename in io.progress_bar_generator(self.model_filename_list, "Saving", leave=False):
|
|
model.save_weights( self.weights_file_root / filename )
|
|
|
|
def extract (self, input_image):
|
|
input_shape_len = len(input_image.shape)
|
|
if input_shape_len == 3:
|
|
input_image = input_image[None,...]
|
|
|
|
result = np.clip ( self.net_run(input_image), 0, 1.0 )
|
|
result[result < 0.1] = 0 #get rid of noise
|
|
|
|
if input_shape_len == 3:
|
|
result = result[0]
|
|
|
|
return result |