mirror of
https://github.com/iperov/DeepFaceLive
synced 2025-07-05 20:42:12 -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 cv2
|
||||||
# import numpy as np
|
# import numpy as np
|
||||||
# from xlib.math import Affine2DMat
|
# from xlib.math import Affine2DMat
|
||||||
|
|
|
@ -288,10 +288,10 @@ class FaceAlignerTrainerApp:
|
||||||
if self._is_training or \
|
if self._is_training or \
|
||||||
self._is_previewing_samples or \
|
self._is_previewing_samples or \
|
||||||
self._ev_request_preview.is_set():
|
self._ev_request_preview.is_set():
|
||||||
|
|
||||||
training_data = self._training_generator.get_next_data(wait=False)
|
training_data = self._training_generator.get_next_data(wait=False)
|
||||||
if training_data is not None and \
|
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:
|
if self._is_training:
|
||||||
self._model_optimizer.zero_grad()
|
self._model_optimizer.zero_grad()
|
||||||
|
|
||||||
|
@ -304,18 +304,14 @@ class FaceAlignerTrainerApp:
|
||||||
if self._is_training:
|
if self._is_training:
|
||||||
# Training optimization step
|
# Training optimization step
|
||||||
shift_uni_mats_t = torch.tensor(training_data.shift_uni_mats).to(self._device)
|
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 = (shift_uni_mats_pred_t-shift_uni_mats_t).square().mean()*10.0
|
||||||
loss_t.backward()
|
loss_t.backward()
|
||||||
self._model_optimizer.step()
|
self._model_optimizer.step()
|
||||||
|
|
||||||
loss = loss_t.detach().cpu().numpy()
|
loss = loss_t.detach().cpu().numpy()
|
||||||
|
|
||||||
rec_loss_history = self._loss_history.get('reconstruct', None)
|
rec_loss_history = self._loss_history.get('reconstruct', None)
|
||||||
if rec_loss_history is None:
|
if rec_loss_history is None:
|
||||||
rec_loss_history = self._loss_history['reconstruct'] = []
|
rec_loss_history = self._loss_history['reconstruct'] = []
|
||||||
rec_loss_history.append(float(loss))
|
rec_loss_history.append(float(loss))
|
||||||
|
|
||||||
self.set_iteration( self.get_iteration() + 1 )
|
self.set_iteration( self.get_iteration() + 1 )
|
||||||
|
|
||||||
if self._ev_request_preview.is_set():
|
if self._ev_request_preview.is_set():
|
||||||
|
@ -375,8 +371,6 @@ class FaceAlignerTrainerApp:
|
||||||
def on_main_dlg_quit(self, dlg):
|
def on_main_dlg_quit(self, dlg):
|
||||||
self._ev_request_quit.set()
|
self._ev_request_quit.set()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def on_main_dlg_print_loss_history(self, dlg):
|
def on_main_dlg_print_loss_history(self, dlg):
|
||||||
max_lines = 20
|
max_lines = 20
|
||||||
for key in self._loss_history.keys():
|
for key in self._loss_history.keys():
|
||||||
|
@ -384,6 +378,8 @@ class FaceAlignerTrainerApp:
|
||||||
|
|
||||||
print(f'Loss history for: {key}')
|
print(f'Loss history for: {key}')
|
||||||
|
|
||||||
|
lh_len = len(lh)
|
||||||
|
if lh_len >= max_lines:
|
||||||
d = len(lh) // max_lines
|
d = len(lh) // max_lines
|
||||||
|
|
||||||
lh_ar = np.array(lh[-d*max_lines:], np.float32)
|
lh_ar = np.array(lh[-d*max_lines:], np.float32)
|
||||||
|
|
|
@ -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,
|
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,
|
torch.sin(angle_t)*scale_t, torch.cos(angle_t)*scale_t, ty_t,
|
||||||
], dim=-1).view(-1,2,3)
|
], 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
|
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)
|
return torch.max(x, self.tau)
|
||||||
|
|
||||||
class BlurPool(nn.Module):
|
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__()
|
super().__init__()
|
||||||
|
self.in_ch = in_ch
|
||||||
self.filt_size = filt_size
|
self.filt_size = filt_size
|
||||||
self.pad_off = pad_off
|
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))]
|
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 = torch.Tensor(a[:,None]*a[None,:])
|
||||||
filt = filt/torch.sum(filt)
|
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)
|
self.pad = nn.ZeroPad2d(self.pad_sizes)
|
||||||
|
|
||||||
def forward(self, inp):
|
def forward(self, inp):
|
||||||
filt = self.filt.repeat((inp.shape[1],1,1,1))
|
return F.conv2d(self.pad(inp), self.filt, stride=self.stride, groups=self.in_ch)
|
||||||
return F.conv2d(self.pad(inp), filt, stride=self.stride, groups=inp.shape[1])
|
|
||||||
|
|
||||||
|
|
||||||
class ConvBlock(nn.Module):
|
class ConvBlock(nn.Module):
|
||||||
|
@ -99,30 +99,30 @@ class XSegNet(nn.Module):
|
||||||
|
|
||||||
self.conv01 = ConvBlock(in_ch, base_ch)
|
self.conv01 = ConvBlock(in_ch, base_ch)
|
||||||
self.conv02 = ConvBlock(base_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.conv11 = ConvBlock(base_ch, base_ch*2)
|
||||||
self.conv12 = ConvBlock(base_ch*2, 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.conv21 = ConvBlock(base_ch*2, base_ch*4)
|
||||||
self.conv22 = ConvBlock(base_ch*4, 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.conv31 = ConvBlock(base_ch*4, base_ch*8)
|
||||||
self.conv32 = ConvBlock(base_ch*8, base_ch*8)
|
self.conv32 = ConvBlock(base_ch*8, base_ch*8)
|
||||||
self.conv33 = 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.conv41 = ConvBlock(base_ch*8, base_ch*8)
|
||||||
self.conv42 = 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.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.conv51 = ConvBlock(base_ch*8, base_ch*8)
|
||||||
self.conv52 = 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.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.dense1 = nn.Linear ( 4*4* base_ch*8, 512)
|
||||||
self.dense2 = nn.Linear ( 512, 4*4* base_ch*8)
|
self.dense2 = nn.Linear ( 512, 4*4* base_ch*8)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue