-
Notifications
You must be signed in to change notification settings - Fork 0
/
reconstruct_hd5.py
408 lines (375 loc) · 14.1 KB
/
reconstruct_hd5.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
import os
import argparse
import silence_tensorflow.auto
import scipy.io as sio
import numpy as np
import time
def reconstruct_ds(ds_path, key, prms_net, rec_prms, options, skip=1, dose=None):
rec_prms['step_size'] *= skip
ds_class = airpi_dataset(rec_prms, ds_path, key, dose, skip=skip)
return retrieve_phase_from_generator(ds_class, prms_net, options, live_update=True)
def get_model_ckp(cp_path):
hp_file = os.path.join(cp_path, "hyperparameters.pickle")
_, prms_net = load_hparams(hp_file)
prms_net["cp_path"] = cp_path
return prms_net
if __name__ == "__main__":
os.system("clear")
parser = argparse.ArgumentParser()
parser.add_argument("--dose", type=int, default=0, help="Dose")
parser.add_argument("--ds", type=int, default=0, help="Dataset")
parser.add_argument("--skip", type=int, default=1, help="Step skip")
parser.add_argument("--gpu_id", type=int, default=0, help="GPU")
# parser.add_argument("--model", type=str, default='UNET_24_D3_sk', help="GPU")
parser.add_argument("--model", type=str, default='V_32_D3_sk_r10', help="GPU")
parser.add_argument("--ap_fcn", type=str, default='avrg', help="Aperture function estimation, gene: parameter generated, avrg: use PACBED")
args = vars(parser.parse_args())
dose = int(args["dose"])
ds = int(args["ds"])
if dose == 0:
dose = None
cp_path = 'Ckp/Training/' + args["model"]
os.environ["CUDA_DEVICE_ORDER"] = "PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"] = str(args["gpu_id"])
from tensorflow import config as tf_config, debugging
tf_config.optimizer.set_jit("autoclustering")
from ap_reconstruction.reconstruction_functions import (
retrieve_phase_from_generator,
)
debugging.disable_traceback_filtering()
# debugging.disable_check_numerics()
from ap_reconstruction.airpi_dataset import airpi_dataset
from ap_utils.file_ops import load_hparams
from ap_utils.globals import debugger_is_active
tf_config.run_functions_eagerly(debugger_is_active())
prms_net = get_model_ckp(cp_path)
# twisted bilayer_graphene
if ds == 0:
hd5_in = "/media/thomas/SSD/Samples/graphene/gra.hdf5"
hd5_key = "ds"
rec_prms = {
"E0": 200.0,
"apeture": 25,
"gmax": 2.5,
"cbed_size": 128,
"step_size": 0.2,
"aberrations": [-1, 0.001],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
"b_resize":True
}
options={'threads':1, 'batch_size':256}
if ds == 1:
hd5_in = "/media/thomas/SSD/Samples/graphene/gra_tim.h5"
hd5_key = "ds"
rec_prms = {
"E0": 60.0,
"apeture": 34,
"gmax": 1.8632454,
"cbed_size": 64,
"step_size": 0.04,
"aberrations": [0, 0],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1.0,
"order": ['ry','rx','kx','ky']
}
options={'threads':2, 'batch_size':256}
if ds == 2:
hd5_in = "/media/thomas/SSD/Samples/STO/sto_sro.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 20,
"gmax": 1.6253973,
"cbed_size": 64,
"step_size": 0.43,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
"order": ['ry','rx','kx','ky']
}
options={'threads':2, 'ew_ds_path':None}
if ds == 3:
hd5_in = "/media/thomas/SSD/Samples/MgO/MgO.h5"
hd5_key = "ds_cbed"
rec_prms = {
"E0": 300.0,
"apeture": 20,
"gmax": 1.6253973,
"cbed_size": 64,
"step_size": 0.05,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample":2.0,
"order": ['rx','ry','kx','ky']
}
options={'threads':2, 'batch_size':128}
if ds == 4:
# hd5_in = "/media/thomas/SSD/Samples/Zeolite/64x64x1000x1000_processed_cheat.hdf5"
# hd5_in = "/media/thomas/SSD/Samples/Zeolite/64x64x1000x1000_processed.hdf5"
hd5_in = "/media/thomas/SSD/Samples/Zeolite/64x64x499x499_aligned_uin8_cheat5.h5"
hd5_key = "ds"
rec_prms = {
"E0": 200.0,
"apeture": 12,
"gmax": 0.76557,
"cbed_size": 64,
"step_size": 0.3,
"aberrations": [-1, 1e-3],
# "aberrations": [0, 0],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1.0,
"order": ['rx','ry','kx','ky']
}
options={'threads':8, 'batch_size': 128}
# MoS2ew_ds_path
if ds == 5:
hd5_in = "/media/thomas/SSD/Samples/MSO/airpi_sto.h5"
hd5_key = "ds_int"
rec_prms = {
"E0": 300.0,
"apeture": 20.0,
"gmax": 4.5714,
"cbed_size": 128,
"step_size": 0.05,
# "aberrations": [-1, 1e-3],
"aberrations": [14.0312, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1.0,
"b_resize":True
}
options={'threads':1, 'ew_ds_path':None}
if ds == 6:
# STO
hd5_in = "/media/thomas/SSD/Samples/STO/hole_preprocessed_cropped_2.h5"
# hd5_in = "/media/thomas/SSD/Samples/STO/hole.h5"
# hd5_in = "/media/thomas/SSD/Samples/STO/hole3_64x64_bin_cheat.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 20.0,
"gmax": 1.6671,
"cbed_size": 64,
"step_size": 0.1818*2,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1.0,
"order": ['rx','ry','kx','ky']
}
options={'threads':1, 'batch_size':256}
if ds == 7:
# WS
hd5_in = "/media/thomas/SSD/Samples/WS2/WS2_d0.h5"
hd5_key = "dataset_1"
rec_prms = {
"E0": 60.0,
"apeture": 25.0,
"gmax": 0.6089,
"cbed_size": 64,
"step_size": 0.09,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1.0,
}
options={'threads':1, 'batch_size':256}
if ds == 8:
# Au
hd5_in = "/media/thomas/SSD/Samples/Au/Au_big_crop2.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 20.0,
"gmax": 1.6254,
"cbed_size": 128,
"step_size": 0.1394,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
"b_resize":True
}
options={'threads':1, 'batch_size':256}
if ds == 9:
# In2Se3
hd5_in = "/media/thomas/SSD/Samples/In2Se3/default10.h5"
hd5_key = "ds"
rec_prms = {
"E0": 200.0,
"apeture": 23.0,
"gmax": 1.6254,
"cbed_size": 128,
"step_size": 0.0314,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
"b_resize":True
}
options={'threads':1, 'batch_size':256}
# MoS2
if ds == 10:
hd5_in = "/media/thomas/SSD/Samples/MSO/mos_Cs.h5"
hd5_key = "ds_int"
rec_prms = {
"E0": 200.0,
"apeture": 10.0,
"gmax": 1.9973,
"cbed_size": 128,
"step_size": 0.1,
"aberrations": [-1, 1.0],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1,
"b_resize":True
}
options={'threads':1, 'batch_size':256}
if ds == 11:
# Au 2
hd5_in = "/media/thomas/SSD/Samples/Au/Au_NPb.h5"
# hd5_in = "/media/thomas/SSD/Samples/Au/Au_NP.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 20.0,
"gmax": 4.0635,
"cbed_size": 128,
"step_size": 0.12587,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
"b_resize":True
}
options={'threads':1, 'batch_size':256}
##########################
# Safiyye #
##########################
if ds == 20:
# MOF_PCN_222
# hd5_in = "/media/thomas/SSD/Samples/MOF_PCN_222/64x64x1000x1000_cheat2_centered.h5"
hd5_in = "/media/thomas/SSD/People/Safiyye/Thu_Sep_29_14_48_24_2022_STEM_300kV_2048x2048_3_us_1_scans_2000x2000_yx_skip3_sum6.h5"
# hd5_in = "/media/thomas/SSD/Samples/MOF_PCN_222/64x64x1000x1000_centered.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 10.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 0.238,
# "step_size": 0.358,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
}
options={'threads':1, 'batch_size':256}
if ds == 21:
# MOF_PCN_222
hd5_in = "/media/thomas/SSD/People/Safiyye/Thu_Dec_15_12_58_36_2022_STEM_300kV_2048x2048_2.0_us_1_scans_alpha0_64_b1.h5"
# hd5_in = "/media/thomas/SSD/People/Safiyye/Thu_Dec_15_12_58_36_2022_STEM_300kV_2048x2048_2.0_us_1_scans_alpha0_64_centered.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 12.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 2.88,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1.0,
}
options={'threads':1, 'batch_size':256}
if ds == 22:
# MOF_PCN_222
# hd5_in = "/media/thomas/SSD/People/Safiyye/Wed_Jan_25_15_36_30_2023_STEM_300kV_2048x2048_2.0_us_2_scans_alpha-42_yx_skip2_sum3.h5"
hd5_in = "/media/thomas/SSD/People/Safiyye/Thu_Dec_15_12_58_36_2022_STEM_300kV_2048x2048_2.0_us_1_scans_alpha0_64_centered.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 10.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 2.88*3,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1,
}
options={'threads':1, 'batch_size':256}
##########################
# Nadine #
##########################
if ds == 30:
# hd5_in = "/media/thomas/SSD/People/Nadine/Thu_Sep_29_14_48_24_2022_STEM_300kV_2048x2048_3_us_1_scans_1000x1000_yx_skip2_sum3.h5"
# hd5_in = "/media/thomas/SSD/People/Nadine/Tue_Oct_25_20_14_24_2022_STEM_200kV_2048x2048_1_us_3_scans_1000x1000x32x32_yx_skip2_sum3.h5"
hd5_in = "/media/thomas/SSD/People/Nadine/Tue_Oct_25_20_14_24_2022_STEM_200kV_2048x2048_1_us_3_scans_1000x1000_yx_skip2_sum3.h5"
# hd5_in = "/media/thomas/SSD/People/Nadine/Thu_Mar__3_15_25_48_2022_STEM_200kV_2048x2048_1_us_stepsize_0.081_scans_yx.h5"
hd5_key = "ds"
rec_prms = {
"E0": 200.0,
"apeture": 13.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 0.21*3,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 1,
}
options={'threads':1, 'batch_size':256}
if ds == 31:
hd5_in = "/media/thomas/SSD/People/Nadine/Thu_Mar__3_15_25_48_2022_STEM_200kV_2048x2048_1_us_stepsize_0.081_scans_yx_skip2_sum3.h5"
# hd5_in = "/media/thomas/SSD/People/Nadine/Thu_Mar__3_15_25_48_2022_STEM_200kV_2048x2048_1_us_stepsize_0.081_scans_yx.h5"
hd5_key = "ds"
rec_prms = {
"E0": 200.0,
"apeture": 21.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 0.081*3,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2,
}
options={'threads':1, 'batch_size':256}
if ds == 32:
hd5_in = "/media/thomas/SSD/People/Nadine/Mon_Feb_14_14_09_50_2022_STEM_200kV_2048x2048_6_us_stepsize_0.081_scans_2000x2000_yx_skip2.h5"
# hd5_in = "/media/thomas/SSD/People/Nadine/Thu_Mar__3_15_25_48_2022_STEM_200kV_2048x2048_1_us_stepsize_0.081_scans_yx.h5"
hd5_key = "ds"
rec_prms = {
"E0": 200.0,
"apeture": 21.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 0.081*3,
"aberrations": [-1, 1e-3],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2,
}
options={'threads':1, 'batch_size':256}
##########################
# Yansongs squishy stuff #
##########################
if ds == 40:
# Yansongs squishy stuff
hd5_in = "/media/thomas/SSD/People/Yansong/yansong_64.h5"
hd5_key = "ds"
rec_prms = {
"E0": 300.0,
"apeture": 21.0,
"gmax": 4.0635,
"cbed_size": 64,
"step_size": 0.21484375,
"aberrations": [0, 0],
"probe_estimation_method": args["ap_fcn"],
"oversample": 2.0,
}
options={'threads':1, 'batch_size':256}
dose_cbed = args["dose"]*(rec_prms['step_size']*args['skip'])**2
if dose_cbed < 1:
dose_cbed = None
folder, file_name = os.path.split(hd5_in)
file_name = os.path.splitext(os.path.basename(file_name))[0]
out_path = os.path.join(folder,file_name + "_s_" + str(args["skip"]) + '_d_' + str(args["dose"]) + '_airpi.mat')
start = time.time()
obj = reconstruct_ds(hd5_in, hd5_key, prms_net, rec_prms, skip=int(args["skip"]), dose=dose_cbed, options=options)
end = time.time()
print(end - start)
rec_prms['skip'] = args["skip"]
rec_prms['dose'] = args["dose"]
rec_prms['model'] = args["model"]
sio.savemat(out_path, {"obj": obj, "prms": rec_prms})
quit()