-
Notifications
You must be signed in to change notification settings - Fork 5
/
predict_unet.py
74 lines (54 loc) · 1.83 KB
/
predict_unet.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
import argparse
import numpy as np
import tensorflow as tf
from mrtoct import ioutil, model
# global maximum in MR volumes
INPUTS_MAX = 5200
TARGETS_MAX = 3700
def predict(inputs_path, outputs_path, checkpoint_path, params):
encoder = ioutil.TFRecordEncoder()
options = ioutil.TFRecordOptions
estimator = tf.estimator.Estimator(
model_fn=model.cnn_model_fn,
params=params)
def create_input_fn(offset):
def input_fn():
inputs = model.predict_slice_input_fn(
offset=offset,
inputs_div=INPUTS_MAX,
slice_shape=params.slice_shape,
inputs_path=inputs_path)
return {'inputs': inputs}
return input_fn
with tf.python_io.TFRecordWriter(outputs_path, options) as writer:
offset = 0
while True:
predictions = estimator.predict(
input_fn=create_input_fn(offset),
checkpoint_path=checkpoint_path)
outputs = [p['outputs'] for p in predictions]
if len(outputs) == 0:
break
else:
offset += 1
volume = np.stack(outputs, axis=0)
volume *= TARGETS_MAX
writer.write(encoder.encode(volume))
def main(args):
tf.logging.set_verbosity(tf.logging.INFO)
hparams = tf.contrib.training.HParams(
data_format='channels_last',
slice_shape=[384, 384],
generator_fn=model.unet.generator_fn)
hparams.parse(args.hparams)
predict(inputs_path=args.inputs_path,
outputs_path=args.outputs_path,
checkpoint_path=args.checkpoint_path,
params=hparams)
if __name__ == '__main__':
parser = argparse.ArgumentParser('predict')
parser.add_argument('--inputs-path', required=True)
parser.add_argument('--outputs-path', required=True)
parser.add_argument('--checkpoint-path', required=True)
parser.add_argument('--hparams', type=str, default='')
main(parser.parse_args())