-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsuper_res_train.py
executable file
·124 lines (110 loc) · 3.82 KB
/
super_res_train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"""
Train a super-resolution model.
"""
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "1"
import argparse
import torch.nn.functional as F
from guided_diffusion import dist_util, logger
from guided_diffusion.image_datasets import load_data
from guided_diffusion.resample import create_named_schedule_sampler
from guided_diffusion.script_util import (
sr_model_and_diffusion_defaults,
sr_create_model_and_diffusion,
args_to_dict,
add_dict_to_argparser,
)
from guided_diffusion.train_util import TrainLoop
import blobfile as bf
def main():
args = create_argparser().parse_args()
#args.use_fp16=True
dist_util.setup_dist()
logger.configure(dir='./experiments/'+args.save_forder)
logger.log("creating model...")
model, diffusion = sr_create_model_and_diffusion(
**args_to_dict(args, sr_model_and_diffusion_defaults().keys()),data_dir=args.data_dir+'.'+args.cloudmodel
)
model.to(dist_util.dev())
print('Total params: %.2fM' % (sum(p.numel() for p in model.parameters())/1000000.0))
schedule_sampler = create_named_schedule_sampler(args.schedule_sampler, diffusion)
logger.log(args.model_path)
model_path = args.model_path
if(len(sorted(bf.listdir(model_path)))):
entry = sorted(bf.listdir(model_path))[-1]
full_path = bf.join(model_path, entry)
logger.log(model_path)
model.load_state_dict(
dist_util.load_state_dict(full_path, map_location="cpu"),strict=False
)
model.to(dist_util.dev())
logger.log("load model from "+entry)
logger.log("creating data loader...")
data_dir = "./data/"+args.data_dir+"/train/cloud"
data = load_superres_data(
data_dir,
args.batch_size,
large_size=args.image_size,
small_size=args.small_size,
class_cond=args.class_cond,
)
logger.log("lr:" +str(args.lr))
logger.log("image size:" +str(args.image_size))
logger.log("training...")
TrainLoop(
model=model,
diffusion=diffusion,
data=data,
batch_size=args.batch_size,
microbatch=args.microbatch,
lr=args.lr,
ema_rate=args.ema_rate,
log_interval=args.log_interval,
save_interval=args.save_interval,
resume_checkpoint=args.resume_checkpoint,
use_fp16=args.use_fp16,
fp16_scale_growth=args.fp16_scale_growth,
schedule_sampler=schedule_sampler,
weight_decay=args.weight_decay,
lr_anneal_steps=args.lr_anneal_steps,
).run_loop()
def load_superres_data(data_dir, batch_size, large_size, small_size, class_cond=False):
data = load_data(
data_dir=data_dir,
batch_size=batch_size,
image_size=large_size,
class_cond=class_cond,
)
for large_batch, model_kwargs in data:
# large_batch = large_batch['label']
# model_kwargs["low_res"] = F.interpolate(large_batch, small_size, mode="area")
model_kwargs["low_res"] = large_batch['cloud']
model_kwargs["previous"] = large_batch['previous']
large_batch = large_batch['label']
yield large_batch, model_kwargs
def create_argparser():
defaults = dict(
data_dir="RICE2", #换数据集
schedule_sampler="uniform",
lr=1e-5,
weight_decay=0.0,
lr_anneal_steps=0,
batch_size=64, #根据image_size改 64
microbatch=-1,
ema_rate="0.9999",
log_interval=1000,
save_interval=1000,
resume_checkpoint="",
use_fp16=True,
fp16_scale_growth=1e-3,
save_forder='train_model',
model_path="./pre_train",
image_size=64,
cloudmodel = 'mn'
)
defaults.update(sr_model_and_diffusion_defaults())
parser = argparse.ArgumentParser()
add_dict_to_argparser(parser, defaults)
return parser
if __name__ == "__main__":
main()