CT Training and Reconstruction with ODPΒΆ
This example demonstrates the training of the unrolled optimization with deep priors (ODP) gradient descent architecture described in [20] applied to a CT reconstruction problem.
The source images are foam phantoms generated with xdesign.
A class scico.flax.ODPNet implements the ODP architecture, which solves the optimization problem
where \(A\) is a tomographic projector, \(\mathbf{y}\) is a set of sinograms, \(r\) is a regularizer and \(\mathbf{x}\) is the set of reconstructed images. The ODP, gradient descent architecture, abstracts the iterative solution by an unrolled network where each iteration corresponds to a different stage in the ODP network and updates the prediction by solving
which for the CT problem, using gradient descent, corresponds to
where \(k\) is the index of the stage (iteration), \(\mathbf{x}^k + \mathbf{x}^{k+1/2} = \mathrm{ResNet}(\mathbf{x}^{k})\) is the regularization (implemented as a residual convolutional neural network), \(\mathbf{x}^k\) is the output of the previous stage and \(\alpha_k > 0\) is a learned stage-wise parameter weighting the contribution of the fidelity term. The output of the final stage is the set of reconstructed images.
[1]:
# isort: off
import os
from functools import partial
from time import time
import numpy as np
import logging
import ray
ray.init(logging_level=logging.ERROR) # need to call init before jax import: ray-project/ray#44087
# Set an arbitrary processor count (only applies if GPU is not available).
os.environ["XLA_FLAGS"] = "--xla_force_host_platform_device_count=8"
import jax
try:
from jax.extend.backend import get_backend # introduced in jax 0.4.33
except ImportError:
from jax.lib.xla_bridge import get_backend
from mpl_toolkits.axes_grid1 import make_axes_locatable
import komplot as kplt
from scico import flax as sflax
from scico import metric
from scico.flax.examples import load_ct_data
from scico.flax.train.traversals import clip_positive, construct_traversal
from scico.linop.xray import XRayTransform2D
kplt.config_notebook_plotting()
platform = get_backend().platform
print("Platform: ", platform)
Platform: gpu
Read data from cache or generate if not available.
[2]:
N = 256 # phantom size
train_nimg = 536 # number of training images
test_nimg = 64 # number of testing images
nimg = train_nimg + test_nimg
n_projection = 45 # CT views
trdt, ttdt = load_ct_data(train_nimg, test_nimg, N, n_projection, verbose=True)
Data read from path: ~/.cache/scico/examples/data
Set --training-- size: 536
Set --testing -- size: 64
Data range --images -- Min: 0.00 Max: 1.00
Data range --sinogram-- Min: 0.00 Max: 0.95
Data range --FBP -- Min: 0.00 Max: 1.00
Build CT projection operator. Parameters are chosen so that the operator is equivalent to the one used to generate the training data.
[3]:
angles = np.linspace(0, np.pi, n_projection, endpoint=False) # evenly spaced projection angles
A = XRayTransform2D(
input_shape=(N, N),
angles=angles,
det_count=int(N * 1.05 / np.sqrt(2.0)),
dx=1.0 / np.sqrt(2),
)
A = (1.0 / N) * A # normalize projection operator
Build training and testing structures. Inputs are the sinograms and outputs are the original generated foams. Keep training and testing partitions.
[4]:
numtr = 320
numtt = 32
train_ds = {"image": trdt["sino"][:numtr], "label": trdt["img"][:numtr]}
test_ds = {"image": ttdt["sino"][:numtt], "label": ttdt["img"][:numtt]}
Define configuration dictionary for model and training loop.
Parameters have been selected for demonstration purposes and relatively short training. The model depth is akin to the number of unrolled iterations in the MoDL model. The block depth controls the number of layers at each unrolled iteration. The number of filters is uniform throughout the iterations. The iterations used for the conjugate gradient (CG) solver can also be specified. Better performance may be obtained by increasing depth, block depth, number of filters, CG iterations, or training epochs, but may require longer training times.
[5]:
# model configuration
model_conf = {
"depth": 8,
"num_filters": 64,
"block_depth": 6,
}
# training configuration
train_conf: sflax.ConfigDict = {
"seed": 1234,
"opt_type": "ADAM",
"batch_size": 16,
"num_epochs": 200,
"base_learning_rate": 1e-3,
"warmup_epochs": 0,
"log_every_steps": 160,
"log": True,
"checkpointing": True,
}
Construct functionality for ensuring that the learned fidelity weight parameter is always positive.
[6]:
alphatrav = construct_traversal("alpha") # select alpha parameters in model
alphapost = partial(
clip_positive, # apply this function
traversal=alphatrav, # to alpha parameters in model
minval=1e-3,
)
Print configuration of distributed run.
[7]:
print(f"\nJAX process: {jax.process_index()}{' / '}{jax.process_count()}")
print(f"JAX local devices: {jax.local_devices()}\n")
JAX process: 0 / 1
JAX local devices: [CudaDevice(id=0), CudaDevice(id=1), CudaDevice(id=2), CudaDevice(id=3), CudaDevice(id=4), CudaDevice(id=5), CudaDevice(id=6), CudaDevice(id=7)]
Construct ODPNet model.
[8]:
channels = train_ds["image"].shape[-1]
model = sflax.ODPNet(
operator=A,
depth=model_conf["depth"],
channels=channels,
num_filters=model_conf["num_filters"],
block_depth=model_conf["block_depth"],
odp_block=sflax.inverse.ODPGrDescBlock,
alpha_ini=1e-2,
)
Run training loop.
[9]:
workdir = os.path.join(os.path.expanduser("~"), ".cache", "scico", "examples", "odp_ct_out")
train_conf["workdir"] = workdir
train_conf["post_lst"] = [alphapost]
# Construct training object
trainer = sflax.BasicFlaxTrainer(
train_conf,
model,
train_ds,
test_ds,
)
modvar, stats_object = trainer.train()
channels: 1 training signals: 320 testing signals: 32 signal size: 256
Network Structure:
+---------------------------------------------------------+----------------+--------+-----------+---------+
| Name | Shape | Size | Mean | Std |
+---------------------------------------------------------+----------------+--------+-----------+---------+
| ODPGrDescBlock_0/alpha | (1,) | 1 | 0.0648 | 0.0 |
| ODPGrDescBlock_0/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0262 | 0.0 |
| ODPGrDescBlock_0/resnet/BatchNorm_0/scale | (1,) | 1 | 0.996 | 0.0 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | 0.00266 | 0.0147 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0119 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | -0.00368 | 0.0575 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | -0.00284 | 0.0115 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.00991 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.00043 | 0.0427 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | 0.000808 | 0.0106 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0104 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000519 | 0.0425 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.000376 | 0.0134 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00814 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000473 | 0.0425 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | -0.00313 | 0.0185 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0156 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000266 | 0.0427 |
| ODPGrDescBlock_0/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | 0.00335 | 0.0606 |
| ODPGrDescBlock_1/alpha | (1,) | 1 | 0.00103 | 0.0 |
| ODPGrDescBlock_1/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0262 | 0.0 |
| ODPGrDescBlock_1/resnet/BatchNorm_0/scale | (1,) | 1 | 0.986 | 0.0 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | 0.00176 | 0.0137 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0074 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | 0.00142 | 0.0557 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | -0.00194 | 0.0141 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.00851 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000126 | 0.0423 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | -0.000629 | 0.0148 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00814 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 7.45e-05 | 0.0425 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.00254 | 0.0106 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00805 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000442 | 0.0428 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | 0.000142 | 0.0114 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0107 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -9.53e-05 | 0.0425 |
| ODPGrDescBlock_1/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | -0.000611 | 0.0577 |
| ODPGrDescBlock_2/alpha | (1,) | 1 | 0.171 | 0.0 |
| ODPGrDescBlock_2/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0265 | 0.0 |
| ODPGrDescBlock_2/resnet/BatchNorm_0/scale | (1,) | 1 | 0.993 | 0.0 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | 0.00777 | 0.02 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0108 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | -0.0043 | 0.0603 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | -0.000502 | 0.0219 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0113 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000601 | 0.0425 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | -0.00063 | 0.019 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0138 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000108 | 0.0427 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.00244 | 0.0205 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0119 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.00039 | 0.0427 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | 0.00175 | 0.0132 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0127 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000256 | 0.043 |
| ODPGrDescBlock_2/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | 0.00299 | 0.0606 |
| ODPGrDescBlock_3/alpha | (1,) | 1 | 0.00116 | 0.0 |
| ODPGrDescBlock_3/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0265 | 0.0 |
| ODPGrDescBlock_3/resnet/BatchNorm_0/scale | (1,) | 1 | 0.984 | 0.0 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | 0.00216 | 0.0147 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00743 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | -0.00273 | 0.0595 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | -0.00152 | 0.0111 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.00823 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 5.7e-05 | 0.0424 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | -0.00048 | 0.013 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00939 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000426 | 0.0424 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.000157 | 0.0135 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00993 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000171 | 0.0427 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | -0.00628 | 0.0153 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00741 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000305 | 0.0427 |
| ODPGrDescBlock_3/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | -0.00678 | 0.0605 |
| ODPGrDescBlock_4/alpha | (1,) | 1 | 0.001 | 0.0 |
| ODPGrDescBlock_4/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0266 | 0.0 |
| ODPGrDescBlock_4/resnet/BatchNorm_0/scale | (1,) | 1 | 0.984 | 0.0 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | -0.00291 | 0.0131 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00764 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | 0.00129 | 0.0608 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | -0.0012 | 0.0166 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00796 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 5.19e-05 | 0.0426 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | 0.000838 | 0.0183 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00864 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -4.95e-05 | 0.0427 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.00124 | 0.0137 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00926 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000122 | 0.0425 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | -0.000712 | 0.0151 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00962 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000675 | 0.0424 |
| ODPGrDescBlock_4/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | -0.000632 | 0.0605 |
| ODPGrDescBlock_5/alpha | (1,) | 1 | 0.001 | 0.0 |
| ODPGrDescBlock_5/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0266 | 0.0 |
| ODPGrDescBlock_5/resnet/BatchNorm_0/scale | (1,) | 1 | 0.995 | 0.0 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | 0.00913 | 0.0229 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0106 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | 0.00262 | 0.0561 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | 0.00968 | 0.0236 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00753 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000724 | 0.0425 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | 0.00337 | 0.0149 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00768 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000271 | 0.0428 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.0067 | 0.0186 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00795 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000511 | 0.0428 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | -0.00278 | 0.0184 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00819 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000175 | 0.0428 |
| ODPGrDescBlock_5/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | 0.00261 | 0.0584 |
| ODPGrDescBlock_6/alpha | (1,) | 1 | 0.0658 | 0.0 |
| ODPGrDescBlock_6/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0265 | 0.0 |
| ODPGrDescBlock_6/resnet/BatchNorm_0/scale | (1,) | 1 | 1.0 | 0.0 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | 0.00619 | 0.0203 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.0121 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | 0.00096 | 0.0577 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | 0.00337 | 0.0169 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0141 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000574 | 0.0426 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | 0.00479 | 0.0128 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0135 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000141 | 0.043 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.00265 | 0.0159 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0131 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000247 | 0.0427 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | 0.00391 | 0.0115 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.0113 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000698 | 0.0428 |
| ODPGrDescBlock_6/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | -0.00134 | 0.058 |
| ODPGrDescBlock_7/alpha | (1,) | 1 | 0.049 | 0.0 |
| ODPGrDescBlock_7/resnet/BatchNorm_0/bias | (1,) | 1 | 0.0266 | 0.0 |
| ODPGrDescBlock_7/resnet/BatchNorm_0/scale | (1,) | 1 | 0.971 | 0.0 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_0/BatchNorm_0/bias | (64,) | 64 | -0.00278 | 0.0257 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_0/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.00977 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_0/Conv_0/kernel | (3, 3, 1, 64) | 576 | 0.00289 | 0.059 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_1/BatchNorm_0/bias | (64,) | 64 | 0.00266 | 0.0153 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_1/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00904 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_1/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.00103 | 0.0427 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_2/BatchNorm_0/bias | (64,) | 64 | -0.000124 | 0.0149 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_2/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.0142 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_2/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000185 | 0.0427 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_3/BatchNorm_0/bias | (64,) | 64 | 0.00159 | 0.0207 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_3/BatchNorm_0/scale | (64,) | 64 | 0.999 | 0.0209 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_3/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | -0.000639 | 0.0428 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_4/BatchNorm_0/bias | (64,) | 64 | 0.00137 | 0.0199 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_4/BatchNorm_0/scale | (64,) | 64 | 1.0 | 0.00946 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_4/Conv_0/kernel | (3, 3, 64, 64) | 36,864 | 0.000129 | 0.0426 |
| ODPGrDescBlock_7/resnet/Conv_0/kernel | (3, 3, 64, 1) | 576 | -0.00372 | 0.059 |
+---------------------------------------------------------+----------------+--------+-----------+---------+
Total weights: 1,194,008
Batch Normalization:
+--------------------------------------------------------+-------+------+----------+----------+
| Name | Shape | Size | Mean | Std |
+--------------------------------------------------------+-------+------+----------+----------+
| ODPGrDescBlock_0/resnet/BatchNorm_0/mean | (1,) | 1 | 0.807 | 0.0 |
| ODPGrDescBlock_0/resnet/BatchNorm_0/var | (1,) | 1 | 11.1 | 0.0 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | -0.00256 | 0.0128 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 1.17e-05 | 2.02e-05 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | 0.0638 | 0.57 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 0.602 | 0.434 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | -0.126 | 0.497 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 1.17 | 0.857 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | -0.118 | 0.45 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 0.554 | 0.477 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | -0.0707 | 0.563 |
| ODPGrDescBlock_0/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 0.801 | 0.549 |
| ODPGrDescBlock_1/resnet/BatchNorm_0/mean | (1,) | 1 | -0.0338 | 0.0 |
| ODPGrDescBlock_1/resnet/BatchNorm_0/var | (1,) | 1 | 13.2 | 0.0 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | 0.00139 | 0.0175 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.0249 | 0.0285 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | 0.0347 | 0.733 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.8 | 1.51 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | 0.00609 | 0.58 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 0.866 | 0.592 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | -0.0947 | 0.546 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 1.04 | 0.735 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | -0.0177 | 0.454 |
| ODPGrDescBlock_1/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 0.911 | 0.7 |
| ODPGrDescBlock_2/resnet/BatchNorm_0/mean | (1,) | 1 | 0.615 | 0.0 |
| ODPGrDescBlock_2/resnet/BatchNorm_0/var | (1,) | 1 | 8.31 | 0.0 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | -0.00525 | 0.0231 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.0461 | 0.0842 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | -0.148 | 0.544 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.17 | 0.75 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | -0.0272 | 0.562 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 0.949 | 0.828 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | -0.0947 | 0.552 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 0.852 | 0.53 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | -0.0679 | 0.49 |
| ODPGrDescBlock_2/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 0.918 | 0.835 |
| ODPGrDescBlock_3/resnet/BatchNorm_0/mean | (1,) | 1 | -0.891 | 0.0 |
| ODPGrDescBlock_3/resnet/BatchNorm_0/var | (1,) | 1 | 26.4 | 0.0 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | -0.00435 | 0.0358 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.0256 | 0.0254 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | 0.00828 | 0.523 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.04 | 0.649 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | 0.0841 | 0.444 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 0.913 | 0.76 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | 0.0209 | 0.433 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 0.918 | 0.688 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | 0.0432 | 0.499 |
| ODPGrDescBlock_3/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 1.1 | 0.954 |
| ODPGrDescBlock_4/resnet/BatchNorm_0/mean | (1,) | 1 | -0.0614 | 0.0 |
| ODPGrDescBlock_4/resnet/BatchNorm_0/var | (1,) | 1 | 13.0 | 0.0 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | 0.00229 | 0.0405 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.0126 | 0.0133 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | 0.00821 | 0.551 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.5 | 0.752 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | -0.0216 | 0.451 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 1.09 | 0.636 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | 0.0118 | 0.471 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 0.994 | 0.577 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | -0.149 | 0.479 |
| ODPGrDescBlock_4/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 1.31 | 0.987 |
| ODPGrDescBlock_5/resnet/BatchNorm_0/mean | (1,) | 1 | 0.17 | 0.0 |
| ODPGrDescBlock_5/resnet/BatchNorm_0/var | (1,) | 1 | 18.4 | 0.0 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | 0.00548 | 0.0365 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.0441 | 0.0471 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | -0.0864 | 0.292 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.23 | 1.04 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | -0.0311 | 0.31 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 1.08 | 1.48 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | -0.0616 | 0.367 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 1.48 | 1.38 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | 0.00654 | 0.258 |
| ODPGrDescBlock_5/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 1.15 | 1.09 |
| ODPGrDescBlock_6/resnet/BatchNorm_0/mean | (1,) | 1 | -0.373 | 0.0 |
| ODPGrDescBlock_6/resnet/BatchNorm_0/var | (1,) | 1 | 19.4 | 0.0 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | 0.00223 | 0.04 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.00349 | 0.00417 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | -0.111 | 0.553 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.2 | 0.742 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | -0.0271 | 0.431 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 1.12 | 0.705 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | -0.046 | 0.51 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 1.09 | 0.602 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | -0.164 | 0.556 |
| ODPGrDescBlock_6/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 1.38 | 0.885 |
| ODPGrDescBlock_7/resnet/BatchNorm_0/mean | (1,) | 1 | -1.0 | 0.0 |
| ODPGrDescBlock_7/resnet/BatchNorm_0/var | (1,) | 1 | 20.5 | 0.0 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_0/BatchNorm_0/mean | (64,) | 64 | 0.00733 | 0.0432 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_0/BatchNorm_0/var | (64,) | 64 | 0.0144 | 0.0164 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_1/BatchNorm_0/mean | (64,) | 64 | -0.256 | 0.659 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_1/BatchNorm_0/var | (64,) | 64 | 1.68 | 1.29 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_2/BatchNorm_0/mean | (64,) | 64 | -0.0494 | 0.544 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_2/BatchNorm_0/var | (64,) | 64 | 1.09 | 0.738 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_3/BatchNorm_0/mean | (64,) | 64 | -0.166 | 0.56 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_3/BatchNorm_0/var | (64,) | 64 | 1.57 | 1.09 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_4/BatchNorm_0/mean | (64,) | 64 | 0.0202 | 0.446 |
| ODPGrDescBlock_7/resnet/ConvBNBlock_4/BatchNorm_0/var | (64,) | 64 | 1.33 | 0.822 |
+--------------------------------------------------------+-------+------+----------+----------+
Total weights: 5,136
Initial compilation, which might take some time ...
Evaluate on testing data.
[10]:
del train_ds["image"]
del train_ds["label"]
fmap = sflax.FlaxMap(model, modvar)
del model, modvar
maxn = numtt
start_time = time()
output = fmap(test_ds["image"][:maxn])
time_eval = time() - start_time
output = np.clip(output, a_min=0, a_max=1.0)
epochs = train_conf["num_epochs"]
Evaluate trained model in terms of reconstruction time and data fidelity.
[11]:
snr_eval = metric.snr(test_ds["label"][:maxn], output)
psnr_eval = metric.psnr(test_ds["label"][:maxn], output)
print(
f"{'ODPNet training':18s}{'epochs:':2s}{epochs:>5d}{'':21s}"
f"{'time[s]:':10s}{trainer.train_time:>7.2f}"
)
print(
f"{'ODPNet testing':18s}{'SNR:':5s}{snr_eval:>5.2f}{' dB'}{'':3s}"
f"{'PSNR:':6s}{psnr_eval:>5.2f}{' dB'}{'':3s}{'time[s]:':10s}{time_eval:>7.2f}"
)
ODPNet training epochs: 200 time[s]: 4.19
ODPNet testing SNR: 6.78 dB PSNR: 17.16 dB time[s]: 13.08
Plot comparison.
[12]:
np.random.seed(123)
indx = np.random.randint(0, high=maxn)
fig, ax = kplt.subplots(nrows=1, ncols=3, figsize=(15, 5))
kplt.imview(test_ds["label"][indx, ..., 0], title="Ground truth", show_cbar=None, ax=ax[0])
kplt.imview(
test_ds["image"][indx, ..., 0],
title="Sinogram",
show_cbar=None,
ax=ax[1],
)
kplt.imview(
output[indx, ..., 0],
title="ODPNet Reconstruction\nSNR: %.2f (dB), PSNR: %.2f"
% (
metric.snr(test_ds["label"][indx, ..., 0], output[indx, ..., 0]),
metric.psnr(test_ds["label"][indx, ..., 0], output[indx, ..., 0]),
),
ax=ax[2],
)
divider = make_axes_locatable(ax[2])
cax = divider.append_axes("right", size="5%", pad=0.2)
fig.colorbar(ax[2].get_images()[0], cax=cax, label="arbitrary units")
fig.show()
Plot convergence statistics. Statistics are generated only if a training cycle was done (i.e. if not reading final epoch results from checkpoint).
[13]:
if stats_object is not None and len(stats_object.iterations) > 0:
hist = stats_object.history(transpose=True)
fig, ax = kplt.subplots(nrows=1, ncols=2, figsize=(12, 5))
kplt.plot(
hist.Epoch,
np.array((hist.Train_Loss, hist.Eval_Loss)).T,
ylog=True,
title="Loss function",
xlabel="Epoch",
ylabel="Loss value",
legend=("Train", "Test"),
ax=ax[0],
)
kplt.plot(
hist.Epoch,
np.array((hist.Train_SNR, hist.Eval_SNR)).T,
title="Metric",
xlabel="Epoch",
ylabel="SNR (dB)",
legend=("Train", "Test"),
ax=ax[1],
)
fig.show()