AD phase reconstruction
Automatic differentiation (AD) based methods of phase reconstruction.
AD phase reconstruction
- class PyLorentz.phase.AD_phase.ADPhase(dd: DefocusedDataset, device: str | int, save_dir: PathLike | None = None, name: str | None = None, verbose: bool = 1, scope: Microscope | None = None, sample_params: dict = {}, rng_seed: int | None = None, LRs: dict = {}, scheduler_type: str | None = None, noise_frac: float = 0.01, gaussian_sigma: float = 1)[source]
Bases:
BasePhaseReconstructionADPhase class for phase reconstruction using defocused datasets and DIPs.
- __init__(dd: DefocusedDataset, device: str | int, save_dir: PathLike | None = None, name: str | None = None, verbose: bool = 1, scope: Microscope | None = None, sample_params: dict = {}, rng_seed: int | None = None, LRs: dict = {}, scheduler_type: str | None = None, noise_frac: float = 0.01, gaussian_sigma: float = 1)[source]
Initialize the ADPhase object.
- Parameters:
dd (DefocusedDataset) – The defocused dataset.
device (Union[str, int]) – The device to use (CPU or GPU).
save_dir (Optional[os.PathLike], optional) – Directory to save results.
name (Optional[str], optional) – Name for the results.
verbose (bool, optional) – Verbosity level.
scope (Optional[Microscope], optional) – Microscope object.
sample_params (dict, optional) – Sample parameters.
rng_seed (Optional[int], optional) – Random seed.
LRs (dict, optional) – Learning rates for optimization.
scheduler_type (Optional[str], optional) – Type of learning rate scheduler.
noise_frac (float, optional) – Fraction of noise to add.
gaussian_sigma (float, optional) – Sigma value for Gaussian filter.
- property recon_phase: ndarray | None
Returns the reconstructed phase after applying Gaussian filter.
- Returns:
Reconstructed phase image.
- Return type:
Optional[np.ndarray]
- property best_phase: ndarray | None
Returns the best phase after applying Gaussian filter.
- Returns:
Best phase image.
- Return type:
Optional[np.ndarray]
- set_best_phase(iter_ind: int = -1) None[source]
Sets the best phase from the specified iteration index.
- Parameters:
iter_ind (int, optional) – Index of the iteration to use for the best phase.
- property best_amp: ndarray | None
Returns the best amplitude after applying Gaussian filter.
- Returns:
Best amplitude image.
- Return type:
Optional[np.ndarray]
- property gaussian_sigma: float
Returns the Gaussian sigma value.
- Returns:
Gaussian sigma value.
- Return type:
float
- property recon_amp: ndarray | None
Returns the reconstructed amplitude after applying Gaussian filter.
- Returns:
Reconstructed amplitude image.
- Return type:
Optional[np.ndarray]
- property scope: Microscope | None
Returns the microscope object.
- Returns:
Microscope object.
- Return type:
Optional[Microscope]
- property device: str
Returns the device used for computation.
- Returns:
Device for computation.
- Return type:
str
- property model_input: torch.Tensor | None
Returns the model input tensor.
- Returns:
Model input tensor.
- Return type:
Optional[torch.Tensor]
- property guess_phase: torch.Tensor | None
Returns the guess phase used to pre-train the DIP.
- Returns:
Guess phase tensor.
- Return type:
Optional[torch.Tensor]
- property guess_amp: torch.Tensor | None
Returns the guess amplitude used to pre-train the DIP or if solve_amp is False.
- Returns:
Guess amplitude tensor.
- Return type:
Optional[torch.Tensor]
- property input_DIP: torch.Tensor | None
Returns the noise used as input for one or both DIPs.
- Returns:
Input noise tensor.
- Return type:
Optional[torch.Tensor]
- reconstruct(num_iter: int, model: torch.nn.Module | List[torch.nn.Module] | None = None, num_pretrain_iter: int = 0, solve_amp: bool = False, solve_amp_scale: bool = True, guess_amp: float | ndarray | None = None, LRs: dict = {}, scheduler_type: str | None = None, save: bool = False, name: str | None = None, save_dir: PathLike | None = None, noise_frac: float | None = None, guess_phase: str | ndarray | None = 'SITIE', reset: bool = True, print_every: int = -1, verbose: int = 1, store_iters_every: int = -1, qc: any | None = None, **kwargs) None[source]
Performs the reconstruction process.
- Parameters:
num_iter (int) – Number of iterations for reconstruction.
model (Optional[Union[nn.Module, List[nn.Module]]], optional) – Model or list of models for DIP.
num_pretrain_iter (int, optional) – Number of pretraining iterations.
solve_amp (bool, optional) – Whether to solve for amplitude.
solve_amp_scale (bool, optional) – Whether to solve amplitude scale.
guess_amp (Optional[Union[float, np.ndarray]], optional) – Guess amplitude.
LRs (dict, optional) – Learning rates for optimization.
scheduler_type (Optional[str], optional) – Type of learning rate scheduler.
save (bool, optional) – Whether to save results.
name (Optional[str], optional) – Name for the saved results.
save_dir (Optional[os.PathLike], optional) – Directory to save results.
noise_frac (Optional[float], optional) – Fraction of noise to add.
guess_phase (Union[str, np.ndarray, None], optional) – Guess phase or method to obtain it.
reset (bool, optional) – Whether to reset the model.
print_every (int, optional) – Frequency of printing progress.
verbose (int, optional) – Verbosity level.
store_iters_every (int, optional) – Frequency of storing iterations.
qc (Optional[any], optional) – Quality control object.
**kwargs – Additional keyword arguments for scheduler parameters.
- show_final(crop: int = 5, **kwargs) None[source]
Show the phase and induction of the final iteration.
- Parameters:
crop (int, optional) – Amount to crop off of induction maps before displaying; often
5. (necessary in order to avoid edge artifacts. Defaults to) –
- visualize(crop=5)[source]
Plot the best reconstructed phase and induction maps.
- Parameters:
crop (int, optional) – Amount to crop off of induction maps before displaying; often
5. (necessary in order to avoid edge artifacts. Defaults to) –
- save_results(iter_ind: int = None, save_dir: PathLike | None = None, name: str | None = None, overwrite: bool = False)[source]
Save the recontructed phase, Bx, By, and color images. # TODO add saving amplitude and phase_E
- Parameters:
iter_ind (int, optional) – Index to save. Defaults to None which saves best phase.
save_dir (os.PathLik], optional) – Directory to save in. Defaults to None
self.save_dir. (which saves in) –
name (str, optional) – Name to prepend saved files. Defaults to None.
overwrite (bool, optional) – Whether or not to overwrite files. Defaults to False.
Deep image prior model
- class PyLorentz.phase.DIP_NN.DIP_NN(*args: Any, **kwargs: Any)[source]
Bases:
ModuleAutoencoder for reconstructing object wave and amplitude of LTEM images.
- Parameters:
num_images – int, number of input channels, equal to number of images in TFS.
nb_filters – int, number of filters in the first convolutional block.
use_dropout – bool, whether to use dropout in the inner layers.
batch_norm – bool, whether to use batch normalization after each convolutional layer.
upsampling_mode – str, “bilinear” or “nearest” upsampling method.
- class PyLorentz.phase.DIP_NN.conv2dblock(*args: Any, **kwargs: Any)[source]
Bases:
ModuleA block consisting of convolutional layers with optional batch normalization and dropout.
- Parameters:
nb_layers – int, number of convolutional layers.
input_channels – int, number of input channels.
output_channels – int, number of output channels.
kernel_size – int, size of the convolutional kernel.
stride – int, stride of the convolution.
padding – int, padding for the convolution.
use_batchnorm – bool, whether to use batch normalization.
lrelu_a – float, negative slope for the Leaky ReLU activation.
dropout – float, dropout rate.
last_sigmoid – bool, whether to use a sigmoid activation on the last layer.
last_tanh – bool, whether to use a tanh activation on the last layer.
last_skipReLU – bool, whether to skip ReLU activation on the last layer.
- class PyLorentz.phase.DIP_NN.upsample_block(*args: Any, **kwargs: Any)[source]
Bases:
ModuleUpsampling block using interpolation followed by a convolution.
- Parameters:
input_channels – int, number of input channels.
output_channels – int, number of output channels.
scale_factor – int, factor by which to scale the input.
mode – str, interpolation mode, either “bilinear” or “nearest”.