Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 60 additions & 21 deletions UnwrapMosaic/Audio2Face.ipynb

Large diffs are not rendered by default.

127 changes: 83 additions & 44 deletions UnwrapMosaic/Face2Face_UnwrapMosaic.ipynb

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions UnwrapMosaic/NoSkipNet_X2Face.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,11 @@ def print_network(net):
class Pix2PixModel(nn.Module):
def __init__(self, input_nc, output_nc, inner_nc=512):
super(Pix2PixModel, self).__init__()

self.netG = define_G(input_nc, output_nc, 64, 'unet_256', 'batch', False, 'xavier', [0], inner_nc=inner_nc)

gpu = []
if torch.cuda.is_available():
gpu = [0]
self.netG = define_G(input_nc, output_nc, 64, 'unet_256', 'batch', False, 'xavier', gpu, inner_nc=inner_nc)

def forward(self, *cycles):
# First one
Expand Down
5 changes: 4 additions & 1 deletion UnwrapMosaic/NoSkipNet_X2Face_pose.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,10 @@ class Pix2PixModel(nn.Module):
def __init__(self, input_nc, output_nc, input_pose=False, audio=False, expr=False, inner_nc=512):
super(Pix2PixModel, self).__init__()

self.netG = define_G(input_nc, output_nc, input_pose, audio, expr, 64, 'unet_256', 'batch', False, 'xavier', [0], inner_nc=inner_nc)
gpu = []
if torch.cuda.is_available():
gpu = [0]
self.netG = define_G(input_nc, output_nc, input_pose, audio, expr, 64, 'unet_256', 'batch', False, 'xavier', gpu, inner_nc=inner_nc)

def forward(self, *cycles):
# First one
Expand Down
89 changes: 64 additions & 25 deletions UnwrapMosaic/Pose2Face.ipynb

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions UnwrapMosaic/SkipNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,11 @@ def print_network(net):
class Pix2PixModel(nn.Module):
def __init__(self, output_nc, input_nc=3):
super(Pix2PixModel, self).__init__()

self.netG = define_G(input_nc, output_nc, 64, 'unet_256', 'batch', False, 'xavier', [0])

gpu = []
if torch.cuda.is_available():
gpu = [0]
self.netG = define_G(input_nc, output_nc, 64, 'unet_256', 'batch', False, 'xavier', gpu)

def forward(self, *cycles):
# First one
Expand Down
26 changes: 14 additions & 12 deletions UnwrapMosaic/UnwrappedFace.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# E.g. the two images go from the given image to a sampler with values between -1,1
# denoting the offset from the identity mapping.

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

class UnwrappedFaceWeightedAverage(nn.Module):
def __init__(self, output_num_channels=2, input_num_channels=3, inner_nc=512):
super(UnwrappedFaceWeightedAverage, self).__init__()
Expand All @@ -26,7 +28,7 @@ def forward(self, target_pose, *input_imgs):
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)

xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).to(device)

input_imgs_t = [0] * len(input_imgs)
confidence = [0] * len(input_imgs)
Expand Down Expand Up @@ -59,8 +61,8 @@ def forward(self, target_pose, *input_imgs):

sampler_xy = nn.Tanh()(sampler[:,0:2,:,:])
#print(confidences.size(), xs.size())
stddev = nn.Softplus().cuda()(sampler[:,2:,:,:]).clamp(max=40)
sampler_xy = sampler_xy.permute(0,2,3,1) + stddev.permute(0,2,3,1).contiguous().mul(Variable(torch.randn(xs.size()).cuda(), requires_grad=False)) + Variable(xs, requires_grad=False) # choose values according to the std dev
stddev = nn.Softplus().to(device)(sampler[:,2:,:,:]).clamp(max=40)
sampler_xy = sampler_xy.permute(0,2,3,1) + stddev.permute(0,2,3,1).contiguous().mul(Variable(torch.randn(xs.size()).to(device), requires_grad=False)) + Variable(xs, requires_grad=False) # choose values according to the std dev
sampler_xy = sampler_xy.clamp(min=-1,max=1)

