Upload 203 files
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+1
- video-tetalking/CODE_OF_CONDUCT.mdAdded+43
- video-tetalking/inference.pyAdded+345
- video-tetalking/inference_videoretalking.shAdded+4
- video-tetalking/models/DNet.pyAdded+118
- video-tetalking/models/ENet.pyAdded+139
- video-tetalking/models/LNet.pyAdded+139
- video-tetalking/models/__init__.pyAdded+37
- video-tetalking/models/base_blocks.pyAdded+554
- video-tetalking/models/ffc.pyAdded+233
- video-tetalking/models/transformer.pyAdded+119
- video-tetalking/predict.pyAdded+552
- video-tetalking/quick_demo.ipynbAdded+293
- video-tetalking/requirements.txtAdded+10
- video-tetalking/third_part/GFPGAN/LICENSEAdded+351
- video-tetalking/third_part/GFPGAN/gfpgan/__init__.pyAdded+8
- video-tetalking/third_part/GFPGAN/gfpgan/archs/__init__.pyAdded+10
- video-tetalking/third_part/GFPGAN/gfpgan/archs/arcface_arch.pyAdded+245
- video-tetalking/third_part/GFPGAN/gfpgan/archs/gfpgan_bilinear_arch.pyAdded+312
- video-tetalking/third_part/GFPGAN/gfpgan/archs/gfpganv1_arch.pyAdded+439
- video-tetalking/third_part/GFPGAN/gfpgan/archs/gfpganv1_clean_arch.pyAdded+324
- video-tetalking/third_part/GFPGAN/gfpgan/archs/stylegan2_bilinear_arch.pyAdded+613
- video-tetalking/third_part/GFPGAN/gfpgan/archs/stylegan2_clean_arch.pyAdded+368
- video-tetalking/third_part/GFPGAN/gfpgan/data/__init__.pyAdded+10
- video-tetalking/third_part/GFPGAN/gfpgan/data/ffhq_degradation_dataset.pyAdded+230
- video-tetalking/third_part/GFPGAN/gfpgan/models/__init__.pyAdded+10
- video-tetalking/third_part/GFPGAN/gfpgan/models/gfpgan_model.pyAdded+580
- video-tetalking/third_part/GFPGAN/gfpgan/train.pyAdded+11
- video-tetalking/third_part/GFPGAN/gfpgan/utils.pyAdded+143
- video-tetalking/third_part/GFPGAN/gfpgan/version.pyAdded+5
- video-tetalking/third_part/GFPGAN/gfpgan/weights/README.mdAdded+3
- video-tetalking/third_part/GFPGAN/options/train_gfpgan_v1.ymlAdded+216
- video-tetalking/third_part/GFPGAN/options/train_gfpgan_v1_simple.ymlAdded+182
- video-tetalking/third_part/GPEN/align_faces.pyAdded+271
- video-tetalking/third_part/GPEN/face_detect/.DS_StoreAdded
- video-tetalking/third_part/GPEN/face_detect/data/FDDB/img_list.txtAdded+2845
- video-tetalking/third_part/GPEN/face_detect/data/__init__.pyAdded+3
- video-tetalking/third_part/GPEN/face_detect/data/config.pyAdded+42
- video-tetalking/third_part/GPEN/face_detect/data/data_augment.pyAdded+237
- video-tetalking/third_part/GPEN/face_detect/data/wider_face.pyAdded+101
- video-tetalking/third_part/GPEN/face_detect/facemodels/__init__.pyAdded
- video-tetalking/third_part/GPEN/face_detect/facemodels/net.pyAdded+137
- video-tetalking/third_part/GPEN/face_detect/facemodels/retinaface.pyAdded+127
- video-tetalking/third_part/GPEN/face_detect/layers/__init__.pyAdded+2
- video-tetalking/third_part/GPEN/face_detect/layers/functions/prior_box.pyAdded+34
- video-tetalking/third_part/GPEN/face_detect/layers/modules/__init__.pyAdded+3
- video-tetalking/third_part/GPEN/face_detect/layers/modules/multibox_loss.pyAdded+125
- video-tetalking/third_part/GPEN/face_detect/retinaface_detection.pyAdded+193
- video-tetalking/third_part/GPEN/face_detect/utils/__init__.pyAdded
- video-tetalking/third_part/GPEN/face_detect/utils/box_utils.pyAdded+330
186 unmodified lines187188189190186 unmodified linesSimSwap/temp_results/frame_0000059.jpg filter=lfs diff=lfs merge=lfs -textSimSwap/temp_results/frame_0000060.jpg filter=lfs diff=lfs merge=lfs -textSimSwap/temp_results/frame_0000061.jpg filter=lfs diff=lfs merge=lfs -textvideo-tetalking/third_part/GPEN/face_parse/test.png filter=lfs diff=lfs merge=lfs -text12345678910111213141516171819202122232425262728293031323334353637383940414243# Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to make participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to creating a positive environment include:
- Using welcoming and inclusive language
- Being respectful of differing viewpoints and experiences
- Gracefully accepting constructive criticism
- Focusing on what is best for the community
- Showing empathy towards other community members
Examples of unacceptable behavior by participants include:
- The use of sexualized language or imagery and unwelcome sexual attention or advances
- Trolling, insulting/derogatory comments, and personal or political attacks
- Public or private harassment
- Publishing others' private information, such as a physical or electronic address, without explicit permission
- Other conduct that could reasonably be considered inappropriate in a professional setting
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned with this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project email address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident.
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
## Attribution
This Code of Conduct is adapted from the Contributor Covenant, version 2.0, available at [https://www.contributor-covenant.org/version/2/0/code_of_conduct.html](https://www.contributor-covenant.org/version/2/0/code_of_conduct.html).123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345import numpy as np
import cv2, os, sys, subprocess, platform, torch
from tqdm import tqdm
from PIL import Image
from scipy.io import loadmat
sys.path.insert(0, 'third_part')
sys.path.insert(0, 'third_part/GPEN')
sys.path.insert(0, 'third_part/GFPGAN')
# 3dmm extraction
from third_part.face3d.util.preprocess import align_img
from third_part.face3d.util.load_mats import load_lm3d
from third_part.face3d.extract_kp_videos import KeypointExtractor
# face enhancement
from third_part.GPEN.gpen_face_enhancer import FaceEnhancement
from third_part.GFPGAN.gfpgan import GFPGANer
# expression control
from third_part.ganimation_replicate.model.ganimation import GANimationModel
from utils import audio
from utils.ffhq_preprocess import Croper
from utils.alignment_stit import crop_faces, calc_alignment_coefficients, paste_image
from utils.inference_utils import Laplacian_Pyramid_Blending_with_mask, face_detect, load_model, options, split_coeff, \
trans_image, transform_semantic, find_crop_norm_ratio, load_face3d_net, exp_aus_dict
import warnings
warnings.filterwarnings("ignore")
args = options()
def main():
device = 'cuda' if torch.cuda.is_available() else 'cpu'
print('[Info] Using {} for inference.'.format(device))
os.makedirs(os.path.join('temp', args.tmp_dir), exist_ok=True)
enhancer = FaceEnhancement(base_dir='checkpoints', size=512, model='GPEN-BFR-512', use_sr=False, \
sr_model='rrdb_realesrnet_psnr', channel_multiplier=2, narrow=1, device=device)
restorer = GFPGANer(model_path='checkpoints/GFPGANv1.3.pth', upscale=1, arch='clean', \
channel_multiplier=2, bg_upsampler=None)
base_name = args.face.split('/')[-1]
if os.path.isfile(args.face) and args.face.split('.')[1] in ['jpg', 'png', 'jpeg']:
args.static = True
if not os.path.isfile(args.face):
raise ValueError('--face argument must be a valid path to video/image file')
elif args.face.split('.')[1] in ['jpg', 'png', 'jpeg']:
full_frames = [cv2.imread(args.face)]
fps = args.fps
else:
video_stream = cv2.VideoCapture(args.face)
fps = video_stream.get(cv2.CAP_PROP_FPS)
full_frames = []
while True:
still_reading, frame = video_stream.read()
if not still_reading:
video_stream.release()
break
y1, y2, x1, x2 = args.crop
if x2 == -1: x2 = frame.shape[1]
if y2 == -1: y2 = frame.shape[0]
frame = frame[y1:y2, x1:x2]
full_frames.append(frame)
print ("[Step 0] Number of frames available for inference: "+str(len(full_frames)))
# face detection & cropping, cropping the first frame as the style of FFHQ
croper = Croper('checkpoints/shape_predictor_68_face_landmarks.dat')
full_frames_RGB = [cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in full_frames]
full_frames_RGB, crop, quad = croper.crop(full_frames_RGB, xsize=512)
clx, cly, crx, cry = crop
lx, ly, rx, ry = quad
lx, ly, rx, ry = int(lx), int(ly), int(rx), int(ry)
oy1, oy2, ox1, ox2 = cly+ly, min(cly+ry, full_frames[0].shape[0]), clx+lx, min(clx+rx, full_frames[0].shape[1])
# original_size = (ox2 - ox1, oy2 - oy1)
frames_pil = [Image.fromarray(cv2.resize(frame,(256,256))) for frame in full_frames_RGB]
# get the landmark according to the detected face.
if not os.path.isfile('temp/'+base_name+'_landmarks.txt') or args.re_preprocess:
print('[Step 1] Landmarks Extraction in Video.')
kp_extractor = KeypointExtractor()
lm = kp_extractor.extract_keypoint(frames_pil, './temp/'+base_name+'_landmarks.txt')
else:
print('[Step 1] Using saved landmarks.')
lm = np.loadtxt('temp/'+base_name+'_landmarks.txt').astype(np.float32)
lm = lm.reshape([len(full_frames), -1, 2])
if not os.path.isfile('temp/'+base_name+'_coeffs.npy') or args.exp_img is not None or args.re_preprocess:
net_recon = load_face3d_net(args.face3d_net_path, device)
lm3d_std = load_lm3d('checkpoints/BFM')
video_coeffs = []
for idx in tqdm(range(len(frames_pil)), desc="[Step 2] 3DMM Extraction In Video:"):
frame = frames_pil[idx]
W, H = frame.size
lm_idx = lm[idx].reshape([-1, 2])
if np.mean(lm_idx) == -1:
lm_idx = (lm3d_std[:, :2]+1) / 2.
lm_idx = np.concatenate([lm_idx[:, :1] * W, lm_idx[:, 1:2] * H], 1)
else:
lm_idx[:, -1] = H - 1 - lm_idx[:, -1]
trans_params, im_idx, lm_idx, _ = align_img(frame, lm_idx, lm3d_std)
trans_params = np.array([float(item) for item in np.hsplit(trans_params, 5)]).astype(np.float32)
im_idx_tensor = torch.tensor(np.array(im_idx)/255., dtype=torch.float32).permute(2, 0, 1).to(device).unsqueeze(0)
with torch.no_grad():
coeffs = split_coeff(net_recon(im_idx_tensor))
pred_coeff = {key:coeffs[key].cpu().numpy() for key in coeffs}
pred_coeff = np.concatenate([pred_coeff['id'], pred_coeff['exp'], pred_coeff['tex'], pred_coeff['angle'],\
pred_coeff['gamma'], pred_coeff['trans'], trans_params[None]], 1)
video_coeffs.append(pred_coeff)
semantic_npy = np.array(video_coeffs)[:,0]
np.save('temp/'+base_name+'_coeffs.npy', semantic_npy)
else:
print('[Step 2] Using saved coeffs.')
semantic_npy = np.load('temp/'+base_name+'_coeffs.npy').astype(np.float32)
# generate the 3dmm coeff from a single image
if args.exp_img is not None and ('.png' in args.exp_img or '.jpg' in args.exp_img):
print('extract the exp from',args.exp_img)
exp_pil = Image.open(args.exp_img).convert('RGB')
lm3d_std = load_lm3d('third_part/face3d/BFM')
W, H = exp_pil.size
kp_extractor = KeypointExtractor()
lm_exp = kp_extractor.extract_keypoint([exp_pil], 'temp/'+base_name+'_temp.txt')[0]
if np.mean(lm_exp) == -1:
lm_exp = (lm3d_std[:, :2] + 1) / 2.
lm_exp = np.concatenate(
[lm_exp[:, :1] * W, lm_exp[:, 1:2] * H], 1)
else:
lm_exp[:, -1] = H - 1 - lm_exp[:, -1]
trans_params, im_exp, lm_exp, _ = align_img(exp_pil, lm_exp, lm3d_std)
trans_params = np.array([float(item) for item in np.hsplit(trans_params, 5)]).astype(np.float32)
im_exp_tensor = torch.tensor(np.array(im_exp)/255., dtype=torch.float32).permute(2, 0, 1).to(device).unsqueeze(0)
with torch.no_grad():
expression = split_coeff(net_recon(im_exp_tensor))['exp'][0]
del net_recon
elif args.exp_img == 'smile':
expression = torch.tensor(loadmat('checkpoints/expression.mat')['expression_mouth'])[0]
else:
print('using expression center')
expression = torch.tensor(loadmat('checkpoints/expression.mat')['expression_center'])[0]
# load DNet, model(LNet and ENet)
D_Net, model = load_model(args, device)
if not os.path.isfile('temp/'+base_name+'_stablized.npy') or args.re_preprocess:
imgs = []
for idx in tqdm(range(len(frames_pil)), desc="[Step 3] Stabilize the expression In Video:"):
if args.one_shot:
source_img = trans_image(frames_pil[0]).unsqueeze(0).to(device)
semantic_source_numpy = semantic_npy[0:1]
else:
source_img = trans_image(frames_pil[idx]).unsqueeze(0).to(device)
semantic_source_numpy = semantic_npy[idx:idx+1]
ratio = find_crop_norm_ratio(semantic_source_numpy, semantic_npy)
coeff = transform_semantic(semantic_npy, idx, ratio).unsqueeze(0).to(device)
# hacking the new expression
coeff[:, :64, :] = expression[None, :64, None].to(device)
with torch.no_grad():
output = D_Net(source_img, coeff)
img_stablized = np.uint8((output['fake_image'].squeeze(0).permute(1,2,0).cpu().clamp_(-1, 1).numpy() + 1 )/2. * 255)
imgs.append(cv2.cvtColor(img_stablized,cv2.COLOR_RGB2BGR))
np.save('temp/'+base_name+'_stablized.npy',imgs)
del D_Net
else:
print('[Step 3] Using saved stabilized video.')
imgs = np.load('temp/'+base_name+'_stablized.npy')
torch.cuda.empty_cache()
if not args.audio.endswith('.wav'):
command = 'ffmpeg -loglevel error -y -i {} -strict -2 {}'.format(args.audio, 'temp/{}/temp.wav'.format(args.tmp_dir))
subprocess.call(command, shell=True)
args.audio = 'temp/{}/temp.wav'.format(args.tmp_dir)
wav = audio.load_wav(args.audio, 16000)
mel = audio.melspectrogram(wav)
if np.isnan(mel.reshape(-1)).sum() > 0:
raise ValueError('Mel contains nan! Using a TTS voice? Add a small epsilon noise to the wav file and try again')
mel_step_size, mel_idx_multiplier, i, mel_chunks = 16, 80./fps, 0, []
while True:
start_idx = int(i * mel_idx_multiplier)
if start_idx + mel_step_size > len(mel[0]):
mel_chunks.append(mel[:, len(mel[0]) - mel_step_size:])
break
mel_chunks.append(mel[:, start_idx : start_idx + mel_step_size])
i += 1
print("[Step 4] Load audio; Length of mel chunks: {}".format(len(mel_chunks)))
imgs = imgs[:len(mel_chunks)]
full_frames = full_frames[:len(mel_chunks)]
lm = lm[:len(mel_chunks)]
imgs_enhanced = []
for idx in tqdm(range(len(imgs)), desc='[Step 5] Reference Enhancement'):
img = imgs[idx]
pred, _, _ = enhancer.process(img, img, face_enhance=True, possion_blending=False)
imgs_enhanced.append(pred)
gen = datagen(imgs_enhanced.copy(), mel_chunks, full_frames, None, (oy1,oy2,ox1,ox2))
frame_h, frame_w = full_frames[0].shape[:-1]
out = cv2.VideoWriter('temp/{}/result.mp4'.format(args.tmp_dir), cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_w, frame_h))
if args.up_face != 'original':
instance = GANimationModel()
instance.initialize()
instance.setup()
kp_extractor = KeypointExtractor()
for i, (img_batch, mel_batch, frames, coords, img_original, f_frames) in enumerate(tqdm(gen, desc='[Step 6] Lip Synthesis:', total=int(np.ceil(float(len(mel_chunks)) / args.LNet_batch_size)))):
img_batch = torch.FloatTensor(np.transpose(img_batch, (0, 3, 1, 2))).to(device)
mel_batch = torch.FloatTensor(np.transpose(mel_batch, (0, 3, 1, 2))).to(device)
img_original = torch.FloatTensor(np.transpose(img_original, (0, 3, 1, 2))).to(device)/255. # BGR -> RGB
with torch.no_grad():
incomplete, reference = torch.split(img_batch, 3, dim=1)
pred, low_res = model(mel_batch, img_batch, reference)
pred = torch.clamp(pred, 0, 1)
if args.up_face in ['sad', 'angry', 'surprise']:
tar_aus = exp_aus_dict[args.up_face]
else:
pass
if args.up_face == 'original':
cur_gen_faces = img_original
else:
test_batch = {'src_img': torch.nn.functional.interpolate((img_original * 2 - 1), size=(128, 128), mode='bilinear'),
'tar_aus': tar_aus.repeat(len(incomplete), 1)}
instance.feed_batch(test_batch)
instance.forward()
cur_gen_faces = torch.nn.functional.interpolate(instance.fake_img / 2. + 0.5, size=(384, 384), mode='bilinear')
if args.without_rl1 is not False:
incomplete, reference = torch.split(img_batch, 3, dim=1)
mask = torch.where(incomplete==0, torch.ones_like(incomplete), torch.zeros_like(incomplete))
pred = pred * mask + cur_gen_faces * (1 - mask)
pred = pred.cpu().numpy().transpose(0, 2, 3, 1) * 255.
torch.cuda.empty_cache()
for p, f, xf, c in zip(pred, frames, f_frames, coords):
y1, y2, x1, x2 = c
p = cv2.resize(p.astype(np.uint8), (x2 - x1, y2 - y1))
ff = xf.copy()
ff[y1:y2, x1:x2] = p
# month region enhancement by GFPGAN
cropped_faces, restored_faces, restored_img = restorer.enhance(
ff, has_aligned=False, only_center_face=True, paste_back=True)
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
mm = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0]
mouse_mask = np.zeros_like(restored_img)
tmp_mask = enhancer.faceparser.process(restored_img[y1:y2, x1:x2], mm)[0]
mouse_mask[y1:y2, x1:x2]= cv2.resize(tmp_mask, (x2 - x1, y2 - y1))[:, :, np.newaxis] / 255.
height, width = ff.shape[:2]
restored_img, ff, full_mask = [cv2.resize(x, (512, 512)) for x in (restored_img, ff, np.float32(mouse_mask))]
img = Laplacian_Pyramid_Blending_with_mask(restored_img, ff, full_mask[:, :, 0], 10)
pp = np.uint8(cv2.resize(np.clip(img, 0 ,255), (width, height)))
pp, orig_faces, enhanced_faces = enhancer.process(pp, xf, bbox=c, face_enhance=False, possion_blending=True)
out.write(pp)
out.release()
if not os.path.isdir(os.path.dirname(args.outfile)):
os.makedirs(os.path.dirname(args.outfile), exist_ok=True)
command = 'ffmpeg -loglevel error -y -i {} -i {} -strict -2 -q:v 1 {}'.format(args.audio, 'temp/{}/result.mp4'.format(args.tmp_dir), args.outfile)
subprocess.call(command, shell=platform.system() != 'Windows')
print('outfile:', args.outfile)
# frames:256x256, full_frames: original size
def datagen(frames, mels, full_frames, frames_pil, cox):
img_batch, mel_batch, frame_batch, coords_batch, ref_batch, full_frame_batch = [], [], [], [], [], []
base_name = args.face.split('/')[-1]
refs = []
image_size = 256
# original frames
kp_extractor = KeypointExtractor()
fr_pil = [Image.fromarray(frame) for frame in frames]
lms = kp_extractor.extract_keypoint(fr_pil, 'temp/'+base_name+'x12_landmarks.txt')
frames_pil = [ (lm, frame) for frame,lm in zip(fr_pil, lms)] # frames is the croped version of modified face
crops, orig_images, quads = crop_faces(image_size, frames_pil, scale=1.0, use_fa=True)
inverse_transforms = [calc_alignment_coefficients(quad + 0.5, [[0, 0], [0, image_size], [image_size, image_size], [image_size, 0]]) for quad in quads]
del kp_extractor.detector
oy1,oy2,ox1,ox2 = cox
face_det_results = face_detect(full_frames, args, jaw_correction=True)
for inverse_transform, crop, full_frame, face_det in zip(inverse_transforms, crops, full_frames, face_det_results):
imc_pil = paste_image(inverse_transform, crop, Image.fromarray(
cv2.resize(full_frame[int(oy1):int(oy2), int(ox1):int(ox2)], (256, 256))))
ff = full_frame.copy()
ff[int(oy1):int(oy2), int(ox1):int(ox2)] = cv2.resize(np.array(imc_pil.convert('RGB')), (ox2 - ox1, oy2 - oy1))
oface, coords = face_det
y1, y2, x1, x2 = coords
refs.append(ff[y1: y2, x1:x2])
for i, m in enumerate(mels):
idx = 0 if args.static else i % len(frames)
frame_to_save = frames[idx].copy()
face = refs[idx]
oface, coords = face_det_results[idx].copy()
face = cv2.resize(face, (args.img_size, args.img_size))
oface = cv2.resize(oface, (args.img_size, args.img_size))
img_batch.append(oface)
ref_batch.append(face)
mel_batch.append(m)
coords_batch.append(coords)
frame_batch.append(frame_to_save)
full_frame_batch.append(full_frames[idx].copy())
if len(img_batch) >= args.LNet_batch_size:
img_batch, mel_batch, ref_batch = np.asarray(img_batch), np.asarray(mel_batch), np.asarray(ref_batch)
img_masked = img_batch.copy()
img_original = img_batch.copy()
img_masked[:, args.img_size//2:] = 0
img_batch = np.concatenate((img_masked, ref_batch), axis=3) / 255.
mel_batch = np.reshape(mel_batch, [len(mel_batch), mel_batch.shape[1], mel_batch.shape[2], 1])
yield img_batch, mel_batch, frame_batch, coords_batch, img_original, full_frame_batch
img_batch, mel_batch, frame_batch, coords_batch, img_original, full_frame_batch, ref_batch = [], [], [], [], [], [], []
if len(img_batch) > 0:
img_batch, mel_batch, ref_batch = np.asarray(img_batch), np.asarray(mel_batch), np.asarray(ref_batch)
img_masked = img_batch.copy()
img_original = img_batch.copy()
img_masked[:, args.img_size//2:] = 0
img_batch = np.concatenate((img_masked, ref_batch), axis=3) / 255.
mel_batch = np.reshape(mel_batch, [len(mel_batch), mel_batch.shape[1], mel_batch.shape[2], 1])
yield img_batch, mel_batch, frame_batch, coords_batch, img_original, full_frame_batch
if __name__ == '__main__':
main()1234python3 inference.py \
--face ./examples/face/1.mp4 \
--audio ./examples/audio/1.wav \
--outfile results/1_1.mp4123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118# TODO
import functools
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from utils import flow_util
from models.base_blocks import LayerNorm2d, ADAINHourglass, FineEncoder, FineDecoder
# DNet
class DNet(nn.Module):
def __init__(self):
super(DNet, self).__init__()
self.mapping_net = MappingNet()
self.warpping_net = WarpingNet()
self.editing_net = EditingNet()
def forward(self, input_image, driving_source, stage=None):
if stage == 'warp':
descriptor = self.mapping_net(driving_source)
output = self.warpping_net(input_image, descriptor)
else:
descriptor = self.mapping_net(driving_source)
output = self.warpping_net(input_image, descriptor)
output['fake_image'] = self.editing_net(input_image, output['warp_image'], descriptor)
return output
class MappingNet(nn.Module):
def __init__(self, coeff_nc=73, descriptor_nc=256, layer=3):
super( MappingNet, self).__init__()
self.layer = layer
nonlinearity = nn.LeakyReLU(0.1)
self.first = nn.Sequential(
torch.nn.Conv1d(coeff_nc, descriptor_nc, kernel_size=7, padding=0, bias=True))
for i in range(layer):
net = nn.Sequential(nonlinearity,
torch.nn.Conv1d(descriptor_nc, descriptor_nc, kernel_size=3, padding=0, dilation=3))
setattr(self, 'encoder' + str(i), net)
self.pooling = nn.AdaptiveAvgPool1d(1)
self.output_nc = descriptor_nc
def forward(self, input_3dmm):
out = self.first(input_3dmm)
for i in range(self.layer):
model = getattr(self, 'encoder' + str(i))
out = model(out) + out[:,:,3:-3]
out = self.pooling(out)
return out
class WarpingNet(nn.Module):
def __init__(
self,
image_nc=3,
descriptor_nc=256,
base_nc=32,
max_nc=256,
encoder_layer=5,
decoder_layer=3,
use_spect=False
):
super( WarpingNet, self).__init__()
nonlinearity = nn.LeakyReLU(0.1)
norm_layer = functools.partial(LayerNorm2d, affine=True)
kwargs = {'nonlinearity':nonlinearity, 'use_spect':use_spect}
self.descriptor_nc = descriptor_nc
self.hourglass = ADAINHourglass(image_nc, self.descriptor_nc, base_nc,
max_nc, encoder_layer, decoder_layer, **kwargs)
self.flow_out = nn.Sequential(norm_layer(self.hourglass.output_nc),
nonlinearity,
nn.Conv2d(self.hourglass.output_nc, 2, kernel_size=7, stride=1, padding=3))
self.pool = nn.AdaptiveAvgPool2d(1)
def forward(self, input_image, descriptor):
final_output={}
output = self.hourglass(input_image, descriptor)
final_output['flow_field'] = self.flow_out(output)
deformation = flow_util.convert_flow_to_deformation(final_output['flow_field'])
final_output['warp_image'] = flow_util.warp_image(input_image, deformation)
return final_output
class EditingNet(nn.Module):
def __init__(
self,
image_nc=3,
descriptor_nc=256,
layer=3,
base_nc=64,
max_nc=256,
num_res_blocks=2,
use_spect=False):
super(EditingNet, self).__init__()
nonlinearity = nn.LeakyReLU(0.1)
norm_layer = functools.partial(LayerNorm2d, affine=True)
kwargs = {'norm_layer':norm_layer, 'nonlinearity':nonlinearity, 'use_spect':use_spect}
self.descriptor_nc = descriptor_nc
# encoder part
self.encoder = FineEncoder(image_nc*2, base_nc, max_nc, layer, **kwargs)
self.decoder = FineDecoder(image_nc, self.descriptor_nc, base_nc, max_nc, layer, num_res_blocks, **kwargs)
def forward(self, input_image, warp_image, descriptor):
x = torch.cat([input_image, warp_image], 1)
x = self.encoder(x)
gen_image = self.decoder(x, descriptor)
return gen_image123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import torch
import torch.nn as nn
import torch.nn.functional as F
from models.base_blocks import ResBlock, StyleConv, ToRGB
class ENet(nn.Module):
def __init__(
self,
num_style_feat=512,
lnet=None,
concat=False
):
super(ENet, self).__init__()
self.low_res = lnet
for param in self.low_res.parameters():
param.requires_grad = False
channel_multiplier, narrow = 2, 1
channels = {
'4': int(512 * narrow),
'8': int(512 * narrow),
'16': int(512 * narrow),
'32': int(512 * narrow),
'64': int(256 * channel_multiplier * narrow),
'128': int(128 * channel_multiplier * narrow),
'256': int(64 * channel_multiplier * narrow),
'512': int(32 * channel_multiplier * narrow),
'1024': int(16 * channel_multiplier * narrow)
}
self.log_size = 8
first_out_size = 128
self.conv_body_first = nn.Conv2d(3, channels[f'{first_out_size}'], 1) # 256 -> 128
# downsample
in_channels = channels[f'{first_out_size}']
self.conv_body_down = nn.ModuleList()
for i in range(8, 2, -1):
out_channels = channels[f'{2**(i - 1)}']
self.conv_body_down.append(ResBlock(in_channels, out_channels, mode='down'))
in_channels = out_channels
self.num_style_feat = num_style_feat
linear_out_channel = num_style_feat
self.final_linear = nn.Linear(channels['4'] * 4 * 4, linear_out_channel)
self.final_conv = nn.Conv2d(in_channels, channels['4'], 3, 1, 1)
self.style_convs = nn.ModuleList()
self.to_rgbs = nn.ModuleList()
self.noises = nn.Module()
self.concat = concat
if concat:
in_channels = 3 + 32 # channels['64']
else:
in_channels = 3
for i in range(7, 9): # 128, 256
out_channels = channels[f'{2**i}'] #
self.style_convs.append(
StyleConv(
in_channels,
out_channels,
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode='upsample'))
self.style_convs.append(
StyleConv(
out_channels,
out_channels,
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode=None))
self.to_rgbs.append(ToRGB(out_channels, num_style_feat, upsample=True))
in_channels = out_channels
def forward(self, audio_sequences, face_sequences, gt_sequences):
B = audio_sequences.size(0)
input_dim_size = len(face_sequences.size())
inp, ref = torch.split(face_sequences,3,dim=1)
if input_dim_size > 4:
audio_sequences = torch.cat([audio_sequences[:, i] for i in range(audio_sequences.size(1))], dim=0)
inp = torch.cat([inp[:, :, i] for i in range(inp.size(2))], dim=0)
ref = torch.cat([ref[:, :, i] for i in range(ref.size(2))], dim=0)
gt_sequences = torch.cat([gt_sequences[:, :, i] for i in range(gt_sequences.size(2))], dim=0)
# get the global style
feat = F.leaky_relu_(self.conv_body_first(F.interpolate(ref, size=(256,256), mode='bilinear')), negative_slope=0.2)
for i in range(self.log_size - 2):
feat = self.conv_body_down[i](feat)
feat = F.leaky_relu_(self.final_conv(feat), negative_slope=0.2)
# style code
style_code = self.final_linear(feat.reshape(feat.size(0), -1))
style_code = style_code.reshape(style_code.size(0), -1, self.num_style_feat)
LNet_input = torch.cat([inp, gt_sequences], dim=1)
LNet_input = F.interpolate(LNet_input, size=(96,96), mode='bilinear')
if self.concat:
low_res_img, low_res_feat = self.low_res(audio_sequences, LNet_input)
low_res_img.detach()
low_res_feat.detach()
out = torch.cat([low_res_img, low_res_feat], dim=1)
else:
low_res_img = self.low_res(audio_sequences, LNet_input)
low_res_img.detach()
# 96 x 96
out = low_res_img
p2d = (2,2,2,2)
out = F.pad(out, p2d, "reflect", 0)
skip = out
for conv1, conv2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], self.to_rgbs):
out = conv1(out, style_code) # 96, 192, 384
out = conv2(out, style_code)
skip = to_rgb(out, style_code, skip)
_outputs = skip
# remove padding
_outputs = _outputs[:,:,8:-8,8:-8]
if input_dim_size > 4:
_outputs = torch.split(_outputs, B, dim=0)
outputs = torch.stack(_outputs, dim=2)
low_res_img = F.interpolate(low_res_img, outputs.size()[3:])
low_res_img = torch.split(low_res_img, B, dim=0)
low_res_img = torch.stack(low_res_img, dim=2)
else:
outputs = _outputs
return outputs, low_res_img123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139import functools
import torch
import torch.nn as nn
from models.transformer import RETURNX, Transformer
from models.base_blocks import Conv2d, LayerNorm2d, FirstBlock2d, DownBlock2d, UpBlock2d, \
FFCADAINResBlocks, Jump, FinalBlock2d
class Visual_Encoder(nn.Module):
def __init__(self, image_nc, ngf, img_f, layers, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(Visual_Encoder, self).__init__()
self.layers = layers
self.first_inp = FirstBlock2d(image_nc, ngf, norm_layer, nonlinearity, use_spect)
self.first_ref = FirstBlock2d(image_nc, ngf, norm_layer, nonlinearity, use_spect)
for i in range(layers):
in_channels = min(ngf*(2**i), img_f)
out_channels = min(ngf*(2**(i+1)), img_f)
model_ref = DownBlock2d(in_channels, out_channels, norm_layer, nonlinearity, use_spect)
model_inp = DownBlock2d(in_channels, out_channels, norm_layer, nonlinearity, use_spect)
if i < 2:
ca_layer = RETURNX()
else:
ca_layer = Transformer(2**(i+1) * ngf,2,4,ngf,ngf*4)
setattr(self, 'ca' + str(i), ca_layer)
setattr(self, 'ref_down' + str(i), model_ref)
setattr(self, 'inp_down' + str(i), model_inp)
self.output_nc = out_channels * 2
def forward(self, maskGT, ref):
x_maskGT, x_ref = self.first_inp(maskGT), self.first_ref(ref)
out=[x_maskGT]
for i in range(self.layers):
model_ref = getattr(self, 'ref_down'+str(i))
model_inp = getattr(self, 'inp_down'+str(i))
ca_layer = getattr(self, 'ca'+str(i))
x_maskGT, x_ref = model_inp(x_maskGT), model_ref(x_ref)
x_maskGT = ca_layer(x_maskGT, x_ref)
if i < self.layers - 1:
out.append(x_maskGT)
else:
out.append(torch.cat([x_maskGT, x_ref], dim=1)) # concat ref features !
return out
class Decoder(nn.Module):
def __init__(self, image_nc, feature_nc, ngf, img_f, layers, num_block, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(Decoder, self).__init__()
self.layers = layers
for i in range(layers)[::-1]:
if i == layers-1:
in_channels = ngf*(2**(i+1)) * 2
else:
in_channels = min(ngf*(2**(i+1)), img_f)
out_channels = min(ngf*(2**i), img_f)
up = UpBlock2d(in_channels, out_channels, norm_layer, nonlinearity, use_spect)
res = FFCADAINResBlocks(num_block, in_channels, feature_nc, norm_layer, nonlinearity, use_spect)
jump = Jump(out_channels, norm_layer, nonlinearity, use_spect)
setattr(self, 'up' + str(i), up)
setattr(self, 'res' + str(i), res)
setattr(self, 'jump' + str(i), jump)
self.final = FinalBlock2d(out_channels, image_nc, use_spect, 'sigmoid')
self.output_nc = out_channels
def forward(self, x, z):
out = x.pop()
for i in range(self.layers)[::-1]:
res_model = getattr(self, 'res' + str(i))
up_model = getattr(self, 'up' + str(i))
jump_model = getattr(self, 'jump' + str(i))
out = res_model(out, z)
out = up_model(out)
out = jump_model(x.pop()) + out
out_image = self.final(out)
return out_image
class LNet(nn.Module):
def __init__(
self,
image_nc=3,
descriptor_nc=512,
layer=3,
base_nc=64,
max_nc=512,
num_res_blocks=9,
use_spect=True,
encoder=Visual_Encoder,
decoder=Decoder
):
super(LNet, self).__init__()
nonlinearity = nn.LeakyReLU(0.1)
norm_layer = functools.partial(LayerNorm2d, affine=True)
kwargs = {'norm_layer':norm_layer, 'nonlinearity':nonlinearity, 'use_spect':use_spect}
self.descriptor_nc = descriptor_nc
self.encoder = encoder(image_nc, base_nc, max_nc, layer, **kwargs)
self.decoder = decoder(image_nc, self.descriptor_nc, base_nc, max_nc, layer, num_res_blocks, **kwargs)
self.audio_encoder = nn.Sequential(
Conv2d(1, 32, kernel_size=3, stride=1, padding=1),
Conv2d(32, 32, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(32, 32, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(32, 64, kernel_size=3, stride=(3, 1), padding=1),
Conv2d(64, 64, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(64, 64, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(64, 128, kernel_size=3, stride=3, padding=1),
Conv2d(128, 128, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(128, 128, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(128, 256, kernel_size=3, stride=(3, 2), padding=1),
Conv2d(256, 256, kernel_size=3, stride=1, padding=1, residual=True),
Conv2d(256, 512, kernel_size=3, stride=1, padding=0),
Conv2d(512, descriptor_nc, kernel_size=1, stride=1, padding=0),
)
def forward(self, audio_sequences, face_sequences):
B = audio_sequences.size(0)
input_dim_size = len(face_sequences.size())
if input_dim_size > 4:
audio_sequences = torch.cat([audio_sequences[:, i] for i in range(audio_sequences.size(1))], dim=0)
face_sequences = torch.cat([face_sequences[:, :, i] for i in range(face_sequences.size(2))], dim=0)
cropped, ref = torch.split(face_sequences, 3, dim=1)
vis_feat = self.encoder(cropped, ref)
audio_feat = self.audio_encoder(audio_sequences)
_outputs = self.decoder(vis_feat, audio_feat)
if input_dim_size > 4:
_outputs = torch.split(_outputs, B, dim=0)
outputs = torch.stack(_outputs, dim=2)
else:
outputs = _outputs
return outputs12345678910111213141516171819202122232425262728293031323334353637import torch
from models.DNet import DNet
from models.LNet import LNet
from models.ENet import ENet
def _load(checkpoint_path):
map_location=None if torch.cuda.is_available() else torch.device('cpu')
checkpoint = torch.load(checkpoint_path, map_location=map_location)
return checkpoint
def load_checkpoint(path, model):
print("Load checkpoint from: {}".format(path))
checkpoint = _load(path)
s = checkpoint["state_dict"] if 'arcface' not in path else checkpoint
new_s = {}
for k, v in s.items():
if 'low_res' in k:
continue
else:
new_s[k.replace('module.', '')] = v
model.load_state_dict(new_s, strict=False)
return model
def load_network(args):
L_net = LNet()
L_net = load_checkpoint(args.LNet_path, L_net)
E_net = ENet(lnet=L_net)
model = load_checkpoint(args.ENet_path, E_net)
return model.eval()
def load_DNet(args):
D_Net = DNet()
print("Load checkpoint from: {}".format(args.DNet_path))
checkpoint = torch.load(args.DNet_path, map_location=lambda storage, loc: storage)
D_Net.load_state_dict(checkpoint['net_G_ema'], strict=False)
return D_Net.eval()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.batchnorm import BatchNorm2d
from torch.nn.utils.spectral_norm import spectral_norm as SpectralNorm
from models.ffc import FFC
from basicsr.archs.arch_util import default_init_weights
class Conv2d(nn.Module):
def __init__(self, cin, cout, kernel_size, stride, padding, residual=False, *args, **kwargs):
super().__init__(*args, **kwargs)
self.conv_block = nn.Sequential(
nn.Conv2d(cin, cout, kernel_size, stride, padding),
nn.BatchNorm2d(cout)
)
self.act = nn.ReLU()
self.residual = residual
def forward(self, x):
out = self.conv_block(x)
if self.residual:
out += x
return self.act(out)
class ResBlock(nn.Module):
def __init__(self, in_channels, out_channels, mode='down'):
super(ResBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels, 3, 1, 1)
self.conv2 = nn.Conv2d(in_channels, out_channels, 3, 1, 1)
self.skip = nn.Conv2d(in_channels, out_channels, 1, bias=False)
if mode == 'down':
self.scale_factor = 0.5
elif mode == 'up':
self.scale_factor = 2
def forward(self, x):
out = F.leaky_relu_(self.conv1(x), negative_slope=0.2)
# upsample/downsample
out = F.interpolate(out, scale_factor=self.scale_factor, mode='bilinear', align_corners=False)
out = F.leaky_relu_(self.conv2(out), negative_slope=0.2)
# skip
x = F.interpolate(x, scale_factor=self.scale_factor, mode='bilinear', align_corners=False)
skip = self.skip(x)
out = out + skip
return out
class LayerNorm2d(nn.Module):
def __init__(self, n_out, affine=True):
super(LayerNorm2d, self).__init__()
self.n_out = n_out
self.affine = affine
if self.affine:
self.weight = nn.Parameter(torch.ones(n_out, 1, 1))
self.bias = nn.Parameter(torch.zeros(n_out, 1, 1))
def forward(self, x):
normalized_shape = x.size()[1:]
if self.affine:
return F.layer_norm(x, normalized_shape, \
self.weight.expand(normalized_shape),
self.bias.expand(normalized_shape))
else:
return F.layer_norm(x, normalized_shape)
def spectral_norm(module, use_spect=True):
if use_spect:
return SpectralNorm(module)
else:
return module
class FirstBlock2d(nn.Module):
def __init__(self, input_nc, output_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FirstBlock2d, self).__init__()
kwargs = {'kernel_size': 7, 'stride': 1, 'padding': 3}
conv = spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs), use_spect)
if type(norm_layer) == type(None):
self.model = nn.Sequential(conv, nonlinearity)
else:
self.model = nn.Sequential(conv, norm_layer(output_nc), nonlinearity)
def forward(self, x):
out = self.model(x)
return out
class DownBlock2d(nn.Module):
def __init__(self, input_nc, output_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(DownBlock2d, self).__init__()
kwargs = {'kernel_size': 3, 'stride': 1, 'padding': 1}
conv = spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs), use_spect)
pool = nn.AvgPool2d(kernel_size=(2, 2))
if type(norm_layer) == type(None):
self.model = nn.Sequential(conv, nonlinearity, pool)
else:
self.model = nn.Sequential(conv, norm_layer(output_nc), nonlinearity, pool)
def forward(self, x):
out = self.model(x)
return out
class UpBlock2d(nn.Module):
def __init__(self, input_nc, output_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(UpBlock2d, self).__init__()
kwargs = {'kernel_size': 3, 'stride': 1, 'padding': 1}
conv = spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs), use_spect)
if type(norm_layer) == type(None):
self.model = nn.Sequential(conv, nonlinearity)
else:
self.model = nn.Sequential(conv, norm_layer(output_nc), nonlinearity)
def forward(self, x):
out = self.model(F.interpolate(x, scale_factor=2))
return out
class ADAIN(nn.Module):
def __init__(self, norm_nc, feature_nc):
super().__init__()
self.param_free_norm = nn.InstanceNorm2d(norm_nc, affine=False)
nhidden = 128
use_bias=True
self.mlp_shared = nn.Sequential(
nn.Linear(feature_nc, nhidden, bias=use_bias),
nn.ReLU()
)
self.mlp_gamma = nn.Linear(nhidden, norm_nc, bias=use_bias)
self.mlp_beta = nn.Linear(nhidden, norm_nc, bias=use_bias)
def forward(self, x, feature):
# Part 1. generate parameter-free normalized activations
normalized = self.param_free_norm(x)
# Part 2. produce scaling and bias conditioned on feature
feature = feature.view(feature.size(0), -1)
actv = self.mlp_shared(feature)
gamma = self.mlp_gamma(actv)
beta = self.mlp_beta(actv)
# apply scale and bias
gamma = gamma.view(*gamma.size()[:2], 1,1)
beta = beta.view(*beta.size()[:2], 1,1)
out = normalized * (1 + gamma) + beta
return out
class FineADAINResBlock2d(nn.Module):
"""
Define an Residual block for different types
"""
def __init__(self, input_nc, feature_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FineADAINResBlock2d, self).__init__()
kwargs = {'kernel_size': 3, 'stride': 1, 'padding': 1}
self.conv1 = spectral_norm(nn.Conv2d(input_nc, input_nc, **kwargs), use_spect)
self.conv2 = spectral_norm(nn.Conv2d(input_nc, input_nc, **kwargs), use_spect)
self.norm1 = ADAIN(input_nc, feature_nc)
self.norm2 = ADAIN(input_nc, feature_nc)
self.actvn = nonlinearity
def forward(self, x, z):
dx = self.actvn(self.norm1(self.conv1(x), z))
dx = self.norm2(self.conv2(x), z)
out = dx + x
return out
class FineADAINResBlocks(nn.Module):
def __init__(self, num_block, input_nc, feature_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FineADAINResBlocks, self).__init__()
self.num_block = num_block
for i in range(num_block):
model = FineADAINResBlock2d(input_nc, feature_nc, norm_layer, nonlinearity, use_spect)
setattr(self, 'res'+str(i), model)
def forward(self, x, z):
for i in range(self.num_block):
model = getattr(self, 'res'+str(i))
x = model(x, z)
return x
class ADAINEncoderBlock(nn.Module):
def __init__(self, input_nc, output_nc, feature_nc, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(ADAINEncoderBlock, self).__init__()
kwargs_down = {'kernel_size': 4, 'stride': 2, 'padding': 1}
kwargs_fine = {'kernel_size': 3, 'stride': 1, 'padding': 1}
self.conv_0 = spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs_down), use_spect)
self.conv_1 = spectral_norm(nn.Conv2d(output_nc, output_nc, **kwargs_fine), use_spect)
self.norm_0 = ADAIN(input_nc, feature_nc)
self.norm_1 = ADAIN(output_nc, feature_nc)
self.actvn = nonlinearity
def forward(self, x, z):
x = self.conv_0(self.actvn(self.norm_0(x, z)))
x = self.conv_1(self.actvn(self.norm_1(x, z)))
return x
class ADAINDecoderBlock(nn.Module):
def __init__(self, input_nc, output_nc, hidden_nc, feature_nc, use_transpose=True, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(ADAINDecoderBlock, self).__init__()
# Attributes
self.actvn = nonlinearity
hidden_nc = min(input_nc, output_nc) if hidden_nc is None else hidden_nc
kwargs_fine = {'kernel_size':3, 'stride':1, 'padding':1}
if use_transpose:
kwargs_up = {'kernel_size':3, 'stride':2, 'padding':1, 'output_padding':1}
else:
kwargs_up = {'kernel_size':3, 'stride':1, 'padding':1}
# create conv layers
self.conv_0 = spectral_norm(nn.Conv2d(input_nc, hidden_nc, **kwargs_fine), use_spect)
if use_transpose:
self.conv_1 = spectral_norm(nn.ConvTranspose2d(hidden_nc, output_nc, **kwargs_up), use_spect)
self.conv_s = spectral_norm(nn.ConvTranspose2d(input_nc, output_nc, **kwargs_up), use_spect)
else:
self.conv_1 = nn.Sequential(spectral_norm(nn.Conv2d(hidden_nc, output_nc, **kwargs_up), use_spect),
nn.Upsample(scale_factor=2))
self.conv_s = nn.Sequential(spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs_up), use_spect),
nn.Upsample(scale_factor=2))
# define normalization layers
self.norm_0 = ADAIN(input_nc, feature_nc)
self.norm_1 = ADAIN(hidden_nc, feature_nc)
self.norm_s = ADAIN(input_nc, feature_nc)
def forward(self, x, z):
x_s = self.shortcut(x, z)
dx = self.conv_0(self.actvn(self.norm_0(x, z)))
dx = self.conv_1(self.actvn(self.norm_1(dx, z)))
out = x_s + dx
return out
def shortcut(self, x, z):
x_s = self.conv_s(self.actvn(self.norm_s(x, z)))
return x_s
class FineEncoder(nn.Module):
"""docstring for Encoder"""
def __init__(self, image_nc, ngf, img_f, layers, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FineEncoder, self).__init__()
self.layers = layers
self.first = FirstBlock2d(image_nc, ngf, norm_layer, nonlinearity, use_spect)
for i in range(layers):
in_channels = min(ngf*(2**i), img_f)
out_channels = min(ngf*(2**(i+1)), img_f)
model = DownBlock2d(in_channels, out_channels, norm_layer, nonlinearity, use_spect)
setattr(self, 'down' + str(i), model)
self.output_nc = out_channels
def forward(self, x):
x = self.first(x)
out=[x]
for i in range(self.layers):
model = getattr(self, 'down'+str(i))
x = model(x)
out.append(x)
return out
class FineDecoder(nn.Module):
"""docstring for FineDecoder"""
def __init__(self, image_nc, feature_nc, ngf, img_f, layers, num_block, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FineDecoder, self).__init__()
self.layers = layers
for i in range(layers)[::-1]:
in_channels = min(ngf*(2**(i+1)), img_f)
out_channels = min(ngf*(2**i), img_f)
up = UpBlock2d(in_channels, out_channels, norm_layer, nonlinearity, use_spect)
res = FineADAINResBlocks(num_block, in_channels, feature_nc, norm_layer, nonlinearity, use_spect)
jump = Jump(out_channels, norm_layer, nonlinearity, use_spect)
setattr(self, 'up' + str(i), up)
setattr(self, 'res' + str(i), res)
setattr(self, 'jump' + str(i), jump)
self.final = FinalBlock2d(out_channels, image_nc, use_spect, 'tanh')
self.output_nc = out_channels
def forward(self, x, z):
out = x.pop()
for i in range(self.layers)[::-1]:
res_model = getattr(self, 'res' + str(i))
up_model = getattr(self, 'up' + str(i))
jump_model = getattr(self, 'jump' + str(i))
out = res_model(out, z)
out = up_model(out)
out = jump_model(x.pop()) + out
out_image = self.final(out)
return out_image
class ADAINEncoder(nn.Module):
def __init__(self, image_nc, pose_nc, ngf, img_f, layers, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(ADAINEncoder, self).__init__()
self.layers = layers
self.input_layer = nn.Conv2d(image_nc, ngf, kernel_size=7, stride=1, padding=3)
for i in range(layers):
in_channels = min(ngf * (2**i), img_f)
out_channels = min(ngf *(2**(i+1)), img_f)
model = ADAINEncoderBlock(in_channels, out_channels, pose_nc, nonlinearity, use_spect)
setattr(self, 'encoder' + str(i), model)
self.output_nc = out_channels
def forward(self, x, z):
out = self.input_layer(x)
out_list = [out]
for i in range(self.layers):
model = getattr(self, 'encoder' + str(i))
out = model(out, z)
out_list.append(out)
return out_list
class ADAINDecoder(nn.Module):
"""docstring for ADAINDecoder"""
def __init__(self, pose_nc, ngf, img_f, encoder_layers, decoder_layers, skip_connect=True,
nonlinearity=nn.LeakyReLU(), use_spect=False):
super(ADAINDecoder, self).__init__()
self.encoder_layers = encoder_layers
self.decoder_layers = decoder_layers
self.skip_connect = skip_connect
use_transpose = True
for i in range(encoder_layers-decoder_layers, encoder_layers)[::-1]:
in_channels = min(ngf * (2**(i+1)), img_f)
in_channels = in_channels*2 if i != (encoder_layers-1) and self.skip_connect else in_channels
out_channels = min(ngf * (2**i), img_f)
model = ADAINDecoderBlock(in_channels, out_channels, out_channels, pose_nc, use_transpose, nonlinearity, use_spect)
setattr(self, 'decoder' + str(i), model)
self.output_nc = out_channels*2 if self.skip_connect else out_channels
def forward(self, x, z):
out = x.pop() if self.skip_connect else x
for i in range(self.encoder_layers-self.decoder_layers, self.encoder_layers)[::-1]:
model = getattr(self, 'decoder' + str(i))
out = model(out, z)
out = torch.cat([out, x.pop()], 1) if self.skip_connect else out
return out
class ADAINHourglass(nn.Module):
def __init__(self, image_nc, pose_nc, ngf, img_f, encoder_layers, decoder_layers, nonlinearity, use_spect):
super(ADAINHourglass, self).__init__()
self.encoder = ADAINEncoder(image_nc, pose_nc, ngf, img_f, encoder_layers, nonlinearity, use_spect)
self.decoder = ADAINDecoder(pose_nc, ngf, img_f, encoder_layers, decoder_layers, True, nonlinearity, use_spect)
self.output_nc = self.decoder.output_nc
def forward(self, x, z):
return self.decoder(self.encoder(x, z), z)
class FineADAINLama(nn.Module):
def __init__(self, input_nc, feature_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FineADAINLama, self).__init__()
kwargs = {'kernel_size': 3, 'stride': 1, 'padding': 1}
self.actvn = nonlinearity
ratio_gin = 0.75
ratio_gout = 0.75
self.ffc = FFC(input_nc, input_nc, 3,
ratio_gin, ratio_gout, 1, 1, 1,
1, False, False, padding_type='reflect')
global_channels = int(input_nc * ratio_gout)
self.bn_l = ADAIN(input_nc - global_channels, feature_nc)
self.bn_g = ADAIN(global_channels, feature_nc)
def forward(self, x, z):
x_l, x_g = self.ffc(x)
x_l = self.actvn(self.bn_l(x_l,z))
x_g = self.actvn(self.bn_g(x_g,z))
return x_l, x_g
class FFCResnetBlock(nn.Module):
def __init__(self, dim, feature_dim, padding_type='reflect', norm_layer=BatchNorm2d, activation_layer=nn.ReLU, dilation=1,
spatial_transform_kwargs=None, inline=False, **conv_kwargs):
super().__init__()
self.conv1 = FineADAINLama(dim, feature_dim, **conv_kwargs)
self.conv2 = FineADAINLama(dim, feature_dim, **conv_kwargs)
self.inline = True
def forward(self, x, z):
if self.inline:
x_l, x_g = x[:, :-self.conv1.ffc.global_in_num], x[:, -self.conv1.ffc.global_in_num:]
else:
x_l, x_g = x if type(x) is tuple else (x, 0)
id_l, id_g = x_l, x_g
x_l, x_g = self.conv1((x_l, x_g), z)
x_l, x_g = self.conv2((x_l, x_g), z)
x_l, x_g = id_l + x_l, id_g + x_g
out = x_l, x_g
if self.inline:
out = torch.cat(out, dim=1)
return out
class FFCADAINResBlocks(nn.Module):
def __init__(self, num_block, input_nc, feature_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(FFCADAINResBlocks, self).__init__()
self.num_block = num_block
for i in range(num_block):
model = FFCResnetBlock(input_nc, feature_nc, norm_layer, nonlinearity, use_spect)
setattr(self, 'res'+str(i), model)
def forward(self, x, z):
for i in range(self.num_block):
model = getattr(self, 'res'+str(i))
x = model(x, z)
return x
class Jump(nn.Module):
def __init__(self, input_nc, norm_layer=nn.BatchNorm2d, nonlinearity=nn.LeakyReLU(), use_spect=False):
super(Jump, self).__init__()
kwargs = {'kernel_size': 3, 'stride': 1, 'padding': 1}
conv = spectral_norm(nn.Conv2d(input_nc, input_nc, **kwargs), use_spect)
if type(norm_layer) == type(None):
self.model = nn.Sequential(conv, nonlinearity)
else:
self.model = nn.Sequential(conv, norm_layer(input_nc), nonlinearity)
def forward(self, x):
out = self.model(x)
return out
class FinalBlock2d(nn.Module):
def __init__(self, input_nc, output_nc, use_spect=False, tanh_or_sigmoid='tanh'):
super(FinalBlock2d, self).__init__()
kwargs = {'kernel_size': 7, 'stride': 1, 'padding':3}
conv = spectral_norm(nn.Conv2d(input_nc, output_nc, **kwargs), use_spect)
if tanh_or_sigmoid == 'sigmoid':
out_nonlinearity = nn.Sigmoid()
else:
out_nonlinearity = nn.Tanh()
self.model = nn.Sequential(conv, out_nonlinearity)
def forward(self, x):
out = self.model(x)
return out
class ModulatedConv2d(nn.Module):
def __init__(self,
in_channels,
out_channels,
kernel_size,
num_style_feat,
demodulate=True,
sample_mode=None,
eps=1e-8):
super(ModulatedConv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.demodulate = demodulate
self.sample_mode = sample_mode
self.eps = eps
# modulation inside each modulated conv
self.modulation = nn.Linear(num_style_feat, in_channels, bias=True)
# initialization
default_init_weights(self.modulation, scale=1, bias_fill=1, a=0, mode='fan_in', nonlinearity='linear')
self.weight = nn.Parameter(
torch.randn(1, out_channels, in_channels, kernel_size, kernel_size) /
math.sqrt(in_channels * kernel_size**2))
self.padding = kernel_size // 2
def forward(self, x, style):
b, c, h, w = x.shape
style = self.modulation(style).view(b, 1, c, 1, 1)
weight = self.weight * style
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + self.eps)
weight = weight * demod.view(b, self.out_channels, 1, 1, 1)
weight = weight.view(b * self.out_channels, c, self.kernel_size, self.kernel_size)
# upsample or downsample if necessary
if self.sample_mode == 'upsample':
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
elif self.sample_mode == 'downsample':
x = F.interpolate(x, scale_factor=0.5, mode='bilinear', align_corners=False)
b, c, h, w = x.shape
x = x.view(1, b * c, h, w)
out = F.conv2d(x, weight, padding=self.padding, groups=b)
out = out.view(b, self.out_channels, *out.shape[2:4])
return out
def __repr__(self):
return (f'{self.__class__.__name__}(in_channels={self.in_channels}, out_channels={self.out_channels}, '
f'kernel_size={self.kernel_size}, demodulate={self.demodulate}, sample_mode={self.sample_mode})')
class StyleConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None):
super(StyleConv, self).__init__()
self.modulated_conv = ModulatedConv2d(
in_channels, out_channels, kernel_size, num_style_feat, demodulate=demodulate, sample_mode=sample_mode)
self.weight = nn.Parameter(torch.zeros(1)) # for noise injection
self.bias = nn.Parameter(torch.zeros(1, out_channels, 1, 1))
self.activate = nn.LeakyReLU(negative_slope=0.2, inplace=True)
def forward(self, x, style, noise=None):
# modulate
out = self.modulated_conv(x, style) * 2**0.5 # for conversion
# noise injection
if noise is None:
b, _, h, w = out.shape
noise = out.new_empty(b, 1, h, w).normal_()
out = out + self.weight * noise
# add bias
out = out + self.bias
# activation
out = self.activate(out)
return out
class ToRGB(nn.Module):
def __init__(self, in_channels, num_style_feat, upsample=True):
super(ToRGB, self).__init__()
self.upsample = upsample
self.modulated_conv = ModulatedConv2d(
in_channels, 3, kernel_size=1, num_style_feat=num_style_feat, demodulate=False, sample_mode=None)
self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))
def forward(self, x, style, skip=None):
out = self.modulated_conv(x, style)
out = out + self.bias
if skip is not None:
if self.upsample:
skip = F.interpolate(skip, scale_factor=2, mode='bilinear', align_corners=False)
out = out + skip
return out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233# Fast Fourier Convolution NeurIPS 2020
# original implementation https://github.com/pkumivision/FFC/blob/main/model_zoo/ffc.py
# paper https://proceedings.neurips.cc/paper/2020/file/2fd5d41ec6cfab47e32164d5624269b1-Paper.pdf
import torch
import torch.nn as nn
import torch.nn.functional as F
# from models.modules.squeeze_excitation import SELayer
import torch.fft
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction, bias=False),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel, bias=False),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
res = x * y.expand_as(x)
return res
class FFCSE_block(nn.Module):
def __init__(self, channels, ratio_g):
super(FFCSE_block, self).__init__()
in_cg = int(channels * ratio_g)
in_cl = channels - in_cg
r = 16
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.conv1 = nn.Conv2d(channels, channels // r,
kernel_size=1, bias=True)
self.relu1 = nn.ReLU(inplace=True)
self.conv_a2l = None if in_cl == 0 else nn.Conv2d(
channels // r, in_cl, kernel_size=1, bias=True)
self.conv_a2g = None if in_cg == 0 else nn.Conv2d(
channels // r, in_cg, kernel_size=1, bias=True)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = x if type(x) is tuple else (x, 0)
id_l, id_g = x
x = id_l if type(id_g) is int else torch.cat([id_l, id_g], dim=1)
x = self.avgpool(x)
x = self.relu1(self.conv1(x))
x_l = 0 if self.conv_a2l is None else id_l * \
self.sigmoid(self.conv_a2l(x))
x_g = 0 if self.conv_a2g is None else id_g * \
self.sigmoid(self.conv_a2g(x))
return x_l, x_g
class FourierUnit(nn.Module):
def __init__(self, in_channels, out_channels, groups=1, spatial_scale_factor=None, spatial_scale_mode='bilinear',
spectral_pos_encoding=False, use_se=False, se_kwargs=None, ffc3d=False, fft_norm='ortho'):
# bn_layer not used
super(FourierUnit, self).__init__()
self.groups = groups
self.conv_layer = torch.nn.Conv2d(in_channels=in_channels * 2 + (2 if spectral_pos_encoding else 0),
out_channels=out_channels * 2,
kernel_size=1, stride=1, padding=0, groups=self.groups, bias=False)
self.bn = torch.nn.BatchNorm2d(out_channels * 2)
self.relu = torch.nn.ReLU(inplace=True)
# squeeze and excitation block
self.use_se = use_se
if use_se:
if se_kwargs is None:
se_kwargs = {}
self.se = SELayer(self.conv_layer.in_channels, **se_kwargs)
self.spatial_scale_factor = spatial_scale_factor
self.spatial_scale_mode = spatial_scale_mode
self.spectral_pos_encoding = spectral_pos_encoding
self.ffc3d = ffc3d
self.fft_norm = fft_norm
def forward(self, x):
batch = x.shape[0]
if self.spatial_scale_factor is not None:
orig_size = x.shape[-2:]
x = F.interpolate(x, scale_factor=self.spatial_scale_factor, mode=self.spatial_scale_mode, align_corners=False)
r_size = x.size()
# (batch, c, h, w/2+1, 2)
fft_dim = (-3, -2, -1) if self.ffc3d else (-2, -1)
ffted = torch.fft.rfftn(x, dim=fft_dim, norm=self.fft_norm)
ffted = torch.stack((ffted.real, ffted.imag), dim=-1)
ffted = ffted.permute(0, 1, 4, 2, 3).contiguous() # (batch, c, 2, h, w/2+1)
ffted = ffted.view((batch, -1,) + ffted.size()[3:])
if self.spectral_pos_encoding:
height, width = ffted.shape[-2:]
coords_vert = torch.linspace(0, 1, height)[None, None, :, None].expand(batch, 1, height, width).to(ffted)
coords_hor = torch.linspace(0, 1, width)[None, None, None, :].expand(batch, 1, height, width).to(ffted)
ffted = torch.cat((coords_vert, coords_hor, ffted), dim=1)
if self.use_se:
ffted = self.se(ffted)
ffted = self.conv_layer(ffted) # (batch, c*2, h, w/2+1)
ffted = self.relu(self.bn(ffted))
ffted = ffted.view((batch, -1, 2,) + ffted.size()[2:]).permute(
0, 1, 3, 4, 2).contiguous() # (batch,c, t, h, w/2+1, 2)
ffted = torch.complex(ffted[..., 0], ffted[..., 1])
ifft_shape_slice = x.shape[-3:] if self.ffc3d else x.shape[-2:]
output = torch.fft.irfftn(ffted, s=ifft_shape_slice, dim=fft_dim, norm=self.fft_norm)
if self.spatial_scale_factor is not None:
output = F.interpolate(output, size=orig_size, mode=self.spatial_scale_mode, align_corners=False)
return output
class SpectralTransform(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, groups=1, enable_lfu=True, **fu_kwargs):
# bn_layer not used
super(SpectralTransform, self).__init__()
self.enable_lfu = enable_lfu
if stride == 2:
self.downsample = nn.AvgPool2d(kernel_size=(2, 2), stride=2)
else:
self.downsample = nn.Identity()
self.stride = stride
self.conv1 = nn.Sequential(
nn.Conv2d(in_channels, out_channels //
2, kernel_size=1, groups=groups, bias=False),
nn.BatchNorm2d(out_channels // 2),
nn.ReLU(inplace=True)
)
self.fu = FourierUnit(
out_channels // 2, out_channels // 2, groups, **fu_kwargs)
if self.enable_lfu:
self.lfu = FourierUnit(
out_channels // 2, out_channels // 2, groups)
self.conv2 = torch.nn.Conv2d(
out_channels // 2, out_channels, kernel_size=1, groups=groups, bias=False)
def forward(self, x):
x = self.downsample(x)
x = self.conv1(x)
output = self.fu(x)
if self.enable_lfu:
n, c, h, w = x.shape
split_no = 2
split_s = h // split_no
xs = torch.cat(torch.split(
x[:, :c // 4], split_s, dim=-2), dim=1).contiguous()
xs = torch.cat(torch.split(xs, split_s, dim=-1),
dim=1).contiguous()
xs = self.lfu(xs)
xs = xs.repeat(1, 1, split_no, split_no).contiguous()
else:
xs = 0
output = self.conv2(x + output + xs)
return output
class FFC(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size,
ratio_gin, ratio_gout, stride=1, padding=0,
dilation=1, groups=1, bias=False, enable_lfu=True,
padding_type='reflect', gated=False, **spectral_kwargs):
super(FFC, self).__init__()
assert stride == 1 or stride == 2, "Stride should be 1 or 2."
self.stride = stride
in_cg = int(in_channels * ratio_gin)
in_cl = in_channels - in_cg
out_cg = int(out_channels * ratio_gout)
out_cl = out_channels - out_cg
self.ratio_gin = ratio_gin
self.ratio_gout = ratio_gout
self.global_in_num = in_cg
module = nn.Identity if in_cl == 0 or out_cl == 0 else nn.Conv2d
self.convl2l = module(in_cl, out_cl, kernel_size,
stride, padding, dilation, groups, bias, padding_mode=padding_type)
module = nn.Identity if in_cl == 0 or out_cg == 0 else nn.Conv2d
self.convl2g = module(in_cl, out_cg, kernel_size,
stride, padding, dilation, groups, bias, padding_mode=padding_type)
module = nn.Identity if in_cg == 0 or out_cl == 0 else nn.Conv2d
self.convg2l = module(in_cg, out_cl, kernel_size,
stride, padding, dilation, groups, bias, padding_mode=padding_type)
module = nn.Identity if in_cg == 0 or out_cg == 0 else SpectralTransform
self.convg2g = module(
in_cg, out_cg, stride, 1 if groups == 1 else groups // 2, enable_lfu, **spectral_kwargs)
self.gated = gated
module = nn.Identity if in_cg == 0 or out_cl == 0 or not self.gated else nn.Conv2d
self.gate = module(in_channels, 2, 1)
def forward(self, x):
x_l, x_g = x if type(x) is tuple else (x, 0)
out_xl, out_xg = 0, 0
if self.gated:
total_input_parts = [x_l]
if torch.is_tensor(x_g):
total_input_parts.append(x_g)
total_input = torch.cat(total_input_parts, dim=1)
gates = torch.sigmoid(self.gate(total_input))
g2l_gate, l2g_gate = gates.chunk(2, dim=1)
else:
g2l_gate, l2g_gate = 1, 1
if self.ratio_gout != 1:
out_xl = self.convl2l(x_l) + self.convg2l(x_g) * g2l_gate
if self.ratio_gout != 0:
out_xg = self.convl2g(x_l) * l2g_gate + self.convg2g(x_g)
return out_xl, out_xg123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119import torch
from torch import nn
from einops import rearrange
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
class GELU(nn.Module):
def __init__(self):
super(GELU, self).__init__()
def forward(self, x):
return 0.5*x*(1+F.tanh(np.sqrt(2/np.pi)*(x+0.044715*torch.pow(x,3))))
# helpers
def pair(t):
return t if isinstance(t, tuple) else (t, t)
# classes
class PreNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.fn = fn
def forward(self, x, **kwargs):
return self.fn(self.norm(x), **kwargs)
class DualPreNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.normx = nn.LayerNorm(dim)
self.normy = nn.LayerNorm(dim)
self.fn = fn
def forward(self, x, y, **kwargs):
return self.fn(self.normx(x), self.normy(y), **kwargs)
class FeedForward(nn.Module):
def __init__(self, dim, hidden_dim, dropout = 0.):
super().__init__()
self.net = nn.Sequential(
nn.Linear(dim, hidden_dim),
GELU(),
nn.Dropout(dropout),
nn.Linear(hidden_dim, dim),
nn.Dropout(dropout)
)
def forward(self, x):
return self.net(x)
class Attention(nn.Module):
def __init__(self, dim, heads = 8, dim_head = 64, dropout = 0.):
super().__init__()
inner_dim = dim_head * heads
project_out = not (heads == 1 and dim_head == dim)
self.heads = heads
self.scale = dim_head ** -0.5
self.attend = nn.Softmax(dim = -1)
self.to_q = nn.Linear(dim, inner_dim, bias = False)
self.to_k = nn.Linear(dim, inner_dim, bias = False)
self.to_v = nn.Linear(dim, inner_dim, bias = False)
self.to_out = nn.Sequential(
nn.Linear(inner_dim, dim),
nn.Dropout(dropout)
) if project_out else nn.Identity()
def forward(self, x, y):
# qk = self.to_qk(x).chunk(2, dim = -1) #
q = rearrange(self.to_q(x), 'b n (h d) -> b h n d', h = self.heads) # q,k from the zero feature
k = rearrange(self.to_k(x), 'b n (h d) -> b h n d', h = self.heads) # v from the reference features
v = rearrange(self.to_v(y), 'b n (h d) -> b h n d', h = self.heads)
dots = torch.matmul(q, k.transpose(-1, -2)) * self.scale
attn = self.attend(dots)
out = torch.matmul(attn, v)
out = rearrange(out, 'b h n d -> b n (h d)')
return self.to_out(out)
class Transformer(nn.Module):
def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout = 0.):
super().__init__()
self.layers = nn.ModuleList([])
for _ in range(depth):
self.layers.append(nn.ModuleList([
DualPreNorm(dim, Attention(dim, heads = heads, dim_head = dim_head, dropout = dropout)),
PreNorm(dim, FeedForward(dim, mlp_dim, dropout = dropout))
]))
def forward(self, x, y): # x is the cropped, y is the foreign reference
bs,c,h,w = x.size()
# img to embedding
x = x.view(bs,c,-1).permute(0,2,1)
y = y.view(bs,c,-1).permute(0,2,1)
for attn, ff in self.layers:
x = attn(x, y) + x
x = ff(x) + x
x = x.view(bs,h,w,c).permute(0,3,1,2)
return x
class RETURNX(nn.Module):
def __init__(self,):
super().__init__()
def forward(self, x, y): # x is the cropped, y is the foreign reference
return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552# Prediction interface for Cog ⚙️
# https://github.com/replicate/cog/blob/main/docs/python.md
import os
import sys
import argparse
import subprocess
import numpy as np
from tqdm import tqdm
from PIL import Image
from scipy.io import loadmat
import torch
import cv2
from cog import BasePredictor, Input, Path
sys.path.insert(0, "third_part")
sys.path.insert(0, "third_part/GPEN")
sys.path.insert(0, "third_part/GFPGAN")
# 3dmm extraction
from third_part.face3d.util.preprocess import align_img
from third_part.face3d.util.load_mats import load_lm3d
from third_part.face3d.extract_kp_videos import KeypointExtractor
# face enhancement
from third_part.GPEN.gpen_face_enhancer import FaceEnhancement
from third_part.GFPGAN.gfpgan import GFPGANer
# expression control
from third_part.ganimation_replicate.model.ganimation import GANimationModel
from utils import audio
from utils.ffhq_preprocess import Croper
from utils.alignment_stit import crop_faces, calc_alignment_coefficients, paste_image
from utils.inference_utils import (
Laplacian_Pyramid_Blending_with_mask,
face_detect,
load_model,
options,
split_coeff,
trans_image,
transform_semantic,
find_crop_norm_ratio,
load_face3d_net,
exp_aus_dict,
)
class Predictor(BasePredictor):
def setup(self) -> None:
"""Load the model into memory to make running multiple predictions efficient"""
self.enhancer = FaceEnhancement(
base_dir="checkpoints",
size=512,
model="GPEN-BFR-512",
use_sr=False,
sr_model="rrdb_realesrnet_psnr",
channel_multiplier=2,
narrow=1,
device="cuda",
)
self.restorer = GFPGANer(
model_path="checkpoints/GFPGANv1.3.pth",
upscale=1,
arch="clean",
channel_multiplier=2,
bg_upsampler=None,
)
self.croper = Croper("checkpoints/shape_predictor_68_face_landmarks.dat")
self.kp_extractor = KeypointExtractor()
face3d_net_path = "checkpoints/face3d_pretrain_epoch_20.pth"
self.net_recon = load_face3d_net(face3d_net_path, "cuda")
self.lm3d_std = load_lm3d("checkpoints/BFM")
def predict(
self,
face: Path = Input(description="Input video file of a talking-head."),
input_audio: Path = Input(description="Input audio file."),
) -> Path:
"""Run a single prediction on the model"""
device = "cuda"
args = argparse.Namespace(
DNet_path="checkpoints/DNet.pt",
LNet_path="checkpoints/LNet.pth",
ENet_path="checkpoints/ENet.pth",
face3d_net_path="checkpoints/face3d_pretrain_epoch_20.pth",
face=str(face),
audio=str(input_audio),
exp_img="neutral",
outfile=None,
fps=25,
pads=[0, 20, 0, 0],
face_det_batch_size=4,
LNet_batch_size=16,
img_size=384,
crop=[0, -1, 0, -1],
box=[-1, -1, -1, -1],
nosmooth=False,
static=False,
up_face="original",
one_shot=False,
without_rl1=False,
tmp_dir="temp",
re_preprocess=False,
)
base_name = args.face.split("/")[-1]
if args.face.split(".")[1] in ["jpg", "png", "jpeg"]:
full_frames = [cv2.imread(args.face)]
args.static = True
fps = args.fps
else:
video_stream = cv2.VideoCapture(args.face)
fps = video_stream.get(cv2.CAP_PROP_FPS)
full_frames = []
while True:
still_reading, frame = video_stream.read()
if not still_reading:
video_stream.release()
break
y1, y2, x1, x2 = args.crop
if x2 == -1:
x2 = frame.shape[1]
if y2 == -1:
y2 = frame.shape[0]
frame = frame[y1:y2, x1:x2]
full_frames.append(frame)
full_frames_RGB = [
cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) for frame in full_frames
]
full_frames_RGB, crop, quad = self.croper.crop(full_frames_RGB, xsize=512)
clx, cly, crx, cry = crop
lx, ly, rx, ry = quad
lx, ly, rx, ry = int(lx), int(ly), int(rx), int(ry)
oy1, oy2, ox1, ox2 = (
cly + ly,
min(cly + ry, full_frames[0].shape[0]),
clx + lx,
min(clx + rx, full_frames[0].shape[1]),
)
# original_size = (ox2 - ox1, oy2 - oy1)
frames_pil = [
Image.fromarray(cv2.resize(frame, (256, 256))) for frame in full_frames_RGB
]
# get the landmark according to the detected face.
if (
not os.path.isfile("temp/" + base_name + "_landmarks.txt")
or args.re_preprocess
):
print("[Step 1] Landmarks Extraction in Video.")
lm = self.kp_extractor.extract_keypoint(
frames_pil, "./temp/" + base_name + "_landmarks.txt"
)
else:
print("[Step 1] Using saved landmarks.")
lm = np.loadtxt("temp/" + base_name + "_landmarks.txt").astype(np.float32)
lm = lm.reshape([len(full_frames), -1, 2])
if (
not os.path.isfile("temp/" + base_name + "_coeffs.npy")
or args.exp_img is not None
or args.re_preprocess
):
video_coeffs = []
for idx in tqdm(
range(len(frames_pil)), desc="[Step 2] 3DMM Extraction In Video:"
):
frame = frames_pil[idx]
W, H = frame.size
lm_idx = lm[idx].reshape([-1, 2])
if np.mean(lm_idx) == -1:
lm_idx = (self.lm3d_std[:, :2] + 1) / 2.0
lm_idx = np.concatenate([lm_idx[:, :1] * W, lm_idx[:, 1:2] * H], 1)
else:
lm_idx[:, -1] = H - 1 - lm_idx[:, -1]
trans_params, im_idx, lm_idx, _ = align_img(
frame, lm_idx, self.lm3d_std
)
trans_params = np.array(
[float(item) for item in np.hsplit(trans_params, 5)]
).astype(np.float32)
im_idx_tensor = (
torch.tensor(np.array(im_idx) / 255.0, dtype=torch.float32)
.permute(2, 0, 1)
.to(device)
.unsqueeze(0)
)
with torch.no_grad():
coeffs = split_coeff(self.net_recon(im_idx_tensor))
pred_coeff = {key: coeffs[key].cpu().numpy() for key in coeffs}
pred_coeff = np.concatenate(
[
pred_coeff["id"],
pred_coeff["exp"],
pred_coeff["tex"],
pred_coeff["angle"],
pred_coeff["gamma"],
pred_coeff["trans"],
trans_params[None],
],
1,
)
video_coeffs.append(pred_coeff)
semantic_npy = np.array(video_coeffs)[:, 0]
np.save("temp/" + base_name + "_coeffs.npy", semantic_npy)
else:
print("[Step 2] Using saved coeffs.")
semantic_npy = np.load("temp/" + base_name + "_coeffs.npy").astype(
np.float32
)
# generate the 3dmm coeff from a single image
if args.exp_img == "smile":
expression = torch.tensor(
loadmat("checkpoints/expression.mat")["expression_mouth"]
)[0]
else:
print("using expression center")
expression = torch.tensor(
loadmat("checkpoints/expression.mat")["expression_center"]
)[0]
# load DNet, model(LNet and ENet)
D_Net, model = load_model(args, device)
if (
not os.path.isfile("temp/" + base_name + "_stablized.npy")
or args.re_preprocess
):
imgs = []
for idx in tqdm(
range(len(frames_pil)),
desc="[Step 3] Stabilize the expression In Video:",
):
if args.one_shot:
source_img = trans_image(frames_pil[0]).unsqueeze(0).to(device)
semantic_source_numpy = semantic_npy[0:1]
else:
source_img = trans_image(frames_pil[idx]).unsqueeze(0).to(device)
semantic_source_numpy = semantic_npy[idx : idx + 1]
ratio = find_crop_norm_ratio(semantic_source_numpy, semantic_npy)
coeff = (
transform_semantic(semantic_npy, idx, ratio).unsqueeze(0).to(device)
)
# hacking the new expression
coeff[:, :64, :] = expression[None, :64, None].to(device)
with torch.no_grad():
output = D_Net(source_img, coeff)
img_stablized = np.uint8(
(
output["fake_image"]
.squeeze(0)
.permute(1, 2, 0)
.cpu()
.clamp_(-1, 1)
.numpy()
+ 1
)
/ 2.0
* 255
)
imgs.append(cv2.cvtColor(img_stablized, cv2.COLOR_RGB2BGR))
np.save("temp/" + base_name + "_stablized.npy", imgs)
del D_Net
else:
print("[Step 3] Using saved stabilized video.")
imgs = np.load("temp/" + base_name + "_stablized.npy")
torch.cuda.empty_cache()
if not args.audio.endswith(".wav"):
command = "ffmpeg -loglevel error -y -i {} -strict -2 {}".format(
args.audio, "temp/{}/temp.wav".format(args.tmp_dir)
)
subprocess.call(command, shell=True)
args.audio = "temp/{}/temp.wav".format(args.tmp_dir)
wav = audio.load_wav(args.audio, 16000)
mel = audio.melspectrogram(wav)
if np.isnan(mel.reshape(-1)).sum() > 0:
raise ValueError(
"Mel contains nan! Using a TTS voice? Add a small epsilon noise to the wav file and try again"
)
mel_step_size, mel_idx_multiplier, i, mel_chunks = 16, 80.0 / fps, 0, []
while True:
start_idx = int(i * mel_idx_multiplier)
if start_idx + mel_step_size > len(mel[0]):
mel_chunks.append(mel[:, len(mel[0]) - mel_step_size :])
break
mel_chunks.append(mel[:, start_idx : start_idx + mel_step_size])
i += 1
print("[Step 4] Load audio; Length of mel chunks: {}".format(len(mel_chunks)))
imgs = imgs[: len(mel_chunks)]
full_frames = full_frames[: len(mel_chunks)]
lm = lm[: len(mel_chunks)]
imgs_enhanced = []
for idx in tqdm(range(len(imgs)), desc="[Step 5] Reference Enhancement"):
img = imgs[idx]
pred, _, _ = self.enhancer.process(
img, img, face_enhance=True, possion_blending=False
)
imgs_enhanced.append(pred)
gen = datagen(
imgs_enhanced.copy(), mel_chunks, full_frames, args, (oy1, oy2, ox1, ox2)
)
frame_h, frame_w = full_frames[0].shape[:-1]
out = cv2.VideoWriter(
"temp/{}/result.mp4".format(args.tmp_dir),
cv2.VideoWriter_fourcc(*"mp4v"),
fps,
(frame_w, frame_h),
)
if args.up_face != "original":
instance = GANimationModel()
instance.initialize()
instance.setup()
# kp_extractor = KeypointExtractor()
for i, (
img_batch,
mel_batch,
frames,
coords,
img_original,
f_frames,
) in enumerate(
tqdm(
gen,
desc="[Step 6] Lip Synthesis:",
total=int(np.ceil(float(len(mel_chunks)) / args.LNet_batch_size)),
)
):
img_batch = torch.FloatTensor(np.transpose(img_batch, (0, 3, 1, 2))).to(
device
)
mel_batch = torch.FloatTensor(np.transpose(mel_batch, (0, 3, 1, 2))).to(
device
)
img_original = (
torch.FloatTensor(np.transpose(img_original, (0, 3, 1, 2))).to(device)
/ 255.0
) # BGR -> RGB
with torch.no_grad():
incomplete, reference = torch.split(img_batch, 3, dim=1)
pred, low_res = model(mel_batch, img_batch, reference)
pred = torch.clamp(pred, 0, 1)
if args.up_face in ["sad", "angry", "surprise"]:
tar_aus = exp_aus_dict[args.up_face]
else:
pass
if args.up_face == "original":
cur_gen_faces = img_original
else:
test_batch = {
"src_img": torch.nn.functional.interpolate(
(img_original * 2 - 1), size=(128, 128), mode="bilinear"
),
"tar_aus": tar_aus.repeat(len(incomplete), 1),
}
instance.feed_batch(test_batch)
instance.forward()
cur_gen_faces = torch.nn.functional.interpolate(
instance.fake_img / 2.0 + 0.5, size=(384, 384), mode="bilinear"
)
if args.without_rl1 is not False:
incomplete, reference = torch.split(img_batch, 3, dim=1)
mask = torch.where(
incomplete == 0,
torch.ones_like(incomplete),
torch.zeros_like(incomplete),
)
pred = pred * mask + cur_gen_faces * (1 - mask)
pred = pred.cpu().numpy().transpose(0, 2, 3, 1) * 255.0
torch.cuda.empty_cache()
for p, f, xf, c in zip(pred, frames, f_frames, coords):
y1, y2, x1, x2 = c
p = cv2.resize(p.astype(np.uint8), (x2 - x1, y2 - y1))
ff = xf.copy()
ff[y1:y2, x1:x2] = p
# month region enhancement by GFPGAN
cropped_faces, restored_faces, restored_img = self.restorer.enhance(
ff, has_aligned=False, only_center_face=True, paste_back=True
)
# 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
mm = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 0, 0, 0, 0, 0, 0]
mouse_mask = np.zeros_like(restored_img)
tmp_mask = self.enhancer.faceparser.process(
restored_img[y1:y2, x1:x2], mm
)[0]
mouse_mask[y1:y2, x1:x2] = (
cv2.resize(tmp_mask, (x2 - x1, y2 - y1))[:, :, np.newaxis] / 255.0
)
height, width = ff.shape[:2]
restored_img, ff, full_mask = [
cv2.resize(x, (512, 512))
for x in (restored_img, ff, np.float32(mouse_mask))
]
img = Laplacian_Pyramid_Blending_with_mask(
restored_img, ff, full_mask[:, :, 0], 10
)
pp = np.uint8(cv2.resize(np.clip(img, 0, 255), (width, height)))
pp, orig_faces, enhanced_faces = self.enhancer.process(
pp, xf, bbox=c, face_enhance=False, possion_blending=True
)
out.write(pp)
out.release()
output_file = "/tmp/output.mp4"
command = "ffmpeg -loglevel error -y -i {} -i {} -strict -2 -q:v 1 {}".format(
args.audio, "temp/{}/result.mp4".format(args.tmp_dir), output_file
)
subprocess.call(command, shell=True)
return Path(output_file)
# frames:256x256, full_frames: original size
def datagen(frames, mels, full_frames, args, cox):
img_batch, mel_batch, frame_batch, coords_batch, ref_batch, full_frame_batch = (
[],
[],
[],
[],
[],
[],
)
base_name = args.face.split("/")[-1]
refs = []
image_size = 256
# original frames
kp_extractor = KeypointExtractor()
fr_pil = [Image.fromarray(frame) for frame in frames]
lms = kp_extractor.extract_keypoint(
fr_pil, "temp/" + base_name + "x12_landmarks.txt"
)
frames_pil = [
(lm, frame) for frame, lm in zip(fr_pil, lms)
] # frames is the croped version of modified face
crops, orig_images, quads = crop_faces(
image_size, frames_pil, scale=1.0, use_fa=True
)
inverse_transforms = [
calc_alignment_coefficients(
quad + 0.5,
[[0, 0], [0, image_size], [image_size, image_size], [image_size, 0]],
)
for quad in quads
]
del kp_extractor.detector
oy1, oy2, ox1, ox2 = cox
face_det_results = face_detect(full_frames, args, jaw_correction=True)
for inverse_transform, crop, full_frame, face_det in zip(
inverse_transforms, crops, full_frames, face_det_results
):
imc_pil = paste_image(
inverse_transform,
crop,
Image.fromarray(
cv2.resize(
full_frame[int(oy1) : int(oy2), int(ox1) : int(ox2)], (256, 256)
)
),
)
ff = full_frame.copy()
ff[int(oy1) : int(oy2), int(ox1) : int(ox2)] = cv2.resize(
np.array(imc_pil.convert("RGB")), (ox2 - ox1, oy2 - oy1)
)
oface, coords = face_det
y1, y2, x1, x2 = coords
refs.append(ff[y1:y2, x1:x2])
for i, m in enumerate(mels):
idx = 0 if args.static else i % len(frames)
frame_to_save = frames[idx].copy()
face = refs[idx]
oface, coords = face_det_results[idx].copy()
face = cv2.resize(face, (args.img_size, args.img_size))
oface = cv2.resize(oface, (args.img_size, args.img_size))
img_batch.append(oface)
ref_batch.append(face)
mel_batch.append(m)
coords_batch.append(coords)
frame_batch.append(frame_to_save)
full_frame_batch.append(full_frames[idx].copy())
if len(img_batch) >= args.LNet_batch_size:
img_batch, mel_batch, ref_batch = (
np.asarray(img_batch),
np.asarray(mel_batch),
np.asarray(ref_batch),
)
img_masked = img_batch.copy()
img_original = img_batch.copy()
img_masked[:, args.img_size // 2 :] = 0
img_batch = np.concatenate((img_masked, ref_batch), axis=3) / 255.0
mel_batch = np.reshape(
mel_batch, [len(mel_batch), mel_batch.shape[1], mel_batch.shape[2], 1]
)
yield img_batch, mel_batch, frame_batch, coords_batch, img_original, full_frame_batch
(
img_batch,
mel_batch,
frame_batch,
coords_batch,
img_original,
full_frame_batch,
ref_batch,
) = ([], [], [], [], [], [], [])
if len(img_batch) > 0:
img_batch, mel_batch, ref_batch = (
np.asarray(img_batch),
np.asarray(mel_batch),
np.asarray(ref_batch),
)
img_masked = img_batch.copy()
img_original = img_batch.copy()
img_masked[:, args.img_size // 2 :] = 0
img_batch = np.concatenate((img_masked, ref_batch), axis=3) / 255.0
mel_batch = np.reshape(
mel_batch, [len(mel_batch), mel_batch.shape[1], mel_batch.shape[2], 1]
)
yield img_batch, mel_batch, frame_batch, coords_batch, img_original, full_frame_batch123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyMPin07iIA2oewCCP9ZTz6w",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
},
"accelerator": "GPU",
"gpuClass": "standard"
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/github/vinthony/video-retalking/blob/main/quick_demo.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "markdown",
"source": [
"## VideoReTalking:Audio-based Lip Synchronization for Talking Head Video Editing In the Wild\n",
"\n",
"[Arxiv](https://arxiv.org/abs/2211.14758) | [Project](https://vinthony.github.io/video-retalking/) | [Github](https://github.com/vinthony/video-retalking)\n",
"\n",
"Kun Cheng, Xiaodong Cun, Yong Zhang, Menghan Xia, Fei Yin, Mingrui Zhu, Xuan Wang, Jue Wang, Nannan Wang\n",
"\n",
"Xidian University, Tencent AI Lab, Tsinghua University\n",
"\n",
"*SIGGRAPH Asia 2022 Conferenence Track*\n",
"\n"
],
"metadata": {
"id": "NVfkv2BXSpr3"
}
},
{
"cell_type": "markdown",
"source": [
"**Installation** (30s)"
],
"metadata": {
"id": "u9hdPaH6UL_F"
}
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "PnKT9goiQ3Hk"
},
"outputs": [],
"source": [
"#@title\n",
"### make sure that CUDA is available in Edit -> Nootbook settings -> GPU\n",
"!nvidia-smi\n",
"\n",
"!python --version \n",
"!apt-get update\n",
"!apt install ffmpeg &> /dev/null \n",
"\n",
"print('Git clone project and install requirements...')\n",
"!git clone https://github.com/vinthony/video-retalking.git &> /dev/null\n",
"%cd video-retalking\n",
"# !pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html\n",
"!pip install -r requirements.txt"
]
},
{
"cell_type": "markdown",
"source": [
"**Download Pretrained Models**"
],
"metadata": {
"id": "uwJS0eaM61Cq"
}
},
{
"cell_type": "code",
"source": [
"#@title\n",
"print('Download pre-trained models...')\n",
"!mkdir ./checkpoints \n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/30_net_gen.pth -O ./checkpoints/30_net_gen.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/BFM.zip -O ./checkpoints/BFM.zip\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/DNet.pt -O ./checkpoints/DNet.pt\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/ENet.pth -O ./checkpoints/ENet.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/expression.mat -O ./checkpoints/expression.mat\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/face3d_pretrain_epoch_20.pth -O ./checkpoints/face3d_pretrain_epoch_20.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/GFPGANv1.3.pth -O ./checkpoints/GFPGANv1.3.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/GPEN-BFR-512.pth -O ./checkpoints/GPEN-BFR-512.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/LNet.pth -O ./checkpoints/LNet.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/ParseNet-latest.pth -O ./checkpoints/ParseNet-latest.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/RetinaFace-R50.pth -O ./checkpoints/RetinaFace-R50.pth\n",
"!wget https://github.com/vinthony/video-retalking/releases/download/v0.0.1/shape_predictor_68_face_landmarks.dat -O ./checkpoints/shape_predictor_68_face_landmarks.dat\n",
"!unzip -d ./checkpoints/BFM ./checkpoints/BFM.zip"
],
"metadata": {
"id": "x18qYuQY678E"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"**Inference**\n",
"\n",
"`--face`: Input video.\n",
"\n",
"`--audio`: Input audio. Both *.wav* and *.mp4* files are supported.\n",
"\n",
"You can choose our provided data from `./examples` folder or upload from your local computer.\n",
"\n",
"\n",
"\n",
"\n"
],
"metadata": {
"id": "QJRTF4U8UOjv"
}
},
{
"cell_type": "code",
"source": [
"#@title\n",
"import glob, os, sys\n",
"import ipywidgets as widgets\n",
"from IPython.display import HTML\n",
"from base64 import b64encode\n",
"\n",
"print(\"Choose the Video name to edit: (saved in folder 'examples/face')\")\n",
"vid_list = glob.glob1('examples/face/', '*.mp4')\n",
"vid_list.sort()\n",
"default_vid_name = widgets.Dropdown(options=vid_list, value='1.mp4')\n",
"display(default_vid_name)\n",
"\n",
"print(\"Choose the Audio name to edit: (saved in folder 'examples/audio')\")\n",
"audio_list = glob.glob1('examples/audio/', '*')\n",
"audio_list.sort()\n",
"default_audio_name = widgets.Dropdown(options=audio_list, value='1.wav')\n",
"display(default_audio_name)\n"
],
"metadata": {
"id": "U-IY-cBSporP",
"cellView": "form"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Visualize the input video and audio:"
],
"metadata": {
"id": "-MtI_R1bLJ-f"
}
},
{
"cell_type": "code",
"source": [
"#@title\n",
"input_video_name = './examples/face/{}'.format(default_vid_name.value)\n",
"input_video_mp4 = open('{}'.format(input_video_name),'rb').read()\n",
"input_video_data_url = \"data:video/x-m4v;base64,\" + b64encode(input_video_mp4).decode()\n",
"print('Display input video: {}'.format(input_video_name), file=sys.stderr)\n",
"display(HTML(\"\"\"\n",
" <video width=400 controls>\n",
" <source src=\"%s\" type=\"video/mp4\">\n",
" </video>\n",
" \"\"\" % input_video_data_url))\n",
"\n",
"input_audio_name = './examples/audio/{}'.format(default_audio_name.value)\n",
"input_audio_mp4 = open('{}'.format(input_audio_name),'rb').read()\n",
"input_audio_data_url = \"data:audio/wav;base64,\" + b64encode(input_audio_mp4).decode()\n",
"print('Display input audio: {}'.format(input_audio_name), file=sys.stderr)\n",
"display(HTML(\"\"\"\n",
" <audio width=400 controls>\n",
" <source src=\"%s\" type=\"audio/wav\">\n",
" </audio>\n",
" \"\"\" % input_audio_data_url))\n"
],
"metadata": {
"id": "ljbScdofJyGO",
"cellView": "form"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"input_video_path = 'examples/face/{}'.format(default_vid_name.value)\n",
"input_audio_path = 'examples/audio/{}'.format(default_audio_name.value)\n",
"\n",
"!python3 inference.py \\\n",
" --face {input_video_path} \\\n",
" --audio {input_audio_path} \\\n",
" --outfile results/output.mp4"
],
"metadata": {
"id": "D7hUwRCyUYEA"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Visualize the output video:"
],
"metadata": {
"id": "JB5RbKc-njkB"
}
},
{
"cell_type": "code",
"source": [
"#@title\n",
"# visualize code from makeittalk\n",
"from IPython.display import HTML\n",
"from base64 import b64encode\n",
"import os, sys, glob, cv2, subprocess, platform\n",
"\n",
"def read_video(vid_name):\n",
" video_stream = cv2.VideoCapture(vid_name)\n",
" fps = video_stream.get(cv2.CAP_PROP_FPS)\n",
" full_frames = []\n",
" while True:\n",
" still_reading, frame = video_stream.read()\n",
" if not still_reading:\n",
" video_stream.release()\n",
" break\n",
" full_frames.append(frame)\n",
" return full_frames, fps\n",
"\n",
"input_video_frames, fps = read_video(input_video_path)\n",
"output_video_frames, _ = read_video('./results/output.mp4')\n",
"\n",
"frame_h, frame_w = input_video_frames[0].shape[:-1]\n",
"out_concat = cv2.VideoWriter('./temp/temp/result_concat.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (frame_w*2, frame_h))\n",
"for i in range(len(output_video_frames)):\n",
" frame_input = input_video_frames[i % len(input_video_frames)]\n",
" frame_output = output_video_frames[i]\n",
" out_concat.write(cv2.hconcat([frame_input, frame_output]))\n",
"out_concat.release()\n",
"\n",
"command = 'ffmpeg -loglevel error -y -i {} -i {} -strict -2 -q:v 1 {}'.format(input_audio_path, './temp/temp/result_concat.mp4', './results/output_concat_input.mp4')\n",
"subprocess.call(command, shell=platform.system() != 'Windows')\n",
"\n",
"\n",
"output_video_name = './results/output.mp4'\n",
"output_video_mp4 = open('{}'.format(output_video_name),'rb').read()\n",
"output_video_data_url = \"data:video/mp4;base64,\" + b64encode(output_video_mp4).decode()\n",
"print('Display lip-syncing video: {}'.format(output_video_name), file=sys.stderr)\n",
"display(HTML(\"\"\"\n",
" <video height=400 controls>\n",
" <source src=\"%s\" type=\"video/mp4\">\n",
" </video>\n",
" \"\"\" % output_video_data_url))\n",
"\n",
"output_concat_video_name = './results/output_concat_input.mp4'\n",
"output_concat_video_mp4 = open('{}'.format(output_concat_video_name),'rb').read()\n",
"output_concat_video_data_url = \"data:video/mp4;base64,\" + b64encode(output_concat_video_mp4).decode()\n",
"print('Display input video and lip-syncing video: {}'.format(output_concat_video_name), file=sys.stderr)\n",
"display(HTML(\"\"\"\n",
" <video height=400 controls>\n",
" <source src=\"%s\" type=\"video/mp4\">\n",
" </video>\n",
" \"\"\" % output_concat_video_data_url))\n"
],
"metadata": {
"id": "ravs9UDucMfy",
"cellView": "form"
},
"execution_count": null,
"outputs": []
}
]
}12345678910basicsr==1.4.2
kornia==0.5.1
face-alignment==1.3.4
ninja==1.10.2.3
einops==0.4.1
facexlib==0.2.5
librosa==0.9.2
dlib==19.24.0
gradio>=3.7.0
numpy==1.23.4123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351Tencent is pleased to support the open source community by making GFPGAN available.
Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
GFPGAN is licensed under the Apache License Version 2.0 except for the third-party components listed below.
Terms of the Apache License Version 2.0:
---------------------------------------------
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
“License” shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
“Licensor” shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
“Legal Entity” shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, “control” means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
“You” (or “Your”) shall mean an individual or Legal Entity exercising permissions granted by this License.
“Source” form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
“Object” form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
“Work” shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
“Derivative Works” shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
“Contribution” shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, “submitted” means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as “Not a Contribution.”
“Contributor” shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of this License; and
You must cause any modified files to carry prominent notices stating that You changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
If the Work includes a “NOTICE” text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
Other dependencies and licenses:
Open Source Software licensed under the Apache 2.0 license and Other Licenses of the Third-Party Components therein:
---------------------------------------------
1. basicsr
Copyright 2018-2020 BasicSR Authors
This BasicSR project is released under the Apache 2.0 license.
A copy of Apache 2.0 is included in this file.
StyleGAN2
The codes are modified from the repository stylegan2-pytorch. Many thanks to the author - Kim Seonghyeon 😊 for translating from the official TensorFlow codes to PyTorch ones. Here is the license of stylegan2-pytorch.
The official repository is https://github.com/NVlabs/stylegan2, and here is the NVIDIA license.
DFDNet
The codes are largely modified from the repository DFDNet. Their license is Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Terms of the Nvidia License:
---------------------------------------------
1. Definitions
"Licensor" means any person or entity that distributes its Work.
"Software" means the original work of authorship made available under
this License.
"Work" means the Software and any additions to or derivative works of
the Software that are made available under this License.
"Nvidia Processors" means any central processing unit (CPU), graphics
processing unit (GPU), field-programmable gate array (FPGA),
application-specific integrated circuit (ASIC) or any combination
thereof designed, made, sold, or provided by Nvidia or its affiliates.
The terms "reproduce," "reproduction," "derivative works," and
"distribution" have the meaning as provided under U.S. copyright law;
provided, however, that for the purposes of this License, derivative
works shall not include works that remain separable from, or merely
link (or bind by name) to the interfaces of, the Work.
Works, including the Software, are "made available" under this License
by including in or with the Work either (a) a copyright notice
referencing the applicability of this License to the Work, or (b) a
copy of this License.
2. License Grants
2.1 Copyright Grant. Subject to the terms and conditions of this
License, each Licensor grants to you a perpetual, worldwide,
non-exclusive, royalty-free, copyright license to reproduce,
prepare derivative works of, publicly display, publicly perform,
sublicense and distribute its Work and any resulting derivative
works in any form.
3. Limitations
3.1 Redistribution. You may reproduce or distribute the Work only
if (a) you do so under this License, (b) you include a complete
copy of this License with your distribution, and (c) you retain
without modification any copyright, patent, trademark, or
attribution notices that are present in the Work.
3.2 Derivative Works. You may specify that additional or different
terms apply to the use, reproduction, and distribution of your
derivative works of the Work ("Your Terms") only if (a) Your Terms
provide that the use limitation in Section 3.3 applies to your
derivative works, and (b) you identify the specific derivative
works that are subject to Your Terms. Notwithstanding Your Terms,
this License (including the redistribution requirements in Section
3.1) will continue to apply to the Work itself.
3.3 Use Limitation. The Work and any derivative works thereof only
may be used or intended for use non-commercially. The Work or
derivative works thereof may be used or intended for use by Nvidia
or its affiliates commercially or non-commercially. As used herein,
"non-commercially" means for research or evaluation purposes only.
3.4 Patent Claims. If you bring or threaten to bring a patent claim
against any Licensor (including any claim, cross-claim or
counterclaim in a lawsuit) to enforce any patents that you allege
are infringed by any Work, then your rights under this License from
such Licensor (including the grants in Sections 2.1 and 2.2) will
terminate immediately.
3.5 Trademarks. This License does not grant any rights to use any
Licensor's or its affiliates' names, logos, or trademarks, except
as necessary to reproduce the notices described in this License.
3.6 Termination. If you violate any term of this License, then your
rights under this License (including the grants in Sections 2.1 and
2.2) will terminate immediately.
4. Disclaimer of Warranty.
THE WORK IS PROVIDED "AS IS" WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WARRANTIES OR CONDITIONS OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE OR
NON-INFRINGEMENT. YOU BEAR THE RISK OF UNDERTAKING ANY ACTIVITIES UNDER
THIS LICENSE.
5. Limitation of Liability.
EXCEPT AS PROHIBITED BY APPLICABLE LAW, IN NO EVENT AND UNDER NO LEGAL
THEORY, WHETHER IN TORT (INCLUDING NEGLIGENCE), CONTRACT, OR OTHERWISE
SHALL ANY LICENSOR BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY DIRECT,
INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
OR RELATED TO THIS LICENSE, THE USE OR INABILITY TO USE THE WORK
(INCLUDING BUT NOT LIMITED TO LOSS OF GOODWILL, BUSINESS INTERRUPTION,
LOST PROFITS OR DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY OTHER
COMMERCIAL DAMAGES OR LOSSES), EVEN IF THE LICENSOR HAS BEEN ADVISED OF
THE POSSIBILITY OF SUCH DAMAGES.
MIT License
Copyright (c) 2019 Kim Seonghyeon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Open Source Software licensed under the BSD 3-Clause license:
---------------------------------------------
1. torchvision
Copyright (c) Soumith Chintala 2016,
All rights reserved.
2. torch
Copyright (c) 2016- Facebook, Inc (Adam Paszke)
Copyright (c) 2014- Facebook, Inc (Soumith Chintala)
Copyright (c) 2011-2014 Idiap Research Institute (Ronan Collobert)
Copyright (c) 2012-2014 Deepmind Technologies (Koray Kavukcuoglu)
Copyright (c) 2011-2012 NEC Laboratories America (Koray Kavukcuoglu)
Copyright (c) 2011-2013 NYU (Clement Farabet)
Copyright (c) 2006-2010 NEC Laboratories America (Ronan Collobert, Leon Bottou, Iain Melvin, Jason Weston)
Copyright (c) 2006 Idiap Research Institute (Samy Bengio)
Copyright (c) 2001-2004 Idiap Research Institute (Ronan Collobert, Samy Bengio, Johnny Mariethoz)
Terms of the BSD 3-Clause License:
---------------------------------------------
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Open Source Software licensed under the BSD 3-Clause License and Other Licenses of the Third-Party Components therein:
---------------------------------------------
1. numpy
Copyright (c) 2005-2020, NumPy Developers.
All rights reserved.
A copy of BSD 3-Clause License is included in this file.
The NumPy repository and source distributions bundle several libraries that are
compatibly licensed. We list these here.
Name: Numpydoc
Files: doc/sphinxext/numpydoc/*
License: BSD-2-Clause
For details, see doc/sphinxext/LICENSE.txt
Name: scipy-sphinx-theme
Files: doc/scipy-sphinx-theme/*
License: BSD-3-Clause AND PSF-2.0 AND Apache-2.0
For details, see doc/scipy-sphinx-theme/LICENSE.txt
Name: lapack-lite
Files: numpy/linalg/lapack_lite/*
License: BSD-3-Clause
For details, see numpy/linalg/lapack_lite/LICENSE.txt
Name: tempita
Files: tools/npy_tempita/*
License: MIT
For details, see tools/npy_tempita/license.txt
Name: dragon4
Files: numpy/core/src/multiarray/dragon4.c
License: MIT
For license text, see numpy/core/src/multiarray/dragon4.c
Open Source Software licensed under the MIT license:
---------------------------------------------
1. facexlib
Copyright (c) 2020 Xintao Wang
2. opencv-python
Copyright (c) Olli-Pekka Heinisuo
Please note that only files in cv2 package are used.
Terms of the MIT License:
---------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Open Source Software licensed under the MIT license and Other Licenses of the Third-Party Components therein:
---------------------------------------------
1. tqdm
Copyright (c) 2013 noamraph
`tqdm` is a product of collaborative work.
Unless otherwise stated, all authors (see commit logs) retain copyright
for their respective work, and release the work under the MIT licence
(text below).
Exceptions or notable authors are listed below
in reverse chronological order:
* files: *
MPLv2.0 2015-2020 (c) Casper da Costa-Luis
[casperdcl](https://github.com/casperdcl).
* files: tqdm/_tqdm.py
MIT 2016 (c) [PR #96] on behalf of Google Inc.
* files: tqdm/_tqdm.py setup.py README.rst MANIFEST.in .gitignore
MIT 2013 (c) Noam Yorav-Raphael, original author.
[PR #96]: https://github.com/tqdm/tqdm/pull/96
Mozilla Public Licence (MPL) v. 2.0 - Exhibit A
-----------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file,
You can obtain one at https://mozilla.org/MPL/2.0/.
MIT License (MIT)
-----------------
Copyright (c) 2013 noamraph
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.12345678# flake8: noqa
from .archs import *
from .data import *
from .models import *
from .utils import *
# from .version import *12345678910import importlib
from basicsr.utils import scandir
from os import path as osp
# automatically scan and import arch modules for registry
# scan all the files that end with '_arch.py' under the archs folder
arch_folder = osp.dirname(osp.abspath(__file__))
arch_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(arch_folder) if v.endswith('_arch.py')]
# import all the arch modules
_arch_modules = [importlib.import_module(f'gfpgan.archs.{file_name}') for file_name in arch_filenames]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245import torch.nn as nn
from basicsr.utils.registry import ARCH_REGISTRY
def conv3x3(inplanes, outplanes, stride=1):
"""A simple wrapper for 3x3 convolution with padding.
Args:
inplanes (int): Channel number of inputs.
outplanes (int): Channel number of outputs.
stride (int): Stride in convolution. Default: 1.
"""
return nn.Conv2d(inplanes, outplanes, kernel_size=3, stride=stride, padding=1, bias=False)
class BasicBlock(nn.Module):
"""Basic residual block used in the ResNetArcFace architecture.
Args:
inplanes (int): Channel number of inputs.
planes (int): Channel number of outputs.
stride (int): Stride in convolution. Default: 1.
downsample (nn.Module): The downsample module. Default: None.
"""
expansion = 1 # output channel expansion ratio
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(BasicBlock, self).__init__()
self.conv1 = conv3x3(inplanes, planes, stride)
self.bn1 = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
self.conv2 = conv3x3(planes, planes)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class IRBlock(nn.Module):
"""Improved residual block (IR Block) used in the ResNetArcFace architecture.
Args:
inplanes (int): Channel number of inputs.
planes (int): Channel number of outputs.
stride (int): Stride in convolution. Default: 1.
downsample (nn.Module): The downsample module. Default: None.
use_se (bool): Whether use the SEBlock (squeeze and excitation block). Default: True.
"""
expansion = 1 # output channel expansion ratio
def __init__(self, inplanes, planes, stride=1, downsample=None, use_se=True):
super(IRBlock, self).__init__()
self.bn0 = nn.BatchNorm2d(inplanes)
self.conv1 = conv3x3(inplanes, inplanes)
self.bn1 = nn.BatchNorm2d(inplanes)
self.prelu = nn.PReLU()
self.conv2 = conv3x3(inplanes, planes, stride)
self.bn2 = nn.BatchNorm2d(planes)
self.downsample = downsample
self.stride = stride
self.use_se = use_se
if self.use_se:
self.se = SEBlock(planes)
def forward(self, x):
residual = x
out = self.bn0(x)
out = self.conv1(out)
out = self.bn1(out)
out = self.prelu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.use_se:
out = self.se(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.prelu(out)
return out
class Bottleneck(nn.Module):
"""Bottleneck block used in the ResNetArcFace architecture.
Args:
inplanes (int): Channel number of inputs.
planes (int): Channel number of outputs.
stride (int): Stride in convolution. Default: 1.
downsample (nn.Module): The downsample module. Default: None.
"""
expansion = 4 # output channel expansion ratio
def __init__(self, inplanes, planes, stride=1, downsample=None):
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, planes * self.expansion, kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
class SEBlock(nn.Module):
"""The squeeze-and-excitation block (SEBlock) used in the IRBlock.
Args:
channel (int): Channel number of inputs.
reduction (int): Channel reduction ration. Default: 16.
"""
def __init__(self, channel, reduction=16):
super(SEBlock, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1) # pool to 1x1 without spatial information
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction), nn.PReLU(), nn.Linear(channel // reduction, channel),
nn.Sigmoid())
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y
@ARCH_REGISTRY.register()
class ResNetArcFace(nn.Module):
"""ArcFace with ResNet architectures.
Ref: ArcFace: Additive Angular Margin Loss for Deep Face Recognition.
Args:
block (str): Block used in the ArcFace architecture.
layers (tuple(int)): Block numbers in each layer.
use_se (bool): Whether use the SEBlock (squeeze and excitation block). Default: True.
"""
def __init__(self, block, layers, use_se=True):
if block == 'IRBlock':
block = IRBlock
self.inplanes = 64
self.use_se = use_se
super(ResNetArcFace, self).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.prelu = nn.PReLU()
self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
self.bn4 = nn.BatchNorm2d(512)
self.dropout = nn.Dropout()
self.fc5 = nn.Linear(512 * 8 * 8, 512)
self.bn5 = nn.BatchNorm1d(512)
# initialization
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.xavier_normal_(m.weight)
elif isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.BatchNorm1d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
nn.init.constant_(m.bias, 0)
def _make_layer(self, block, planes, num_blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, use_se=self.use_se))
self.inplanes = planes
for _ in range(1, num_blocks):
layers.append(block(self.inplanes, planes, use_se=self.use_se))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.prelu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.bn4(x)
x = self.dropout(x)
x = x.view(x.size(0), -1)
x = self.fc5(x)
x = self.bn5(x)
return x123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312import math
import random
import torch
from basicsr.utils.registry import ARCH_REGISTRY
from torch import nn
from .gfpganv1_arch import ResUpBlock
from .stylegan2_bilinear_arch import (ConvLayer, EqualConv2d, EqualLinear, ResBlock, ScaledLeakyReLU,
StyleGAN2GeneratorBilinear)
class StyleGAN2GeneratorBilinearSFT(StyleGAN2GeneratorBilinear):
"""StyleGAN2 Generator with SFT modulation (Spatial Feature Transform).
It is the bilinear version. It does not use the complicated UpFirDnSmooth function that is not friendly for
deployment. It can be easily converted to the clean version: StyleGAN2GeneratorCSFT.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
num_mlp (int): Layer number of MLP style layers. Default: 8.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
lr_mlp (float): Learning rate multiplier for mlp layers. Default: 0.01.
narrow (float): The narrow ratio for channels. Default: 1.
sft_half (bool): Whether to apply SFT on half of the input channels. Default: False.
"""
def __init__(self,
out_size,
num_style_feat=512,
num_mlp=8,
channel_multiplier=2,
lr_mlp=0.01,
narrow=1,
sft_half=False):
super(StyleGAN2GeneratorBilinearSFT, self).__init__(
out_size,
num_style_feat=num_style_feat,
num_mlp=num_mlp,
channel_multiplier=channel_multiplier,
lr_mlp=lr_mlp,
narrow=narrow)
self.sft_half = sft_half
def forward(self,
styles,
conditions,
input_is_latent=False,
noise=None,
randomize_noise=True,
truncation=1,
truncation_latent=None,
inject_index=None,
return_latents=False):
"""Forward function for StyleGAN2GeneratorBilinearSFT.
Args:
styles (list[Tensor]): Sample codes of styles.
conditions (list[Tensor]): SFT conditions to generators.
input_is_latent (bool): Whether input is latent style. Default: False.
noise (Tensor | None): Input noise or None. Default: None.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
truncation (float): The truncation ratio. Default: 1.
truncation_latent (Tensor | None): The truncation latent tensor. Default: None.
inject_index (int | None): The injection index for mixing noise. Default: None.
return_latents (bool): Whether to return style latents. Default: False.
"""
# style codes -> latents with Style MLP layer
if not input_is_latent:
styles = [self.style_mlp(s) for s in styles]
# noises
if noise is None:
if randomize_noise:
noise = [None] * self.num_layers # for each style conv layer
else: # use the stored noise
noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
# style truncation
if truncation < 1:
style_truncation = []
for style in styles:
style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
styles = style_truncation
# get style latents with injection
if len(styles) == 1:
inject_index = self.num_latent
if styles[0].ndim < 3:
# repeat latent code for all the layers
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
else: # used for encoder with different latent code for each layer
latent = styles[0]
elif len(styles) == 2: # mixing noises
if inject_index is None:
inject_index = random.randint(1, self.num_latent - 1)
latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
latent = torch.cat([latent1, latent2], 1)
# main generation
out = self.constant_input(latent.shape[0])
out = self.style_conv1(out, latent[:, 0], noise=noise[0])
skip = self.to_rgb1(out, latent[:, 1])
i = 1
for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
noise[2::2], self.to_rgbs):
out = conv1(out, latent[:, i], noise=noise1)
# the conditions may have fewer levels
if i < len(conditions):
# SFT part to combine the conditions
if self.sft_half: # only apply SFT to half of the channels
out_same, out_sft = torch.split(out, int(out.size(1) // 2), dim=1)
out_sft = out_sft * conditions[i - 1] + conditions[i]
out = torch.cat([out_same, out_sft], dim=1)
else: # apply SFT to all the channels
out = out * conditions[i - 1] + conditions[i]
out = conv2(out, latent[:, i + 1], noise=noise2)
skip = to_rgb(out, latent[:, i + 2], skip) # feature back to the rgb space
i += 2
image = skip
if return_latents:
return image, latent
else:
return image, None
@ARCH_REGISTRY.register()
class GFPGANBilinear(nn.Module):
"""The GFPGAN architecture: Unet + StyleGAN2 decoder with SFT.
It is the bilinear version and it does not use the complicated UpFirDnSmooth function that is not friendly for
deployment. It can be easily converted to the clean version: GFPGANv1Clean.
Ref: GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
decoder_load_path (str): The path to the pre-trained decoder model (usually, the StyleGAN2). Default: None.
fix_decoder (bool): Whether to fix the decoder. Default: True.
num_mlp (int): Layer number of MLP style layers. Default: 8.
lr_mlp (float): Learning rate multiplier for mlp layers. Default: 0.01.
input_is_latent (bool): Whether input is latent style. Default: False.
different_w (bool): Whether to use different latent w for different layers. Default: False.
narrow (float): The narrow ratio for channels. Default: 1.
sft_half (bool): Whether to apply SFT on half of the input channels. Default: False.
"""
def __init__(
self,
out_size,
num_style_feat=512,
channel_multiplier=1,
decoder_load_path=None,
fix_decoder=True,
# for stylegan decoder
num_mlp=8,
lr_mlp=0.01,
input_is_latent=False,
different_w=False,
narrow=1,
sft_half=False):
super(GFPGANBilinear, self).__init__()
self.input_is_latent = input_is_latent
self.different_w = different_w
self.num_style_feat = num_style_feat
unet_narrow = narrow * 0.5 # by default, use a half of input channels
channels = {
'4': int(512 * unet_narrow),
'8': int(512 * unet_narrow),
'16': int(512 * unet_narrow),
'32': int(512 * unet_narrow),
'64': int(256 * channel_multiplier * unet_narrow),
'128': int(128 * channel_multiplier * unet_narrow),
'256': int(64 * channel_multiplier * unet_narrow),
'512': int(32 * channel_multiplier * unet_narrow),
'1024': int(16 * channel_multiplier * unet_narrow)
}
self.log_size = int(math.log(out_size, 2))
first_out_size = 2**(int(math.log(out_size, 2)))
self.conv_body_first = ConvLayer(3, channels[f'{first_out_size}'], 1, bias=True, activate=True)
# downsample
in_channels = channels[f'{first_out_size}']
self.conv_body_down = nn.ModuleList()
for i in range(self.log_size, 2, -1):
out_channels = channels[f'{2**(i - 1)}']
self.conv_body_down.append(ResBlock(in_channels, out_channels))
in_channels = out_channels
self.final_conv = ConvLayer(in_channels, channels['4'], 3, bias=True, activate=True)
# upsample
in_channels = channels['4']
self.conv_body_up = nn.ModuleList()
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
self.conv_body_up.append(ResUpBlock(in_channels, out_channels))
in_channels = out_channels
# to RGB
self.toRGB = nn.ModuleList()
for i in range(3, self.log_size + 1):
self.toRGB.append(EqualConv2d(channels[f'{2**i}'], 3, 1, stride=1, padding=0, bias=True, bias_init_val=0))
if different_w:
linear_out_channel = (int(math.log(out_size, 2)) * 2 - 2) * num_style_feat
else:
linear_out_channel = num_style_feat
self.final_linear = EqualLinear(
channels['4'] * 4 * 4, linear_out_channel, bias=True, bias_init_val=0, lr_mul=1, activation=None)
# the decoder: stylegan2 generator with SFT modulations
self.stylegan_decoder = StyleGAN2GeneratorBilinearSFT(
out_size=out_size,
num_style_feat=num_style_feat,
num_mlp=num_mlp,
channel_multiplier=channel_multiplier,
lr_mlp=lr_mlp,
narrow=narrow,
sft_half=sft_half)
# load pre-trained stylegan2 model if necessary
if decoder_load_path:
self.stylegan_decoder.load_state_dict(
torch.load(decoder_load_path, map_location=lambda storage, loc: storage)['params_ema'])
# fix decoder without updating params
if fix_decoder:
for _, param in self.stylegan_decoder.named_parameters():
param.requires_grad = False
# for SFT modulations (scale and shift)
self.condition_scale = nn.ModuleList()
self.condition_shift = nn.ModuleList()
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
if sft_half:
sft_out_channels = out_channels
else:
sft_out_channels = out_channels * 2
self.condition_scale.append(
nn.Sequential(
EqualConv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0),
ScaledLeakyReLU(0.2),
EqualConv2d(out_channels, sft_out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=1)))
self.condition_shift.append(
nn.Sequential(
EqualConv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0),
ScaledLeakyReLU(0.2),
EqualConv2d(out_channels, sft_out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0)))
def forward(self, x, return_latents=False, return_rgb=True, randomize_noise=True):
"""Forward function for GFPGANBilinear.
Args:
x (Tensor): Input images.
return_latents (bool): Whether to return style latents. Default: False.
return_rgb (bool): Whether return intermediate rgb images. Default: True.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
"""
conditions = []
unet_skips = []
out_rgbs = []
# encoder
feat = self.conv_body_first(x)
for i in range(self.log_size - 2):
feat = self.conv_body_down[i](feat)
unet_skips.insert(0, feat)
feat = self.final_conv(feat)
# style code
style_code = self.final_linear(feat.view(feat.size(0), -1))
if self.different_w:
style_code = style_code.view(style_code.size(0), -1, self.num_style_feat)
# decode
for i in range(self.log_size - 2):
# add unet skip
feat = feat + unet_skips[i]
# ResUpLayer
feat = self.conv_body_up[i](feat)
# generate scale and shift for SFT layers
scale = self.condition_scale[i](feat)
conditions.append(scale.clone())
shift = self.condition_shift[i](feat)
conditions.append(shift.clone())
# generate rgb images
if return_rgb:
out_rgbs.append(self.toRGB[i](feat))
# decoder
image, _ = self.stylegan_decoder([style_code],
conditions,
return_latents=return_latents,
input_is_latent=self.input_is_latent,
randomize_noise=randomize_noise)
return image, out_rgbs123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439import math
import random
import torch
from basicsr.archs.stylegan2_arch import (ConvLayer, EqualConv2d, EqualLinear, ResBlock, ScaledLeakyReLU,
StyleGAN2Generator)
from basicsr.ops.fused_act import FusedLeakyReLU
from basicsr.utils.registry import ARCH_REGISTRY
from torch import nn
from torch.nn import functional as F
class StyleGAN2GeneratorSFT(StyleGAN2Generator):
"""StyleGAN2 Generator with SFT modulation (Spatial Feature Transform).
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
num_mlp (int): Layer number of MLP style layers. Default: 8.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
resample_kernel (list[int]): A list indicating the 1D resample kernel magnitude. A cross production will be
applied to extent 1D resample kernel to 2D resample kernel. Default: (1, 3, 3, 1).
lr_mlp (float): Learning rate multiplier for mlp layers. Default: 0.01.
narrow (float): The narrow ratio for channels. Default: 1.
sft_half (bool): Whether to apply SFT on half of the input channels. Default: False.
"""
def __init__(self,
out_size,
num_style_feat=512,
num_mlp=8,
channel_multiplier=2,
resample_kernel=(1, 3, 3, 1),
lr_mlp=0.01,
narrow=1,
sft_half=False):
super(StyleGAN2GeneratorSFT, self).__init__(
out_size,
num_style_feat=num_style_feat,
num_mlp=num_mlp,
channel_multiplier=channel_multiplier,
resample_kernel=resample_kernel,
lr_mlp=lr_mlp,
narrow=narrow)
self.sft_half = sft_half
def forward(self,
styles,
conditions,
input_is_latent=False,
noise=None,
randomize_noise=True,
truncation=1,
truncation_latent=None,
inject_index=None,
return_latents=False):
"""Forward function for StyleGAN2GeneratorSFT.
Args:
styles (list[Tensor]): Sample codes of styles.
conditions (list[Tensor]): SFT conditions to generators.
input_is_latent (bool): Whether input is latent style. Default: False.
noise (Tensor | None): Input noise or None. Default: None.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
truncation (float): The truncation ratio. Default: 1.
truncation_latent (Tensor | None): The truncation latent tensor. Default: None.
inject_index (int | None): The injection index for mixing noise. Default: None.
return_latents (bool): Whether to return style latents. Default: False.
"""
# style codes -> latents with Style MLP layer
if not input_is_latent:
styles = [self.style_mlp(s) for s in styles]
# noises
if noise is None:
if randomize_noise:
noise = [None] * self.num_layers # for each style conv layer
else: # use the stored noise
noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
# style truncation
if truncation < 1:
style_truncation = []
for style in styles:
style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
styles = style_truncation
# get style latents with injection
if len(styles) == 1:
inject_index = self.num_latent
if styles[0].ndim < 3:
# repeat latent code for all the layers
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
else: # used for encoder with different latent code for each layer
latent = styles[0]
elif len(styles) == 2: # mixing noises
if inject_index is None:
inject_index = random.randint(1, self.num_latent - 1)
latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
latent = torch.cat([latent1, latent2], 1)
# main generation
out = self.constant_input(latent.shape[0])
out = self.style_conv1(out, latent[:, 0], noise=noise[0])
skip = self.to_rgb1(out, latent[:, 1])
i = 1
for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
noise[2::2], self.to_rgbs):
out = conv1(out, latent[:, i], noise=noise1)
# the conditions may have fewer levels
if i < len(conditions):
# SFT part to combine the conditions
if self.sft_half: # only apply SFT to half of the channels
out_same, out_sft = torch.split(out, int(out.size(1) // 2), dim=1)
out_sft = out_sft * conditions[i - 1] + conditions[i]
out = torch.cat([out_same, out_sft], dim=1)
else: # apply SFT to all the channels
out = out * conditions[i - 1] + conditions[i]
out = conv2(out, latent[:, i + 1], noise=noise2)
skip = to_rgb(out, latent[:, i + 2], skip) # feature back to the rgb space
i += 2
image = skip
if return_latents:
return image, latent
else:
return image, None
class ConvUpLayer(nn.Module):
"""Convolutional upsampling layer. It uses bilinear upsampler + Conv.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Size of the convolving kernel.
stride (int): Stride of the convolution. Default: 1
padding (int): Zero-padding added to both sides of the input. Default: 0.
bias (bool): If ``True``, adds a learnable bias to the output. Default: ``True``.
bias_init_val (float): Bias initialized value. Default: 0.
activate (bool): Whether use activateion. Default: True.
"""
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
bias=True,
bias_init_val=0,
activate=True):
super(ConvUpLayer, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
# self.scale is used to scale the convolution weights, which is related to the common initializations.
self.scale = 1 / math.sqrt(in_channels * kernel_size**2)
self.weight = nn.Parameter(torch.randn(out_channels, in_channels, kernel_size, kernel_size))
if bias and not activate:
self.bias = nn.Parameter(torch.zeros(out_channels).fill_(bias_init_val))
else:
self.register_parameter('bias', None)
# activation
if activate:
if bias:
self.activation = FusedLeakyReLU(out_channels)
else:
self.activation = ScaledLeakyReLU(0.2)
else:
self.activation = None
def forward(self, x):
# bilinear upsample
out = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
# conv
out = F.conv2d(
out,
self.weight * self.scale,
bias=self.bias,
stride=self.stride,
padding=self.padding,
)
# activation
if self.activation is not None:
out = self.activation(out)
return out
class ResUpBlock(nn.Module):
"""Residual block with upsampling.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
"""
def __init__(self, in_channels, out_channels):
super(ResUpBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, in_channels, 3, bias=True, activate=True)
self.conv2 = ConvUpLayer(in_channels, out_channels, 3, stride=1, padding=1, bias=True, activate=True)
self.skip = ConvUpLayer(in_channels, out_channels, 1, bias=False, activate=False)
def forward(self, x):
out = self.conv1(x)
out = self.conv2(out)
skip = self.skip(x)
out = (out + skip) / math.sqrt(2)
return out
@ARCH_REGISTRY.register()
class GFPGANv1(nn.Module):
"""The GFPGAN architecture: Unet + StyleGAN2 decoder with SFT.
Ref: GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
resample_kernel (list[int]): A list indicating the 1D resample kernel magnitude. A cross production will be
applied to extent 1D resample kernel to 2D resample kernel. Default: (1, 3, 3, 1).
decoder_load_path (str): The path to the pre-trained decoder model (usually, the StyleGAN2). Default: None.
fix_decoder (bool): Whether to fix the decoder. Default: True.
num_mlp (int): Layer number of MLP style layers. Default: 8.
lr_mlp (float): Learning rate multiplier for mlp layers. Default: 0.01.
input_is_latent (bool): Whether input is latent style. Default: False.
different_w (bool): Whether to use different latent w for different layers. Default: False.
narrow (float): The narrow ratio for channels. Default: 1.
sft_half (bool): Whether to apply SFT on half of the input channels. Default: False.
"""
def __init__(
self,
out_size,
num_style_feat=512,
channel_multiplier=1,
resample_kernel=(1, 3, 3, 1),
decoder_load_path=None,
fix_decoder=True,
# for stylegan decoder
num_mlp=8,
lr_mlp=0.01,
input_is_latent=False,
different_w=False,
narrow=1,
sft_half=False):
super(GFPGANv1, self).__init__()
self.input_is_latent = input_is_latent
self.different_w = different_w
self.num_style_feat = num_style_feat
unet_narrow = narrow * 0.5 # by default, use a half of input channels
channels = {
'4': int(512 * unet_narrow),
'8': int(512 * unet_narrow),
'16': int(512 * unet_narrow),
'32': int(512 * unet_narrow),
'64': int(256 * channel_multiplier * unet_narrow),
'128': int(128 * channel_multiplier * unet_narrow),
'256': int(64 * channel_multiplier * unet_narrow),
'512': int(32 * channel_multiplier * unet_narrow),
'1024': int(16 * channel_multiplier * unet_narrow)
}
self.log_size = int(math.log(out_size, 2))
first_out_size = 2**(int(math.log(out_size, 2)))
self.conv_body_first = ConvLayer(3, channels[f'{first_out_size}'], 1, bias=True, activate=True)
# downsample
in_channels = channels[f'{first_out_size}']
self.conv_body_down = nn.ModuleList()
for i in range(self.log_size, 2, -1):
out_channels = channels[f'{2**(i - 1)}']
self.conv_body_down.append(ResBlock(in_channels, out_channels, resample_kernel))
in_channels = out_channels
self.final_conv = ConvLayer(in_channels, channels['4'], 3, bias=True, activate=True)
# upsample
in_channels = channels['4']
self.conv_body_up = nn.ModuleList()
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
self.conv_body_up.append(ResUpBlock(in_channels, out_channels))
in_channels = out_channels
# to RGB
self.toRGB = nn.ModuleList()
for i in range(3, self.log_size + 1):
self.toRGB.append(EqualConv2d(channels[f'{2**i}'], 3, 1, stride=1, padding=0, bias=True, bias_init_val=0))
if different_w:
linear_out_channel = (int(math.log(out_size, 2)) * 2 - 2) * num_style_feat
else:
linear_out_channel = num_style_feat
self.final_linear = EqualLinear(
channels['4'] * 4 * 4, linear_out_channel, bias=True, bias_init_val=0, lr_mul=1, activation=None)
# the decoder: stylegan2 generator with SFT modulations
self.stylegan_decoder = StyleGAN2GeneratorSFT(
out_size=out_size,
num_style_feat=num_style_feat,
num_mlp=num_mlp,
channel_multiplier=channel_multiplier,
resample_kernel=resample_kernel,
lr_mlp=lr_mlp,
narrow=narrow,
sft_half=sft_half)
# load pre-trained stylegan2 model if necessary
if decoder_load_path:
self.stylegan_decoder.load_state_dict(
torch.load(decoder_load_path, map_location=lambda storage, loc: storage)['params_ema'])
# fix decoder without updating params
if fix_decoder:
for _, param in self.stylegan_decoder.named_parameters():
param.requires_grad = False
# for SFT modulations (scale and shift)
self.condition_scale = nn.ModuleList()
self.condition_shift = nn.ModuleList()
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
if sft_half:
sft_out_channels = out_channels
else:
sft_out_channels = out_channels * 2
self.condition_scale.append(
nn.Sequential(
EqualConv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0),
ScaledLeakyReLU(0.2),
EqualConv2d(out_channels, sft_out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=1)))
self.condition_shift.append(
nn.Sequential(
EqualConv2d(out_channels, out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0),
ScaledLeakyReLU(0.2),
EqualConv2d(out_channels, sft_out_channels, 3, stride=1, padding=1, bias=True, bias_init_val=0)))
def forward(self, x, return_latents=False, return_rgb=True, randomize_noise=True):
"""Forward function for GFPGANv1.
Args:
x (Tensor): Input images.
return_latents (bool): Whether to return style latents. Default: False.
return_rgb (bool): Whether return intermediate rgb images. Default: True.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
"""
conditions = []
unet_skips = []
out_rgbs = []
# encoder
feat = self.conv_body_first(x)
for i in range(self.log_size - 2):
feat = self.conv_body_down[i](feat)
unet_skips.insert(0, feat)
feat = self.final_conv(feat)
# style code
style_code = self.final_linear(feat.view(feat.size(0), -1))
if self.different_w:
style_code = style_code.view(style_code.size(0), -1, self.num_style_feat)
# decode
for i in range(self.log_size - 2):
# add unet skip
feat = feat + unet_skips[i]
# ResUpLayer
feat = self.conv_body_up[i](feat)
# generate scale and shift for SFT layers
scale = self.condition_scale[i](feat)
conditions.append(scale.clone())
shift = self.condition_shift[i](feat)
conditions.append(shift.clone())
# generate rgb images
if return_rgb:
out_rgbs.append(self.toRGB[i](feat))
# decoder
image, _ = self.stylegan_decoder([style_code],
conditions,
return_latents=return_latents,
input_is_latent=self.input_is_latent,
randomize_noise=randomize_noise)
return image, out_rgbs
@ARCH_REGISTRY.register()
class FacialComponentDiscriminator(nn.Module):
"""Facial component (eyes, mouth, noise) discriminator used in GFPGAN.
"""
def __init__(self):
super(FacialComponentDiscriminator, self).__init__()
# It now uses a VGG-style architectrue with fixed model size
self.conv1 = ConvLayer(3, 64, 3, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
self.conv2 = ConvLayer(64, 128, 3, downsample=True, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
self.conv3 = ConvLayer(128, 128, 3, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
self.conv4 = ConvLayer(128, 256, 3, downsample=True, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
self.conv5 = ConvLayer(256, 256, 3, downsample=False, resample_kernel=(1, 3, 3, 1), bias=True, activate=True)
self.final_conv = ConvLayer(256, 1, 3, bias=True, activate=False)
def forward(self, x, return_feats=False):
"""Forward function for FacialComponentDiscriminator.
Args:
x (Tensor): Input images.
return_feats (bool): Whether to return intermediate features. Default: False.
"""
feat = self.conv1(x)
feat = self.conv3(self.conv2(feat))
rlt_feats = []
if return_feats:
rlt_feats.append(feat.clone())
feat = self.conv5(self.conv4(feat))
if return_feats:
rlt_feats.append(feat.clone())
out = self.final_conv(feat)
if return_feats:
return out, rlt_feats
else:
return out, None123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324import math
import random
import torch
from basicsr.utils.registry import ARCH_REGISTRY
from torch import nn
from torch.nn import functional as F
from .stylegan2_clean_arch import StyleGAN2GeneratorClean
class StyleGAN2GeneratorCSFT(StyleGAN2GeneratorClean):
"""StyleGAN2 Generator with SFT modulation (Spatial Feature Transform).
It is the clean version without custom compiled CUDA extensions used in StyleGAN2.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
num_mlp (int): Layer number of MLP style layers. Default: 8.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
narrow (float): The narrow ratio for channels. Default: 1.
sft_half (bool): Whether to apply SFT on half of the input channels. Default: False.
"""
def __init__(self, out_size, num_style_feat=512, num_mlp=8, channel_multiplier=2, narrow=1, sft_half=False):
super(StyleGAN2GeneratorCSFT, self).__init__(
out_size,
num_style_feat=num_style_feat,
num_mlp=num_mlp,
channel_multiplier=channel_multiplier,
narrow=narrow)
self.sft_half = sft_half
def forward(self,
styles,
conditions,
input_is_latent=False,
noise=None,
randomize_noise=True,
truncation=1,
truncation_latent=None,
inject_index=None,
return_latents=False):
"""Forward function for StyleGAN2GeneratorCSFT.
Args:
styles (list[Tensor]): Sample codes of styles.
conditions (list[Tensor]): SFT conditions to generators.
input_is_latent (bool): Whether input is latent style. Default: False.
noise (Tensor | None): Input noise or None. Default: None.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
truncation (float): The truncation ratio. Default: 1.
truncation_latent (Tensor | None): The truncation latent tensor. Default: None.
inject_index (int | None): The injection index for mixing noise. Default: None.
return_latents (bool): Whether to return style latents. Default: False.
"""
# style codes -> latents with Style MLP layer
if not input_is_latent:
styles = [self.style_mlp(s) for s in styles]
# noises
if noise is None:
if randomize_noise:
noise = [None] * self.num_layers # for each style conv layer
else: # use the stored noise
noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
# style truncation
if truncation < 1:
style_truncation = []
for style in styles:
style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
styles = style_truncation
# get style latents with injection
if len(styles) == 1:
inject_index = self.num_latent
if styles[0].ndim < 3:
# repeat latent code for all the layers
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
else: # used for encoder with different latent code for each layer
latent = styles[0]
elif len(styles) == 2: # mixing noises
if inject_index is None:
inject_index = random.randint(1, self.num_latent - 1)
latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
latent = torch.cat([latent1, latent2], 1)
# main generation
out = self.constant_input(latent.shape[0])
out = self.style_conv1(out, latent[:, 0], noise=noise[0])
skip = self.to_rgb1(out, latent[:, 1])
i = 1
for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
noise[2::2], self.to_rgbs):
out = conv1(out, latent[:, i], noise=noise1)
# the conditions may have fewer levels
if i < len(conditions):
# SFT part to combine the conditions
if self.sft_half: # only apply SFT to half of the channels
out_same, out_sft = torch.split(out, int(out.size(1) // 2), dim=1)
out_sft = out_sft * conditions[i - 1] + conditions[i]
out = torch.cat([out_same, out_sft], dim=1)
else: # apply SFT to all the channels
out = out * conditions[i - 1] + conditions[i]
out = conv2(out, latent[:, i + 1], noise=noise2)
skip = to_rgb(out, latent[:, i + 2], skip) # feature back to the rgb space
i += 2
image = skip
if return_latents:
return image, latent
else:
return image, None
class ResBlock(nn.Module):
"""Residual block with bilinear upsampling/downsampling.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
mode (str): Upsampling/downsampling mode. Options: down | up. Default: down.
"""
def __init__(self, in_channels, out_channels, mode='down'):
super(ResBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels, 3, 1, 1)
self.conv2 = nn.Conv2d(in_channels, out_channels, 3, 1, 1)
self.skip = nn.Conv2d(in_channels, out_channels, 1, bias=False)
if mode == 'down':
self.scale_factor = 0.5
elif mode == 'up':
self.scale_factor = 2
def forward(self, x):
out = F.leaky_relu_(self.conv1(x), negative_slope=0.2)
# upsample/downsample
out = F.interpolate(out, scale_factor=self.scale_factor, mode='bilinear', align_corners=False)
out = F.leaky_relu_(self.conv2(out), negative_slope=0.2)
# skip
x = F.interpolate(x, scale_factor=self.scale_factor, mode='bilinear', align_corners=False)
skip = self.skip(x)
out = out + skip
return out
@ARCH_REGISTRY.register()
class GFPGANv1Clean(nn.Module):
"""The GFPGAN architecture: Unet + StyleGAN2 decoder with SFT.
It is the clean version without custom compiled CUDA extensions used in StyleGAN2.
Ref: GFP-GAN: Towards Real-World Blind Face Restoration with Generative Facial Prior.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
decoder_load_path (str): The path to the pre-trained decoder model (usually, the StyleGAN2). Default: None.
fix_decoder (bool): Whether to fix the decoder. Default: True.
num_mlp (int): Layer number of MLP style layers. Default: 8.
input_is_latent (bool): Whether input is latent style. Default: False.
different_w (bool): Whether to use different latent w for different layers. Default: False.
narrow (float): The narrow ratio for channels. Default: 1.
sft_half (bool): Whether to apply SFT on half of the input channels. Default: False.
"""
def __init__(
self,
out_size,
num_style_feat=512,
channel_multiplier=1,
decoder_load_path=None,
fix_decoder=True,
# for stylegan decoder
num_mlp=8,
input_is_latent=False,
different_w=False,
narrow=1,
sft_half=False):
super(GFPGANv1Clean, self).__init__()
self.input_is_latent = input_is_latent
self.different_w = different_w
self.num_style_feat = num_style_feat
unet_narrow = narrow * 0.5 # by default, use a half of input channels
channels = {
'4': int(512 * unet_narrow),
'8': int(512 * unet_narrow),
'16': int(512 * unet_narrow),
'32': int(512 * unet_narrow),
'64': int(256 * channel_multiplier * unet_narrow),
'128': int(128 * channel_multiplier * unet_narrow),
'256': int(64 * channel_multiplier * unet_narrow),
'512': int(32 * channel_multiplier * unet_narrow),
'1024': int(16 * channel_multiplier * unet_narrow)
}
self.log_size = int(math.log(out_size, 2))
first_out_size = 2**(int(math.log(out_size, 2)))
self.conv_body_first = nn.Conv2d(3, channels[f'{first_out_size}'], 1)
# downsample
in_channels = channels[f'{first_out_size}']
self.conv_body_down = nn.ModuleList()
for i in range(self.log_size, 2, -1):
out_channels = channels[f'{2**(i - 1)}']
self.conv_body_down.append(ResBlock(in_channels, out_channels, mode='down'))
in_channels = out_channels
self.final_conv = nn.Conv2d(in_channels, channels['4'], 3, 1, 1)
# upsample
in_channels = channels['4']
self.conv_body_up = nn.ModuleList()
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
self.conv_body_up.append(ResBlock(in_channels, out_channels, mode='up'))
in_channels = out_channels
# to RGB
self.toRGB = nn.ModuleList()
for i in range(3, self.log_size + 1):
self.toRGB.append(nn.Conv2d(channels[f'{2**i}'], 3, 1))
if different_w:
linear_out_channel = (int(math.log(out_size, 2)) * 2 - 2) * num_style_feat
else:
linear_out_channel = num_style_feat
self.final_linear = nn.Linear(channels['4'] * 4 * 4, linear_out_channel)
# the decoder: stylegan2 generator with SFT modulations
self.stylegan_decoder = StyleGAN2GeneratorCSFT(
out_size=out_size,
num_style_feat=num_style_feat,
num_mlp=num_mlp,
channel_multiplier=channel_multiplier,
narrow=narrow,
sft_half=sft_half)
# load pre-trained stylegan2 model if necessary
if decoder_load_path:
self.stylegan_decoder.load_state_dict(
torch.load(decoder_load_path, map_location=lambda storage, loc: storage)['params_ema'])
# fix decoder without updating params
if fix_decoder:
for _, param in self.stylegan_decoder.named_parameters():
param.requires_grad = False
# for SFT modulations (scale and shift)
self.condition_scale = nn.ModuleList()
self.condition_shift = nn.ModuleList()
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
if sft_half:
sft_out_channels = out_channels
else:
sft_out_channels = out_channels * 2
self.condition_scale.append(
nn.Sequential(
nn.Conv2d(out_channels, out_channels, 3, 1, 1), nn.LeakyReLU(0.2, True),
nn.Conv2d(out_channels, sft_out_channels, 3, 1, 1)))
self.condition_shift.append(
nn.Sequential(
nn.Conv2d(out_channels, out_channels, 3, 1, 1), nn.LeakyReLU(0.2, True),
nn.Conv2d(out_channels, sft_out_channels, 3, 1, 1)))
def forward(self, x, return_latents=False, return_rgb=True, randomize_noise=True):
"""Forward function for GFPGANv1Clean.
Args:
x (Tensor): Input images.
return_latents (bool): Whether to return style latents. Default: False.
return_rgb (bool): Whether return intermediate rgb images. Default: True.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
"""
conditions = []
unet_skips = []
out_rgbs = []
# encoder
feat = F.leaky_relu_(self.conv_body_first(x), negative_slope=0.2)
for i in range(self.log_size - 2):
feat = self.conv_body_down[i](feat)
unet_skips.insert(0, feat)
feat = F.leaky_relu_(self.final_conv(feat), negative_slope=0.2)
# style code
style_code = self.final_linear(feat.view(feat.size(0), -1))
if self.different_w:
style_code = style_code.view(style_code.size(0), -1, self.num_style_feat)
# decode
for i in range(self.log_size - 2):
# add unet skip
feat = feat + unet_skips[i]
# ResUpLayer
feat = self.conv_body_up[i](feat)
# generate scale and shift for SFT layers
scale = self.condition_scale[i](feat)
conditions.append(scale.clone())
shift = self.condition_shift[i](feat)
conditions.append(shift.clone())
# generate rgb images
if return_rgb:
out_rgbs.append(self.toRGB[i](feat))
# decoder
image, _ = self.stylegan_decoder([style_code],
conditions,
return_latents=return_latents,
input_is_latent=self.input_is_latent,
randomize_noise=randomize_noise)
return image, out_rgbs123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613import math
import random
import torch
from basicsr.ops.fused_act import FusedLeakyReLU, fused_leaky_relu
from basicsr.utils.registry import ARCH_REGISTRY
from torch import nn
from torch.nn import functional as F
class NormStyleCode(nn.Module):
def forward(self, x):
"""Normalize the style codes.
Args:
x (Tensor): Style codes with shape (b, c).
Returns:
Tensor: Normalized tensor.
"""
return x * torch.rsqrt(torch.mean(x**2, dim=1, keepdim=True) + 1e-8)
class EqualLinear(nn.Module):
"""Equalized Linear as StyleGAN2.
Args:
in_channels (int): Size of each sample.
out_channels (int): Size of each output sample.
bias (bool): If set to ``False``, the layer will not learn an additive
bias. Default: ``True``.
bias_init_val (float): Bias initialized value. Default: 0.
lr_mul (float): Learning rate multiplier. Default: 1.
activation (None | str): The activation after ``linear`` operation.
Supported: 'fused_lrelu', None. Default: None.
"""
def __init__(self, in_channels, out_channels, bias=True, bias_init_val=0, lr_mul=1, activation=None):
super(EqualLinear, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.lr_mul = lr_mul
self.activation = activation
if self.activation not in ['fused_lrelu', None]:
raise ValueError(f'Wrong activation value in EqualLinear: {activation}'
"Supported ones are: ['fused_lrelu', None].")
self.scale = (1 / math.sqrt(in_channels)) * lr_mul
self.weight = nn.Parameter(torch.randn(out_channels, in_channels).div_(lr_mul))
if bias:
self.bias = nn.Parameter(torch.zeros(out_channels).fill_(bias_init_val))
else:
self.register_parameter('bias', None)
def forward(self, x):
if self.bias is None:
bias = None
else:
bias = self.bias * self.lr_mul
if self.activation == 'fused_lrelu':
out = F.linear(x, self.weight * self.scale)
out = fused_leaky_relu(out, bias)
else:
out = F.linear(x, self.weight * self.scale, bias=bias)
return out
def __repr__(self):
return (f'{self.__class__.__name__}(in_channels={self.in_channels}, '
f'out_channels={self.out_channels}, bias={self.bias is not None})')
class ModulatedConv2d(nn.Module):
"""Modulated Conv2d used in StyleGAN2.
There is no bias in ModulatedConv2d.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Size of the convolving kernel.
num_style_feat (int): Channel number of style features.
demodulate (bool): Whether to demodulate in the conv layer.
Default: True.
sample_mode (str | None): Indicating 'upsample', 'downsample' or None.
Default: None.
eps (float): A value added to the denominator for numerical stability.
Default: 1e-8.
"""
def __init__(self,
in_channels,
out_channels,
kernel_size,
num_style_feat,
demodulate=True,
sample_mode=None,
eps=1e-8,
interpolation_mode='bilinear'):
super(ModulatedConv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.demodulate = demodulate
self.sample_mode = sample_mode
self.eps = eps
self.interpolation_mode = interpolation_mode
if self.interpolation_mode == 'nearest':
self.align_corners = None
else:
self.align_corners = False
self.scale = 1 / math.sqrt(in_channels * kernel_size**2)
# modulation inside each modulated conv
self.modulation = EqualLinear(
num_style_feat, in_channels, bias=True, bias_init_val=1, lr_mul=1, activation=None)
self.weight = nn.Parameter(torch.randn(1, out_channels, in_channels, kernel_size, kernel_size))
self.padding = kernel_size // 2
def forward(self, x, style):
"""Forward function.
Args:
x (Tensor): Tensor with shape (b, c, h, w).
style (Tensor): Tensor with shape (b, num_style_feat).
Returns:
Tensor: Modulated tensor after convolution.
"""
b, c, h, w = x.shape # c = c_in
# weight modulation
style = self.modulation(style).view(b, 1, c, 1, 1)
# self.weight: (1, c_out, c_in, k, k); style: (b, 1, c, 1, 1)
weight = self.scale * self.weight * style # (b, c_out, c_in, k, k)
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + self.eps)
weight = weight * demod.view(b, self.out_channels, 1, 1, 1)
weight = weight.view(b * self.out_channels, c, self.kernel_size, self.kernel_size)
if self.sample_mode == 'upsample':
x = F.interpolate(x, scale_factor=2, mode=self.interpolation_mode, align_corners=self.align_corners)
elif self.sample_mode == 'downsample':
x = F.interpolate(x, scale_factor=0.5, mode=self.interpolation_mode, align_corners=self.align_corners)
b, c, h, w = x.shape
x = x.view(1, b * c, h, w)
# weight: (b*c_out, c_in, k, k), groups=b
out = F.conv2d(x, weight, padding=self.padding, groups=b)
out = out.view(b, self.out_channels, *out.shape[2:4])
return out
def __repr__(self):
return (f'{self.__class__.__name__}(in_channels={self.in_channels}, '
f'out_channels={self.out_channels}, '
f'kernel_size={self.kernel_size}, '
f'demodulate={self.demodulate}, sample_mode={self.sample_mode})')
class StyleConv(nn.Module):
"""Style conv.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Size of the convolving kernel.
num_style_feat (int): Channel number of style features.
demodulate (bool): Whether demodulate in the conv layer. Default: True.
sample_mode (str | None): Indicating 'upsample', 'downsample' or None.
Default: None.
"""
def __init__(self,
in_channels,
out_channels,
kernel_size,
num_style_feat,
demodulate=True,
sample_mode=None,
interpolation_mode='bilinear'):
super(StyleConv, self).__init__()
self.modulated_conv = ModulatedConv2d(
in_channels,
out_channels,
kernel_size,
num_style_feat,
demodulate=demodulate,
sample_mode=sample_mode,
interpolation_mode=interpolation_mode)
self.weight = nn.Parameter(torch.zeros(1)) # for noise injection
self.activate = FusedLeakyReLU(out_channels)
def forward(self, x, style, noise=None):
# modulate
out = self.modulated_conv(x, style)
# noise injection
if noise is None:
b, _, h, w = out.shape
noise = out.new_empty(b, 1, h, w).normal_()
out = out + self.weight * noise
# activation (with bias)
out = self.activate(out)
return out
class ToRGB(nn.Module):
"""To RGB from features.
Args:
in_channels (int): Channel number of input.
num_style_feat (int): Channel number of style features.
upsample (bool): Whether to upsample. Default: True.
"""
def __init__(self, in_channels, num_style_feat, upsample=True, interpolation_mode='bilinear'):
super(ToRGB, self).__init__()
self.upsample = upsample
self.interpolation_mode = interpolation_mode
if self.interpolation_mode == 'nearest':
self.align_corners = None
else:
self.align_corners = False
self.modulated_conv = ModulatedConv2d(
in_channels,
3,
kernel_size=1,
num_style_feat=num_style_feat,
demodulate=False,
sample_mode=None,
interpolation_mode=interpolation_mode)
self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))
def forward(self, x, style, skip=None):
"""Forward function.
Args:
x (Tensor): Feature tensor with shape (b, c, h, w).
style (Tensor): Tensor with shape (b, num_style_feat).
skip (Tensor): Base/skip tensor. Default: None.
Returns:
Tensor: RGB images.
"""
out = self.modulated_conv(x, style)
out = out + self.bias
if skip is not None:
if self.upsample:
skip = F.interpolate(
skip, scale_factor=2, mode=self.interpolation_mode, align_corners=self.align_corners)
out = out + skip
return out
class ConstantInput(nn.Module):
"""Constant input.
Args:
num_channel (int): Channel number of constant input.
size (int): Spatial size of constant input.
"""
def __init__(self, num_channel, size):
super(ConstantInput, self).__init__()
self.weight = nn.Parameter(torch.randn(1, num_channel, size, size))
def forward(self, batch):
out = self.weight.repeat(batch, 1, 1, 1)
return out
@ARCH_REGISTRY.register()
class StyleGAN2GeneratorBilinear(nn.Module):
"""StyleGAN2 Generator.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
num_mlp (int): Layer number of MLP style layers. Default: 8.
channel_multiplier (int): Channel multiplier for large networks of
StyleGAN2. Default: 2.
lr_mlp (float): Learning rate multiplier for mlp layers. Default: 0.01.
narrow (float): Narrow ratio for channels. Default: 1.0.
"""
def __init__(self,
out_size,
num_style_feat=512,
num_mlp=8,
channel_multiplier=2,
lr_mlp=0.01,
narrow=1,
interpolation_mode='bilinear'):
super(StyleGAN2GeneratorBilinear, self).__init__()
# Style MLP layers
self.num_style_feat = num_style_feat
style_mlp_layers = [NormStyleCode()]
for i in range(num_mlp):
style_mlp_layers.append(
EqualLinear(
num_style_feat, num_style_feat, bias=True, bias_init_val=0, lr_mul=lr_mlp,
activation='fused_lrelu'))
self.style_mlp = nn.Sequential(*style_mlp_layers)
channels = {
'4': int(512 * narrow),
'8': int(512 * narrow),
'16': int(512 * narrow),
'32': int(512 * narrow),
'64': int(256 * channel_multiplier * narrow),
'128': int(128 * channel_multiplier * narrow),
'256': int(64 * channel_multiplier * narrow),
'512': int(32 * channel_multiplier * narrow),
'1024': int(16 * channel_multiplier * narrow)
}
self.channels = channels
self.constant_input = ConstantInput(channels['4'], size=4)
self.style_conv1 = StyleConv(
channels['4'],
channels['4'],
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode=None,
interpolation_mode=interpolation_mode)
self.to_rgb1 = ToRGB(channels['4'], num_style_feat, upsample=False, interpolation_mode=interpolation_mode)
self.log_size = int(math.log(out_size, 2))
self.num_layers = (self.log_size - 2) * 2 + 1
self.num_latent = self.log_size * 2 - 2
self.style_convs = nn.ModuleList()
self.to_rgbs = nn.ModuleList()
self.noises = nn.Module()
in_channels = channels['4']
# noise
for layer_idx in range(self.num_layers):
resolution = 2**((layer_idx + 5) // 2)
shape = [1, 1, resolution, resolution]
self.noises.register_buffer(f'noise{layer_idx}', torch.randn(*shape))
# style convs and to_rgbs
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
self.style_convs.append(
StyleConv(
in_channels,
out_channels,
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode='upsample',
interpolation_mode=interpolation_mode))
self.style_convs.append(
StyleConv(
out_channels,
out_channels,
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode=None,
interpolation_mode=interpolation_mode))
self.to_rgbs.append(
ToRGB(out_channels, num_style_feat, upsample=True, interpolation_mode=interpolation_mode))
in_channels = out_channels
def make_noise(self):
"""Make noise for noise injection."""
device = self.constant_input.weight.device
noises = [torch.randn(1, 1, 4, 4, device=device)]
for i in range(3, self.log_size + 1):
for _ in range(2):
noises.append(torch.randn(1, 1, 2**i, 2**i, device=device))
return noises
def get_latent(self, x):
return self.style_mlp(x)
def mean_latent(self, num_latent):
latent_in = torch.randn(num_latent, self.num_style_feat, device=self.constant_input.weight.device)
latent = self.style_mlp(latent_in).mean(0, keepdim=True)
return latent
def forward(self,
styles,
input_is_latent=False,
noise=None,
randomize_noise=True,
truncation=1,
truncation_latent=None,
inject_index=None,
return_latents=False):
"""Forward function for StyleGAN2Generator.
Args:
styles (list[Tensor]): Sample codes of styles.
input_is_latent (bool): Whether input is latent style.
Default: False.
noise (Tensor | None): Input noise or None. Default: None.
randomize_noise (bool): Randomize noise, used when 'noise' is
False. Default: True.
truncation (float): TODO. Default: 1.
truncation_latent (Tensor | None): TODO. Default: None.
inject_index (int | None): The injection index for mixing noise.
Default: None.
return_latents (bool): Whether to return style latents.
Default: False.
"""
# style codes -> latents with Style MLP layer
if not input_is_latent:
styles = [self.style_mlp(s) for s in styles]
# noises
if noise is None:
if randomize_noise:
noise = [None] * self.num_layers # for each style conv layer
else: # use the stored noise
noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
# style truncation
if truncation < 1:
style_truncation = []
for style in styles:
style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
styles = style_truncation
# get style latent with injection
if len(styles) == 1:
inject_index = self.num_latent
if styles[0].ndim < 3:
# repeat latent code for all the layers
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
else: # used for encoder with different latent code for each layer
latent = styles[0]
elif len(styles) == 2: # mixing noises
if inject_index is None:
inject_index = random.randint(1, self.num_latent - 1)
latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
latent = torch.cat([latent1, latent2], 1)
# main generation
out = self.constant_input(latent.shape[0])
out = self.style_conv1(out, latent[:, 0], noise=noise[0])
skip = self.to_rgb1(out, latent[:, 1])
i = 1
for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
noise[2::2], self.to_rgbs):
out = conv1(out, latent[:, i], noise=noise1)
out = conv2(out, latent[:, i + 1], noise=noise2)
skip = to_rgb(out, latent[:, i + 2], skip)
i += 2
image = skip
if return_latents:
return image, latent
else:
return image, None
class ScaledLeakyReLU(nn.Module):
"""Scaled LeakyReLU.
Args:
negative_slope (float): Negative slope. Default: 0.2.
"""
def __init__(self, negative_slope=0.2):
super(ScaledLeakyReLU, self).__init__()
self.negative_slope = negative_slope
def forward(self, x):
out = F.leaky_relu(x, negative_slope=self.negative_slope)
return out * math.sqrt(2)
class EqualConv2d(nn.Module):
"""Equalized Linear as StyleGAN2.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Size of the convolving kernel.
stride (int): Stride of the convolution. Default: 1
padding (int): Zero-padding added to both sides of the input.
Default: 0.
bias (bool): If ``True``, adds a learnable bias to the output.
Default: ``True``.
bias_init_val (float): Bias initialized value. Default: 0.
"""
def __init__(self, in_channels, out_channels, kernel_size, stride=1, padding=0, bias=True, bias_init_val=0):
super(EqualConv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.scale = 1 / math.sqrt(in_channels * kernel_size**2)
self.weight = nn.Parameter(torch.randn(out_channels, in_channels, kernel_size, kernel_size))
if bias:
self.bias = nn.Parameter(torch.zeros(out_channels).fill_(bias_init_val))
else:
self.register_parameter('bias', None)
def forward(self, x):
out = F.conv2d(
x,
self.weight * self.scale,
bias=self.bias,
stride=self.stride,
padding=self.padding,
)
return out
def __repr__(self):
return (f'{self.__class__.__name__}(in_channels={self.in_channels}, '
f'out_channels={self.out_channels}, '
f'kernel_size={self.kernel_size},'
f' stride={self.stride}, padding={self.padding}, '
f'bias={self.bias is not None})')
class ConvLayer(nn.Sequential):
"""Conv Layer used in StyleGAN2 Discriminator.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Kernel size.
downsample (bool): Whether downsample by a factor of 2.
Default: False.
bias (bool): Whether with bias. Default: True.
activate (bool): Whether use activateion. Default: True.
"""
def __init__(self,
in_channels,
out_channels,
kernel_size,
downsample=False,
bias=True,
activate=True,
interpolation_mode='bilinear'):
layers = []
self.interpolation_mode = interpolation_mode
# downsample
if downsample:
if self.interpolation_mode == 'nearest':
self.align_corners = None
else:
self.align_corners = False
layers.append(
torch.nn.Upsample(scale_factor=0.5, mode=interpolation_mode, align_corners=self.align_corners))
stride = 1
self.padding = kernel_size // 2
# conv
layers.append(
EqualConv2d(
in_channels, out_channels, kernel_size, stride=stride, padding=self.padding, bias=bias
and not activate))
# activation
if activate:
if bias:
layers.append(FusedLeakyReLU(out_channels))
else:
layers.append(ScaledLeakyReLU(0.2))
super(ConvLayer, self).__init__(*layers)
class ResBlock(nn.Module):
"""Residual block used in StyleGAN2 Discriminator.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
"""
def __init__(self, in_channels, out_channels, interpolation_mode='bilinear'):
super(ResBlock, self).__init__()
self.conv1 = ConvLayer(in_channels, in_channels, 3, bias=True, activate=True)
self.conv2 = ConvLayer(
in_channels,
out_channels,
3,
downsample=True,
interpolation_mode=interpolation_mode,
bias=True,
activate=True)
self.skip = ConvLayer(
in_channels,
out_channels,
1,
downsample=True,
interpolation_mode=interpolation_mode,
bias=False,
activate=False)
def forward(self, x):
out = self.conv1(x)
out = self.conv2(out)
skip = self.skip(x)
out = (out + skip) / math.sqrt(2)
return out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368import math
import random
import torch
from basicsr.archs.arch_util import default_init_weights
from basicsr.utils.registry import ARCH_REGISTRY
from torch import nn
from torch.nn import functional as F
class NormStyleCode(nn.Module):
def forward(self, x):
"""Normalize the style codes.
Args:
x (Tensor): Style codes with shape (b, c).
Returns:
Tensor: Normalized tensor.
"""
return x * torch.rsqrt(torch.mean(x**2, dim=1, keepdim=True) + 1e-8)
class ModulatedConv2d(nn.Module):
"""Modulated Conv2d used in StyleGAN2.
There is no bias in ModulatedConv2d.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Size of the convolving kernel.
num_style_feat (int): Channel number of style features.
demodulate (bool): Whether to demodulate in the conv layer. Default: True.
sample_mode (str | None): Indicating 'upsample', 'downsample' or None. Default: None.
eps (float): A value added to the denominator for numerical stability. Default: 1e-8.
"""
def __init__(self,
in_channels,
out_channels,
kernel_size,
num_style_feat,
demodulate=True,
sample_mode=None,
eps=1e-8):
super(ModulatedConv2d, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.demodulate = demodulate
self.sample_mode = sample_mode
self.eps = eps
# modulation inside each modulated conv
self.modulation = nn.Linear(num_style_feat, in_channels, bias=True)
# initialization
default_init_weights(self.modulation, scale=1, bias_fill=1, a=0, mode='fan_in', nonlinearity='linear')
self.weight = nn.Parameter(
torch.randn(1, out_channels, in_channels, kernel_size, kernel_size) /
math.sqrt(in_channels * kernel_size**2))
self.padding = kernel_size // 2
def forward(self, x, style):
"""Forward function.
Args:
x (Tensor): Tensor with shape (b, c, h, w).
style (Tensor): Tensor with shape (b, num_style_feat).
Returns:
Tensor: Modulated tensor after convolution.
"""
b, c, h, w = x.shape # c = c_in
# weight modulation
style = self.modulation(style).view(b, 1, c, 1, 1)
# self.weight: (1, c_out, c_in, k, k); style: (b, 1, c, 1, 1)
weight = self.weight * style # (b, c_out, c_in, k, k)
if self.demodulate:
demod = torch.rsqrt(weight.pow(2).sum([2, 3, 4]) + self.eps)
weight = weight * demod.view(b, self.out_channels, 1, 1, 1)
weight = weight.view(b * self.out_channels, c, self.kernel_size, self.kernel_size)
# upsample or downsample if necessary
if self.sample_mode == 'upsample':
x = F.interpolate(x, scale_factor=2, mode='bilinear', align_corners=False)
elif self.sample_mode == 'downsample':
x = F.interpolate(x, scale_factor=0.5, mode='bilinear', align_corners=False)
b, c, h, w = x.shape
x = x.view(1, b * c, h, w)
# weight: (b*c_out, c_in, k, k), groups=b
out = F.conv2d(x, weight, padding=self.padding, groups=b)
out = out.view(b, self.out_channels, *out.shape[2:4])
return out
def __repr__(self):
return (f'{self.__class__.__name__}(in_channels={self.in_channels}, out_channels={self.out_channels}, '
f'kernel_size={self.kernel_size}, demodulate={self.demodulate}, sample_mode={self.sample_mode})')
class StyleConv(nn.Module):
"""Style conv used in StyleGAN2.
Args:
in_channels (int): Channel number of the input.
out_channels (int): Channel number of the output.
kernel_size (int): Size of the convolving kernel.
num_style_feat (int): Channel number of style features.
demodulate (bool): Whether demodulate in the conv layer. Default: True.
sample_mode (str | None): Indicating 'upsample', 'downsample' or None. Default: None.
"""
def __init__(self, in_channels, out_channels, kernel_size, num_style_feat, demodulate=True, sample_mode=None):
super(StyleConv, self).__init__()
self.modulated_conv = ModulatedConv2d(
in_channels, out_channels, kernel_size, num_style_feat, demodulate=demodulate, sample_mode=sample_mode)
self.weight = nn.Parameter(torch.zeros(1)) # for noise injection
self.bias = nn.Parameter(torch.zeros(1, out_channels, 1, 1))
self.activate = nn.LeakyReLU(negative_slope=0.2, inplace=True)
def forward(self, x, style, noise=None):
# modulate
out = self.modulated_conv(x, style) * 2**0.5 # for conversion
# noise injection
if noise is None:
b, _, h, w = out.shape
noise = out.new_empty(b, 1, h, w).normal_()
out = out + self.weight * noise
# add bias
out = out + self.bias
# activation
out = self.activate(out)
return out
class ToRGB(nn.Module):
"""To RGB (image space) from features.
Args:
in_channels (int): Channel number of input.
num_style_feat (int): Channel number of style features.
upsample (bool): Whether to upsample. Default: True.
"""
def __init__(self, in_channels, num_style_feat, upsample=True):
super(ToRGB, self).__init__()
self.upsample = upsample
self.modulated_conv = ModulatedConv2d(
in_channels, 3, kernel_size=1, num_style_feat=num_style_feat, demodulate=False, sample_mode=None)
self.bias = nn.Parameter(torch.zeros(1, 3, 1, 1))
def forward(self, x, style, skip=None):
"""Forward function.
Args:
x (Tensor): Feature tensor with shape (b, c, h, w).
style (Tensor): Tensor with shape (b, num_style_feat).
skip (Tensor): Base/skip tensor. Default: None.
Returns:
Tensor: RGB images.
"""
out = self.modulated_conv(x, style)
out = out + self.bias
if skip is not None:
if self.upsample:
skip = F.interpolate(skip, scale_factor=2, mode='bilinear', align_corners=False)
out = out + skip
return out
class ConstantInput(nn.Module):
"""Constant input.
Args:
num_channel (int): Channel number of constant input.
size (int): Spatial size of constant input.
"""
def __init__(self, num_channel, size):
super(ConstantInput, self).__init__()
self.weight = nn.Parameter(torch.randn(1, num_channel, size, size))
def forward(self, batch):
out = self.weight.repeat(batch, 1, 1, 1)
return out
@ARCH_REGISTRY.register()
class StyleGAN2GeneratorClean(nn.Module):
"""Clean version of StyleGAN2 Generator.
Args:
out_size (int): The spatial size of outputs.
num_style_feat (int): Channel number of style features. Default: 512.
num_mlp (int): Layer number of MLP style layers. Default: 8.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
narrow (float): Narrow ratio for channels. Default: 1.0.
"""
def __init__(self, out_size, num_style_feat=512, num_mlp=8, channel_multiplier=2, narrow=1):
super(StyleGAN2GeneratorClean, self).__init__()
# Style MLP layers
self.num_style_feat = num_style_feat
style_mlp_layers = [NormStyleCode()]
for i in range(num_mlp):
style_mlp_layers.extend(
[nn.Linear(num_style_feat, num_style_feat, bias=True),
nn.LeakyReLU(negative_slope=0.2, inplace=True)])
self.style_mlp = nn.Sequential(*style_mlp_layers)
# initialization
default_init_weights(self.style_mlp, scale=1, bias_fill=0, a=0.2, mode='fan_in', nonlinearity='leaky_relu')
# channel list
channels = {
'4': int(512 * narrow),
'8': int(512 * narrow),
'16': int(512 * narrow),
'32': int(512 * narrow),
'64': int(256 * channel_multiplier * narrow),
'128': int(128 * channel_multiplier * narrow),
'256': int(64 * channel_multiplier * narrow),
'512': int(32 * channel_multiplier * narrow),
'1024': int(16 * channel_multiplier * narrow)
}
self.channels = channels
self.constant_input = ConstantInput(channels['4'], size=4)
self.style_conv1 = StyleConv(
channels['4'],
channels['4'],
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode=None)
self.to_rgb1 = ToRGB(channels['4'], num_style_feat, upsample=False)
self.log_size = int(math.log(out_size, 2))
self.num_layers = (self.log_size - 2) * 2 + 1
self.num_latent = self.log_size * 2 - 2
self.style_convs = nn.ModuleList()
self.to_rgbs = nn.ModuleList()
self.noises = nn.Module()
in_channels = channels['4']
# noise
for layer_idx in range(self.num_layers):
resolution = 2**((layer_idx + 5) // 2)
shape = [1, 1, resolution, resolution]
self.noises.register_buffer(f'noise{layer_idx}', torch.randn(*shape))
# style convs and to_rgbs
for i in range(3, self.log_size + 1):
out_channels = channels[f'{2**i}']
self.style_convs.append(
StyleConv(
in_channels,
out_channels,
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode='upsample'))
self.style_convs.append(
StyleConv(
out_channels,
out_channels,
kernel_size=3,
num_style_feat=num_style_feat,
demodulate=True,
sample_mode=None))
self.to_rgbs.append(ToRGB(out_channels, num_style_feat, upsample=True))
in_channels = out_channels
def make_noise(self):
"""Make noise for noise injection."""
device = self.constant_input.weight.device
noises = [torch.randn(1, 1, 4, 4, device=device)]
for i in range(3, self.log_size + 1):
for _ in range(2):
noises.append(torch.randn(1, 1, 2**i, 2**i, device=device))
return noises
def get_latent(self, x):
return self.style_mlp(x)
def mean_latent(self, num_latent):
latent_in = torch.randn(num_latent, self.num_style_feat, device=self.constant_input.weight.device)
latent = self.style_mlp(latent_in).mean(0, keepdim=True)
return latent
def forward(self,
styles,
input_is_latent=False,
noise=None,
randomize_noise=True,
truncation=1,
truncation_latent=None,
inject_index=None,
return_latents=False):
"""Forward function for StyleGAN2GeneratorClean.
Args:
styles (list[Tensor]): Sample codes of styles.
input_is_latent (bool): Whether input is latent style. Default: False.
noise (Tensor | None): Input noise or None. Default: None.
randomize_noise (bool): Randomize noise, used when 'noise' is False. Default: True.
truncation (float): The truncation ratio. Default: 1.
truncation_latent (Tensor | None): The truncation latent tensor. Default: None.
inject_index (int | None): The injection index for mixing noise. Default: None.
return_latents (bool): Whether to return style latents. Default: False.
"""
# style codes -> latents with Style MLP layer
if not input_is_latent:
styles = [self.style_mlp(s) for s in styles]
# noises
if noise is None:
if randomize_noise:
noise = [None] * self.num_layers # for each style conv layer
else: # use the stored noise
noise = [getattr(self.noises, f'noise{i}') for i in range(self.num_layers)]
# style truncation
if truncation < 1:
style_truncation = []
for style in styles:
style_truncation.append(truncation_latent + truncation * (style - truncation_latent))
styles = style_truncation
# get style latents with injection
if len(styles) == 1:
inject_index = self.num_latent
if styles[0].ndim < 3:
# repeat latent code for all the layers
latent = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
else: # used for encoder with different latent code for each layer
latent = styles[0]
elif len(styles) == 2: # mixing noises
if inject_index is None:
inject_index = random.randint(1, self.num_latent - 1)
latent1 = styles[0].unsqueeze(1).repeat(1, inject_index, 1)
latent2 = styles[1].unsqueeze(1).repeat(1, self.num_latent - inject_index, 1)
latent = torch.cat([latent1, latent2], 1)
# main generation
out = self.constant_input(latent.shape[0])
out = self.style_conv1(out, latent[:, 0], noise=noise[0])
skip = self.to_rgb1(out, latent[:, 1])
i = 1
for conv1, conv2, noise1, noise2, to_rgb in zip(self.style_convs[::2], self.style_convs[1::2], noise[1::2],
noise[2::2], self.to_rgbs):
out = conv1(out, latent[:, i], noise=noise1)
out = conv2(out, latent[:, i + 1], noise=noise2)
skip = to_rgb(out, latent[:, i + 2], skip) # feature back to the rgb space
i += 2
image = skip
if return_latents:
return image, latent
else:
return image, None12345678910import importlib
from basicsr.utils import scandir
from os import path as osp
# automatically scan and import dataset modules for registry
# scan all the files that end with '_dataset.py' under the data folder
data_folder = osp.dirname(osp.abspath(__file__))
dataset_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(data_folder) if v.endswith('_dataset.py')]
# import all the dataset modules
_dataset_modules = [importlib.import_module(f'gfpgan.data.{file_name}') for file_name in dataset_filenames]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230import cv2
import math
import numpy as np
import os.path as osp
import torch
import torch.utils.data as data
from basicsr.data import degradations as degradations
from basicsr.data.data_util import paths_from_folder
from basicsr.data.transforms import augment
from basicsr.utils import FileClient, get_root_logger, imfrombytes, img2tensor
from basicsr.utils.registry import DATASET_REGISTRY
from torchvision.transforms.functional import (adjust_brightness, adjust_contrast, adjust_hue, adjust_saturation,
normalize)
@DATASET_REGISTRY.register()
class FFHQDegradationDataset(data.Dataset):
"""FFHQ dataset for GFPGAN.
It reads high resolution images, and then generate low-quality (LQ) images on-the-fly.
Args:
opt (dict): Config for train datasets. It contains the following keys:
dataroot_gt (str): Data root path for gt.
io_backend (dict): IO backend type and other kwarg.
mean (list | tuple): Image mean.
std (list | tuple): Image std.
use_hflip (bool): Whether to horizontally flip.
Please see more options in the codes.
"""
def __init__(self, opt):
super(FFHQDegradationDataset, self).__init__()
self.opt = opt
# file client (io backend)
self.file_client = None
self.io_backend_opt = opt['io_backend']
self.gt_folder = opt['dataroot_gt']
self.mean = opt['mean']
self.std = opt['std']
self.out_size = opt['out_size']
self.crop_components = opt.get('crop_components', False) # facial components
self.eye_enlarge_ratio = opt.get('eye_enlarge_ratio', 1) # whether enlarge eye regions
if self.crop_components:
# load component list from a pre-process pth files
self.components_list = torch.load(opt.get('component_path'))
# file client (lmdb io backend)
if self.io_backend_opt['type'] == 'lmdb':
self.io_backend_opt['db_paths'] = self.gt_folder
if not self.gt_folder.endswith('.lmdb'):
raise ValueError(f"'dataroot_gt' should end with '.lmdb', but received {self.gt_folder}")
with open(osp.join(self.gt_folder, 'meta_info.txt')) as fin:
self.paths = [line.split('.')[0] for line in fin]
else:
# disk backend: scan file list from a folder
self.paths = paths_from_folder(self.gt_folder)
# degradation configurations
self.blur_kernel_size = opt['blur_kernel_size']
self.kernel_list = opt['kernel_list']
self.kernel_prob = opt['kernel_prob']
self.blur_sigma = opt['blur_sigma']
self.downsample_range = opt['downsample_range']
self.noise_range = opt['noise_range']
self.jpeg_range = opt['jpeg_range']
# color jitter
self.color_jitter_prob = opt.get('color_jitter_prob')
self.color_jitter_pt_prob = opt.get('color_jitter_pt_prob')
self.color_jitter_shift = opt.get('color_jitter_shift', 20)
# to gray
self.gray_prob = opt.get('gray_prob')
logger = get_root_logger()
logger.info(f'Blur: blur_kernel_size {self.blur_kernel_size}, sigma: [{", ".join(map(str, self.blur_sigma))}]')
logger.info(f'Downsample: downsample_range [{", ".join(map(str, self.downsample_range))}]')
logger.info(f'Noise: [{", ".join(map(str, self.noise_range))}]')
logger.info(f'JPEG compression: [{", ".join(map(str, self.jpeg_range))}]')
if self.color_jitter_prob is not None:
logger.info(f'Use random color jitter. Prob: {self.color_jitter_prob}, shift: {self.color_jitter_shift}')
if self.gray_prob is not None:
logger.info(f'Use random gray. Prob: {self.gray_prob}')
self.color_jitter_shift /= 255.
@staticmethod
def color_jitter(img, shift):
"""jitter color: randomly jitter the RGB values, in numpy formats"""
jitter_val = np.random.uniform(-shift, shift, 3).astype(np.float32)
img = img + jitter_val
img = np.clip(img, 0, 1)
return img
@staticmethod
def color_jitter_pt(img, brightness, contrast, saturation, hue):
"""jitter color: randomly jitter the brightness, contrast, saturation, and hue, in torch Tensor formats"""
fn_idx = torch.randperm(4)
for fn_id in fn_idx:
if fn_id == 0 and brightness is not None:
brightness_factor = torch.tensor(1.0).uniform_(brightness[0], brightness[1]).item()
img = adjust_brightness(img, brightness_factor)
if fn_id == 1 and contrast is not None:
contrast_factor = torch.tensor(1.0).uniform_(contrast[0], contrast[1]).item()
img = adjust_contrast(img, contrast_factor)
if fn_id == 2 and saturation is not None:
saturation_factor = torch.tensor(1.0).uniform_(saturation[0], saturation[1]).item()
img = adjust_saturation(img, saturation_factor)
if fn_id == 3 and hue is not None:
hue_factor = torch.tensor(1.0).uniform_(hue[0], hue[1]).item()
img = adjust_hue(img, hue_factor)
return img
def get_component_coordinates(self, index, status):
"""Get facial component (left_eye, right_eye, mouth) coordinates from a pre-loaded pth file"""
components_bbox = self.components_list[f'{index:08d}']
if status[0]: # hflip
# exchange right and left eye
tmp = components_bbox['left_eye']
components_bbox['left_eye'] = components_bbox['right_eye']
components_bbox['right_eye'] = tmp
# modify the width coordinate
components_bbox['left_eye'][0] = self.out_size - components_bbox['left_eye'][0]
components_bbox['right_eye'][0] = self.out_size - components_bbox['right_eye'][0]
components_bbox['mouth'][0] = self.out_size - components_bbox['mouth'][0]
# get coordinates
locations = []
for part in ['left_eye', 'right_eye', 'mouth']:
mean = components_bbox[part][0:2]
half_len = components_bbox[part][2]
if 'eye' in part:
half_len *= self.eye_enlarge_ratio
loc = np.hstack((mean - half_len + 1, mean + half_len))
loc = torch.from_numpy(loc).float()
locations.append(loc)
return locations
def __getitem__(self, index):
if self.file_client is None:
self.file_client = FileClient(self.io_backend_opt.pop('type'), **self.io_backend_opt)
# load gt image
# Shape: (h, w, c); channel order: BGR; image range: [0, 1], float32.
gt_path = self.paths[index]
img_bytes = self.file_client.get(gt_path)
img_gt = imfrombytes(img_bytes, float32=True)
# random horizontal flip
img_gt, status = augment(img_gt, hflip=self.opt['use_hflip'], rotation=False, return_status=True)
h, w, _ = img_gt.shape
# get facial component coordinates
if self.crop_components:
locations = self.get_component_coordinates(index, status)
loc_left_eye, loc_right_eye, loc_mouth = locations
# ------------------------ generate lq image ------------------------ #
# blur
kernel = degradations.random_mixed_kernels(
self.kernel_list,
self.kernel_prob,
self.blur_kernel_size,
self.blur_sigma,
self.blur_sigma, [-math.pi, math.pi],
noise_range=None)
img_lq = cv2.filter2D(img_gt, -1, kernel)
# downsample
scale = np.random.uniform(self.downsample_range[0], self.downsample_range[1])
img_lq = cv2.resize(img_lq, (int(w // scale), int(h // scale)), interpolation=cv2.INTER_LINEAR)
# noise
if self.noise_range is not None:
img_lq = degradations.random_add_gaussian_noise(img_lq, self.noise_range)
# jpeg compression
if self.jpeg_range is not None:
img_lq = degradations.random_add_jpg_compression(img_lq, self.jpeg_range)
# resize to original size
img_lq = cv2.resize(img_lq, (w, h), interpolation=cv2.INTER_LINEAR)
# random color jitter (only for lq)
if self.color_jitter_prob is not None and (np.random.uniform() < self.color_jitter_prob):
img_lq = self.color_jitter(img_lq, self.color_jitter_shift)
# random to gray (only for lq)
if self.gray_prob and np.random.uniform() < self.gray_prob:
img_lq = cv2.cvtColor(img_lq, cv2.COLOR_BGR2GRAY)
img_lq = np.tile(img_lq[:, :, None], [1, 1, 3])
if self.opt.get('gt_gray'): # whether convert GT to gray images
img_gt = cv2.cvtColor(img_gt, cv2.COLOR_BGR2GRAY)
img_gt = np.tile(img_gt[:, :, None], [1, 1, 3]) # repeat the color channels
# BGR to RGB, HWC to CHW, numpy to tensor
img_gt, img_lq = img2tensor([img_gt, img_lq], bgr2rgb=True, float32=True)
# random color jitter (pytorch version) (only for lq)
if self.color_jitter_pt_prob is not None and (np.random.uniform() < self.color_jitter_pt_prob):
brightness = self.opt.get('brightness', (0.5, 1.5))
contrast = self.opt.get('contrast', (0.5, 1.5))
saturation = self.opt.get('saturation', (0, 1.5))
hue = self.opt.get('hue', (-0.1, 0.1))
img_lq = self.color_jitter_pt(img_lq, brightness, contrast, saturation, hue)
# round and clip
img_lq = torch.clamp((img_lq * 255.0).round(), 0, 255) / 255.
# normalize
normalize(img_gt, self.mean, self.std, inplace=True)
normalize(img_lq, self.mean, self.std, inplace=True)
if self.crop_components:
return_dict = {
'lq': img_lq,
'gt': img_gt,
'gt_path': gt_path,
'loc_left_eye': loc_left_eye,
'loc_right_eye': loc_right_eye,
'loc_mouth': loc_mouth
}
return return_dict
else:
return {'lq': img_lq, 'gt': img_gt, 'gt_path': gt_path}
def __len__(self):
return len(self.paths)12345678910import importlib
from basicsr.utils import scandir
from os import path as osp
# automatically scan and import model modules for registry
# scan all the files that end with '_model.py' under the model folder
model_folder = osp.dirname(osp.abspath(__file__))
model_filenames = [osp.splitext(osp.basename(v))[0] for v in scandir(model_folder) if v.endswith('_model.py')]
# import all the model modules
_model_modules = [importlib.import_module(f'gfpgan.models.{file_name}') for file_name in model_filenames]123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580import math
import os.path as osp
import torch
from basicsr.archs import build_network
from basicsr.losses import build_loss
# from basicsr.losses.losses import r1_penalty
from basicsr.losses import r1_penalty
from basicsr.metrics import calculate_metric
from basicsr.models.base_model import BaseModel
from basicsr.utils import get_root_logger, imwrite, tensor2img
from basicsr.utils.registry import MODEL_REGISTRY
from collections import OrderedDict
from torch.nn import functional as F
from torchvision.ops import roi_align
from tqdm import tqdm
@MODEL_REGISTRY.register()
class GFPGANModel(BaseModel):
"""The GFPGAN model for Towards real-world blind face restoratin with generative facial prior"""
def __init__(self, opt):
super(GFPGANModel, self).__init__(opt)
self.idx = 0 # it is used for saving data for check
# define network
self.net_g = build_network(opt['network_g'])
self.net_g = self.model_to_device(self.net_g)
self.print_network(self.net_g)
# load pretrained model
load_path = self.opt['path'].get('pretrain_network_g', None)
if load_path is not None:
param_key = self.opt['path'].get('param_key_g', 'params')
self.load_network(self.net_g, load_path, self.opt['path'].get('strict_load_g', True), param_key)
self.log_size = int(math.log(self.opt['network_g']['out_size'], 2))
if self.is_train:
self.init_training_settings()
def init_training_settings(self):
train_opt = self.opt['train']
# ----------- define net_d ----------- #
self.net_d = build_network(self.opt['network_d'])
self.net_d = self.model_to_device(self.net_d)
self.print_network(self.net_d)
# load pretrained model
load_path = self.opt['path'].get('pretrain_network_d', None)
if load_path is not None:
self.load_network(self.net_d, load_path, self.opt['path'].get('strict_load_d', True))
# ----------- define net_g with Exponential Moving Average (EMA) ----------- #
# net_g_ema only used for testing on one GPU and saving. There is no need to wrap with DistributedDataParallel
self.net_g_ema = build_network(self.opt['network_g']).to(self.device)
# load pretrained model
load_path = self.opt['path'].get('pretrain_network_g', None)
if load_path is not None:
self.load_network(self.net_g_ema, load_path, self.opt['path'].get('strict_load_g', True), 'params_ema')
else:
self.model_ema(0) # copy net_g weight
self.net_g.train()
self.net_d.train()
self.net_g_ema.eval()
# ----------- facial component networks ----------- #
if ('network_d_left_eye' in self.opt and 'network_d_right_eye' in self.opt and 'network_d_mouth' in self.opt):
self.use_facial_disc = True
else:
self.use_facial_disc = False
if self.use_facial_disc:
# left eye
self.net_d_left_eye = build_network(self.opt['network_d_left_eye'])
self.net_d_left_eye = self.model_to_device(self.net_d_left_eye)
self.print_network(self.net_d_left_eye)
load_path = self.opt['path'].get('pretrain_network_d_left_eye')
if load_path is not None:
self.load_network(self.net_d_left_eye, load_path, True, 'params')
# right eye
self.net_d_right_eye = build_network(self.opt['network_d_right_eye'])
self.net_d_right_eye = self.model_to_device(self.net_d_right_eye)
self.print_network(self.net_d_right_eye)
load_path = self.opt['path'].get('pretrain_network_d_right_eye')
if load_path is not None:
self.load_network(self.net_d_right_eye, load_path, True, 'params')
# mouth
self.net_d_mouth = build_network(self.opt['network_d_mouth'])
self.net_d_mouth = self.model_to_device(self.net_d_mouth)
self.print_network(self.net_d_mouth)
load_path = self.opt['path'].get('pretrain_network_d_mouth')
if load_path is not None:
self.load_network(self.net_d_mouth, load_path, True, 'params')
self.net_d_left_eye.train()
self.net_d_right_eye.train()
self.net_d_mouth.train()
# ----------- define facial component gan loss ----------- #
self.cri_component = build_loss(train_opt['gan_component_opt']).to(self.device)
# ----------- define losses ----------- #
# pixel loss
if train_opt.get('pixel_opt'):
self.cri_pix = build_loss(train_opt['pixel_opt']).to(self.device)
else:
self.cri_pix = None
# perceptual loss
if train_opt.get('perceptual_opt'):
self.cri_perceptual = build_loss(train_opt['perceptual_opt']).to(self.device)
else:
self.cri_perceptual = None
# L1 loss is used in pyramid loss, component style loss and identity loss
self.cri_l1 = build_loss(train_opt['L1_opt']).to(self.device)
# gan loss (wgan)
self.cri_gan = build_loss(train_opt['gan_opt']).to(self.device)
# ----------- define identity loss ----------- #
if 'network_identity' in self.opt:
self.use_identity = True
else:
self.use_identity = False
if self.use_identity:
# define identity network
self.network_identity = build_network(self.opt['network_identity'])
self.network_identity = self.model_to_device(self.network_identity)
self.print_network(self.network_identity)
load_path = self.opt['path'].get('pretrain_network_identity')
if load_path is not None:
self.load_network(self.network_identity, load_path, True, None)
self.network_identity.eval()
for param in self.network_identity.parameters():
param.requires_grad = False
# regularization weights
self.r1_reg_weight = train_opt['r1_reg_weight'] # for discriminator
self.net_d_iters = train_opt.get('net_d_iters', 1)
self.net_d_init_iters = train_opt.get('net_d_init_iters', 0)
self.net_d_reg_every = train_opt['net_d_reg_every']
# set up optimizers and schedulers
self.setup_optimizers()
self.setup_schedulers()
def setup_optimizers(self):
train_opt = self.opt['train']
# ----------- optimizer g ----------- #
net_g_reg_ratio = 1
normal_params = []
for _, param in self.net_g.named_parameters():
normal_params.append(param)
optim_params_g = [{ # add normal params first
'params': normal_params,
'lr': train_opt['optim_g']['lr']
}]
optim_type = train_opt['optim_g'].pop('type')
lr = train_opt['optim_g']['lr'] * net_g_reg_ratio
betas = (0**net_g_reg_ratio, 0.99**net_g_reg_ratio)
self.optimizer_g = self.get_optimizer(optim_type, optim_params_g, lr, betas=betas)
self.optimizers.append(self.optimizer_g)
# ----------- optimizer d ----------- #
net_d_reg_ratio = self.net_d_reg_every / (self.net_d_reg_every + 1)
normal_params = []
for _, param in self.net_d.named_parameters():
normal_params.append(param)
optim_params_d = [{ # add normal params first
'params': normal_params,
'lr': train_opt['optim_d']['lr']
}]
optim_type = train_opt['optim_d'].pop('type')
lr = train_opt['optim_d']['lr'] * net_d_reg_ratio
betas = (0**net_d_reg_ratio, 0.99**net_d_reg_ratio)
self.optimizer_d = self.get_optimizer(optim_type, optim_params_d, lr, betas=betas)
self.optimizers.append(self.optimizer_d)
# ----------- optimizers for facial component networks ----------- #
if self.use_facial_disc:
# setup optimizers for facial component discriminators
optim_type = train_opt['optim_component'].pop('type')
lr = train_opt['optim_component']['lr']
# left eye
self.optimizer_d_left_eye = self.get_optimizer(
optim_type, self.net_d_left_eye.parameters(), lr, betas=(0.9, 0.99))
self.optimizers.append(self.optimizer_d_left_eye)
# right eye
self.optimizer_d_right_eye = self.get_optimizer(
optim_type, self.net_d_right_eye.parameters(), lr, betas=(0.9, 0.99))
self.optimizers.append(self.optimizer_d_right_eye)
# mouth
self.optimizer_d_mouth = self.get_optimizer(
optim_type, self.net_d_mouth.parameters(), lr, betas=(0.9, 0.99))
self.optimizers.append(self.optimizer_d_mouth)
def feed_data(self, data):
self.lq = data['lq'].to(self.device)
if 'gt' in data:
self.gt = data['gt'].to(self.device)
if 'loc_left_eye' in data:
# get facial component locations, shape (batch, 4)
self.loc_left_eyes = data['loc_left_eye']
self.loc_right_eyes = data['loc_right_eye']
self.loc_mouths = data['loc_mouth']
# uncomment to check data
# import torchvision
# if self.opt['rank'] == 0:
# import os
# os.makedirs('tmp/gt', exist_ok=True)
# os.makedirs('tmp/lq', exist_ok=True)
# print(self.idx)
# torchvision.utils.save_image(
# self.gt, f'tmp/gt/gt_{self.idx}.png', nrow=4, padding=2, normalize=True, range=(-1, 1))
# torchvision.utils.save_image(
# self.lq, f'tmp/lq/lq{self.idx}.png', nrow=4, padding=2, normalize=True, range=(-1, 1))
# self.idx = self.idx + 1
def construct_img_pyramid(self):
"""Construct image pyramid for intermediate restoration loss"""
pyramid_gt = [self.gt]
down_img = self.gt
for _ in range(0, self.log_size - 3):
down_img = F.interpolate(down_img, scale_factor=0.5, mode='bilinear', align_corners=False)
pyramid_gt.insert(0, down_img)
return pyramid_gt
def get_roi_regions(self, eye_out_size=80, mouth_out_size=120):
face_ratio = int(self.opt['network_g']['out_size'] / 512)
eye_out_size *= face_ratio
mouth_out_size *= face_ratio
rois_eyes = []
rois_mouths = []
for b in range(self.loc_left_eyes.size(0)): # loop for batch size
# left eye and right eye
img_inds = self.loc_left_eyes.new_full((2, 1), b)
bbox = torch.stack([self.loc_left_eyes[b, :], self.loc_right_eyes[b, :]], dim=0) # shape: (2, 4)
rois = torch.cat([img_inds, bbox], dim=-1) # shape: (2, 5)
rois_eyes.append(rois)
# mouse
img_inds = self.loc_left_eyes.new_full((1, 1), b)
rois = torch.cat([img_inds, self.loc_mouths[b:b + 1, :]], dim=-1) # shape: (1, 5)
rois_mouths.append(rois)
rois_eyes = torch.cat(rois_eyes, 0).to(self.device)
rois_mouths = torch.cat(rois_mouths, 0).to(self.device)
# real images
all_eyes = roi_align(self.gt, boxes=rois_eyes, output_size=eye_out_size) * face_ratio
self.left_eyes_gt = all_eyes[0::2, :, :, :]
self.right_eyes_gt = all_eyes[1::2, :, :, :]
self.mouths_gt = roi_align(self.gt, boxes=rois_mouths, output_size=mouth_out_size) * face_ratio
# output
all_eyes = roi_align(self.output, boxes=rois_eyes, output_size=eye_out_size) * face_ratio
self.left_eyes = all_eyes[0::2, :, :, :]
self.right_eyes = all_eyes[1::2, :, :, :]
self.mouths = roi_align(self.output, boxes=rois_mouths, output_size=mouth_out_size) * face_ratio
def _gram_mat(self, x):
"""Calculate Gram matrix.
Args:
x (torch.Tensor): Tensor with shape of (n, c, h, w).
Returns:
torch.Tensor: Gram matrix.
"""
n, c, h, w = x.size()
features = x.view(n, c, w * h)
features_t = features.transpose(1, 2)
gram = features.bmm(features_t) / (c * h * w)
return gram
def gray_resize_for_identity(self, out, size=128):
out_gray = (0.2989 * out[:, 0, :, :] + 0.5870 * out[:, 1, :, :] + 0.1140 * out[:, 2, :, :])
out_gray = out_gray.unsqueeze(1)
out_gray = F.interpolate(out_gray, (size, size), mode='bilinear', align_corners=False)
return out_gray
def optimize_parameters(self, current_iter):
# optimize net_g
for p in self.net_d.parameters():
p.requires_grad = False
self.optimizer_g.zero_grad()
# do not update facial component net_d
if self.use_facial_disc:
for p in self.net_d_left_eye.parameters():
p.requires_grad = False
for p in self.net_d_right_eye.parameters():
p.requires_grad = False
for p in self.net_d_mouth.parameters():
p.requires_grad = False
# image pyramid loss weight
pyramid_loss_weight = self.opt['train'].get('pyramid_loss_weight', 0)
if pyramid_loss_weight > 0 and current_iter > self.opt['train'].get('remove_pyramid_loss', float('inf')):
pyramid_loss_weight = 1e-12 # very small weight to avoid unused param error
if pyramid_loss_weight > 0:
self.output, out_rgbs = self.net_g(self.lq, return_rgb=True)
pyramid_gt = self.construct_img_pyramid()
else:
self.output, out_rgbs = self.net_g(self.lq, return_rgb=False)
# get roi-align regions
if self.use_facial_disc:
self.get_roi_regions(eye_out_size=80, mouth_out_size=120)
l_g_total = 0
loss_dict = OrderedDict()
if (current_iter % self.net_d_iters == 0 and current_iter > self.net_d_init_iters):
# pixel loss
if self.cri_pix:
l_g_pix = self.cri_pix(self.output, self.gt)
l_g_total += l_g_pix
loss_dict['l_g_pix'] = l_g_pix
# image pyramid loss
if pyramid_loss_weight > 0:
for i in range(0, self.log_size - 2):
l_pyramid = self.cri_l1(out_rgbs[i], pyramid_gt[i]) * pyramid_loss_weight
l_g_total += l_pyramid
loss_dict[f'l_p_{2**(i+3)}'] = l_pyramid
# perceptual loss
if self.cri_perceptual:
l_g_percep, l_g_style = self.cri_perceptual(self.output, self.gt)
if l_g_percep is not None:
l_g_total += l_g_percep
loss_dict['l_g_percep'] = l_g_percep
if l_g_style is not None:
l_g_total += l_g_style
loss_dict['l_g_style'] = l_g_style
# gan loss
fake_g_pred = self.net_d(self.output)
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
l_g_total += l_g_gan
loss_dict['l_g_gan'] = l_g_gan
# facial component loss
if self.use_facial_disc:
# left eye
fake_left_eye, fake_left_eye_feats = self.net_d_left_eye(self.left_eyes, return_feats=True)
l_g_gan = self.cri_component(fake_left_eye, True, is_disc=False)
l_g_total += l_g_gan
loss_dict['l_g_gan_left_eye'] = l_g_gan
# right eye
fake_right_eye, fake_right_eye_feats = self.net_d_right_eye(self.right_eyes, return_feats=True)
l_g_gan = self.cri_component(fake_right_eye, True, is_disc=False)
l_g_total += l_g_gan
loss_dict['l_g_gan_right_eye'] = l_g_gan
# mouth
fake_mouth, fake_mouth_feats = self.net_d_mouth(self.mouths, return_feats=True)
l_g_gan = self.cri_component(fake_mouth, True, is_disc=False)
l_g_total += l_g_gan
loss_dict['l_g_gan_mouth'] = l_g_gan
if self.opt['train'].get('comp_style_weight', 0) > 0:
# get gt feat
_, real_left_eye_feats = self.net_d_left_eye(self.left_eyes_gt, return_feats=True)
_, real_right_eye_feats = self.net_d_right_eye(self.right_eyes_gt, return_feats=True)
_, real_mouth_feats = self.net_d_mouth(self.mouths_gt, return_feats=True)
def _comp_style(feat, feat_gt, criterion):
return criterion(self._gram_mat(feat[0]), self._gram_mat(
feat_gt[0].detach())) * 0.5 + criterion(
self._gram_mat(feat[1]), self._gram_mat(feat_gt[1].detach()))
# facial component style loss
comp_style_loss = 0
comp_style_loss += _comp_style(fake_left_eye_feats, real_left_eye_feats, self.cri_l1)
comp_style_loss += _comp_style(fake_right_eye_feats, real_right_eye_feats, self.cri_l1)
comp_style_loss += _comp_style(fake_mouth_feats, real_mouth_feats, self.cri_l1)
comp_style_loss = comp_style_loss * self.opt['train']['comp_style_weight']
l_g_total += comp_style_loss
loss_dict['l_g_comp_style_loss'] = comp_style_loss
# identity loss
if self.use_identity:
identity_weight = self.opt['train']['identity_weight']
# get gray images and resize
out_gray = self.gray_resize_for_identity(self.output)
gt_gray = self.gray_resize_for_identity(self.gt)
identity_gt = self.network_identity(gt_gray).detach()
identity_out = self.network_identity(out_gray)
l_identity = self.cri_l1(identity_out, identity_gt) * identity_weight
l_g_total += l_identity
loss_dict['l_identity'] = l_identity
l_g_total.backward()
self.optimizer_g.step()
# EMA
self.model_ema(decay=0.5**(32 / (10 * 1000)))
# ----------- optimize net_d ----------- #
for p in self.net_d.parameters():
p.requires_grad = True
self.optimizer_d.zero_grad()
if self.use_facial_disc:
for p in self.net_d_left_eye.parameters():
p.requires_grad = True
for p in self.net_d_right_eye.parameters():
p.requires_grad = True
for p in self.net_d_mouth.parameters():
p.requires_grad = True
self.optimizer_d_left_eye.zero_grad()
self.optimizer_d_right_eye.zero_grad()
self.optimizer_d_mouth.zero_grad()
fake_d_pred = self.net_d(self.output.detach())
real_d_pred = self.net_d(self.gt)
l_d = self.cri_gan(real_d_pred, True, is_disc=True) + self.cri_gan(fake_d_pred, False, is_disc=True)
loss_dict['l_d'] = l_d
# In WGAN, real_score should be positive and fake_score should be negative
loss_dict['real_score'] = real_d_pred.detach().mean()
loss_dict['fake_score'] = fake_d_pred.detach().mean()
l_d.backward()
# regularization loss
if current_iter % self.net_d_reg_every == 0:
self.gt.requires_grad = True
real_pred = self.net_d(self.gt)
l_d_r1 = r1_penalty(real_pred, self.gt)
l_d_r1 = (self.r1_reg_weight / 2 * l_d_r1 * self.net_d_reg_every + 0 * real_pred[0])
loss_dict['l_d_r1'] = l_d_r1.detach().mean()
l_d_r1.backward()
self.optimizer_d.step()
# optimize facial component discriminators
if self.use_facial_disc:
# left eye
fake_d_pred, _ = self.net_d_left_eye(self.left_eyes.detach())
real_d_pred, _ = self.net_d_left_eye(self.left_eyes_gt)
l_d_left_eye = self.cri_component(
real_d_pred, True, is_disc=True) + self.cri_gan(
fake_d_pred, False, is_disc=True)
loss_dict['l_d_left_eye'] = l_d_left_eye
l_d_left_eye.backward()
# right eye
fake_d_pred, _ = self.net_d_right_eye(self.right_eyes.detach())
real_d_pred, _ = self.net_d_right_eye(self.right_eyes_gt)
l_d_right_eye = self.cri_component(
real_d_pred, True, is_disc=True) + self.cri_gan(
fake_d_pred, False, is_disc=True)
loss_dict['l_d_right_eye'] = l_d_right_eye
l_d_right_eye.backward()
# mouth
fake_d_pred, _ = self.net_d_mouth(self.mouths.detach())
real_d_pred, _ = self.net_d_mouth(self.mouths_gt)
l_d_mouth = self.cri_component(
real_d_pred, True, is_disc=True) + self.cri_gan(
fake_d_pred, False, is_disc=True)
loss_dict['l_d_mouth'] = l_d_mouth
l_d_mouth.backward()
self.optimizer_d_left_eye.step()
self.optimizer_d_right_eye.step()
self.optimizer_d_mouth.step()
self.log_dict = self.reduce_loss_dict(loss_dict)
def test(self):
with torch.no_grad():
if hasattr(self, 'net_g_ema'):
self.net_g_ema.eval()
self.output, _ = self.net_g_ema(self.lq)
else:
logger = get_root_logger()
logger.warning('Do not have self.net_g_ema, use self.net_g.')
self.net_g.eval()
self.output, _ = self.net_g(self.lq)
self.net_g.train()
def dist_validation(self, dataloader, current_iter, tb_logger, save_img):
if self.opt['rank'] == 0:
self.nondist_validation(dataloader, current_iter, tb_logger, save_img)
def nondist_validation(self, dataloader, current_iter, tb_logger, save_img):
dataset_name = dataloader.dataset.opt['name']
with_metrics = self.opt['val'].get('metrics') is not None
use_pbar = self.opt['val'].get('pbar', False)
if with_metrics:
if not hasattr(self, 'metric_results'): # only execute in the first run
self.metric_results = {metric: 0 for metric in self.opt['val']['metrics'].keys()}
# initialize the best metric results for each dataset_name (supporting multiple validation datasets)
self._initialize_best_metric_results(dataset_name)
# zero self.metric_results
self.metric_results = {metric: 0 for metric in self.metric_results}
metric_data = dict()
if use_pbar:
pbar = tqdm(total=len(dataloader), unit='image')
for idx, val_data in enumerate(dataloader):
img_name = osp.splitext(osp.basename(val_data['lq_path'][0]))[0]
self.feed_data(val_data)
self.test()
sr_img = tensor2img(self.output.detach().cpu(), min_max=(-1, 1))
metric_data['img'] = sr_img
if hasattr(self, 'gt'):
gt_img = tensor2img(self.gt.detach().cpu(), min_max=(-1, 1))
metric_data['img2'] = gt_img
del self.gt
# tentative for out of GPU memory
del self.lq
del self.output
torch.cuda.empty_cache()
if save_img:
if self.opt['is_train']:
save_img_path = osp.join(self.opt['path']['visualization'], img_name,
f'{img_name}_{current_iter}.png')
else:
if self.opt['val']['suffix']:
save_img_path = osp.join(self.opt['path']['visualization'], dataset_name,
f'{img_name}_{self.opt["val"]["suffix"]}.png')
else:
save_img_path = osp.join(self.opt['path']['visualization'], dataset_name,
f'{img_name}_{self.opt["name"]}.png')
imwrite(sr_img, save_img_path)
if with_metrics:
# calculate metrics
for name, opt_ in self.opt['val']['metrics'].items():
self.metric_results[name] += calculate_metric(metric_data, opt_)
if use_pbar:
pbar.update(1)
pbar.set_description(f'Test {img_name}')
if use_pbar:
pbar.close()
if with_metrics:
for metric in self.metric_results.keys():
self.metric_results[metric] /= (idx + 1)
# update the best metric result
self._update_best_metric_result(dataset_name, metric, self.metric_results[metric], current_iter)
self._log_validation_metric_values(current_iter, dataset_name, tb_logger)
def _log_validation_metric_values(self, current_iter, dataset_name, tb_logger):
log_str = f'Validation {dataset_name}\n'
for metric, value in self.metric_results.items():
log_str += f'\t # {metric}: {value:.4f}'
if hasattr(self, 'best_metric_results'):
log_str += (f'\tBest: {self.best_metric_results[dataset_name][metric]["val"]:.4f} @ '
f'{self.best_metric_results[dataset_name][metric]["iter"]} iter')
log_str += '\n'
logger = get_root_logger()
logger.info(log_str)
if tb_logger:
for metric, value in self.metric_results.items():
tb_logger.add_scalar(f'metrics/{dataset_name}/{metric}', value, current_iter)
def save(self, epoch, current_iter):
# save net_g and net_d
self.save_network([self.net_g, self.net_g_ema], 'net_g', current_iter, param_key=['params', 'params_ema'])
self.save_network(self.net_d, 'net_d', current_iter)
# save component discriminators
if self.use_facial_disc:
self.save_network(self.net_d_left_eye, 'net_d_left_eye', current_iter)
self.save_network(self.net_d_right_eye, 'net_d_right_eye', current_iter)
self.save_network(self.net_d_mouth, 'net_d_mouth', current_iter)
# save training state
self.save_training_state(epoch, current_iter)1234567891011# flake8: noqa
import os.path as osp
from basicsr.train import train_pipeline
import gfpgan.archs
import gfpgan.data
import gfpgan.models
if __name__ == '__main__':
root_path = osp.abspath(osp.join(__file__, osp.pardir, osp.pardir))
train_pipeline(root_path)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143import cv2
import os
import torch
from basicsr.utils import img2tensor, tensor2img
from basicsr.utils.download_util import load_file_from_url
from facexlib.utils.face_restoration_helper import FaceRestoreHelper
from torchvision.transforms.functional import normalize
from gfpgan.archs.gfpgan_bilinear_arch import GFPGANBilinear
from gfpgan.archs.gfpganv1_arch import GFPGANv1
from gfpgan.archs.gfpganv1_clean_arch import GFPGANv1Clean
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
class GFPGANer():
"""Helper for restoration with GFPGAN.
It will detect and crop faces, and then resize the faces to 512x512.
GFPGAN is used to restored the resized faces.
The background is upsampled with the bg_upsampler.
Finally, the faces will be pasted back to the upsample background image.
Args:
model_path (str): The path to the GFPGAN model. It can be urls (will first download it automatically).
upscale (float): The upscale of the final output. Default: 2.
arch (str): The GFPGAN architecture. Option: clean | original. Default: clean.
channel_multiplier (int): Channel multiplier for large networks of StyleGAN2. Default: 2.
bg_upsampler (nn.Module): The upsampler for the background. Default: None.
"""
def __init__(self, model_path, upscale=2, arch='clean', channel_multiplier=2, bg_upsampler=None):
self.upscale = upscale
self.bg_upsampler = bg_upsampler
# initialize model
self.device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# initialize the GFP-GAN
if arch == 'clean':
self.gfpgan = GFPGANv1Clean(
out_size=512,
num_style_feat=512,
channel_multiplier=channel_multiplier,
decoder_load_path=None,
fix_decoder=False,
num_mlp=8,
input_is_latent=True,
different_w=True,
narrow=1,
sft_half=True)
elif arch == 'bilinear':
self.gfpgan = GFPGANBilinear(
out_size=512,
num_style_feat=512,
channel_multiplier=channel_multiplier,
decoder_load_path=None,
fix_decoder=False,
num_mlp=8,
input_is_latent=True,
different_w=True,
narrow=1,
sft_half=True)
elif arch == 'original':
self.gfpgan = GFPGANv1(
out_size=512,
num_style_feat=512,
channel_multiplier=channel_multiplier,
decoder_load_path=None,
fix_decoder=True,
num_mlp=8,
input_is_latent=True,
different_w=True,
narrow=1,
sft_half=True)
# initialize face helper
self.face_helper = FaceRestoreHelper(
upscale,
face_size=512,
crop_ratio=(1, 1),
det_model='retinaface_resnet50',
save_ext='png',
device=self.device)
if model_path.startswith('https://'):
model_path = load_file_from_url(
url=model_path, model_dir=os.path.join(ROOT_DIR, 'gfpgan/weights'), progress=True, file_name=None)
loadnet = torch.load(model_path)
if 'params_ema' in loadnet:
keyname = 'params_ema'
else:
keyname = 'params'
self.gfpgan.load_state_dict(loadnet[keyname], strict=True)
self.gfpgan.eval()
self.gfpgan = self.gfpgan.to(self.device)
@torch.no_grad()
def enhance(self, img, has_aligned=False, only_center_face=False, paste_back=True):
self.face_helper.clean_all()
if has_aligned: # the inputs are already aligned
img = cv2.resize(img, (512, 512))
self.face_helper.cropped_faces = [img]
else:
self.face_helper.read_image(img)
# get face landmarks for each face
self.face_helper.get_face_landmarks_5(only_center_face=only_center_face, eye_dist_threshold=5)
# eye_dist_threshold=5: skip faces whose eye distance is smaller than 5 pixels
# TODO: even with eye_dist_threshold, it will still introduce wrong detections and restorations.
# align and warp each face
self.face_helper.align_warp_face()
# face restoration
for cropped_face in self.face_helper.cropped_faces:
# prepare data
cropped_face_t = img2tensor(cropped_face / 255., bgr2rgb=True, float32=True)
normalize(cropped_face_t, (0.5, 0.5, 0.5), (0.5, 0.5, 0.5), inplace=True)
cropped_face_t = cropped_face_t.unsqueeze(0).to(self.device)
try:
output = self.gfpgan(cropped_face_t, return_rgb=False)[0]
# convert to image
restored_face = tensor2img(output.squeeze(0), rgb2bgr=True, min_max=(-1, 1))
except RuntimeError as error:
print(f'\tFailed inference for GFPGAN: {error}.')
restored_face = cropped_face
restored_face = restored_face.astype('uint8')
self.face_helper.add_restored_face(restored_face)
if not has_aligned and paste_back:
# upsample the background
if self.bg_upsampler is not None:
# Now only support RealESRGAN for upsampling background
bg_img = self.bg_upsampler.enhance(img, outscale=self.upscale)[0]
else:
bg_img = None
self.face_helper.get_inverse_affine(None)
# paste each restored face to the input image
restored_img = self.face_helper.paste_faces_to_input_image(upsample_img=bg_img)
return self.face_helper.cropped_faces, self.face_helper.restored_faces, restored_img
else:
return self.face_helper.cropped_faces, self.face_helper.restored_faces, None12345# GENERATED VERSION FILE
# TIME: Wed Apr 20 14:43:06 2022
__version__ = '1.3.2'
__gitsha__ = '924ce47'
version_info = (1, 3, 2)123# Weights
Put the downloaded weights to this folder.123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216# general settings
name: train_GFPGANv1_512
model_type: GFPGANModel
num_gpu: auto # officially, we use 4 GPUs
manual_seed: 0
# dataset and data loader settings
datasets:
train:
name: FFHQ
type: FFHQDegradationDataset
# dataroot_gt: datasets/ffhq/ffhq_512.lmdb
dataroot_gt: datasets/ffhq/ffhq_512
io_backend:
# type: lmdb
type: disk
use_hflip: true
mean: [0.5, 0.5, 0.5]
std: [0.5, 0.5, 0.5]
out_size: 512
blur_kernel_size: 41
kernel_list: ['iso', 'aniso']
kernel_prob: [0.5, 0.5]
blur_sigma: [0.1, 10]
downsample_range: [0.8, 8]
noise_range: [0, 20]
jpeg_range: [60, 100]
# color jitter and gray
color_jitter_prob: 0.3
color_jitter_shift: 20
color_jitter_pt_prob: 0.3
gray_prob: 0.01
# If you do not want colorization, please set
# color_jitter_prob: ~
# color_jitter_pt_prob: ~
# gray_prob: 0.01
# gt_gray: True
crop_components: true
component_path: experiments/pretrained_models/FFHQ_eye_mouth_landmarks_512.pth
eye_enlarge_ratio: 1.4
# data loader
use_shuffle: true
num_worker_per_gpu: 6
batch_size_per_gpu: 3
dataset_enlarge_ratio: 1
prefetch_mode: ~
val:
# Please modify accordingly to use your own validation
# Or comment the val block if do not need validation during training
name: validation
type: PairedImageDataset
dataroot_lq: datasets/faces/validation/input
dataroot_gt: datasets/faces/validation/reference
io_backend:
type: disk
mean: [0.5, 0.5, 0.5]
std: [0.5, 0.5, 0.5]
scale: 1
# network structures
network_g:
type: GFPGANv1
out_size: 512
num_style_feat: 512
channel_multiplier: 1
resample_kernel: [1, 3, 3, 1]
decoder_load_path: experiments/pretrained_models/StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
fix_decoder: true
num_mlp: 8
lr_mlp: 0.01
input_is_latent: true
different_w: true
narrow: 1
sft_half: true
network_d:
type: StyleGAN2Discriminator
out_size: 512
channel_multiplier: 1
resample_kernel: [1, 3, 3, 1]
network_d_left_eye:
type: FacialComponentDiscriminator
network_d_right_eye:
type: FacialComponentDiscriminator
network_d_mouth:
type: FacialComponentDiscriminator
network_identity:
type: ResNetArcFace
block: IRBlock
layers: [2, 2, 2, 2]
use_se: False
# path
path:
pretrain_network_g: ~
param_key_g: params_ema
strict_load_g: ~
pretrain_network_d: ~
pretrain_network_d_left_eye: ~
pretrain_network_d_right_eye: ~
pretrain_network_d_mouth: ~
pretrain_network_identity: experiments/pretrained_models/arcface_resnet18.pth
# resume
resume_state: ~
ignore_resume_networks: ['network_identity']
# training settings
train:
optim_g:
type: Adam
lr: !!float 2e-3
optim_d:
type: Adam
lr: !!float 2e-3
optim_component:
type: Adam
lr: !!float 2e-3
scheduler:
type: MultiStepLR
milestones: [600000, 700000]
gamma: 0.5
total_iter: 800000
warmup_iter: -1 # no warm up
# losses
# pixel loss
pixel_opt:
type: L1Loss
loss_weight: !!float 1e-1
reduction: mean
# L1 loss used in pyramid loss, component style loss and identity loss
L1_opt:
type: L1Loss
loss_weight: 1
reduction: mean
# image pyramid loss
pyramid_loss_weight: 1
remove_pyramid_loss: 50000
# perceptual loss (content and style losses)
perceptual_opt:
type: PerceptualLoss
layer_weights:
# before relu
'conv1_2': 0.1
'conv2_2': 0.1
'conv3_4': 1
'conv4_4': 1
'conv5_4': 1
vgg_type: vgg19
use_input_norm: true
perceptual_weight: !!float 1
style_weight: 50
range_norm: true
criterion: l1
# gan loss
gan_opt:
type: GANLoss
gan_type: wgan_softplus
loss_weight: !!float 1e-1
# r1 regularization for discriminator
r1_reg_weight: 10
# facial component loss
gan_component_opt:
type: GANLoss
gan_type: vanilla
real_label_val: 1.0
fake_label_val: 0.0
loss_weight: !!float 1
comp_style_weight: 200
# identity loss
identity_weight: 10
net_d_iters: 1
net_d_init_iters: 0
net_d_reg_every: 16
# validation settings
val:
val_freq: !!float 5e3
save_img: true
metrics:
psnr: # metric name
type: calculate_psnr
crop_border: 0
test_y_channel: false
# logging settings
logger:
print_freq: 100
save_checkpoint_freq: !!float 5e3
use_tb_logger: true
wandb:
project: ~
resume_id: ~
# dist training settings
dist_params:
backend: nccl
port: 29500
find_unused_parameters: true123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182# general settings
name: train_GFPGANv1_512_simple
model_type: GFPGANModel
num_gpu: auto # officially, we use 4 GPUs
manual_seed: 0
# dataset and data loader settings
datasets:
train:
name: FFHQ
type: FFHQDegradationDataset
# dataroot_gt: datasets/ffhq/ffhq_512.lmdb
dataroot_gt: datasets/ffhq/ffhq_512
io_backend:
# type: lmdb
type: disk
use_hflip: true
mean: [0.5, 0.5, 0.5]
std: [0.5, 0.5, 0.5]
out_size: 512
blur_kernel_size: 41
kernel_list: ['iso', 'aniso']
kernel_prob: [0.5, 0.5]
blur_sigma: [0.1, 10]
downsample_range: [0.8, 8]
noise_range: [0, 20]
jpeg_range: [60, 100]
# color jitter and gray
color_jitter_prob: 0.3
color_jitter_shift: 20
color_jitter_pt_prob: 0.3
gray_prob: 0.01
# If you do not want colorization, please set
# color_jitter_prob: ~
# color_jitter_pt_prob: ~
# gray_prob: 0.01
# gt_gray: True
# data loader
use_shuffle: true
num_worker_per_gpu: 6
batch_size_per_gpu: 3
dataset_enlarge_ratio: 1
prefetch_mode: ~
val:
# Please modify accordingly to use your own validation
# Or comment the val block if do not need validation during training
name: validation
type: PairedImageDataset
dataroot_lq: datasets/faces/validation/input
dataroot_gt: datasets/faces/validation/reference
io_backend:
type: disk
mean: [0.5, 0.5, 0.5]
std: [0.5, 0.5, 0.5]
scale: 1
# network structures
network_g:
type: GFPGANv1
out_size: 512
num_style_feat: 512
channel_multiplier: 1
resample_kernel: [1, 3, 3, 1]
decoder_load_path: experiments/pretrained_models/StyleGAN2_512_Cmul1_FFHQ_B12G4_scratch_800k.pth
fix_decoder: true
num_mlp: 8
lr_mlp: 0.01
input_is_latent: true
different_w: true
narrow: 1
sft_half: true
network_d:
type: StyleGAN2Discriminator
out_size: 512
channel_multiplier: 1
resample_kernel: [1, 3, 3, 1]
# path
path:
pretrain_network_g: ~
param_key_g: params_ema
strict_load_g: ~
pretrain_network_d: ~
resume_state: ~
# training settings
train:
optim_g:
type: Adam
lr: !!float 2e-3
optim_d:
type: Adam
lr: !!float 2e-3
optim_component:
type: Adam
lr: !!float 2e-3
scheduler:
type: MultiStepLR
milestones: [600000, 700000]
gamma: 0.5
total_iter: 800000
warmup_iter: -1 # no warm up
# losses
# pixel loss
pixel_opt:
type: L1Loss
loss_weight: !!float 1e-1
reduction: mean
# L1 loss used in pyramid loss, component style loss and identity loss
L1_opt:
type: L1Loss
loss_weight: 1
reduction: mean
# image pyramid loss
pyramid_loss_weight: 1
remove_pyramid_loss: 50000
# perceptual loss (content and style losses)
perceptual_opt:
type: PerceptualLoss
layer_weights:
# before relu
'conv1_2': 0.1
'conv2_2': 0.1
'conv3_4': 1
'conv4_4': 1
'conv5_4': 1
vgg_type: vgg19
use_input_norm: true
perceptual_weight: !!float 1
style_weight: 50
range_norm: true
criterion: l1
# gan loss
gan_opt:
type: GANLoss
gan_type: wgan_softplus
loss_weight: !!float 1e-1
# r1 regularization for discriminator
r1_reg_weight: 10
net_d_iters: 1
net_d_init_iters: 0
net_d_reg_every: 16
# validation settings
val:
val_freq: !!float 5e3
save_img: true
metrics:
psnr: # metric name
type: calculate_psnr
crop_border: 0
test_y_channel: false
# logging settings
logger:
print_freq: 100
save_checkpoint_freq: !!float 5e3
use_tb_logger: true
wandb:
project: ~
resume_id: ~
# dist training settings
dist_params:
backend: nccl
port: 29500
find_unused_parameters: true123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271# -*- coding: utf-8 -*-
"""
Created on Mon Apr 24 15:43:29 2017
@author: zhaoy
"""
"""
@Modified by yangxy (yangtao9009@gmail.com)
"""
import cv2
import numpy as np
from skimage import transform as trans
# reference facial points, a list of coordinates (x,y)
REFERENCE_FACIAL_POINTS = [
[30.29459953, 51.69630051],
[65.53179932, 51.50139999],
[48.02519989, 71.73660278],
[33.54930115, 92.3655014],
[62.72990036, 92.20410156]
]
DEFAULT_CROP_SIZE = (96, 112)
def _umeyama(src, dst, estimate_scale=True, scale=1.0):
"""Estimate N-D similarity transformation with or without scaling.
Parameters
----------
src : (M, N) array
Source coordinates.
dst : (M, N) array
Destination coordinates.
estimate_scale : bool
Whether to estimate scaling factor.
Returns
-------
T : (N + 1, N + 1)
The homogeneous similarity transformation matrix. The matrix contains
NaN values only if the problem is not well-conditioned.
References
----------
.. [1] "Least-squares estimation of transformation parameters between two
point patterns", Shinji Umeyama, PAMI 1991, :DOI:`10.1109/34.88573`
"""
num = src.shape[0]
dim = src.shape[1]
# Compute mean of src and dst.
src_mean = src.mean(axis=0)
dst_mean = dst.mean(axis=0)
# Subtract mean from src and dst.
src_demean = src - src_mean
dst_demean = dst - dst_mean
# Eq. (38).
A = dst_demean.T @ src_demean / num
# Eq. (39).
d = np.ones((dim,), dtype=np.double)
if np.linalg.det(A) < 0:
d[dim - 1] = -1
T = np.eye(dim + 1, dtype=np.double)
U, S, V = np.linalg.svd(A)
# Eq. (40) and (43).
rank = np.linalg.matrix_rank(A)
if rank == 0:
return np.nan * T
elif rank == dim - 1:
if np.linalg.det(U) * np.linalg.det(V) > 0:
T[:dim, :dim] = U @ V
else:
s = d[dim - 1]
d[dim - 1] = -1
T[:dim, :dim] = U @ np.diag(d) @ V
d[dim - 1] = s
else:
T[:dim, :dim] = U @ np.diag(d) @ V
if estimate_scale:
# Eq. (41) and (42).
scale = 1.0 / src_demean.var(axis=0).sum() * (S @ d)
else:
scale = scale
T[:dim, dim] = dst_mean - scale * (T[:dim, :dim] @ src_mean.T)
T[:dim, :dim] *= scale
return T, scale
class FaceWarpException(Exception):
def __str__(self):
return 'In File {}:{}'.format(
__file__, super.__str__(self))
def get_reference_facial_points(output_size=None,
inner_padding_factor=0.0,
outer_padding=(0, 0),
default_square=False):
tmp_5pts = np.array(REFERENCE_FACIAL_POINTS)
tmp_crop_size = np.array(DEFAULT_CROP_SIZE)
# 0) make the inner region a square
if default_square:
size_diff = max(tmp_crop_size) - tmp_crop_size
tmp_5pts += size_diff / 2
tmp_crop_size += size_diff
if (output_size and
output_size[0] == tmp_crop_size[0] and
output_size[1] == tmp_crop_size[1]):
print('output_size == DEFAULT_CROP_SIZE {}: return default reference points'.format(tmp_crop_size))
return tmp_5pts
if (inner_padding_factor == 0 and
outer_padding == (0, 0)):
if output_size is None:
print('No paddings to do: return default reference points')
return tmp_5pts
else:
raise FaceWarpException(
'No paddings to do, output_size must be None or {}'.format(tmp_crop_size))
# check output size
if not (0 <= inner_padding_factor <= 1.0):
raise FaceWarpException('Not (0 <= inner_padding_factor <= 1.0)')
if ((inner_padding_factor > 0 or outer_padding[0] > 0 or outer_padding[1] > 0)
and output_size is None):
output_size = tmp_crop_size * \
(1 + inner_padding_factor * 2).astype(np.int32)
output_size += np.array(outer_padding)
print(' deduced from paddings, output_size = ', output_size)
if not (outer_padding[0] < output_size[0]
and outer_padding[1] < output_size[1]):
raise FaceWarpException('Not (outer_padding[0] < output_size[0]'
'and outer_padding[1] < output_size[1])')
# 1) pad the inner region according inner_padding_factor
# print('---> STEP1: pad the inner region according inner_padding_factor')
if inner_padding_factor > 0:
size_diff = tmp_crop_size * inner_padding_factor * 2
tmp_5pts += size_diff / 2
tmp_crop_size += np.round(size_diff).astype(np.int32)
# print(' crop_size = ', tmp_crop_size)
# print(' reference_5pts = ', tmp_5pts)
# 2) resize the padded inner region
# print('---> STEP2: resize the padded inner region')
size_bf_outer_pad = np.array(output_size) - np.array(outer_padding) * 2
# print(' crop_size = ', tmp_crop_size)
# print(' size_bf_outer_pad = ', size_bf_outer_pad)
if size_bf_outer_pad[0] * tmp_crop_size[1] != size_bf_outer_pad[1] * tmp_crop_size[0]:
raise FaceWarpException('Must have (output_size - outer_padding)'
'= some_scale * (crop_size * (1.0 + inner_padding_factor)')
scale_factor = size_bf_outer_pad[0].astype(np.float32) / tmp_crop_size[0]
# print(' resize scale_factor = ', scale_factor)
tmp_5pts = tmp_5pts * scale_factor
# size_diff = tmp_crop_size * (scale_factor - min(scale_factor))
# tmp_5pts = tmp_5pts + size_diff / 2
tmp_crop_size = size_bf_outer_pad
# print(' crop_size = ', tmp_crop_size)
# print(' reference_5pts = ', tmp_5pts)
# 3) add outer_padding to make output_size
reference_5point = tmp_5pts + np.array(outer_padding)
tmp_crop_size = output_size
# print('---> STEP3: add outer_padding to make output_size')
# print(' crop_size = ', tmp_crop_size)
# print(' reference_5pts = ', tmp_5pts)
#
# print('===> end get_reference_facial_points\n')
return reference_5point
def get_affine_transform_matrix(src_pts, dst_pts):
tfm = np.float32([[1, 0, 0], [0, 1, 0]])
n_pts = src_pts.shape[0]
ones = np.ones((n_pts, 1), src_pts.dtype)
src_pts_ = np.hstack([src_pts, ones])
dst_pts_ = np.hstack([dst_pts, ones])
A, res, rank, s = np.linalg.lstsq(src_pts_, dst_pts_)
if rank == 3:
tfm = np.float32([
[A[0, 0], A[1, 0], A[2, 0]],
[A[0, 1], A[1, 1], A[2, 1]]
])
elif rank == 2:
tfm = np.float32([
[A[0, 0], A[1, 0], 0],
[A[0, 1], A[1, 1], 0]
])
return tfm
def warp_and_crop_face(src_img,
facial_pts,
reference_pts=None,
crop_size=(96, 112),
align_type='smilarity'): #smilarity cv2_affine affine
if reference_pts is None:
if crop_size[0] == 96 and crop_size[1] == 112:
reference_pts = REFERENCE_FACIAL_POINTS
else:
default_square = False
inner_padding_factor = 0
outer_padding = (0, 0)
output_size = crop_size
reference_pts = get_reference_facial_points(output_size,
inner_padding_factor,
outer_padding,
default_square)
ref_pts = np.float32(reference_pts)
ref_pts_shp = ref_pts.shape
if max(ref_pts_shp) < 3: # or min(ref_pts_shp) != 2:
raise FaceWarpException(
'reference_pts.shape must be (K,2) or (2,K) and K>2')
if ref_pts_shp[0] == 2 or ref_pts_shp[0] == 3:
ref_pts = ref_pts.T
src_pts = np.float32(facial_pts)
src_pts_shp = src_pts.shape
if max(src_pts_shp) < 3: # or min(src_pts_shp) != 2:
raise FaceWarpException(
'facial_pts.shape must be (K,2) or (2,K) and K>2')
if src_pts_shp[0] == 2 or src_pts_shp[0] == 3:
src_pts = src_pts.T
if src_pts.shape != ref_pts.shape:
raise FaceWarpException(
'facial_pts and reference_pts must have the same shape')
if align_type is 'cv2_affine':
tfm = cv2.getAffineTransform(src_pts[0:3], ref_pts[0:3])
tfm_inv = cv2.getAffineTransform(ref_pts[0:3], src_pts[0:3])
elif align_type is 'cv2_rigid':
tfm, _ = cv2.estimateAffinePartial2D(src_pts[0:3], ref_pts[0:3])
tfm_inv, _ = cv2.estimateAffinePartial2D(ref_pts[0:3], src_pts[0:3])
elif align_type is 'affine':
tfm = get_affine_transform_matrix(src_pts, ref_pts)
tfm_inv = get_affine_transform_matrix(ref_pts, src_pts)
else:
params, scale = _umeyama(src_pts, ref_pts)
tfm = params[:2, :]
params, _ = _umeyama(ref_pts, src_pts, False, scale=1.0/scale)
tfm_inv = params[:2, :]
# M = cv2.getPerspectiveTransform(ref_pts[0:4], src_pts[0:4])
face_img = cv2.warpAffine(src_img, tfm, (crop_size[0], crop_size[1]), flags=3)
# face_img = cv2.warpPerspective(src_img, M, (crop_size[0], crop_size[1]), flags=cv2.INTER_LINEAR )
return face_img, tfm_inv12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452002/08/11/big/img_591
2002/08/26/big/img_265
2002/07/19/big/img_423
2002/08/24/big/img_490
2002/08/31/big/img_17676
2002/07/31/big/img_228
2002/07/24/big/img_402
2002/08/04/big/img_769
2002/07/19/big/img_581
2002/08/13/big/img_723
2002/08/12/big/img_821
2003/01/17/big/img_610
2002/08/13/big/img_1116
2002/08/28/big/img_19238
2002/08/21/big/img_660
2002/08/14/big/img_607
2002/08/05/big/img_3708
2002/08/19/big/img_511
2002/08/07/big/img_1316
2002/07/25/big/img_1047
2002/07/23/big/img_474
2002/07/27/big/img_970
2002/09/02/big/img_15752
2002/09/01/big/img_16378
2002/09/01/big/img_16189
2002/08/26/big/img_276
2002/07/24/big/img_518
2002/08/14/big/img_1027
2002/08/24/big/img_733
2002/08/15/big/img_249
2003/01/15/big/img_1371
2002/08/07/big/img_1348
2003/01/01/big/img_331
2002/08/23/big/img_536
2002/07/30/big/img_224
2002/08/10/big/img_763
2002/08/21/big/img_293
2002/08/15/big/img_1211
2002/08/15/big/img_1194
2003/01/15/big/img_390
2002/08/06/big/img_2893
2002/08/17/big/img_691
2002/08/07/big/img_1695
2002/08/16/big/img_829
2002/07/25/big/img_201
2002/08/23/big/img_36
2003/01/15/big/img_763
2003/01/15/big/img_637
2002/08/22/big/img_592
2002/07/25/big/img_817
2003/01/15/big/img_1219
2002/08/05/big/img_3508
2002/08/15/big/img_1108
2002/07/19/big/img_488
2003/01/16/big/img_704
2003/01/13/big/img_1087
2002/08/10/big/img_670
2002/07/24/big/img_104
2002/08/27/big/img_19823
2002/09/01/big/img_16229
2003/01/13/big/img_846
2002/08/04/big/img_412
2002/07/22/big/img_554
2002/08/12/big/img_331
2002/08/02/big/img_533
2002/08/12/big/img_259
2002/08/18/big/img_328
2003/01/14/big/img_630
2002/08/05/big/img_3541
2002/08/06/big/img_2390
2002/08/20/big/img_150
2002/08/02/big/img_1231
2002/08/16/big/img_710
2002/08/19/big/img_591
2002/07/22/big/img_725
2002/07/24/big/img_820
2003/01/13/big/img_568
2002/08/22/big/img_853
2002/08/09/big/img_648
2002/08/23/big/img_528
2003/01/14/big/img_888
2002/08/30/big/img_18201
2002/08/13/big/img_965
2003/01/14/big/img_660
2002/07/19/big/img_517
2003/01/14/big/img_406
2002/08/30/big/img_18433
2002/08/07/big/img_1630
2002/08/06/big/img_2717
2002/08/21/big/img_470
2002/07/23/big/img_633
2002/08/20/big/img_915
2002/08/16/big/img_893
2002/07/29/big/img_644
2002/08/15/big/img_529
2002/08/16/big/img_668
2002/08/07/big/img_1871
2002/07/25/big/img_192
2002/07/31/big/img_961
2002/08/19/big/img_738
2002/07/31/big/img_382
2002/08/19/big/img_298
2003/01/17/big/img_608
2002/08/21/big/img_514
2002/07/23/big/img_183
2003/01/17/big/img_536
2002/07/24/big/img_478
2002/08/06/big/img_2997
2002/09/02/big/img_15380
2002/08/07/big/img_1153
2002/07/31/big/img_967
2002/07/31/big/img_711
2002/08/26/big/img_664
2003/01/01/big/img_326
2002/08/24/big/img_775
2002/08/08/big/img_961
2002/08/16/big/img_77
2002/08/12/big/img_296
2002/07/22/big/img_905
2003/01/13/big/img_284
2002/08/13/big/img_887
2002/08/24/big/img_849
2002/07/30/big/img_345
2002/08/18/big/img_419
2002/08/01/big/img_1347
2002/08/05/big/img_3670
2002/07/21/big/img_479
2002/08/08/big/img_913
2002/09/02/big/img_15828
2002/08/30/big/img_18194
2002/08/08/big/img_471
2002/08/22/big/img_734
2002/08/09/big/img_586
2002/08/09/big/img_454
2002/07/29/big/img_47
2002/07/19/big/img_381
2002/07/29/big/img_733
2002/08/20/big/img_327
2002/07/21/big/img_96
2002/08/06/big/img_2680
2002/07/25/big/img_919
2002/07/21/big/img_158
2002/07/22/big/img_801
2002/07/22/big/img_567
2002/07/24/big/img_804
2002/07/24/big/img_690
2003/01/15/big/img_576
2002/08/14/big/img_335
2003/01/13/big/img_390
2002/08/11/big/img_258
2002/07/23/big/img_917
2002/08/15/big/img_525
2003/01/15/big/img_505
2002/07/30/big/img_886
2003/01/16/big/img_640
2003/01/14/big/img_642
2003/01/17/big/img_844
2002/08/04/big/img_571
2002/08/29/big/img_18702
2003/01/15/big/img_240
2002/07/29/big/img_553
2002/08/10/big/img_354
2002/08/18/big/img_17
2003/01/15/big/img_782
2002/07/27/big/img_382
2002/08/14/big/img_970
2003/01/16/big/img_70
2003/01/16/big/img_625
2002/08/18/big/img_341
2002/08/26/big/img_188
2002/08/09/big/img_405
2002/08/02/big/img_37
2002/08/13/big/img_748
2002/07/22/big/img_399
2002/07/25/big/img_844
2002/08/12/big/img_340
2003/01/13/big/img_815
2002/08/26/big/img_5
2002/08/10/big/img_158
2002/08/18/big/img_95
2002/07/29/big/img_1297
2003/01/13/big/img_508
2002/09/01/big/img_16680
2003/01/16/big/img_338
2002/08/13/big/img_517
2002/07/22/big/img_626
2002/08/06/big/img_3024
2002/07/26/big/img_499
2003/01/13/big/img_387
2002/08/31/big/img_18025
2002/08/13/big/img_520
2003/01/16/big/img_576
2002/07/26/big/img_121
2002/08/25/big/img_703
2002/08/26/big/img_615
2002/08/17/big/img_434
2002/08/02/big/img_677
2002/08/18/big/img_276
2002/08/05/big/img_3672
2002/07/26/big/img_700
2002/07/31/big/img_277
2003/01/14/big/img_220
2002/08/23/big/img_232
2002/08/31/big/img_17422
2002/07/22/big/img_508
2002/08/13/big/img_681
2003/01/15/big/img_638
2002/08/30/big/img_18408
2003/01/14/big/img_533
2003/01/17/big/img_12
2002/08/28/big/img_19388
2002/08/08/big/img_133
2002/07/26/big/img_885
2002/08/19/big/img_387
2002/08/27/big/img_19976
2002/08/26/big/img_118
2002/08/28/big/img_19146
2002/08/05/big/img_3259
2002/08/15/big/img_536
2002/07/22/big/img_279
2002/07/22/big/img_9
2002/08/13/big/img_301
2002/08/15/big/img_974
2002/08/06/big/img_2355
2002/08/01/big/img_1526
2002/08/03/big/img_417
2002/08/04/big/img_407
2002/08/15/big/img_1029
2002/07/29/big/img_700
2002/08/01/big/img_1463
2002/08/31/big/img_17365
2002/07/28/big/img_223
2002/07/19/big/img_827
2002/07/27/big/img_531
2002/07/19/big/img_845
2002/08/20/big/img_382
2002/07/31/big/img_268
2002/08/27/big/img_19705
2002/08/02/big/img_830
2002/08/23/big/img_250
2002/07/20/big/img_777
2002/08/21/big/img_879
2002/08/26/big/img_20146
2002/08/23/big/img_789
2002/08/06/big/img_2683
2002/08/25/big/img_576
2002/08/09/big/img_498
2002/08/08/big/img_384
2002/08/26/big/img_592
2002/07/29/big/img_1470
2002/08/21/big/img_452
2002/08/30/big/img_18395
2002/08/15/big/img_215
2002/07/21/big/img_643
2002/07/22/big/img_209
2003/01/17/big/img_346
2002/08/25/big/img_658
2002/08/21/big/img_221
2002/08/14/big/img_60
2003/01/17/big/img_885
2003/01/16/big/img_482
2002/08/19/big/img_593
2002/08/08/big/img_233
2002/07/30/big/img_458
2002/07/23/big/img_384
2003/01/15/big/img_670
2003/01/15/big/img_267
2002/08/26/big/img_540
2002/07/29/big/img_552
2002/07/30/big/img_997
2003/01/17/big/img_377
2002/08/21/big/img_265
2002/08/09/big/img_561
2002/07/31/big/img_945
2002/09/02/big/img_15252
2002/08/11/big/img_276
2002/07/22/big/img_491
2002/07/26/big/img_517
2002/08/14/big/img_726
2002/08/08/big/img_46
2002/08/28/big/img_19458
2002/08/06/big/img_2935
2002/07/29/big/img_1392
2002/08/13/big/img_776
2002/08/24/big/img_616
2002/08/14/big/img_1065
2002/07/29/big/img_889
2002/08/18/big/img_188
2002/08/07/big/img_1453
2002/08/02/big/img_760
2002/07/28/big/img_416
2002/08/07/big/img_1393
2002/08/26/big/img_292
2002/08/26/big/img_301
2003/01/13/big/img_195
2002/07/26/big/img_532
2002/08/20/big/img_550
2002/08/05/big/img_3658
2002/08/26/big/img_738
2002/09/02/big/img_15750
2003/01/17/big/img_451
2002/07/23/big/img_339
2002/08/16/big/img_637
2002/08/14/big/img_748
2002/08/06/big/img_2739
2002/07/25/big/img_482
2002/08/19/big/img_191
2002/08/26/big/img_537
2003/01/15/big/img_716
2003/01/15/big/img_767
2002/08/02/big/img_452
2002/08/08/big/img_1011
2002/08/10/big/img_144
2003/01/14/big/img_122
2002/07/24/big/img_586
2002/07/24/big/img_762
2002/08/20/big/img_369
2002/07/30/big/img_146
2002/08/23/big/img_396
2003/01/15/big/img_200
2002/08/15/big/img_1183
2003/01/14/big/img_698
2002/08/09/big/img_792
2002/08/06/big/img_2347
2002/07/31/big/img_911
2002/08/26/big/img_722
2002/08/23/big/img_621
2002/08/05/big/img_3790
2003/01/13/big/img_633
2002/08/09/big/img_224
2002/07/24/big/img_454
2002/07/21/big/img_202
2002/08/02/big/img_630
2002/08/30/big/img_18315
2002/07/19/big/img_491
2002/09/01/big/img_16456
2002/08/09/big/img_242
2002/07/25/big/img_595
2002/07/22/big/img_522
2002/08/01/big/img_1593
2002/07/29/big/img_336
2002/08/15/big/img_448
2002/08/28/big/img_19281
2002/07/29/big/img_342
2002/08/12/big/img_78
2003/01/14/big/img_525
2002/07/28/big/img_147
2002/08/11/big/img_353
2002/08/22/big/img_513
2002/08/04/big/img_721
2002/08/17/big/img_247
2003/01/14/big/img_891
2002/08/20/big/img_853
2002/07/19/big/img_414
2002/08/01/big/img_1530
2003/01/14/big/img_924
2002/08/22/big/img_468
2002/08/18/big/img_354
2002/08/30/big/img_18193
2002/08/23/big/img_492
2002/08/15/big/img_871
2002/08/12/big/img_494
2002/08/06/big/img_2470
2002/07/23/big/img_923
2002/08/26/big/img_155
2002/08/08/big/img_669
2002/07/23/big/img_404
2002/08/28/big/img_19421
2002/08/29/big/img_18993
2002/08/25/big/img_416
2003/01/17/big/img_434
2002/07/29/big/img_1370
2002/07/28/big/img_483
2002/08/11/big/img_50
2002/08/10/big/img_404
2002/09/02/big/img_15057
2003/01/14/big/img_911
2002/09/01/big/img_16697
2003/01/16/big/img_665
2002/09/01/big/img_16708
2002/08/22/big/img_612
2002/08/28/big/img_19471
2002/08/02/big/img_198
2003/01/16/big/img_527
2002/08/22/big/img_209
2002/08/30/big/img_18205
2003/01/14/big/img_114
2003/01/14/big/img_1028
2003/01/16/big/img_894
2003/01/14/big/img_837
2002/07/30/big/img_9
2002/08/06/big/img_2821
2002/08/04/big/img_85
2003/01/13/big/img_884
2002/07/22/big/img_570
2002/08/07/big/img_1773
2002/07/26/big/img_208
2003/01/17/big/img_946
2002/07/19/big/img_930
2003/01/01/big/img_698
2003/01/17/big/img_612
2002/07/19/big/img_372
2002/07/30/big/img_721
2003/01/14/big/img_649
2002/08/19/big/img_4
2002/07/25/big/img_1024
2003/01/15/big/img_601
2002/08/30/big/img_18470
2002/07/22/big/img_29
2002/08/07/big/img_1686
2002/07/20/big/img_294
2002/08/14/big/img_800
2002/08/19/big/img_353
2002/08/19/big/img_350
2002/08/05/big/img_3392
2002/08/09/big/img_622
2003/01/15/big/img_236
2002/08/11/big/img_643
2002/08/05/big/img_3458
2002/08/12/big/img_413
2002/08/22/big/img_415
2002/08/13/big/img_635
2002/08/07/big/img_1198
2002/08/04/big/img_873
2002/08/12/big/img_407
2003/01/15/big/img_346
2002/08/02/big/img_275
2002/08/17/big/img_997
2002/08/21/big/img_958
2002/08/20/big/img_579
2002/07/29/big/img_142
2003/01/14/big/img_1115
2002/08/16/big/img_365
2002/07/29/big/img_1414
2002/08/17/big/img_489
2002/08/13/big/img_1010
2002/07/31/big/img_276
2002/07/25/big/img_1000
2002/08/23/big/img_524
2002/08/28/big/img_19147
2003/01/13/big/img_433
2002/08/20/big/img_205
2003/01/01/big/img_458
2002/07/29/big/img_1449
2003/01/16/big/img_696
2002/08/28/big/img_19296
2002/08/29/big/img_18688
2002/08/21/big/img_767
2002/08/20/big/img_532
2002/08/26/big/img_187
2002/07/26/big/img_183
2002/07/27/big/img_890
2003/01/13/big/img_576
2002/07/30/big/img_15
2002/07/31/big/img_889
2002/08/31/big/img_17759
2003/01/14/big/img_1114
2002/07/19/big/img_445
2002/08/03/big/img_593
2002/07/24/big/img_750
2002/07/30/big/img_133
2002/08/25/big/img_671
2002/07/20/big/img_351
2002/08/31/big/img_17276
2002/08/05/big/img_3231
2002/09/02/big/img_15882
2002/08/14/big/img_115
2002/08/02/big/img_1148
2002/07/25/big/img_936
2002/07/31/big/img_639
2002/08/04/big/img_427
2002/08/22/big/img_843
2003/01/17/big/img_17
2003/01/13/big/img_690
2002/08/13/big/img_472
2002/08/09/big/img_425
2002/08/05/big/img_3450
2003/01/17/big/img_439
2002/08/13/big/img_539
2002/07/28/big/img_35
2002/08/16/big/img_241
2002/08/06/big/img_2898
2003/01/16/big/img_429
2002/08/05/big/img_3817
2002/08/27/big/img_19919
2002/07/19/big/img_422
2002/08/15/big/img_560
2002/07/23/big/img_750
2002/07/30/big/img_353
2002/08/05/big/img_43
2002/08/23/big/img_305
2002/08/01/big/img_2137
2002/08/30/big/img_18097
2002/08/01/big/img_1389
2002/08/02/big/img_308
2003/01/14/big/img_652
2002/08/01/big/img_1798
2003/01/14/big/img_732
2003/01/16/big/img_294
2002/08/26/big/img_213
2002/07/24/big/img_842
2003/01/13/big/img_630
2003/01/13/big/img_634
2002/08/06/big/img_2285
2002/08/01/big/img_2162
2002/08/30/big/img_18134
2002/08/02/big/img_1045
2002/08/01/big/img_2143
2002/07/25/big/img_135
2002/07/20/big/img_645
2002/08/05/big/img_3666
2002/08/14/big/img_523
2002/08/04/big/img_425
2003/01/14/big/img_137
2003/01/01/big/img_176
2002/08/15/big/img_505
2002/08/24/big/img_386
2002/08/05/big/img_3187
2002/08/15/big/img_419
2003/01/13/big/img_520
2002/08/04/big/img_444
2002/08/26/big/img_483
2002/08/05/big/img_3449
2002/08/30/big/img_18409
2002/08/28/big/img_19455
2002/08/27/big/img_20090
2002/07/23/big/img_625
2002/08/24/big/img_205
2002/08/08/big/img_938
2003/01/13/big/img_527
2002/08/07/big/img_1712
2002/07/24/big/img_801
2002/08/09/big/img_579
2003/01/14/big/img_41
2003/01/15/big/img_1130
2002/07/21/big/img_672
2002/08/07/big/img_1590
2003/01/01/big/img_532
2002/08/02/big/img_529
2002/08/05/big/img_3591
2002/08/23/big/img_5
2003/01/14/big/img_882
2002/08/28/big/img_19234
2002/07/24/big/img_398
2003/01/14/big/img_592
2002/08/22/big/img_548
2002/08/12/big/img_761
2003/01/16/big/img_497
2002/08/18/big/img_133
2002/08/08/big/img_874
2002/07/19/big/img_247
2002/08/15/big/img_170
2002/08/27/big/img_19679
2002/08/20/big/img_246
2002/08/24/big/img_358
2002/07/29/big/img_599
2002/08/01/big/img_1555
2002/07/30/big/img_491
2002/07/30/big/img_371
2003/01/16/big/img_682
2002/07/25/big/img_619
2003/01/15/big/img_587
2002/08/02/big/img_1212
2002/08/01/big/img_2152
2002/07/25/big/img_668
2003/01/16/big/img_574
2002/08/28/big/img_19464
2002/08/11/big/img_536
2002/07/24/big/img_201
2002/08/05/big/img_3488
2002/07/25/big/img_887
2002/07/22/big/img_789
2002/07/30/big/img_432
2002/08/16/big/img_166
2002/09/01/big/img_16333
2002/07/26/big/img_1010
2002/07/21/big/img_793
2002/07/22/big/img_720
2002/07/31/big/img_337
2002/07/27/big/img_185
2002/08/23/big/img_440
2002/07/31/big/img_801
2002/07/25/big/img_478
2003/01/14/big/img_171
2002/08/07/big/img_1054
2002/09/02/big/img_15659
2002/07/29/big/img_1348
2002/08/09/big/img_337
2002/08/26/big/img_684
2002/07/31/big/img_537
2002/08/15/big/img_808
2003/01/13/big/img_740
2002/08/07/big/img_1667
2002/08/03/big/img_404
2002/08/06/big/img_2520
2002/07/19/big/img_230
2002/07/19/big/img_356
2003/01/16/big/img_627
2002/08/04/big/img_474
2002/07/29/big/img_833
2002/07/25/big/img_176
2002/08/01/big/img_1684
2002/08/21/big/img_643
2002/08/27/big/img_19673
2002/08/02/big/img_838
2002/08/06/big/img_2378
2003/01/15/big/img_48
2002/07/30/big/img_470
2002/08/15/big/img_963
2002/08/24/big/img_444
2002/08/16/big/img_662
2002/08/15/big/img_1209
2002/07/24/big/img_25
2002/08/06/big/img_2740
2002/07/29/big/img_996
2002/08/31/big/img_18074
2002/08/04/big/img_343
2003/01/17/big/img_509
2003/01/13/big/img_726
2002/08/07/big/img_1466
2002/07/26/big/img_307
2002/08/10/big/img_598
2002/08/13/big/img_890
2002/08/14/big/img_997
2002/07/19/big/img_392
2002/08/02/big/img_475
2002/08/29/big/img_19038
2002/07/29/big/img_538
2002/07/29/big/img_502
2002/08/02/big/img_364
2002/08/31/big/img_17353
2002/08/08/big/img_539
2002/08/01/big/img_1449
2002/07/22/big/img_363
2002/08/02/big/img_90
2002/09/01/big/img_16867
2002/08/05/big/img_3371
2002/07/30/big/img_342
2002/08/07/big/img_1363
2002/08/22/big/img_790
2003/01/15/big/img_404
2002/08/05/big/img_3447
2002/09/01/big/img_16167
2003/01/13/big/img_840
2002/08/22/big/img_1001
2002/08/09/big/img_431
2002/07/27/big/img_618
2002/07/31/big/img_741
2002/07/30/big/img_964
2002/07/25/big/img_86
2002/07/29/big/img_275
2002/08/21/big/img_921
2002/07/26/big/img_892
2002/08/21/big/img_663
2003/01/13/big/img_567
2003/01/14/big/img_719
2002/07/28/big/img_251
2003/01/15/big/img_1123
2002/07/29/big/img_260
2002/08/24/big/img_337
2002/08/01/big/img_1914
2002/08/13/big/img_373
2003/01/15/big/img_589
2002/08/13/big/img_906
2002/07/26/big/img_270
2002/08/26/big/img_313
2002/08/25/big/img_694
2003/01/01/big/img_327
2002/07/23/big/img_261
2002/08/26/big/img_642
2002/07/29/big/img_918
2002/07/23/big/img_455
2002/07/24/big/img_612
2002/07/23/big/img_534
2002/07/19/big/img_534
2002/07/19/big/img_726
2002/08/01/big/img_2146
2002/08/02/big/img_543
2003/01/16/big/img_777
2002/07/30/big/img_484
2002/08/13/big/img_1161
2002/07/21/big/img_390
2002/08/06/big/img_2288
2002/08/21/big/img_677
2002/08/13/big/img_747
2002/08/15/big/img_1248
2002/07/31/big/img_416
2002/09/02/big/img_15259
2002/08/16/big/img_781
2002/08/24/big/img_754
2002/07/24/big/img_803
2002/08/20/big/img_609
2002/08/28/big/img_19571
2002/09/01/big/img_16140
2002/08/26/big/img_769
2002/07/20/big/img_588
2002/08/02/big/img_898
2002/07/21/big/img_466
2002/08/14/big/img_1046
2002/07/25/big/img_212
2002/08/26/big/img_353
2002/08/19/big/img_810
2002/08/31/big/img_17824
2002/08/12/big/img_631
2002/07/19/big/img_828
2002/07/24/big/img_130
2002/08/25/big/img_580
2002/07/31/big/img_699
2002/07/23/big/img_808
2002/07/31/big/img_377
2003/01/16/big/img_570
2002/09/01/big/img_16254
2002/07/21/big/img_471
2002/08/01/big/img_1548
2002/08/18/big/img_252
2002/08/19/big/img_576
2002/08/20/big/img_464
2002/07/27/big/img_735
2002/08/21/big/img_589
2003/01/15/big/img_1192
2002/08/09/big/img_302
2002/07/31/big/img_594
2002/08/23/big/img_19
2002/08/29/big/img_18819
2002/08/19/big/img_293
2002/07/30/big/img_331
2002/08/23/big/img_607
2002/07/30/big/img_363
2002/08/16/big/img_766
2003/01/13/big/img_481
2002/08/06/big/img_2515
2002/09/02/big/img_15913
2002/09/02/big/img_15827
2002/09/02/big/img_15053
2002/08/07/big/img_1576
2002/07/23/big/img_268
2002/08/21/big/img_152
2003/01/15/big/img_578
2002/07/21/big/img_589
2002/07/20/big/img_548
2002/08/27/big/img_19693
2002/08/31/big/img_17252
2002/07/31/big/img_138
2002/07/23/big/img_372
2002/08/16/big/img_695
2002/07/27/big/img_287
2002/08/15/big/img_315
2002/08/10/big/img_361
2002/07/29/big/img_899
2002/08/13/big/img_771
2002/08/21/big/img_92
2003/01/15/big/img_425
2003/01/16/big/img_450
2002/09/01/big/img_16942
2002/08/02/big/img_51
2002/09/02/big/img_15379
2002/08/24/big/img_147
2002/08/30/big/img_18122
2002/07/26/big/img_950
2002/08/07/big/img_1400
2002/08/17/big/img_468
2002/08/15/big/img_470
2002/07/30/big/img_318
2002/07/22/big/img_644
2002/08/27/big/img_19732
2002/07/23/big/img_601
2002/08/26/big/img_398
2002/08/21/big/img_428
2002/08/06/big/img_2119
2002/08/29/big/img_19103
2003/01/14/big/img_933
2002/08/11/big/img_674
2002/08/28/big/img_19420
2002/08/03/big/img_418
2002/08/17/big/img_312
2002/07/25/big/img_1044
2003/01/17/big/img_671
2002/08/30/big/img_18297
2002/07/25/big/img_755
2002/07/23/big/img_471
2002/08/21/big/img_39
2002/07/26/big/img_699
2003/01/14/big/img_33
2002/07/31/big/img_411
2002/08/16/big/img_645
2003/01/17/big/img_116
2002/09/02/big/img_15903
2002/08/20/big/img_120
2002/08/22/big/img_176
2002/07/29/big/img_1316
2002/08/27/big/img_19914
2002/07/22/big/img_719
2002/08/28/big/img_19239
2003/01/13/big/img_385
2002/08/08/big/img_525
2002/07/19/big/img_782
2002/08/13/big/img_843
2002/07/30/big/img_107
2002/08/11/big/img_752
2002/07/29/big/img_383
2002/08/26/big/img_249
2002/08/29/big/img_18860
2002/07/30/big/img_70
2002/07/26/big/img_194
2002/08/15/big/img_530
2002/08/08/big/img_816
2002/07/31/big/img_286
2003/01/13/big/img_294
2002/07/31/big/img_251
2002/07/24/big/img_13
2002/08/31/big/img_17938
2002/07/22/big/img_642
2003/01/14/big/img_728
2002/08/18/big/img_47
2002/08/22/big/img_306
2002/08/20/big/img_348
2002/08/15/big/img_764
2002/08/08/big/img_163
2002/07/23/big/img_531
2002/07/23/big/img_467
2003/01/16/big/img_743
2003/01/13/big/img_535
2002/08/02/big/img_523
2002/08/22/big/img_120
2002/08/11/big/img_496
2002/08/29/big/img_19075
2002/08/08/big/img_465
2002/08/09/big/img_790
2002/08/19/big/img_588
2002/08/23/big/img_407
2003/01/17/big/img_435
2002/08/24/big/img_398
2002/08/27/big/img_19899
2003/01/15/big/img_335
2002/08/13/big/img_493
2002/09/02/big/img_15460
2002/07/31/big/img_470
2002/08/05/big/img_3550
2002/07/28/big/img_123
2002/08/01/big/img_1498
2002/08/04/big/img_504
2003/01/17/big/img_427
2002/08/27/big/img_19708
2002/07/27/big/img_861
2002/07/25/big/img_685
2002/07/31/big/img_207
2003/01/14/big/img_745
2002/08/31/big/img_17756
2002/08/24/big/img_288
2002/08/18/big/img_181
2002/08/10/big/img_520
2002/08/25/big/img_705
2002/08/23/big/img_226
2002/08/04/big/img_727
2002/07/24/big/img_625
2002/08/28/big/img_19157
2002/08/23/big/img_586
2002/07/31/big/img_232
2003/01/13/big/img_240
2003/01/14/big/img_321
2003/01/15/big/img_533
2002/07/23/big/img_480
2002/07/24/big/img_371
2002/08/21/big/img_702
2002/08/31/big/img_17075
2002/09/02/big/img_15278
2002/07/29/big/img_246
2003/01/15/big/img_829
2003/01/15/big/img_1213
2003/01/16/big/img_441
2002/08/14/big/img_921
2002/07/23/big/img_425
2002/08/15/big/img_296
2002/07/19/big/img_135
2002/07/26/big/img_402
2003/01/17/big/img_88
2002/08/20/big/img_872
2002/08/13/big/img_1110
2003/01/16/big/img_1040
2002/07/23/big/img_9
2002/08/13/big/img_700
2002/08/16/big/img_371
2002/08/27/big/img_19966
2003/01/17/big/img_391
2002/08/18/big/img_426
2002/08/01/big/img_1618
2002/07/21/big/img_754
2003/01/14/big/img_1101
2003/01/16/big/img_1022
2002/07/22/big/img_275
2002/08/24/big/img_86
2002/08/17/big/img_582
2003/01/15/big/img_765
2003/01/17/big/img_449
2002/07/28/big/img_265
2003/01/13/big/img_552
2002/07/28/big/img_115
2003/01/16/big/img_56
2002/08/02/big/img_1232
2003/01/17/big/img_925
2002/07/22/big/img_445
2002/07/25/big/img_957
2002/07/20/big/img_589
2002/08/31/big/img_17107
2002/07/29/big/img_483
2002/08/14/big/img_1063
2002/08/07/big/img_1545
2002/08/14/big/img_680
2002/09/01/big/img_16694
2002/08/14/big/img_257
2002/08/11/big/img_726
2002/07/26/big/img_681
2002/07/25/big/img_481
2003/01/14/big/img_737
2002/08/28/big/img_19480
2003/01/16/big/img_362
2002/08/27/big/img_19865
2003/01/01/big/img_547
2002/09/02/big/img_15074
2002/08/01/big/img_1453
2002/08/22/big/img_594
2002/08/28/big/img_19263
2002/08/13/big/img_478
2002/07/29/big/img_1358
2003/01/14/big/img_1022
2002/08/16/big/img_450
2002/08/02/big/img_159
2002/07/26/big/img_781
2003/01/13/big/img_601
2002/08/20/big/img_407
2002/08/15/big/img_468
2002/08/31/big/img_17902
2002/08/16/big/img_81
2002/07/25/big/img_987
2002/07/25/big/img_500
2002/08/02/big/img_31
2002/08/18/big/img_538
2002/08/08/big/img_54
2002/07/23/big/img_686
2002/07/24/big/img_836
2003/01/17/big/img_734
2002/08/16/big/img_1055
2003/01/16/big/img_521
2002/07/25/big/img_612
2002/08/22/big/img_778
2002/08/03/big/img_251
2002/08/12/big/img_436
2002/08/23/big/img_705
2002/07/28/big/img_243
2002/07/25/big/img_1029
2002/08/20/big/img_287
2002/08/29/big/img_18739
2002/08/05/big/img_3272
2002/07/27/big/img_214
2003/01/14/big/img_5
2002/08/01/big/img_1380
2002/08/29/big/img_19097
2002/07/30/big/img_486
2002/08/29/big/img_18707
2002/08/10/big/img_559
2002/08/15/big/img_365
2002/08/09/big/img_525
2002/08/10/big/img_689
2002/07/25/big/img_502
2002/08/03/big/img_667
2002/08/10/big/img_855
2002/08/10/big/img_706
2002/08/18/big/img_603
2003/01/16/big/img_1055
2002/08/31/big/img_17890
2002/08/15/big/img_761
2003/01/15/big/img_489
2002/08/26/big/img_351
2002/08/01/big/img_1772
2002/08/31/big/img_17729
2002/07/25/big/img_609
2003/01/13/big/img_539
2002/07/27/big/img_686
2002/07/31/big/img_311
2002/08/22/big/img_799
2003/01/16/big/img_936
2002/08/31/big/img_17813
2002/08/04/big/img_862
2002/08/09/big/img_332
2002/07/20/big/img_148
2002/08/12/big/img_426
2002/07/24/big/img_69
2002/07/27/big/img_685
2002/08/02/big/img_480
2002/08/26/big/img_154
2002/07/24/big/img_598
2002/08/01/big/img_1881
2002/08/20/big/img_667
2003/01/14/big/img_495
2002/07/21/big/img_744
2002/07/30/big/img_150
2002/07/23/big/img_924
2002/08/08/big/img_272
2002/07/23/big/img_310
2002/07/25/big/img_1011
2002/09/02/big/img_15725
2002/07/19/big/img_814
2002/08/20/big/img_936
2002/07/25/big/img_85
2002/08/24/big/img_662
2002/08/09/big/img_495
2003/01/15/big/img_196
2002/08/16/big/img_707
2002/08/28/big/img_19370
2002/08/06/big/img_2366
2002/08/06/big/img_3012
2002/08/01/big/img_1452
2002/07/31/big/img_742
2002/07/27/big/img_914
2003/01/13/big/img_290
2002/07/31/big/img_288
2002/08/02/big/img_171
2002/08/22/big/img_191
2002/07/27/big/img_1066
2002/08/12/big/img_383
2003/01/17/big/img_1018
2002/08/01/big/img_1785
2002/08/11/big/img_390
2002/08/27/big/img_20037
2002/08/12/big/img_38
2003/01/15/big/img_103
2002/08/26/big/img_31
2002/08/18/big/img_660
2002/07/22/big/img_694
2002/08/15/big/img_24
2002/07/27/big/img_1077
2002/08/01/big/img_1943
2002/07/22/big/img_292
2002/09/01/big/img_16857
2002/07/22/big/img_892
2003/01/14/big/img_46
2002/08/09/big/img_469
2002/08/09/big/img_414
2003/01/16/big/img_40
2002/08/28/big/img_19231
2002/07/27/big/img_978
2002/07/23/big/img_475
2002/07/25/big/img_92
2002/08/09/big/img_799
2002/07/25/big/img_491
2002/08/03/big/img_654
2003/01/15/big/img_687
2002/08/11/big/img_478
2002/08/07/big/img_1664
2002/08/20/big/img_362
2002/08/01/big/img_1298
2003/01/13/big/img_500
2002/08/06/big/img_2896
2002/08/30/big/img_18529
2002/08/16/big/img_1020
2002/07/29/big/img_892
2002/08/29/big/img_18726
2002/07/21/big/img_453
2002/08/17/big/img_437
2002/07/19/big/img_665
2002/07/22/big/img_440
2002/07/19/big/img_582
2002/07/21/big/img_233
2003/01/01/big/img_82
2002/07/25/big/img_341
2002/07/29/big/img_864
2002/08/02/big/img_276
2002/08/29/big/img_18654
2002/07/27/big/img_1024
2002/08/19/big/img_373
2003/01/15/big/img_241
2002/07/25/big/img_84
2002/08/13/big/img_834
2002/08/10/big/img_511
2002/08/01/big/img_1627
2002/08/08/big/img_607
2002/08/06/big/img_2083
2002/08/01/big/img_1486
2002/08/08/big/img_700
2002/08/01/big/img_1954
2002/08/21/big/img_54
2002/07/30/big/img_847
2002/08/28/big/img_19169
2002/07/21/big/img_549
2002/08/03/big/img_693
2002/07/31/big/img_1002
2003/01/14/big/img_1035
2003/01/16/big/img_622
2002/07/30/big/img_1201
2002/08/10/big/img_444
2002/07/31/big/img_374
2002/08/21/big/img_301
2002/08/13/big/img_1095
2003/01/13/big/img_288
2002/07/25/big/img_232
2003/01/13/big/img_967
2002/08/26/big/img_360
2002/08/05/big/img_67
2002/08/29/big/img_18969
2002/07/28/big/img_16
2002/08/16/big/img_515
2002/07/20/big/img_708
2002/08/18/big/img_178
2003/01/15/big/img_509
2002/07/25/big/img_430
2002/08/21/big/img_738
2002/08/16/big/img_886
2002/09/02/big/img_15605
2002/09/01/big/img_16242
2002/08/24/big/img_711
2002/07/25/big/img_90
2002/08/09/big/img_491
2002/07/30/big/img_534
2003/01/13/big/img_474
2002/08/25/big/img_510
2002/08/15/big/img_555
2002/08/02/big/img_775
2002/07/23/big/img_975
2002/08/19/big/img_229
2003/01/17/big/img_860
2003/01/02/big/img_10
2002/07/23/big/img_542
2002/08/06/big/img_2535
2002/07/22/big/img_37
2002/08/06/big/img_2342
2002/08/25/big/img_515
2002/08/25/big/img_336
2002/08/18/big/img_837
2002/08/21/big/img_616
2003/01/17/big/img_24
2002/07/26/big/img_936
2002/08/14/big/img_896
2002/07/29/big/img_465
2002/07/31/big/img_543
2002/08/01/big/img_1411
2002/08/02/big/img_423
2002/08/21/big/img_44
2002/07/31/big/img_11
2003/01/15/big/img_628
2003/01/15/big/img_605
2002/07/30/big/img_571
2002/07/23/big/img_428
2002/08/15/big/img_942
2002/07/26/big/img_531
2003/01/16/big/img_59
2002/08/02/big/img_410
2002/07/31/big/img_230
2002/08/19/big/img_806
2003/01/14/big/img_462
2002/08/16/big/img_370
2002/08/13/big/img_380
2002/08/16/big/img_932
2002/07/19/big/img_393
2002/08/20/big/img_764
2002/08/15/big/img_616
2002/07/26/big/img_267
2002/07/27/big/img_1069
2002/08/14/big/img_1041
2003/01/13/big/img_594
2002/09/01/big/img_16845
2002/08/09/big/img_229
2003/01/16/big/img_639
2002/08/19/big/img_398
2002/08/18/big/img_978
2002/08/24/big/img_296
2002/07/29/big/img_415
2002/07/30/big/img_923
2002/08/18/big/img_575
2002/08/22/big/img_182
2002/07/25/big/img_806
2002/07/22/big/img_49
2002/07/29/big/img_989
2003/01/17/big/img_789
2003/01/15/big/img_503
2002/09/01/big/img_16062
2003/01/17/big/img_794
2002/08/15/big/img_564
2003/01/15/big/img_222
2002/08/01/big/img_1656
2003/01/13/big/img_432
2002/07/19/big/img_426
2002/08/17/big/img_244
2002/08/13/big/img_805
2002/09/02/big/img_15067
2002/08/11/big/img_58
2002/08/22/big/img_636
2002/07/22/big/img_416
2002/08/13/big/img_836
2002/08/26/big/img_363
2002/07/30/big/img_917
2003/01/14/big/img_206
2002/08/12/big/img_311
2002/08/31/big/img_17623
2002/07/29/big/img_661
2003/01/13/big/img_417
2002/08/02/big/img_463
2002/08/02/big/img_669
2002/08/26/big/img_670
2002/08/02/big/img_375
2002/07/19/big/img_209
2002/08/08/big/img_115
2002/08/21/big/img_399
2002/08/20/big/img_911
2002/08/07/big/img_1212
2002/08/20/big/img_578
2002/08/22/big/img_554
2002/08/21/big/img_484
2002/07/25/big/img_450
2002/08/03/big/img_542
2002/08/15/big/img_561
2002/07/23/big/img_360
2002/08/30/big/img_18137
2002/07/25/big/img_250
2002/08/03/big/img_647
2002/08/20/big/img_375
2002/08/14/big/img_387
2002/09/01/big/img_16990
2002/08/28/big/img_19341
2003/01/15/big/img_239
2002/08/20/big/img_528
2002/08/12/big/img_130
2002/09/02/big/img_15108
2003/01/15/big/img_372
2002/08/16/big/img_678
2002/08/04/big/img_623
2002/07/23/big/img_477
2002/08/28/big/img_19590
2003/01/17/big/img_978
2002/09/01/big/img_16692
2002/07/20/big/img_109
2002/08/06/big/img_2660
2003/01/14/big/img_464
2002/08/09/big/img_618
2002/07/22/big/img_722
2002/08/25/big/img_419
2002/08/03/big/img_314
2002/08/25/big/img_40
2002/07/27/big/img_430
2002/08/10/big/img_569
2002/08/23/big/img_398
2002/07/23/big/img_893
2002/08/16/big/img_261
2002/08/06/big/img_2668
2002/07/22/big/img_835
2002/09/02/big/img_15093
2003/01/16/big/img_65
2002/08/21/big/img_448
2003/01/14/big/img_351
2003/01/17/big/img_133
2002/07/28/big/img_493
2003/01/15/big/img_640
2002/09/01/big/img_16880
2002/08/15/big/img_350
2002/08/20/big/img_624
2002/08/25/big/img_604
2002/08/06/big/img_2200
2002/08/23/big/img_290
2002/08/13/big/img_1152
2003/01/14/big/img_251
2002/08/02/big/img_538
2002/08/22/big/img_613
2003/01/13/big/img_351
2002/08/18/big/img_368
2002/07/23/big/img_392
2002/07/25/big/img_198
2002/07/25/big/img_418
2002/08/26/big/img_614
2002/07/23/big/img_405
2003/01/14/big/img_445
2002/07/25/big/img_326
2002/08/10/big/img_734
2003/01/14/big/img_530
2002/08/08/big/img_561
2002/08/29/big/img_18990
2002/08/10/big/img_576
2002/07/29/big/img_1494
2002/07/19/big/img_198
2002/08/10/big/img_562
2002/07/22/big/img_901
2003/01/14/big/img_37
2002/09/02/big/img_15629
2003/01/14/big/img_58
2002/08/01/big/img_1364
2002/07/27/big/img_636
2003/01/13/big/img_241
2002/09/01/big/img_16988
2003/01/13/big/img_560
2002/08/09/big/img_533
2002/07/31/big/img_249
2003/01/17/big/img_1007
2002/07/21/big/img_64
2003/01/13/big/img_537
2003/01/15/big/img_606
2002/08/18/big/img_651
2002/08/24/big/img_405
2002/07/26/big/img_837
2002/08/09/big/img_562
2002/08/01/big/img_1983
2002/08/03/big/img_514
2002/07/29/big/img_314
2002/08/12/big/img_493
2003/01/14/big/img_121
2003/01/14/big/img_479
2002/08/04/big/img_410
2002/07/22/big/img_607
2003/01/17/big/img_417
2002/07/20/big/img_547
2002/08/13/big/img_396
2002/08/31/big/img_17538
2002/08/13/big/img_187
2002/08/12/big/img_328
2003/01/14/big/img_569
2002/07/27/big/img_1081
2002/08/14/big/img_504
2002/08/23/big/img_785
2002/07/26/big/img_339
2002/08/07/big/img_1156
2002/08/07/big/img_1456
2002/08/23/big/img_378
2002/08/27/big/img_19719
2002/07/31/big/img_39
2002/07/31/big/img_883
2003/01/14/big/img_676
2002/07/29/big/img_214
2002/07/26/big/img_669
2002/07/25/big/img_202
2002/08/08/big/img_259
2003/01/17/big/img_943
2003/01/15/big/img_512
2002/08/05/big/img_3295
2002/08/27/big/img_19685
2002/08/08/big/img_277
2002/08/30/big/img_18154
2002/07/22/big/img_663
2002/08/29/big/img_18914
2002/07/31/big/img_908
2002/08/27/big/img_19926
2003/01/13/big/img_791
2003/01/15/big/img_827
2002/08/18/big/img_878
2002/08/14/big/img_670
2002/07/20/big/img_182
2002/08/15/big/img_291
2002/08/06/big/img_2600
2002/07/23/big/img_587
2002/08/14/big/img_577
2003/01/15/big/img_585
2002/07/30/big/img_310
2002/08/03/big/img_658
2002/08/10/big/img_157
2002/08/19/big/img_811
2002/07/29/big/img_1318
2002/08/04/big/img_104
2002/07/30/big/img_332
2002/07/24/big/img_789
2002/07/29/big/img_516
2002/07/23/big/img_843
2002/08/01/big/img_1528
2002/08/13/big/img_798
2002/08/07/big/img_1729
2002/08/28/big/img_19448
2003/01/16/big/img_95
2002/08/12/big/img_473
2002/07/27/big/img_269
2003/01/16/big/img_621
2002/07/29/big/img_772
2002/07/24/big/img_171
2002/07/19/big/img_429
2002/08/07/big/img_1933
2002/08/27/big/img_19629
2002/08/05/big/img_3688
2002/08/07/big/img_1691
2002/07/23/big/img_600
2002/07/29/big/img_666
2002/08/25/big/img_566
2002/08/06/big/img_2659
2002/08/29/big/img_18929
2002/08/16/big/img_407
2002/08/18/big/img_774
2002/08/19/big/img_249
2002/08/06/big/img_2427
2002/08/29/big/img_18899
2002/08/01/big/img_1818
2002/07/31/big/img_108
2002/07/29/big/img_500
2002/08/11/big/img_115
2002/07/19/big/img_521
2002/08/02/big/img_1163
2002/07/22/big/img_62
2002/08/13/big/img_466
2002/08/21/big/img_956
2002/08/23/big/img_602
2002/08/20/big/img_858
2002/07/25/big/img_690
2002/07/19/big/img_130
2002/08/04/big/img_874
2002/07/26/big/img_489
2002/07/22/big/img_548
2002/08/10/big/img_191
2002/07/25/big/img_1051
2002/08/18/big/img_473
2002/08/12/big/img_755
2002/08/18/big/img_413
2002/08/08/big/img_1044
2002/08/17/big/img_680
2002/08/26/big/img_235
2002/08/20/big/img_330
2002/08/22/big/img_344
2002/08/09/big/img_593
2002/07/31/big/img_1006
2002/08/14/big/img_337
2002/08/16/big/img_728
2002/07/24/big/img_834
2002/08/04/big/img_552
2002/09/02/big/img_15213
2002/07/25/big/img_725
2002/08/30/big/img_18290
2003/01/01/big/img_475
2002/07/27/big/img_1083
2002/08/29/big/img_18955
2002/08/31/big/img_17232
2002/08/08/big/img_480
2002/08/01/big/img_1311
2002/07/30/big/img_745
2002/08/03/big/img_649
2002/08/12/big/img_193
2002/07/29/big/img_228
2002/07/25/big/img_836
2002/08/20/big/img_400
2002/07/30/big/img_507
2002/09/02/big/img_15072
2002/07/26/big/img_658
2002/07/28/big/img_503
2002/08/05/big/img_3814
2002/08/24/big/img_745
2003/01/13/big/img_817
2002/08/08/big/img_579
2002/07/22/big/img_251
2003/01/13/big/img_689
2002/07/25/big/img_407
2002/08/13/big/img_1050
2002/08/14/big/img_733
2002/07/24/big/img_82
2003/01/17/big/img_288
2003/01/15/big/img_475
2002/08/14/big/img_620
2002/08/21/big/img_167
2002/07/19/big/img_300
2002/07/26/big/img_219
2002/08/01/big/img_1468
2002/07/23/big/img_260
2002/08/09/big/img_555
2002/07/19/big/img_160
2002/08/02/big/img_1060
2003/01/14/big/img_149
2002/08/15/big/img_346
2002/08/24/big/img_597
2002/08/22/big/img_502
2002/08/30/big/img_18228
2002/07/21/big/img_766
2003/01/15/big/img_841
2002/07/24/big/img_516
2002/08/02/big/img_265
2002/08/15/big/img_1243
2003/01/15/big/img_223
2002/08/04/big/img_236
2002/07/22/big/img_309
2002/07/20/big/img_656
2002/07/31/big/img_412
2002/09/01/big/img_16462
2003/01/16/big/img_431
2002/07/22/big/img_793
2002/08/15/big/img_877
2002/07/26/big/img_282
2002/07/25/big/img_529
2002/08/24/big/img_613
2003/01/17/big/img_700
2002/08/06/big/img_2526
2002/08/24/big/img_394
2002/08/21/big/img_521
2002/08/25/big/img_560
2002/07/29/big/img_966
2002/07/25/big/img_448
2003/01/13/big/img_782
2002/08/21/big/img_296
2002/09/01/big/img_16755
2002/08/05/big/img_3552
2002/09/02/big/img_15823
2003/01/14/big/img_193
2002/07/21/big/img_159
2002/08/02/big/img_564
2002/08/16/big/img_300
2002/07/19/big/img_269
2002/08/13/big/img_676
2002/07/28/big/img_57
2002/08/05/big/img_3318
2002/07/31/big/img_218
2002/08/21/big/img_898
2002/07/29/big/img_109
2002/07/19/big/img_854
2002/08/23/big/img_311
2002/08/14/big/img_318
2002/07/25/big/img_523
2002/07/21/big/img_678
2003/01/17/big/img_690
2002/08/28/big/img_19503
2002/08/18/big/img_251
2002/08/22/big/img_672
2002/08/20/big/img_663
2002/08/02/big/img_148
2002/09/02/big/img_15580
2002/07/25/big/img_778
2002/08/14/big/img_565
2002/08/12/big/img_374
2002/08/13/big/img_1018
2002/08/20/big/img_474
2002/08/25/big/img_33
2002/08/02/big/img_1190
2002/08/08/big/img_864
2002/08/14/big/img_1071
2002/08/30/big/img_18103
2002/08/18/big/img_533
2003/01/16/big/img_650
2002/07/25/big/img_108
2002/07/26/big/img_81
2002/07/27/big/img_543
2002/07/29/big/img_521
2003/01/13/big/img_434
2002/08/26/big/img_674
2002/08/06/big/img_2932
2002/08/07/big/img_1262
2003/01/15/big/img_201
2003/01/16/big/img_673
2002/09/02/big/img_15988
2002/07/29/big/img_1306
2003/01/14/big/img_1072
2002/08/30/big/img_18232
2002/08/05/big/img_3711
2002/07/23/big/img_775
2002/08/01/big/img_16
2003/01/16/big/img_630
2002/08/22/big/img_695
2002/08/14/big/img_51
2002/08/14/big/img_782
2002/08/24/big/img_742
2003/01/14/big/img_512
2003/01/15/big/img_1183
2003/01/15/big/img_714
2002/08/01/big/img_2078
2002/07/31/big/img_682
2002/09/02/big/img_15687
2002/07/26/big/img_518
2002/08/27/big/img_19676
2002/09/02/big/img_15969
2002/08/02/big/img_931
2002/08/25/big/img_508
2002/08/29/big/img_18616
2002/07/22/big/img_839
2002/07/28/big/img_313
2003/01/14/big/img_155
2002/08/02/big/img_1105
2002/08/09/big/img_53
2002/08/16/big/img_469
2002/08/15/big/img_502
2002/08/20/big/img_575
2002/07/25/big/img_138
2003/01/16/big/img_579
2002/07/19/big/img_352
2003/01/14/big/img_762
2003/01/01/big/img_588
2002/08/02/big/img_981
2002/08/21/big/img_447
2002/09/01/big/img_16151
2003/01/14/big/img_769
2002/08/23/big/img_461
2002/08/17/big/img_240
2002/09/02/big/img_15220
2002/07/19/big/img_408
2002/09/02/big/img_15496
2002/07/29/big/img_758
2002/08/28/big/img_19392
2002/08/06/big/img_2723
2002/08/31/big/img_17752
2002/08/23/big/img_469
2002/08/13/big/img_515
2002/09/02/big/img_15551
2002/08/03/big/img_462
2002/07/24/big/img_613
2002/07/22/big/img_61
2002/08/08/big/img_171
2002/08/21/big/img_177
2003/01/14/big/img_105
2002/08/02/big/img_1017
2002/08/22/big/img_106
2002/07/27/big/img_542
2002/07/21/big/img_665
2002/07/23/big/img_595
2002/08/04/big/img_657
2002/08/29/big/img_19002
2003/01/15/big/img_550
2002/08/14/big/img_662
2002/07/20/big/img_425
2002/08/30/big/img_18528
2002/07/26/big/img_611
2002/07/22/big/img_849
2002/08/07/big/img_1655
2002/08/21/big/img_638
2003/01/17/big/img_732
2003/01/01/big/img_496
2002/08/18/big/img_713
2002/08/08/big/img_109
2002/07/27/big/img_1008
2002/07/20/big/img_559
2002/08/16/big/img_699
2002/08/31/big/img_17702
2002/07/31/big/img_1013
2002/08/01/big/img_2027
2002/08/02/big/img_1001
2002/08/03/big/img_210
2002/08/01/big/img_2087
2003/01/14/big/img_199
2002/07/29/big/img_48
2002/07/19/big/img_727
2002/08/09/big/img_249
2002/08/04/big/img_632
2002/08/22/big/img_620
2003/01/01/big/img_457
2002/08/05/big/img_3223
2002/07/27/big/img_240
2002/07/25/big/img_797
2002/08/13/big/img_430
2002/07/25/big/img_615
2002/08/12/big/img_28
2002/07/30/big/img_220
2002/07/24/big/img_89
2002/08/21/big/img_357
2002/08/09/big/img_590
2003/01/13/big/img_525
2002/08/17/big/img_818
2003/01/02/big/img_7
2002/07/26/big/img_636
2003/01/13/big/img_1122
2002/07/23/big/img_810
2002/08/20/big/img_888
2002/07/27/big/img_3
2002/08/15/big/img_451
2002/09/02/big/img_15787
2002/07/31/big/img_281
2002/08/05/big/img_3274
2002/08/07/big/img_1254
2002/07/31/big/img_27
2002/08/01/big/img_1366
2002/07/30/big/img_182
2002/08/27/big/img_19690
2002/07/29/big/img_68
2002/08/23/big/img_754
2002/07/30/big/img_540
2002/08/27/big/img_20063
2002/08/14/big/img_471
2002/08/02/big/img_615
2002/07/30/big/img_186
2002/08/25/big/img_150
2002/07/27/big/img_626
2002/07/20/big/img_225
2003/01/15/big/img_1252
2002/07/19/big/img_367
2003/01/15/big/img_582
2002/08/09/big/img_572
2002/08/08/big/img_428
2003/01/15/big/img_639
2002/08/28/big/img_19245
2002/07/24/big/img_321
2002/08/02/big/img_662
2002/08/08/big/img_1033
2003/01/17/big/img_867
2002/07/22/big/img_652
2003/01/14/big/img_224
2002/08/18/big/img_49
2002/07/26/big/img_46
2002/08/31/big/img_18021
2002/07/25/big/img_151
2002/08/23/big/img_540
2002/08/25/big/img_693
2002/07/23/big/img_340
2002/07/28/big/img_117
2002/09/02/big/img_15768
2002/08/26/big/img_562
2002/07/24/big/img_480
2003/01/15/big/img_341
2002/08/10/big/img_783
2002/08/20/big/img_132
2003/01/14/big/img_370
2002/07/20/big/img_720
2002/08/03/big/img_144
2002/08/20/big/img_538
2002/08/01/big/img_1745
2002/08/11/big/img_683
2002/08/03/big/img_328
2002/08/10/big/img_793
2002/08/14/big/img_689
2002/08/02/big/img_162
2003/01/17/big/img_411
2002/07/31/big/img_361
2002/08/15/big/img_289
2002/08/08/big/img_254
2002/08/15/big/img_996
2002/08/20/big/img_785
2002/07/24/big/img_511
2002/08/06/big/img_2614
2002/08/29/big/img_18733
2002/08/17/big/img_78
2002/07/30/big/img_378
2002/08/31/big/img_17947
2002/08/26/big/img_88
2002/07/30/big/img_558
2002/08/02/big/img_67
2003/01/14/big/img_325
2002/07/29/big/img_1357
2002/07/19/big/img_391
2002/07/30/big/img_307
2003/01/13/big/img_219
2002/07/24/big/img_807
2002/08/23/big/img_543
2002/08/29/big/img_18620
2002/07/22/big/img_769
2002/08/26/big/img_503
2002/07/30/big/img_78
2002/08/14/big/img_1036
2002/08/09/big/img_58
2002/07/24/big/img_616
2002/08/02/big/img_464
2002/07/26/big/img_576
2002/07/22/big/img_273
2003/01/16/big/img_470
2002/07/29/big/img_329
2002/07/30/big/img_1086
2002/07/31/big/img_353
2002/09/02/big/img_15275
2003/01/17/big/img_555
2002/08/26/big/img_212
2002/08/01/big/img_1692
2003/01/15/big/img_600
2002/07/29/big/img_825
2002/08/08/big/img_68
2002/08/10/big/img_719
2002/07/31/big/img_636
2002/07/29/big/img_325
2002/07/21/big/img_515
2002/07/22/big/img_705
2003/01/13/big/img_818
2002/08/09/big/img_486
2002/08/22/big/img_141
2002/07/22/big/img_303
2002/08/09/big/img_393
2002/07/29/big/img_963
2002/08/02/big/img_1215
2002/08/19/big/img_674
2002/08/12/big/img_690
2002/08/21/big/img_637
2002/08/21/big/img_841
2002/08/24/big/img_71
2002/07/25/big/img_596
2002/07/24/big/img_864
2002/08/18/big/img_293
2003/01/14/big/img_657
2002/08/15/big/img_411
2002/08/16/big/img_348
2002/08/05/big/img_3157
2002/07/20/big/img_663
2003/01/13/big/img_654
2003/01/16/big/img_433
2002/08/30/big/img_18200
2002/08/12/big/img_226
2003/01/16/big/img_491
2002/08/08/big/img_666
2002/07/19/big/img_576
2003/01/15/big/img_776
2003/01/16/big/img_899
2002/07/19/big/img_397
2002/08/14/big/img_44
2003/01/15/big/img_762
2002/08/02/big/img_982
2002/09/02/big/img_15234
2002/08/17/big/img_556
2002/08/21/big/img_410
2002/08/21/big/img_386
2002/07/19/big/img_690
2002/08/05/big/img_3052
2002/08/14/big/img_219
2002/08/16/big/img_273
2003/01/15/big/img_752
2002/08/08/big/img_184
2002/07/31/big/img_743
2002/08/23/big/img_338
2003/01/14/big/img_1055
2002/08/05/big/img_3405
2003/01/15/big/img_17
2002/08/03/big/img_141
2002/08/14/big/img_549
2002/07/27/big/img_1034
2002/07/31/big/img_932
2002/08/30/big/img_18487
2002/09/02/big/img_15814
2002/08/01/big/img_2086
2002/09/01/big/img_16535
2002/07/22/big/img_500
2003/01/13/big/img_400
2002/08/25/big/img_607
2002/08/30/big/img_18384
2003/01/14/big/img_951
2002/08/13/big/img_1150
2002/08/08/big/img_1022
2002/08/10/big/img_428
2002/08/28/big/img_19242
2002/08/05/big/img_3098
2002/07/23/big/img_400
2002/08/26/big/img_365
2002/07/20/big/img_318
2002/08/13/big/img_740
2003/01/16/big/img_37
2002/08/26/big/img_274
2002/08/02/big/img_205
2002/08/21/big/img_695
2002/08/06/big/img_2289
2002/08/20/big/img_794
2002/08/18/big/img_438
2002/08/07/big/img_1380
2002/08/02/big/img_737
2002/08/07/big/img_1651
2002/08/15/big/img_1238
2002/08/01/big/img_1681
2002/08/06/big/img_3017
2002/07/23/big/img_706
2002/07/31/big/img_392
2002/08/09/big/img_539
2002/07/29/big/img_835
2002/08/26/big/img_723
2002/08/28/big/img_19235
2003/01/16/big/img_353
2002/08/10/big/img_150
2002/08/29/big/img_19025
2002/08/21/big/img_310
2002/08/10/big/img_823
2002/07/26/big/img_981
2002/08/11/big/img_288
2002/08/19/big/img_534
2002/08/21/big/img_300
2002/07/31/big/img_49
2002/07/30/big/img_469
2002/08/28/big/img_19197
2002/08/25/big/img_205
2002/08/10/big/img_390
2002/08/23/big/img_291
2002/08/26/big/img_230
2002/08/18/big/img_76
2002/07/23/big/img_409
2002/08/14/big/img_1053
2003/01/14/big/img_291
2002/08/10/big/img_503
2002/08/27/big/img_19928
2002/08/03/big/img_563
2002/08/17/big/img_250
2002/08/06/big/img_2381
2002/08/17/big/img_948
2002/08/06/big/img_2710
2002/07/22/big/img_696
2002/07/31/big/img_670
2002/08/12/big/img_594
2002/07/29/big/img_624
2003/01/17/big/img_934
2002/08/03/big/img_584
2002/08/22/big/img_1003
2002/08/05/big/img_3396
2003/01/13/big/img_570
2002/08/02/big/img_219
2002/09/02/big/img_15774
2002/08/16/big/img_818
2002/08/23/big/img_402
2003/01/14/big/img_552
2002/07/29/big/img_71
2002/08/05/big/img_3592
2002/08/16/big/img_80
2002/07/27/big/img_672
2003/01/13/big/img_470
2003/01/16/big/img_702
2002/09/01/big/img_16130
2002/08/08/big/img_240
2002/09/01/big/img_16338
2002/07/26/big/img_312
2003/01/14/big/img_538
2002/07/20/big/img_695
2002/08/30/big/img_18098
2002/08/25/big/img_259
2002/08/16/big/img_1042
2002/08/09/big/img_837
2002/08/31/big/img_17760
2002/07/31/big/img_14
2002/08/09/big/img_361
2003/01/16/big/img_107
2002/08/14/big/img_124
2002/07/19/big/img_463
2003/01/15/big/img_275
2002/07/25/big/img_1151
2002/07/29/big/img_1501
2002/08/27/big/img_19889
2002/08/29/big/img_18603
2003/01/17/big/img_601
2002/08/25/big/img_355
2002/08/08/big/img_297
2002/08/20/big/img_290
2002/07/31/big/img_195
2003/01/01/big/img_336
2002/08/18/big/img_369
2002/07/25/big/img_621
2002/08/11/big/img_508
2003/01/14/big/img_458
2003/01/15/big/img_795
2002/08/12/big/img_498
2002/08/01/big/img_1734
2002/08/02/big/img_246
2002/08/16/big/img_565
2002/08/11/big/img_475
2002/08/22/big/img_408
2002/07/28/big/img_78
2002/07/21/big/img_81
2003/01/14/big/img_697
2002/08/14/big/img_661
2002/08/15/big/img_507
2002/08/19/big/img_55
2002/07/22/big/img_152
2003/01/14/big/img_470
2002/08/03/big/img_379
2002/08/22/big/img_506
2003/01/16/big/img_966
2002/08/18/big/img_698
2002/08/24/big/img_528
2002/08/23/big/img_10
2002/08/01/big/img_1655
2002/08/22/big/img_953
2002/07/19/big/img_630
2002/07/22/big/img_889
2002/08/16/big/img_351
2003/01/16/big/img_83
2002/07/19/big/img_805
2002/08/14/big/img_704
2002/07/19/big/img_389
2002/08/31/big/img_17765
2002/07/29/big/img_606
2003/01/17/big/img_939
2002/09/02/big/img_15081
2002/08/21/big/img_181
2002/07/29/big/img_1321
2002/07/21/big/img_497
2002/07/20/big/img_539
2002/08/24/big/img_119
2002/08/01/big/img_1281
2002/07/26/big/img_207
2002/07/26/big/img_432
2002/07/27/big/img_1006
2002/08/05/big/img_3087
2002/08/14/big/img_252
2002/08/14/big/img_798
2002/07/24/big/img_538
2002/09/02/big/img_15507
2002/08/08/big/img_901
2003/01/14/big/img_557
2002/08/07/big/img_1819
2002/08/04/big/img_470
2002/08/01/big/img_1504
2002/08/16/big/img_1070
2002/08/16/big/img_372
2002/08/23/big/img_416
2002/08/30/big/img_18208
2002/08/01/big/img_2043
2002/07/22/big/img_385
2002/08/22/big/img_466
2002/08/21/big/img_869
2002/08/28/big/img_19429
2002/08/02/big/img_770
2002/07/23/big/img_433
2003/01/14/big/img_13
2002/07/27/big/img_953
2002/09/02/big/img_15728
2002/08/01/big/img_1361
2002/08/29/big/img_18897
2002/08/26/big/img_534
2002/08/11/big/img_121
2002/08/26/big/img_20130
2002/07/31/big/img_363
2002/08/13/big/img_978
2002/07/25/big/img_835
2002/08/02/big/img_906
2003/01/14/big/img_548
2002/07/30/big/img_80
2002/07/26/big/img_982
2003/01/16/big/img_99
2002/08/19/big/img_362
2002/08/24/big/img_376
2002/08/07/big/img_1264
2002/07/27/big/img_938
2003/01/17/big/img_535
2002/07/26/big/img_457
2002/08/08/big/img_848
2003/01/15/big/img_859
2003/01/15/big/img_622
2002/07/30/big/img_403
2002/07/29/big/img_217
2002/07/26/big/img_891
2002/07/24/big/img_70
2002/08/25/big/img_619
2002/08/05/big/img_3375
2002/08/01/big/img_2160
2002/08/06/big/img_2227
2003/01/14/big/img_117
2002/08/14/big/img_227
2002/08/13/big/img_565
2002/08/19/big/img_625
2002/08/03/big/img_812
2002/07/24/big/img_41
2002/08/16/big/img_235
2002/07/29/big/img_759
2002/07/21/big/img_433
2002/07/29/big/img_190
2003/01/16/big/img_435
2003/01/13/big/img_708
2002/07/30/big/img_57
2002/08/22/big/img_162
2003/01/01/big/img_558
2003/01/15/big/img_604
2002/08/16/big/img_935
2002/08/20/big/img_394
2002/07/28/big/img_465
2002/09/02/big/img_15534
2002/08/16/big/img_87
2002/07/22/big/img_469
2002/08/12/big/img_245
2003/01/13/big/img_236
2002/08/06/big/img_2736
2002/08/03/big/img_348
2003/01/14/big/img_218
2002/07/26/big/img_232
2003/01/15/big/img_244
2002/07/25/big/img_1121
2002/08/01/big/img_1484
2002/07/26/big/img_541
2002/08/07/big/img_1244
2002/07/31/big/img_3
2002/08/30/big/img_18437
2002/08/29/big/img_19094
2002/08/01/big/img_1355
2002/08/19/big/img_338
2002/07/19/big/img_255
2002/07/21/big/img_76
2002/08/25/big/img_199
2002/08/12/big/img_740
2002/07/30/big/img_852
2002/08/15/big/img_599
2002/08/23/big/img_254
2002/08/19/big/img_125
2002/07/24/big/img_2
2002/08/04/big/img_145
2002/08/05/big/img_3137
2002/07/28/big/img_463
2003/01/14/big/img_801
2002/07/23/big/img_366
2002/08/26/big/img_600
2002/08/26/big/img_649
2002/09/02/big/img_15849
2002/07/26/big/img_248
2003/01/13/big/img_200
2002/08/07/big/img_1794
2002/08/31/big/img_17270
2002/08/23/big/img_608
2003/01/13/big/img_837
2002/08/23/big/img_581
2002/08/20/big/img_754
2002/08/18/big/img_183
2002/08/20/big/img_328
2002/07/22/big/img_494
2002/07/29/big/img_399
2002/08/28/big/img_19284
2002/08/08/big/img_566
2002/07/25/big/img_376
2002/07/23/big/img_138
2002/07/25/big/img_435
2002/08/17/big/img_685
2002/07/19/big/img_90
2002/07/20/big/img_716
2002/08/31/big/img_17458
2002/08/26/big/img_461
2002/07/25/big/img_355
2002/08/06/big/img_2152
2002/07/27/big/img_932
2002/07/23/big/img_232
2002/08/08/big/img_1020
2002/07/31/big/img_366
2002/08/06/big/img_2667
2002/08/21/big/img_465
2002/08/15/big/img_305
2002/08/02/big/img_247
2002/07/28/big/img_46
2002/08/27/big/img_19922
2002/08/23/big/img_643
2003/01/13/big/img_624
2002/08/23/big/img_625
2002/08/05/big/img_3787
2003/01/13/big/img_627
2002/09/01/big/img_16381
2002/08/05/big/img_3668
2002/07/21/big/img_535
2002/08/27/big/img_19680
2002/07/22/big/img_413
2002/07/29/big/img_481
2003/01/15/big/img_496
2002/07/23/big/img_701
2002/08/29/big/img_18670
2002/07/28/big/img_319
2003/01/14/big/img_517
2002/07/26/big/img_256
2003/01/16/big/img_593
2002/07/30/big/img_956
2002/07/30/big/img_667
2002/07/25/big/img_100
2002/08/11/big/img_570
2002/07/26/big/img_745
2002/08/04/big/img_834
2002/08/25/big/img_521
2002/08/01/big/img_2148
2002/09/02/big/img_15183
2002/08/22/big/img_514
2002/08/23/big/img_477
2002/07/23/big/img_336
2002/07/26/big/img_481
2002/08/20/big/img_409
2002/07/23/big/img_918
2002/08/09/big/img_474
2002/08/02/big/img_929
2002/08/31/big/img_17932
2002/08/19/big/img_161
2002/08/09/big/img_667
2002/07/31/big/img_805
2002/09/02/big/img_15678
2002/08/31/big/img_17509
2002/08/29/big/img_18998
2002/07/23/big/img_301
2002/08/07/big/img_1612
2002/08/06/big/img_2472
2002/07/23/big/img_466
2002/08/27/big/img_19634
2003/01/16/big/img_16
2002/08/14/big/img_193
2002/08/21/big/img_340
2002/08/27/big/img_19799
2002/08/01/big/img_1345
2002/08/07/big/img_1448
2002/08/11/big/img_324
2003/01/16/big/img_754
2002/08/13/big/img_418
2003/01/16/big/img_544
2002/08/19/big/img_135
2002/08/10/big/img_455
2002/08/10/big/img_693
2002/08/31/big/img_17967
2002/08/28/big/img_19229
2002/08/04/big/img_811
2002/09/01/big/img_16225
2003/01/16/big/img_428
2002/09/02/big/img_15295
2002/07/26/big/img_108
2002/07/21/big/img_477
2002/08/07/big/img_1354
2002/08/23/big/img_246
2002/08/16/big/img_652
2002/07/27/big/img_553
2002/07/31/big/img_346
2002/08/04/big/img_537
2002/08/08/big/img_498
2002/08/29/big/img_18956
2003/01/13/big/img_922
2002/08/31/big/img_17425
2002/07/26/big/img_438
2002/08/19/big/img_185
2003/01/16/big/img_33
2002/08/10/big/img_252
2002/07/29/big/img_598
2002/08/27/big/img_19820
2002/08/06/big/img_2664
2002/08/20/big/img_705
2003/01/14/big/img_816
2002/08/03/big/img_552
2002/07/25/big/img_561
2002/07/25/big/img_934
2002/08/01/big/img_1893
2003/01/14/big/img_746
2003/01/16/big/img_519
2002/08/03/big/img_681
2002/07/24/big/img_808
2002/08/14/big/img_803
2002/08/25/big/img_155
2002/07/30/big/img_1107
2002/08/29/big/img_18882
2003/01/15/big/img_598
2002/08/19/big/img_122
2002/07/30/big/img_428
2002/07/24/big/img_684
2002/08/22/big/img_192
2002/08/22/big/img_543
2002/08/07/big/img_1318
2002/08/18/big/img_25
2002/07/26/big/img_583
2002/07/20/big/img_464
2002/08/19/big/img_664
2002/08/24/big/img_861
2002/09/01/big/img_16136
2002/08/22/big/img_400
2002/08/12/big/img_445
2003/01/14/big/img_174
2002/08/27/big/img_19677
2002/08/31/big/img_17214
2002/08/30/big/img_18175
2003/01/17/big/img_402
2002/08/06/big/img_2396
2002/08/18/big/img_448
2002/08/21/big/img_165
2002/08/31/big/img_17609
2003/01/01/big/img_151
2002/08/26/big/img_372
2002/09/02/big/img_15994
2002/07/26/big/img_660
2002/09/02/big/img_15197
2002/07/29/big/img_258
2002/08/30/big/img_18525
2003/01/13/big/img_368
2002/07/29/big/img_1538
2002/07/21/big/img_787
2002/08/18/big/img_152
2002/08/06/big/img_2379
2003/01/17/big/img_864
2002/08/27/big/img_19998
2002/08/01/big/img_1634
2002/07/25/big/img_414
2002/08/22/big/img_627
2002/08/07/big/img_1669
2002/08/16/big/img_1052
2002/08/31/big/img_17796
2002/08/18/big/img_199
2002/09/02/big/img_15147
2002/08/09/big/img_460
2002/08/14/big/img_581
2002/08/30/big/img_18286
2002/07/26/big/img_337
2002/08/18/big/img_589
2003/01/14/big/img_866
2002/07/20/big/img_624
2002/08/01/big/img_1801
2002/07/24/big/img_683
2002/08/09/big/img_725
2003/01/14/big/img_34
2002/07/30/big/img_144
2002/07/30/big/img_706
2002/08/08/big/img_394
2002/08/19/big/img_619
2002/08/06/big/img_2703
2002/08/29/big/img_19034
2002/07/24/big/img_67
2002/08/27/big/img_19841
2002/08/19/big/img_427
2003/01/14/big/img_333
2002/09/01/big/img_16406
2002/07/19/big/img_882
2002/08/17/big/img_238
2003/01/14/big/img_739
2002/07/22/big/img_151
2002/08/21/big/img_743
2002/07/25/big/img_1048
2002/07/30/big/img_395
2003/01/13/big/img_584
2002/08/13/big/img_742
2002/08/13/big/img_1168
2003/01/14/big/img_147
2002/07/26/big/img_803
2002/08/05/big/img_3298
2002/08/07/big/img_1451
2002/08/16/big/img_424
2002/07/29/big/img_1069
2002/09/01/big/img_16735
2002/07/21/big/img_637
2003/01/14/big/img_585
2002/08/02/big/img_358
2003/01/13/big/img_358
2002/08/14/big/img_198
2002/08/17/big/img_935
2002/08/04/big/img_42
2002/08/30/big/img_18245
2002/07/25/big/img_158
2002/08/22/big/img_744
2002/08/06/big/img_2291
2002/08/05/big/img_3044
2002/07/30/big/img_272
2002/08/23/big/img_641
2002/07/24/big/img_797
2002/07/30/big/img_392
2003/01/14/big/img_447
2002/07/31/big/img_898
2002/08/06/big/img_2812
2002/08/13/big/img_564
2002/07/22/big/img_43
2002/07/26/big/img_634
2002/07/19/big/img_843
2002/08/26/big/img_58
2002/07/21/big/img_375
2002/08/25/big/img_729
2002/07/19/big/img_561
2003/01/15/big/img_884
2002/07/25/big/img_891
2002/08/09/big/img_558
2002/08/26/big/img_587
2002/08/13/big/img_1146
2002/09/02/big/img_15153
2002/07/26/big/img_316
2002/08/01/big/img_1940
2002/08/26/big/img_90
2003/01/13/big/img_347
2002/07/25/big/img_520
2002/08/29/big/img_18718
2002/08/28/big/img_19219
2002/08/13/big/img_375
2002/07/20/big/img_719
2002/08/31/big/img_17431
2002/07/28/big/img_192
2002/08/26/big/img_259
2002/08/18/big/img_484
2002/07/29/big/img_580
2002/07/26/big/img_84
2002/08/02/big/img_302
2002/08/31/big/img_17007
2003/01/15/big/img_543
2002/09/01/big/img_16488
2002/08/22/big/img_798
2002/07/30/big/img_383
2002/08/04/big/img_668
2002/08/13/big/img_156
2002/08/07/big/img_1353
2002/07/25/big/img_281
2003/01/14/big/img_587
2003/01/15/big/img_524
2002/08/19/big/img_726
2002/08/21/big/img_709
2002/08/26/big/img_465
2002/07/31/big/img_658
2002/08/28/big/img_19148
2002/07/23/big/img_423
2002/08/16/big/img_758
2002/08/22/big/img_523
2002/08/16/big/img_591
2002/08/23/big/img_845
2002/07/26/big/img_678
2002/08/09/big/img_806
2002/08/06/big/img_2369
2002/07/29/big/img_457
2002/07/19/big/img_278
2002/08/30/big/img_18107
2002/07/26/big/img_444
2002/08/20/big/img_278
2002/08/26/big/img_92
2002/08/26/big/img_257
2002/07/25/big/img_266
2002/08/05/big/img_3829
2002/07/26/big/img_757
2002/07/29/big/img_1536
2002/08/09/big/img_472
2003/01/17/big/img_480
2002/08/28/big/img_19355
2002/07/26/big/img_97
2002/08/06/big/img_2503
2002/07/19/big/img_254
2002/08/01/big/img_1470
2002/08/21/big/img_42
2002/08/20/big/img_217
2002/08/06/big/img_2459
2002/07/19/big/img_552
2002/08/13/big/img_717
2002/08/12/big/img_586
2002/08/20/big/img_411
2003/01/13/big/img_768
2002/08/07/big/img_1747
2002/08/15/big/img_385
2002/08/01/big/img_1648
2002/08/15/big/img_311
2002/08/21/big/img_95
2002/08/09/big/img_108
2002/08/21/big/img_398
2002/08/17/big/img_340
2002/08/14/big/img_474
2002/08/13/big/img_294
2002/08/24/big/img_840
2002/08/09/big/img_808
2002/08/23/big/img_491
2002/07/28/big/img_33
2003/01/13/big/img_664
2002/08/02/big/img_261
2002/08/09/big/img_591
2002/07/26/big/img_309
2003/01/14/big/img_372
2002/08/19/big/img_581
2002/08/19/big/img_168
2002/08/26/big/img_422
2002/07/24/big/img_106
2002/08/01/big/img_1936
2002/08/05/big/img_3764
2002/08/21/big/img_266
2002/08/31/big/img_17968
2002/08/01/big/img_1941
2002/08/15/big/img_550
2002/08/14/big/img_13
2002/07/30/big/img_171
2003/01/13/big/img_490
2002/07/25/big/img_427
2002/07/19/big/img_770
2002/08/12/big/img_759
2003/01/15/big/img_1360
2002/08/05/big/img_3692
2003/01/16/big/img_30
2002/07/25/big/img_1026
2002/07/22/big/img_288
2002/08/29/big/img_18801
2002/07/24/big/img_793
2002/08/13/big/img_178
2002/08/06/big/img_2322
2003/01/14/big/img_560
2002/08/18/big/img_408
2003/01/16/big/img_915
2003/01/16/big/img_679
2002/08/07/big/img_1552
2002/08/29/big/img_19050
2002/08/01/big/img_2172
2002/07/31/big/img_30
2002/07/30/big/img_1019
2002/07/30/big/img_587
2003/01/13/big/img_773
2002/07/30/big/img_410
2002/07/28/big/img_65
2002/08/05/big/img_3138
2002/07/23/big/img_541
2002/08/22/big/img_963
2002/07/27/big/img_657
2002/07/30/big/img_1051
2003/01/16/big/img_150
2002/07/31/big/img_519
2002/08/01/big/img_1961
2002/08/05/big/img_3752
2002/07/23/big/img_631
2003/01/14/big/img_237
2002/07/28/big/img_21
2002/07/22/big/img_813
2002/08/05/big/img_3563
2003/01/17/big/img_620
2002/07/19/big/img_523
2002/07/30/big/img_904
2002/08/29/big/img_18642
2002/08/11/big/img_492
2002/08/01/big/img_2130
2002/07/25/big/img_618
2002/08/17/big/img_305
2003/01/16/big/img_520
2002/07/26/big/img_495
2002/08/17/big/img_164
2002/08/03/big/img_440
2002/07/24/big/img_441
2002/08/06/big/img_2146
2002/08/11/big/img_558
2002/08/02/big/img_545
2002/08/31/big/img_18090
2003/01/01/big/img_136
2002/07/25/big/img_1099
2003/01/13/big/img_728
2003/01/16/big/img_197
2002/07/26/big/img_651
2002/08/11/big/img_676
2003/01/15/big/img_10
2002/08/21/big/img_250
2002/08/14/big/img_325
2002/08/04/big/img_390
2002/07/24/big/img_554
2003/01/16/big/img_333
2002/07/31/big/img_922
2002/09/02/big/img_15586
2003/01/16/big/img_184
2002/07/22/big/img_766
2002/07/21/big/img_608
2002/08/07/big/img_1578
2002/08/17/big/img_961
2002/07/27/big/img_324
2002/08/05/big/img_3765
2002/08/23/big/img_462
2003/01/16/big/img_382
2002/08/27/big/img_19838
2002/08/01/big/img_1505
2002/08/21/big/img_662
2002/08/14/big/img_605
2002/08/19/big/img_816
2002/07/29/big/img_136
2002/08/20/big/img_719
2002/08/06/big/img_2826
2002/08/10/big/img_630
2003/01/17/big/img_973
2002/08/14/big/img_116
2002/08/02/big/img_666
2002/08/21/big/img_710
2002/08/05/big/img_55
2002/07/31/big/img_229
2002/08/01/big/img_1549
2002/07/23/big/img_432
2002/07/21/big/img_430
2002/08/21/big/img_549
2002/08/08/big/img_985
2002/07/20/big/img_610
2002/07/23/big/img_978
2002/08/23/big/img_219
2002/07/25/big/img_175
2003/01/15/big/img_230
2002/08/23/big/img_385
2002/07/31/big/img_879
2002/08/12/big/img_495
2002/08/22/big/img_499
2002/08/30/big/img_18322
2002/08/15/big/img_795
2002/08/13/big/img_835
2003/01/17/big/img_930
2002/07/30/big/img_873
2002/08/11/big/img_257
2002/07/31/big/img_593
2002/08/21/big/img_916
2003/01/13/big/img_814
2002/07/25/big/img_722
2002/08/16/big/img_379
2002/07/31/big/img_497
2002/07/22/big/img_602
2002/08/21/big/img_642
2002/08/21/big/img_614
2002/08/23/big/img_482
2002/07/29/big/img_603
2002/08/13/big/img_705
2002/07/23/big/img_833
2003/01/14/big/img_511
2002/07/24/big/img_376
2002/08/17/big/img_1030
2002/08/05/big/img_3576
2002/08/16/big/img_540
2002/07/22/big/img_630
2002/08/10/big/img_180
2002/08/14/big/img_905
2002/08/29/big/img_18777
2002/08/22/big/img_693
2003/01/16/big/img_933
2002/08/20/big/img_555
2002/08/15/big/img_549
2003/01/14/big/img_830
2003/01/16/big/img_64
2002/08/27/big/img_19670
2002/08/22/big/img_729
2002/07/27/big/img_981
2002/08/09/big/img_458
2003/01/17/big/img_884
2002/07/25/big/img_639
2002/08/31/big/img_18008
2002/08/22/big/img_249
2002/08/17/big/img_971
2002/08/04/big/img_308
2002/07/28/big/img_362
2002/08/12/big/img_142
2002/08/26/big/img_61
2002/08/14/big/img_422
2002/07/19/big/img_607
2003/01/15/big/img_717
2002/08/01/big/img_1475
2002/08/29/big/img_19061
2003/01/01/big/img_346
2002/07/20/big/img_315
2003/01/15/big/img_756
2002/08/15/big/img_879
2002/08/08/big/img_615
2003/01/13/big/img_431
2002/08/05/big/img_3233
2002/08/24/big/img_526
2003/01/13/big/img_717
2002/09/01/big/img_16408
2002/07/22/big/img_217
2002/07/31/big/img_960
2002/08/21/big/img_610
2002/08/05/big/img_3753
2002/08/03/big/img_151
2002/08/21/big/img_267
2002/08/01/big/img_2175
2002/08/04/big/img_556
2002/08/21/big/img_527
2002/09/02/big/img_15800
2002/07/27/big/img_156
2002/07/20/big/img_590
2002/08/15/big/img_700
2002/08/08/big/img_444
2002/07/25/big/img_94
2002/07/24/big/img_778
2002/08/14/big/img_694
2002/07/20/big/img_666
2002/08/02/big/img_200
2002/08/02/big/img_578
2003/01/17/big/img_332
2002/09/01/big/img_16352
2002/08/27/big/img_19668
2002/07/23/big/img_823
2002/08/13/big/img_431
2003/01/16/big/img_463
2002/08/27/big/img_19711
2002/08/23/big/img_154
2002/07/31/big/img_360
2002/08/23/big/img_555
2002/08/10/big/img_561
2003/01/14/big/img_550
2002/08/07/big/img_1370
2002/07/30/big/img_1184
2002/08/01/big/img_1445
2002/08/23/big/img_22
2002/07/30/big/img_606
2003/01/17/big/img_271
2002/08/31/big/img_17316
2002/08/16/big/img_973
2002/07/26/big/img_77
2002/07/20/big/img_788
2002/08/06/big/img_2426
2002/08/07/big/img_1498
2002/08/16/big/img_358
2002/08/06/big/img_2851
2002/08/12/big/img_359
2002/08/01/big/img_1521
2002/08/02/big/img_709
2002/08/20/big/img_935
2002/08/12/big/img_188
2002/08/24/big/img_411
2002/08/22/big/img_680
2002/08/06/big/img_2480
2002/07/20/big/img_627
2002/07/30/big/img_214
2002/07/25/big/img_354
2002/08/02/big/img_636
2003/01/15/big/img_661
2002/08/07/big/img_1327
2002/08/01/big/img_2108
2002/08/31/big/img_17919
2002/08/29/big/img_18768
2002/08/05/big/img_3840
2002/07/26/big/img_242
2003/01/14/big/img_451
2002/08/20/big/img_923
2002/08/27/big/img_19908
2002/08/16/big/img_282
2002/08/19/big/img_440
2003/01/01/big/img_230
2002/08/08/big/img_212
2002/07/20/big/img_443
2002/08/25/big/img_635
2003/01/13/big/img_1169
2002/07/26/big/img_998
2002/08/15/big/img_995
2002/08/06/big/img_3002
2002/07/29/big/img_460
2003/01/14/big/img_925
2002/07/23/big/img_539
2002/08/16/big/img_694
2003/01/13/big/img_459
2002/07/23/big/img_249
2002/08/20/big/img_539
2002/08/04/big/img_186
2002/08/26/big/img_264
2002/07/22/big/img_704
2002/08/25/big/img_277
2002/08/22/big/img_988
2002/07/29/big/img_504
2002/08/05/big/img_3600
2002/08/30/big/img_18380
2003/01/14/big/img_937
2002/08/21/big/img_254
2002/08/10/big/img_130
2002/08/20/big/img_339
2003/01/14/big/img_428
2002/08/20/big/img_889
2002/08/31/big/img_17637
2002/07/26/big/img_644
2002/09/01/big/img_16776
2002/08/06/big/img_2239
2002/08/06/big/img_2646
2003/01/13/big/img_491
2002/08/10/big/img_579
2002/08/21/big/img_713
2002/08/22/big/img_482
2002/07/22/big/img_167
2002/07/24/big/img_539
2002/08/14/big/img_721
2002/07/25/big/img_389
2002/09/01/big/img_16591
2002/08/13/big/img_543
2003/01/14/big/img_432
2002/08/09/big/img_287
2002/07/26/big/img_126
2002/08/23/big/img_412
2002/08/15/big/img_1034
2002/08/28/big/img_19485
2002/07/31/big/img_236
2002/07/30/big/img_523
2002/07/19/big/img_141
2003/01/17/big/img_957
2002/08/04/big/img_81
2002/07/25/big/img_206
2002/08/15/big/img_716
2002/08/13/big/img_403
2002/08/15/big/img_685
2002/07/26/big/img_884
2002/07/19/big/img_499
2002/07/23/big/img_772
2002/07/27/big/img_752
2003/01/14/big/img_493
2002/08/25/big/img_664
2002/07/31/big/img_334
2002/08/26/big/img_678
2002/09/01/big/img_16541
2003/01/14/big/img_347
2002/07/23/big/img_187
2002/07/30/big/img_1163
2002/08/05/big/img_35
2002/08/22/big/img_944
2002/08/07/big/img_1239
2002/07/29/big/img_1215
2002/08/03/big/img_312
2002/08/05/big/img_3523
2002/07/29/big/img_218
2002/08/13/big/img_672
2002/08/16/big/img_205
2002/08/17/big/img_594
2002/07/29/big/img_1411
2002/07/30/big/img_942
2003/01/16/big/img_312
2002/08/08/big/img_312
2002/07/25/big/img_15
2002/08/09/big/img_839
2002/08/01/big/img_2069
2002/08/31/big/img_17512
2002/08/01/big/img_3
2002/07/31/big/img_320
2003/01/15/big/img_1265
2002/08/14/big/img_563
2002/07/31/big/img_167
2002/08/20/big/img_374
2002/08/13/big/img_406
2002/08/08/big/img_625
2002/08/02/big/img_314
2002/08/27/big/img_19964
2002/09/01/big/img_16670
2002/07/31/big/img_599
2002/08/29/big/img_18906
2002/07/24/big/img_373
2002/07/26/big/img_513
2002/09/02/big/img_15497
2002/08/19/big/img_117
2003/01/01/big/img_158
2002/08/24/big/img_178
2003/01/13/big/img_935
2002/08/13/big/img_609
2002/08/30/big/img_18341
2002/08/25/big/img_674
2003/01/13/big/img_209
2002/08/13/big/img_258
2002/08/05/big/img_3543
2002/08/07/big/img_1970
2002/08/06/big/img_3004
2003/01/17/big/img_487
2002/08/24/big/img_873
2002/08/29/big/img_18730
2002/08/09/big/img_375
2003/01/16/big/img_751
2002/08/02/big/img_603
2002/08/19/big/img_325
2002/09/01/big/img_16420
2002/08/05/big/img_3633
2002/08/21/big/img_516
2002/07/19/big/img_501
2002/07/26/big/img_688
2002/07/24/big/img_256
2002/07/25/big/img_438
2002/07/31/big/img_1017
2002/08/22/big/img_512
2002/07/21/big/img_543
2002/08/08/big/img_223
2002/08/19/big/img_189
2002/08/12/big/img_630
2002/07/30/big/img_958
2002/07/28/big/img_208
2002/08/31/big/img_17691
2002/07/22/big/img_542
2002/07/19/big/img_741
2002/07/19/big/img_158
2002/08/15/big/img_399
2002/08/01/big/img_2159
2002/08/14/big/img_455
2002/08/17/big/img_1011
2002/08/26/big/img_744
2002/08/12/big/img_624
2003/01/17/big/img_821
2002/08/16/big/img_980
2002/07/28/big/img_281
2002/07/25/big/img_171
2002/08/03/big/img_116
2002/07/22/big/img_467
2002/07/31/big/img_750
2002/07/26/big/img_435
2002/07/19/big/img_822
2002/08/13/big/img_626
2002/08/11/big/img_344
2002/08/02/big/img_473
2002/09/01/big/img_16817
2002/08/01/big/img_1275
2002/08/28/big/img_19270
2002/07/23/big/img_607
2002/08/09/big/img_316
2002/07/29/big/img_626
2002/07/24/big/img_824
2002/07/22/big/img_342
2002/08/08/big/img_794
2002/08/07/big/img_1209
2002/07/19/big/img_18
2002/08/25/big/img_634
2002/07/24/big/img_730
2003/01/17/big/img_356
2002/07/23/big/img_305
2002/07/30/big/img_453
2003/01/13/big/img_972
2002/08/06/big/img_2610
2002/08/29/big/img_18920
2002/07/31/big/img_123
2002/07/26/big/img_979
2002/08/24/big/img_635
2002/08/05/big/img_3704
2002/08/07/big/img_1358
2002/07/22/big/img_306
2002/08/13/big/img_619
2002/08/02/big/img_366123from .wider_face import WiderFaceDetection, detection_collate
from .data_augment import *
from .config import *123456789101112131415161718192021222324252627282930313233343536373839404142# config.py
cfg_mnet = {
'name': 'mobilenet0.25',
'min_sizes': [[16, 32], [64, 128], [256, 512]],
'steps': [8, 16, 32],
'variance': [0.1, 0.2],
'clip': False,
'loc_weight': 2.0,
'gpu_train': True,
'batch_size': 32,
'ngpu': 1,
'epoch': 250,
'decay1': 190,
'decay2': 220,
'image_size': 640,
'pretrain': False,
'return_layers': {'stage1': 1, 'stage2': 2, 'stage3': 3},
'in_channel': 32,
'out_channel': 64
}
cfg_re50 = {
'name': 'Resnet50',
'min_sizes': [[16, 32], [64, 128], [256, 512]],
'steps': [8, 16, 32],
'variance': [0.1, 0.2],
'clip': False,
'loc_weight': 2.0,
'gpu_train': True,
'batch_size': 24,
'ngpu': 4,
'epoch': 100,
'decay1': 70,
'decay2': 90,
'image_size': 840,
'pretrain': False,
'return_layers': {'layer2': 1, 'layer3': 2, 'layer4': 3},
'in_channel': 256,
'out_channel': 256
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237import cv2
import numpy as np
import random
from face_detect.utils.box_utils import matrix_iof
def _crop(image, boxes, labels, landm, img_dim):
height, width, _ = image.shape
pad_image_flag = True
for _ in range(250):
"""
if random.uniform(0, 1) <= 0.2:
scale = 1.0
else:
scale = random.uniform(0.3, 1.0)
"""
PRE_SCALES = [0.3, 0.45, 0.6, 0.8, 1.0]
scale = random.choice(PRE_SCALES)
short_side = min(width, height)
w = int(scale * short_side)
h = w
if width == w:
l = 0
else:
l = random.randrange(width - w)
if height == h:
t = 0
else:
t = random.randrange(height - h)
roi = np.array((l, t, l + w, t + h))
value = matrix_iof(boxes, roi[np.newaxis])
flag = (value >= 1)
if not flag.any():
continue
centers = (boxes[:, :2] + boxes[:, 2:]) / 2
mask_a = np.logical_and(roi[:2] < centers, centers < roi[2:]).all(axis=1)
boxes_t = boxes[mask_a].copy()
labels_t = labels[mask_a].copy()
landms_t = landm[mask_a].copy()
landms_t = landms_t.reshape([-1, 5, 2])
if boxes_t.shape[0] == 0:
continue
image_t = image[roi[1]:roi[3], roi[0]:roi[2]]
boxes_t[:, :2] = np.maximum(boxes_t[:, :2], roi[:2])
boxes_t[:, :2] -= roi[:2]
boxes_t[:, 2:] = np.minimum(boxes_t[:, 2:], roi[2:])
boxes_t[:, 2:] -= roi[:2]
# landm
landms_t[:, :, :2] = landms_t[:, :, :2] - roi[:2]
landms_t[:, :, :2] = np.maximum(landms_t[:, :, :2], np.array([0, 0]))
landms_t[:, :, :2] = np.minimum(landms_t[:, :, :2], roi[2:] - roi[:2])
landms_t = landms_t.reshape([-1, 10])
# make sure that the cropped image contains at least one face > 16 pixel at training image scale
b_w_t = (boxes_t[:, 2] - boxes_t[:, 0] + 1) / w * img_dim
b_h_t = (boxes_t[:, 3] - boxes_t[:, 1] + 1) / h * img_dim
mask_b = np.minimum(b_w_t, b_h_t) > 0.0
boxes_t = boxes_t[mask_b]
labels_t = labels_t[mask_b]
landms_t = landms_t[mask_b]
if boxes_t.shape[0] == 0:
continue
pad_image_flag = False
return image_t, boxes_t, labels_t, landms_t, pad_image_flag
return image, boxes, labels, landm, pad_image_flag
def _distort(image):
def _convert(image, alpha=1, beta=0):
tmp = image.astype(float) * alpha + beta
tmp[tmp < 0] = 0
tmp[tmp > 255] = 255
image[:] = tmp
image = image.copy()
if random.randrange(2):
#brightness distortion
if random.randrange(2):
_convert(image, beta=random.uniform(-32, 32))
#contrast distortion
if random.randrange(2):
_convert(image, alpha=random.uniform(0.5, 1.5))
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#saturation distortion
if random.randrange(2):
_convert(image[:, :, 1], alpha=random.uniform(0.5, 1.5))
#hue distortion
if random.randrange(2):
tmp = image[:, :, 0].astype(int) + random.randint(-18, 18)
tmp %= 180
image[:, :, 0] = tmp
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
else:
#brightness distortion
if random.randrange(2):
_convert(image, beta=random.uniform(-32, 32))
image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#saturation distortion
if random.randrange(2):
_convert(image[:, :, 1], alpha=random.uniform(0.5, 1.5))
#hue distortion
if random.randrange(2):
tmp = image[:, :, 0].astype(int) + random.randint(-18, 18)
tmp %= 180
image[:, :, 0] = tmp
image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR)
#contrast distortion
if random.randrange(2):
_convert(image, alpha=random.uniform(0.5, 1.5))
return image
def _expand(image, boxes, fill, p):
if random.randrange(2):
return image, boxes
height, width, depth = image.shape
scale = random.uniform(1, p)
w = int(scale * width)
h = int(scale * height)
left = random.randint(0, w - width)
top = random.randint(0, h - height)
boxes_t = boxes.copy()
boxes_t[:, :2] += (left, top)
boxes_t[:, 2:] += (left, top)
expand_image = np.empty(
(h, w, depth),
dtype=image.dtype)
expand_image[:, :] = fill
expand_image[top:top + height, left:left + width] = image
image = expand_image
return image, boxes_t
def _mirror(image, boxes, landms):
_, width, _ = image.shape
if random.randrange(2):
image = image[:, ::-1]
boxes = boxes.copy()
boxes[:, 0::2] = width - boxes[:, 2::-2]
# landm
landms = landms.copy()
landms = landms.reshape([-1, 5, 2])
landms[:, :, 0] = width - landms[:, :, 0]
tmp = landms[:, 1, :].copy()
landms[:, 1, :] = landms[:, 0, :]
landms[:, 0, :] = tmp
tmp1 = landms[:, 4, :].copy()
landms[:, 4, :] = landms[:, 3, :]
landms[:, 3, :] = tmp1
landms = landms.reshape([-1, 10])
return image, boxes, landms
def _pad_to_square(image, rgb_mean, pad_image_flag):
if not pad_image_flag:
return image
height, width, _ = image.shape
long_side = max(width, height)
image_t = np.empty((long_side, long_side, 3), dtype=image.dtype)
image_t[:, :] = rgb_mean
image_t[0:0 + height, 0:0 + width] = image
return image_t
def _resize_subtract_mean(image, insize, rgb_mean):
interp_methods = [cv2.INTER_LINEAR, cv2.INTER_CUBIC, cv2.INTER_AREA, cv2.INTER_NEAREST, cv2.INTER_LANCZOS4]
interp_method = interp_methods[random.randrange(5)]
image = cv2.resize(image, (insize, insize), interpolation=interp_method)
image = image.astype(np.float32)
image -= rgb_mean
return image.transpose(2, 0, 1)
class preproc(object):
def __init__(self, img_dim, rgb_means):
self.img_dim = img_dim
self.rgb_means = rgb_means
def __call__(self, image, targets):
assert targets.shape[0] > 0, "this image does not have gt"
boxes = targets[:, :4].copy()
labels = targets[:, -1].copy()
landm = targets[:, 4:-1].copy()
image_t, boxes_t, labels_t, landm_t, pad_image_flag = _crop(image, boxes, labels, landm, self.img_dim)
image_t = _distort(image_t)
image_t = _pad_to_square(image_t,self.rgb_means, pad_image_flag)
image_t, boxes_t, landm_t = _mirror(image_t, boxes_t, landm_t)
height, width, _ = image_t.shape
image_t = _resize_subtract_mean(image_t, self.img_dim, self.rgb_means)
boxes_t[:, 0::2] /= width
boxes_t[:, 1::2] /= height
landm_t[:, 0::2] /= width
landm_t[:, 1::2] /= height
labels_t = np.expand_dims(labels_t, 1)
targets_t = np.hstack((boxes_t, landm_t, labels_t))
return image_t, targets_t123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import os
import os.path
import sys
import torch
import torch.utils.data as data
import cv2
import numpy as np
class WiderFaceDetection(data.Dataset):
def __init__(self, txt_path, preproc=None):
self.preproc = preproc
self.imgs_path = []
self.words = []
f = open(txt_path,'r')
lines = f.readlines()
isFirst = True
labels = []
for line in lines:
line = line.rstrip()
if line.startswith('#'):
if isFirst is True:
isFirst = False
else:
labels_copy = labels.copy()
self.words.append(labels_copy)
labels.clear()
path = line[2:]
path = txt_path.replace('label.txt','images/') + path
self.imgs_path.append(path)
else:
line = line.split(' ')
label = [float(x) for x in line]
labels.append(label)
self.words.append(labels)
def __len__(self):
return len(self.imgs_path)
def __getitem__(self, index):
img = cv2.imread(self.imgs_path[index])
height, width, _ = img.shape
labels = self.words[index]
annotations = np.zeros((0, 15))
if len(labels) == 0:
return annotations
for idx, label in enumerate(labels):
annotation = np.zeros((1, 15))
# bbox
annotation[0, 0] = label[0] # x1
annotation[0, 1] = label[1] # y1
annotation[0, 2] = label[0] + label[2] # x2
annotation[0, 3] = label[1] + label[3] # y2
# landmarks
annotation[0, 4] = label[4] # l0_x
annotation[0, 5] = label[5] # l0_y
annotation[0, 6] = label[7] # l1_x
annotation[0, 7] = label[8] # l1_y
annotation[0, 8] = label[10] # l2_x
annotation[0, 9] = label[11] # l2_y
annotation[0, 10] = label[13] # l3_x
annotation[0, 11] = label[14] # l3_y
annotation[0, 12] = label[16] # l4_x
annotation[0, 13] = label[17] # l4_y
if (annotation[0, 4]<0):
annotation[0, 14] = -1
else:
annotation[0, 14] = 1
annotations = np.append(annotations, annotation, axis=0)
target = np.array(annotations)
if self.preproc is not None:
img, target = self.preproc(img, target)
return torch.from_numpy(img), target
def detection_collate(batch):
"""Custom collate fn for dealing with batches of images that have a different
number of associated object annotations (bounding boxes).
Arguments:
batch: (tuple) A tuple of tensor images and lists of annotations
Return:
A tuple containing:
1) (tensor) batch of images stacked on their 0 dim
2) (list of tensors) annotations for a given image are stacked on 0 dim
"""
targets = []
imgs = []
for _, sample in enumerate(batch):
for _, tup in enumerate(sample):
if torch.is_tensor(tup):
imgs.append(tup)
elif isinstance(tup, type(np.empty(0))):
annos = torch.from_numpy(tup).float()
targets.append(annos)
return (torch.stack(imgs, 0), targets)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137import time
import torch
import torch.nn as nn
import torchvision.models._utils as _utils
import torchvision.models as models
import torch.nn.functional as F
from torch.autograd import Variable
def conv_bn(inp, oup, stride = 1, leaky = 0):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
nn.LeakyReLU(negative_slope=leaky, inplace=True)
)
def conv_bn_no_relu(inp, oup, stride):
return nn.Sequential(
nn.Conv2d(inp, oup, 3, stride, 1, bias=False),
nn.BatchNorm2d(oup),
)
def conv_bn1X1(inp, oup, stride, leaky=0):
return nn.Sequential(
nn.Conv2d(inp, oup, 1, stride, padding=0, bias=False),
nn.BatchNorm2d(oup),
nn.LeakyReLU(negative_slope=leaky, inplace=True)
)
def conv_dw(inp, oup, stride, leaky=0.1):
return nn.Sequential(
nn.Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False),
nn.BatchNorm2d(inp),
nn.LeakyReLU(negative_slope= leaky,inplace=True),
nn.Conv2d(inp, oup, 1, 1, 0, bias=False),
nn.BatchNorm2d(oup),
nn.LeakyReLU(negative_slope= leaky,inplace=True),
)
class SSH(nn.Module):
def __init__(self, in_channel, out_channel):
super(SSH, self).__init__()
assert out_channel % 4 == 0
leaky = 0
if (out_channel <= 64):
leaky = 0.1
self.conv3X3 = conv_bn_no_relu(in_channel, out_channel//2, stride=1)
self.conv5X5_1 = conv_bn(in_channel, out_channel//4, stride=1, leaky = leaky)
self.conv5X5_2 = conv_bn_no_relu(out_channel//4, out_channel//4, stride=1)
self.conv7X7_2 = conv_bn(out_channel//4, out_channel//4, stride=1, leaky = leaky)
self.conv7x7_3 = conv_bn_no_relu(out_channel//4, out_channel//4, stride=1)
def forward(self, input):
conv3X3 = self.conv3X3(input)
conv5X5_1 = self.conv5X5_1(input)
conv5X5 = self.conv5X5_2(conv5X5_1)
conv7X7_2 = self.conv7X7_2(conv5X5_1)
conv7X7 = self.conv7x7_3(conv7X7_2)
out = torch.cat([conv3X3, conv5X5, conv7X7], dim=1)
out = F.relu(out)
return out
class FPN(nn.Module):
def __init__(self,in_channels_list,out_channels):
super(FPN,self).__init__()
leaky = 0
if (out_channels <= 64):
leaky = 0.1
self.output1 = conv_bn1X1(in_channels_list[0], out_channels, stride = 1, leaky = leaky)
self.output2 = conv_bn1X1(in_channels_list[1], out_channels, stride = 1, leaky = leaky)
self.output3 = conv_bn1X1(in_channels_list[2], out_channels, stride = 1, leaky = leaky)
self.merge1 = conv_bn(out_channels, out_channels, leaky = leaky)
self.merge2 = conv_bn(out_channels, out_channels, leaky = leaky)
def forward(self, input):
# names = list(input.keys())
input = list(input.values())
output1 = self.output1(input[0])
output2 = self.output2(input[1])
output3 = self.output3(input[2])
up3 = F.interpolate(output3, size=[output2.size(2), output2.size(3)], mode="nearest")
output2 = output2 + up3
output2 = self.merge2(output2)
up2 = F.interpolate(output2, size=[output1.size(2), output1.size(3)], mode="nearest")
output1 = output1 + up2
output1 = self.merge1(output1)
out = [output1, output2, output3]
return out
class MobileNetV1(nn.Module):
def __init__(self):
super(MobileNetV1, self).__init__()
self.stage1 = nn.Sequential(
conv_bn(3, 8, 2, leaky = 0.1), # 3
conv_dw(8, 16, 1), # 7
conv_dw(16, 32, 2), # 11
conv_dw(32, 32, 1), # 19
conv_dw(32, 64, 2), # 27
conv_dw(64, 64, 1), # 43
)
self.stage2 = nn.Sequential(
conv_dw(64, 128, 2), # 43 + 16 = 59
conv_dw(128, 128, 1), # 59 + 32 = 91
conv_dw(128, 128, 1), # 91 + 32 = 123
conv_dw(128, 128, 1), # 123 + 32 = 155
conv_dw(128, 128, 1), # 155 + 32 = 187
conv_dw(128, 128, 1), # 187 + 32 = 219
)
self.stage3 = nn.Sequential(
conv_dw(128, 256, 2), # 219 +3 2 = 241
conv_dw(256, 256, 1), # 241 + 64 = 301
)
self.avg = nn.AdaptiveAvgPool2d((1,1))
self.fc = nn.Linear(256, 1000)
def forward(self, x):
x = self.stage1(x)
x = self.stage2(x)
x = self.stage3(x)
x = self.avg(x)
# x = self.model(x)
x = x.view(-1, 256)
x = self.fc(x)
return x
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127import torch
import torch.nn as nn
import torchvision.models.detection.backbone_utils as backbone_utils
import torchvision.models._utils as _utils
import torch.nn.functional as F
from collections import OrderedDict
from face_detect.facemodels.net import MobileNetV1 as MobileNetV1
from face_detect.facemodels.net import FPN as FPN
from face_detect.facemodels.net import SSH as SSH
class ClassHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(ClassHead,self).__init__()
self.num_anchors = num_anchors
self.conv1x1 = nn.Conv2d(inchannels,self.num_anchors*2,kernel_size=(1,1),stride=1,padding=0)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1).contiguous()
return out.view(out.shape[0], -1, 2)
class BboxHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(BboxHead,self).__init__()
self.conv1x1 = nn.Conv2d(inchannels,num_anchors*4,kernel_size=(1,1),stride=1,padding=0)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1).contiguous()
return out.view(out.shape[0], -1, 4)
class LandmarkHead(nn.Module):
def __init__(self,inchannels=512,num_anchors=3):
super(LandmarkHead,self).__init__()
self.conv1x1 = nn.Conv2d(inchannels,num_anchors*10,kernel_size=(1,1),stride=1,padding=0)
def forward(self,x):
out = self.conv1x1(x)
out = out.permute(0,2,3,1).contiguous()
return out.view(out.shape[0], -1, 10)
class RetinaFace(nn.Module):
def __init__(self, cfg = None, phase = 'train'):
"""
:param cfg: Network related settings.
:param phase: train or test.
"""
super(RetinaFace,self).__init__()
self.phase = phase
backbone = None
if cfg['name'] == 'mobilenet0.25':
backbone = MobileNetV1()
if cfg['pretrain']:
checkpoint = torch.load("./weights/mobilenetV1X0.25_pretrain.tar", map_location=torch.device('cpu'))
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in checkpoint['state_dict'].items():
name = k[7:] # remove module.
new_state_dict[name] = v
# load params
backbone.load_state_dict(new_state_dict)
elif cfg['name'] == 'Resnet50':
import torchvision.models as models
backbone = models.resnet50(pretrained=cfg['pretrain'])
self.body = _utils.IntermediateLayerGetter(backbone, cfg['return_layers'])
in_channels_stage2 = cfg['in_channel']
in_channels_list = [
in_channels_stage2 * 2,
in_channels_stage2 * 4,
in_channels_stage2 * 8,
]
out_channels = cfg['out_channel']
self.fpn = FPN(in_channels_list,out_channels)
self.ssh1 = SSH(out_channels, out_channels)
self.ssh2 = SSH(out_channels, out_channels)
self.ssh3 = SSH(out_channels, out_channels)
self.ClassHead = self._make_class_head(fpn_num=3, inchannels=cfg['out_channel'])
self.BboxHead = self._make_bbox_head(fpn_num=3, inchannels=cfg['out_channel'])
self.LandmarkHead = self._make_landmark_head(fpn_num=3, inchannels=cfg['out_channel'])
def _make_class_head(self,fpn_num=3,inchannels=64,anchor_num=2):
classhead = nn.ModuleList()
for i in range(fpn_num):
classhead.append(ClassHead(inchannels,anchor_num))
return classhead
def _make_bbox_head(self,fpn_num=3,inchannels=64,anchor_num=2):
bboxhead = nn.ModuleList()
for i in range(fpn_num):
bboxhead.append(BboxHead(inchannels,anchor_num))
return bboxhead
def _make_landmark_head(self,fpn_num=3,inchannels=64,anchor_num=2):
landmarkhead = nn.ModuleList()
for i in range(fpn_num):
landmarkhead.append(LandmarkHead(inchannels,anchor_num))
return landmarkhead
def forward(self,inputs):
out = self.body(inputs)
# FPN
fpn = self.fpn(out)
# SSH
feature1 = self.ssh1(fpn[0])
feature2 = self.ssh2(fpn[1])
feature3 = self.ssh3(fpn[2])
features = [feature1, feature2, feature3]
bbox_regressions = torch.cat([self.BboxHead[i](feature) for i, feature in enumerate(features)], dim=1)
classifications = torch.cat([self.ClassHead[i](feature) for i, feature in enumerate(features)],dim=1)
ldm_regressions = torch.cat([self.LandmarkHead[i](feature) for i, feature in enumerate(features)], dim=1)
if self.phase == 'train':
output = (bbox_regressions, classifications, ldm_regressions)
else:
output = (bbox_regressions, F.softmax(classifications, dim=-1), ldm_regressions)
return output12from .functions import *
from .modules import *12345678910111213141516171819202122232425262728293031323334import torch
from itertools import product as product
import numpy as np
from math import ceil
class PriorBox(object):
def __init__(self, cfg, image_size=None, phase='train'):
super(PriorBox, self).__init__()
self.min_sizes = cfg['min_sizes']
self.steps = cfg['steps']
self.clip = cfg['clip']
self.image_size = image_size
self.feature_maps = [[ceil(self.image_size[0]/step), ceil(self.image_size[1]/step)] for step in self.steps]
self.name = "s"
def forward(self):
anchors = []
for k, f in enumerate(self.feature_maps):
min_sizes = self.min_sizes[k]
for i, j in product(range(f[0]), range(f[1])):
for min_size in min_sizes:
s_kx = min_size / self.image_size[1]
s_ky = min_size / self.image_size[0]
dense_cx = [x * self.steps[k] / self.image_size[1] for x in [j + 0.5]]
dense_cy = [y * self.steps[k] / self.image_size[0] for y in [i + 0.5]]
for cy, cx in product(dense_cy, dense_cx):
anchors += [cx, cy, s_kx, s_ky]
# back to torch land
output = torch.Tensor(anchors).view(-1, 4)
if self.clip:
output.clamp_(max=1, min=0)
return output123from .multibox_loss import MultiBoxLoss
__all__ = ['MultiBoxLoss']123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
from face_detect.utils.box_utils import match, log_sum_exp
from face_detect.data import cfg_mnet
GPU = cfg_mnet['gpu_train']
class MultiBoxLoss(nn.Module):
"""SSD Weighted Loss Function
Compute Targets:
1) Produce Confidence Target Indices by matching ground truth boxes
with (default) 'priorboxes' that have jaccard index > threshold parameter
(default threshold: 0.5).
2) Produce localization target by 'encoding' variance into offsets of ground
truth boxes and their matched 'priorboxes'.
3) Hard negative mining to filter the excessive number of negative examples
that comes with using a large number of default bounding boxes.
(default negative:positive ratio 3:1)
Objective Loss:
L(x,c,l,g) = (Lconf(x, c) + αLloc(x,l,g)) / N
Where, Lconf is the CrossEntropy Loss and Lloc is the SmoothL1 Loss
weighted by α which is set to 1 by cross val.
Args:
c: class confidences,
l: predicted boxes,
g: ground truth boxes
N: number of matched default boxes
See: https://arxiv.org/pdf/1512.02325.pdf for more details.
"""
def __init__(self, num_classes, overlap_thresh, prior_for_matching, bkg_label, neg_mining, neg_pos, neg_overlap, encode_target):
super(MultiBoxLoss, self).__init__()
self.num_classes = num_classes
self.threshold = overlap_thresh
self.background_label = bkg_label
self.encode_target = encode_target
self.use_prior_for_matching = prior_for_matching
self.do_neg_mining = neg_mining
self.negpos_ratio = neg_pos
self.neg_overlap = neg_overlap
self.variance = [0.1, 0.2]
def forward(self, predictions, priors, targets):
"""Multibox Loss
Args:
predictions (tuple): A tuple containing loc preds, conf preds,
and prior boxes from SSD net.
conf shape: torch.size(batch_size,num_priors,num_classes)
loc shape: torch.size(batch_size,num_priors,4)
priors shape: torch.size(num_priors,4)
ground_truth (tensor): Ground truth boxes and labels for a batch,
shape: [batch_size,num_objs,5] (last idx is the label).
"""
loc_data, conf_data, landm_data = predictions
priors = priors
num = loc_data.size(0)
num_priors = (priors.size(0))
# match priors (default boxes) and ground truth boxes
loc_t = torch.Tensor(num, num_priors, 4)
landm_t = torch.Tensor(num, num_priors, 10)
conf_t = torch.LongTensor(num, num_priors)
for idx in range(num):
truths = targets[idx][:, :4].data
labels = targets[idx][:, -1].data
landms = targets[idx][:, 4:14].data
defaults = priors.data
match(self.threshold, truths, defaults, self.variance, labels, landms, loc_t, conf_t, landm_t, idx)
if GPU:
loc_t = loc_t.cuda()
conf_t = conf_t.cuda()
landm_t = landm_t.cuda()
zeros = torch.tensor(0).cuda()
# landm Loss (Smooth L1)
# Shape: [batch,num_priors,10]
pos1 = conf_t > zeros
num_pos_landm = pos1.long().sum(1, keepdim=True)
N1 = max(num_pos_landm.data.sum().float(), 1)
pos_idx1 = pos1.unsqueeze(pos1.dim()).expand_as(landm_data)
landm_p = landm_data[pos_idx1].view(-1, 10)
landm_t = landm_t[pos_idx1].view(-1, 10)
loss_landm = F.smooth_l1_loss(landm_p, landm_t, reduction='sum')
pos = conf_t != zeros
conf_t[pos] = 1
# Localization Loss (Smooth L1)
# Shape: [batch,num_priors,4]
pos_idx = pos.unsqueeze(pos.dim()).expand_as(loc_data)
loc_p = loc_data[pos_idx].view(-1, 4)
loc_t = loc_t[pos_idx].view(-1, 4)
loss_l = F.smooth_l1_loss(loc_p, loc_t, reduction='sum')
# Compute max conf across batch for hard negative mining
batch_conf = conf_data.view(-1, self.num_classes)
loss_c = log_sum_exp(batch_conf) - batch_conf.gather(1, conf_t.view(-1, 1))
# Hard Negative Mining
loss_c[pos.view(-1, 1)] = 0 # filter out pos boxes for now
loss_c = loss_c.view(num, -1)
_, loss_idx = loss_c.sort(1, descending=True)
_, idx_rank = loss_idx.sort(1)
num_pos = pos.long().sum(1, keepdim=True)
num_neg = torch.clamp(self.negpos_ratio*num_pos, max=pos.size(1)-1)
neg = idx_rank < num_neg.expand_as(idx_rank)
# Confidence Loss Including Positive and Negative Examples
pos_idx = pos.unsqueeze(2).expand_as(conf_data)
neg_idx = neg.unsqueeze(2).expand_as(conf_data)
conf_p = conf_data[(pos_idx+neg_idx).gt(0)].view(-1,self.num_classes)
targets_weighted = conf_t[(pos+neg).gt(0)]
loss_c = F.cross_entropy(conf_p, targets_weighted, reduction='sum')
# Sum of losses: L(x,c,l,g) = (Lconf(x, c) + αLloc(x,l,g)) / N
N = max(num_pos.data.sum().float(), 1)
loss_l /= N
loss_c /= N
loss_landm /= N1
return loss_l, loss_c, loss_landm123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193'''
@paper: GAN Prior Embedded Network for Blind Face Restoration in the Wild (CVPR2021)
@author: yangxy (yangtao9009@gmail.com)
'''
import os
import torch
import torch.backends.cudnn as cudnn
import numpy as np
from face_detect.data import cfg_re50
from face_detect.layers.functions.prior_box import PriorBox
from face_detect.utils.nms.py_cpu_nms import py_cpu_nms
import cv2
from face_detect.facemodels.retinaface import RetinaFace
from face_detect.utils.box_utils import decode, decode_landm
import time
import torch.nn.functional as F
class RetinaFaceDetection(object):
def __init__(self, base_dir, device='cuda', network='RetinaFace-R50'):
torch.set_grad_enabled(False)
cudnn.benchmark = True
self.pretrained_path = os.path.join(base_dir, network+'.pth')
self.device = device #torch.cuda.current_device()
self.cfg = cfg_re50
self.net = RetinaFace(cfg=self.cfg, phase='test')
self.load_model()
self.net = self.net.to(device)
self.mean = torch.tensor([[[[104]], [[117]], [[123]]]]).to(device)
def check_keys(self, pretrained_state_dict):
ckpt_keys = set(pretrained_state_dict.keys())
model_keys = set(self.net.state_dict().keys())
used_pretrained_keys = model_keys & ckpt_keys
unused_pretrained_keys = ckpt_keys - model_keys
missing_keys = model_keys - ckpt_keys
assert len(used_pretrained_keys) > 0, 'load NONE from pretrained checkpoint'
return True
def remove_prefix(self, state_dict, prefix):
''' Old style model is stored with all names of parameters sharing common prefix 'module.' '''
f = lambda x: x.split(prefix, 1)[-1] if x.startswith(prefix) else x
return {f(key): value for key, value in state_dict.items()}
def load_model(self, load_to_cpu=False):
#if load_to_cpu:
# pretrained_dict = torch.load(self.pretrained_path, map_location=lambda storage, loc: storage)
#else:
# pretrained_dict = torch.load(self.pretrained_path, map_location=lambda storage, loc: storage.cuda())
pretrained_dict = torch.load(self.pretrained_path, map_location=torch.device('cpu'))
if "state_dict" in pretrained_dict.keys():
pretrained_dict = self.remove_prefix(pretrained_dict['state_dict'], 'module.')
else:
pretrained_dict = self.remove_prefix(pretrained_dict, 'module.')
self.check_keys(pretrained_dict)
self.net.load_state_dict(pretrained_dict, strict=False)
self.net.eval()
def detect(self, img_raw, resize=1, confidence_threshold=0.9, nms_threshold=0.4, top_k=5000, keep_top_k=750, save_image=False):
img = np.float32(img_raw)
im_height, im_width = img.shape[:2]
ss = 1.0
# tricky
if max(im_height, im_width) > 1500:
ss = 1000.0/max(im_height, im_width)
img = cv2.resize(img, (0,0), fx=ss, fy=ss)
im_height, im_width = img.shape[:2]
scale = torch.Tensor([img.shape[1], img.shape[0], img.shape[1], img.shape[0]])
img -= (104, 117, 123)
img = img.transpose(2, 0, 1)
img = torch.from_numpy(img).unsqueeze(0)
img = img.to(self.device)
scale = scale.to(self.device)
with torch.no_grad():
loc, conf, landms = self.net(img) # forward pass
priorbox = PriorBox(self.cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
priors = priors.to(self.device)
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, self.cfg['variance'])
boxes = boxes * scale / resize
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
landms = decode_landm(landms.data.squeeze(0), prior_data, self.cfg['variance'])
scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2]])
scale1 = scale1.to(self.device)
landms = landms * scale1 / resize
landms = landms.cpu().numpy()
# ignore low scores
inds = np.where(scores > confidence_threshold)[0]
boxes = boxes[inds]
landms = landms[inds]
scores = scores[inds]
# keep top-K before NMS
order = scores.argsort()[::-1][:top_k]
boxes = boxes[order]
landms = landms[order]
scores = scores[order]
# do NMS
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = py_cpu_nms(dets, nms_threshold)
# keep = nms(dets, nms_threshold,force_cpu=args.cpu)
dets = dets[keep, :]
landms = landms[keep]
# keep top-K faster NMS
dets = dets[:keep_top_k, :]
landms = landms[:keep_top_k, :]
# sort faces(delete)
'''
fscores = [det[4] for det in dets]
sorted_idx = sorted(range(len(fscores)), key=lambda k:fscores[k], reverse=False) # sort index
tmp = [landms[idx] for idx in sorted_idx]
landms = np.asarray(tmp)
'''
landms = landms.reshape((-1, 5, 2))
landms = landms.transpose((0, 2, 1))
landms = landms.reshape(-1, 10, )
return dets/ss, landms/ss
def detect_tensor(self, img, resize=1, confidence_threshold=0.9, nms_threshold=0.4, top_k=5000, keep_top_k=750, save_image=False):
im_height, im_width = img.shape[-2:]
ss = 1000/max(im_height, im_width)
img = F.interpolate(img, scale_factor=ss)
im_height, im_width = img.shape[-2:]
scale = torch.Tensor([im_width, im_height, im_width, im_height]).to(self.device)
img -= self.mean
loc, conf, landms = self.net(img) # forward pass
priorbox = PriorBox(self.cfg, image_size=(im_height, im_width))
priors = priorbox.forward()
priors = priors.to(self.device)
prior_data = priors.data
boxes = decode(loc.data.squeeze(0), prior_data, self.cfg['variance'])
boxes = boxes * scale / resize
boxes = boxes.cpu().numpy()
scores = conf.squeeze(0).data.cpu().numpy()[:, 1]
landms = decode_landm(landms.data.squeeze(0), prior_data, self.cfg['variance'])
scale1 = torch.Tensor([img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2], img.shape[3], img.shape[2],
img.shape[3], img.shape[2]])
scale1 = scale1.to(self.device)
landms = landms * scale1 / resize
landms = landms.cpu().numpy()
# ignore low scores
inds = np.where(scores > confidence_threshold)[0]
boxes = boxes[inds]
landms = landms[inds]
scores = scores[inds]
# keep top-K before NMS
order = scores.argsort()[::-1][:top_k]
boxes = boxes[order]
landms = landms[order]
scores = scores[order]
# do NMS
dets = np.hstack((boxes, scores[:, np.newaxis])).astype(np.float32, copy=False)
keep = py_cpu_nms(dets, nms_threshold)
# keep = nms(dets, nms_threshold,force_cpu=args.cpu)
dets = dets[keep, :]
landms = landms[keep]
# keep top-K faster NMS
dets = dets[:keep_top_k, :]
landms = landms[:keep_top_k, :]
# sort faces(delete)
'''
fscores = [det[4] for det in dets]
sorted_idx = sorted(range(len(fscores)), key=lambda k:fscores[k], reverse=False) # sort index
tmp = [landms[idx] for idx in sorted_idx]
landms = np.asarray(tmp)
'''
landms = landms.reshape((-1, 5, 2))
landms = landms.transpose((0, 2, 1))
landms = landms.reshape(-1, 10, )
return dets/ss, landms/ss123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330import torch
import numpy as np
def point_form(boxes):
""" Convert prior_boxes to (xmin, ymin, xmax, ymax)
representation for comparison to point form ground truth data.
Args:
boxes: (tensor) center-size default boxes from priorbox layers.
Return:
boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes.
"""
return torch.cat((boxes[:, :2] - boxes[:, 2:]/2, # xmin, ymin
boxes[:, :2] + boxes[:, 2:]/2), 1) # xmax, ymax
def center_size(boxes):
""" Convert prior_boxes to (cx, cy, w, h)
representation for comparison to center-size form ground truth data.
Args:
boxes: (tensor) point_form boxes
Return:
boxes: (tensor) Converted xmin, ymin, xmax, ymax form of boxes.
"""
return torch.cat((boxes[:, 2:] + boxes[:, :2])/2, # cx, cy
boxes[:, 2:] - boxes[:, :2], 1) # w, h
def intersect(box_a, box_b):
""" We resize both tensors to [A,B,2] without new malloc:
[A,2] -> [A,1,2] -> [A,B,2]
[B,2] -> [1,B,2] -> [A,B,2]
Then we compute the area of intersect between box_a and box_b.
Args:
box_a: (tensor) bounding boxes, Shape: [A,4].
box_b: (tensor) bounding boxes, Shape: [B,4].
Return:
(tensor) intersection area, Shape: [A,B].
"""
A = box_a.size(0)
B = box_b.size(0)
max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
box_b[:, 2:].unsqueeze(0).expand(A, B, 2))
min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),
box_b[:, :2].unsqueeze(0).expand(A, B, 2))
inter = torch.clamp((max_xy - min_xy), min=0)
return inter[:, :, 0] * inter[:, :, 1]
def jaccard(box_a, box_b):
"""Compute the jaccard overlap of two sets of boxes. The jaccard overlap
is simply the intersection over union of two boxes. Here we operate on
ground truth boxes and default boxes.
E.g.:
A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
Args:
box_a: (tensor) Ground truth bounding boxes, Shape: [num_objects,4]
box_b: (tensor) Prior boxes from priorbox layers, Shape: [num_priors,4]
Return:
jaccard overlap: (tensor) Shape: [box_a.size(0), box_b.size(0)]
"""
inter = intersect(box_a, box_b)
area_a = ((box_a[:, 2]-box_a[:, 0]) *
(box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter) # [A,B]
area_b = ((box_b[:, 2]-box_b[:, 0]) *
(box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter) # [A,B]
union = area_a + area_b - inter
return inter / union # [A,B]
def matrix_iou(a, b):
"""
return iou of a and b, numpy version for data augenmentation
"""
lt = np.maximum(a[:, np.newaxis, :2], b[:, :2])
rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:])
area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2)
area_a = np.prod(a[:, 2:] - a[:, :2], axis=1)
area_b = np.prod(b[:, 2:] - b[:, :2], axis=1)
return area_i / (area_a[:, np.newaxis] + area_b - area_i)
def matrix_iof(a, b):
"""
return iof of a and b, numpy version for data augenmentation
"""
lt = np.maximum(a[:, np.newaxis, :2], b[:, :2])
rb = np.minimum(a[:, np.newaxis, 2:], b[:, 2:])
area_i = np.prod(rb - lt, axis=2) * (lt < rb).all(axis=2)
area_a = np.prod(a[:, 2:] - a[:, :2], axis=1)
return area_i / np.maximum(area_a[:, np.newaxis], 1)
def match(threshold, truths, priors, variances, labels, landms, loc_t, conf_t, landm_t, idx):
"""Match each prior box with the ground truth box of the highest jaccard
overlap, encode the bounding boxes, then return the matched indices
corresponding to both confidence and location preds.
Args:
threshold: (float) The overlap threshold used when mathing boxes.
truths: (tensor) Ground truth boxes, Shape: [num_obj, 4].
priors: (tensor) Prior boxes from priorbox layers, Shape: [n_priors,4].
variances: (tensor) Variances corresponding to each prior coord,
Shape: [num_priors, 4].
labels: (tensor) All the class labels for the image, Shape: [num_obj].
landms: (tensor) Ground truth landms, Shape [num_obj, 10].
loc_t: (tensor) Tensor to be filled w/ endcoded location targets.
conf_t: (tensor) Tensor to be filled w/ matched indices for conf preds.
landm_t: (tensor) Tensor to be filled w/ endcoded landm targets.
idx: (int) current batch index
Return:
The matched indices corresponding to 1)location 2)confidence 3)landm preds.
"""
# jaccard index
overlaps = jaccard(
truths,
point_form(priors)
)
# (Bipartite Matching)
# [1,num_objects] best prior for each ground truth
best_prior_overlap, best_prior_idx = overlaps.max(1, keepdim=True)
# ignore hard gt
valid_gt_idx = best_prior_overlap[:, 0] >= 0.2
best_prior_idx_filter = best_prior_idx[valid_gt_idx, :]
if best_prior_idx_filter.shape[0] <= 0:
loc_t[idx] = 0
conf_t[idx] = 0
return
# [1,num_priors] best ground truth for each prior
best_truth_overlap, best_truth_idx = overlaps.max(0, keepdim=True)
best_truth_idx.squeeze_(0)
best_truth_overlap.squeeze_(0)
best_prior_idx.squeeze_(1)
best_prior_idx_filter.squeeze_(1)
best_prior_overlap.squeeze_(1)
best_truth_overlap.index_fill_(0, best_prior_idx_filter, 2) # ensure best prior
# TODO refactor: index best_prior_idx with long tensor
# ensure every gt matches with its prior of max overlap
for j in range(best_prior_idx.size(0)): # 判别此anchor是预测哪一个boxes
best_truth_idx[best_prior_idx[j]] = j
matches = truths[best_truth_idx] # Shape: [num_priors,4] 此处为每一个anchor对应的bbox取出来
conf = labels[best_truth_idx] # Shape: [num_priors] 此处为每一个anchor对应的label取出来
conf[best_truth_overlap < threshold] = 0 # label as background overlap<0.35的全部作为负样本
loc = encode(matches, priors, variances)
matches_landm = landms[best_truth_idx]
landm = encode_landm(matches_landm, priors, variances)
loc_t[idx] = loc # [num_priors,4] encoded offsets to learn
conf_t[idx] = conf # [num_priors] top class label for each prior
landm_t[idx] = landm
def encode(matched, priors, variances):
"""Encode the variances from the priorbox layers into the ground truth boxes
we have matched (based on jaccard overlap) with the prior boxes.
Args:
matched: (tensor) Coords of ground truth for each prior in point-form
Shape: [num_priors, 4].
priors: (tensor) Prior boxes in center-offset form
Shape: [num_priors,4].
variances: (list[float]) Variances of priorboxes
Return:
encoded boxes (tensor), Shape: [num_priors, 4]
"""
# dist b/t match center and prior's center
g_cxcy = (matched[:, :2] + matched[:, 2:])/2 - priors[:, :2]
# encode variance
g_cxcy /= (variances[0] * priors[:, 2:])
# match wh / prior wh
g_wh = (matched[:, 2:] - matched[:, :2]) / priors[:, 2:]
g_wh = torch.log(g_wh) / variances[1]
# return target for smooth_l1_loss
return torch.cat([g_cxcy, g_wh], 1) # [num_priors,4]
def encode_landm(matched, priors, variances):
"""Encode the variances from the priorbox layers into the ground truth boxes
we have matched (based on jaccard overlap) with the prior boxes.
Args:
matched: (tensor) Coords of ground truth for each prior in point-form
Shape: [num_priors, 10].
priors: (tensor) Prior boxes in center-offset form
Shape: [num_priors,4].
variances: (list[float]) Variances of priorboxes
Return:
encoded landm (tensor), Shape: [num_priors, 10]
"""
# dist b/t match center and prior's center
matched = torch.reshape(matched, (matched.size(0), 5, 2))
priors_cx = priors[:, 0].unsqueeze(1).expand(matched.size(0), 5).unsqueeze(2)
priors_cy = priors[:, 1].unsqueeze(1).expand(matched.size(0), 5).unsqueeze(2)
priors_w = priors[:, 2].unsqueeze(1).expand(matched.size(0), 5).unsqueeze(2)
priors_h = priors[:, 3].unsqueeze(1).expand(matched.size(0), 5).unsqueeze(2)
priors = torch.cat([priors_cx, priors_cy, priors_w, priors_h], dim=2)
g_cxcy = matched[:, :, :2] - priors[:, :, :2]
# encode variance
g_cxcy /= (variances[0] * priors[:, :, 2:])
# g_cxcy /= priors[:, :, 2:]
g_cxcy = g_cxcy.reshape(g_cxcy.size(0), -1)
# return target for smooth_l1_loss
return g_cxcy
# Adapted from https://github.com/Hakuyume/chainer-ssd
def decode(loc, priors, variances):
"""Decode locations from predictions using priors to undo
the encoding we did for offset regression at train time.
Args:
loc (tensor): location predictions for loc layers,
Shape: [num_priors,4]
priors (tensor): Prior boxes in center-offset form.
Shape: [num_priors,4].
variances: (list[float]) Variances of priorboxes
Return:
decoded bounding box predictions
"""
boxes = torch.cat((
priors[:, :2] + loc[:, :2] * variances[0] * priors[:, 2:],
priors[:, 2:] * torch.exp(loc[:, 2:] * variances[1])), 1)
boxes[:, :2] -= boxes[:, 2:] / 2
boxes[:, 2:] += boxes[:, :2]
return boxes
def decode_landm(pre, priors, variances):
"""Decode landm from predictions using priors to undo
the encoding we did for offset regression at train time.
Args:
pre (tensor): landm predictions for loc layers,
Shape: [num_priors,10]
priors (tensor): Prior boxes in center-offset form.
Shape: [num_priors,4].
variances: (list[float]) Variances of priorboxes
Return:
decoded landm predictions
"""
landms = torch.cat((priors[:, :2] + pre[:, :2] * variances[0] * priors[:, 2:],
priors[:, :2] + pre[:, 2:4] * variances[0] * priors[:, 2:],
priors[:, :2] + pre[:, 4:6] * variances[0] * priors[:, 2:],
priors[:, :2] + pre[:, 6:8] * variances[0] * priors[:, 2:],
priors[:, :2] + pre[:, 8:10] * variances[0] * priors[:, 2:],
), dim=1)
return landms
def log_sum_exp(x):
"""Utility function for computing log_sum_exp while determining
This will be used to determine unaveraged confidence loss across
all examples in a batch.
Args:
x (Variable(tensor)): conf_preds from conf layers
"""
x_max = x.data.max()
return torch.log(torch.sum(torch.exp(x-x_max), 1, keepdim=True)) + x_max
# Original author: Francisco Massa:
# https://github.com/fmassa/object-detection.torch
# Ported to PyTorch by Max deGroot (02/01/2017)
def nms(boxes, scores, overlap=0.5, top_k=200):
"""Apply non-maximum suppression at test time to avoid detecting too many
overlapping bounding boxes for a given object.
Args:
boxes: (tensor) The location preds for the img, Shape: [num_priors,4].
scores: (tensor) The class predscores for the img, Shape:[num_priors].
overlap: (float) The overlap thresh for suppressing unnecessary boxes.
top_k: (int) The Maximum number of box preds to consider.
Return:
The indices of the kept boxes with respect to num_priors.
"""
keep = torch.Tensor(scores.size(0)).fill_(0).long()
if boxes.numel() == 0:
return keep
x1 = boxes[:, 0]
y1 = boxes[:, 1]
x2 = boxes[:, 2]
y2 = boxes[:, 3]
area = torch.mul(x2 - x1, y2 - y1)
v, idx = scores.sort(0) # sort in ascending order
# I = I[v >= 0.01]
idx = idx[-top_k:] # indices of the top-k largest vals
xx1 = boxes.new()
yy1 = boxes.new()
xx2 = boxes.new()
yy2 = boxes.new()
w = boxes.new()
h = boxes.new()
# keep = torch.Tensor()
count = 0
while idx.numel() > 0:
i = idx[-1] # index of current largest val
# keep.append(i)
keep[count] = i
count += 1
if idx.size(0) == 1:
break
idx = idx[:-1] # remove kept element from view
# load bboxes of next highest vals
torch.index_select(x1, 0, idx, out=xx1)
torch.index_select(y1, 0, idx, out=yy1)
torch.index_select(x2, 0, idx, out=xx2)
torch.index_select(y2, 0, idx, out=yy2)
# store element-wise max with next highest score
xx1 = torch.clamp(xx1, min=x1[i])
yy1 = torch.clamp(yy1, min=y1[i])
xx2 = torch.clamp(xx2, max=x2[i])
yy2 = torch.clamp(yy2, max=y2[i])
w.resize_as_(xx2)
h.resize_as_(yy2)
w = xx2 - xx1
h = yy2 - yy1
# check sizes of xx1 and xx2.. after each iteration
w = torch.clamp(w, min=0.0)
h = torch.clamp(h, min=0.0)
inter = w*h
# IoU = i / (area(a) + area(b) - i)
rem_areas = torch.index_select(area, 0, idx) # load remaining areas)
union = (rem_areas - inter) + area[i]
IoU = inter/union # store result in iou
# keep only elements with an IoU <= overlap
idx = idx[IoU.le(overlap)]
return keep, count
.gitattributes+1
video-tetalking/CODE_OF_CONDUCT.mdAdded+43
video-tetalking/inference.pyAdded+345
video-tetalking/inference_videoretalking.shAdded+4
video-tetalking/models/DNet.pyAdded+118
video-tetalking/models/ENet.pyAdded+139
video-tetalking/models/LNet.pyAdded+139
video-tetalking/models/__init__.pyAdded+37
video-tetalking/models/base_blocks.pyAdded+554
video-tetalking/models/ffc.pyAdded+233
video-tetalking/models/transformer.pyAdded+119
video-tetalking/predict.pyAdded+552
video-tetalking/quick_demo.ipynbAdded+293
video-tetalking/requirements.txtAdded+10
video-tetalking/third_part/GFPGAN/LICENSEAdded+351
video-tetalking/third_part/GFPGAN/gfpgan/__init__.pyAdded+8
video-tetalking/third_part/GFPGAN/gfpgan/archs/__init__.pyAdded+10
video-tetalking/third_part/GFPGAN/gfpgan/archs/arcface_arch.pyAdded+245
video-tetalking/third_part/GFPGAN/gfpgan/archs/gfpgan_bilinear_arch.pyAdded+312
video-tetalking/third_part/GFPGAN/gfpgan/archs/gfpganv1_arch.pyAdded+439
video-tetalking/third_part/GFPGAN/gfpgan/archs/gfpganv1_clean_arch.pyAdded+324
video-tetalking/third_part/GFPGAN/gfpgan/archs/stylegan2_bilinear_arch.pyAdded+613
video-tetalking/third_part/GFPGAN/gfpgan/archs/stylegan2_clean_arch.pyAdded+368
video-tetalking/third_part/GFPGAN/gfpgan/data/__init__.pyAdded+10
video-tetalking/third_part/GFPGAN/gfpgan/data/ffhq_degradation_dataset.pyAdded+230
video-tetalking/third_part/GFPGAN/gfpgan/models/__init__.pyAdded+10
video-tetalking/third_part/GFPGAN/gfpgan/models/gfpgan_model.pyAdded+580
video-tetalking/third_part/GFPGAN/gfpgan/train.pyAdded+11
video-tetalking/third_part/GFPGAN/gfpgan/utils.pyAdded+143
video-tetalking/third_part/GFPGAN/gfpgan/version.pyAdded+5
video-tetalking/third_part/GFPGAN/gfpgan/weights/README.mdAdded+3
video-tetalking/third_part/GFPGAN/options/train_gfpgan_v1.ymlAdded+216
video-tetalking/third_part/GFPGAN/options/train_gfpgan_v1_simple.ymlAdded+182
video-tetalking/third_part/GPEN/align_faces.pyAdded+271
video-tetalking/third_part/GPEN/face_detect/.DS_StoreAdded
video-tetalking/third_part/GPEN/face_detect/data/FDDB/img_list.txtAdded+2845
video-tetalking/third_part/GPEN/face_detect/data/__init__.pyAdded+3
video-tetalking/third_part/GPEN/face_detect/data/config.pyAdded+42
video-tetalking/third_part/GPEN/face_detect/data/data_augment.pyAdded+237
video-tetalking/third_part/GPEN/face_detect/data/wider_face.pyAdded+101
video-tetalking/third_part/GPEN/face_detect/facemodels/__init__.pyAdded
video-tetalking/third_part/GPEN/face_detect/facemodels/net.pyAdded+137
video-tetalking/third_part/GPEN/face_detect/facemodels/retinaface.pyAdded+127
video-tetalking/third_part/GPEN/face_detect/layers/__init__.pyAdded+2
video-tetalking/third_part/GPEN/face_detect/layers/functions/prior_box.pyAdded+34
video-tetalking/third_part/GPEN/face_detect/layers/modules/__init__.pyAdded+3
video-tetalking/third_part/GPEN/face_detect/layers/modules/multibox_loss.pyAdded+125
video-tetalking/third_part/GPEN/face_detect/retinaface_detection.pyAdded+193
video-tetalking/third_part/GPEN/face_detect/utils/__init__.pyAdded
video-tetalking/third_part/GPEN/face_detect/utils/box_utils.pyAdded+330