|
a |
|
b/options/base_options.py |
|
|
1 |
import os |
|
|
2 |
import argparse |
|
|
3 |
import tensorflow |
|
|
4 |
|
|
|
5 |
class BaseOptions(): |
|
|
6 |
|
|
|
7 |
def __init__(self): |
|
|
8 |
self.initialized = False |
|
|
9 |
|
|
|
10 |
def initialize(self, parser): |
|
|
11 |
"""This class defines options used during both training and test time. |
|
|
12 |
""" |
|
|
13 |
parser.add_argument('--dataroot', type=str, default='./datasets/sample_nifti_4D', help='path to data') |
|
|
14 |
parser.add_argument('--dataformat', type=str, default='NIFTI', help='data format (e.g., NIFTI, DICOM)') |
|
|
15 |
parser.add_argument('--max_dataset_size', type=float, default=float("inf"), help=' ') |
|
|
16 |
parser.add_argument('--gpu_ids', type=str, default='0', help='gpu ids: e.g. 0 0,1,2, 0,2, -1 for CPU mode') |
|
|
17 |
|
|
|
18 |
# data parameters |
|
|
19 |
parser.add_argument('--preprocess_carson', type=str, default='reshape_to_carson', help=' ') |
|
|
20 |
parser.add_argument('--preprocess_carmen', type=str, default='reshape_to_carmen', help=' ') |
|
|
21 |
parser.add_argument('--preprocess', type=str, default='crop_zscore', help=' ') |
|
|
22 |
parser.add_argument('--image_shape', type=tuple, default=(128,128,1), help=' ') |
|
|
23 |
parser.add_argument('--volume_shape', type=tuple, default=(128,128,16,1), help=' ') |
|
|
24 |
parser.add_argument('--nlabels', type=int, default=4, help='number of tissue classes') |
|
|
25 |
|
|
|
26 |
# data parameters |
|
|
27 |
parser.add_argument('--order', type=int, default=1, help='resampling order') |
|
|
28 |
parser.add_argument('--mode', type=str, default='nearest', help='resampling mode') |
|
|
29 |
parser.add_argument('--in_plane_resolution_mm', type=float, default=1.25, help='resample to in_plane_resolution_mm') |
|
|
30 |
parser.add_argument('--number_of_slices', type=int, default=None, help='resample to number_of_slices') |
|
|
31 |
parser.add_argument('--slice_thickness_mm', type=float, default=None, help='resample to slice_thickness_mm') |
|
|
32 |
|
|
|
33 |
# special tasks |
|
|
34 |
self.initialized = True |
|
|
35 |
return parser |
|
|
36 |
|
|
|
37 |
def gather_options(self): |
|
|
38 |
"""Initialize our parser with basic options(only once). |
|
|
39 |
Add additional model-specific and dataset-specific options. |
|
|
40 |
These options are defined in the <modify_commandline_options> function |
|
|
41 |
in model and dataset classes. |
|
|
42 |
""" |
|
|
43 |
if not self.initialized: # check if it has been initialized |
|
|
44 |
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) |
|
|
45 |
parser = self.initialize(parser) |
|
|
46 |
|
|
|
47 |
# get the basic options |
|
|
48 |
opt, _ = parser.parse_known_args() |
|
|
49 |
|
|
|
50 |
# save and return the parser |
|
|
51 |
self.parser = parser |
|
|
52 |
return parser.parse_args() |
|
|
53 |
|
|
|
54 |
def print_options(self, opt): |
|
|
55 |
"""Print and save options |
|
|
56 |
It will print both current options and default values(if different). |
|
|
57 |
It will save options into a text file / [checkpoints_dir] / opt.txt |
|
|
58 |
""" |
|
|
59 |
message = '' |
|
|
60 |
message += '----------------- Options ---------------\n' |
|
|
61 |
for k, v in sorted(vars(opt).items()): |
|
|
62 |
comment = '' |
|
|
63 |
default = self.parser.get_default(k) |
|
|
64 |
if v != default: |
|
|
65 |
comment = '\t[default: %s]' % str(default) |
|
|
66 |
message += '{:>25}: {:<30}{}\n'.format(str(k), str(v), comment) |
|
|
67 |
message += '----------------- End -------------------' |
|
|
68 |
print(message) |
|
|
69 |
|
|
|
70 |
# save to the disk |
|
|
71 |
expr_dir = os.path.join(opt.checkpoints_dir, opt.name) |
|
|
72 |
util.mkdirs(expr_dir) |
|
|
73 |
file_name = os.path.join(expr_dir, 'opt.txt') |
|
|
74 |
with open(file_name, 'wt') as opt_file: |
|
|
75 |
opt_file.write(message) |
|
|
76 |
opt_file.write('\n') |
|
|
77 |
|
|
|
78 |
def parse(self): |
|
|
79 |
"""Parse our options, create checkpoints directory suffix, and set up gpu device.""" |
|
|
80 |
opt = self.gather_options() |
|
|
81 |
|
|
|
82 |
self.opt = opt |
|
|
83 |
return self.opt |