mirror of
https://github.com/iperov/DeepFaceLab.git
synced 2025-07-06 04:52:13 -07:00
DFL-2.0 initial branch commit
This commit is contained in:
parent
52a67a61b3
commit
38b85108b3
154 changed files with 5251 additions and 9414 deletions
|
@ -5,10 +5,11 @@ import traceback
|
|||
import cv2
|
||||
import numpy as np
|
||||
|
||||
from core import mplib
|
||||
from core.joblib import SubprocessGenerator, ThisThreadGenerator
|
||||
from facelib import LandmarksProcessor
|
||||
from samplelib import (SampleGeneratorBase, SampleHost, SampleProcessor,
|
||||
SampleType)
|
||||
from utils import iter_utils, mp_utils
|
||||
|
||||
|
||||
'''
|
||||
|
@ -19,12 +20,12 @@ output_sample_types = [
|
|||
]
|
||||
'''
|
||||
class SampleGeneratorFacePerson(SampleGeneratorBase):
|
||||
def __init__ (self, samples_path, debug=False, batch_size=1,
|
||||
sample_process_options=SampleProcessor.Options(),
|
||||
output_sample_types=[],
|
||||
def __init__ (self, samples_path, debug=False, batch_size=1,
|
||||
sample_process_options=SampleProcessor.Options(),
|
||||
output_sample_types=[],
|
||||
person_id_mode=1,
|
||||
**kwargs):
|
||||
|
||||
|
||||
super().__init__(samples_path, debug, batch_size)
|
||||
self.sample_process_options = sample_process_options
|
||||
self.output_sample_types = output_sample_types
|
||||
|
@ -39,13 +40,13 @@ class SampleGeneratorFacePerson(SampleGeneratorBase):
|
|||
if self.samples_len == 0:
|
||||
raise ValueError('No training data provided.')
|
||||
|
||||
unique_person_names = { sample.person_name for sample in samples }
|
||||
persons_name_idxs = { person_name : [] for person_name in unique_person_names }
|
||||
for i,sample in enumerate(samples):
|
||||
persons_name_idxs[sample.person_name].append (i)
|
||||
unique_person_names = { sample.person_name for sample in samples }
|
||||
persons_name_idxs = { person_name : [] for person_name in unique_person_names }
|
||||
for i,sample in enumerate(samples):
|
||||
persons_name_idxs[sample.person_name].append (i)
|
||||
indexes2D = [ persons_name_idxs[person_name] for person_name in unique_person_names ]
|
||||
index2d_host = mp_utils.Index2DHost(indexes2D)
|
||||
|
||||
index2d_host = mplib.Index2DHost(indexes2D)
|
||||
|
||||
if self.debug:
|
||||
self.generators_count = 1
|
||||
self.generators = [iter_utils.ThisThreadGenerator ( self.batch_func, (samples_host.create_cli(), index2d_host.create_cli(),) )]
|
||||
|
@ -54,11 +55,7 @@ class SampleGeneratorFacePerson(SampleGeneratorBase):
|
|||
self.generators = [iter_utils.SubprocessGenerator ( self.batch_func, (samples_host.create_cli(), index2d_host.create_cli(),), start_now=True ) for i in range(self.generators_count) ]
|
||||
|
||||
self.generator_counter = -1
|
||||
|
||||
#overridable
|
||||
def get_total_sample_count(self):
|
||||
return self.samples_len
|
||||
|
||||
|
||||
def __iter__(self):
|
||||
return self
|
||||
|
||||
|
@ -67,14 +64,14 @@ class SampleGeneratorFacePerson(SampleGeneratorBase):
|
|||
generator = self.generators[self.generator_counter % len(self.generators) ]
|
||||
return next(generator)
|
||||
|
||||
def batch_func(self, param ):
|
||||
def batch_func(self, param ):
|
||||
samples, index2d_host, = param
|
||||
bs = self.batch_size
|
||||
|
||||
while True:
|
||||
person_idxs = index2d_host.get_1D(bs)
|
||||
person_idxs = index2d_host.get_1D(bs)
|
||||
samples_idxs = index2d_host.get_2D(person_idxs, 1)
|
||||
|
||||
|
||||
batches = None
|
||||
for n_batch in range(bs):
|
||||
person_id = person_idxs[n_batch]
|
||||
|
@ -85,10 +82,10 @@ class SampleGeneratorFacePerson(SampleGeneratorBase):
|
|||
x, = SampleProcessor.process ([sample], self.sample_process_options, self.output_sample_types, self.debug)
|
||||
except:
|
||||
raise Exception ("Exception occured in sample %s. Error: %s" % (sample.filename, traceback.format_exc() ) )
|
||||
|
||||
|
||||
if batches is None:
|
||||
batches = [ [] for _ in range(len(x)) ]
|
||||
|
||||
|
||||
batches += [ [] ]
|
||||
i_person_id = len(batches)-1
|
||||
|
||||
|
@ -96,9 +93,9 @@ class SampleGeneratorFacePerson(SampleGeneratorBase):
|
|||
batches[i].append ( x[i] )
|
||||
|
||||
batches[i_person_id].append ( np.array([person_id]) )
|
||||
|
||||
|
||||
yield [ np.array(batch) for batch in batches]
|
||||
|
||||
|
||||
@staticmethod
|
||||
def get_person_id_max_count(samples_path):
|
||||
return SampleHost.get_person_id_max_count(samples_path)
|
||||
|
@ -110,43 +107,43 @@ if self.person_id_mode==1:
|
|||
shuffle_idxs = []
|
||||
elif self.person_id_mode==2:
|
||||
persons_count = len(samples)
|
||||
|
||||
|
||||
person_idxs = []
|
||||
for j in range(persons_count):
|
||||
for i in range(j+1,persons_count):
|
||||
person_idxs += [ [i,j] ]
|
||||
|
||||
shuffle_person_idxs = []
|
||||
|
||||
|
||||
samples_idxs = [None]*persons_count
|
||||
shuffle_idxs = [None]*persons_count
|
||||
|
||||
|
||||
for i in range(persons_count):
|
||||
samples_idxs[i] = [*range(len(samples[i]))]
|
||||
shuffle_idxs[i] = []
|
||||
elif self.person_id_mode==3:
|
||||
persons_count = len(samples)
|
||||
|
||||
|
||||
person_idxs = [ *range(persons_count) ]
|
||||
shuffle_person_idxs = []
|
||||
|
||||
|
||||
samples_idxs = [None]*persons_count
|
||||
shuffle_idxs = [None]*persons_count
|
||||
|
||||
|
||||
for i in range(persons_count):
|
||||
samples_idxs[i] = [*range(len(samples[i]))]
|
||||
shuffle_idxs[i] = []
|
||||
|
||||
if self.person_id_mode==2:
|
||||
|
||||
if self.person_id_mode==2:
|
||||
if len(shuffle_person_idxs) == 0:
|
||||
shuffle_person_idxs = person_idxs.copy()
|
||||
np.random.shuffle(shuffle_person_idxs)
|
||||
person_ids = shuffle_person_idxs.pop()
|
||||
|
||||
|
||||
|
||||
|
||||
batches = None
|
||||
for n_batch in range(self.batch_size):
|
||||
|
||||
|
||||
if self.person_id_mode==1:
|
||||
if len(shuffle_idxs) == 0:
|
||||
shuffle_idxs = samples_idxs.copy()
|
||||
|
@ -154,7 +151,7 @@ if self.person_id_mode==2:
|
|||
|
||||
idx = shuffle_idxs.pop()
|
||||
sample = samples[ idx ]
|
||||
|
||||
|
||||
try:
|
||||
x, = SampleProcessor.process ([sample], self.sample_process_options, self.output_sample_types, self.debug)
|
||||
except:
|
||||
|
@ -165,7 +162,7 @@ if self.person_id_mode==2:
|
|||
|
||||
if batches is None:
|
||||
batches = [ [] for _ in range(len(x)) ]
|
||||
|
||||
|
||||
batches += [ [] ]
|
||||
i_person_id = len(batches)-1
|
||||
|
||||
|
@ -174,30 +171,30 @@ if self.person_id_mode==2:
|
|||
|
||||
batches[i_person_id].append ( np.array([sample.person_id]) )
|
||||
|
||||
|
||||
|
||||
elif self.person_id_mode==2:
|
||||
person_id1, person_id2 = person_ids
|
||||
|
||||
|
||||
if len(shuffle_idxs[person_id1]) == 0:
|
||||
shuffle_idxs[person_id1] = samples_idxs[person_id1].copy()
|
||||
np.random.shuffle(shuffle_idxs[person_id1])
|
||||
|
||||
idx = shuffle_idxs[person_id1].pop()
|
||||
sample1 = samples[person_id1][idx]
|
||||
|
||||
|
||||
if len(shuffle_idxs[person_id2]) == 0:
|
||||
shuffle_idxs[person_id2] = samples_idxs[person_id2].copy()
|
||||
np.random.shuffle(shuffle_idxs[person_id2])
|
||||
|
||||
idx = shuffle_idxs[person_id2].pop()
|
||||
sample2 = samples[person_id2][idx]
|
||||
|
||||
|
||||
if sample1 is not None and sample2 is not None:
|
||||
try:
|
||||
x1, = SampleProcessor.process ([sample1], self.sample_process_options, self.output_sample_types, self.debug)
|
||||
except:
|
||||
raise Exception ("Exception occured in sample %s. Error: %s" % (sample1.filename, traceback.format_exc() ) )
|
||||
|
||||
|
||||
try:
|
||||
x2, = SampleProcessor.process ([sample2], self.sample_process_options, self.output_sample_types, self.debug)
|
||||
except:
|
||||
|
@ -205,50 +202,50 @@ if self.person_id_mode==2:
|
|||
|
||||
x1_len = len(x1)
|
||||
if batches is None:
|
||||
batches = [ [] for _ in range(x1_len) ]
|
||||
batches = [ [] for _ in range(x1_len) ]
|
||||
batches += [ [] ]
|
||||
i_person_id1 = len(batches)-1
|
||||
|
||||
batches += [ [] for _ in range(len(x2)) ]
|
||||
|
||||
batches += [ [] for _ in range(len(x2)) ]
|
||||
batches += [ [] ]
|
||||
i_person_id2 = len(batches)-1
|
||||
|
||||
for i in range(x1_len):
|
||||
batches[i].append ( x1[i] )
|
||||
|
||||
|
||||
for i in range(len(x2)):
|
||||
batches[x1_len+1+i].append ( x2[i] )
|
||||
|
||||
batches[i_person_id1].append ( np.array([sample1.person_id]) )
|
||||
|
||||
batches[i_person_id2].append ( np.array([sample2.person_id]) )
|
||||
|
||||
elif self.person_id_mode==3:
|
||||
|
||||
elif self.person_id_mode==3:
|
||||
if len(shuffle_person_idxs) == 0:
|
||||
shuffle_person_idxs = person_idxs.copy()
|
||||
np.random.shuffle(shuffle_person_idxs)
|
||||
person_id = shuffle_person_idxs.pop()
|
||||
|
||||
|
||||
if len(shuffle_idxs[person_id]) == 0:
|
||||
shuffle_idxs[person_id] = samples_idxs[person_id].copy()
|
||||
np.random.shuffle(shuffle_idxs[person_id])
|
||||
|
||||
idx = shuffle_idxs[person_id].pop()
|
||||
sample1 = samples[person_id][idx]
|
||||
|
||||
|
||||
if len(shuffle_idxs[person_id]) == 0:
|
||||
shuffle_idxs[person_id] = samples_idxs[person_id].copy()
|
||||
np.random.shuffle(shuffle_idxs[person_id])
|
||||
|
||||
idx = shuffle_idxs[person_id].pop()
|
||||
sample2 = samples[person_id][idx]
|
||||
|
||||
|
||||
if sample1 is not None and sample2 is not None:
|
||||
try:
|
||||
x1, = SampleProcessor.process ([sample1], self.sample_process_options, self.output_sample_types, self.debug)
|
||||
except:
|
||||
raise Exception ("Exception occured in sample %s. Error: %s" % (sample1.filename, traceback.format_exc() ) )
|
||||
|
||||
|
||||
try:
|
||||
x2, = SampleProcessor.process ([sample2], self.sample_process_options, self.output_sample_types, self.debug)
|
||||
except:
|
||||
|
@ -256,21 +253,21 @@ if self.person_id_mode==2:
|
|||
|
||||
x1_len = len(x1)
|
||||
if batches is None:
|
||||
batches = [ [] for _ in range(x1_len) ]
|
||||
batches = [ [] for _ in range(x1_len) ]
|
||||
batches += [ [] ]
|
||||
i_person_id1 = len(batches)-1
|
||||
|
||||
batches += [ [] for _ in range(len(x2)) ]
|
||||
|
||||
batches += [ [] for _ in range(len(x2)) ]
|
||||
batches += [ [] ]
|
||||
i_person_id2 = len(batches)-1
|
||||
|
||||
for i in range(x1_len):
|
||||
batches[i].append ( x1[i] )
|
||||
|
||||
|
||||
for i in range(len(x2)):
|
||||
batches[x1_len+1+i].append ( x2[i] )
|
||||
|
||||
batches[i_person_id1].append ( np.array([sample1.person_id]) )
|
||||
|
||||
batches[i_person_id2].append ( np.array([sample2.person_id]) )
|
||||
"""
|
||||
batches[i_person_id2].append ( np.array([sample2.person_id]) )
|
||||
"""
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue