Skip to content

Commit 589e31a

Browse files
committed
Support Simswap 512
Support Simswap 512
1 parent 0f2e4fd commit 589e31a

22 files changed

+549
-54
lines changed

MultiSpecific.ipynb

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

SimSwap colab.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@
398398
"opt.isTrain = False\n",
399399
"opt.use_mask = True ## new feature up-to-date\n",
400400
"\n",
401-
"crop_size = 224\n",
401+
"crop_size = opt.crop_size\n",
402402
"\n",
403403
"torch.nn.Module.dump_patches = True\n",
404404
"model = create_model(opt)\n",
@@ -420,7 +420,7 @@
420420
" img_id = img_id.cuda()\n",
421421
"\n",
422422
" #create latent id\n",
423-
" img_id_downsample = F.interpolate(img_id, scale_factor=0.5)\n",
423+
" img_id_downsample = F.interpolate(img_id, size=(112,112))\n",
424424
" latend_id = model.netArc(img_id_downsample)\n",
425425
" latend_id = latend_id.detach().to('cpu')\n",
426426
" latend_id = latend_id/np.linalg.norm(latend_id,axis=1,keepdims=True)\n",

insightface_func/face_detect_crop_multi.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
'''
2+
Author: Naiyuan liu
3+
Github: https://github.com/NNNNAI
4+
Date: 2021-11-23 17:03:58
5+
LastEditors: Naiyuan liu
6+
LastEditTime: 2021-11-24 16:45:41
7+
Description:
8+
'''
19
from __future__ import division
210
import collections
311
import numpy as np
@@ -6,7 +14,7 @@
614
import os.path as osp
715
import cv2
816
from insightface.model_zoo import model_zoo
9-
from insightface.utils import face_align
17+
from insightface_func.utils import face_align_ffhqandnewarc as face_align
1018

1119
__all__ = ['Face_detect_crop', 'Face']
1220

@@ -40,8 +48,9 @@ def __init__(self, name, root='~/.insightface_func/models'):
4048
self.det_model = self.models['detection']
4149

4250

43-
def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640)):
51+
def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640), mode ='None'):
4452
self.det_thresh = det_thresh
53+
self.mode = mode
4554
assert det_size is not None
4655
print('set det-size:', det_size)
4756
self.det_size = det_size
@@ -73,7 +82,7 @@ def get(self, img, crop_size, max_num=0):
7382
kps = None
7483
if kpss is not None:
7584
kps = kpss[i]
76-
M, _ = face_align.estimate_norm(kps, crop_size, mode ='None')
85+
M, _ = face_align.estimate_norm(kps, crop_size, mode = self.mode)
7786
align_img = cv2.warpAffine(img, M, (crop_size, crop_size), borderValue=0.0)
7887
align_img_list.append(align_img)
7988
M_list.append(M)

insightface_func/face_detect_crop_single.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
'''
2+
Author: Naiyuan liu
3+
Github: https://github.com/NNNNAI
4+
Date: 2021-11-23 17:03:58
5+
LastEditors: Naiyuan liu
6+
LastEditTime: 2021-11-24 16:46:04
7+
Description:
8+
'''
19
from __future__ import division
210
import collections
311
import numpy as np
@@ -6,7 +14,7 @@
614
import os.path as osp
715
import cv2
816
from insightface.model_zoo import model_zoo
9-
from insightface.utils import face_align
17+
from insightface_func.utils import face_align_ffhqandnewarc as face_align
1018

1119
__all__ = ['Face_detect_crop', 'Face']
1220

@@ -40,8 +48,9 @@ def __init__(self, name, root='~/.insightface_func/models'):
4048
self.det_model = self.models['detection']
4149

4250

43-
def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640)):
51+
def prepare(self, ctx_id, det_thresh=0.5, det_size=(640, 640), mode ='None'):
4452
self.det_thresh = det_thresh
53+
self.mode = mode
4554
assert det_size is not None
4655
print('set det-size:', det_size)
4756
self.det_size = det_size
@@ -82,7 +91,7 @@ def get(self, img, crop_size, max_num=0):
8291
kps = None
8392
if kpss is not None:
8493
kps = kpss[best_index]
85-
M, _ = face_align.estimate_norm(kps, crop_size, mode ='None')
94+
M, _ = face_align.estimate_norm(kps, crop_size, mode = self.mode)
8695
align_img = cv2.warpAffine(img, M, (crop_size, crop_size), borderValue=0.0)
8796

8897
return [align_img], [M]
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
'''
2+
Author: Naiyuan liu
3+
Github: https://github.com/NNNNAI
4+
Date: 2021-11-15 19:42:42
5+
LastEditors: Naiyuan liu
6+
LastEditTime: 2021-11-15 20:01:47
7+
Description:
8+
'''
9+
10+
import cv2
11+
import numpy as np
12+
from skimage import transform as trans
13+
14+
src1 = np.array([[51.642, 50.115], [57.617, 49.990], [35.740, 69.007],
15+
[51.157, 89.050], [57.025, 89.702]],
16+
dtype=np.float32)
17+
#<--left
18+
src2 = np.array([[45.031, 50.118], [65.568, 50.872], [39.677, 68.111],
19+
[45.177, 86.190], [64.246, 86.758]],
20+
dtype=np.float32)
21+
22+
#---frontal
23+
src3 = np.array([[39.730, 51.138], [72.270, 51.138], [56.000, 68.493],
24+
[42.463, 87.010], [69.537, 87.010]],
25+
dtype=np.float32)
26+
27+
#-->right
28+
src4 = np.array([[46.845, 50.872], [67.382, 50.118], [72.737, 68.111],
29+
[48.167, 86.758], [67.236, 86.190]],
30+
dtype=np.float32)
31+
32+
#-->right profile
33+
src5 = np.array([[54.796, 49.990], [60.771, 50.115], [76.673, 69.007],
34+
[55.388, 89.702], [61.257, 89.050]],
35+
dtype=np.float32)
36+
37+
src = np.array([src1, src2, src3, src4, src5])
38+
src_map = src
39+
40+
ffhq_src = np.array([[192.98138, 239.94708], [318.90277, 240.1936], [256.63416, 314.01935],
41+
[201.26117, 371.41043], [313.08905, 371.15118]])
42+
ffhq_src = np.expand_dims(ffhq_src, axis=0)
43+
44+
# arcface_src = np.array(
45+
# [[38.2946, 51.6963], [73.5318, 51.5014], [56.0252, 71.7366],
46+
# [41.5493, 92.3655], [70.7299, 92.2041]],
47+
# dtype=np.float32)
48+
49+
# arcface_src = np.expand_dims(arcface_src, axis=0)
50+
51+
# In[66]:
52+
53+
54+
# lmk is prediction; src is template
55+
def estimate_norm(lmk, image_size=112, mode='ffhq'):
56+
assert lmk.shape == (5, 2)
57+
tform = trans.SimilarityTransform()
58+
lmk_tran = np.insert(lmk, 2, values=np.ones(5), axis=1)
59+
min_M = []
60+
min_index = []
61+
min_error = float('inf')
62+
if mode == 'ffhq':
63+
# assert image_size == 112
64+
src = ffhq_src * image_size / 512
65+
else:
66+
src = src_map * image_size / 112
67+
for i in np.arange(src.shape[0]):
68+
tform.estimate(lmk, src[i])
69+
M = tform.params[0:2, :]
70+
results = np.dot(M, lmk_tran.T)
71+
results = results.T
72+
error = np.sum(np.sqrt(np.sum((results - src[i])**2, axis=1)))
73+
# print(error)
74+
if error < min_error:
75+
min_error = error
76+
min_M = M
77+
min_index = i
78+
return min_M, min_index
79+
80+
81+
def norm_crop(img, landmark, image_size=112, mode='ffhq'):
82+
if mode == 'Both':
83+
M_None, _ = estimate_norm(landmark, image_size, mode = 'newarc')
84+
M_ffhq, _ = estimate_norm(landmark, image_size, mode='ffhq')
85+
warped_None = cv2.warpAffine(img, M_None, (image_size, image_size), borderValue=0.0)
86+
warped_ffhq = cv2.warpAffine(img, M_ffhq, (image_size, image_size), borderValue=0.0)
87+
return warped_ffhq, warped_None
88+
else:
89+
M, pose_index = estimate_norm(landmark, image_size, mode)
90+
warped = cv2.warpAffine(img, M, (image_size, image_size), borderValue=0.0)
91+
return warped
92+
93+
def square_crop(im, S):
94+
if im.shape[0] > im.shape[1]:
95+
height = S
96+
width = int(float(im.shape[1]) / im.shape[0] * S)
97+
scale = float(S) / im.shape[0]
98+
else:
99+
width = S
100+
height = int(float(im.shape[0]) / im.shape[1] * S)
101+
scale = float(S) / im.shape[1]
102+
resized_im = cv2.resize(im, (width, height))
103+
det_im = np.zeros((S, S, 3), dtype=np.uint8)
104+
det_im[:resized_im.shape[0], :resized_im.shape[1], :] = resized_im
105+
return det_im, scale
106+
107+
108+
def transform(data, center, output_size, scale, rotation):
109+
scale_ratio = scale
110+
rot = float(rotation) * np.pi / 180.0
111+
#translation = (output_size/2-center[0]*scale_ratio, output_size/2-center[1]*scale_ratio)
112+
t1 = trans.SimilarityTransform(scale=scale_ratio)
113+
cx = center[0] * scale_ratio
114+
cy = center[1] * scale_ratio
115+
t2 = trans.SimilarityTransform(translation=(-1 * cx, -1 * cy))
116+
t3 = trans.SimilarityTransform(rotation=rot)
117+
t4 = trans.SimilarityTransform(translation=(output_size / 2,
118+
output_size / 2))
119+
t = t1 + t2 + t3 + t4
120+
M = t.params[0:2]
121+
cropped = cv2.warpAffine(data,
122+
M, (output_size, output_size),
123+
borderValue=0.0)
124+
return cropped, M
125+
126+
127+
def trans_points2d(pts, M):
128+
new_pts = np.zeros(shape=pts.shape, dtype=np.float32)
129+
for i in range(pts.shape[0]):
130+
pt = pts[i]
131+
new_pt = np.array([pt[0], pt[1], 1.], dtype=np.float32)
132+
new_pt = np.dot(M, new_pt)
133+
#print('new_pt', new_pt.shape, new_pt)
134+
new_pts[i] = new_pt[0:2]
135+
136+
return new_pts
137+
138+
139+
def trans_points3d(pts, M):
140+
scale = np.sqrt(M[0][0] * M[0][0] + M[0][1] * M[0][1])
141+
#print(scale)
142+
new_pts = np.zeros(shape=pts.shape, dtype=np.float32)
143+
for i in range(pts.shape[0]):
144+
pt = pts[i]
145+
new_pt = np.array([pt[0], pt[1], 1.], dtype=np.float32)
146+
new_pt = np.dot(M, new_pt)
147+
#print('new_pt', new_pt.shape, new_pt)
148+
new_pts[i][0:2] = new_pt[0:2]
149+
new_pts[i][2] = pts[i][2] * scale
150+
151+
return new_pts
152+
153+
154+
def trans_points(pts, M):
155+
if pts.shape[1] == 2:
156+
return trans_points2d(pts, M)
157+
else:
158+
return trans_points3d(pts, M)
159+

models/fs_model.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
import torch.nn.functional as F
55
import os
66
from torch.autograd import Variable
7-
from util.image_pool import ImagePool
87
from .base_model import BaseModel
98
from . import networks
10-
from .fs_networks import Generator_Adain_Upsample, Discriminator
119

1210
class SpecificNorm(nn.Module):
1311
def __init__(self, epsilon=1e-8):
@@ -52,6 +50,11 @@ def initialize(self, opt):
5250

5351
device = torch.device("cuda:0")
5452

53+
if opt.crop_size == 224:
54+
from .fs_networks import Generator_Adain_Upsample, Discriminator
55+
elif opt.crop_size == 512:
56+
from .fs_networks_512 import Generator_Adain_Upsample, Discriminator
57+
5558
# Generator network
5659
self.netG = Generator_Adain_Upsample(input_nc=3, output_nc=3, latent_size=512, n_blocks=9, deep=False)
5760
self.netG.to(device)
@@ -197,7 +200,7 @@ def forward(self, img_id, img_att, latent_id, latent_att, for_G=False):
197200

198201

199202
#G_ID
200-
img_fake_down = F.interpolate(img_fake, scale_factor=0.5)
203+
img_fake_down = F.interpolate(img_fake, size=(112,112))
201204
img_fake_down = self.spNorm(img_fake_down)
202205
latent_fake = self.netArc(img_fake_down)
203206
loss_G_ID = (1 - self.cosin_metric(latent_fake, latent_id))

0 commit comments

Comments
 (0)