sampled_image = nn.functional.grid_sample(result_xc, sampler_xy)
Expand All @@ -70,7 +72,7 @@ def get_unwrapped_oneimage(self, input_img):
xs = np.linspace(-1,1,input_img.size(2))
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_img.size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_img.size(0), 1,1,1).to(device)

temp = self.pix2pixUnwrapped(input_img)[0]
sampler = temp[:,0:2,:,:]
Expand All @@ -82,7 +84,7 @@ def get_unwrapped(self, *input_imgs):
xs = np.linspace(-1,1,input_imgs[0].size(2))
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).to(device)

input_imgs_t = [0] * len(input_imgs)
confidence = [0] * len(input_imgs)
Expand Down Expand Up @@ -110,7 +112,7 @@ def get_sampler(self, target_pose):
xs = np.linspace(-1,1,sampler.size(2))
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)
xs = torch.Tensor(xs).unsqueeze(0).repeat(target_pose.size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(target_pose.size(0), 1,1,1).to(device)

sampler = sampler.permute(0,2,3,1) + Variable(xs, requires_grad=False)
return sampler
Expand All @@ -121,14 +123,14 @@ def __init__(self, output_num_channels=2, input_num_channels=3, inner_nc=512, in
super(UnwrappedFaceWeightedAveragePose, self).__init__()

self.pix2pixUnwrapped = Pix2PixModel(3)
self.pix2pixSampler = NoSkipPix2PixModel_pose(input_num_channels, output_num_channels, input_pose=input_pose, inner_nc=inner_nc)
self.pix2pixSampler = NoSkipPix2PixModel_pose(input_num_channels, output_num_channels, input_pose=input_pose, inner_nc=inner_nc)

def forward(self, target_pose, pose_gt, *input_imgs):
xs = np.linspace(-1,1,input_imgs[0].size(2))
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)

xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).to(device)

input_imgs_t = [0] * len(input_imgs)
confidence = [0] * len(input_imgs)
Expand Down Expand Up @@ -161,8 +163,8 @@ def forward(self, target_pose, pose_gt, *input_imgs):

sampler_xy = nn.Tanh()(sampler[:,0:2,:,:])
#print(confidences.size(), xs.size())
stddev = nn.Softplus().cuda()(sampler[:,2:,:,:]).clamp(max=40)
sampler_xy = sampler_xy.permute(0,2,3,1) + stddev.permute(0,2,3,1).contiguous().mul(Variable(torch.randn(xs.size()).cuda(), requires_grad=False)) + Variable(xs, requires_grad=False) # choose values according to the std dev
stddev = nn.Softplus().to(device)(sampler[:,2:,:,:]).clamp(max=40)
sampler_xy = sampler_xy.permute(0,2,3,1) + stddev.permute(0,2,3,1).contiguous().mul(Variable(torch.randn(xs.size()).to(device), requires_grad=False)) + Variable(xs, requires_grad=False) # choose values according to the std dev
sampler_xy = sampler_xy.clamp(min=-1,max=1)

sampled_image = nn.functional.grid_sample(result_xc, sampler_xy)
Expand All @@ -172,7 +174,7 @@ def get_unwrapped(self, *input_imgs):
xs = np.linspace(-1,1,input_imgs[0].size(2))
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(input_imgs[0].size(0), 1,1,1).to(device)

input_imgs_t = [0] * len(input_imgs)
confidence = [0] * len(input_imgs)
Expand Down Expand Up @@ -200,7 +202,7 @@ def get_sampler(self, target_pose):
xs = np.linspace(-1,1,sampler.size(2))
xs = np.meshgrid(xs, xs)
xs = np.stack(xs, 2)
xs = torch.Tensor(xs).unsqueeze(0).repeat(target_pose.size(0), 1,1,1).cuda()
xs = torch.Tensor(xs).unsqueeze(0).repeat(target_pose.size(0), 1,1,1).to(device)

sampler = sampler.permute(0,2,3,1) + Variable(xs, requires_grad=False)
return sampler
Expand Down