-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtrain_image_regression_3d.py
140 lines (109 loc) · 5.45 KB
/
train_image_regression_3d.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
import os
import argparse
import shutil
import torch
import torch.nn as nn
import torchvision
import torchvision.utils as vutils
import torch.backends.cudnn as cudnn
import tensorboardX
import numpy as np
from tqdm import tqdm
from networks import Positional_Encoder, FFN, SIREN
from utils import get_config, prepare_sub_folder, get_data_loader, ct_parallel_project_2d_batch, save_image_3d
parser = argparse.ArgumentParser()
parser.add_argument('--config', type=str, default='', help='Path to the config file.')
parser.add_argument('--output_path', type=str, default='.', help="outputs path")
# Load experiment setting
opts = parser.parse_args()
config = get_config(opts.config)
max_iter = config['max_iter']
cudnn.benchmark = True
# Setup output folder
output_folder = os.path.splitext(os.path.basename(opts.config))[0]
model_name = os.path.join(output_folder, config['data'] + '/img{}_{}_{}_{}_{}_{}_lr{:.2g}_encoder_{}' \
.format(config['img_size'], config['model'], \
config['net']['network_input_size'], config['net']['network_width'], \
config['net']['network_depth'], config['loss'], config['lr'], config['encoder']['embedding']))
if not(config['encoder']['embedding'] == 'none'):
model_name += '_scale{}_size{}'.format(config['encoder']['scale'], config['encoder']['embedding_size'])
print(model_name)
train_writer = tensorboardX.SummaryWriter(os.path.join(opts.output_path + "/logs", model_name))
output_directory = os.path.join(opts.output_path + "/outputs", model_name)
checkpoint_directory, image_directory = prepare_sub_folder(output_directory)
shutil.copy(opts.config, os.path.join(output_directory, 'config.yaml')) # copy config file to output folder
# Setup input encoder:
encoder = Positional_Encoder(config['encoder'])
# Setup model
if config['model'] == 'SIREN':
model = SIREN(config['net'])
elif config['model'] == 'FFN':
model = FFN(config['net'])
else:
raise NotImplementedError
model.cuda()
model.train()
# Setup optimizer
if config['optimizer'] == 'Adam':
optim = torch.optim.Adam(model.parameters(), lr=config['lr'], betas=(config['beta1'], config['beta2']), weight_decay=config['weight_decay'])
else:
NotImplementedError
# Setup loss functions
if config['loss'] == 'L2':
loss_fn = torch.nn.MSELoss()
elif config['loss'] == 'L1':
loss_fn = torch.nn.L1Loss()
else:
NotImplementedError
# Setup data loader
print('Load image: {}'.format(config['img_path']))
data_loader = get_data_loader(config['data'], config['img_path'], config['img_size'], img_slice=None, train=True, batch_size=config['batch_size'])
config['img_size'] = (config['img_size'], config['img_size'], config['img_size']) if type(config['img_size']) == int else tuple(config['img_size'])
slice_idx = list(range(0, config['img_size'][0], int(config['img_size'][0]/config['display_image_num'])))
for it, (grid, image) in enumerate(data_loader):
# Input coordinates (x, y, z) grid and target image
grid = grid.cuda() # [bs, c, h, w, 3], [0, 1]
image = image.cuda() # [bs, c, h, w, 1], [0, 1]
# Data loading
# Change training inputs for downsampling image
test_data = (grid, image)
train_data = (grid, image)
save_image_3d(test_data[1], slice_idx, os.path.join(image_directory, "test.png"))
save_image_3d(train_data[1], slice_idx, os.path.join(image_directory, "train.png"))
# Train model
for iterations in range(max_iter):
model.train()
optim.zero_grad()
train_embedding = encoder.embedding(train_data[0]) # [B, C, H, W, embedding*2]
train_output = model(train_embedding) # [B, C, H, W, 1]
train_loss = 0.5 * loss_fn(train_output, train_data[1])
train_loss.backward()
optim.step()
# Compute training psnr
if (iterations + 1) % config['log_iter'] == 0:
train_psnr = -10 * torch.log10(2 * train_loss).item()
train_loss = train_loss.item()
train_writer.add_scalar('train_loss', train_loss, iterations + 1)
train_writer.add_scalar('train_psnr', train_psnr, iterations + 1)
print("[Iteration: {}/{}] Train loss: {:.4g} | Train psnr: {:.4g}".format(iterations + 1, max_iter, train_loss, train_psnr))
# Compute testing psnr
if (iterations + 1) % config['val_iter'] == 0:
model.eval()
with torch.no_grad():
test_embedding = encoder.embedding(test_data[0])
test_output = model(test_embedding)
test_loss = 0.5 * loss_fn(test_output, test_data[1])
test_psnr = - 10 * torch.log10(2 * test_loss).item()
test_loss = test_loss.item()
train_writer.add_scalar('test_loss', test_loss, iterations + 1)
train_writer.add_scalar('test_psnr', test_psnr, iterations + 1)
# Must transfer to .cpu() tensor firstly for saving images
save_image_3d(test_output, slice_idx, os.path.join(image_directory, "recon_{}_{:.4g}dB.png".format(iterations + 1, test_psnr)))
print("[Validation Iteration: {}/{}] Test loss: {:.4g} | Test psnr: {:.4g}".format(iterations + 1, max_iter, test_loss, test_psnr))
if (iterations + 1) % config['image_save_iter'] == 0:
# Save final model
model_name = os.path.join(checkpoint_directory, 'model_%06d.pt' % (iterations + 1))
torch.save({'net': model.state_dict(), \
'enc': encoder.B, \
'opt': optim.state_dict(), \
}, model_name)