Skip to content

Commit

Permalink
Merge pull request #21 from openspyrit/2022_OE_spyrit2
Browse files Browse the repository at this point in the history
Make it compatible with spyrit v2 in beta
  • Loading branch information
tbaudier authored Jan 23, 2023
2 parents b50d269 + 19baadc commit c1e24ce
Show file tree
Hide file tree
Showing 9 changed files with 604 additions and 100 deletions.
411 changes: 411 additions & 0 deletions 2022_OE_spyrit2/caption.tex

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions 2022_OE_spyrit2/fig8_mask.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# -*- coding: utf-8 -*-

import numpy as np
from spyrit.misc.statistics import Cov2Var, img2mask
from spyrit.misc.statistics import Cov2Var
from spyrit.misc.sampling import img2mask
from matplotlib import pyplot as plt
from spyrit.misc.disp import add_colorbar, noaxis
from spyrit.misc.disp import noaxis

from pathlib import Path


#%%
M = [4096, 1024, 512] #[1024, 4095, 4096]
N = 128
Expand Down
27 changes: 17 additions & 10 deletions 2022_OE_spyrit2/fig8_recon.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,23 @@
import torch
import numpy as np
import spyrit.misc.walsh_hadamard as wh
import math

from matplotlib import pyplot as plt

from spyrit.learning.model_Had_DCAN import *
from spyrit.misc.disp import torch2numpy, imagesc, plot

from spyrit.learning.nets import *
from spyrit.restructured.Updated_Had_Dcan import *
from spyrit.misc.metrics import psnr_
from spyrit.misc.disp import imagesc, add_colorbar, noaxis
from spyrit.misc.statistics import Cov2Var

from spyrit.core.Acquisition import Acquisition_Poisson_approx_Gauss
from spyrit.core.Forward_Operator import Forward_operator_Split_ft_had
from spyrit.core.Preprocess import Preprocess_Split_diag_poisson
from spyrit.core.Data_Consistency import Generalized_Orthogonal_Tikhonov, Pinv_orthogonal
from spyrit.core.training import load_net
from spyrit.core.neural_network import Unet, Identity
from spyrit.core.reconstruction import Pinv_Net, DC2_Net

from spyrit.misc.disp import add_colorbar, noaxis
from spyrit.misc.sampling import Permutation_Matrix

from spas import read_metadata, plot_color, spectral_binning, spectral_slicing

Expand Down Expand Up @@ -51,7 +58,7 @@
net_arch = 'dc-net' # ['dc-net','pinv-net']
net_denoi = 'unet' # ['unet', 'cnn']
net_data = 'imagenet' # 'imagenet'
save_root = Path('./recon_128_b/')
save_root = Path('./recon_128/')

#%% covariance matrix and network filnames
stat_folder_rec = Path('../../stat/ILSVRC2012_v10102019/')
Expand Down Expand Up @@ -102,9 +109,9 @@
Hperm = Perm_rec @ H
Pmat = Hperm[:M,:]

Forward = Split_Forward_operator_ft_had(Pmat, Perm_rec, N_rec, N_rec)
Noise = Bruit_Poisson_approx_Gauss(N0, Forward)
Prep = Split_diag_poisson_preprocess(N0, M, N_rec**2)
Forward = Forward_operator_Split_ft_had(Pmat, Perm_rec, N_rec, N_rec)
Noise = Acquisition_Poisson_approx_Gauss(N0, Forward)
Prep = Preprocess_Split_diag_poisson(N0, M, N_rec**2)

Denoi = Unet()
Cov_perm = Perm_rec @ Cov_rec @ Perm_rec.T
Expand Down
16 changes: 11 additions & 5 deletions 2022_OE_spyrit2/readMe.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Optics Express paper, in preparation (revision after rejection)
*Authors:* N Ducros, A Lorente Mur, G. Beneti-Martin, L Mahieu-Williame
# OpenSpyrit: an Ecosystem for Reproducible Single-Pixel Hyperspectral Imaging

