-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconstruction_module_train.py
227 lines (170 loc) · 9.17 KB
/
reconstruction_module_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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
'''
Reconstruction Extraction Module training code
'''
from itertools import count
import os
from turtle import width
import torch
import torch.utils.data
from skimage import io, transform
import numpy as np
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
from torchvision import transforms, utils
from PIL import Image
import torch.optim as optim
import torch.nn as nn
from tqdm import tqdm
from tools.pytorchtools import EarlyStopping
import re
from tools import dataloaders_reconstruction
from models.Reconstruction_module.reconst_module import reconstruction_module
import argparse
# GPU check
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
print(torch.cuda.is_available())
print(device)
np.random.seed(0)
torch.manual_seed(0)
if torch.cuda.is_available():
torch.cuda.manual_seed(0)
def get_args():
parser = argparse.ArgumentParser(description='sample',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--batch', type=int, default=8, help='Batch size')
parser.add_argument('--epoch', type=int, default=10000, help='Number of epoch')
parser.add_argument('--input1', type=str, default='./sample_data/text_train', help='input(text image) path')
parser.add_argument('--input2', type=str, default='./sample_data/background_train', help='input(background) path')
parser.add_argument('--label', type=str, default='./sample_data/scene_train', help='label path')
parser.add_argument('--img_size', type=int, default=512, help='Image size')
parser.add_argument('--early_stopping', type=int, default=50, help='Early stopping epoch')
return parser.parse_args()
# validation
def validate(net, dataloader, dataset, criterion, epoch, save_dir_input, save_dir_input2, save_dir_label, save_dir_output):
net.eval()
with torch.no_grad():
total_loss = 0.0
i = 0
if ((epoch+1)%100) == 0:
new_save_dir_input = save_dir_input + '/epoch' + str(epoch+1)
new_save_dir_input2 = save_dir_input2 + '/epoch' + str(epoch+1)
new_save_dir_label = save_dir_label + '/epoch' + str(epoch+1)
new_save_dir_output = save_dir_output + '/epoch' + str(epoch+1)
os.makedirs(new_save_dir_input, exist_ok=True)
os.makedirs(new_save_dir_input2, exist_ok=True)
os.makedirs(new_save_dir_label, exist_ok=True)
os.makedirs(new_save_dir_output, exist_ok=True)
for data in dataloader:
inputs, inputs2, labels = data
inputs = inputs.to(device)
inputs2 = inputs2.to(device)
labels = labels.to(device)
outputs = net(inputs, inputs2)
loss = criterion(outputs, labels)
total_loss+=loss.item()*inputs.shape[0]
if ((epoch+1)%100) == 0:
img_name_input = new_save_dir_input +'/input_back_image' + str(i) + '.png'
img_name_input2 = new_save_dir_input2 +'/input_scene_image' + str(i) + '.png'
img_name_label = new_save_dir_label + '/label_text_image' + str(i) + '.png'
img_name_output = new_save_dir_output + '/output_image' + str(i) + '.png'
utils.save_image(inputs, img_name_input, normalize=True)
utils.save_image(inputs2, img_name_input2, normalize=True)
utils.save_image(labels, img_name_label, normalize=True)
utils.save_image(outputs, img_name_output, normalize=True)
i+=1
avg_loss = total_loss / len(dataset)
return avg_loss
# training
def train(net, traindataloader, traindataset, criterion, optimizer, epochs, save_dir_input_train, save_dir_input2_train, save_dir_label_train, save_dir_output_train,
valdataloader, valdataset, save_dir_input_val, save_dir_input2_val, save_dir_label_val, save_dir_output_val, early_stopping):
train_loss_history = []
val_loss_history = []
epoch_history = []
i = 0
for epoch in (range(epochs)):
loss_item = 0
print("Now epoch : %d/%d" %(epoch,epochs))
if ((epoch+1)%100) == 0:
new_save_dir_input_train = save_dir_input_train + '/epoch' + str(epoch+1)
new_save_dir_input2_train = save_dir_input2_train + '/epoch' + str(epoch+1)
new_save_dir_label_train = save_dir_label_train + '/epoch' + str(epoch+1)
new_save_dir_output_train = save_dir_output_train + '/epoch' + str(epoch+1)
os.makedirs(new_save_dir_input_train, exist_ok=True)
os.makedirs(new_save_dir_input2_train, exist_ok=True)
os.makedirs(new_save_dir_label_train, exist_ok=True)
os.makedirs(new_save_dir_output_train, exist_ok=True)
net.train()
for data in tqdm(traindataloader, leave=False):
inputs, inputs2, labels = data
inputs = inputs.to(device)
inputs2 = inputs2.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = net(inputs, inputs2)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
loss_item+=loss.item()*inputs.shape[0]
if ((epoch+1)%100) == 0:
img_name_input = new_save_dir_input_train +'/input_image' + str(i) + '.png'
img_name_input2 = new_save_dir_input2_train +'/input_image' + str(i) + '.png'
img_name_label = new_save_dir_label_train + '/label_image' + str(i) + '.png'
img_name_outputs = new_save_dir_output_train + '/output_in_image' + str(i) + '.png'
utils.save_image(inputs, img_name_input, normalize=True)
utils.save_image(inputs2, img_name_input2, normalize=True)
utils.save_image(labels, img_name_label, normalize=True)
utils.save_image(outputs, img_name_outputs, normalize=True)
i+=1
avg_loss_train = loss_item/len(traindataset)
train_loss_history.append(avg_loss_train)
epoch_history.append(epoch)
loss_val = validate(net, valdataloader, valdataset, criterion, epoch, save_dir_input_val, save_dir_input2_val, save_dir_label_val, save_dir_output_val)
val_loss_history.append(loss_val)
early_stopping(loss_val, net)
if early_stopping.early_stop:
break
print('Finished Training')
return train_loss_history, val_loss_history, epoch_history, epochs
if __name__ == '__main__':
args = get_args()
input_root_dir = args.input1
input2_root_dir = args.input2
label_root_dir = args.label
im_list = dataloaders_reconstruction.pair(input_root_dir)
target_size = args.img_size
dataset = dataloaders_reconstruction.SynthTextDataset(im_list, input_root_dir, input2_root_dir, label_root_dir, target_size, dataloaders_reconstruction.resize_w_pad)
n_train = int(len(dataset)*0.875)
n_val = len(dataset) - n_train
traindataset, valdataset = torch.utils.data.random_split(dataset, [n_train, n_val])
traindataloader = DataLoader(dataset=traindataset, batch_size = args.batch, shuffle=True, num_workers=4)
valdataloader = DataLoader(dataset=valdataset, batch_size = args.batch, shuffle=True, num_workers=4)
net = reconstruction_module(n_channels=7, n_classes=3)
net.to(device)
save_dir_input_train = './train_val_output/reconstruction_module/train/input'
save_dir_input2_train = './train_val_output/reconstruction_module/train/input2'
save_dir_label_train = './train_val_output/reconstruction_module/train/label'
save_dir_output_train = './train_val_output/reconstruction_module/train/output'
save_dir_input_val = './train_val_output/reconstruction_module/validation/input'
save_dir_input2_val = './train_val_output/reconstruction_module/validation/input2'
save_dir_label_val = './train_val_output/reconstruction_module/validation/label'
save_dir_output_val = './train_val_output/reconstruction_module/validation/output'
criterion = nn.MSELoss()
optimizer = optim.Adam(net.parameters(), lr=0.0001, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
epochs = args.epoch
checkpoint = './train_model/reconstruction_module'
os.makedirs(checkpoint, exist_ok=True)
checkpoint_name = checkpoint + '/checkpoint_model.pth'
early_stopping = EarlyStopping(patience=args.early_stopping, verbose=True, path=checkpoint_name)
train_loss_history, val_loss_history, epoch_history, epochs = train(net, traindataloader, traindataset, criterion, optimizer, epochs,
save_dir_input_train, save_dir_input2_train, save_dir_label_train, save_dir_output_train,
valdataloader, valdataset, save_dir_input_val, save_dir_input2_val, save_dir_label_val, save_dir_output_val, early_stopping)
fig1 = plt.figure()
plt.plot(epoch_history, train_loss_history, label='train')
plt.plot(epoch_history, val_loss_history, label='val')
plt.legend()
plt.grid()
plt.xlabel('epoch')
plt.title("loss")
graph_path = './graph/reconstruction_module.png'
os.makedirs('./graph', exist_ok=True)
fig1.savefig(graph_path)