14. Hypertune#
Description of this walkthrough goes here.
1# Created with PtyRAD 0.1.0b13
2# Documentation: https://ptyrad.readthedocs.io/en/latest/
3# Detailed description for each option: https://ptyrad.readthedocs.io/en/latest/_autosummary/ptyrad.params.html
4
5# Run PtyRAD in hypertune mode to refine parameters
6
7# PSO
8
9init_params:
10 # Experimental params
11 probe_kv : 300 # [kV] Acceleration voltage
12 probe_conv_angle : 21.4 # [mrad] Semi-convergence angle for probe-forming aperture
13 probe_aberrations : {'C10': 200} # [Angstrom, degree] Aberration coefficients in Krivanel polar notations. C10 = -df, and positive C10 refers to overfocus (stronger lens).
14 meas_Npix : 256 # Detector pixel number, EMPAD is 128. Only supports square detector for simplicity
15 pos_N_scan_slow : 64 # Number of scan position along slow scan direction. Usually it's the vertical direction of acquisition GUI
16 pos_N_scan_fast : 64 # Number of scan position along fast scan direction. Usually it's the horizontal direction of acquisition GUI
17 pos_scan_step_size : 0.410 # [Angstrom] Step size between probe positions in a rectangular raster scan pattern
18 # Model complexity
19 probe_pmode_max : 4 # Maximum number of mixed probe modes
20 obj_Nlayer : 21 # Number of slices for multislice object
21 obj_slice_thickness : 10 # [Angstrom] Slice thickness (propagation distance) for multislice ptychography. Typical values are between 1 to 20 Ang.
22 # Preprocessing
23 meas_permute : null # Permute meas array with a list of ints to reorder datasets into (N_scans, ky, kx) if needed.
24 meas_reshape : null # Reshape meas array with a list of 3 ints to convert the 4D diffraction dataset (Ry,Rx,ky,kx) into 3D (N_scans,ky,kx) for PtyRAD.
25 meas_flipT : null # Flip meas orientation with a list of 3 binary booleans (0 or 1) as [flipud, fliplr, transpose]
26 meas_crop : [null,null,[68,188],[68,188]] # Crops the 4D dataset with [[scan_slow_start, scan_slow_end], [scan_fast_start, scan_fast_end], [ky_start, ky_end], [kx_start, kx_end]].
27 meas_pad : {'mode': 'on_the_fly', 'padding_type': 'power', 'target_Npix': 256, 'value': 0, 'threshold': 70} # Pads the diffraction pattern to side length = 'target_Npix' and correspondingly change the kMax, dx, Npix.
28 pos_scan_affine : null # Affine transformation [scale, asymmetry, rotation, shear] of scan patterns. e.g, [1,0,3,0], rotation and shear are in unit of degree.
29 # Input source and params
30 meas_params : {'path': 'data/PSO/sample_data_PrScO3.mat', 'key': 'dp'} # Supports EMPAD .raw, .hdf5, .mat, and .tif
31
32hypertune_params:
33 ## NOTE: PtyRAD provides integrated hyperparameter tuning via 'hypertune_params', so everything can be contained in a SINGLE params file.
34 ## Use 'if_hypertune' to toggle ON/OFF of whether to use the hypertune mode.
35 ## Optimizing in high-dimensional space is particularly challenging, so it's suggested to only turn on 2-4 params at most for each hypertune task.
36 ## In this specific example, we switch on (i.e., 'state': true) both 'C10' and 'Nlayer' to refine our guess of probe overfocus and number of slices.
37 ## The results of each hypertune trial in the study are saved in the sqlite3 database file (i.e., storage_path),
38 ## which can be easily examined using VS Code extension: Optuna Dashboard
39 if_hypertune : true
40 n_trials : 50 # Number of hypertune trials. Each trial is a separate PtyRAD reconstruction with a set of optimizable parameter values (i.e., a configuration).
41 storage_path : 'sqlite:///hypertune.sqlite3' # Path to the SQLite database file (i.e., sotrage) that will be automatically created with new hypertube run.
42 study_name : 'PSO'
43 tune_params :
44 batch_size : {'state': false, 'suggest': 'int', 'kwargs': {'low': 16, 'high': 512, 'log': true}}
45 dx : {'state': false, 'suggest': 'float', 'kwargs': {'low': 0.1400, 'high': 0.1600, 'step': null, 'log': false}}
46 conv_angle : {'state': false, 'suggest': 'float', 'kwargs': {'low': 24, 'high': 26, 'step': 1, 'log': false}}
47 C10 : {'state': true, 'suggest': 'float', 'kwargs': {'low': -100, 'high': 100, 'step': 1, 'log': false}}
48 C12 : {'state': false, 'suggest': 'float', 'kwargs': {'low': -100, 'high': -100, 'step': 1, 'log': false}}
49 C21 : {'state': false, 'suggest': 'float', 'kwargs': {'low': -1000, 'high': 1000, 'step': 10, 'log': false}}
50 C23 : {'state': false, 'suggest': 'float', 'kwargs': {'low': -1000, 'high': 1000, 'step': 10, 'log': false}}
51 Nlayer : {'state': true, 'suggest': 'int', 'kwargs': {'low': 15, 'high': 30, 'step': 1, 'log': false}}
52 scale : {'state': false, 'suggest': 'float', 'kwargs': {'low': 0.8, 'high': 1.2, 'step': 0.02, 'log': false}} # This modifies the effective scan step size
53 rotation : {'state': false, 'suggest': 'float', 'kwargs': {'low': -4, 'high': 4, 'step': 0.5, 'log': false}} # Scan rotation
54 tilt_y : {'state': false, 'suggest': 'float', 'kwargs': {'low': -5, 'high': 5, 'step': 0.5, 'log': false}}
55 tilt_x : {'state': false, 'suggest': 'float', 'kwargs': {'low': -5, 'high': 5, 'step': 0.5, 'log': false}}
56
57model_params:
58 detector_blur_std : 1 # [k-space px] Gaussian blur std of forward simulated diffraction patterns. Typical value is 0-1 px.
59
60loss_params:
61 loss_single: {'state': true, 'weight': 1.0, 'dp_pow': 0.5} # Amplitude noise model for typical dataset (dose-sufficient) under the maximum-likelihood formalism
62 loss_sparse: {'state': true, 'weight': 0.1, 'ln_order': 1} # L_n norm sparsity regularization calculated for object phase ('objp')
63
64constraint_params:
65 obj_zblur : {'start_iter': 1, 'step': 1, 'end_iter': null, 'obj_type': 'both', 'kernel_size': 5, 'std': 1} # Apply a "z-direction" 1D Gaussian blur to the object.
66 mirrored_amp : {'start_iter': 1, 'step': 1, 'end_iter': null, 'relax': 0.1, 'scale': 0.03, 'power': 4} # Apply a more flexible, ad hoc constraint for constraining amplitude using 1-scale*phase**power, which provide more arbitrary parameters to tune the constrained amplitude based on the phase.
67 obja_thresh : {'start_iter': 1, 'step': 1, 'end_iter': null, 'relax': 0, 'thresh': [0.96, 1.04]} # Thresholds the object amplitude around 1 with specified range in 'thresh'.
68 objp_postiv : {'start_iter': 1, 'step': 1, 'end_iter': null, 'relax': 0} # Apply a positivity constraint of the object phase by clipping negative values
69
70recon_params:
71
72 ## NOTE: The hyperparameter search space is usually high-dimensional, so we want to try as many trials as possible.
73 ## We want to make sure the NITER is high enough to give informative evaluation of the reconstruciton quality,
74 ## however, we also don't want to make N-hypertune-trials become N-full rectonstructions, as it's very inefficient.
75 ## Setting NITER to an acceptable number, and let hypertune decides whether to prune unpromissing trials automatically.
76 NITER: 50 # Total number of reconstruction iterations. 1 iteration means a full pass of all selected diffraction patterns.
77
78 BATCH_SIZE: {'size': 32, 'grad_accumulation': 1} # Number of diffraction patterns processed simultaneously to get the gradient update.
79
80 ## NOTE: Since the purpose of hypertune is to explore the "promising parameters",
81 ## generally we're not interested in the intermediate output files, so will set SAVE_ITERS to null to skip saving the large model files.
82 SAVE_ITERS: null # Number of completed iterations before saving the current reconstruction results (model, probe, object) and summary figures.
83
84 output_dir: 'output/walkthrough/14_hypertune/'
85 recon_dir_affixes: ['minimal', 'model', 'loss', 'constraint'] # Customizable affixes of reconstruction folder name with presets like 'minimal', 'default', 'all'. See docs for 19 more detailed controls.
86 prefix_time: false # type: boolean, preset strings, and time format strings. Set to true to prepend a date str like '20240903_' in front of the reconstruction folder name
87 prefix: '' # Prefix this string to the reconstruction folder name. Note that a "_" will be automatically generated.
88 postfix: '' # Postfix this string to the reconstruction folder name. Note that a "_" will be automatically generated.
89 save_result: ['model', 'objp', 'probe_prop'] # Selected results to save every SAVE_ITERS.
90 result_modes: {'obj_dim': [2, 3, 4], 'FOV': ['crop'], 'bit': ['8', 'raw']} # Specifies which object output is saved by their final dimension ('obj_dim'), whether to save the full or cropped FOV ('FOV') of object, and whether to save the raw or normalized bit depth version of object and probe.
91 selected_figs: ['convergence', 'forward', 'probe_r_amp', 'pos'] # Selected figures that will be saved. See full.yaml for all available keys, recommended keys are ['convergence', 'forward', 'probe_r_amp', 'pos'].
92 compiler_configs: {'enable': true} # Set to {'enable': true} to enable PyTorch JIT compilation for a 1.3-1.9x speedup on supported hardware.