-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain_x4.py
191 lines (163 loc) · 7.15 KB
/
main_x4.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
185
186
187
188
189
190
191
from __future__ import print_function
import argparse
from math import log10
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
from torch.autograd import Variable
from torch.utils.data import DataLoader
from models.SRCNN import Net as SRCNN
from models.VDSR import Net as VDSR
from models.LGCNet import Net as LGCNet
from models.EEGAN import Net as EEGAN
from models.model import Net as Mymodel
from models.DDBPN import DDBPN
from models.rcan import RCAN
from models.RDN import RDN
from models.san import Net as SAN
from data_x4 import get_training_set
import pdb
import socket
import time
#os.environ["CUDA_VISIBLE_DEVICES"] = "0"
# Training settings
parser = argparse.ArgumentParser(description='PyTorch Super Res Example')
parser.add_argument("--scale", type=int, default=4, help="super resolution upscale factor")
parser.add_argument("--n_colors", type=int, default=3, help="scale size")
parser.add_argument("--num_features", type=int, default=64, help="number of feature maps")
parser.add_argument('--upscale_factor', type=int, default=4, help="super resolution upscale factor")
parser.add_argument('--batchSize', type=int, default=48, help='training batch size')
parser.add_argument('--nEpochs', type=int, default=1500, help='number of epochs to train for')
parser.add_argument('--snapshots', type=int, default=5, help='Snapshots')
parser.add_argument('--start_epoch', type=int, default=0, help='Starting Epoch')
parser.add_argument('--lr', type=float, default=1e-4, help='Learning Rate. Default=0.01')
parser.add_argument('--gpu_mode', type=bool, default=True)
parser.add_argument('--threads', type=int, default=1, help='number of threads for data loader to use')
parser.add_argument('--seed', type=int, default=123, help='random seed to use. Default=123')
parser.add_argument('--gpus', default=1, type=int, help='number of gpu')
parser.add_argument('--data_dir', type=str, default='/titan_data1/lxy/dataset/Satellite_Imagery/AID_train')
parser.add_argument('--data_augmentation', type=bool, default=True)
parser.add_argument('--hr_train_dataset', type=str, default='AID_train_HR')
parser.add_argument('--model_type', type=str, default='EEGAN')
parser.add_argument('--patch_size', type=int, default=48, help='Size of cropped HR image')
parser.add_argument('--save_folder', default='weights', help='Location to save checkpoint models')
parser.add_argument("--resume", default="", type=str, help="Path to checkpoint (default: none)")
parser.add_argument('--lr_train_dataset', type=str, default='/titan_data1/lxy/dataset/Satellite_Imagery/AID_train/AID_srcnn_x4')
opt = parser.parse_args()
gpus_list = range(opt.gpus)
cudnn.benchmark = True
print(opt)
def train(model, epoch):
epoch_loss = 0
model.train()
for iteration, batch in enumerate(training_data_loader, 1):
input, target = Variable(batch[0]), Variable(batch[1])
if cuda:
input = input.cuda()
target = target.cuda()
optimizer.zero_grad()
t0 = time.time()
#prediction = model(input)
out1,out2 = model(input)
# print(prediction[0])
# print(prediction.size)
# print(target.size)
#loss = criterion(prediction, target)
loss1 = criterion(out1, target)
loss2 = criterion(out2, target)
loss = 10 * loss1 + loss2
t1 = time.time()
epoch_loss += loss.data
loss.backward()
optimizer.step()
if (iteration) % 50 == 0:
print("===> Epoch[{}]({}/{}): Loss: {:.4f} || Timer: {:.4f} sec.".format(epoch, iteration, len(training_data_loader), loss.data, (t1 - t0)))
#save_checkpoint(model, epoch)
print("===> Epoch {} Complete: Avg. Loss: {:.4f}".format(epoch, epoch_loss / len(training_data_loader)))
def test():
avg_psnr = 0
for batch in testing_data_loader:
input, target = Variable(batch[0]), Variable(batch[1])
if cuda:
input = input.cuda()
target = target.cuda()
prediction = model(input)
mse = criterion(prediction, target)
psnr = 10 * log10(1 / mse.data[0])
avg_psnr += psnr
print("===> Avg. PSNR: {:.4f} dB".format(avg_psnr / len(testing_data_loader)))
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
#print(net)
print('Total number of parameters: %d' % num_params)
def save_checkpoint(model, epoch):
model_folder = "checkpoint_EEGAN_x4/"
model_out_path = model_folder + opt.model_type + "_x4" +"_epoch_{}.pth".format(epoch)
state = {"epoch": epoch ,"model": model}
if not os.path.exists(model_folder):
os.makedirs(model_folder)
torch.save(state, model_out_path)
cuda = opt.gpu_mode
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
torch.manual_seed(opt.seed)
if cuda:
torch.cuda.manual_seed(opt.seed)
print('===> Loading datasets')
train_set = get_training_set(opt.data_dir, opt.lr_train_dataset, opt.hr_train_dataset, opt.upscale_factor, opt.patch_size, opt.data_augmentation)
training_data_loader = DataLoader(dataset=train_set, num_workers=opt.threads, batch_size=opt.batchSize, shuffle=True)
print('===> Building model ', opt.model_type)
#model = SRCNN()
#model = VDSR()
#model = LGCNet()
#model = EEGAN(4)
model = Mymodel(3, 64, opt.scale)
#model = DDBPN(opt)
#model = RDN(opt.scale)
#model = RCAN(opt)
#model = SAN(opt)
#model = torch.nn.DataParallel(model, device_ids=gpus_list)
criterion = nn.L1Loss()#nn.MSELoss()#nn.L1Loss()
#################################
#################################
# pre_net = Mymodel_pre(3,64,4)
# pre_path = 'checkpoints_my_x4/model_my_x4_epoch_929.pth'
# checkpoint = torch.load(pre_path)
# pre_net.load_state_dict(checkpoint["model"].state_dict())
# pre_dict = pre_net.state_dict()
# model_dict = model.state_dict()
# pre_dict = {k: v for k, v in pre_dict.items() if k in model_dict}
# model_dict.update(pre_dict)
# model.load_state_dict(model_dict)
# print('Pre-train from:', pre_path)
################################
################################
print('---------- Networks architecture -------------')
print_network(model)
print('----------------------------------------------')
if opt.resume:
if os.path.isfile(opt.resume):
print("=> loading checkpoint '{}'".format(opt.resume))
checkpoint = torch.load(opt.resume)
opt.start_epoch = checkpoint["epoch"] + 1
print(opt.start_epoch)
model.load_state_dict(checkpoint["model"].state_dict())
else:
print("=> no checkpoint found at '{}'".format(opt.resume))
if cuda:
model = model.cuda()
criterion = criterion.cuda()
optimizer = optim.Adam(model.parameters(), lr=opt.lr, betas=(0.9, 0.999), eps=1e-8)
for epoch in range(opt.start_epoch, opt.nEpochs + 1):
train(model, epoch)
# learning rate is decayed by a factor of 2 every 200 epochs
if (epoch+1) % 500 == 0:
for param_group in optimizer.param_groups:
param_group['lr'] /= 10.0
print('Learning rate decay: lr={}'.format(optimizer.param_groups[0]['lr']))
if (epoch+1) % (opt.snapshots) == 0:
save_checkpoint(model, epoch)