This commit is contained in:
chenxuanhong
2022-02-27 19:37:24 +08:00
parent 1f2aa26bd1
commit dc11678ed6
15 changed files with 2603 additions and 22 deletions
+14 -6
View File
@@ -60,7 +60,7 @@
"face_crop.py": 1643789609.1834445,
"face_crop_video.py": 1643815024.5516832,
"similarity.py": 1643269705.1073737,
"train_multigpu.py": 1645548174.898882,
"train_multigpu.py": 1645935139.5672748,
"components\\arcface_decoder.py": 1643396144.2575414,
"components\\Generator_nobias.py": 1643179001.810856,
"data_tools\\data_loader_VGGFace2HQ_multigpu.py": 1644861019.9044807,
@@ -105,13 +105,13 @@
"components\\Generator_ori.py": 1644689174.414655,
"losses\\cos.py": 1644229583.4023254,
"data_tools\\data_loader_VGGFace2HQ_multigpu1.py": 1644860106.943826,
"speed_test.py": 1645266259.6685307,
"speed_test.py": 1645863205.1120403,
"components\\DeConv_Invo.py": 1644426607.1588645,
"components\\Generator_reduce_up.py": 1644688655.2096283,
"components\\Generator_upsample.py": 1644689723.8293872,
"components\\misc\\Involution.py": 1644509321.5267963,
"train_yamls\\train_Invoup.yaml": 1644689981.9794765,
"flops.py": 1645540971.0513766,
"flops.py": 1645883189.3803008,
"detection_test.py": 1644935512.6830947,
"components\\DeConv_Depthwise.py": 1645064447.4379447,
"components\\DeConv_Depthwise1.py": 1644946969.5054545,
@@ -124,13 +124,21 @@
"train_yamls\\train_Depthwise.yaml": 1644860961.099242,
"train_yamls\\train_depthwise_modulation.yaml": 1645035964.9551077,
"train_yamls\\train_oriae_modulation.yaml": 1644897891.2576747,
"train_distillation_mgpu.py": 1645553439.948758,
"train_distillation_mgpu.py": 1645554603.908166,
"components\\DeConv.py": 1645263338.9001615,
"components\\DeConv_Depthwise_ECA.py": 1645265769.1076133,
"components\\ECA.py": 1614848426.9604986,
"components\\ECA_Depthwise_Conv.py": 1645265754.2023985,
"components\\Generator_eca_depthwise.py": 1645266338.9750814,
"losses\\KA.py": 1645546325.331715,
"train_scripts\\trainer_distillation_mgpu.py": 1645553282.8011973,
"train_yamls\\train_distillation.yaml": 1645553621.3982964
"train_scripts\\trainer_distillation_mgpu.py": 1645601961.4139585,
"train_yamls\\train_distillation.yaml": 1645600099.540936,
"annotation.py": 1645931038.719335,
"components\\DeConv_ECA_Invo.py": 1645869347.379311,
"components\\DeConv_Invobn.py": 1645862876.018001,
"components\\Generator_Invobn_config.py": 1645929418.6924264,
"components\\Generator_Invobn_config1.py": 1645862695.8743145,
"components\\misc\\Involution_BN.py": 1645867197.3984175,
"components\\misc\\Involution_ECA.py": 1645869012.4927464,
"train_yamls\\train_Invobn_config.yaml": 1645934993.5420852
}
+132
View File
@@ -0,0 +1,132 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: annotation.py
# Created Date: Saturday February 26th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Sunday, 27th February 2022 11:03:58 am
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import cv2
import os
import glob
import json
import argparse
keytable={
"left":2424832,
"right":2555904,
"up":2490368,
"down":2621440,
"esc":27,
"space":32
}
def str2bool(v):
return v.lower() in ('true')
def getParameters():
parser = argparse.ArgumentParser()
# general
parser.add_argument('--image_dir', type=str, default="G:\\VGGFace2-HQ\\VGGface2_None_norm_512_true_bygfpgan")
parser.add_argument('--savetxt', type=str, default="./check_list.txt")
parser.add_argument('--winWidth', type=int, default=512)
parser.add_argument('--winHeight', type=int, default=512)
return parser.parse_args()
if __name__ == "__main__":
config = getParameters()
savePath = config.savetxt
log_txt = "./breakpoint.json"
temp_path = os.path.join(config.image_dir,'*/')
pathes = glob.glob(temp_path)
dataset = []
for dir_item in pathes:
join_path = glob.glob(os.path.join(dir_item,'*.jpg'))
print("processing %s"%dir_item,end='\r')
temp_list = []
for item in join_path:
temp_list.append(item)
dataset.append(temp_list)
cv2.namedWindow("Annatation",0)
cv2.resizeWindow("Annatation", config.winWidth, config.winHeight)
font = cv2.FONT_HERSHEY_SIMPLEX
try:
with open(log_txt,'r') as cf:
breakpoint_json = cf.read()
breakpoint_json = json.loads(breakpoint_json)
if isinstance(breakpoint_json,str):
breakpoint_json = json.loads(breakpoint_json)
except:
breakpoint_json = {"breakpoint":[0,0]}
save_point = breakpoint_json["breakpoint"]
total_dir = len(dataset)
dir_pointer = save_point[0]
indir_pointer = save_point[1]
while(1):
try:
img = cv2.imread(os.path.join(config.image_dir,dataset[dir_pointer][indir_pointer]))
imgshow = cv2.putText(img, "[%d]/[%d]-[%d]/[%d]"%(dir_pointer+1,total_dir,
indir_pointer+1,len(dataset[dir_pointer])), (0, 30), font, 0.4, (255, 255, 255), 1)
imgshow = cv2.putText(imgshow, "%s"%(dataset[dir_pointer][indir_pointer][-19:]), (0, 60), font, 0.4, (255, 255, 255), 1)
cv2.imshow('Annatation',imgshow)
waitkey_num = cv2.waitKeyEx(20)
# if waitkey_num != -1:
# print(waitkey_num)
if waitkey_num == keytable["left"]:
# print("Left")
indir_pointer -= 1
if indir_pointer<0:
indir_pointer = 0
if waitkey_num == keytable["right"]:
# print("Right")
indir_pointer += 1
if indir_pointer>= len(dataset[dir_pointer]):
indir_pointer = len(dataset[dir_pointer])-1
if waitkey_num == keytable["up"]:
# print("Left")
dir_pointer -= 1
indir_pointer = 0
if dir_pointer<0:
dir_pointer = 0
if waitkey_num == keytable["down"]:
# print("Right")
dir_pointer += 1
indir_pointer = 0
if dir_pointer>= total_dir:
dir_pointer = total_dir-1
if waitkey_num == keytable["space"]:
image_name_cur = dataset[dir_pointer][indir_pointer][-19:]
print("Save image name %s"%image_name_cur)
with open(savePath,'a+') as logf:
logf.writelines("%s\n"%(image_name_cur))
if waitkey_num == keytable["esc"]:
breakpoint_json = {"breakpoint":[dir_pointer,indir_pointer]}
with open(log_txt, 'w') as cf:
configjson = json.dumps(breakpoint_json, indent=4)
cf.writelines(configjson)
break
except KeyboardInterrupt:
breakpoint_json = {"breakpoint":[dir_pointer,indir_pointer]}
with open(log_txt, 'w') as cf:
configjson = json.dumps(breakpoint_json, indent=4)
cf.writelines(configjson)
break
cv2.destroyAllWindows()
+6
View File
@@ -0,0 +1,6 @@
{
"breakpoint": [
1053,
0
]
}
+871
View File
@@ -0,0 +1,871 @@
n001001\0015_03.jpg
n001001\0043_02.jpg
n001001\0053_01.jpg
n001001\0094_01.jpg
n001001\0171_02.jpg
n001001\0233_02.jpg
n001001\0278_02.jpg
n001001\0281_03.jpg
n001001\0335_02.jpg
n001001\0356_05.jpg
n001001\0421_01.jpg
n001001\0455_02.jpg
n001002\0012_02.jpg
n001002\0091_02.jpg
n001002\0094_01.jpg
n001002\0106_02.jpg
n001002\0142_02.jpg
n001002\0263_01.jpg
n001002\0280_01.jpg
n001002\0357_01.jpg
n001002\0398_04.jpg
n001003\0007_02.jpg
n001003\0029_01.jpg
n001003\0124_01.jpg
n001003\0139_03.jpg
n001003\0145_03.jpg
n001003\0174_01.jpg
n001003\0230_02.jpg
n001003\0327_02.jpg
n001003\0329_01.jpg
n001003\0338_01.jpg
n001004\0168_01.jpg
n001005\0003_01.jpg
n001005\0018_03.jpg
n001005\0060_01.jpg
n001005\0122_02.jpg
n001005\0131_03.jpg
n001005\0144_01.jpg
n001005\0199_01.jpg
n001005\0282_01.jpg
n001005\0311_01.jpg
n001005\0351_02.jpg
n001006\0162_02.jpg
n001006\0168_01.jpg
n001006\0174_02.jpg
n001006\0175_01.jpg
n001006\0421_01.jpg
n001006\0425_01.jpg
n001006\0472_01.jpg
n001006\0574_01.jpg
n001007\0038_01.jpg
n001007\0063_01.jpg
n001007\0158_02.jpg
n001007\0189_01.jpg
n001007\0248_01.jpg
n001007\0330_02.jpg
n001007\0332_01.jpg
n001007\0343_01.jpg
n001007\0374_02.jpg
n001008\0030_02.jpg
n001008\0031_02.jpg
n001008\0038_02.jpg
n001008\0055_01.jpg
n001008\0062_02.jpg
n001008\0123_01.jpg
n001008\0120_01.jpg
n001008\0138_01.jpg
n001008\0435_04.jpg
n001009\0036_01.jpg
n001009\0064_01.jpg
n001009\0159_01.jpg
n001009\0261_02.jpg
n001009\0395_01.jpg
n001010\0076_03.jpg
n001010\0093_01.jpg
n001010\0151_01.jpg
n001010\0152_02.jpg
n001010\0509_01.jpg
n001010\0509_03.jpg
n001010\0511_01.jpg
n001011\0037_01.jpg
n001011\0144_01.jpg
n001011\0199_01.jpg
n001011\0273_01.jpg
n001011\0275_01.jpg
n001012\0096_01.jpg
n001012\0383_02.jpg
n001014\0038_02.jpg
n001015\0022_02.jpg
n001015\0037_01.jpg
n001015\0047_02.jpg
n001015\0063_03.jpg
n001015\0097_05.jpg
n001015\0213_03.jpg
n001015\0225_02.jpg
n001015\0278_01.jpg
n001015\0304_01.jpg
n001015\0305_01.jpg
n001015\0310_02.jpg
n001015\0314_01.jpg
n001015\0322_02.jpg
n001015\0359_01.jpg
n001015\0356_01.jpg
n001015\0394_01.jpg
n001015\0409_01.jpg
n001015\0448_01.jpg
n001015\0477_01.jpg
n001015\0515_01.jpg
n001015\0556_01.jpg
n001016\0151_01.jpg
n001016\0153_02.jpg
n001016\0163_02.jpg
n001016\0172_02.jpg
n001016\0323_01.jpg
n001016\0380_01.jpg
n001017\0013_01.jpg
n001017\0133_01.jpg
n001017\0253_01.jpg
n001017\0297_01.jpg
n001018\0076_02.jpg
n001018\0188_01.jpg
n001018\0208_01.jpg
n001018\0310_01.jpg
n001018\0386_01.jpg
n001018\0441_01.jpg
n001018\0470_01.jpg
n001019\0083_02.jpg
n001019\0093_01.jpg
n001019\0141_03.jpg
n001019\0273_01.jpg
n001019\0291_01.jpg
n001019\0301_01.jpg
n001019\0340_02.jpg
n001019\0347_01.jpg
n001019\0444_02.jpg
n001019\0532_01.jpg
n001023\0010_01.jpg
n001023\0039_02.jpg
n001023\0041_01.jpg
n001023\0085_01.jpg
n001023\0263_01.jpg
n001024\0064_01.jpg
n001024\0122_01.jpg
n001024\0162_01.jpg
n001024\0167_01.jpg
n001024\0199_01.jpg
n001024\0260_01.jpg
n001024\0261_01.jpg
n001024\0262_01.jpg
n001024\0280_01.jpg
n001024\0364_01.jpg
n001024\0476_01.jpg
n001025\0184_02.jpg
n001025\0195_01.jpg
n001025\0203_01.jpg
n001025\0226_01.jpg
n001025\0281_02.jpg
n001025\0404_02.jpg
n001025\0441_02.jpg
n001025\0446_01.jpg
n001026\0030_01.jpg
n001026\0100_01.jpg
n001026\0266_01.jpg
n001026\0349_01.jpg
n001027\0046_01.jpg
n001027\0135_01.jpg
n001027\0146_02.jpg
n001027\0153_01.jpg
n001027\0238_01.jpg
n001027\0265_01.jpg
n001027\0302_01.jpg
n001027\0304_01.jpg
n001027\0339_01.jpg
n001027\0363_01.jpg
n001028\0036_01.jpg
n001028\0072_01.jpg
n001028\0177_01.jpg
n001028\0178_02.jpg
n001028\0219_01.jpg
n001028\0227_01.jpg
n001028\0237_02.jpg
n001028\0287_02.jpg
n001028\0457_05.jpg
n001028\0496_01.jpg
n001028\0553_01.jpg
n001029\0034_03.jpg
n001029\0181_01.jpg
n001030\0014_02.jpg
n001030\0123_02.jpg
n001030\0157_01.jpg
n001030\0162_02.jpg
n001030\0208_02.jpg
n001030\0312_01.jpg
n001031\0018_01.jpg
n001031\0046_01.jpg
n001031\0144_02.jpg
n001031\0183_01.jpg
n001031\0200_01.jpg
n001031\0221_03.jpg
n001031\0278_02.jpg
n001031\0288_02.jpg
n001031\0399_01.jpg
n001032\0086_01.jpg
n001032\0112_02.jpg
n001032\0206_01.jpg
n001032\0326_01.jpg
n001033\0016_02.jpg
n001033\0059_03.jpg
n001033\0077_04.jpg
n001033\0110_01.jpg
n001033\0128_02.jpg
n001033\0138_01.jpg
n001033\0173_01.jpg
n001033\0194_01.jpg
n001033\0305_01.jpg
n001033\0328_02.jpg
n001033\0329_01.jpg
n001033\0336_01.jpg
n001033\0365_01.jpg
n001033\0372_01.jpg
n001033\0393_02.jpg
n001033\0431_01.jpg
n001033\0435_02.jpg
n001034\0075_02.jpg
n001034\0085_01.jpg
n001034\0090_01.jpg
n001034\0090_02.jpg
n001034\0188_01.jpg
n001034\0214_01.jpg
n001034\0220_01.jpg
n001035\0141_03.jpg
n001035\0153_01.jpg
n001036\0007_02.jpg
n001036\0034_02.jpg
n001036\0032_02.jpg
n001036\0117_02.jpg
n001036\0125_03.jpg
n001036\0132_01.jpg
n001036\0148_01.jpg
n001036\0206_01.jpg
n001036\0266_01.jpg
n001036\0269_01.jpg
n001036\0338_04.jpg
n001036\0359_02.jpg
n001036\0381_01.jpg
n001040\0035_02.jpg
n001040\0075_04.jpg
n001040\0188_01.jpg
n001040\0235_01.jpg
n001040\0329_01.jpg
n001040\0373_02.jpg
n001040\0378_01.jpg
n001040\0381_01.jpg
n001040\0391_02.jpg
n001040\0394_01.jpg
n001041\0073_02.jpg
n001041\0310_02.jpg
n001042\0060_01.jpg
n001042\0122_01.jpg
n001042\0152_01.jpg
n001042\0374_01.jpg
n001042\0379_01.jpg
n001042\0380_01.jpg
n001042\0391_02.jpg
n001042\0397_01.jpg
n001042\0399_01.jpg
n001042\0400_01.jpg
n001042\0403_01.jpg
n001042\0404_01.jpg
n001042\0496_01.jpg
n001044\0020_04.jpg
n001044\0051_05.jpg
n001044\0085_01.jpg
n001044\0131_01.jpg
n001044\0259_02.jpg
n001044\0326_01.jpg
n001044\0326_02.jpg
n001044\0373_02.jpg
n001044\0433_01.jpg
n001044\0445_01.jpg
n001045\0085_02.jpg
n001045\0136_01.jpg
n001045\0230_01.jpg
n001045\0239_01.jpg
n001045\0239_04.jpg
n001046\0099_02.jpg
n001046\0108_01.jpg
n001047\0119_02.jpg
n001047\0122_01.jpg
n001047\0136_01.jpg
n001047\0253_01.jpg
n001047\0254_02.jpg
n001047\0277_01.jpg
n001047\0305_01.jpg
n001047\0392_01.jpg
n001048\0156_02.jpg
n001048\0228_02.jpg
n001048\0230_01.jpg
n001048\0366_02.jpg
n001049\0009_03.jpg
n001049\0041_01.jpg
n001049\0077_01.jpg
n001049\0118_02.jpg
n001049\0149_01.jpg
n001049\0186_02.jpg
n001050\0016_01.jpg
n001050\0061_01.jpg
n001050\0059_01.jpg
n001050\0076_02.jpg
n001050\0077_01.jpg
n001050\0087_01.jpg
n001050\0099_01.jpg
n001050\0108_01.jpg
n001050\0115_02.jpg
n001050\0117_01.jpg
n001050\0118_01.jpg
n001050\0125_01.jpg
n001050\0134_01.jpg
n001050\0164_01.jpg
n001050\0201_01.jpg
n001050\0202_03.jpg
n001050\0207_01.jpg
n001050\0225_02.jpg
n001050\0228_01.jpg
n001050\0238_01.jpg
n001050\0247_01.jpg
n001050\0252_01.jpg
n001050\0258_02.jpg
n001050\0354_01.jpg
n001050\0372_01.jpg
n001050\0373_01.jpg
n001050\0384_01.jpg
n001050\0387_01.jpg
n001050\0395_01.jpg
n001051\0043_02.jpg
n001051\0097_01.jpg
n001051\0239_01.jpg
n001051\0271_01.jpg
n001052\0018_03.jpg
n001052\0150_02.jpg
n001052\0179_02.jpg
n001052\0208_02.jpg
n001052\0228_01.jpg
n001052\0263_01.jpg
n001052\0354_02.jpg
n001052\0354_01.jpg
n001052\0376_01.jpg
n001052\0387_01.jpg
n001052\0407_01.jpg
n001052\0415_02.jpg
n001052\0418_01.jpg
n001052\0511_01.jpg
n001052\0524_01.jpg
n001053\0125_01.jpg
n001053\0121_01.jpg
n001053\0190_01.jpg
n001053\0255_01.jpg
n001053\0511_03.jpg
n001054\0080_03.jpg
n001054\0140_01.jpg
n001054\0159_01.jpg
n001054\0579_01.jpg
n001055\0025_01.jpg
n001055\0061_01.jpg
n001055\0072_01.jpg
n001055\0140_01.jpg
n001055\0142_01.jpg
n001055\0627_01.jpg
n001056\0357_01.jpg
n001056\0385_01.jpg
n001056\0393_02.jpg
n001056\0402_01.jpg
n001057\0004_02.jpg
n001057\0088_02.jpg
n001057\0091_02.jpg
n001057\0108_01.jpg
n001057\0115_01.jpg
n001057\0152_01.jpg
n001057\0228_03.jpg
n001057\0242_02.jpg
n001057\0260_01.jpg
n001057\0282_02.jpg
n001057\0289_01.jpg
n001057\0291_02.jpg
n001057\0329_02.jpg
n001057\0336_01.jpg
n001057\0336_02.jpg
n001057\0346_01.jpg
n001057\0359_01.jpg
n001057\0375_01.jpg
n001057\0414_02.jpg
n001057\0415_02.jpg
n001057\0416_04.jpg
n001057\0438_01.jpg
n001057\0493_01.jpg
n001057\0501_02.jpg
n001043\0017_01.jpg
n001043\0080_01.jpg
n001043\0083_01.jpg
n001043\0087_01.jpg
n001038\0002_01.jpg
n001038\0019_03.jpg
n001038\0035_01.jpg
n001038\0050_01.jpg
n001038\0060_02.jpg
n001038\0063_01.jpg
n001038\0077_01.jpg
n001038\0090_02.jpg
n001038\0120_04.jpg
n001038\0124_01.jpg
n001038\0128_01.jpg
n001038\0133_01.jpg
n001038\0140_01.jpg
n001038\0149_01.jpg
n001038\0178_01.jpg
n001038\0196_01.jpg
n001038\0198_01.jpg
n001038\0206_02.jpg
n001038\0210_01.jpg
n001038\0233_01.jpg
n001038\0235_01.jpg
n001038\0235_04.jpg
n001038\0282_01.jpg
n001038\0286_01.jpg
n001038\0335_02.jpg
n001038\0335_03.jpg
n001038\0395_01.jpg
n001038\0426_01.jpg
n001038\0458_01.jpg
n001038\0484_02.jpg
n001038\0512_02.jpg
n001037\0087_01.jpg
n001037\0325_01.jpg
n001037\0339_01.jpg
n001037\0366_01.jpg
n001058\0081_01.jpg
n001058\0256_02.jpg
n001058\0282_01.jpg
n001060\0118_02.jpg
n001060\0245_01.jpg
n001060\0249_02.jpg
n001060\0259_02.jpg
n001060\0334_02.jpg
n001060\0355_02.jpg
n001061\0129_01.jpg
n001061\0330_02.jpg
n001061\0342_01.jpg
n001061\0350_01.jpg
n001062\0222_01.jpg
n001063\0040_01.jpg
n001063\0049_01.jpg
n001063\0152_01.jpg
n001063\0155_01.jpg
n001063\0158_01.jpg
n001063\0227_03.jpg
n001063\0424_02.jpg
n001063\0429_01.jpg
n001063\0432_01.jpg
n001063\0442_01.jpg
n001064\0234_01.jpg
n001064\0234_02.jpg
n001064\0276_01.jpg
n001064\0371_01.jpg
n001064\0512_01.jpg
n001065\0065_01.jpg
n001065\0066_01.jpg
n001065\0068_02.jpg
n001065\0070_01.jpg
n001065\0107_01.jpg
n001065\0108_01.jpg
n001065\0125_01.jpg
n001065\0126_02.jpg
n001065\0153_02.jpg
n001065\0215_01.jpg
n001065\0227_01.jpg
n001065\0296_01.jpg
n001065\0326_01.jpg
n001065\0366_01.jpg
n001065\0367_01.jpg
n001065\0379_01.jpg
n001066\0055_01.jpg
n001066\0087_01.jpg
n001066\0122_02.jpg
n001066\0123_01.jpg
n001066\0154_01.jpg
n001066\0174_01.jpg
n001066\0214_01.jpg
n001066\0250_01.jpg
n001066\0300_04.jpg
n001066\0309_01.jpg
n001066\0360_02.jpg
n001066\0388_01.jpg
n001066\0401_01.jpg
n001066\0419_01.jpg
n001066\0504_01.jpg
n001066\0513_01.jpg
n001066\0517_02.jpg
n001067\0093_01.jpg
n001067\0127_02.jpg
n001068\0043_01.jpg
n001068\0062_01.jpg
n001068\0087_01.jpg
n001068\0117_01.jpg
n001068\0174_01.jpg
n001068\0182_03.jpg
n001068\0202_01.jpg
n001068\0351_01.jpg
n001068\0399_05.jpg
n001068\0514_02.jpg
n001069\0202_02.jpg
n001069\0279_01.jpg
n001071\0156_01.jpg
n001071\0317_02.jpg
n001071\0421_01.jpg
n001071\0426_02.jpg
n001072\0044_01.jpg
n001072\0057_02.jpg
n001072\0119_01.jpg
n001072\0138_01.jpg
n001072\0140_01.jpg
n001072\0148_01.jpg
n001072\0184_01.jpg
n001072\0221_01.jpg
n001072\0239_02.jpg
n001072\0250_01.jpg
n001072\0270_01.jpg
n001072\0276_01.jpg
n001072\0293_02.jpg
n001072\0305_01.jpg
n001072\0310_01.jpg
n001072\0348_01.jpg
n001072\0375_01.jpg
n001073\0174_01.jpg
n001073\0175_01.jpg
n001073\0210_01.jpg
n001073\0238_02.jpg
n001073\0261_02.jpg
n001073\0310_01.jpg
n001074\0038_01.jpg
n001074\0046_01.jpg
n001074\0116_01.jpg
n001074\0130_01.jpg
n001074\0176_05.jpg
n001074\0189_02.jpg
n001074\0201_01.jpg
n001074\0204_01.jpg
n001074\0208_01.jpg
n001075\0163_02.jpg
n001075\0201_01.jpg
n001075\0221_01.jpg
n001075\0312_01.jpg
n001076\0180_02.jpg
n001076\0222_01.jpg
n001076\0234_02.jpg
n001076\0242_01.jpg
n001076\0265_01.jpg
n001076\0285_01.jpg
n001076\0292_01.jpg
n001077\0094_01.jpg
n001077\0244_01.jpg
n001077\0252_02.jpg
n001077\0254_02.jpg
n001077\0266_01.jpg
n001077\0267_02.jpg
n001077\0346_01.jpg
n001077\0389_02.jpg
n001077\0400_01.jpg
n001078\0030_01.jpg
n001078\0089_01.jpg
n001078\0127_01.jpg
n001078\0222_02.jpg
n001078\0231_01.jpg
n001078\0231_02.jpg
n001078\0349_01.jpg
n001078\0384_02.jpg
n001079\0005_01.jpg
n001079\0072_01.jpg
n001080\0001_01.jpg
n001080\0252_03.jpg
n001080\0268_01.jpg
n001080\0325_01.jpg
n001081\0197_02.jpg
n001081\0204_02.jpg
n001081\0214_01.jpg
n001081\0246_01.jpg
n001082\0379_01.jpg
n001082\0335_01.jpg
n001082\0420_02.jpg
n001083\0092_01.jpg
n001083\0117_01.jpg
n001083\0119_01.jpg
n001083\0141_01.jpg
n001083\0159_01.jpg
n001083\0177_02.jpg
n001083\0202_01.jpg
n001083\0223_01.jpg
n001083\0223_03.jpg
n001083\0378_02.jpg
n001084\0009_01.jpg
n001084\0031_04.jpg
n001084\0085_02.jpg
n001084\0081_01.jpg
n001084\0088_01.jpg
n001084\0090_02.jpg
n001084\0099_02.jpg
n001084\0217_02.jpg
n001084\0255_02.jpg
n001084\0267_01.jpg
n001084\0279_02.jpg
n001084\0295_01.jpg
n001084\0511_02.jpg
n001084\0544_01.jpg
n001084\0551_01.jpg
n001084\0566_02.jpg
n001085\0017_01.jpg
n001085\0056_02.jpg
n001085\0076_01.jpg
n001085\0191_01.jpg
n001085\0193_01.jpg
n001085\0206_02.jpg
n001085\0240_01.jpg
n001085\0259_01.jpg
n001086\0061_02.jpg
n001086\0140_01.jpg
n001086\0140_02.jpg
n001086\0168_02.jpg
n001086\0192_01.jpg
n001086\0230_02.jpg
n001086\0260_01.jpg
n001086\0279_01.jpg
n001087\0303_01.jpg
n001088\0043_02.jpg
n001088\0139_01.jpg
n001088\0169_01.jpg
n001088\0169_02.jpg
n001088\0253_02.jpg
n001088\0255_01.jpg
n001088\0329_01.jpg
n001088\0346_01.jpg
n001088\0347_02.jpg
n001088\0360_01.jpg
n001089\0002_02.jpg
n001089\0104_01.jpg
n001089\0319_01.jpg
n001089\0322_01.jpg
n001090\0036_01.jpg
n001090\0108_01.jpg
n001090\0299_01.jpg
n001090\0319_01.jpg
n001090\0388_01.jpg
n001090\0391_01.jpg
n001090\0396_01.jpg
n001090\0399_01.jpg
n001090\0488_01.jpg
n001091\0088_02.jpg
n001091\0129_02.jpg
n001091\0177_03.jpg
n001091\0177_04.jpg
n001091\0266_02.jpg
n001091\0297_01.jpg
n001091\0316_03.jpg
n001091\0514_02.jpg
n001091\0526_01.jpg
n001091\0538_02.jpg
n001091\0552_01.jpg
n001091\0552_02.jpg
n001091\0554_02.jpg
n001092\0078_01.jpg
n001092\0079_01.jpg
n001092\0094_01.jpg
n001092\0170_01.jpg
n001092\0179_01.jpg
n001092\0192_01.jpg
n001092\0226_01.jpg
n001092\0228_01.jpg
n001092\0237_03.jpg
n001092\0275_01.jpg
n001092\0294_02.jpg
n001092\0301_01.jpg
n001093\0029_01.jpg
n001093\0168_02.jpg
n001093\0202_01.jpg
n001093\0250_01.jpg
n001093\0271_01.jpg
n001093\0287_01.jpg
n001093\0313_01.jpg
n001093\0359_01.jpg
n001093\0391_02.jpg
n001093\0402_01.jpg
n001093\0425_01.jpg
n001094\0187_01.jpg
n001094\0197_01.jpg
n001094\0206_01.jpg
n001094\0218_01.jpg
n001094\0254_01.jpg
n001094\0263_01.jpg
n001094\0311_03.jpg
n001094\0339_01.jpg
n001094\0340_01.jpg
n001094\0417_01.jpg
n001094\0447_02.jpg
n001094\0453_02.jpg
n001094\0479_01.jpg
n001094\0481_01.jpg
n001094\0494_01.jpg
n001095\0011_01.jpg
n001095\0127_01.jpg
n001095\0138_01.jpg
n001095\0369_01.jpg
n001095\0370_01.jpg
n001095\0379_02.jpg
n001095\0449_01.jpg
n001096\0082_02.jpg
n001096\0110_03.jpg
n001096\0150_01.jpg
n001096\0226_02.jpg
n001096\0274_02.jpg
n001096\0275_03.jpg
n001096\0278_01.jpg
n001096\0284_02.jpg
n001096\0298_01.jpg
n001096\0303_02.jpg
n001096\0318_02.jpg
n001096\0320_01.jpg
n001096\0332_03.jpg
n001096\0336_01.jpg
n001096\0340_02.jpg
n001096\0410_02.jpg
n001097\0073_02.jpg
n001097\0091_01.jpg
n001097\0091_04.jpg
n001097\0133_02.jpg
n001097\0136_03.jpg
n001097\0155_04.jpg
n001097\0197_01.jpg
n001097\0198_01.jpg
n001097\0241_02.jpg
n001097\0275_02.jpg
n001098\0107_01.jpg
n001098\0148_01.jpg
n001098\0170_02.jpg
n001098\0171_01.jpg
n001098\0212_02.jpg
n001098\0219_01.jpg
n001098\0244_01.jpg
n001098\0490_01.jpg
n001098\0502_01.jpg
n001099\0074_02.jpg
n001099\0078_01.jpg
n001099\0140_01.jpg
n001099\0206_01.jpg
n001099\0212_01.jpg
n001099\0216_01.jpg
n001099\0221_02.jpg
n001099\0244_03.jpg
n001100\0045_01.jpg
n001100\0057_01.jpg
n001100\0062_02.jpg
n001100\0063_01.jpg
n001100\0089_01.jpg
n001100\0111_02.jpg
n001100\0127_01.jpg
n001100\0199_01.jpg
n001100\0205_02.jpg
n001100\0206_02.jpg
n001100\0210_04.jpg
n001100\0248_01.jpg
n001100\0250_02.jpg
n001100\0268_01.jpg
n001100\0269_01.jpg
n001100\0270_01.jpg
n001100\0305_02.jpg
n001100\0319_04.jpg
n001100\0371_02.jpg
n001100\0388_01.jpg
n001100\0390_02.jpg
n001100\0395_01.jpg
n001100\0396_01.jpg
n001100\0409_01.jpg
n001100\0411_02.jpg
n001100\0423_02.jpg
n001101\0027_01.jpg
n001101\0034_03.jpg
n001101\0146_01.jpg
n001101\0172_01.jpg
n001101\0221_01.jpg
n001101\0235_01.jpg
n001101\0258_01.jpg
n001101\0271_01.jpg
n001101\0275_01.jpg
n001101\0284_01.jpg
n001102\0048_01.jpg
n001102\0050_02.jpg
n001102\0092_01.jpg
n001102\0201_01.jpg
n001102\0250_01.jpg
n001103\0020_01.jpg
n001103\0111_01.jpg
n001103\0124_02.jpg
n001103\0130_01.jpg
n001103\0186_01.jpg
n001103\0188_01.jpg
n001103\0190_02.jpg
n001103\0201_01.jpg
n001103\0217_02.jpg
n001103\0225_01.jpg
n001103\0242_01.jpg
n001104\0105_02.jpg
n001104\0106_02.jpg
n001104\0181_02.jpg
n001104\0255_01.jpg
n001104\0255_02.jpg
n001104\0272_01.jpg
n001104\0316_02.jpg
n001104\0353_01.jpg
n001105\0052_02.jpg
n001105\0092_01.jpg
n001105\0213_01.jpg
n001105\0214_01.jpg
n001105\0266_01.jpg
n001105\0303_02.jpg
n001105\0316_01.jpg
n001105\0323_01.jpg
n001105\0351_01.jpg
n001105\0377_01.jpg
n001105\0425_01.jpg
n001105\0434_02.jpg
n001105\0432_01.jpg
n001106\0041_01.jpg
n001106\0079_01.jpg
n001106\0101_01.jpg
n001106\0171_01.jpg
n001106\0189_01.jpg
n001106\0244_01.jpg
n001106\0301_01.jpg
n001106\0362_01.jpg
n001106\0411_01.jpg
n001106\0428_02.jpg
n001106\0446_02.jpg
n001108\0032_01.jpg
n001108\0057_01.jpg
n001108\0073_01.jpg
n001108\0193_01.jpg
n001108\0213_02.jpg
n001108\0288_01.jpg
n001108\0357_01.jpg
n001108\0444_01.jpg
n001109\0195_01.jpg
n001109\0197_01.jpg
n001109\0198_01.jpg
n001109\0204_01.jpg
n001109\0205_01.jpg
n001109\0209_01.jpg
n001109\0221_01.jpg
n001109\0324_01.jpg
n001109\0396_01.jpg
n001109\0403_01.jpg
n001110\0220_02.jpg
n001111\0060_01.jpg
n001111\0190_01.jpg
n001111\0193_01.jpg
n001111\0223_01.jpg
n001111\0242_01.jpg
n001111\0280_01.jpg
n001111\0276_02.jpg
n001111\0377_01.jpg
n001111\0393_02.jpg
n001111\0426_02.jpg
n001112\0164_01.jpg
n001112\0186_02.jpg
n001112\0234_01.jpg
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: DeConv copy.py
# Created Date: Tuesday July 20th 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Saturday, 26th February 2022 5:55:46 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
from torch import nn
from components.misc.Involution_ECA import involution
class DeConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size = 3, upsampl_scale = 2, padding="zero", up_mode = "bilinear"):
super().__init__()
if up_mode.lower() == "bilinear":
self.upsampling = nn.UpsamplingBilinear2d(scale_factor=upsampl_scale)
elif up_mode.lower() == "nearest":
self.upsampling = nn.UpsamplingNearest2d(scale_factor=upsampl_scale)
# self.upsampling = nn.UpsamplingBilinear2d(scale_factor=upsampl_scale)
padding_size = int((kernel_size -1)/2)
self.conv1x1 = nn.Conv2d(in_channels = in_channels, out_channels = out_channels, kernel_size= 1)
# self.same_padding = nn.ReflectionPad2d(padding_size)
if padding.lower() == "reflect":
self.conv = involution(out_channels,kernel_size,1)
# self.conv = nn.Sequential(
# nn.ReflectionPad2d(padding_size),
# nn.Conv2d(in_channels = in_channels, out_channels = out_channels, kernel_size= kernel_size, bias= False))
# for layer in self.conv:
# if isinstance(layer,nn.Conv2d):
# nn.init.xavier_uniform_(layer.weight)
elif padding.lower() == "zero":
self.conv = involution(out_channels,kernel_size,1)
# nn.init.xavier_uniform_(self.conv.weight)
# self.__weights_init__()
# def __weights_init__(self):
# nn.init.xavier_uniform_(self.conv.weight)
def forward(self, input):
h = self.conv1x1(input)
h = self.upsampling(h)
h = self.conv(h)
return h
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: DeConv copy.py
# Created Date: Tuesday July 20th 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Saturday, 26th February 2022 4:07:55 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
from torch import nn
from components.misc.Involution_BN import involution
class DeConv(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size = 3, upsampl_scale = 2, padding="zero", up_mode = "bilinear"):
super().__init__()
if up_mode.lower() == "bilinear":
self.upsampling = nn.UpsamplingBilinear2d(scale_factor=upsampl_scale)
elif up_mode.lower() == "nearest":
self.upsampling = nn.UpsamplingNearest2d(scale_factor=upsampl_scale)
# self.upsampling = nn.UpsamplingBilinear2d(scale_factor=upsampl_scale)
padding_size = int((kernel_size -1)/2)
self.conv1x1 = nn.Conv2d(in_channels = in_channels, out_channels = out_channels, kernel_size= 1)
# self.same_padding = nn.ReflectionPad2d(padding_size)
if padding.lower() == "reflect":
self.conv = involution(out_channels,kernel_size,1)
# self.conv = nn.Sequential(
# nn.ReflectionPad2d(padding_size),
# nn.Conv2d(in_channels = in_channels, out_channels = out_channels, kernel_size= kernel_size, bias= False))
# for layer in self.conv:
# if isinstance(layer,nn.Conv2d):
# nn.init.xavier_uniform_(layer.weight)
elif padding.lower() == "zero":
self.conv = involution(out_channels,kernel_size,1)
# nn.init.xavier_uniform_(self.conv.weight)
# self.__weights_init__()
# def __weights_init__(self):
# nn.init.xavier_uniform_(self.conv.weight)
def forward(self, input):
h = self.conv1x1(input)
h = self.upsampling(h)
h = self.conv(h)
return h
+273
View File
@@ -0,0 +1,273 @@
#!/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: Sunday, 27th February 2022 10:36:58 am
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import torch
from torch import nn
# from components.DeConv_Invo import DeConv
class Demodule(nn.Module):
def __init__(self, epsilon=1e-8):
"""
@notice: avoid in-place ops.
https://discuss.pytorch.org/t/encounter-the-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/836/3
"""
super(Demodule, self).__init__()
self.epsilon = epsilon
def forward(self, x):
tmp = torch.mul(x, x) # or x ** 2
tmp = torch.rsqrt(torch.mean(tmp, (2, 3), True) + self.epsilon)
return x * tmp
class ApplyStyle(nn.Module):
"""
@ref: https://github.com/lernapparat/lernapparat/blob/master/style_gan/pytorch_style_gan.ipynb
"""
def __init__(self, latent_size, channels):
super(ApplyStyle, self).__init__()
self.linear = nn.Linear(latent_size, channels * 2)
def forward(self, x, latent):
style = self.linear(latent) # style => [batch_size, n_channels*2]
shape = [-1, 2, x.size(1), 1, 1]
style = style.view(shape) # [batch_size, 2, n_channels, ...]
#x = x * (style[:, 0] + 1.) + style[:, 1]
x = x * (style[:, 0] * 1 + 1.) + style[:, 1] * 1
return x
class Modulation(nn.Module):
def __init__(self, latent_size, channels):
super(Modulation, self).__init__()
self.linear = nn.Linear(latent_size, channels)
def forward(self, x, latent):
style = self.linear(latent) # style => [batch_size, n_channels*2]
shape = [-1, x.size(1), 1, 1]
style = style.view(shape) # [batch_size, 2, n_channels, ...]
#x = x * (style[:, 0] + 1.) + style[:, 1]
x = x * style
return x
class ResnetBlock_Modulation(nn.Module):
def __init__(self, dim, latent_size, padding_type, activation=nn.ReLU(True),res_mode="depthwise"):
super(ResnetBlock_Modulation, self).__init__()
p = 0
conv1 = []
if padding_type == 'reflect':
conv1 += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv1 += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
if res_mode.lower() == "conv":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding = p), Demodule()]
elif res_mode.lower() == "depthwise":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "depthwise_eca":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "invo":
from components.misc.Involution_BN import involution
conv1 += [involution(dim,3, 1, 0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "eca_invo":
from components.misc.Involution_ECA import involution
conv1 += [involution(dim, 3, 1, 0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
self.conv1 = nn.Sequential(*conv1)
self.style1 = Modulation(latent_size, dim)
self.act1 = activation
p = 0
conv2 = []
if padding_type == 'reflect':
conv2 += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv2 += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
if res_mode.lower() == "conv":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), Demodule()]
elif res_mode.lower() == "depthwise":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "depthwise_eca":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "invo":
from components.misc.Involution_BN import involution
conv2 += [involution(dim, 3, 1,0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "eca_invo":
from components.misc.Involution_ECA import involution
conv2 += [involution(dim, 3, 1, 0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
self.conv2 = nn.Sequential(*conv2)
self.style2 = Modulation(latent_size, dim)
def forward(self, x, dlatents_in_slice):
y = self.style1(x, dlatents_in_slice)
y = self.conv1(y)
y = self.act1(y)
y = self.style2(y, dlatents_in_slice)
y = self.conv2(y)
out = x + y
return out
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 = aggregator
padding_size= int((k_size -1)/2)
padding_type= 'reflect'
activation = nn.ReLU(True)
if aggregator == "invo":
from components.misc.Involution_BN import involution
from components.DeConv_Invobn import DeConv
elif aggregator == "eca_invo":
from components.misc.Involution_ECA import involution
from components.DeConv_ECA_Invo import DeConv
# 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.ReflectionPad2d(1),
nn.Conv2d(3, in_channel, kernel_size=3, padding=0, bias=False),
nn.BatchNorm2d(in_channel),
activation)
# self.first_layer = nn.Sequential(nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64), activation)
### downsample
self.down1 = nn.Sequential(
involution(in_channel,3,2),
nn.Conv2d(in_channel, in_channel*2, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*2),
activation)
self.down2 = nn.Sequential(
involution(in_channel*2,3,2),
nn.Conv2d(in_channel*2, in_channel*4, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*4),
activation)
self.down3 = nn.Sequential(
involution(in_channel*4,3,2),
nn.Conv2d(in_channel*4, in_channel*8, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*8),
activation)
self.down4 = nn.Sequential(
involution(in_channel*8,3,2),
nn.Conv2d(in_channel*8, in_channel*8, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*8),
activation)
### resnet blocks
BN = []
for i in range(res_num):
BN += [
ResnetBlock_Modulation(in_channel*8, latent_size=id_dim,
padding_type=padding_type, activation=activation, res_mode=res_mode)]
self.BottleNeck = nn.Sequential(*BN)
self.up4 = nn.Sequential(
DeConv(in_channel*8,in_channel*8,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*8),
activation
)
self.up3 = nn.Sequential(
DeConv(in_channel*8,in_channel*4,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*4),
activation
)
self.up2 = nn.Sequential(
DeConv(in_channel*4,in_channel*2,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*2),
activation
)
self.up1 = nn.Sequential(
DeConv(in_channel*2,in_channel,3,up_mode=up_mode),
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(in_channel, 3, kernel_size=3, padding=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.first_layer(img)
res = self.down1(res)
res = self.down2(res)
res = self.down3(res)
res = self.down4(res)
for i in range(len(self.BottleNeck)):
res = self.BottleNeck[i](res, id)
res = self.up4(res)
res = self.up3(res)
res = self.up2(res)
res = self.up1(res)
res = self.last_layer(res)
return res
+244
View File
@@ -0,0 +1,244 @@
#!/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: Saturday, 26th February 2022 4:04:55 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import torch
from torch import nn
from components.DeConv_Invobn import DeConv
from components.misc.Involution_BN import involution
# from components.DeConv_Invo import DeConv
class Demodule(nn.Module):
def __init__(self, epsilon=1e-8):
"""
@notice: avoid in-place ops.
https://discuss.pytorch.org/t/encounter-the-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/836/3
"""
super(Demodule, self).__init__()
self.epsilon = epsilon
def forward(self, x):
tmp = torch.mul(x, x) # or x ** 2
tmp = torch.rsqrt(torch.mean(tmp, (2, 3), True) + self.epsilon)
return x * tmp
class ApplyStyle(nn.Module):
"""
@ref: https://github.com/lernapparat/lernapparat/blob/master/style_gan/pytorch_style_gan.ipynb
"""
def __init__(self, latent_size, channels):
super(ApplyStyle, self).__init__()
self.linear = nn.Linear(latent_size, channels * 2)
def forward(self, x, latent):
style = self.linear(latent) # style => [batch_size, n_channels*2]
shape = [-1, 2, x.size(1), 1, 1]
style = style.view(shape) # [batch_size, 2, n_channels, ...]
#x = x * (style[:, 0] + 1.) + style[:, 1]
x = x * (style[:, 0] * 1 + 1.) + style[:, 1] * 1
return x
class Modulation(nn.Module):
def __init__(self, latent_size, channels):
super(Modulation, self).__init__()
self.linear = nn.Linear(latent_size, channels)
def forward(self, x, latent):
style = self.linear(latent) # style => [batch_size, n_channels*2]
shape = [-1, x.size(1), 1, 1]
style = style.view(shape) # [batch_size, 2, n_channels, ...]
#x = x * (style[:, 0] + 1.) + style[:, 1]
x = x * style
return x
class ResnetBlock_Modulation(nn.Module):
def __init__(self, dim, latent_size, padding_type, activation=nn.ReLU(True),res_mode="depthwise"):
super(ResnetBlock_Modulation, self).__init__()
p = 0
conv1 = []
if padding_type == 'reflect':
conv1 += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv1 += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
if res_mode.lower() == "conv":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding = p), Demodule()]
elif res_mode.lower() == "depthwise":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "depthwise_eca":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
self.conv1 = nn.Sequential(*conv1)
self.style1 = Modulation(latent_size, dim)
self.act1 = activation
p = 0
conv2 = []
if padding_type == 'reflect':
conv2 += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv2 += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
if res_mode.lower() == "conv":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), Demodule()]
elif res_mode.lower() == "depthwise":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "depthwise_eca":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
self.conv2 = nn.Sequential(*conv2)
self.style2 = Modulation(latent_size, dim)
def forward(self, x, dlatents_in_slice):
y = self.style1(x, dlatents_in_slice)
y = self.conv1(y)
y = self.act1(y)
y = self.style2(y, dlatents_in_slice)
y = self.conv2(y)
out = x + y
return out
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"]
res_mode = kwargs["res_mode"]
padding_size= int((k_size -1)/2)
padding_type= 'reflect'
activation = nn.ReLU(True)
# 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.ReflectionPad2d(1),
nn.Conv2d(3, in_channel, kernel_size=3, padding=0, bias=False),
nn.BatchNorm2d(in_channel),
activation)
# self.first_layer = nn.Sequential(nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64), activation)
### downsample
self.down1 = nn.Sequential(
involution(in_channel,3,2),
nn.Conv2d(in_channel, in_channel*2, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*2),
activation)
self.down2 = nn.Sequential(
involution(in_channel*2,3,2),
nn.Conv2d(in_channel*2, in_channel*4, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*4),
activation)
self.down3 = nn.Sequential(
involution(in_channel*4,3,2),
nn.Conv2d(in_channel*4, in_channel*8, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*8),
activation)
self.down4 = nn.Sequential(
involution(in_channel*8,3,2),
nn.Conv2d(in_channel*8, in_channel*16, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*16),
activation)
### resnet blocks
BN = []
for i in range(res_num):
BN += [
ResnetBlock_Modulation(in_channel*16, latent_size=id_dim,
padding_type=padding_type, activation=activation, res_mode=res_mode)]
self.BottleNeck = nn.Sequential(*BN)
self.up4 = nn.Sequential(
DeConv(in_channel*16,in_channel*8,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*8),
activation
)
self.up3 = nn.Sequential(
DeConv(in_channel*8,in_channel*4,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*4),
activation
)
self.up2 = nn.Sequential(
DeConv(in_channel*4,in_channel*2,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*2),
activation
)
self.up1 = nn.Sequential(
DeConv(in_channel*2,in_channel,3,up_mode=up_mode),
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(in_channel, 3, kernel_size=3, padding=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.first_layer(img)
res = self.down1(res)
res = self.down2(res)
res = self.down3(res)
res = self.down4(res)
for i in range(len(self.BottleNeck)):
res = self.BottleNeck[i](res, id)
res = self.up4(res)
res = self.up3(res)
res = self.up2(res)
res = self.up1(res)
res = self.last_layer(res)
return res
+274
View File
@@ -0,0 +1,274 @@
#!/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: Sunday, 27th February 2022 3:58:23 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
import torch
from torch import nn
# from components.DeConv_Invo import DeConv
class Demodule(nn.Module):
def __init__(self, epsilon=1e-8):
"""
@notice: avoid in-place ops.
https://discuss.pytorch.org/t/encounter-the-runtimeerror-one-of-the-variables-needed-for-gradient-computation-has-been-modified-by-an-inplace-operation/836/3
"""
super(Demodule, self).__init__()
self.epsilon = epsilon
def forward(self, x):
tmp = torch.mul(x, x) # or x ** 2
tmp = torch.rsqrt(torch.mean(tmp, (2, 3), True) + self.epsilon)
return x * tmp
class ApplyStyle(nn.Module):
"""
@ref: https://github.com/lernapparat/lernapparat/blob/master/style_gan/pytorch_style_gan.ipynb
"""
def __init__(self, latent_size, channels):
super(ApplyStyle, self).__init__()
self.linear = nn.Linear(latent_size, channels * 2)
def forward(self, x, latent):
style = self.linear(latent) # style => [batch_size, n_channels*2]
shape = [-1, 2, x.size(1), 1, 1]
style = style.view(shape) # [batch_size, 2, n_channels, ...]
#x = x * (style[:, 0] + 1.) + style[:, 1]
x = x * (style[:, 0] * 1 + 1.) + style[:, 1] * 1
return x
class Modulation(nn.Module):
def __init__(self, latent_size, channels):
super(Modulation, self).__init__()
self.linear = nn.Linear(latent_size, channels)
def forward(self, x, latent):
style = self.linear(latent) # style => [batch_size, n_channels*2]
shape = [-1, x.size(1), 1, 1]
style = style.view(shape) # [batch_size, 2, n_channels, ...]
#x = x * (style[:, 0] + 1.) + style[:, 1]
x = x * style
return x
class ResnetBlock_Modulation(nn.Module):
def __init__(self, dim, latent_size, padding_type, activation=nn.ReLU(True),res_mode="depthwise"):
super(ResnetBlock_Modulation, self).__init__()
p = 0
conv1 = []
if padding_type == 'reflect':
conv1 += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv1 += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
if res_mode.lower() == "conv":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding = p), Demodule()]
elif res_mode.lower() == "depthwise":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "depthwise_eca":
conv1 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "invo":
from components.misc.Involution_BN import involution
conv1 += [involution(dim,3, 1, 0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "eca_invo":
from components.misc.Involution_ECA import involution
conv1 += [involution(dim, 3, 1, 0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
self.conv1 = nn.Sequential(*conv1)
self.style1 = Modulation(latent_size, dim)
self.act1 = activation
p = 0
conv2 = []
if padding_type == 'reflect':
conv2 += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
conv2 += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError('padding [%s] is not implemented' % padding_type)
res_mode = "conv"
if res_mode.lower() == "conv":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p), Demodule()]
elif res_mode.lower() == "depthwise":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "depthwise_eca":
conv2 += [nn.Conv2d(dim, dim, kernel_size=3, padding=p,groups=dim, bias=False),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "invo":
from components.misc.Involution_BN import involution
conv2 += [involution(dim, 3, 1,0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
elif res_mode.lower() == "eca_invo":
from components.misc.Involution_ECA import involution
conv2 += [involution(dim, 3, 1, 0),
nn.Conv2d(dim, dim, kernel_size=1),
Demodule()]
self.conv2 = nn.Sequential(*conv2)
self.style2 = Modulation(latent_size, dim)
def forward(self, x, dlatents_in_slice):
y = self.style1(x, dlatents_in_slice)
y = self.conv1(y)
y = self.act1(y)
y = self.style2(y, dlatents_in_slice)
y = self.conv2(y)
out = x + y
return out
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 = aggregator
padding_size= int((k_size -1)/2)
padding_type= 'reflect'
activation = nn.ReLU(True)
if aggregator == "invo":
from components.misc.Involution_BN import involution
from components.DeConv_Invobn import DeConv
elif aggregator == "eca_invo":
from components.misc.Involution_ECA import involution
from components.DeConv_ECA_Invo import DeConv
# 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.ReflectionPad2d(1),
nn.Conv2d(3, in_channel, kernel_size=3, padding=0, bias=False),
nn.BatchNorm2d(in_channel),
activation)
# self.first_layer = nn.Sequential(nn.Conv2d(3, 64, kernel_size=3, padding=1, bias=False),
# nn.BatchNorm2d(64), activation)
### downsample
self.down1 = nn.Sequential(
involution(in_channel,3,2),
nn.Conv2d(in_channel, in_channel*2, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*2),
activation)
self.down2 = nn.Sequential(
involution(in_channel*2,3,2),
nn.Conv2d(in_channel*2, in_channel*4, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*4),
activation)
self.down3 = nn.Sequential(
involution(in_channel*4,3,2),
nn.Conv2d(in_channel*4, in_channel*8, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*8),
activation)
self.down4 = nn.Sequential(
involution(in_channel*8,3,2),
nn.Conv2d(in_channel*8, in_channel*8, kernel_size=1, bias=False),
nn.BatchNorm2d(in_channel*8),
activation)
### resnet blocks
BN = []
for i in range(res_num):
BN += [
ResnetBlock_Modulation(in_channel*8, latent_size=id_dim,
padding_type=padding_type, activation=activation, res_mode=res_mode)]
self.BottleNeck = nn.Sequential(*BN)
self.up4 = nn.Sequential(
DeConv(in_channel*8,in_channel*8,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*8),
activation
)
self.up3 = nn.Sequential(
DeConv(in_channel*8,in_channel*4,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*4),
activation
)
self.up2 = nn.Sequential(
DeConv(in_channel*4,in_channel*2,3,up_mode=up_mode),
nn.BatchNorm2d(in_channel*2),
activation
)
self.up1 = nn.Sequential(
DeConv(in_channel*2,in_channel,3,up_mode=up_mode),
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(in_channel, 3, kernel_size=3, padding=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.first_layer(img)
res = self.down1(res)
res = self.down2(res)
res = self.down3(res)
res = self.down4(res)
for i in range(len(self.BottleNeck)):
res = self.BottleNeck[i](res, id)
res = self.up4(res)
res = self.up3(res)
res = self.up2(res)
res = self.up1(res)
res = self.last_layer(res)
return res
+303
View File
@@ -0,0 +1,303 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: Involution.py
# Created Date: Tuesday July 20th 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Saturday, 26th February 2022 5:19:56 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
import torch
import torch.nn as nn
from torch.nn.modules.utils import _pair
from torch.autograd import Function
import cupy
from string import Template
from collections import namedtuple
Stream = namedtuple('Stream', ['ptr'])
def Dtype(t):
if isinstance(t, torch.cuda.FloatTensor):
return 'float'
elif isinstance(t, torch.cuda.DoubleTensor):
return 'double'
@cupy._util.memoize(for_each_device=True)
def load_kernel(kernel_name, code, **kwargs):
code = Template(code).substitute(**kwargs)
kernel_code = cupy.cuda.compile_with_cache(code)
return kernel_code.get_function(kernel_name)
CUDA_NUM_THREADS = 1024
kernel_loop = '''
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
'''
def GET_BLOCKS(N):
return (N + CUDA_NUM_THREADS - 1) // CUDA_NUM_THREADS
_involution_kernel = kernel_loop + '''
extern "C"
__global__ void involution_forward_kernel(
const ${Dtype}* bottom_data, const ${Dtype}* weight_data, ${Dtype}* top_data) {
CUDA_KERNEL_LOOP(index, ${nthreads}) {
const int n = index / ${channels} / ${top_height} / ${top_width};
const int c = (index / ${top_height} / ${top_width}) % ${channels};
const int h = (index / ${top_width}) % ${top_height};
const int w = index % ${top_width};
const int g = c / (${channels} / ${groups});
${Dtype} value = 0;
#pragma unroll
for (int kh = 0; kh < ${kernel_h}; ++kh) {
#pragma unroll
for (int kw = 0; kw < ${kernel_w}; ++kw) {
const int h_in = -${pad_h} + h * ${stride_h} + kh * ${dilation_h};
const int w_in = -${pad_w} + w * ${stride_w} + kw * ${dilation_w};
if ((h_in >= 0) && (h_in < ${bottom_height})
&& (w_in >= 0) && (w_in < ${bottom_width})) {
const int offset = ((n * ${channels} + c) * ${bottom_height} + h_in)
* ${bottom_width} + w_in;
const int offset_weight = ((((n * ${groups} + g) * ${kernel_h} + kh) * ${kernel_w} + kw) * ${top_height} + h)
* ${top_width} + w;
value += weight_data[offset_weight] * bottom_data[offset];
}
}
}
top_data[index] = value;
}
}
'''
_involution_kernel_backward_grad_input = kernel_loop + '''
extern "C"
__global__ void involution_backward_grad_input_kernel(
const ${Dtype}* const top_diff, const ${Dtype}* const weight_data, ${Dtype}* const bottom_diff) {
CUDA_KERNEL_LOOP(index, ${nthreads}) {
const int n = index / ${channels} / ${bottom_height} / ${bottom_width};
const int c = (index / ${bottom_height} / ${bottom_width}) % ${channels};
const int h = (index / ${bottom_width}) % ${bottom_height};
const int w = index % ${bottom_width};
const int g = c / (${channels} / ${groups});
${Dtype} value = 0;
#pragma unroll
for (int kh = 0; kh < ${kernel_h}; ++kh) {
#pragma unroll
for (int kw = 0; kw < ${kernel_w}; ++kw) {
const int h_out_s = h + ${pad_h} - kh * ${dilation_h};
const int w_out_s = w + ${pad_w} - kw * ${dilation_w};
if (((h_out_s % ${stride_h}) == 0) && ((w_out_s % ${stride_w}) == 0)) {
const int h_out = h_out_s / ${stride_h};
const int w_out = w_out_s / ${stride_w};
if ((h_out >= 0) && (h_out < ${top_height})
&& (w_out >= 0) && (w_out < ${top_width})) {
const int offset = ((n * ${channels} + c) * ${top_height} + h_out)
* ${top_width} + w_out;
const int offset_weight = ((((n * ${groups} + g) * ${kernel_h} + kh) * ${kernel_w} + kw) * ${top_height} + h_out)
* ${top_width} + w_out;
value += weight_data[offset_weight] * top_diff[offset];
}
}
}
}
bottom_diff[index] = value;
}
}
'''
_involution_kernel_backward_grad_weight = kernel_loop + '''
extern "C"
__global__ void involution_backward_grad_weight_kernel(
const ${Dtype}* const top_diff, const ${Dtype}* const bottom_data, ${Dtype}* const buffer_data) {
CUDA_KERNEL_LOOP(index, ${nthreads}) {
const int h = (index / ${top_width}) % ${top_height};
const int w = index % ${top_width};
const int kh = (index / ${kernel_w} / ${top_height} / ${top_width})
% ${kernel_h};
const int kw = (index / ${top_height} / ${top_width}) % ${kernel_w};
const int h_in = -${pad_h} + h * ${stride_h} + kh * ${dilation_h};
const int w_in = -${pad_w} + w * ${stride_w} + kw * ${dilation_w};
if ((h_in >= 0) && (h_in < ${bottom_height})
&& (w_in >= 0) && (w_in < ${bottom_width})) {
const int g = (index / ${kernel_h} / ${kernel_w} / ${top_height} / ${top_width}) % ${groups};
const int n = (index / ${groups} / ${kernel_h} / ${kernel_w} / ${top_height} / ${top_width}) % ${num};
${Dtype} value = 0;
#pragma unroll
for (int c = g * (${channels} / ${groups}); c < (g + 1) * (${channels} / ${groups}); ++c) {
const int top_offset = ((n * ${channels} + c) * ${top_height} + h)
* ${top_width} + w;
const int bottom_offset = ((n * ${channels} + c) * ${bottom_height} + h_in)
* ${bottom_width} + w_in;
value += top_diff[top_offset] * bottom_data[bottom_offset];
}
buffer_data[index] = value;
} else {
buffer_data[index] = 0;
}
}
}
'''
class _involution(Function):
@staticmethod
def forward(ctx, input, weight, stride, padding, dilation):
assert input.dim() == 4 and input.is_cuda
assert weight.dim() == 6 and weight.is_cuda
batch_size, channels, height, width = input.size()
kernel_h, kernel_w = weight.size()[2:4]
output_h = int((height + 2 * padding[0] - (dilation[0] * (kernel_h - 1) + 1)) / stride[0] + 1)
output_w = int((width + 2 * padding[1] - (dilation[1] * (kernel_w - 1) + 1)) / stride[1] + 1)
output = input.new(batch_size, channels, output_h, output_w)
n = output.numel()
with torch.cuda.device_of(input):
f = load_kernel('involution_forward_kernel', _involution_kernel, Dtype=Dtype(input), nthreads=n,
num=batch_size, channels=channels, groups=weight.size()[1],
bottom_height=height, bottom_width=width,
top_height=output_h, top_width=output_w,
kernel_h=kernel_h, kernel_w=kernel_w,
stride_h=stride[0], stride_w=stride[1],
dilation_h=dilation[0], dilation_w=dilation[1],
pad_h=padding[0], pad_w=padding[1])
f(block=(CUDA_NUM_THREADS,1,1),
grid=(GET_BLOCKS(n),1,1),
args=[input.data_ptr(), weight.data_ptr(), output.data_ptr()],
stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
ctx.save_for_backward(input, weight)
ctx.stride, ctx.padding, ctx.dilation = stride, padding, dilation
return output
@staticmethod
def backward(ctx, grad_output):
assert grad_output.is_cuda and grad_output.is_contiguous()
input, weight = ctx.saved_tensors
stride, padding, dilation = ctx.stride, ctx.padding, ctx.dilation
batch_size, channels, height, width = input.size()
kernel_h, kernel_w = weight.size()[2:4]
output_h, output_w = grad_output.size()[2:]
grad_input, grad_weight = None, None
opt = dict(Dtype=Dtype(grad_output),
num=batch_size, channels=channels, groups=weight.size()[1],
bottom_height=height, bottom_width=width,
top_height=output_h, top_width=output_w,
kernel_h=kernel_h, kernel_w=kernel_w,
stride_h=stride[0], stride_w=stride[1],
dilation_h=dilation[0], dilation_w=dilation[1],
pad_h=padding[0], pad_w=padding[1])
with torch.cuda.device_of(input):
if ctx.needs_input_grad[0]:
grad_input = input.new(input.size())
n = grad_input.numel()
opt['nthreads'] = n
f = load_kernel('involution_backward_grad_input_kernel',
_involution_kernel_backward_grad_input, **opt)
f(block=(CUDA_NUM_THREADS,1,1),
grid=(GET_BLOCKS(n),1,1),
args=[grad_output.data_ptr(), weight.data_ptr(), grad_input.data_ptr()],
stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
if ctx.needs_input_grad[1]:
grad_weight = weight.new(weight.size())
n = grad_weight.numel()
opt['nthreads'] = n
f = load_kernel('involution_backward_grad_weight_kernel',
_involution_kernel_backward_grad_weight, **opt)
f(block=(CUDA_NUM_THREADS,1,1),
grid=(GET_BLOCKS(n),1,1),
args=[grad_output.data_ptr(), input.data_ptr(), grad_weight.data_ptr()],
stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
return grad_input, grad_weight, None, None, None
def _involution_cuda(input, weight, bias=None, stride=1, padding=0, dilation=1):
""" involution kernel
"""
assert input.size(0) == weight.size(0)
assert input.size(-2)//stride == weight.size(-2)
assert input.size(-1)//stride == weight.size(-1)
if input.is_cuda:
out = _involution.apply(input, weight, _pair(stride), _pair(padding), _pair(dilation))
if bias is not None:
out += bias.view(1,-1,1,1)
else:
raise NotImplementedError
return out
class involution(nn.Module):
def __init__(self,
channels,
kernel_size,
stride,
padding=1):
super(involution, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.channels = channels
reduction_ratio = 4
self.group_channels = 8
self.groups = self.channels // self.group_channels
self.seblock = nn.Sequential(
nn.Conv2d(in_channels = channels, out_channels = channels // reduction_ratio, kernel_size= 1),
nn.BatchNorm2d(channels // reduction_ratio),
nn.ReLU(),
nn.Conv2d(in_channels = channels // reduction_ratio, out_channels = kernel_size**2 * self.groups, kernel_size= 1)
)
self.padding = padding
# self.conv1 = ConvModule(
# in_channels=channels,
# out_channels=channels // reduction_ratio,
# kernel_size=1,
# conv_cfg=None,
# norm_cfg=dict(type='BN'),
# act_cfg=dict(type='ReLU'))
# self.conv2 = ConvModule(
# in_channels=channels // reduction_ratio,
# out_channels=kernel_size**2 * self.groups,
# kernel_size=1,
# stride=1,
# conv_cfg=None,
# norm_cfg=None,
# act_cfg=None)
if stride > 1:
self.avgpool = nn.AvgPool2d(stride, stride)
def forward(self, x):
# weight = self.conv2(self.conv1(x if self.stride == 1 else self.avgpool(x)))
weight = self.seblock(x if self.stride == 1 else self.avgpool(x))
b, c, h, w = weight.shape
weight = weight.view(b, self.groups, self.kernel_size, self.kernel_size, h, w)
out = _involution_cuda(x, weight, stride=self.stride, padding=self.padding)
return out
+308
View File
@@ -0,0 +1,308 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
#############################################################
# File: Involution.py
# Created Date: Tuesday July 20th 2021
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Saturday, 26th February 2022 5:50:12 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2021 Shanghai Jiao Tong University
#############################################################
import torch
import torch.nn as nn
from torch.nn.modules.utils import _pair
from torch.autograd import Function
import cupy
import math
from string import Template
from collections import namedtuple
Stream = namedtuple('Stream', ['ptr'])
def Dtype(t):
if isinstance(t, torch.cuda.FloatTensor):
return 'float'
elif isinstance(t, torch.cuda.DoubleTensor):
return 'double'
@cupy._util.memoize(for_each_device=True)
def load_kernel(kernel_name, code, **kwargs):
code = Template(code).substitute(**kwargs)
kernel_code = cupy.cuda.compile_with_cache(code)
return kernel_code.get_function(kernel_name)
CUDA_NUM_THREADS = 1024
kernel_loop = '''
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; \
i < (n); \
i += blockDim.x * gridDim.x)
'''
def GET_BLOCKS(N):
return (N + CUDA_NUM_THREADS - 1) // CUDA_NUM_THREADS
_involution_kernel = kernel_loop + '''
extern "C"
__global__ void involution_forward_kernel(
const ${Dtype}* bottom_data, const ${Dtype}* weight_data, ${Dtype}* top_data) {
CUDA_KERNEL_LOOP(index, ${nthreads}) {
const int n = index / ${channels} / ${top_height} / ${top_width};
const int c = (index / ${top_height} / ${top_width}) % ${channels};
const int h = (index / ${top_width}) % ${top_height};
const int w = index % ${top_width};
const int g = c / (${channels} / ${groups});
${Dtype} value = 0;
#pragma unroll
for (int kh = 0; kh < ${kernel_h}; ++kh) {
#pragma unroll
for (int kw = 0; kw < ${kernel_w}; ++kw) {
const int h_in = -${pad_h} + h * ${stride_h} + kh * ${dilation_h};
const int w_in = -${pad_w} + w * ${stride_w} + kw * ${dilation_w};
if ((h_in >= 0) && (h_in < ${bottom_height})
&& (w_in >= 0) && (w_in < ${bottom_width})) {
const int offset = ((n * ${channels} + c) * ${bottom_height} + h_in)
* ${bottom_width} + w_in;
const int offset_weight = ((((n * ${groups} + g) * ${kernel_h} + kh) * ${kernel_w} + kw) * ${top_height} + h)
* ${top_width} + w;
value += weight_data[offset_weight] * bottom_data[offset];
}
}
}
top_data[index] = value;
}
}
'''
_involution_kernel_backward_grad_input = kernel_loop + '''
extern "C"
__global__ void involution_backward_grad_input_kernel(
const ${Dtype}* const top_diff, const ${Dtype}* const weight_data, ${Dtype}* const bottom_diff) {
CUDA_KERNEL_LOOP(index, ${nthreads}) {
const int n = index / ${channels} / ${bottom_height} / ${bottom_width};
const int c = (index / ${bottom_height} / ${bottom_width}) % ${channels};
const int h = (index / ${bottom_width}) % ${bottom_height};
const int w = index % ${bottom_width};
const int g = c / (${channels} / ${groups});
${Dtype} value = 0;
#pragma unroll
for (int kh = 0; kh < ${kernel_h}; ++kh) {
#pragma unroll
for (int kw = 0; kw < ${kernel_w}; ++kw) {
const int h_out_s = h + ${pad_h} - kh * ${dilation_h};
const int w_out_s = w + ${pad_w} - kw * ${dilation_w};
if (((h_out_s % ${stride_h}) == 0) && ((w_out_s % ${stride_w}) == 0)) {
const int h_out = h_out_s / ${stride_h};
const int w_out = w_out_s / ${stride_w};
if ((h_out >= 0) && (h_out < ${top_height})
&& (w_out >= 0) && (w_out < ${top_width})) {
const int offset = ((n * ${channels} + c) * ${top_height} + h_out)
* ${top_width} + w_out;
const int offset_weight = ((((n * ${groups} + g) * ${kernel_h} + kh) * ${kernel_w} + kw) * ${top_height} + h_out)
* ${top_width} + w_out;
value += weight_data[offset_weight] * top_diff[offset];
}
}
}
}
bottom_diff[index] = value;
}
}
'''
_involution_kernel_backward_grad_weight = kernel_loop + '''
extern "C"
__global__ void involution_backward_grad_weight_kernel(
const ${Dtype}* const top_diff, const ${Dtype}* const bottom_data, ${Dtype}* const buffer_data) {
CUDA_KERNEL_LOOP(index, ${nthreads}) {
const int h = (index / ${top_width}) % ${top_height};
const int w = index % ${top_width};
const int kh = (index / ${kernel_w} / ${top_height} / ${top_width})
% ${kernel_h};
const int kw = (index / ${top_height} / ${top_width}) % ${kernel_w};
const int h_in = -${pad_h} + h * ${stride_h} + kh * ${dilation_h};
const int w_in = -${pad_w} + w * ${stride_w} + kw * ${dilation_w};
if ((h_in >= 0) && (h_in < ${bottom_height})
&& (w_in >= 0) && (w_in < ${bottom_width})) {
const int g = (index / ${kernel_h} / ${kernel_w} / ${top_height} / ${top_width}) % ${groups};
const int n = (index / ${groups} / ${kernel_h} / ${kernel_w} / ${top_height} / ${top_width}) % ${num};
${Dtype} value = 0;
#pragma unroll
for (int c = g * (${channels} / ${groups}); c < (g + 1) * (${channels} / ${groups}); ++c) {
const int top_offset = ((n * ${channels} + c) * ${top_height} + h)
* ${top_width} + w;
const int bottom_offset = ((n * ${channels} + c) * ${bottom_height} + h_in)
* ${bottom_width} + w_in;
value += top_diff[top_offset] * bottom_data[bottom_offset];
}
buffer_data[index] = value;
} else {
buffer_data[index] = 0;
}
}
}
'''
class _involution(Function):
@staticmethod
def forward(ctx, input, weight, stride, padding, dilation):
assert input.dim() == 4 and input.is_cuda
assert weight.dim() == 6 and weight.is_cuda
batch_size, channels, height, width = input.size()
kernel_h, kernel_w = weight.size()[2:4]
output_h = int((height + 2 * padding[0] - (dilation[0] * (kernel_h - 1) + 1)) / stride[0] + 1)
output_w = int((width + 2 * padding[1] - (dilation[1] * (kernel_w - 1) + 1)) / stride[1] + 1)
output = input.new(batch_size, channels, output_h, output_w)
n = output.numel()
with torch.cuda.device_of(input):
f = load_kernel('involution_forward_kernel', _involution_kernel, Dtype=Dtype(input), nthreads=n,
num=batch_size, channels=channels, groups=weight.size()[1],
bottom_height=height, bottom_width=width,
top_height=output_h, top_width=output_w,
kernel_h=kernel_h, kernel_w=kernel_w,
stride_h=stride[0], stride_w=stride[1],
dilation_h=dilation[0], dilation_w=dilation[1],
pad_h=padding[0], pad_w=padding[1])
f(block=(CUDA_NUM_THREADS,1,1),
grid=(GET_BLOCKS(n),1,1),
args=[input.data_ptr(), weight.data_ptr(), output.data_ptr()],
stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
ctx.save_for_backward(input, weight)
ctx.stride, ctx.padding, ctx.dilation = stride, padding, dilation
return output
@staticmethod
def backward(ctx, grad_output):
assert grad_output.is_cuda and grad_output.is_contiguous()
input, weight = ctx.saved_tensors
stride, padding, dilation = ctx.stride, ctx.padding, ctx.dilation
batch_size, channels, height, width = input.size()
kernel_h, kernel_w = weight.size()[2:4]
output_h, output_w = grad_output.size()[2:]
grad_input, grad_weight = None, None
opt = dict(Dtype=Dtype(grad_output),
num=batch_size, channels=channels, groups=weight.size()[1],
bottom_height=height, bottom_width=width,
top_height=output_h, top_width=output_w,
kernel_h=kernel_h, kernel_w=kernel_w,
stride_h=stride[0], stride_w=stride[1],
dilation_h=dilation[0], dilation_w=dilation[1],
pad_h=padding[0], pad_w=padding[1])
with torch.cuda.device_of(input):
if ctx.needs_input_grad[0]:
grad_input = input.new(input.size())
n = grad_input.numel()
opt['nthreads'] = n
f = load_kernel('involution_backward_grad_input_kernel',
_involution_kernel_backward_grad_input, **opt)
f(block=(CUDA_NUM_THREADS,1,1),
grid=(GET_BLOCKS(n),1,1),
args=[grad_output.data_ptr(), weight.data_ptr(), grad_input.data_ptr()],
stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
if ctx.needs_input_grad[1]:
grad_weight = weight.new(weight.size())
n = grad_weight.numel()
opt['nthreads'] = n
f = load_kernel('involution_backward_grad_weight_kernel',
_involution_kernel_backward_grad_weight, **opt)
f(block=(CUDA_NUM_THREADS,1,1),
grid=(GET_BLOCKS(n),1,1),
args=[grad_output.data_ptr(), input.data_ptr(), grad_weight.data_ptr()],
stream=Stream(ptr=torch.cuda.current_stream().cuda_stream))
return grad_input, grad_weight, None, None, None
def _involution_cuda(input, weight, bias=None, stride=1, padding=0, dilation=1):
""" involution kernel
"""
assert input.size(0) == weight.size(0)
assert input.size(-2)//stride == weight.size(-2)
assert input.size(-1)//stride == weight.size(-1)
if input.is_cuda:
out = _involution.apply(input, weight, _pair(stride), _pair(padding), _pair(dilation))
if bias is not None:
out += bias.view(1,-1,1,1)
else:
raise NotImplementedError
return out
class involution(nn.Module):
def __init__(self,
channels,
kernel_size,
stride,
padding=1):
super(involution, self).__init__()
self.kernel_size = kernel_size
self.stride = stride
self.channels = channels
reduction_ratio = 4
self.group_channels = 8
self.groups = self.channels // self.group_channels
self.seblock = nn.Sequential(
nn.Conv2d(in_channels = channels, out_channels = channels // reduction_ratio, kernel_size= 1),
nn.BatchNorm2d(channels // reduction_ratio),
nn.ReLU(),
nn.Conv2d(in_channels = channels // reduction_ratio, out_channels = kernel_size**2 * self.groups, kernel_size= 1)
)
self.padding = padding
b = 1
gamma = 2
k_size = int(abs(math.log(channels,2)+b)/gamma)
k_size = k_size if k_size % 2 else k_size+1
self.eca_pool = nn.AdaptiveAvgPool2d(1)
self.eca_conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
self.sigmoid = nn.Sigmoid()
if stride > 1:
self.avgpool = nn.AvgPool2d(stride, stride)
def forward(self, x):
y = self.eca_pool(x)
# Two different branches of ECA module
y = self.eca_conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
# Multi-scale information fusion
y = self.sigmoid(y)
# weight = self.conv2(self.conv1(x if self.stride == 1 else self.avgpool(x)))
weight = self.seblock(x if self.stride == 1 else self.avgpool(x))
b, c, h, w = weight.shape
weight = weight.view(b, self.groups, self.kernel_size, self.kernel_size, h, w)
out = _involution_cuda(x, weight, stride=self.stride, padding=self.padding)
return out * y.expand_as(out)
+6 -6
View File
@@ -5,7 +5,7 @@
# Created Date: Sunday February 13th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Tuesday, 22nd February 2022 10:42:51 pm
# Last Modified: Sunday, 27th February 2022 6:30:55 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
@@ -20,20 +20,20 @@ from thop import clever_format
if __name__ == '__main__':
#
# script = "Generator_modulation_up"
script = "Generator_modulation_depthwise_config"
script = "Generator_Invobn_config2"
# script = "Generator_ori_config"
class_name = "Generator"
arcface_ckpt= "arcface_ckpt/arcface_checkpoint.tar"
model_config={
"id_dim": 512,
"g_kernel_size": 3,
"in_channel":32,
"res_num": 9,
"in_channel":16,
"res_num": 6,
# "up_mode": "nearest",
"up_mode": "bilinear",
"res_mode": "depthwise"
"aggregator": "eca_invo"
}
+4 -6
View File
@@ -5,7 +5,7 @@
# Created Date: Thursday February 10th 2022
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Saturday, 19th February 2022 6:23:43 pm
# Last Modified: Saturday, 26th February 2022 4:13:24 pm
# Modified By: Chen Xuanhong
# Copyright (c) 2022 Shanghai Jiao Tong University
#############################################################
@@ -21,8 +21,7 @@ if __name__ == '__main__':
# cudnn.benchmark = True
# cudnn.enabled = True
# script = "Generator_modulation_up"
script = "Generator_modulation_depthwise"
# script = "Generator_modulation_depthwise_config"
script = "Generator_Invobn_config1"
# script = "Generator_ori_config"
class_name = "Generator"
arcface_ckpt= "arcface_ckpt/arcface_checkpoint.tar"
@@ -30,11 +29,10 @@ if __name__ == '__main__':
"id_dim": 512,
"g_kernel_size": 3,
"in_channel":16,
"res_num": 9,
"res_num": 4,
# "up_mode": "nearest",
"up_mode": "bilinear",
"res_mode": "depthwise_eca",
"conv_mode": "depthwise_eca"
"res_mode": "depthwise"
}
os.environ['CUDA_VISIBLE_DEVICES'] = str(0)
+4 -4
View File
@@ -5,7 +5,7 @@
# Created Date: Tuesday April 28th 2020
# Author: Chen Xuanhong
# Email: chenxuanhongzju@outlook.com
# Last Modified: Wednesday, 23rd February 2022 12:42:54 am
# Last Modified: Sunday, 27th February 2022 12:12:19 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='depthwise_config0',
parser.add_argument('-v', '--version', type=str, default='Invobn_eca1',
help="version name for train, test, finetune")
parser.add_argument('-t', '--tag', type=str, default='tiny',
help="tag for current experiment")
@@ -46,9 +46,9 @@ def getParameters():
# training
parser.add_argument('--experiment_description', type=str,
default="尝试直接训练最小规模的网络")
default="尝试直接训练最小规模的网络,正往由ECA Invo构成")
parser.add_argument('--train_yaml', type=str, default="train_depthwise_modulation.yaml")
parser.add_argument('--train_yaml', type=str, default="train_Invobn_config.yaml")
# system logger
parser.add_argument('--logger', type=str,
+66
View File
@@ -0,0 +1,66 @@
# Related scripts
train_script_name: multi_gpu
# models' scripts
model_configs:
g_model:
script: Generator_Invobn_config
class_name: Generator
module_params:
id_dim: 512
g_kernel_size: 3
in_channel: 16
res_num: 9
up_mode: bilinear
aggregator: "invo"
d_model:
script: projected_discriminator
class_name: ProjectedDiscriminator
module_params:
diffaug: False
interp224: False
backbone_kwargs: {}
arcface_ckpt: arcface_ckpt/arcface_checkpoint.tar
# Training information
batch_size: 24
# Dataset
dataloader: VGGFace2HQ_multigpu
dataset_name: vggface2_hq
dataset_params:
random_seed: 1234
dataloader_workers: 6
eval_dataloader: DIV2K_hdf5
eval_dataset_name: DF2K_H5_Eval
eval_batch_size: 2
# Dataset
# Optimizer
optim_type: Adam
g_optim_config:
lr: 0.0006
betas: [ 0, 0.99]
eps: !!float 1e-8
d_optim_config:
lr: 0.0006
betas: [ 0, 0.99]
eps: !!float 1e-8
id_weight: 20.0
reconstruct_weight: 10.0
feature_match_weight: 10.0
# Log
log_step: 300
model_save_step: 10000
sample_step: 1000
total_step: 1000000
checkpoint_names:
generator_name: Generator
discriminator_name: Discriminator