mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-07-05 12:36:43 -07:00
_
This commit is contained in:
parent
69f71ddecd
commit
d40fce6a5a
4 changed files with 99 additions and 75 deletions
|
@ -1,4 +1,69 @@
|
|||
|
||||
|
||||
# from modelhub.onnx import YoloV5Face
|
||||
# import numpy as np
|
||||
# import cv2
|
||||
# from xlib import path as lib_path
|
||||
# from xlib import cv as lib_cv
|
||||
# from xlib import face as lib_face
|
||||
# from xlib.face import FRect
|
||||
# from xlib import console as lib_con
|
||||
# from xlib import onnxruntime as lib_ort
|
||||
# from xlib.image import ImageProcessor
|
||||
# from xlib.math import Affine2DUniMat
|
||||
# face_det = YoloV5Face( YoloV5Face.get_available_devices()[0] )
|
||||
|
||||
|
||||
# face_aligner = lib_ort.InferenceSession_with_device(r'D:\DevelopPPP\projects\DeepFaceLive\workspace_facealigner\FaceAligner.onnx', lib_ort.get_available_devices_info()[0])
|
||||
|
||||
# in_path = r'F:\DeepFaceLabCUDA9.2SSE\workspace линеус\data_dst'
|
||||
# #r'F:\DeepFaceLabCUDA9.2SSE\workspace шиа тест\data_dst'
|
||||
# out_path = Path(r'F:\DeepFaceLabCUDA9.2SSE\workspace шиа тест\data_dst_out')
|
||||
# out_path.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
# n = 0
|
||||
# for filepath in lib_con.progress_bar_iterator( lib_path.get_files_paths(in_path, extensions=['.jpg', '.png']), ):
|
||||
# img = lib_cv.imread(filepath)
|
||||
# H,W,C = img.shape
|
||||
|
||||
# rects = face_det.extract (img, threshold=0.5)[0]
|
||||
# if len(rects) == 0:
|
||||
# continue
|
||||
|
||||
# rect = [ FRect.from_ltrb( (l/W, t/H, r/W, b/H) ) for l,t,r,b in rects ][0]
|
||||
|
||||
# cut_img, mat = rect.cut(img, coverage=1.4, output_size=224)
|
||||
|
||||
|
||||
# align_mat = face_aligner.run(None, {'in': ImageProcessor(cut_img).to_ufloat32().get_image('NCHW')})[0]
|
||||
# align_mat = Affine2DUniMat(align_mat.mean(0))
|
||||
|
||||
# align_mat = align_mat.invert().to_exact_mat(224,224,224,224)
|
||||
|
||||
# aligned_img = cv2.warpAffine(cut_img, align_mat, (224,224))
|
||||
|
||||
# screen = np.concatenate( [cut_img, aligned_img], 1)
|
||||
|
||||
# #lib_cv.imwrite( out_path / f'{n:04}.png', screen )
|
||||
|
||||
# n += 1
|
||||
# cv2.imshow('', screen)
|
||||
# cv2.waitKey(1)
|
||||
# import code
|
||||
# code.interact(local=dict(globals(), **locals()))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# import cv2
|
||||
# import numpy as np
|
||||
# from xlib.math import Affine2DMat
|
||||
|
|
|
@ -94,7 +94,7 @@ class FaceAlignerTrainerApp:
|
|||
def set_random_warp(self, random_warp : bool):
|
||||
self._random_warp = random_warp
|
||||
self._training_generator.set_random_warp(random_warp)
|
||||
|
||||
|
||||
def get_iteration(self) -> int: return self._iteration
|
||||
def set_iteration(self, iteration : int):
|
||||
self._iteration = iteration
|
||||
|
@ -234,7 +234,7 @@ class FaceAlignerTrainerApp:
|
|||
def main_loop(self):
|
||||
|
||||
while not self._is_quit:
|
||||
|
||||
|
||||
if self._ev_request_reset_model.is_set():
|
||||
self._ev_request_reset_model.clear()
|
||||
self.reset_model(load=False)
|
||||
|
@ -253,7 +253,7 @@ class FaceAlignerTrainerApp:
|
|||
torch.cuda.empty_cache()
|
||||
self._is_training = self._req_is_training
|
||||
self._req_is_training = None
|
||||
|
||||
|
||||
self.main_loop_training()
|
||||
|
||||
if self._is_training and self._last_save_time is not None:
|
||||
|
@ -267,20 +267,20 @@ class FaceAlignerTrainerApp:
|
|||
self.export()
|
||||
print('Exporting done.')
|
||||
dc.Diacon.get_current_dlg().recreate().set_current()
|
||||
|
||||
|
||||
if self._ev_request_save.is_set():
|
||||
self._ev_request_save.clear()
|
||||
print('Saving...')
|
||||
self.save()
|
||||
print('Saving done.')
|
||||
dc.Diacon.get_current_dlg().recreate().set_current()
|
||||
|
||||
|
||||
if self._ev_request_quit.is_set():
|
||||
self._ev_request_quit.clear()
|
||||
self._is_quit = True
|
||||
|
||||
|
||||
time.sleep(0.005)
|
||||
|
||||
|
||||
def main_loop_training(self):
|
||||
"""
|
||||
separated function, because torch tensors refences must be freed from python locals
|
||||
|
@ -288,36 +288,32 @@ class FaceAlignerTrainerApp:
|
|||
if self._is_training or \
|
||||
self._is_previewing_samples or \
|
||||
self._ev_request_preview.is_set():
|
||||
|
||||
training_data = self._training_generator.get_next_data(wait=False)
|
||||
if training_data is not None and \
|
||||
training_data.resolution == self.get_resolution(): # Skip if resolution is different, due to delay
|
||||
|
||||
training_data.resolution == self.get_resolution(): # Skip if resolution is different, due to delay
|
||||
if self._is_training:
|
||||
self._model_optimizer.zero_grad()
|
||||
|
||||
|
||||
if self._ev_request_preview.is_set() or \
|
||||
self._is_training:
|
||||
# Inference for both preview and training
|
||||
img_aligned_shifted_t = torch.tensor(training_data.img_aligned_shifted).to(self._device)
|
||||
shift_uni_mats_pred_t = self._model(img_aligned_shifted_t)#.view( (-1,2,3) )
|
||||
|
||||
|
||||
if self._is_training:
|
||||
# Training optimization step
|
||||
# Training optimization step
|
||||
shift_uni_mats_t = torch.tensor(training_data.shift_uni_mats).to(self._device)
|
||||
|
||||
loss_t = (shift_uni_mats_pred_t-shift_uni_mats_t).square().mean()*10.0
|
||||
loss_t.backward()
|
||||
self._model_optimizer.step()
|
||||
|
||||
loss = loss_t.detach().cpu().numpy()
|
||||
|
||||
rec_loss_history = self._loss_history.get('reconstruct', None)
|
||||
if rec_loss_history is None:
|
||||
rec_loss_history = self._loss_history['reconstruct'] = []
|
||||
rec_loss_history.append(float(loss))
|
||||
|
||||
self.set_iteration( self.get_iteration() + 1 )
|
||||
|
||||
|
||||
if self._ev_request_preview.is_set():
|
||||
self._ev_request_preview.clear()
|
||||
# Preview request
|
||||
|
@ -325,7 +321,7 @@ class FaceAlignerTrainerApp:
|
|||
pd.training_data = training_data
|
||||
pd.shift_uni_mats_pred = shift_uni_mats_pred_t.detach().cpu().numpy()
|
||||
self._new_preview_data = pd
|
||||
|
||||
|
||||
if self._is_previewing_samples:
|
||||
self._new_viewing_data = training_data
|
||||
|
||||
|
@ -348,7 +344,7 @@ class FaceAlignerTrainerApp:
|
|||
|
||||
dc.DlgChoice(short_name='l', row_def=f'| Print loss history | Last loss = {last_loss:.5f} ',
|
||||
on_choose=self.on_main_dlg_print_loss_history ),
|
||||
|
||||
|
||||
dc.DlgChoice(short_name='p', row_def='| Show current preview.',
|
||||
on_choose=lambda dlg: (self._ev_request_preview.set(), dlg.recreate().set_current())),
|
||||
|
||||
|
@ -375,8 +371,6 @@ class FaceAlignerTrainerApp:
|
|||
def on_main_dlg_quit(self, dlg):
|
||||
self._ev_request_quit.set()
|
||||
|
||||
|
||||
|
||||
def on_main_dlg_print_loss_history(self, dlg):
|
||||
max_lines = 20
|
||||
for key in self._loss_history.keys():
|
||||
|
@ -384,14 +378,16 @@ class FaceAlignerTrainerApp:
|
|||
|
||||
print(f'Loss history for: {key}')
|
||||
|
||||
d = len(lh) // max_lines
|
||||
lh_len = len(lh)
|
||||
if lh_len >= max_lines:
|
||||
d = len(lh) // max_lines
|
||||
|
||||
lh_ar = np.array(lh[-d*max_lines:], np.float32)
|
||||
lh_ar = lh_ar.reshape( (max_lines, d))
|
||||
lh_ar_max, lh_ar_min, lh_ar_mean, lh_ar_median = lh_ar.max(-1), lh_ar.min(-1), lh_ar.mean(-1), np.median(lh_ar, -1)
|
||||
lh_ar = np.array(lh[-d*max_lines:], np.float32)
|
||||
lh_ar = lh_ar.reshape( (max_lines, d))
|
||||
lh_ar_max, lh_ar_min, lh_ar_mean, lh_ar_median = lh_ar.max(-1), lh_ar.min(-1), lh_ar.mean(-1), np.median(lh_ar, -1)
|
||||
|
||||
|
||||
print( '\n'.join( f'max:[{max_value:.5f}] min:[{min_value:.5f}] mean:[{mean_value:.5f}] median:[{median_value:.5f}]' for max_value, min_value, mean_value, median_value in zip(lh_ar_max, lh_ar_min, lh_ar_mean, lh_ar_median) ) )
|
||||
print( '\n'.join( f'max:[{max_value:.5f}] min:[{min_value:.5f}] mean:[{mean_value:.5f}] median:[{median_value:.5f}]' for max_value, min_value, mean_value, median_value in zip(lh_ar_max, lh_ar_min, lh_ar_mean, lh_ar_median) ) )
|
||||
|
||||
dlg.recreate().set_current()
|
||||
|
||||
|
@ -401,7 +397,7 @@ class FaceAlignerTrainerApp:
|
|||
dc.DlgChoice(short_name='v', row_def=f'| Previewing samples | {self._is_previewing_samples}',
|
||||
on_choose=self.on_sample_generator_dlg_previewing_last_samples,
|
||||
),
|
||||
|
||||
|
||||
dc.DlgChoice(short_name='rw', row_def=f'| Random warp | {self.get_random_warp()}',
|
||||
on_choose=lambda dlg: (self.set_random_warp(not self.get_random_warp()), dlg.recreate().set_current()) ),
|
||||
|
||||
|
@ -469,4 +465,4 @@ class DlgTorchDevicesInfo(dc.DlgChoices):
|
|||
|
||||
class PreviewData:
|
||||
training_data : Data = None
|
||||
shift_uni_mats_pred = None
|
||||
shift_uni_mats_pred = None
|
|
@ -152,43 +152,6 @@ class FaceAlignerNet(nn.Module):
|
|||
aff_t = torch.cat([torch.cos(angle_t)*scale_t, -torch.sin(angle_t)*scale_t, tx_t,
|
||||
torch.sin(angle_t)*scale_t, torch.cos(angle_t)*scale_t, ty_t,
|
||||
], dim=-1).view(-1,2,3)
|
||||
# from xlib.console import diacon
|
||||
# diacon.Diacon.stop()
|
||||
# import code
|
||||
# code.interact(local=dict(globals(), **locals()))
|
||||
|
||||
return aff_t
|
||||
|
||||
|
||||
|
||||
# class CTSOT:
|
||||
# def __init__(self, device_info : TorchDeviceInfo = None,
|
||||
# state_dict : Union[dict, None] = None,
|
||||
# training : bool = False):
|
||||
# if device_info is None:
|
||||
# device_info = get_cpu_device_info()
|
||||
# self.device_info = device_info
|
||||
|
||||
# self._net = net = CTSOTNet()
|
||||
|
||||
# if state_dict is not None:
|
||||
# net.load_state_dict(state_dict)
|
||||
|
||||
# if training:
|
||||
# net.train()
|
||||
# else:
|
||||
# net.eval()
|
||||
|
||||
# self.set_device(device_info)
|
||||
|
||||
# def set_device(self, device_info : TorchDeviceInfo = None):
|
||||
# if device_info is None or device_info.is_cpu():
|
||||
# self._net.cpu()
|
||||
# else:
|
||||
# self._net.cuda(device_info.get_index())
|
||||
|
||||
# def get_state_dict(self):
|
||||
# return self.net.state_dict()
|
||||
|
||||
# def get_net(self) -> CTSOTNet:
|
||||
# return self._net
|
||||
|
|
|
@ -30,8 +30,9 @@ class TLU(nn.Module):
|
|||
return torch.max(x, self.tau)
|
||||
|
||||
class BlurPool(nn.Module):
|
||||
def __init__(self, filt_size=3, stride=2, pad_off=0):
|
||||
def __init__(self, in_ch, filt_size=3, stride=2, pad_off=0):
|
||||
super().__init__()
|
||||
self.in_ch = in_ch
|
||||
self.filt_size = filt_size
|
||||
self.pad_off = pad_off
|
||||
self.pad_sizes = [int(1.*(filt_size-1)/2), int(np.ceil(1.*(filt_size-1)/2)), int(1.*(filt_size-1)/2), int(np.ceil(1.*(filt_size-1)/2))]
|
||||
|
@ -54,13 +55,12 @@ class BlurPool(nn.Module):
|
|||
|
||||
filt = torch.Tensor(a[:,None]*a[None,:])
|
||||
filt = filt/torch.sum(filt)
|
||||
self.register_buffer('filt', filt[None,None,:,:])
|
||||
self.register_buffer('filt', filt[None,None,:,:].repeat(in_ch,1,1,1) )
|
||||
|
||||
self.pad = nn.ZeroPad2d(self.pad_sizes)
|
||||
|
||||
def forward(self, inp):
|
||||
filt = self.filt.repeat((inp.shape[1],1,1,1))
|
||||
return F.conv2d(self.pad(inp), filt, stride=self.stride, groups=inp.shape[1])
|
||||
return F.conv2d(self.pad(inp), self.filt, stride=self.stride, groups=self.in_ch)
|
||||
|
||||
|
||||
class ConvBlock(nn.Module):
|
||||
|
@ -99,30 +99,30 @@ class XSegNet(nn.Module):
|
|||
|
||||
self.conv01 = ConvBlock(in_ch, base_ch)
|
||||
self.conv02 = ConvBlock(base_ch, base_ch)
|
||||
self.bp0 = BlurPool (filt_size=4)
|
||||
self.bp0 = BlurPool (base_ch, filt_size=4)
|
||||
|
||||
self.conv11 = ConvBlock(base_ch, base_ch*2)
|
||||
self.conv12 = ConvBlock(base_ch*2, base_ch*2)
|
||||
self.bp1 = BlurPool (filt_size=3)
|
||||
self.bp1 = BlurPool (base_ch*2, filt_size=3)
|
||||
|
||||
self.conv21 = ConvBlock(base_ch*2, base_ch*4)
|
||||
self.conv22 = ConvBlock(base_ch*4, base_ch*4)
|
||||
self.bp2 = BlurPool (filt_size=2)
|
||||
self.bp2 = BlurPool (base_ch*4, filt_size=2)
|
||||
|
||||
self.conv31 = ConvBlock(base_ch*4, base_ch*8)
|
||||
self.conv32 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.conv33 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.bp3 = BlurPool (filt_size=2)
|
||||
self.bp3 = BlurPool (base_ch*8, filt_size=2)
|
||||
|
||||
self.conv41 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.conv42 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.conv43 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.bp4 = BlurPool (filt_size=2)
|
||||
self.bp4 = BlurPool (base_ch*8, filt_size=2)
|
||||
|
||||
self.conv51 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.conv52 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.conv53 = ConvBlock(base_ch*8, base_ch*8)
|
||||
self.bp5 = BlurPool (filt_size=2)
|
||||
self.bp5 = BlurPool (base_ch*8, filt_size=2)
|
||||
|
||||
self.dense1 = nn.Linear ( 4*4* base_ch*8, 512)
|
||||
self.dense2 = nn.Linear ( 512, 4*4* base_ch*8)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue