supli6669 commited on
Commit ·
a282d8c
1
Parent(s): 4b89faf
feat: add CPU-based toy training configs and fix net_g.module AttributeError for CPU training
Browse files- models/CodeFormer/basicsr/models/codeformer_idx_model.py +1 -1
- models/CodeFormer/basicsr/models/codeformer_joint_model.py +3 -3
- models/CodeFormer/basicsr/models/codeformer_model.py +3 -3
- models/CodeFormer/basicsr/models/vqgan_model.py +3 -3
- models/CodeFormer/options/CodeFormer_stage2_toy.yml +128 -0
- models/CodeFormer/options/CodeFormer_stage3_toy.yml +164 -0
- models/CodeFormer/options/VQGAN_toy.yml +111 -0
- prepare_toy_training.py +100 -0
models/CodeFormer/basicsr/models/codeformer_idx_model.py
CHANGED
|
@@ -96,7 +96,7 @@ class CodeFormerIdxModel(SRModel):
|
|
| 96 |
|
| 97 |
if self.hq_feat_loss:
|
| 98 |
# quant_feats
|
| 99 |
-
quant_feat_gt = self.net_g.
|
| 100 |
|
| 101 |
logits, lq_feat = self.net_g(self.input, w=0, code_only=True)
|
| 102 |
|
|
|
|
| 96 |
|
| 97 |
if self.hq_feat_loss:
|
| 98 |
# quant_feats
|
| 99 |
+
quant_feat_gt = self.get_bare_model(self.net_g).quantize.get_codebook_feat(self.idx_gt, shape=[self.b,16,16,256])
|
| 100 |
|
| 101 |
logits, lq_feat = self.net_g(self.input, w=0, code_only=True)
|
| 102 |
|
models/CodeFormer/basicsr/models/codeformer_joint_model.py
CHANGED
|
@@ -172,7 +172,7 @@ class CodeFormerJointModel(SRModel):
|
|
| 172 |
|
| 173 |
if self.hq_feat_loss:
|
| 174 |
# quant_feats
|
| 175 |
-
quant_feat_gt = self.net_g.
|
| 176 |
|
| 177 |
l_g_total = 0
|
| 178 |
loss_dict = OrderedDict()
|
|
@@ -210,11 +210,11 @@ class CodeFormerJointModel(SRModel):
|
|
| 210 |
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
|
| 211 |
recon_loss = l_g_pix + l_g_percep
|
| 212 |
if not self.fix_generator:
|
| 213 |
-
last_layer = self.net_g.
|
| 214 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 215 |
else:
|
| 216 |
largest_fuse_size = self.opt['network_g']['connect_list'][-1]
|
| 217 |
-
last_layer = self.net_g.
|
| 218 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 219 |
|
| 220 |
d_weight *= self.scale_adaptive_gan_weight # 0.8
|
|
|
|
| 172 |
|
| 173 |
if self.hq_feat_loss:
|
| 174 |
# quant_feats
|
| 175 |
+
quant_feat_gt = self.get_bare_model(self.net_g).quantize.get_codebook_feat(self.idx_gt, shape=[self.b,16,16,256])
|
| 176 |
|
| 177 |
l_g_total = 0
|
| 178 |
loss_dict = OrderedDict()
|
|
|
|
| 210 |
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
|
| 211 |
recon_loss = l_g_pix + l_g_percep
|
| 212 |
if not self.fix_generator:
|
| 213 |
+
last_layer = self.get_bare_model(self.net_g).generator.blocks[-1].weight
|
| 214 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 215 |
else:
|
| 216 |
largest_fuse_size = self.opt['network_g']['connect_list'][-1]
|
| 217 |
+
last_layer = self.get_bare_model(self.net_g).fuse_convs_dict[largest_fuse_size].shift[-1].weight
|
| 218 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 219 |
|
| 220 |
d_weight *= self.scale_adaptive_gan_weight # 0.8
|
models/CodeFormer/basicsr/models/codeformer_model.py
CHANGED
|
@@ -155,7 +155,7 @@ class CodeFormerModel(SRModel):
|
|
| 155 |
|
| 156 |
if self.hq_feat_loss:
|
| 157 |
# quant_feats
|
| 158 |
-
quant_feat_gt = self.net_g.
|
| 159 |
|
| 160 |
l_g_total = 0
|
| 161 |
loss_dict = OrderedDict()
|
|
@@ -192,11 +192,11 @@ class CodeFormerModel(SRModel):
|
|
| 192 |
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
|
| 193 |
recon_loss = l_g_pix + l_g_percep
|
| 194 |
if not self.fix_generator:
|
| 195 |
-
last_layer = self.net_g.
|
| 196 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 197 |
else:
|
| 198 |
largest_fuse_size = self.opt['network_g']['connect_list'][-1]
|
| 199 |
-
last_layer = self.net_g.
|
| 200 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 201 |
|
| 202 |
d_weight *= self.scale_adaptive_gan_weight # 0.8
|
|
|
|
| 155 |
|
| 156 |
if self.hq_feat_loss:
|
| 157 |
# quant_feats
|
| 158 |
+
quant_feat_gt = self.get_bare_model(self.net_g).quantize.get_codebook_feat(self.idx_gt, shape=[self.b,16,16,256])
|
| 159 |
|
| 160 |
l_g_total = 0
|
| 161 |
loss_dict = OrderedDict()
|
|
|
|
| 192 |
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
|
| 193 |
recon_loss = l_g_pix + l_g_percep
|
| 194 |
if not self.fix_generator:
|
| 195 |
+
last_layer = self.get_bare_model(self.net_g).generator.blocks[-1].weight
|
| 196 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 197 |
else:
|
| 198 |
largest_fuse_size = self.opt['network_g']['connect_list'][-1]
|
| 199 |
+
last_layer = self.get_bare_model(self.net_g).fuse_convs_dict[largest_fuse_size].shift[-1].weight
|
| 200 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 201 |
|
| 202 |
d_weight *= self.scale_adaptive_gan_weight # 0.8
|
models/CodeFormer/basicsr/models/vqgan_model.py
CHANGED
|
@@ -118,9 +118,9 @@ class VQGANModel(SRModel):
|
|
| 118 |
logger = get_root_logger()
|
| 119 |
loss_dict = OrderedDict()
|
| 120 |
if self.opt['network_g']['quantizer'] == 'gumbel':
|
| 121 |
-
self.net_g.
|
| 122 |
if current_iter%1000 == 0:
|
| 123 |
-
logger.info(f'temperature: {self.net_g.
|
| 124 |
|
| 125 |
# optimize net_g
|
| 126 |
for p in self.net_d.parameters():
|
|
@@ -150,7 +150,7 @@ class VQGANModel(SRModel):
|
|
| 150 |
fake_g_pred = self.net_d(self.output)
|
| 151 |
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
|
| 152 |
recon_loss = l_g_total
|
| 153 |
-
last_layer = self.net_g.
|
| 154 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 155 |
d_weight *= self.adopt_weight(1, current_iter, self.net_d_start_iter)
|
| 156 |
d_weight *= self.disc_weight # tamming setting 0.8
|
|
|
|
| 118 |
logger = get_root_logger()
|
| 119 |
loss_dict = OrderedDict()
|
| 120 |
if self.opt['network_g']['quantizer'] == 'gumbel':
|
| 121 |
+
self.get_bare_model(self.net_g).quantize.temperature = max(1/16, ((-1/160000) * current_iter) + 1)
|
| 122 |
if current_iter%1000 == 0:
|
| 123 |
+
logger.info(f'temperature: {self.get_bare_model(self.net_g).quantize.temperature}')
|
| 124 |
|
| 125 |
# optimize net_g
|
| 126 |
for p in self.net_d.parameters():
|
|
|
|
| 150 |
fake_g_pred = self.net_d(self.output)
|
| 151 |
l_g_gan = self.cri_gan(fake_g_pred, True, is_disc=False)
|
| 152 |
recon_loss = l_g_total
|
| 153 |
+
last_layer = self.get_bare_model(self.net_g).generator.blocks[-1].weight
|
| 154 |
d_weight = self.calculate_adaptive_weight(recon_loss, l_g_gan, last_layer, disc_weight_max=1.0)
|
| 155 |
d_weight *= self.adopt_weight(1, current_iter, self.net_d_start_iter)
|
| 156 |
d_weight *= self.disc_weight # tamming setting 0.8
|
models/CodeFormer/options/CodeFormer_stage2_toy.yml
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|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512
|
| 15 |
+
mean:
|
| 16 |
+
- 0.5
|
| 17 |
+
- 0.5
|
| 18 |
+
- 0.5
|
| 19 |
+
std:
|
| 20 |
+
- 0.5
|
| 21 |
+
- 0.5
|
| 22 |
+
- 0.5
|
| 23 |
+
use_hflip: true
|
| 24 |
+
use_corrupt: true
|
| 25 |
+
blur_kernel_size: 41
|
| 26 |
+
use_motion_kernel: false
|
| 27 |
+
motion_kernel_prob: 0.001
|
| 28 |
+
kernel_list:
|
| 29 |
+
- iso
|
| 30 |
+
- aniso
|
| 31 |
+
kernel_prob:
|
| 32 |
+
- 0.5
|
| 33 |
+
- 0.5
|
| 34 |
+
blur_sigma:
|
| 35 |
+
- 1
|
| 36 |
+
- 15
|
| 37 |
+
downsample_range:
|
| 38 |
+
- 4
|
| 39 |
+
- 30
|
| 40 |
+
noise_range:
|
| 41 |
+
- 0
|
| 42 |
+
- 20
|
| 43 |
+
jpeg_range:
|
| 44 |
+
- 30
|
| 45 |
+
- 80
|
| 46 |
+
latent_gt_path: null
|
| 47 |
+
num_worker_per_gpu: 0
|
| 48 |
+
batch_size_per_gpu: 1
|
| 49 |
+
dataset_enlarge_ratio: 1
|
| 50 |
+
prefetch_mode: cpu
|
| 51 |
+
network_g:
|
| 52 |
+
type: CodeFormer
|
| 53 |
+
dim_embd: 512
|
| 54 |
+
n_head: 8
|
| 55 |
+
n_layers: 9
|
| 56 |
+
codebook_size: 1024
|
| 57 |
+
connect_list:
|
| 58 |
+
- '32'
|
| 59 |
+
- '64'
|
| 60 |
+
- '128'
|
| 61 |
+
- '256'
|
| 62 |
+
fix_modules:
|
| 63 |
+
- quantize
|
| 64 |
+
- generator
|
| 65 |
+
vqgan_path: ./experiments/pretrained_models/vqgan/vqgan_code1024.pth
|
| 66 |
+
network_vqgan:
|
| 67 |
+
type: VQAutoEncoder
|
| 68 |
+
img_size: 512
|
| 69 |
+
nf: 64
|
| 70 |
+
ch_mult:
|
| 71 |
+
- 1
|
| 72 |
+
- 2
|
| 73 |
+
- 2
|
| 74 |
+
- 4
|
| 75 |
+
- 4
|
| 76 |
+
- 8
|
| 77 |
+
quantizer: nearest
|
| 78 |
+
codebook_size: 1024
|
| 79 |
+
path:
|
| 80 |
+
pretrain_network_g: null
|
| 81 |
+
param_key_g: params_ema
|
| 82 |
+
strict_load_g: false
|
| 83 |
+
pretrain_network_d: null
|
| 84 |
+
strict_load_d: true
|
| 85 |
+
resume_state: null
|
| 86 |
+
train:
|
| 87 |
+
use_hq_feat_loss: true
|
| 88 |
+
feat_loss_weight: 1.0
|
| 89 |
+
cross_entropy_loss: true
|
| 90 |
+
entropy_loss_weight: 0.5
|
| 91 |
+
fidelity_weight: 0
|
| 92 |
+
optim_g:
|
| 93 |
+
type: Adam
|
| 94 |
+
lr: 0.0001
|
| 95 |
+
weight_decay: 0
|
| 96 |
+
betas:
|
| 97 |
+
- 0.9
|
| 98 |
+
- 0.99
|
| 99 |
+
scheduler:
|
| 100 |
+
type: MultiStepLR
|
| 101 |
+
milestones:
|
| 102 |
+
- 400000
|
| 103 |
+
- 450000
|
| 104 |
+
gamma: 0.5
|
| 105 |
+
total_iter: 5
|
| 106 |
+
warmup_iter: -1
|
| 107 |
+
ema_decay: 0.995
|
| 108 |
+
use_adaptive_weight: true
|
| 109 |
+
net_g_start_iter: 0
|
| 110 |
+
net_d_iters: 1
|
| 111 |
+
net_d_start_iter: 0
|
| 112 |
+
manual_seed: 0
|
| 113 |
+
val:
|
| 114 |
+
val_freq: 50000000000.0
|
| 115 |
+
save_img: true
|
| 116 |
+
metrics:
|
| 117 |
+
psnr:
|
| 118 |
+
type: calculate_psnr
|
| 119 |
+
crop_border: 4
|
| 120 |
+
test_y_channel: false
|
| 121 |
+
logger:
|
| 122 |
+
print_freq: 1
|
| 123 |
+
save_checkpoint_freq: 5
|
| 124 |
+
use_tb_logger: false
|
| 125 |
+
wandb: null
|
| 126 |
+
dist_params: null
|
| 127 |
+
find_unused_parameters: true
|
| 128 |
+
dist: false
|
models/CodeFormer/options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
|
|
|
|
| 1 |
+
name: CodeFormer_stage3
|
| 2 |
+
model_type: CodeFormerJointModel
|
| 3 |
+
num_gpu: 0
|
| 4 |
+
manual_seed: 0
|
| 5 |
+
datasets:
|
| 6 |
+
train:
|
| 7 |
+
name: FFHQ
|
| 8 |
+
type: FFHQBlindJointDataset
|
| 9 |
+
dataroot_gt: datasets/ffhq/ffhq_512
|
| 10 |
+
filename_tmpl: '{}'
|
| 11 |
+
io_backend:
|
| 12 |
+
type: disk
|
| 13 |
+
in_size: 512
|
| 14 |
+
gt_size: 512
|
| 15 |
+
mean:
|
| 16 |
+
- 0.5
|
| 17 |
+
- 0.5
|
| 18 |
+
- 0.5
|
| 19 |
+
std:
|
| 20 |
+
- 0.5
|
| 21 |
+
- 0.5
|
| 22 |
+
- 0.5
|
| 23 |
+
use_hflip: true
|
| 24 |
+
use_corrupt: true
|
| 25 |
+
blur_kernel_size: 41
|
| 26 |
+
use_motion_kernel: false
|
| 27 |
+
motion_kernel_prob: 0.001
|
| 28 |
+
kernel_list:
|
| 29 |
+
- iso
|
| 30 |
+
- aniso
|
| 31 |
+
kernel_prob:
|
| 32 |
+
- 0.5
|
| 33 |
+
- 0.5
|
| 34 |
+
blur_sigma:
|
| 35 |
+
- 0.1
|
| 36 |
+
- 10
|
| 37 |
+
downsample_range:
|
| 38 |
+
- 1
|
| 39 |
+
- 12
|
| 40 |
+
noise_range:
|
| 41 |
+
- 0
|
| 42 |
+
- 15
|
| 43 |
+
jpeg_range:
|
| 44 |
+
- 60
|
| 45 |
+
- 100
|
| 46 |
+
blur_sigma_large:
|
| 47 |
+
- 1
|
| 48 |
+
- 15
|
| 49 |
+
downsample_range_large:
|
| 50 |
+
- 4
|
| 51 |
+
- 30
|
| 52 |
+
noise_range_large:
|
| 53 |
+
- 0
|
| 54 |
+
- 20
|
| 55 |
+
jpeg_range_large:
|
| 56 |
+
- 30
|
| 57 |
+
- 80
|
| 58 |
+
latent_gt_path: null
|
| 59 |
+
num_worker_per_gpu: 0
|
| 60 |
+
batch_size_per_gpu: 1
|
| 61 |
+
dataset_enlarge_ratio: 1
|
| 62 |
+
prefetch_mode: cpu
|
| 63 |
+
network_g:
|
| 64 |
+
type: CodeFormer
|
| 65 |
+
dim_embd: 512
|
| 66 |
+
n_head: 8
|
| 67 |
+
n_layers: 9
|
| 68 |
+
codebook_size: 1024
|
| 69 |
+
connect_list:
|
| 70 |
+
- '32'
|
| 71 |
+
- '64'
|
| 72 |
+
- '128'
|
| 73 |
+
- '256'
|
| 74 |
+
fix_modules:
|
| 75 |
+
- quantize
|
| 76 |
+
- generator
|
| 77 |
+
network_vqgan:
|
| 78 |
+
type: VQAutoEncoder
|
| 79 |
+
img_size: 512
|
| 80 |
+
nf: 64
|
| 81 |
+
ch_mult:
|
| 82 |
+
- 1
|
| 83 |
+
- 2
|
| 84 |
+
- 2
|
| 85 |
+
- 4
|
| 86 |
+
- 4
|
| 87 |
+
- 8
|
| 88 |
+
quantizer: nearest
|
| 89 |
+
codebook_size: 1024
|
| 90 |
+
network_d:
|
| 91 |
+
type: VQGANDiscriminator
|
| 92 |
+
nc: 3
|
| 93 |
+
ndf: 64
|
| 94 |
+
n_layers: 4
|
| 95 |
+
path:
|
| 96 |
+
pretrain_network_g: ./experiments/pretrained_models/CodeFormer_stage2/net_g_latest.pth
|
| 97 |
+
param_key_g: params_ema
|
| 98 |
+
strict_load_g: false
|
| 99 |
+
pretrain_network_d: null
|
| 100 |
+
resume_state: null
|
| 101 |
+
train:
|
| 102 |
+
use_hq_feat_loss: true
|
| 103 |
+
feat_loss_weight: 1.0
|
| 104 |
+
cross_entropy_loss: true
|
| 105 |
+
entropy_loss_weight: 0.5
|
| 106 |
+
scale_adaptive_gan_weight: 0.1
|
| 107 |
+
optim_g:
|
| 108 |
+
type: Adam
|
| 109 |
+
lr: 5.0e-05
|
| 110 |
+
weight_decay: 0
|
| 111 |
+
betas:
|
| 112 |
+
- 0.9
|
| 113 |
+
- 0.99
|
| 114 |
+
optim_d:
|
| 115 |
+
type: Adam
|
| 116 |
+
lr: 5.0e-05
|
| 117 |
+
weight_decay: 0
|
| 118 |
+
betas:
|
| 119 |
+
- 0.9
|
| 120 |
+
- 0.99
|
| 121 |
+
scheduler:
|
| 122 |
+
type: CosineAnnealingRestartLR
|
| 123 |
+
periods:
|
| 124 |
+
- 150000
|
| 125 |
+
restart_weights:
|
| 126 |
+
- 1
|
| 127 |
+
eta_min: 2.0e-05
|
| 128 |
+
total_iter: 5
|
| 129 |
+
warmup_iter: -1
|
| 130 |
+
ema_decay: 0.997
|
| 131 |
+
pixel_opt:
|
| 132 |
+
type: L1Loss
|
| 133 |
+
loss_weight: 1.0
|
| 134 |
+
reduction: mean
|
| 135 |
+
perceptual_opt:
|
| 136 |
+
type: LPIPSLoss
|
| 137 |
+
loss_weight: 1.0
|
| 138 |
+
use_input_norm: true
|
| 139 |
+
range_norm: true
|
| 140 |
+
gan_opt:
|
| 141 |
+
type: GANLoss
|
| 142 |
+
gan_type: hinge
|
| 143 |
+
loss_weight: 1.0
|
| 144 |
+
use_adaptive_weight: true
|
| 145 |
+
net_g_start_iter: 0
|
| 146 |
+
net_d_iters: 1
|
| 147 |
+
net_d_start_iter: 5001
|
| 148 |
+
manual_seed: 0
|
| 149 |
+
val:
|
| 150 |
+
val_freq: 50000000000.0
|
| 151 |
+
save_img: true
|
| 152 |
+
metrics:
|
| 153 |
+
psnr:
|
| 154 |
+
type: calculate_psnr
|
| 155 |
+
crop_border: 4
|
| 156 |
+
test_y_channel: false
|
| 157 |
+
logger:
|
| 158 |
+
print_freq: 1
|
| 159 |
+
save_checkpoint_freq: 5
|
| 160 |
+
use_tb_logger: false
|
| 161 |
+
wandb: null
|
| 162 |
+
dist_params: null
|
| 163 |
+
find_unused_parameters: true
|
| 164 |
+
dist: false
|
models/CodeFormer/options/VQGAN_toy.yml
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: VQGAN-512-ds32-nearest-stage1
|
| 2 |
+
model_type: VQGANModel
|
| 3 |
+
num_gpu: 0
|
| 4 |
+
manual_seed: 0
|
| 5 |
+
datasets:
|
| 6 |
+
train:
|
| 7 |
+
name: FFHQ
|
| 8 |
+
type: FFHQBlindDataset
|
| 9 |
+
dataroot_gt: datasets/ffhq/ffhq_512
|
| 10 |
+
filename_tmpl: '{}'
|
| 11 |
+
io_backend:
|
| 12 |
+
type: disk
|
| 13 |
+
in_size: 512
|
| 14 |
+
gt_size: 512
|
| 15 |
+
mean:
|
| 16 |
+
- 0.5
|
| 17 |
+
- 0.5
|
| 18 |
+
- 0.5
|
| 19 |
+
std:
|
| 20 |
+
- 0.5
|
| 21 |
+
- 0.5
|
| 22 |
+
- 0.5
|
| 23 |
+
use_hflip: true
|
| 24 |
+
use_corrupt: false
|
| 25 |
+
num_worker_per_gpu: 0
|
| 26 |
+
batch_size_per_gpu: 1
|
| 27 |
+
dataset_enlarge_ratio: 1
|
| 28 |
+
prefetch_mode: cpu
|
| 29 |
+
num_prefetch_queue: 4
|
| 30 |
+
network_g:
|
| 31 |
+
type: VQAutoEncoder
|
| 32 |
+
img_size: 512
|
| 33 |
+
nf: 64
|
| 34 |
+
ch_mult:
|
| 35 |
+
- 1
|
| 36 |
+
- 2
|
| 37 |
+
- 2
|
| 38 |
+
- 4
|
| 39 |
+
- 4
|
| 40 |
+
- 8
|
| 41 |
+
quantizer: nearest
|
| 42 |
+
codebook_size: 1024
|
| 43 |
+
network_d:
|
| 44 |
+
type: VQGANDiscriminator
|
| 45 |
+
nc: 3
|
| 46 |
+
ndf: 64
|
| 47 |
+
path:
|
| 48 |
+
pretrain_network_g: null
|
| 49 |
+
param_key_g: params_ema
|
| 50 |
+
strict_load_g: true
|
| 51 |
+
pretrain_network_d: null
|
| 52 |
+
strict_load_d: true
|
| 53 |
+
resume_state: null
|
| 54 |
+
train:
|
| 55 |
+
optim_g:
|
| 56 |
+
type: Adam
|
| 57 |
+
lr: 7.0e-05
|
| 58 |
+
weight_decay: 0
|
| 59 |
+
betas:
|
| 60 |
+
- 0.9
|
| 61 |
+
- 0.99
|
| 62 |
+
optim_d:
|
| 63 |
+
type: Adam
|
| 64 |
+
lr: 7.0e-05
|
| 65 |
+
weight_decay: 0
|
| 66 |
+
betas:
|
| 67 |
+
- 0.9
|
| 68 |
+
- 0.99
|
| 69 |
+
scheduler:
|
| 70 |
+
type: CosineAnnealingRestartLR
|
| 71 |
+
periods:
|
| 72 |
+
- 1600000
|
| 73 |
+
restart_weights:
|
| 74 |
+
- 1
|
| 75 |
+
eta_min: 6.0e-05
|
| 76 |
+
total_iter: 5
|
| 77 |
+
warmup_iter: -1
|
| 78 |
+
ema_decay: 0.995
|
| 79 |
+
pixel_opt:
|
| 80 |
+
type: L1Loss
|
| 81 |
+
loss_weight: 1.0
|
| 82 |
+
reduction: mean
|
| 83 |
+
perceptual_opt:
|
| 84 |
+
type: LPIPSLoss
|
| 85 |
+
loss_weight: 1.0
|
| 86 |
+
use_input_norm: true
|
| 87 |
+
range_norm: true
|
| 88 |
+
gan_opt:
|
| 89 |
+
type: GANLoss
|
| 90 |
+
gan_type: hinge
|
| 91 |
+
loss_weight: 1.0
|
| 92 |
+
net_g_start_iter: 0
|
| 93 |
+
net_d_iters: 1
|
| 94 |
+
net_d_start_iter: 30001
|
| 95 |
+
manual_seed: 0
|
| 96 |
+
val:
|
| 97 |
+
val_freq: 50000000000.0
|
| 98 |
+
save_img: true
|
| 99 |
+
metrics:
|
| 100 |
+
psnr:
|
| 101 |
+
type: calculate_psnr
|
| 102 |
+
crop_border: 4
|
| 103 |
+
test_y_channel: false
|
| 104 |
+
logger:
|
| 105 |
+
print_freq: 1
|
| 106 |
+
save_checkpoint_freq: 5
|
| 107 |
+
use_tb_logger: false
|
| 108 |
+
wandb: null
|
| 109 |
+
dist_params: null
|
| 110 |
+
find_unused_parameters: true
|
| 111 |
+
dist: false
|
prepare_toy_training.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
import yaml
|
| 4 |
+
|
| 5 |
+
def main():
|
| 6 |
+
project_dir = os.path.dirname(os.path.abspath(__file__))
|
| 7 |
+
codeformer_dir = os.path.join(project_dir, "models", "CodeFormer")
|
| 8 |
+
|
| 9 |
+
# 1. Create target dataset directory
|
| 10 |
+
dataset_dir = os.path.join(codeformer_dir, "datasets", "ffhq", "ffhq_512")
|
| 11 |
+
os.makedirs(dataset_dir, exist_ok=True)
|
| 12 |
+
print(f"Created dataset directory: {dataset_dir}")
|
| 13 |
+
|
| 14 |
+
# 2. Download toy dataset (30 face images from Unsplash)
|
| 15 |
+
unsplash_ids = [
|
| 16 |
+
"1544005313-94ddf0286df2", "1506794778202-cad84cf45f1d", "1534528741775-53994a69daeb",
|
| 17 |
+
"1507003211169-0a1dd7228f2d", "1522075469751-3a6694fb2f61", "1544717305-2782549b5136",
|
| 18 |
+
"1554151228-14d9def656e4", "1531746020798-e6953c6e8e04", "1500648767791-00dcc994a43e",
|
| 19 |
+
"1508214751196-bcfd4ca60f91", "1494790108377-be9c29b29330", "1517841905240-472988babdf9",
|
| 20 |
+
"1539571696357-5a69c17a67c6", "1438761681033-6461ffad8d80", "1524504388940-b1c1722653e1",
|
| 21 |
+
"1519085360753-af0119f7cbe7", "1491528920044-4531310b6531", "1503023344727-8982f00d2947",
|
| 22 |
+
"1534308983496-4fabb1a015ee", "1542206395-9feb3edaa68d", "1501196354995-cbb51c65aaea",
|
| 23 |
+
"1506863530036-1775a06bfa37", "1508214751196-bcfd4ca60f91", "1513956589380-bad6acb9b9d4",
|
| 24 |
+
"1519345182560-3f2917c472ef", "1520155707334-757655122b38", "1530577197743-7adf14294584",
|
| 25 |
+
"1531123897727-8f129e1688ce", "1539571696357-5a69c17a67c6", "1548142813-c348350df52b"
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
headers = {
|
| 29 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
print("Downloading 30 face images from Unsplash (512x512 cropped)...")
|
| 33 |
+
for i, img_id in enumerate(unsplash_ids):
|
| 34 |
+
filename = f"{i:05d}.png"
|
| 35 |
+
filepath = os.path.join(dataset_dir, filename)
|
| 36 |
+
if os.path.exists(filepath):
|
| 37 |
+
print(f"Skipping {filename} (already exists)")
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
+
url = f"https://images.unsplash.com/photo-{img_id}?w=512&h=512&fit=crop&q=80"
|
| 41 |
+
try:
|
| 42 |
+
r = requests.get(url, headers=headers, timeout=15)
|
| 43 |
+
if r.status_code == 200:
|
| 44 |
+
with open(filepath, "wb") as f:
|
| 45 |
+
f.write(r.content)
|
| 46 |
+
print(f"Downloaded {filename}")
|
| 47 |
+
else:
|
| 48 |
+
print(f"Failed to download {filename} (Status: {r.status_code})")
|
| 49 |
+
except Exception as e:
|
| 50 |
+
print(f"Error downloading {filename}: {e}")
|
| 51 |
+
|
| 52 |
+
# 3. Create local toy configurations for Stages I, II, and III
|
| 53 |
+
configs_to_make = [
|
| 54 |
+
("VQGAN_512_ds32_nearest_stage1.yml", "VQGAN_toy.yml"),
|
| 55 |
+
("CodeFormer_stage2.yml", "CodeFormer_stage2_toy.yml"),
|
| 56 |
+
("CodeFormer_stage3.yml", "CodeFormer_stage3_toy.yml")
|
| 57 |
+
]
|
| 58 |
+
|
| 59 |
+
for orig_name, toy_name in configs_to_make:
|
| 60 |
+
orig_path = os.path.join(codeformer_dir, "options", orig_name)
|
| 61 |
+
toy_path = os.path.join(codeformer_dir, "options", toy_name)
|
| 62 |
+
|
| 63 |
+
if not os.path.exists(orig_path):
|
| 64 |
+
print(f"Error: Original config {orig_name} not found!")
|
| 65 |
+
continue
|
| 66 |
+
|
| 67 |
+
with open(orig_path, "r", encoding="utf-8") as f:
|
| 68 |
+
config = yaml.safe_load(f)
|
| 69 |
+
|
| 70 |
+
# Modify for local CPU toy training
|
| 71 |
+
config["num_gpu"] = 0
|
| 72 |
+
config["dist_params"] = None
|
| 73 |
+
config["dist"] = False
|
| 74 |
+
|
| 75 |
+
if "datasets" in config:
|
| 76 |
+
for phase in config["datasets"]:
|
| 77 |
+
dataset = config["datasets"][phase]
|
| 78 |
+
dataset["num_worker_per_gpu"] = 0 # No multiprocessing on CPU (avoid pickling/Windows issues)
|
| 79 |
+
dataset["batch_size_per_gpu"] = 1
|
| 80 |
+
dataset["dataset_enlarge_ratio"] = 1 # Don't enlarge dataset, keep it small
|
| 81 |
+
if "prefetch_mode" in dataset:
|
| 82 |
+
dataset["prefetch_mode"] = "cpu"
|
| 83 |
+
|
| 84 |
+
if "train" in config:
|
| 85 |
+
config["train"]["total_iter"] = 5 # Train for only 5 iterations to test
|
| 86 |
+
|
| 87 |
+
if "logger" in config:
|
| 88 |
+
config["logger"]["print_freq"] = 1
|
| 89 |
+
config["logger"]["save_checkpoint_freq"] = 5
|
| 90 |
+
config["logger"]["use_tb_logger"] = False
|
| 91 |
+
config["logger"]["wandb"] = None
|
| 92 |
+
|
| 93 |
+
with open(toy_path, "w", encoding="utf-8") as f:
|
| 94 |
+
yaml.dump(config, f, default_flow_style=False, sort_keys=False)
|
| 95 |
+
print(f"Created local toy config: {toy_path}")
|
| 96 |
+
|
| 97 |
+
print("\nSUCCESS: Toy training setup complete!")
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|