This repository contains the code that produces the results reported in

> G. Beneti-Martin, L Mahieu-Williame, T Baudier, N Ducros, "OpenSpyrit: an Ecosystem for Reproducible Single-Pixel Hyperspectral Imaging," Preprint, (2022). [HAL (open access PDF)](https://hal.science/hal-03910077).
*Contact:* [email protected], CREATIS Laboratory, University of Lyon, France.

## Install the dependencies
Expand All @@ -20,7 +24,7 @@

Alternatively, you can clone an existing environment with `conda create --name new-env --clone existing-env`

1. Clone the spyrit package, and install the version in the `towards_v2` branch:
1. Clone the spyrit package, and install the version in the `towards_v2_fadoua` branch:

```shell
git clone https://github.com/openspyrit/spyrit.git
Expand All @@ -36,6 +40,7 @@
cd spas
pip install -e .
```
> Todo: Upate spas package to remove unwanted imports of ``reconstruct_nn``

## Get the scripts, networks and raw data

Expand All @@ -57,12 +62,13 @@ Unzip the folders. The directory structure should be

```
|---spyrit-examples
| |---2022_OE
| |---2022_OE_spyrit2
| | |---data
| | | |---
| | |---models
| | | |---
| | |---expe_data_analysis.py
| | |---fig8_recon.py
| | |---fig8_mask.py
| | |---
```

Expand Down
27 changes: 16 additions & 11 deletions 2022_OE_spyrit2/recon_arbitrary_M_N.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,22 @@
#%%
import torch
import numpy as np
import spyrit.misc.walsh_hadamard as wh
import math

from matplotlib import pyplot as plt

from spyrit.learning.model_Had_DCAN import *
from spyrit.misc.disp import torch2numpy, imagesc, plot

from spyrit.learning.nets import *
from spyrit.restructured.Updated_Had_Dcan import *
from spyrit.misc.metrics import psnr_
from spyrit.misc.disp import imagesc, add_colorbar, noaxis
from spyrit.core.Acquisition import Acquisition_Poisson_approx_Gauss
from spyrit.core.Forward_Operator import Forward_operator_Split_ft_had
from spyrit.core.Preprocess import Preprocess_Split_diag_poisson
from spyrit.core.Data_Consistency import Generalized_Orthogonal_Tikhonov
from spyrit.core.training import load_net
from spyrit.core.neural_network import Unet
from spyrit.core.reconstruction import DC2_Net

from spyrit.misc.disp import noaxis, add_colorbar
from spyrit.misc.statistics import Cov2Var
from spyrit.misc.sampling import Permutation_Matrix
import spyrit.misc.walsh_hadamard as wh

from pathlib import Path

Expand Down Expand Up @@ -101,9 +106,9 @@
Hperm = Perm_rec @ H
Pmat = Hperm[:M,:]

Forward = Split_Forward_operator_ft_had(Pmat, Perm_rec, N_rec, N_rec)
Noise = Bruit_Poisson_approx_Gauss(N0, Forward)
Prep = Split_diag_poisson_preprocess(N0, M, N_rec**2)
Forward = Forward_operator_Split_ft_had(Pmat, Perm_rec, N_rec, N_rec)
Noise = Acquisition_Poisson_approx_Gauss(N0, Forward)
Prep = Preprocess_Split_diag_poisson(N0, M, N_rec**2)

Denoi = Unet()
Cov_perm = Perm_rec @ Cov_rec @ Perm_rec.T
Expand Down
52 changes: 37 additions & 15 deletions 2022_OE_spyrit2/supplemental_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,38 @@
Path('star_sector_x2'),
Path('star_sector_x12'),
Path('star_sector_linear'),
Path('half_SiemensStar_with_ThorLabsName/'),
Path('tomato_slice_x2'),
Path('tomato_slice_x12'),
Path('cat'),
Path('cat_linear'),
Path('horse')]
Path('horse'),
Path('Bitten_Apple_t_5min-im_64x64_Zoom_x1_ti_10ms_tc_0.5ms/'),
Path('color_checker_full_FOV_64x64_Zoom_x1_ti_15ms_tc_0.2ms/'),
Path('green_Tree_leaf_ti_20ms_zoomx4_objx40/'),
Path('Thorlabs_box_ti_10ms_without_telecentric/'),
]

