-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
executable file
·190 lines (176 loc) · 5.91 KB
/
utils.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
import matplotlib.pyplot as plt
import yaml
plt.ioff()
plt.switch_backend('agg')
########################################################################################################################
def create_feature_maps(init_channel_number, number_of_fmaps):
return [init_channel_number * 2 ** k for k in range(number_of_fmaps)]
def save_yaml_train(opt, yaml_name):
para = {'n_epochs':0,
'GPU':0,
'batch_size':0,
'datasets_folder':0,
'datasets_path':0,
'output_path':0,
'pth_path':0,
'patch_x':0,
'patch_y':0,
'patch_t':0,
'gap_y':0,
'gap_x':0,
'gap_t':0,
'lr':0,
'b1':0,
'b2':0,
'select_img_num':0,
'train_datasets_size':0}
para["n_epochs"] = opt.n_epochs
para["GPU"] = opt.GPU
para["batch_size"] = opt.batch_size
para["datasets_folder"] = opt.datasets_folder
para["datasets_path"] = opt.datasets_path
para["output_path"] = opt.output_path
para["pth_path"] = opt.pth_path
para["patch_x"] = opt.patch_x
para["patch_y"] = opt.patch_y
para["patch_t"] = opt.patch_t
para["gap_x"] = opt.gap_x
para["gap_y"] = opt.gap_y
para["gap_t"] = opt.gap_t
para["lr"] = opt.lr
para["b1"] = opt.b1
para["b2"] = opt.b2
para["select_img_num"] = opt.select_img_num
para["train_datasets_size"] = opt.train_datasets_size
with open(yaml_name, 'w') as f:
data = yaml.dump(para, f)
def save_yaml_test(opt, yaml_name):
para = {
'datasets_path':0,
'datasets_folder':0,
'denoise_model':0,
'pth_path':0,
'output_path':0,
'GPU':0,
'batch_size':0,
'patch_x':0,
'patch_y':0,
'patch_t':0,
'gap_x':0,
'gap_y':0,
'gap_t':0,
'test_datasize':0,
'scale_factor':0
}
para["datasets_path"] = opt.datasets_path
para["datasets_folder"] = opt.datasets_folder
para["denoise_model"] = opt.denoise_model
para["pth_path"] = opt.pth_path
para["output_path"] = opt.output_path
para["GPU"] = opt.GPU
para["batch_size"] = opt.batch_size
para["patch_x"] = opt.patch_x
para["patch_y"] = opt.patch_y
para["patch_t"] = opt.patch_t
para["gap_x"] = opt.gap_x
para["gap_y"] = opt.gap_y
para["gap_t"] = opt.gap_t
para["test_datasize"] = opt.test_datasize
para["scale_factor"] = opt.scale_factor
with open(yaml_name, 'w') as f:
data = yaml.dump(para, f)
def read_yaml(opt, yaml_name):
with open(yaml_name) as f:
para = yaml.load(f, Loader=yaml.FullLoader)
print(para)
opt.n_epochspara = ["n_epochs"]
# opt.datasets_folder = para["datasets_folder"]
opt.output_dir = para["output_dir"]
opt.batch_size = para["batch_size"]
# opt.patch_t = para["patch_t"]
# opt.patch_x = para["patch_x"]
# opt.patch_y = para["patch_y"]
# opt.gap_y = para["gap_y"]
# opt.gap_x = para["gap_x"]
# opt.gap_t = para["gap_t"]
opt.lr = para["lr"]
opt.fmap = para["fmap"]
opt.b1 = para["b1"]
para["b2"] = opt.b2
para["scale_factor"] = opt.scale_factor
def name2index(opt, input_name, num_h, num_w, num_s):
# print(input_name)
name_list = input_name.split('_')
# print(name_list)
z_part = name_list[-1]
# print(z_part)
y_part = name_list[-2]
# print(y_part)
x_part = name_list[-3]
# print(x_part)
z_index = int(z_part.replace('z',''))
y_index = int(y_part.replace('y',''))
x_index = int(x_part.replace('x',''))
# print("x_index ---> ",x_index,"y_index ---> ", y_index,"z_index ---> ", z_index)
cut_w = (opt.patch_x - opt.gap_x)/2
cut_h = (opt.patch_y - opt.gap_y)/2
cut_s = (opt.patch_t - opt.gap_t)/2
# print("z_index ---> ",cut_w, "cut_h ---> ",cut_h, "cut_s ---> ",cut_s)
if x_index == 0:
stack_start_w = x_index*opt.gap_x
stack_end_w = x_index*opt.gap_x+opt.patch_x-cut_w
patch_start_w = 0
patch_end_w = opt.patch_x-cut_w
elif x_index == num_w-1:
stack_start_w = x_index*opt.gap_x+cut_w
stack_end_w = x_index*opt.gap_x+opt.patch_x
patch_start_w = cut_w
patch_end_w = opt.patch_x
else:
stack_start_w = x_index*opt.gap_x+cut_w
stack_end_w = x_index*opt.gap_x+opt.patch_x-cut_w
patch_start_w = cut_w
patch_end_w = opt.patch_x-cut_w
if y_index == 0:
stack_start_h = y_index*opt.gap_y
stack_end_h = y_index*opt.gap_y+opt.patch_y-cut_h
patch_start_h = 0
patch_end_h = opt.patch_y-cut_h
elif y_index == num_h-1:
stack_start_h = y_index*opt.gap_y+cut_h
stack_end_h = y_index*opt.gap_y+opt.patch_y
patch_start_h = cut_h
patch_end_h = opt.patch_y
else:
stack_start_h = y_index*opt.gap_y+cut_h
stack_end_h = y_index*opt.gap_y+opt.patch_y-cut_h
patch_start_h = cut_h
patch_end_h = opt.patch_y-cut_h
if z_index == 0:
stack_start_s = z_index*opt.gap_t
stack_end_s = z_index*opt.gap_t+opt.patch_t-cut_s
patch_start_s = 0
patch_end_s = opt.patch_t-cut_s
elif z_index == num_s-1:
stack_start_s = z_index*opt.gap_t+cut_s
stack_end_s = z_index*opt.gap_t+opt.patch_t
patch_start_s = cut_s
patch_end_s = opt.patch_t
else:
stack_start_s = z_index*opt.gap_t+cut_s
stack_end_s = z_index*opt.gap_t+opt.patch_t-cut_s
patch_start_s = cut_s
patch_end_s = opt.patch_t-cut_s
return int(stack_start_w) ,int(stack_end_w) ,int(patch_start_w) ,int(patch_end_w) ,\
int(stack_start_h) ,int(stack_end_h) ,int(patch_start_h) ,int(patch_end_h), \
int(stack_start_s) ,int(stack_end_s) ,int(patch_start_s) ,int(patch_end_s)
def ckp_cmp(ckp_1, ckp_2):
idx_1 = ckp_1.split("_")[1]
idx_2 = ckp_2.split("_")[1]
idx_1 = int(idx_1)
idx_2 = int(idx_2)
if idx_1 < idx_2:
return -1
if idx_1 > idx_2:
return 1
return 0