This commit is contained in:
chenxuanhong
2022-03-25 18:52:25 +08:00
parent 17c5d6a6b5
commit 99ed65aaa3
13 changed files with 136078 additions and 26 deletions
+28
View File
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: clear_dataset.py
# Created Date: Thursday March 24th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Thursday, 24th March 2022 3:32:40 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import os
import json
from utilities.json_config import readConfig, writeConfig
if __name__ == "__main__":
savePath = "./vggface2hq_failed.txt"
env_config = readConfig('env/env.json')
env_config = env_config["path"]
dataset_root = env_config["dataset_paths"]["vggface2_hq"]
with open(savePath,'r') as logf:
for line in logf:
img_path = os.path.join(dataset_root,line[:-1]).replace("\\","/")
try:
os.rename(img_path,img_path+".deleted")
except: pass
+15 -10
View File
@@ -5,7 +5,7 @@
# Created Date: Saturday February 26th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Sunday, 27th February 2022 7:50:18 pm
# Last Modified: Thursday, 24th March 2022 11:24:26 am
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
@@ -255,7 +255,8 @@ class Generator(nn.Module):
padding_size= int((k_size -1)/2)
padding_type= 'reflect'
activation = nn.LeakyReLU(0.2)
# activation = nn.LeakyReLU(0.2)
activation = nn.ReLU()
# self.first_layer = nn.Sequential(nn.ReflectionPad2d(3), nn.Conv2d(3, 64, kernel_size=7, padding=0, bias=False),
# nn.BatchNorm2d(64), activation)
@@ -266,13 +267,13 @@ class Generator(nn.Module):
# self.first_layer = nn.Sequential(nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64), activation)
### downsample
self.down1 = ResDownSampleBlock(in_channel, in_channel*2,res_mode=res_mode)
self.down1 = ResDownSampleBlock(in_channel, in_channel*2, activation=activation, res_mode=res_mode) # 128
# nn.Sequential(
# nn.Conv2d(in_channel, in_channel*2, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*2),
# activation) # 128
self.down2 = ResDownSampleBlock(in_channel*2, in_channel*4,res_mode=res_mode)
self.down2 = ResDownSampleBlock(in_channel*2, in_channel*4, activation=activation, res_mode=res_mode) # 64
# nn.Sequential(
# nn.Conv2d(in_channel*2, in_channel*4, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*4),
@@ -280,7 +281,9 @@ class Generator(nn.Module):
# self.lstu = LSTU(in_channel*4,in_channel*4,in_channel*8,4)
self.down3 = ResDownSampleBlock(in_channel*4, in_channel*8,res_mode=res_mode)
self.down3 = ResDownSampleBlock(in_channel*4, in_channel*8, activation=activation, res_mode=res_mode) # 32
self.down4 = ResDownSampleBlock(in_channel*8, in_channel*8, activation=activation, res_mode=res_mode) # 16
# nn.Sequential(
# nn.Conv2d(in_channel*4, in_channel*8, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*8),
@@ -297,10 +300,12 @@ class Generator(nn.Module):
BN = []
for i in range(res_num):
BN += [
ResnetBlock_Adain(in_channel*8, latent_size=id_dim,res_mode=res_mode)]
ResnetBlock_Adain(in_channel*8, latent_size=id_dim, activation=activation, res_mode=res_mode)]
self.BottleNeck = nn.Sequential(*BN)
self.up5 = ResUpSampleBlock(in_channel*8, in_channel*8, id_dim, activation=activation, res_mode=res_mode) # 32
self.up4 = ResUpSampleBlock(in_channel*8,in_channel*8,id_dim,res_mode=res_mode) # 64
self.up4 = ResUpSampleBlock(in_channel*8, in_channel*4, id_dim, activation=activation, res_mode=res_mode) # 64
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*8, in_channel*8, kernel_size=3, stride=1, padding=1, bias=False),
@@ -308,7 +313,7 @@ class Generator(nn.Module):
# activation
# )
self.up3 = ResUpSampleBlock(in_channel*8,in_channel*4,id_dim,res_mode=res_mode) # 128
self.up3 = ResUpSampleBlock(in_channel*4, in_channel*2, id_dim, activation=activation, res_mode=res_mode) # 128
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*8, in_channel*4, kernel_size=3, stride=1, padding=1, bias=False),
@@ -316,7 +321,7 @@ class Generator(nn.Module):
# activation
# )
self.up2 = ResUpSampleBlock(in_channel*4,in_channel*2,id_dim,res_mode=res_mode) # 256
self.up2 = ResUpSampleBlock(in_channel*2, in_channel, id_dim, activation=activation, res_mode=res_mode) # 256
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*4, in_channel*2, kernel_size=3, stride=1, padding=1, bias=False),
@@ -324,7 +329,7 @@ class Generator(nn.Module):
# activation
# )
self.up1 = ResUpSampleBlock(in_channel*2,in_channel,id_dim,res_mode=res_mode) # 512
self.up1 = ResUpSampleBlock(in_channel, in_channel , id_dim, activation=activation, res_mode=res_mode) # 512
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*2, in_channel, kernel_size=3, stride=1, padding=1, bias=False),
+284
View File
@@ -0,0 +1,284 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: Generator_Invobn_config1.py
# Created Date: Saturday February 26th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Thursday, 24th March 2022 2:38:05 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import torch
from torch import nn
import torch.nn.functional as F
import math
from components.LSTU import LSTU
class ResBlk(nn.Module):
def __init__(self, dim_in, dim_out, actv=nn.LeakyReLU(0.2),
normalize=False, downsample=False):
super().__init__()
self.actv = actv
self.normalize = normalize
self.downsample = downsample
self.learned_sc = dim_in != dim_out
self.equal_var = math.sqrt(2)
self._build_weights(dim_in, dim_out)
def _build_weights(self, dim_in, dim_out):
self.conv1 = nn.Conv2d(dim_in, dim_in, 3, 1, 1)
self.conv2 = nn.Conv2d(dim_in, dim_out, 3, 1, 1)
if self.normalize:
self.norm1 = nn.InstanceNorm2d(dim_in, affine=True)
self.norm2 = nn.InstanceNorm2d(dim_in, affine=True)
if self.learned_sc:
self.conv1x1 = nn.Conv2d(dim_in, dim_out, 1, 1, 0, bias=False)
def _shortcut(self, x):
if self.learned_sc:
x = self.conv1x1(x)
if self.downsample:
x = F.avg_pool2d(x, 2)
return x
def _residual(self, x):
if self.normalize:
x = self.norm1(x)
x = self.actv(x)
x = self.conv1(x)
if self.downsample:
x = F.avg_pool2d(x, 2)
if self.normalize:
x = self.norm2(x)
x = self.actv(x)
x = self.conv2(x)
return x
def forward(self, x):
x = self._shortcut(x) + self._residual(x)
return x /self.equal_var # unit variance
class AdaIN(nn.Module):
def __init__(self, style_dim, num_features):
super().__init__()
self.norm = nn.InstanceNorm2d(num_features, affine=False)
self.fc = nn.Linear(style_dim, num_features*2)
def forward(self, x, s):
h = self.fc(s)
h = h.view(h.size(0), h.size(1), 1, 1)
gamma, beta = torch.chunk(h, chunks=2, dim=1)
return (1 + gamma) * self.norm(x) + beta
class AdainResBlk(nn.Module):
def __init__(self, dim_in, dim_out, style_dim=512,
actv=nn.LeakyReLU(0.2), upsample=False):
super().__init__()
self.actv = actv
self.upsample = upsample
self.learned_sc = dim_in != dim_out
self.equal_var = math.sqrt(2)
self._build_weights(dim_in, dim_out, style_dim)
def _build_weights(self, dim_in, dim_out, style_dim=64):
self.conv1 = nn.Conv2d(dim_in, dim_out, 3, 1, 1)
self.conv2 = nn.Conv2d(dim_out, dim_out, 3, 1, 1)
self.norm1 = AdaIN(style_dim, dim_in)
self.norm2 = AdaIN(style_dim, dim_out)
if self.learned_sc:
self.conv1x1 = nn.Conv2d(dim_in, dim_out, 1, 1, 0, bias=False)
def _shortcut(self, x):
if self.upsample:
x = F.interpolate(x, scale_factor=2, mode='nearest')
if self.learned_sc:
x = self.conv1x1(x)
return x
def _residual(self, x, s):
x = self.norm1(x, s)
x = self.actv(x)
if self.upsample:
x = F.interpolate(x, scale_factor=2, mode='nearest')
x = self.conv1(x)
x = self.norm2(x, s)
x = self.actv(x)
x = self.conv2(x)
return x
def forward(self, x, s):
out = self._residual(x, s)
out = (out + self._shortcut(x)) / self.equal_var
return out
class HighPass(nn.Module):
def __init__(self, w_hpf, device):
super(HighPass, self).__init__()
self.register_buffer('filter',
torch.tensor([[-1, -1, -1],
[-1, 8., -1],
[-1, -1, -1]]) / w_hpf)
def forward(self, x):
filter = self.filter.unsqueeze(0).unsqueeze(1).repeat(x.size(1), 1, 1, 1)
return F.conv2d(x, filter, padding=1, groups=x.size(1))
class Generator(nn.Module):
def __init__(
self,
**kwargs
):
super().__init__()
id_dim = kwargs["id_dim"]
k_size = kwargs["g_kernel_size"]
res_num = kwargs["res_num"]
in_channel = kwargs["in_channel"]
up_mode = kwargs["up_mode"]
aggregator = kwargs["aggregator"]
res_mode = kwargs["res_mode"]
padding_size= int((k_size -1)/2)
padding_type= 'reflect'
activation = nn.LeakyReLU(0.2)
# activation = nn.ReLU()
# self.first_layer = nn.Sequential(nn.ReflectionPad2d(3), nn.Conv2d(3, 64, kernel_size=7, padding=0, bias=False),
# nn.BatchNorm2d(64), activation)
# self.first_layer = nn.Sequential(
# nn.Conv2d(3, in_channel, kernel_size=1, padding=0, bias=False),
# # nn.BatchNorm2d(in_channel),
# nn.InstanceNorm2d(in_channel, affine=True),
# activation) # 256
self.from_rgb = nn.Conv2d(3, in_channel, 1, 1, 0)
# self.first_layer = nn.Sequential(nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64), activation)
### downsample
self.down1 = ResBlk(in_channel, in_channel, normalize=True, downsample=True)# 128
self.down2 = ResBlk(in_channel, in_channel*2, normalize=True, downsample=True)# 128
# ResDownSampleBlock(in_channel, in_channel*2, activation=activation, res_mode=res_mode) # 128
# nn.Sequential(
# nn.Conv2d(in_channel, in_channel*2, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*2),
# activation) # 128
self.down3 = ResBlk(in_channel*2, in_channel*4,normalize=True, downsample=True)# 64
# ResDownSampleBlock(in_channel*2, in_channel*4, activation=activation, res_mode=res_mode) # 64
# nn.Sequential(
# nn.Conv2d(in_channel*2, in_channel*4, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*4),
# activation) # 64
# self.lstu = LSTU(in_channel*4,in_channel*4,in_channel*8,4)
self.down4 = ResBlk(in_channel*4, in_channel*8, normalize=True, downsample=True)# 32
# ResDownSampleBlock(in_channel*4, in_channel*8, activation=activation, res_mode=res_mode) # 32
self.down5 = ResBlk(in_channel*8, in_channel*8, normalize=True, downsample=True)# 16
# ResDownSampleBlock(in_channel*8, in_channel*8, activation=activation, res_mode=res_mode) # 16
# nn.Sequential(
# nn.Conv2d(in_channel*4, in_channel*8, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*8),
# activation) # 32
# self.down4 = nn.Sequential(
# nn.Conv2d(in_channel*8, in_channel*8, stride=2, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*8),
# activation)
### resnet blocks
BN = []
for i in range(res_num):
BN += [
AdainResBlk(in_channel*8, in_channel*8, style_dim=id_dim, upsample=False)]
self.BottleNeck = nn.Sequential(*BN)
self.up5 = AdainResBlk(in_channel*8, in_channel*8, style_dim=id_dim, upsample=True) # 32
self.up4 = AdainResBlk(in_channel*8, in_channel*4, style_dim=id_dim, upsample=True) # 64
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*8, in_channel*8, kernel_size=3, stride=1, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*8),
# activation
# )
self.up3 = AdainResBlk(in_channel*4, in_channel*2, style_dim=id_dim, upsample=True) # 128
# ResUpSampleBlock(in_channel*4, in_channel*2, id_dim, activation=activation, res_mode=res_mode) # 128
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*8, in_channel*4, kernel_size=3, stride=1, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*4),
# activation
# )
self.up2 = AdainResBlk(in_channel*2, in_channel, style_dim=id_dim, upsample=True) # 256
# ResUpSampleBlock(in_channel*2, in_channel, id_dim, activation=activation, res_mode=res_mode) # 256
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*4, in_channel*2, kernel_size=3, stride=1, padding=1, bias=False),
# nn.BatchNorm2d(in_channel*2),
# activation
# )
self.up1 = AdainResBlk(in_channel, in_channel, style_dim=id_dim, upsample=True) # 512
# ResUpSampleBlock(in_channel, in_channel , id_dim, activation=activation, res_mode=res_mode) # 512
# nn.Sequential(
# nn.Upsample(scale_factor=2, mode='bilinear'),
# nn.Conv2d(in_channel*2, in_channel, kernel_size=3, stride=1, padding=1, bias=False),
# nn.BatchNorm2d(in_channel),
# activation
# )
# self.last_layer = nn.Sequential(nn.Conv2d(64, 3, kernel_size=3, padding=1))
# self.last_layer = nn.Sequential(nn.ReflectionPad2d(1),
# nn.Conv2d(3, 3, kernel_size=3, padding=0))
self.to_rgb = nn.Sequential(
nn.InstanceNorm2d(in_channel, affine=True),
nn.LeakyReLU(0.2),
nn.Conv2d(in_channel, 3, 1, 1, 0))
# self.last_layer = nn.Sequential(nn.ReflectionPad2d(3),
# nn.Conv2d(64, 3, kernel_size=7, padding=0))
# self.__weights_init__()
# def __weights_init__(self):
# for layer in self.encoder:
# if isinstance(layer,nn.Conv2d):
# nn.init.xavier_uniform_(layer.weight)
# for layer in self.encoder2:
# if isinstance(layer,nn.Conv2d):
# nn.init.xavier_uniform_(layer.weight)
def forward(self, img, id):
res = self.from_rgb(img)
res = self.down1(res)
res = self.down2(res)
res = self.down3(res)
res = self.down4(res)
res = self.down5(res)
for i in range(len(self.BottleNeck)):
res = self.BottleNeck[i](res, id)
res = self.up5(res,id)
res = self.up4(res,id)
res = self.up3(res,id)
res = self.up2(res,id) # + skip
res = self.up1(res,id)
res = self.to_rgb(res)
return res
File diff suppressed because it is too large Load Diff
+1 -1
View File
@@ -1,3 +1,3 @@
nohup python train_multigpu.py > cycle_res1.log 2>&1 &
nohup python train_multigpu.py > cycle_res2.log 2>&1 &
+10 -6
View File
@@ -5,7 +5,7 @@
# Created Date: Saturday July 3rd 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Thursday, 24th March 2022 12:14:59 am
# Last Modified: Friday, 25th March 2022 6:13:32 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
@@ -17,7 +17,9 @@ from torch.backends import cudnn
from utilities.json_config import readConfig
from utilities.reporter import Reporter
from utilities.sshupload import fileUploaderClass
import warnings
warnings.filterwarnings('ignore')
def str2bool(v):
return v.lower() in ('true')
@@ -30,26 +32,28 @@ def getParameters():
parser = argparse.ArgumentParser()
# general settings
parser.add_argument('-v', '--version', type=str, default='cycle_lstu1', #cycle_res1 cycle_lstu1 depthwise depthwise_config0 Invobn_resinvo1
parser.add_argument('-v', '--version', type=str, default='cycle_res1', #cycle_res1 cycle_res2 cycle_res3 cycle_lstu1 depthwise depthwise_config0 Invobn_resinvo1
help="version name for train, test, finetune")
parser.add_argument('-c', '--cuda', type=int, default=0) # >0 if it is set as -1, program will use CPU
parser.add_argument('-s', '--checkpoint_step', type=int, default=40000,
parser.add_argument('-s', '--checkpoint_step', type=int, default=180000,
help="checkpoint epoch for test phase or finetune phase")
parser.add_argument('--start_checkpoint_step', type=int, default=10000,
help="checkpoint epoch for test phase or finetune phase")
# test
parser.add_argument('-t', '--test_script_name', type=str, default='image')
parser.add_argument('-t', '--test_script_name', type=str, default='image_list') #image_list image_nofusion
parser.add_argument('-b', '--batch_size', type=int, default=1)
parser.add_argument('-n', '--node_ip', type=str, default='101.33.242.26') # 101.33.242.26 2001:da8:8000:6880:f284:d61c:3c76:f9cb
parser.add_argument('--crop_mode', type=str, default="vggface", choices=['ffhq','vggface'], help='crop mode for face detector')
parser.add_argument('-i', '--id_imgs', type=str, default='G:\\swap_data\\ID\\hinton.jpg')
parser.add_argument('-i', '--id_imgs', type=str, default='G:\\swap_data\\ID\\dlrb2.jpeg') # 'G:\\swap_data\\FF++\\996_img_00288.jpg' G:\\swap_data\\ID\\hinton.jpg
# parser.add_argument('-i', '--id_imgs', type=str, default='G:\\VGGFace2-HQ\\VGGface2_ffhq_align_256_9_28_512_bygfpgan\\n000002\\0027_01.jpg')
parser.add_argument('-a', '--attr_files', type=str, default='G:\\swap_data\\ID\\bengio.jpeg',
parser.add_argument('-a', '--attr_files', type=str, default='G:/swap_data/video/1', # G:\\swap_data\\ID\\bengio.jpg G:\\swap_data\\FF++\\056_img_00228.jpg
help="file path for attribute images or video")
parser.add_argument('--img_list_txt', type=str, default='./test_imgs_list.txt', # G:\\swap_data\\ID\\bengio.jpg G:\\swap_data\\FF++\\056_img_00228.jpg
help="file path for image list txt")
parser.add_argument('--use_specified_data', action='store_true')
parser.add_argument('--specified_data_paths', type=str, nargs='+', default=[""], help='paths to specified files')
+17
View File
@@ -0,0 +1,17 @@
G:/swap_data/ID/hsw.jpg;G:/swap_data/ID/cruise.jpg;fusion
G:/swap_data/ID/06.jpg;G:/swap_data/ID/hm.jpg;fusion
G:/swap_data/ID/1.png;G:/swap_data/ID/2.png;fusion
G:/swap_data/ID/hinton.jpg;G:/swap_data/ID/bengio.jpg;fusion
G:/swap_data/ID/hsw.jpg;G:/swap_data/ID/ts1.jpg;fusion
G:/swap_data/ID/hsw.jpg;G:/swap_data/ID/lyf2.jpeg;fusion
G:/swap_data/FF++/996_img_00288.jpg;G:/swap_data/FF++/056_img_00228.jpg;no
G:/swap_data/ID/gxt3.jpeg;G:/swap_data/ID/lyf5.jpeg;fusion
G:/swap_data/ID/hsw.jpg;G:/swap_data/ID/hb.jpeg;fusion
G:/swap_data/ID/06.jpg;G:/swap_data/ID/cruise2.jpeg;fusion
G:/swap_data/FF++/019_img_00139.jpg;G:/swap_data/FF++/018_img_00088.jpg;no
G:/swap_data/FF++/052_img_00033.jpg;G:/swap_data/FF++/108_img_00150.jpg;no
G:/swap_data/FF++/011_img_00448.jpg;G:/swap_data/FF++/805_img_00252.jpg;no
G:/swap_data/FF++/638_img_00000.jpg;G:/swap_data/FF++/640_img_00248.jpg;no
G:/swap_data/FF++/819_img_00651.jpg;G:/swap_data/FF++/786_img_00156.jpg;no
G:/swap_data/FF++/416_img_00032.jpg;G:/swap_data/FF++/342_img_00062.jpg;no
G:/swap_data/ID/hsw.jpg;G:/swap_data/ID/lyf4.jpeg;fusion
+259
View File
@@ -0,0 +1,259 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: tester_commonn.py
# Created Date: Saturday July 3rd 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Friday, 25th March 2022 2:07:24 am
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
import os
import cv2
import time
import glob
import torch
import torch.nn.functional as F
from torchvision import transforms
import numpy as np
from PIL import Image
from insightface_func.face_detect_crop_single import Face_detect_crop
class Tester(object):
def __init__(self, config, reporter):
self.config = config
# logger
self.reporter = reporter
self.transformer_Arcface = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
self.imagenet_std = torch.tensor([0.229, 0.224, 0.225]).cuda().view(3,1,1)
self.imagenet_mean = torch.tensor([0.485, 0.456, 0.406]).cuda().view(3,1,1)
def __init_framework__(self):
'''
This function is designed to define the framework,
and print the framework information into the log file
'''
#===============build models================#
print("build models...")
# TODO [import models here]
model_config = self.config["model_configs"]
gscript_name = self.config["com_base"] + model_config["g_model"]["script"]
class_name = model_config["g_model"]["class_name"]
package = __import__(gscript_name, fromlist=True)
gen_class = getattr(package, class_name)
self.network = gen_class(**model_config["g_model"]["module_params"])
# TODO replace below lines to define the model framework
self.network = gen_class(**model_config["g_model"]["module_params"])
self.network = self.network.eval()
# for name in self.network.state_dict():
# print(name)
self.features = {}
mapping_layers = [
"first_layer",
"down4",
"BottleNeck.2"
]
# print and recorde model structure
self.reporter.writeInfo("Model structure:")
self.reporter.writeModel(self.network.__str__())
arcface1 = torch.load(self.arcface_ckpt, map_location=torch.device("cpu"))
self.arcface = arcface1['model'].module
self.arcface.eval()
self.arcface.requires_grad_(False)
model_path = os.path.join(self.config["project_checkpoints"],
"step%d_%s.pth"%(self.config["checkpoint_step"],
self.config["checkpoint_names"]["generator_name"]))
self.network.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
print('loaded trained backbone model step {}...!'.format(self.config["checkpoint_step"]))
# train in GPU
if self.config["cuda"] >=0:
self.network = self.network.cuda()
self.arcface = self.arcface.cuda()
def test(self):
save_dir = self.config["test_samples_path"]
ckp_step = self.config["checkpoint_step"]
version = self.config["version"]
crop_mode = self.config["crop_mode"]
list_txt = self.config["img_list_txt"]
specified_save_path = self.config["specified_save_path"]
self.arcface_ckpt= self.config["arcface_ckpt"]
imgs_list = []
self.reporter.writeInfo("Version %s"%version)
if os.path.isdir(specified_save_path):
print("Input a legal specified save path!")
save_dir = specified_save_path
imgs_list = []
with open(list_txt,'r') as logf:
for line in logf:
cells = line.split(";")
imgs_list.append([cells[0],cells[1],cells[2].replace("\n","")])
# models
self.__init_framework__()
mode = crop_mode.lower()
if mode == "vggface":
mode = "none"
self.detect = Face_detect_crop(name='antelope', root='./insightface_func/models')
self.detect.prepare(ctx_id = 0, det_thresh=0.6, det_size=(640,640),mode = mode)
cos_loss = torch.nn.CosineSimilarity()
font = cv2.FONT_HERSHEY_SIMPLEX
# Start time
import datetime
print("Start to test at %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print('Start =================================== test...')
start_time = time.time()
self.network.eval()
cos_dict = {}
average_cos = 0
with torch.no_grad():
for img in imgs_list:
id_img_n, attr_img_n, fusion= img
print("id image:%s---attr image:%s"%(id_img_n, attr_img_n))
id_img = cv2.imread(id_img_n)
print(fusion)
if fusion.lower() == "fusion":
try:
id_img_align_crop, _ = self.detect.get(id_img,512)
except:
print("Do not detect a face!")
continue
# id_basename = os.path.splitext(os.path.basename(id_img_n))[0]
# cv2.imwrite(os.path.join(save_dir, "id_%s.png"%(id_basename)),id_img_align_crop[0])
id_img_align_crop_pil = Image.fromarray(cv2.cvtColor(id_img_align_crop[0],cv2.COLOR_BGR2RGB))
else:
id_img_align_crop_pil = Image.fromarray(cv2.cvtColor(id_img,cv2.COLOR_BGR2RGB))
id_img = self.transformer_Arcface(id_img_align_crop_pil)
id_img = id_img.unsqueeze(0).cuda()
#create latent id
id_img = F.interpolate(id_img,size=(112,112), mode='bicubic')
latend_id = self.arcface(id_img)
latend_id = F.normalize(latend_id, p=2, dim=1)
attr_img_ori= cv2.imread(attr_img_n)
if fusion.lower() == "fusion":
try:
attr_img_align_crop, mat = self.detect.get(attr_img_ori,512)
except:
print("Do not detect a face!")
continue
# attr_basename = os.path.splitext(os.path.basename(attr_img_n))[0]
# cv2.imwrite(os.path.join(save_dir, "attr_%s.png"%(attr_basename)),attr_img_align_crop[0])
attr_img_align_crop_pil = Image.fromarray(cv2.cvtColor(attr_img_align_crop[0],cv2.COLOR_BGR2RGB))
else:
attr_img_align_crop_pil = Image.fromarray(cv2.cvtColor(attr_img_ori,cv2.COLOR_BGR2RGB))
attr_img = self.transformer_Arcface(attr_img_align_crop_pil).unsqueeze(0).cuda()
attr_img_arc = F.interpolate(attr_img,size=(112,112), mode='bicubic')
attr_id = self.arcface(attr_img_arc)
attr_id = F.normalize(attr_id, p=2, dim=1)
cos_dis = 1 - cos_loss(latend_id, attr_id)
results = self.network(attr_img, latend_id)
results_arc = F.interpolate(results,size=(112,112), mode='bicubic')
results_arc = self.arcface(results_arc)
results_arc = F.normalize(results_arc, p=2, dim=1)
results_cos_dis = 1 - cos_loss(latend_id, results_arc)
average_cos += results_cos_dis
results = results * self.imagenet_std + self.imagenet_mean
results = results.cpu().permute(0,2,3,1)[0,...]
results = results.numpy()
results = np.clip(results,0.0,1.0)
if fusion.lower() == "fusion":
mat = mat[0]
img_white = np.full((512,512), 255, dtype=float)
# inverse the Affine transformation matrix
mat_rev = np.zeros([2,3])
div1 = mat[0][0]*mat[1][1]-mat[0][1]*mat[1][0]
mat_rev[0][0] = mat[1][1]/div1
mat_rev[0][1] = -mat[0][1]/div1
mat_rev[0][2] = -(mat[0][2]*mat[1][1]-mat[0][1]*mat[1][2])/div1
div2 = mat[0][1]*mat[1][0]-mat[0][0]*mat[1][1]
mat_rev[1][0] = mat[1][0]/div2
mat_rev[1][1] = -mat[0][0]/div2
mat_rev[1][2] = -(mat[0][2]*mat[1][0]-mat[0][0]*mat[1][2])/div2
orisize = (attr_img_ori.shape[1], attr_img_ori.shape[0])
target_image = cv2.warpAffine(results, mat_rev, orisize)
img_white = cv2.warpAffine(img_white, mat_rev, orisize)
img_white[img_white>20] =255
img_mask = img_white
kernel = np.ones((40,40),np.uint8)
img_mask = cv2.erode(img_mask,kernel,iterations = 1)
kernel_size = (20, 20)
blur_size = tuple(2*i+1 for i in kernel_size)
img_mask = cv2.GaussianBlur(img_mask, blur_size, 0)
img_mask /= 255
img_mask = np.reshape(img_mask, [img_mask.shape[0],img_mask.shape[1],1])
target_image = np.array(target_image, dtype=np.float)[..., ::-1] * 255
img1 = np.array(attr_img_ori, dtype=np.float)
img1 = img_mask * target_image + (1-img_mask) * img1
else:
results = results*255
img1 = cv2.cvtColor(results,cv2.COLOR_RGB2BGR)
final_img = img1.astype(np.uint8)
id_basename = os.path.basename(id_img_n)
id_basename = os.path.splitext(os.path.basename(id_img_n))[0]
attr_basename = os.path.splitext(os.path.basename(attr_img_n))[0]
final_img = cv2.putText(final_img, 'id dis=%.4f'%results_cos_dis, (50, 50), font, 0.8, (15, 9, 255), 2)
final_img = cv2.putText(final_img, 'id--attr dis=%.4f'%cos_dis, (50, 80), font, 0.8, (15, 9, 255), 2)
print(save_dir)
save_filename = os.path.join(save_dir,
"id_%s--attr_%s_ckp_%s_v_%s.png"%(id_basename,
attr_basename,ckp_step,version))
cv2.imwrite(save_filename, final_img)
average_cos /= len(imgs_list)
elapsed = time.time() - start_time
elapsed = str(datetime.timedelta(seconds=elapsed))
print("Elapsed [{}]".format(elapsed))
print("Average cosin similarity between ID and results [{}]".format(average_cos.item()))
self.reporter.writeInfo("Average cosin similarity between ID and results [{}]".format(average_cos.item()))
+184
View File
@@ -0,0 +1,184 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: tester_commonn.py
# Created Date: Saturday July 3rd 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Thursday, 24th March 2022 12:40:35 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
import os
import cv2
import time
import glob
import torch
import torch.nn.functional as F
from torchvision import transforms
import numpy as np
from PIL import Image
from insightface_func.face_detect_crop_single import Face_detect_crop
class Tester(object):
def __init__(self, config, reporter):
self.config = config
# logger
self.reporter = reporter
self.transformer_Arcface = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
self.imagenet_std = torch.tensor([0.229, 0.224, 0.225]).cuda().view(3,1,1)
self.imagenet_mean = torch.tensor([0.485, 0.456, 0.406]).cuda().view(3,1,1)
def __init_framework__(self):
'''
This function is designed to define the framework,
and print the framework information into the log file
'''
#===============build models================#
print("build models...")
# TODO [import models here]
model_config = self.config["model_configs"]
gscript_name = self.config["com_base"] + model_config["g_model"]["script"]
class_name = model_config["g_model"]["class_name"]
package = __import__(gscript_name, fromlist=True)
gen_class = getattr(package, class_name)
self.network = gen_class(**model_config["g_model"]["module_params"])
# TODO replace below lines to define the model framework
self.network = gen_class(**model_config["g_model"]["module_params"])
self.network = self.network.eval()
# for name in self.network.state_dict():
# print(name)
self.features = {}
mapping_layers = [
"first_layer",
"down4",
"BottleNeck.2"
]
# print and recorde model structure
self.reporter.writeInfo("Model structure:")
self.reporter.writeModel(self.network.__str__())
arcface1 = torch.load(self.arcface_ckpt, map_location=torch.device("cpu"))
self.arcface = arcface1['model'].module
self.arcface.eval()
self.arcface.requires_grad_(False)
model_path = os.path.join(self.config["project_checkpoints"],
"step%d_%s.pth"%(self.config["checkpoint_step"],
self.config["checkpoint_names"]["generator_name"]))
self.network.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
print('loaded trained backbone model step {}...!'.format(self.config["checkpoint_step"]))
# train in GPU
if self.config["cuda"] >=0:
self.network = self.network.cuda()
self.arcface = self.arcface.cuda()
def test(self):
save_dir = self.config["test_samples_path"]
ckp_step = self.config["checkpoint_step"]
version = self.config["version"]
id_imgs = self.config["id_imgs"]
crop_mode = self.config["crop_mode"]
attr_files = self.config["attr_files"]
specified_save_path = self.config["specified_save_path"]
self.arcface_ckpt= self.config["arcface_ckpt"]
imgs_list = []
self.reporter.writeInfo("Version %s"%version)
if os.path.isdir(specified_save_path):
print("Input a legal specified save path!")
save_dir = specified_save_path
if os.path.isdir(attr_files):
print("Input a dir....")
imgs = glob.glob(os.path.join(attr_files,"**"), recursive=True)
for item in imgs:
imgs_list.append(item)
print(imgs_list)
else:
print("Input an image....")
imgs_list.append(attr_files)
id_basename = os.path.basename(id_imgs)
id_basename = os.path.splitext(os.path.basename(id_imgs))[0]
# models
self.__init_framework__()
id_img = cv2.imread(id_imgs)
id_img_align_crop_pil = Image.fromarray(cv2.cvtColor(id_img,cv2.COLOR_BGR2RGB))
id_img = self.transformer_Arcface(id_img_align_crop_pil)
id_img = id_img.unsqueeze(0).cuda()
#create latent id
id_img = F.interpolate(id_img,size=(112,112), mode='bicubic')
latend_id = self.arcface(id_img)
latend_id = F.normalize(latend_id, p=2, dim=1)
cos_loss = torch.nn.CosineSimilarity()
# Start time
import datetime
print("Start to test at %s"%(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print('Start =================================== test...')
start_time = time.time()
self.network.eval()
cos_dict = {}
average_cos = 0
with torch.no_grad():
for img in imgs_list:
print(img)
attr_img_ori= cv2.imread(img)
attr_img_align_crop_pil = Image.fromarray(cv2.cvtColor(attr_img_ori,cv2.COLOR_BGR2RGB))
attr_img = self.transformer_Arcface(attr_img_align_crop_pil).unsqueeze(0).cuda()
attr_img_arc = F.interpolate(attr_img,size=(112,112), mode='bicubic')
# cv2.imwrite(os.path.join("./swap_results", "id_%s.png"%(id_basename)),id_img_align_crop[0])
attr_id = self.arcface(attr_img_arc)
attr_id = F.normalize(attr_id, p=2, dim=1)
cos_dis = 1 - cos_loss(latend_id, attr_id)
results = self.network(attr_img, latend_id)
results_arc = F.interpolate(results,size=(112,112), mode='bicubic')
results_arc = self.arcface(results_arc)
results_arc = F.normalize(results_arc, p=2, dim=1)
results_cos_dis = 1 - cos_loss(latend_id, results_arc)
average_cos += results_cos_dis
results = results * self.imagenet_std + self.imagenet_mean
results = results.cpu().permute(0,2,3,1)[0,...]
results = results.numpy()
results = np.clip(results,0.0,1.0) * 255
results = cv2.cvtColor(results, cv2.COLOR_RGB2BGR)
final_img = results.astype(np.uint8)
attr_basename = os.path.splitext(os.path.basename(img))[0]
save_filename = os.path.join(save_dir,
"id_%s--attr_%s_ckp_%s_v_%s.png"%(id_basename,
attr_basename,ckp_step,version))
cv2.imwrite(save_filename, final_img)
average_cos /= len(imgs_list)
elapsed = time.time() - start_time
elapsed = str(datetime.timedelta(seconds=elapsed))
print("Elapsed [{}]".format(elapsed))
print("Average cosin similarity between ID and results [{}]".format(average_cos.item()))
self.reporter.writeInfo("Average cosin similarity between ID and results [{}]".format(average_cos.item()))
+4 -4
View File
@@ -5,7 +5,7 @@
# Created Date: Tuesday April 28th 2020
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Friday, 4th March 2022 1:53:03 am
# Last Modified: Thursday, 24th March 2022 2:14:07 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2020 Shanghai Jiao Tong University
#############################################################
@@ -31,7 +31,7 @@ def getParameters():
parser = argparse.ArgumentParser()
# general settings
parser.add_argument('-v', '--version', type=str, default='cycle_res1',
parser.add_argument('-v', '--version', type=str, default='cycle_res3',
help="version name for train, test, finetune")
parser.add_argument('-t', '--tag', type=str, default='cycle',
help="tag for current experiment")
@@ -40,13 +40,13 @@ def getParameters():
choices=['train', 'finetune','debug'],
help="The phase of current project")
parser.add_argument('-c', '--gpus', type=int, nargs='+', default=[0,1]) # <0 if it is set as -1, program will use CPU
parser.add_argument('-c', '--gpus', type=int, nargs='+', default=[0,1,2,3]) # <0 if it is set as -1, program will use CPU
parser.add_argument('-e', '--ckpt', type=int, default=74,
help="checkpoint epoch for test phase or finetune phase")
# training
parser.add_argument('--experiment_description', type=str,
default="cycle配合残差decoder,ID注入放在decoder中")
default="cycle配合残差decoder,改用starganv2的generator结构")
parser.add_argument('--train_yaml', type=str, default="train_cycleloss_res.yaml")
+5 -5
View File
@@ -4,13 +4,13 @@ train_script_name: multi_gpu_cycle
# models' scripts
model_configs:
g_model:
script: Generator_Res_config
script: Generator_Res_config2
class_name: Generator
module_params:
id_dim: 512
g_kernel_size: 3
in_channel: 64
res_num: 9
res_num: 3
up_mode: bilinear
aggregator: "conv"
res_mode: "conv"
@@ -27,7 +27,7 @@ model_configs:
arcface_ckpt: arcface_ckpt/arcface_checkpoint.tar
# Training information
batch_size: 18
batch_size: 8
# Dataset
dataloader: VGGFace2HQ_multigpu
@@ -45,12 +45,12 @@ eval_batch_size: 2
# Optimizer
optim_type: Adam
g_optim_config:
lr: 0.0006
lr: 0.0004
betas: [ 0, 0.99]
eps: !!float 1e-8
d_optim_config:
lr: 0.0006
lr: 0.0004
betas: [ 0, 0.99]
eps: !!float 1e-8
+29
View File
@@ -0,0 +1,29 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: translation_list2json.py
# Created Date: Thursday March 24th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Thursday, 24th March 2022 3:20:06 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import json
if __name__ == "__main__":
savePath = "./vggface2hq_failed.txt"
log_txt = "./vggface2hq_failed.json"
images = {}
with open(savePath,'r') as logf:
for line in logf:
cells = line.split("/")
if images.__contains__(cells[0]):
images[cells[0]] += [cells[1]]
else:
images[cells[0]] = [cells[1]]
with open(log_txt, 'w') as cf:
configjson = json.dumps(images, indent=4)
cf.writelines(configjson)
+67621
View File
File diff suppressed because it is too large Load Diff