-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_mse.py
267 lines (207 loc) · 7.82 KB
/
main_mse.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import torch
import torch.nn as nn
import torch.optim as optim
import torch.functional as F
import numpy as np
import tqdm
import argparse
import os
from model import Generator, Discriminator
from dataloader_efficient import *
import datetime
from itertools import cycle
import random
import gc
import skimage
from skimage.io import imsave
import resource
random.seed(1)
rlimit = resource.getrlimit(resource.RLIMIT_NOFILE)
resource.setrlimit(resource.RLIMIT_NOFILE, (4096, rlimit[1]))
parser = argparse.ArgumentParser()
parser.add_argument('--data_path', help='path to data folder', required=True)
parser.add_argument('--image_dim', type=int, help='image dimensions', required=True)
parser.add_argument('--load_prev_model_gen', help='path to previous model')
parser.add_argument('--load_prev_model_dec', help='decoder path')
parser.add_argument('--batch_size_train', type=int, help="train batch size")
parser.add_argument('--batch_size_test', type=int, help="test batch size")
parser.add_argument('--load_prev_model_disc', help='discriminator path')
parser.add_argument('--reset_files', help='reset file stats(True/False)')
parser.add_argument("--start_epoch", type=int, help="specify start epoch to continue from")
parser.add_argument("--end_epoch", type=int, help="specify end epoch to continue to")
parser.add_argument("--learning_rate_ae", type=float ,help="learning rate")
parser.add_argument("--learning_rate_color", type=float ,help="learning rate")
parser.add_argument("--test_mode", type=bool, help="run in test mode")
args = parser.parse_args()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
SAVED_MODEL_DIR = './trained_models/'
RANDOM_OUTPUTS_DIR = './rand_outputs/'
if not os.path.exists(SAVED_MODEL_DIR):
os.makedirs(SAVED_MODEL_DIR)
if not os.path.exists(RANDOM_OUTPUTS_DIR):
os.makedirs(RANDOM_OUTPUTS_DIR)
def update_readings(filename, reading):
f = open(filename, 'a')
f.writelines(reading)
f.close()
def save_model_info(g_model, d_model, DIR, epoch_start, epoch_end, learning_rate_ae, learning_rate_color, optimizer_ae, optimizer_color):
f = open(DIR + "model_info.txt", 'a')
g_model_info = 'Color Generator : \n' + str(g_model) + '\n'
d_model_info = 'Color Discriminator : \n' + str(d_model) + '\n'
metrics = 'Epoch start : {} epoch end: {} learning_rate_ae : {} learning_rate_color: {} \n'.format(str(epoch_start), str(epoch_end), str(learning_rate_ae), str(learning_rate_color)) + '\n'
optimizer_ae_str = "AE optimizer: \n " + str(optimizer_ae.state_dict()) + '\n'
optimizer_color_str = "Color optimizer: \n " + str(optimizer_color.state_dict()) + '\n'
f.writelines(g_model_info)
f.writelines(d_model_info)
f.writelines(metrics)
f.writelines(optimizer_ae_str)
f.writelines(optimizer_color_str)
f.close()
def train(g_model, d_model, learning_rate_ae, learning_rate_color, train_dataloader, test_dataloader, now):
draw_iter = 50
all_save_iter = 500
cur_save_iter = 100
if args.test_mode:
draw_iter = 1
all_save_iter = 1
cur_save_iter = 1
cur_model_dir = SAVED_MODEL_DIR + now + '/'
filenames = os.listdir(args.data_path + COLOR_DIR)
l_criteron = nn.MSELoss()
ab_criterion = nn.BCELoss()
if args.start_epoch:
start_epoch = args.start_epoch
else:start_epoch = 0
if args.end_epoch:
end_epoch = args.end_epoch
else:end_epoch = 100000
criterion_ae = nn.MSELoss()
criterion_color = nn.MSELoss()
# optimizer_ae = optim.Adam(g_model.parameters(), lr=learning_rate_ae)
optimizer = optim.Adam(g_model.parameters(), lr=learning_rate_color)
# save_model_info(g_model, cur_model_dir, start_epoch, end_epoch, learning_rate_ae, learning_rate_color, optimizer)
loader = cycle(train_dataloader)
for i in range(start_epoch, end_epoch):
# gc.collect()
g_model.train()
g_model.train_stat = True
correct = 0
x, (y_l, y_ab) = next(loader)
# x = torch.from_numpy(x)
# y_l = torch.from_numpy(y_l)
# y_ab = torch.from_numpy(y_ab)
x = x.to(device)
y_l = y_l.to(device)
y_ab = y_ab.to(device)
optimizer.zero_grad()
out_l, out_ab = g_model(x)
loss_ae = criterion_ae(out_l, y_l)
loss_color = criterion_color(out_ab, y_ab)
loss = 0.5 * loss_ae + 2.0 * loss_color
loss.backward()
optimizer.step()
value = 'Iter : %d, loss ae %.4f, Loss color %.4f, Total loss %.4f\n'%(i, loss_ae.item(), loss_color.item(), loss.item())
print(value)
update_readings(cur_model_dir + 'train_loss_batch.txt', value)
if i % draw_iter == 0:
draw_outputs(i, g_model, now, args.data_path, filenames)
if i % all_save_iter == 0:
print('..SAVING MODEL')
torch.save(g_model.state_dict(), cur_model_dir + 'colorize2gen_' + str(i) + '.pt')
print('GEN SAVED')
if i % cur_save_iter == 0:
print('SAVING MODEL')
torch.save(g_model.state_dict(), cur_model_dir + 'colorize_gen_cur.pt')
print('SAVED CURRENT')
if args.test_mode:
break
# def test_model(model, test_loader):
# model.eval()
# with torch.no_grad():
def draw_outputs(epoch, model, now, dset_path, filenames):
if not os.path.exists(RANDOM_OUTPUTS_DIR+now):
os.makedirs(RANDOM_OUTPUTS_DIR+now)
file = open(RANDOM_OUTPUTS_DIR + now + '/order.txt', 'a')
indices = []
for i in range(5):
index = random.randint(0, len(filenames)-1)
indices.append(index)
file.writelines(str(epoch) + ',' + filenames[index] + '\n')
file.close()
model.train_stat = False
model.eval()
# model.to(device)
with torch.no_grad():
images = []
base_dir = dset_path + SCAN_DIR + '/'
image_names = os.listdir(base_dir)
for index in indices:
image = cv2.imread(base_dir + filenames[index], 0)
input_image = torch.from_numpy(image).float()
input_image = input_image.unsqueeze(0)
input_image = input_image.unsqueeze(0)
input_image = input_image.to(device)
output = model(input_image)
# print(output.shape)
output = output.squeeze(0).permute(1, 2, 0)
# print(output.shape)
np_image = output.cpu().numpy()
# print(np_image.shape)
a_channel = np_image[:, :, 0]
b_channel = np_image[:, :, 1]
# print('a', a_channel)
# print('b', b_channel)
img_composed = np.dstack((image, a_channel, b_channel))
img_rgb = cv2.cvtColor(img_composed, cv2.COLOR_LAB2RGB)
# print(img_rgb.shape)
# import ipdb; ipdb.set_trace()
# exit()
file_name = (RANDOM_OUTPUTS_DIR + now + '/' + 'cimg_'+ str(epoch)+ '_' + filenames[index]).strip()
imsave(file_name.split('.png')[0] + '_mri.png', image)
imsave(file_name, img_rgb)
# exit()
def main():
now = str(datetime.datetime.now()) + '/'
cur_model_dir = SAVED_MODEL_DIR + now
os.makedirs(cur_model_dir)
if args.reset_files and (args.reset_files == 'False'):
reset = False
else:
reset = True
if reset:
print('Resetting files')
# f = open(cur_model_dir + '/'+ 'train_loss_avg.txt','w')
# f.writelines('')
# f.close()
f = open(cur_model_dir + '/'+ 'train_loss_batch.txt', 'w')
f.writelines('')
f.close()
f = open(cur_model_dir + '/'+ 'test_loss_avg.txt','w')
f.writelines('')
f.close()
else:
print('no reset , appending to former data')
if args.learning_rate_ae:
learning_rate_ae = args.learning_rate_ae
else:
learning_rate_ae = 4e-3
if args.learning_rate_color:
learning_rate_color = args.learning_rate_color
else:
learning_rate_color = 3e-3
batch_size_train = 5
batch_size_test = 5
if args.batch_size_train:
batch_size_train = args.batch_size_train
if args.batch_size_test:
batch_size_test = args.batch_size_test
X_train, X_test, y_train, y_test = generate_train_test_split(args.data_path)
train_dataloader = create_dataloader(args.data_path, X_train, y_train, batch_size_train)
test_dataloader = create_dataloader(args.data_path, X_test, y_test, batch_size_test)
g_model = Generator()
d_model = Discriminator(args.image_dim)
g_model = g_model.to(device)
d_model = d_model.to(device)
train(g_model, d_model, learning_rate_ae, learning_rate_color, train_dataloader, test_dataloader, now)
if __name__ == '__main__':
main()