-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
216 lines (194 loc) · 8.93 KB
/
main.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
import os
import sys
import glob
import argparse
from tqdm import tqdm
parser = argparse.ArgumentParser(description='Train and/or evaluate WMn --> CSFn model.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--name', dest='name', default='model', help='Model name to save/load')
parser.add_argument('--mode', dest='mode', default=None, choices=['train-discriminator', 'train-paired-discriminator', 'train-generator', 'eval-discriminator', 'eval-generator', 'batch-eval'])
parser.add_argument('--identity-mode', dest='identity_mode', default=None, choices=['csfn', 'wmn'], help='Train on identity transformation')
parser.add_argument('--epochs', dest='epochs', type=int, default=10, help='Epochs to train for, if training')
parser.add_argument('--inverse', dest='inverse', action='store_true', help='Train inverse direction (CSFn --> WMn)')
parser.add_argument('--input-mode', dest='input_mode', default='full', help='Whether to use full or stripped volumes', choices=['full', 'stripped_wmn', 'stripped_csfns'])
parser.add_argument('--gpu', dest='gpu', default=None, help='Passed to CUDA_VISIBLE_DEVICES')
parser.add_argument('--seg', dest='seg', action='store_true', help='Segmentation model')
parser.add_argument('--perceptual-loss', dest='perceptual_loss', action='store_true', help='Custom perceptual loss for training')
parser.add_argument('--vgg-perceptual-loss', dest='vgg_perceptual_loss', action='store_true', help='VGG16-based perceptual loss for training')
parser.add_argument('--gan', dest='gan', action='store_true', help='Train a GAN.')
parser.add_argument('--gan-discriminator-name', dest='discriminator_name', default=None, help='Pretrained discriminator for GAN architecture')
parser.add_argument('--gan-generator-name', dest='generator_name', default=None, help='Pretrained generator for GAN architecture')
parser.add_argument('--lr', dest='lr', type=float, default=0.001, help='Adam learning rate')
parser.add_argument('--no-normalize', dest='normalize', action='store_false', help=argparse.SUPPRESS)
parser.add_argument('--single-gpu', dest='multi_gpu', action='store_false', help=argparse.SUPPRESS)
parser.add_argument('--n-volumes', dest='n_volumes', type=int, default=60, help=argparse.SUPPRESS)
parser.add_argument('--dynamic-load', dest='preload_data', action='store_false', help=argparse.SUPPRESS)
parser.add_argument('--in', dest='infile', default=None, help='Volume to load for generator evaluation')
parser.add_argument('--out', dest='outfile', default=None, help='Output save path for generator evaluation')
parser.add_argument('--continue-training', dest='continue_training', action='store_true', help='writeme')
parser.add_argument('--continue-training-from', dest='continue_training_from', default=False, help='writeme')
parser.add_argument('--viz', dest='viz', action='store_true', help='writeme')
args = parser.parse_args()
if args.gpu is not None:
os.environ['CUDA_VISIBLE_DEVICES'] = str(args.gpu)
import tensorflow as tf
from tensorflow.distribute import MirroredStrategy
import visualkeras
from data.datagen import (
set_formats,
paired_generator,
corrupted_with_wmn_generator,
eval_generator,
paired_with_corruption_generator
)
from data.seg_datagen import seg_generator
from models.slice_generator import SliceGenerator
from models.slice_seg_generator import SliceSegGenerator
from models.slice_discriminator import SliceDiscriminator
from models.slice_with_slab_discriminator import SliceWithSlabDiscriminator
from models.slice_gan import SliceGAN
set_formats(args.input_mode)
def viz():
model = SliceSegGenerator().model
visualkeras.graph_view(model).show()
def train_gan():
if args.discriminator_name:
discriminator = SliceWithSlabDiscriminator(name=args.discriminator_name, load=True)
else:
discriminator = SliceWithSlabDiscriminator(name='discriminator')
if args.generator_name:
generator = SliceGenerator(name=args.generator_name, load=True)
else:
generator = SliceGenerator(name='generator')
model = SliceGAN(generator, discriminator, name=args.name)
generator, batches_per_epoch = paired_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
batch_size=12,
n_volumes=args.n_volumes
)
model.train(generator, batches_per_epoch, epochs=args.epochs)
def evaluate_generator():
if args.seg:
model = SliceSegGenerator(name=args.name, load=True)
else:
model = SliceGenerator(name=args.name, load=True)
if args.infile and args.outfile:
model.convert_from_path(args.infile, out_path=args.outfile)
def train_generator(batch_size=10):
if args.seg:
model = SliceSegGenerator(
name=args.name,
lr=args.lr,
load=args.continue_training,
continue_from=args.continue_training_from,
)
generator, batches_per_epoch, weights = seg_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
batch_size=batch_size,
n_volumes=args.n_volumes,
)
model.train(generator, batches_per_epoch, weights, epochs=args.epochs)
else:
model = SliceGenerator(
name=args.name,
vgg_perceptual_loss=args.vgg_perceptual_loss,
perceptual_loss=args.perceptual_loss,
lr=args.lr,
load=args.continue_training,
continue_from=args.continue_training_from,
)
generator, batches_per_epoch = paired_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
batch_size=batch_size,
n_volumes=args.n_volumes,
inverse=args.inverse,
identity=args.identity_mode,
)
model.train(generator, batches_per_epoch, epochs=args.epochs)
def batch_eval():
if args.seg:
model = SliceSegGenerator(name=args.name, load=True)
else:
model = SliceGenerator(name=args.name, load=True)
paths = glob.glob(args.infile)
for subj_infile in tqdm(paths):
subj_path = os.path.dirname(subj_infile)
subj_outfile = os.path.join(subj_path, args.outfile)
# if not os.path.exists(subj_outfile):
model.convert_from_path(subj_infile, subj_outfile)
def train_discriminator(batch_size):
model = SliceDiscriminator(
name=args.name,
perceptual_loss=args.perceptual_loss,
lr=args.lr)
generator, batches_per_epoch = corrupted_with_wmn_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
batch_size=batch_size,
_normalize=args.normalize,
n_volumes=args.n_volumes)
model.train(generator, batches_per_epoch, epochs=args.epochs)
def train_paired_discriminator(batch_size=20):
model = SliceWithSlabDiscriminator(
name=args.name,
lr=args.lr)
generator, batches_per_epoch = paired_with_corruption_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
batch_size=batch_size,
n_volumes=args.n_volumes)
model.train(generator, batches_per_epoch, epochs=args.epochs)
def evaluate_discriminator():
model = SliceDiscriminator(name=args.name, load=True)
csfn_generator, csfn_batches_per_epoch = eval_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
'csfn',
batch_size=50,
n_volumes=args.n_volumes)
wmn_generator, wmn_batches_per_epoch = eval_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
'wmn',
batch_size=50,
n_volumes=args.n_volumes)
csfn_corrupted_generator, csfn_corr_batches_per_epoch = eval_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
'csfn',
corrupted=True,
batch_size=50,
n_volumes=args.n_volumes)
wmn_corrupted_generator, wmn_corr_batches_per_epoch = eval_generator(
'/data/mradovan/7T_WMn_3T_CSFn_pairs',
'wmn',
corrupted=True,
batch_size=50,
n_volumes=args.n_volumes)
print('Evaluating on csfn and csfn_corrupted')
model.eval(csfn_generator, csfn_batches_per_epoch, csfn_corrupted_generator, csfn_corr_batches_per_epoch)
print('Evaluating on wmn and wmn_corrupted')
model.eval(wmn_generator, wmn_batches_per_epoch, wmn_corrupted_generator, wmn_corr_batches_per_epoch)
num_gpus = 4
if args.gpu:
num_gpus = len(args.gpu.split(','))
def main(multi_gpu=True):
if args.viz:
viz()
elif args.mode == 'eval-generator':
evaluate_generator()
elif args.mode == 'eval-discriminator':
evaluate_discriminator()
elif args.mode == 'train-generator':
if args.gan:
train_gan()
else:
train_generator(batch_size=12 if num_gpus > 1 else 10)
elif args.mode == 'train-discriminator':
train_discriminator(batch_size=20)
elif args.mode == 'train-paired-discriminator':
train_paired_discriminator()
elif args.mode == 'batch-eval':
batch_eval()
else:
print('Command not recognized.')
if __name__ == '__main__':
if args.multi_gpu:
with MirroredStrategy().scope():
main()
else:
main(multi_gpu=False)