-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
184 lines (140 loc) · 5.57 KB
/
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import os, time
import numpy as np
from PIL import Image
import glob
import torch
import torch.nn as nn
import torch.optim as optim
from skimage.metrics import peak_signal_noise_ratio as compare_psnr
from Denoise import Denoise
import argparse
import logging
logging.basicConfig(level=logging.INFO)
# Argument for denoise
parser = argparse.ArgumentParser(description="denoise")
parser.add_argument(
"--n_resblocks", type=int, default=32, help="number of residual blocks"
)
parser.add_argument("--n_feats", type=int, default=64, help="number of feature maps")
parser.add_argument("--res_scale", type=float, default=1, help="residual scaling")
parser.add_argument("--scale", type=str, default=1, help="super resolution scale")
parser.add_argument("--patch_size", type=int, default=300, help="output patch size")
parser.add_argument(
"--n_colors", type=int, default=3, help="number of input color channels to use"
)
parser.add_argument(
"--o_colors", type=int, default=3, help="number of output color channels to use"
)
args = parser.parse_args()
input_dir = "../dataset/trainsplit/low/"
gt_dir = "../dataset/trainsplit/high/"
model_dir = "./Model/"
test_name = (
"denoise-lerelu-ps-" + str(args.patch_size) + "-b-" + str(args.n_resblocks) + "/"
)
save_freq = 5
total_epoch = 501
# get train and test IDs
train_fns = glob.glob(gt_dir + "*.png")
train_ids = []
for i in range(len(train_fns)):
_, train_fn = os.path.split(train_fns[i])
train_ids.append(train_fn[0:-4])
ps = args.patch_size # patch size for training
def load_image(path):
img = Image.open(path)
img = np.array(img).astype(np.float32) / 255.0
return img
def reduce_mean(out_im, gt_im):
return torch.abs(out_im - gt_im).mean()
# Load the ground truth and input images
gt_images = [None] * 6000
input_images = [None] * len(train_ids)
g_loss = np.zeros((5000, 1))
allfolders = glob.glob(model_dir + test_name + "denoise_e*.pth")
lastepoch = 0
latest_model = None
# Find the latest saved model
if allfolders:
allfolders.sort()
latest_model = allfolders[-1]
lastepoch = int(latest_model.split("e")[-1].split(".")[0]) + 1
learning_rate = 1e-4
model = Denoise(args) # Check if GPU is available and set device accordingly
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
if torch.cuda.device_count() > 1:
print("Let's use", torch.cuda.device_count(), "GPUs!")
model = nn.DataParallel(model)
model.to(device)
opt = optim.Adam(model.parameters(), lr=learning_rate)
scheduler = optim.lr_scheduler.ReduceLROnPlateau(
opt, mode="min", factor=0.5, patience=10, verbose=True
) # Learning rate scheduler
# Load the latest model if available
if latest_model:
checkpoint = torch.load(latest_model, map_location=device)
model.load_state_dict(checkpoint)
print(f"Resuming training from epoch {lastepoch}")
for epoch in range(lastepoch, total_epoch):
psnr = []
ssim = []
if os.path.isdir("result/%04d" % epoch):
continue
cnt = 0
for ind in np.random.permutation(len(train_ids)):
# get the path from image id
train_id = train_ids[ind]
in_files = glob.glob(input_dir + train_id + ".png")
in_path = in_files[np.random.randint(0, len(in_files))]
gt_files = glob.glob(gt_dir + train_id + ".png")
gt_path = gt_files[0]
st = time.time()
cnt += 1
if input_images[ind] is None:
in_image = load_image(in_path)
gt_image = load_image(gt_path)
input_images[ind] = np.expand_dims(in_image, axis=0)
gt_images[ind] = np.expand_dims(gt_image, axis=0)
# crop
H = input_images[ind].shape[1]
W = input_images[ind].shape[2]
xx = np.random.randint(0, W - ps * args.scale)
yy = np.random.randint(0, H - ps * args.scale)
input_patch = input_images[ind][:, yy : yy + ps, xx : xx + ps, :]
gt_patch = gt_images[ind][
:, yy : yy + ps * args.scale, xx : xx + ps * args.scale, :
]
if np.random.randint(2, size=1)[0] == 1: # random flip
input_patch = np.flip(input_patch, axis=1)
gt_patch = np.flip(gt_patch, axis=1)
if np.random.randint(2, size=1)[0] == 1:
input_patch = np.flip(input_patch, axis=0)
gt_patch = np.flip(gt_patch, axis=0)
if np.random.randint(2, size=1)[0] == 1: # random transpose
input_patch = np.transpose(input_patch, (0, 2, 1, 3))
gt_patch = np.transpose(gt_patch, (0, 2, 1, 3))
input_patch = np.minimum(input_patch, 1.0)
gt_patch = np.maximum(gt_patch, 0.0)
in_img = torch.from_numpy(input_patch).permute(0, 3, 1, 2).to(device)
gt_img = torch.from_numpy(gt_patch).permute(0, 3, 1, 2).to(device)
model.zero_grad()
out_img = model(in_img)
loss = reduce_mean(out_img, gt_img)
loss.backward()
out_img = out_img.permute(0, 2, 3, 1).cpu().data.numpy()
gt_img = gt_img.permute(0, 2, 3, 1).cpu().data.numpy()
opt.step()
g_loss[ind] = loss.detach().cpu().numpy()
psnr.append(compare_psnr(gt_img[:, :, :], out_img[:, :, :]))
logging.info("---------------------------------")
logging.info("%d mean psnr: %.4f", epoch, np.mean(psnr))
scheduler.step(
np.mean(g_loss[np.where(g_loss)])
) # Update learning rate based on the scheduler
if epoch % save_freq == 0:
if not os.path.isdir(model_dir + test_name):
os.makedirs(model_dir + test_name)
torch.save(
model.state_dict(),
model_dir + test_name + "denoise_e%04d.pth" % epoch,
)