mirror of
https://github.com/iperov/DeepFaceLab.git
synced 2025-07-11 15:47:01 -07:00
update SampleGeneratorFaceSkinSegDataset
This commit is contained in:
parent
7c89077321
commit
144675020c
1 changed files with 80 additions and 79 deletions
|
@ -8,6 +8,7 @@ import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from core import imagelib, mplib, pathex
|
from core import imagelib, mplib, pathex
|
||||||
|
from core.imagelib import sd
|
||||||
from core.cv2ex import *
|
from core.cv2ex import *
|
||||||
from core.interact import interact as io
|
from core.interact import interact as io
|
||||||
from core.joblib import SubprocessGenerator, ThisThreadGenerator
|
from core.joblib import SubprocessGenerator, ThisThreadGenerator
|
||||||
|
@ -121,8 +122,9 @@ class SampleGeneratorFaceSkinSegDataset(SampleGeneratorBase):
|
||||||
|
|
||||||
samples = pickle.loads(pickled_samples)
|
samples = pickle.loads(pickled_samples)
|
||||||
|
|
||||||
|
obstructions_images_paths_len = len(obstructions_images_paths)
|
||||||
shuffle_o_idxs = []
|
shuffle_o_idxs = []
|
||||||
o_idxs = [*range(len(obstructions_images_paths))]
|
o_idxs = [*range(obstructions_images_paths_len)]
|
||||||
|
|
||||||
shuffle_idxs = []
|
shuffle_idxs = []
|
||||||
idxs = [*range(len(samples))]
|
idxs = [*range(len(samples))]
|
||||||
|
@ -178,58 +180,59 @@ class SampleGeneratorFaceSkinSegDataset(SampleGeneratorBase):
|
||||||
if len(mask.shape) == 2:
|
if len(mask.shape) == 2:
|
||||||
mask = mask[...,None]
|
mask = mask[...,None]
|
||||||
|
|
||||||
# apply obstruction
|
if obstructions_images_paths_len != 0:
|
||||||
if len(shuffle_o_idxs) == 0:
|
# apply obstruction
|
||||||
shuffle_o_idxs = o_idxs.copy()
|
if len(shuffle_o_idxs) == 0:
|
||||||
np.random.shuffle(shuffle_o_idxs)
|
shuffle_o_idxs = o_idxs.copy()
|
||||||
o_idx = shuffle_o_idxs.pop()
|
np.random.shuffle(shuffle_o_idxs)
|
||||||
o_img = cv2_imread (obstructions_images_paths[o_idx]).astype(np.float32) / 255.0
|
o_idx = shuffle_o_idxs.pop()
|
||||||
oh,ow,oc = o_img.shape
|
o_img = cv2_imread (obstructions_images_paths[o_idx]).astype(np.float32) / 255.0
|
||||||
if oc == 4:
|
oh,ow,oc = o_img.shape
|
||||||
ohw = max(oh,ow)
|
if oc == 4:
|
||||||
scale = resolution / ohw
|
ohw = max(oh,ow)
|
||||||
|
scale = resolution / ohw
|
||||||
|
|
||||||
#o_img = cv2.resize (o_img, ( int(ow*rate), int(oh*rate), ), cv2.INTER_CUBIC)
|
#o_img = cv2.resize (o_img, ( int(ow*rate), int(oh*rate), ), cv2.INTER_CUBIC)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
mat = cv2.getRotationMatrix2D( (ow/2,oh/2),
|
mat = cv2.getRotationMatrix2D( (ow/2,oh/2),
|
||||||
np.random.uniform( o_rotation_range[0], o_rotation_range[1] ),
|
np.random.uniform( o_rotation_range[0], o_rotation_range[1] ),
|
||||||
1.0 )
|
1.0 )
|
||||||
|
|
||||||
mat += np.float32( [[0,0, -ow/2 ],
|
mat += np.float32( [[0,0, -ow/2 ],
|
||||||
[0,0, -oh/2 ]])
|
[0,0, -oh/2 ]])
|
||||||
mat *= scale * np.random.uniform(1 +o_scale_range[0], 1 +o_scale_range[1])
|
mat *= scale * np.random.uniform(1 +o_scale_range[0], 1 +o_scale_range[1])
|
||||||
mat += np.float32( [[0, 0, resolution/2 + resolution*np.random.uniform( o_tx_range[0], o_tx_range[1] ) ],
|
mat += np.float32( [[0, 0, resolution/2 + resolution*np.random.uniform( o_tx_range[0], o_tx_range[1] ) ],
|
||||||
[0, 0, resolution/2 + resolution*np.random.uniform( o_ty_range[0], o_ty_range[1] ) ] ])
|
[0, 0, resolution/2 + resolution*np.random.uniform( o_ty_range[0], o_ty_range[1] ) ] ])
|
||||||
|
|
||||||
|
|
||||||
o_img = cv2.warpAffine( o_img, mat, (resolution,resolution), borderMode=cv2.BORDER_CONSTANT, flags=cv2.INTER_LANCZOS4 )
|
o_img = cv2.warpAffine( o_img, mat, (resolution,resolution), borderMode=cv2.BORDER_CONSTANT, flags=cv2.INTER_LANCZOS4 )
|
||||||
|
|
||||||
if o_random_flip and np.random.randint(10) < 4:
|
if o_random_flip and np.random.randint(10) < 4:
|
||||||
o_img = o_img[:,::-1,...]
|
o_img = o_img[:,::-1,...]
|
||||||
|
|
||||||
o_mask = o_img[...,3:4]
|
o_mask = o_img[...,3:4]
|
||||||
o_mask[o_mask>0] = 1.0
|
o_mask[o_mask>0] = 1.0
|
||||||
|
|
||||||
|
|
||||||
o_mask = cv2.erode (o_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1 )
|
o_mask = cv2.erode (o_mask, cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(5,5)), iterations = 1 )
|
||||||
o_mask = cv2.GaussianBlur(o_mask, (5, 5) , 0)[...,None]
|
o_mask = cv2.GaussianBlur(o_mask, (5, 5) , 0)[...,None]
|
||||||
|
|
||||||
img = img*(1-o_mask) + o_img[...,0:3]*o_mask
|
img = img*(1-o_mask) + o_img[...,0:3]*o_mask
|
||||||
|
|
||||||
o_mask[o_mask<0.5] = 0.0
|
o_mask[o_mask<0.5] = 0.0
|
||||||
|
|
||||||
|
|
||||||
#import code
|
#import code
|
||||||
#code.interact(local=dict(globals(), **locals()))
|
#code.interact(local=dict(globals(), **locals()))
|
||||||
mask *= (1-o_mask)
|
mask *= (1-o_mask)
|
||||||
|
|
||||||
|
|
||||||
#cv2.imshow ("", np.clip(o_img*255, 0,255).astype(np.uint8) )
|
#cv2.imshow ("", np.clip(o_img*255, 0,255).astype(np.uint8) )
|
||||||
#cv2.waitKey(0)
|
#cv2.waitKey(0)
|
||||||
|
|
||||||
|
|
||||||
img = imagelib.warp_by_params (warp_params, img, can_warp=True, can_transform=True, can_flip=True, border_replicate=False)
|
img = imagelib.warp_by_params (warp_params, img, can_warp=True, can_transform=True, can_flip=True, border_replicate=False)
|
||||||
|
@ -241,13 +244,11 @@ class SampleGeneratorFaceSkinSegDataset(SampleGeneratorBase):
|
||||||
mask[mask >= 0.5] = 1.0
|
mask[mask >= 0.5] = 1.0
|
||||||
mask = np.clip(mask, 0, 1)
|
mask = np.clip(mask, 0, 1)
|
||||||
|
|
||||||
img = imagelib.apply_random_hsv_shift(img)
|
|
||||||
|
|
||||||
#todo random mask for blur
|
img = imagelib.apply_random_hsv_shift(img, mask=sd.random_circle_faded ([resolution,resolution]))
|
||||||
|
img = imagelib.apply_random_motion_blur( img, motion_blur_chance, motion_blur_mb_max_size, mask=sd.random_circle_faded ([resolution,resolution]))
|
||||||
img = imagelib.apply_random_motion_blur( img, motion_blur_chance, motion_blur_mb_max_size )
|
img = imagelib.apply_random_gaussian_blur( img, gaussian_blur_chance, gaussian_blur_kernel_max_size, mask=sd.random_circle_faded ([resolution,resolution]))
|
||||||
img = imagelib.apply_random_gaussian_blur( img, gaussian_blur_chance, gaussian_blur_kernel_max_size )
|
img = imagelib.apply_random_bilinear_resize( img, random_bilinear_resize_chance, random_bilinear_resize_max_size_per, mask=sd.random_circle_faded ([resolution,resolution]))
|
||||||
img = imagelib.apply_random_bilinear_resize( img, random_bilinear_resize_chance, random_bilinear_resize_max_size_per )
|
|
||||||
|
|
||||||
if data_format == "NCHW":
|
if data_format == "NCHW":
|
||||||
img = np.transpose(img, (2,0,1) )
|
img = np.transpose(img, (2,0,1) )
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue