-
Notifications
You must be signed in to change notification settings - Fork 0
/
dense_train.py
147 lines (110 loc) · 5.75 KB
/
dense_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
import argparse, os
import pdb
import torch
import random
import torch.backends.cudnn as cudnn
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
from torch.utils.data import DataLoader
from srdense.thinnet import densenet, L1_Charbonnier_loss
from srdense.dataset import DatasetFromfolder
from srdense.proj_utils.plot_utils import *
model_name = 'densenet_super_resolution'
loss_plot = plot_scalar(name = "loss_sr", env= model_name, rate = 1000)
# Training settings
parser = argparse.ArgumentParser(description="PyTorch DenseNet")
parser.add_argument("--batch_size", type=int, default=32, help="training batch size")
parser.add_argument("--nEpochs", type=int, default=100, help="number of epochs to train for")
parser.add_argument("--lr", type=float, default=1e-3, help="Learning Rate. Default=1e-4")
parser.add_argument("--step", type=int, default=30, help="Sets the learning rate to the initial LR decayed by momentum every n epochs, Default: n=10")
parser.add_argument("--cuda", default=True, action="store_false", help="Use cuda?")
parser.add_argument("--resume", default=True, help="Path to checkpoint (default: none)")
parser.add_argument("--reload_epoch", default=24, type=int, help="Manual epoch number (useful on restarts)")
parser.add_argument("--start_epoch", default=1, type=int, help="Manual epoch number (useful on restarts)")
parser.add_argument("--threads", type=int, default=1, help="Number of threads for data loader to use, Default: 1")
parser.add_argument("--momentum", default=0.9, type=float, help="Momentum, Default: 0.9")
parser.add_argument("--weight-decay", "--wd", default=1e-4, type=float, help="weight decay, Default: 1e-4")
parser.add_argument("--pretrained", default="", type=str, help="path to pretrained model (default: none)")
parser.add_argument("--save_freq", default=1, type=int, help="save frequency")
model_folder = os.path.join('model_adam', 'dense')
if not os.path.exists(model_folder):
os.mkdir(model_folder)
def main():
global opt, model
opt = parser.parse_args()
print(opt)
cuda = opt.cuda
if cuda and not torch.cuda.is_available():
raise Exception("No GPU found, please run without --cuda")
opt.seed = random.randint(1, 10000)
print(("Random Seed: ", opt.seed))
torch.manual_seed(opt.seed)
if cuda:
import torch.backends.cudnn as cudnn
torch.cuda.manual_seed(opt.seed)
cudnn.benchmark = True
print("===> Building model")
model = densenet()
criterion = L1_Charbonnier_loss()
print("===> Setting GPU")
if cuda:
model = model.cuda()
criterion = criterion.cuda()
# optionally resume from a checkpoint
if opt.resume is True:
model_path = os.path.join(model_folder, 'model_epoch_{}.pth'.format(opt.reload_epoch))
if os.path.isfile(model_path):
print(("=> loading checkpoint '{}'".format(model_path)))
model_state = torch.load(model_path)
model.load_state_dict(model_state)
else:
print(("=> no checkpoint found at '{}'".format(opt.resume)))
opt.start_epoch = opt.reload_epoch + 1
print("===> Setting Optimizer")
optimizer = optim.Adam(model.parameters(), lr=opt.lr)
print("===> Training")
model.train()
print("===> Loading datasets")
#train_set = DatasetFromHdf5("/path/to/your/dataset/like/imagenet_50K.h5")
home = os.path.expanduser('~')
hd_folder = os.path.join(home, 'DataSet', 'SR_DATA', 'HD')
#hd_folder = os.path.join('data', 'HD')
training_data_loader = DatasetFromfolder(hd_folder, batch_size = opt.batch_size, img_size = 256)
for epoch in range(opt.start_epoch, opt.nEpochs + 1):
#train(training_data_loader, optimizer, model, criterion, epoch)
# lr = adjust_learning_rate(optimizer, epoch-1)
lr = opt.lr * (0.1 ** (( epoch-1) // opt.step))
for param_group in optimizer.param_groups:
param_group["lr"] = lr
print("epoch =", epoch,"lr =",optimizer.param_groups[0]["lr"])
for iteration in range(training_data_loader.epoch_iteration):
batch_data = training_data_loader.get_next()
inputs = Variable(torch.from_numpy(batch_data[0]), requires_grad=False)
label = Variable(torch.from_numpy(batch_data[1]), requires_grad=False)
if opt.cuda:
inputs = inputs.cuda()
label = label.cuda()
#print(inputs.size())
out = model(inputs)
loss = criterion(out, label)
optimizer.zero_grad()
loss.backward()
optimizer.step()
loss_plot.plot(loss.cpu().data.numpy()[0])
if iteration%100 == 0:
print(("===> Epoch[{}]({}/{}): Loss: {:.10f}".format(epoch, iteration, training_data_loader.epoch_iteration, loss.data[0])))
#print("total gradient", total_gradient(model.parameters()))
reg_img_np = batch_data[0][0:1]
hd_img_np = batch_data[1][0:1]
recoverd_img_np = out.data.cpu().numpy()[0:1]
img_disply = [reg_img_np, hd_img_np, recoverd_img_np]
returned_img = save_images(img_disply, save_path=None, save=False, dim_ordering='th')
plot_img(X=returned_img, win='reg_hd_recovered', env=model_name)
# save the checkpoints every epoch
if epoch > 0 and epoch % opt.save_freq == 0:
torch.save(model.state_dict(), os.path.join(model_folder, 'model_epoch_{}.pth'.format(epoch)))
print('save weights at {}'.format(model_folder))
#def train(training_data_loader, optimizer, model, criterion, epoch):
if __name__ == "__main__":
main()