-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
202 lines (162 loc) · 6.66 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision.datasets as dsets
import torchvision.transforms as transforms
from torch.autograd import Variable
import argparse
from tqdm import tqdm
import scipy as sp
from PIL import Image
import numpy as np
import os
from datetime import datetime as dt
from model import Model, ResidualBlock
from loss import loss_function
from data_loader import get_loader
import pair_transforms
from settings import *
def main(args):
tdatetime = dt.now()
train_date = tdatetime.strftime('%Y%m%d')
train_log_file = open(os.path.join(args.save_dir, 'train_{}.txt'.format(train_date)), 'w')
val_log_file = open(os.path.join(args.save_dir, 'val_{}.txt'.format(train_date)), 'w')
if not os.path.exists(args.save_dir):
os.makedirs(args.save_dir)
with torch.cuda.device(args.gpu_device_num):
CASENet = Model(ResidualBlock, [3, 4, 23, 3], class_num)
CASENet.cuda()
train_loader = get_loader(img_root=args.train_image_dir,
mask_root=args.train_mask_dir,
json_path=args.train_json_path,
pair_transform=pair_transform,
input_transform=input_transform,
target_transform=None,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers)
val_loader = get_loader(img_root=args.val_image_dir,
mask_root=args.val_mask_dir,
json_path=args.val_json_path,
pair_transform=val_pair_transform,
input_transform=input_transform,
target_transform=None,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.num_workers)
lr = args.learning_rate
optimizer = torch.optim.SGD(CASENet.parameters(), lr=lr, momentum=0.9, weight_decay=0.0005)
loss_latest = 0
batch_batch_count = 0
# Training
for epoch in tqdm(range(args.epochs)):
if args.batch_batch:
"""
using mini-batch in mini-batch
"""
train_loss_total = 0
train_prog = tqdm(enumerate(train_loader), total=len(train_loader))
for i, (images, masks) in train_prog:
images = Variable(images).cuda()
masks = Variable(masks).cuda()
optimizer.zero_grad()
fused_output, side_output = CASENet(images)
# actually, in the edge detection, we need set the weight, witch is none edge pix rate.
loss_side = loss_function(side_output, masks)
loss_fuse = loss_function(fused_output, masks)
loss = loss_side+loss_fuse
if batch_batch_count < args.batch_batch_size:
batch_batch_count += 1
continue
else:
batch_batch_count = 0
train_loss_total += loss.data[0]
loss.data[0] /= args.batch_batch_size
loss.backward()
optimizer.step()
train_prog.set_description("batch loss : {:.5}".format(loss.data[0]))
torch.save(CASENet.state_dict(), args.save_dir+'CASENet_param_{}.pkl'.format(epoch))
else:
"""
usual training
"""
train_loss_total = 0
train_prog = tqdm(enumerate(train_loader), total=len(train_loader))
for i, (images, masks) in train_prog:
images = Variable(images).cuda()
masks = Variable(masks).cuda()
optimizer.zero_grad()
fused_output, side_output = CASENet(images)
# actually, in the edge detection, we need set the weight, witch is none edge pix rate.
loss_side = loss_function(side_output, masks)
loss_fuse = loss_function(fused_output, masks)
loss = loss_side+loss_fuse;
train_loss_total += loss.data[0]
loss.backward()
optimizer.step()
train_prog.set_description("batch loss : {:.5}".format(loss.data[0]))
torch.save(CASENet.state_dict(), args.save_dir+'CASENet_param_{}.pkl'.format(epoch))
# Decaying Learning Rate
if (epoch+1) % 30 == 0:
lr /= 10
optimizer = torch.optim.SGD(CASENet.parameters(), lr=lr, momentum=0.9)
#print("train loss [epochs {0}/{1}]: {2}".format( epoch, args.epochs,train_loss_total))
train_log_file.write("{}".format(train_loss_total))
train_log_file.flush()
val_prog = tqdm(enumerate(val_loader), total=len(val_loader))
CASENet.eval()
val_loss_total=0
for i, (images, masks) in val_prog:
images = Variable(images).cuda()
masks = Variable(masks).cuda()
fused_output, side_output = CASENet(images)
# actually, in the edge detection, we need set the weight, witch is none edge pix rate.
loss_side = loss_function(side_output, masks)
loss_fuse = loss_function(fused_output, masks)
loss = loss_side+loss_fuse;
val_loss_total += loss.data[0]
val_prog.set_description("validation batch loss : {:.5}".format(loss.data[0]))
if i == 0:
predic = F.log_softmax(fused_output)
predic = predic[0]
_ , ind = predic.sort(1)
ind = ind.cpu().data.numpy()
msk = masks.cpu().data.numpy()
ind = Image.fromarray(np.uint8(ind[-1]))
msk = Image.fromarray(np.uint8(msk[0]))
ind.save(args.save_dir+"output_epoch{}.png".format(epoch))
msk.save(args.save_dir+"mask_epoch{}.png".format(epoch))
#print("validation loss : {0}".format(val_loss_total))
val_log_file.write("{}".format(val_loss_total))
val_log_file.flush()
CASENet.train()
# Save the Model
torch.save(CASENet.state_dict(), 'CASENet_{0}_fin.pkl'.format(args.epochs))
log_file.close()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--train_image_dir', type=str, default='./data/train',
help='directory for train images')
parser.add_argument('--train_mask_dir', type=str, default='./data/train',
help='directory for train mask images')
parser.add_argument('--val_image_dir', type=str, default='./data/val',
help='directory for val images')
parser.add_argument('--val_mask_dir', type=str, default='./data/val',
help='directory for validation mask images')
parser.add_argument('--train_json_path', type=str, default='./data/json',
help='directory of json file for training dataset')
parser.add_argument('--val_json_path', type=str, default='./data/json',
help='directory of json file for validation dataset')
parser.add_argument('--crop_size', type=int, default=224,
help='size for image after processing')
parser.add_argument('--save_dir', type=str, default="./log/",
help='size for image after processing')
parser.add_argument('--epochs', type=int, default=50)
parser.add_argument('--batch_size', type=int, default=2)
parser.add_argument('--batch_batch_size', type=int, default=32)
parser.add_argument('--num_workers', type=int, default=4)
parser.add_argument('--learning_rate', type=float, default=0.01)
parser.add_argument('--gpu_device_num', type=int, default=0)
parser.add_argument('-batch_batch', action="store_true", default=False, help='calc in batch in batch')
args = parser.parse_args()
main(args)