sample_list = ['USAF target (×2 zoom)',
'USAF target (×12 zoom)',
'Siemens star (×2 zoom)',
'Siemens star (×12 zoom)',
'Siemens star with linear variable filter',
'Tomato slice (×2 zoom)',
'Tomato slice (×12 zoom)',
'STL10 cat',
'STL10 cat with linear variable filter',
'STL10 horse']

sample_list = ['USAF ×2 zoom',
'USAF ×12 zoom',
'Star sector ×2 zoom',
'Star sector ×12 zoom',
'Colored star sector',
'Off centered star sector',
'Tomato slice ×2 zoom',
'Tomato slice ×12 zoom',
'Cat',
'Colored cat',
'Horse',
'Apple',
'Color checker',
'Leaf',
'Thorlabs box',
]

too_long = ['Bitten_Apple_t_5min-im_64x64_Zoom_x1_ti_10ms_tc_0.5ms',
'color_checker_full_FOV_64x64_Zoom_x1_ti_15ms_tc_0.2ms']

#%% Loop over compression ratios, network architectures, and image domain denoisers
with open('caption.tex', 'w') as f:
Expand All @@ -55,14 +70,21 @@
f.write('We consider the following samples\n')
f.write('\\begin{enumerate}\n')
for data_folder, sample in zip(data_folder_list,sample_list):
f.write(f'\\item {sample}, see Sec. \\ref{{sec:{data_folder.name}}}\n')
f.write(f'\\item {sample} (see Sec. \\ref{{sec:{data_folder.name}}}).\n')
f.write('\end{enumerate}')
f.write('\n\n')

f.write('\\newpage\n\n')

for data_folder, sample in zip(data_folder_list,sample_list):

if data_folder.name not in too_long:
save_prefix = data_folder.name
elif data_folder.name == too_long[0]:
save_prefix = 'Bitten_Apple'
elif data_folder.name == too_long[1]:
save_prefix = 'color_checker'

text = f'''\subsection{{{sample}}}
\label{{sec:{data_folder.name}}}\n'''
f.write(text)
Expand All @@ -73,15 +95,15 @@
net_suffix = f'{sub}_N0_{N0}_N_128_M_{M}_epo_30_lr_0.001_sss_10_sdr_0.5_bs_256_reg_1e-07'
net_title = f'{net_arch}_{net_denoi}_{net_data}_{net_suffix}'

full_path_slice = (data_folder.name + '_slice_' + net_title)
full_path_bin = (data_folder.name + '_bin_' + net_title)
full_path_slice = (save_prefix + '_slice_' + net_title)
full_path_bin = (save_prefix + '_bin_' + net_title)
print(full_path_bin)

text = f'''
\\begin{{center}}
\includegraphics[width=\linewidth,trim={{0 2.7cm 0 2.5cm}},clip]{{{{{full_path_bin}}}.pdf}}
\includegraphics[width=\linewidth,trim={{0 2.7cm 0 2.9cm}},clip]{{{{{full_path_slice}}}.pdf}}
\captionof{{figure}}{{Reconstruction of four spectral bins (top) and channels (bottom) using a {net_arch} trained for $M = {M}$ measurements.}}
\captionof{{figure}}{{Reconstruction of four spectral bins (top) and channels (bottom) using a {net_arch} trained for $K = {M}$ measurements.}}
\end{{center}}'''#.format(full_path_slice, full_path_bin)
#\captionof{{figure}}{{Reconstruction of four spectral bins (top) and channels (bottom) using a {net_arch} trained at $\\alpha = {N0}$ photons for $M = {M}$ measurements. The image domain denoiser is a {net_denoi}.}}

Expand Down
Loading

0 comments on commit c1e24ce

Please sign in to comment.