-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.py
243 lines (203 loc) · 11.6 KB
/
test.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import os
from pyexpat import model
import torch
import torch.nn as nn
import argparse
import time
import datetime
import numpy as np
import math
import tifffile as tiff
from torch.autograd import Variable
from torch.utils.data import DataLoader
from utils import save_yaml_test
from skimage import io
from tqdm import tqdm
from SRDTrans import SRDTrans
from data_process import test_preprocess_lessMemoryNoTail_chooseOne, testset, singlebatch_test_save, multibatch_test_save
from utils import save_yaml_train
from sampling import *
#############################################################################################################################################
parser = argparse.ArgumentParser()
parser.add_argument('--GPU', type=str, default='0,1', help="the index of GPU used for computation (e.g., '0', '0,1', '0,1,2')")
parser.add_argument('--denoise_model', type=str, default=None, help='A folder containing models to be tested')
parser.add_argument('--datasets_folder', type=str, default='train', help="A folder containing all *.tif files for training")
parser.add_argument('--patch_x', type=int, default=128, help="patch size in x and y")
parser.add_argument('--patch_t', type=int, default=128, help="patch size in t")
parser.add_argument('--overlap_factor', type=float, default=0.5, help="the overlap factor between two adjacent patches")
parser.add_argument('--datasets_path', type=str, default='./datasets', help="dataset root path")
parser.add_argument('--pth_path', type=str, default='./pth', help="the root path to save models")
parser.add_argument('--output_path', type=str, default='./results', help="output directory")
parser.add_argument('--test_datasize', type=int, default=1000000, help='how many slices to be tested')
parser.add_argument('--scale_factor', type=int, default=1, help='the factor for image intensity scaling')
opt = parser.parse_args()
# use isotropic patch size by default
opt.patch_y = opt.patch_x # the height of 3D patches (patch size in y)
opt.patch_t = opt.patch_t # the length of 3D patches (patch size in t)
# opt.gap_t (image gap) is the distance between two adjacent patches
# use isotropic opt.gap by default
opt.gap_t = int(opt.patch_t * (1 - opt.overlap_factor))
opt.gap_x = int(opt.patch_x * (1 - opt.overlap_factor))
opt.gap_y = int(opt.patch_y * (1 - opt.overlap_factor))
opt.ngpu = str(opt.GPU).count(',') + 1
opt.batch_size = opt.ngpu # By default, the batch size is equal to the number of GPU for minimal memory consumption
print('\033[1;31mParameters -----> \033[0m')
print(opt)
########################################################################################################################
os.environ["CUDA_VISIBLE_DEVICES"] = str(opt.GPU)
model_path = os.path.join(opt.pth_path, opt.denoise_model)
model_list = list(os.walk(model_path, topdown=False))[-1][-1]
model_list.sort()
# read paremeters from file
for i in range(len(model_list)):
aaa = model_list[i]
if '.yaml' in aaa:
yaml_name = model_list[i]
del model_list[i]
print('If there are multiple models, only the last one will be used for denoising.')
model_list.sort()
model_list[:-1] = []
# get stacks for processing
im_folder = os.path.join(opt.datasets_path, opt.datasets_folder)
img_list = list(os.walk(im_folder, topdown=False))[-1][-1]
img_list.sort()
print('\033[1;31mStacks to be processed -----> \033[0m')
print('Total stack umber -----> ', len(img_list))
for img in img_list: print(img)
if not os.path.exists(opt.output_path):
os.mkdir(opt.output_path)
current_time = datetime.datetime.now().strftime("%Y%m%d%H%M")
output_name = 'DataFolderIs_' + opt.datasets_folder + '_' + current_time + '_ModelFolderIs_' + opt.denoise_model
output_path1 = os.path.join(opt.output_path, output_name)
if not os.path.exists(output_path1):
os.mkdir(output_path1)
yaml_name = os.path.join(output_path1, 'para.yaml')
save_yaml_test(opt, yaml_name)
##############################################################################################################################################################
denoise_generator = SRDTrans(
img_dim=opt.patch_x,
img_time=opt.patch_t,
in_channel=1,
embedding_dim=128,
num_heads=8,
hidden_dim=128*4,
window_size=7,
num_transBlock=1,
attn_dropout_rate=0.1,
f_maps=[8, 16, 32, 64],
input_dropout_rate=0
)
if torch.cuda.is_available():
print('\033[1;31mUsing {} GPU(s) for testing -----> \033[0m'.format(torch.cuda.device_count()))
denoise_generator = denoise_generator.cuda()
denoise_generator = nn.DataParallel(denoise_generator, device_ids=range(opt.ngpu))
cuda = True if torch.cuda.is_available() else False
Tensor = torch.cuda.FloatTensor if cuda else torch.FloatTensor
##############################################################################################################################################################
def test():
# Start processing
for pth_index in range(len(model_list)):
aaa = model_list[pth_index]
if '.pth' in aaa:
pth_name = model_list[pth_index]
output_path = os.path.join(output_path1, pth_name.replace('.pth', ''))
if not os.path.exists(output_path):
os.mkdir(output_path)
# load model
model_name = os.path.join(opt.pth_path, opt.denoise_model, pth_name)
if isinstance(denoise_generator, nn.DataParallel):
denoise_generator.module.load_state_dict(torch.load(model_name)) # parallel
denoise_generator.eval()
else:
denoise_generator.load_state_dict(torch.load(model_name)) # not parallel
denoise_generator.eval()
denoise_generator.cuda()
# test all stacks
for N in range(len(img_list)):
name_list, noise_img, coordinate_list, img_mean, input_data_type = test_preprocess_lessMemoryNoTail_chooseOne(opt, N)
prev_time = time.time()
time_start = time.time()
denoise_img = np.zeros(noise_img.shape)
result_file_name = img_list[N].replace('.tif', '') + '_' + pth_name.replace('.pth','') + '_output.tif'
result_name = os.path.join(output_path, result_file_name)
print(os.getcwd())
print(result_name)
# print("coordinate_list length:", len(coordinate_list))
test_data = testset(name_list, coordinate_list, noise_img)
testloader = DataLoader(test_data, batch_size=opt.batch_size, shuffle=False)
with torch.no_grad():
for iteration, (noise_patch, single_coordinate) in enumerate(testloader):
noise_patch = noise_patch.cuda()
real_A = noise_patch
real_A = Variable(real_A)
fake_B = denoise_generator(real_A)
preditc_numpy = fake_B.cpu().detach().numpy().astype(np.float32)
################################################################################################################
# Determine approximate time left
batches_done = iteration
batches_left = 1 * len(testloader) - batches_done
time_left_seconds = int(batches_left * (time.time() - prev_time))
time_left = datetime.timedelta(seconds=time_left_seconds)
prev_time = time.time()
################################################################################################################
if iteration % 1 == 0:
time_end = time.time()
time_cost = time_end - time_start # datetime.timedelta(seconds= (time_end - time_start))
print(
'\r[Model %d/%d, %s] [Stack %d/%d, %s] [Patch %d/%d] [Time Cost: %.0d s] [ETA: %s s] '
% (
pth_index + 1,
len(model_list),
pth_name,
N + 1,
len(img_list),
img_list[N],
iteration + 1,
len(testloader),
time_cost,
time_left_seconds
), end=' ')
if (iteration + 1) % len(testloader) == 0:
print('\n', end=' ')
################################################################################################################
output_image = np.squeeze(fake_B.cpu().detach().numpy())
raw_image = np.squeeze(real_A.cpu().detach().numpy())
if (output_image.ndim == 3):
turn = 1
else:
turn = output_image.shape[0]
# print(turn)
if (turn > 1):
for id in range(turn):
# print('shape of output_image -----> ',output_image.shape)
aaaa, bbbb, stack_start_w, stack_end_w, stack_start_h, stack_end_h, stack_start_s, stack_end_s = multibatch_test_save(
single_coordinate, id, output_image, raw_image)
aaaa=aaaa+img_mean
bbbb=bbbb+img_mean
denoise_img[stack_start_s:stack_end_s, stack_start_h:stack_end_h,
stack_start_w:stack_end_w] \
= aaaa * (np.sum(bbbb) / np.sum(aaaa)) ** 0.5
else:
aaaa, bbbb, stack_start_w, stack_end_w, stack_start_h, stack_end_h, stack_start_s, stack_end_s = singlebatch_test_save(
single_coordinate, output_image, raw_image)
aaaa=aaaa+img_mean
bbbb=bbbb+img_mean
denoise_img[stack_start_s:stack_end_s, stack_start_h:stack_end_h, stack_start_w:stack_end_w] \
= aaaa * (np.sum(bbbb) / np.sum(aaaa)) ** 0.5
del noise_img
output_img = denoise_img.squeeze().astype(np.float32) * opt.scale_factor
del denoise_img
output_img=np.clip(output_img, 0, 65535).astype('int32')
# Save inference image
if input_data_type == 'uint16':
output_img=np.clip(output_img, 0, 65535)
output_img = output_img.astype('uint16')
elif input_data_type == 'int16':
output_img=np.clip(output_img, -32767, 32767)
output_img = output_img.astype('int16')
else:
output_img = output_img.astype('int32')
io.imsave(result_name, output_img, check_contrast=False)
print("test result saved in:", result_name)
if __name__ == "__main__":
test()