Skip to content

Commit

Permalink
Merge pull request #4 from juglab/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
tibuch authored Jan 11, 2021
2 parents dd80473 + ce3d5d0 commit 0b89fb2
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Build Python package:
`python setup.py bdist_wheel`

Build singularity recipe:
`neurodocker generate singularity -b nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04 -p apt --copy /home/tibuch/Gitrepos/FourierImageTransformer/dist/fourier_image_transformers-0.1.2-py3-none-any.whl /fourier_image_transformers-0.1.2-py3-none-any.whl --miniconda create_env=fit conda_install='python=3.7 astra-toolbox pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch -c astra-toolbox/label/dev' pip_install='/fourier_image_transformers-0.1.2-py3-none-any.whl' activate=true --entrypoint "/neurodocker/startup.sh python" > v0.1.2.Singularity`
`neurodocker generate singularity -b nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04 -p apt --copy /home/tibuch/Gitrepos/FourierImageTransformer/dist/fourier_image_transformers-0.1.3-py3-none-any.whl /fourier_image_transformers-0.1.3-py3-none-any.whl --miniconda create_env=fit conda_install='python=3.7 astra-toolbox pytorch torchvision torchaudio cudatoolkit=10.2 -c pytorch -c astra-toolbox/label/dev' pip_install='/fourier_image_transformers-0.1.3-py3-none-any.whl' activate=true --entrypoint "/neurodocker/startup.sh python" > v0.1.3.Singularity`

Build singularity container:
`sudo singularity build fit_v0.1.2.simg v0.1.2.Singularity`
`sudo singularity build fit_v0.1.3.simg v0.1.3.Singularity`
10 changes: 5 additions & 5 deletions examples/DataModule - MNIST Tomo .ipynb

Large diffs are not rendered by default.

23 changes: 17 additions & 6 deletions fit/datamodules/tomo_rec/FCDataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,21 @@ def __len__(self):


class FourierCoefficientDataset(FCDataset):
def __init__(self, ds, part='train', img_shape=42):
def __init__(self, ds, mag_min, mag_max, part='train', img_shape=42):
super().__init__(ds, part=part, img_shape=img_shape)
if mag_min == None and mag_max == None:
tmp_sinos = []
for i in np.random.permutation(len(self.ds))[:200]:
sino, _ = self.ds[i]
tmp_sinos.append(sino)

tmp_sinos = torch.stack(tmp_sinos)
tmp_sinos = torch.fft.rfftn(tmp_sinos, dim=[1,2]).abs()
self.mag_min = tmp_sinos.min()
self.mag_max = tmp_sinos.max()
else:
self.mag_min = mag_min
self.mag_max = mag_max

def __getitem__(self, item):
sino, img = self.ds[item]
Expand All @@ -36,14 +49,12 @@ def __getitem__(self, item):
img_mag = torch.log(img_mag)
img_phi = img_fft.angle()

mag_min, mag_max = sino_mag.min(), sino_mag.max()

sino_mag = 2 * (sino_mag - mag_min) / (mag_max - mag_min) - 1
img_mag = 2 * (img_mag - mag_min) / (mag_max - mag_min) - 1
sino_mag = 2 * (sino_mag - self.mag_min) / (self.mag_max - self.mag_min) - 1
img_mag = 2 * (img_mag - self.mag_min) / (self.mag_max - self.mag_min) - 1

sino_phi = 2 * sino_phi / (2 * np.pi) - 1
img_phi = 2 * img_phi / (2 * np.pi) - 1

sino_fft = torch.stack([sino_mag.flatten(), sino_phi.flatten()], dim=-1)
img_fft = torch.stack([img_mag.flatten(), img_phi.flatten()], dim=-1)
return sino_fft, img_fft, img, (mag_min.unsqueeze(-1), mag_max.unsqueeze(-1))
return sino_fft, img_fft, img, (self.mag_min.unsqueeze(-1), self.mag_max.unsqueeze(-1))
28 changes: 22 additions & 6 deletions fit/datamodules/tomo_rec/TRecDataModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ def __init__(self, root_dir, batch_size, num_angles=15, inner_circle=True):
self.gt_ds = None
self.mean = None
self.std = None
self.mag_min = None
self.mag_max = None

def setup(self, stage: Optional[str] = None):
mnist_test = MNIST(self.root_dir, train=False, download=True).data.type(torch.float32)
Expand Down Expand Up @@ -117,20 +119,27 @@ def setup(self, stage: Optional[str] = None):
GroundTruthDataset(mnist_train, mnist_val, mnist_test),
num_angles=self.num_angles, im_shape=70, impl='astra_cpu', inner_circle=self.inner_circle)

tmp_fcds = FourierCoefficientDataset(self.gt_ds, mag_min=None, mag_max=None, part='train',
img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE)
self.mag_min = tmp_fcds.mag_min
self.mag_max = tmp_fcds.mag_max

def train_dataloader(self, *args, **kwargs) -> DataLoader:
return DataLoader(
FourierCoefficientDataset(self.gt_ds, part='train', img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE),
FourierCoefficientDataset(self.gt_ds, mag_min=self.mag_min, mag_max=self.mag_max, part='train',
img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE),
batch_size=self.batch_size, num_workers=2)

def val_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
return DataLoader(
FourierCoefficientDataset(self.gt_ds, part='validation',
FourierCoefficientDataset(self.gt_ds, mag_min=self.mag_min, mag_max=self.mag_max, part='validation',
img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE),
batch_size=self.batch_size, num_workers=2)

def test_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
return DataLoader(
FourierCoefficientDataset(self.gt_ds, part='test', img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE),
FourierCoefficientDataset(self.gt_ds, mag_min=self.mag_min, mag_max=self.mag_max, part='test',
img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE),
batch_size=1)


Expand Down Expand Up @@ -173,18 +182,25 @@ def setup(self, stage: Optional[str] = None):
GroundTruthDataset(gt_train, gt_val, gt_test),
num_angles=self.num_angles, im_shape=450, impl='astra_cpu', inner_circle=self.inner_circle)

tmp_fcds = FourierCoefficientDataset(self.gt_ds, mag_min=None, mag_max=None, part='train',
img_shape=MNISTTomoFourierTargetDataModule.IMG_SHAPE)
self.mag_min = tmp_fcds.mag_min
self.mag_max = tmp_fcds.mag_max

def train_dataloader(self, *args, **kwargs) -> DataLoader:
return DataLoader(
FourierCoefficientDataset(self.gt_ds, part='train', img_shape=LoDoPaBFourierTargetDataModule.IMG_SHAPE),
FourierCoefficientDataset(self.gt_ds, mag_min=self.mag_min, mag_max=self.mag_max, part='train',
img_shape=LoDoPaBFourierTargetDataModule.IMG_SHAPE),
batch_size=self.batch_size, num_workers=2)

def val_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
return DataLoader(
FourierCoefficientDataset(self.gt_ds, part='validation',
FourierCoefficientDataset(self.gt_ds, mag_min=self.mag_min, mag_max=self.mag_max, part='validation',
img_shape=LoDoPaBFourierTargetDataModule.IMG_SHAPE),
batch_size=self.batch_size, num_workers=2)

def test_dataloader(self, *args, **kwargs) -> Union[DataLoader, List[DataLoader]]:
return DataLoader(
FourierCoefficientDataset(self.gt_ds, part='test', img_shape=LoDoPaBFourierTargetDataModule.IMG_SHAPE),
FourierCoefficientDataset(self.gt_ds, mag_min=self.mag_min, mag_max=self.mag_max, part='test',
img_shape=LoDoPaBFourierTargetDataModule.IMG_SHAPE),
batch_size=1)
2 changes: 1 addition & 1 deletion fit/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.2'
__version__ = '0.1.3'
4 changes: 2 additions & 2 deletions scripts/TRec_MNIST.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def main():
dst_flatten_coords=model.dst_flatten_coords,
dst_order=model.dst_order)

test_res = trainer.test(model, test_dl)[0]
test_res = trainer.test(model, datamodule=dm)[0]
out_res = {
"Mean PSNR": test_res["Mean PSNR"].item(),
"SEM PSNR": test_res["SEM PSNR"].item()
Expand All @@ -95,7 +95,7 @@ def main():
dst_flatten_coords=model.dst_flatten_coords,
dst_order=model.dst_order)

test_res = trainer.test(model, test_dl)[0]
test_res = trainer.test(model, datamodule=dm)[0]
out_res = {
"Mean PSNR": test_res["Mean PSNR"].item(),
"SEM PSNR": test_res["SEM PSNR"].item()
Expand Down
138 changes: 138 additions & 0 deletions singularity/v0.1.3.Singularity
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
# Generated by: Neurodocker version 0.7.0
# Latest release: Neurodocker version 0.7.0
# Timestamp: 2021/01/11 14:22:17 UTC
#
# Thank you for using Neurodocker. If you discover any issues
# or ways to improve this software, please submit an issue or
# pull request on our GitHub repository:
#
# https://github.com/ReproNim/neurodocker

Bootstrap: docker
From: nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04

%post
su - root

export ND_ENTRYPOINT="/neurodocker/startup.sh"
apt-get update -qq
apt-get install -y -q --no-install-recommends \
apt-utils \
bzip2 \
ca-certificates \
curl \
locales \
unzip
apt-get clean
rm -rf /var/lib/apt/lists/*
sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen
dpkg-reconfigure --frontend=noninteractive locales
update-locale LANG="en_US.UTF-8"
chmod 777 /opt && chmod a+s /opt
mkdir -p /neurodocker
if [ ! -f "$ND_ENTRYPOINT" ]; then
echo '#!/usr/bin/env bash' >> "$ND_ENTRYPOINT"
echo 'set -e' >> "$ND_ENTRYPOINT"
echo 'export USER="${USER:=`whoami`}"' >> "$ND_ENTRYPOINT"
echo 'if [ -n "$1" ]; then "$@"; else /usr/bin/env bash; fi' >> "$ND_ENTRYPOINT";
fi
chmod -R 777 /neurodocker && chmod a+s /neurodocker

export PATH="/opt/miniconda-latest/bin:$PATH"
echo "Downloading Miniconda installer ..."
conda_installer="/tmp/miniconda.sh"
curl -fsSL --retry 5 -o "$conda_installer" https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash "$conda_installer" -b -p /opt/miniconda-latest
rm -f "$conda_installer"
conda update -yq -nbase conda
conda config --system --prepend channels conda-forge
conda config --system --set auto_update_conda false
conda config --system --set show_channel_urls true
sync && conda clean -y --all && sync
conda create -y -q --name fit
conda install -y -q --name fit \
"python=3.7" \
"astra-toolbox" \
"pytorch" \
"torchvision" \
"torchaudio" \
"cudatoolkit=10.2" \
"-c" \
"pytorch" \
"-c" \
"astra-toolbox/label/dev"
sync && conda clean -y --all && sync
bash -c "source activate fit
pip install --no-cache-dir \
"/fourier_image_transformers-0.1.3-py3-none-any.whl""
rm -rf ~/.cache/pip/*
sync
sed -i '$isource activate fit' $ND_ENTRYPOINT


echo '{
\n "pkg_manager": "apt",
\n "instructions": [
\n [
\n "base",
\n "nvidia/cuda:10.2-cudnn7-devel-ubuntu18.04"
\n ],
\n [
\n "user",
\n "root"
\n ],
\n [
\n "_header",
\n {
\n "version": "generic",
\n "method": "custom"
\n }
\n ],
\n [
\n "copy",
\n [
\n "/home/tibuch/Gitrepos/FourierImageTransformer/dist/fourier_image_transformers-0.1.3-py3-none-any.whl",
\n "/fourier_image_transformers-0.1.3-py3-none-any.whl"
\n ]
\n ],
\n [
\n "miniconda",
\n {
\n "create_env": "fit",
\n "conda_install": [
\n "python=3.7",
\n "astra-toolbox",
\n "pytorch",
\n "torchvision",
\n "torchaudio",
\n "cudatoolkit=10.2",
\n "-c",
\n "pytorch",
\n "-c",
\n "astra-toolbox/label/dev"
\n ],
\n "pip_install": [
\n "/fourier_image_transformers-0.1.3-py3-none-any.whl"
\n ],
\n "activate": true
\n }
\n ],
\n [
\n "entrypoint",
\n "/neurodocker/startup.sh python"
\n ]
\n ]
\n}' > /neurodocker/neurodocker_specs.json

%environment
export LANG="en_US.UTF-8"
export LC_ALL="en_US.UTF-8"
export ND_ENTRYPOINT="/neurodocker/startup.sh"
export CONDA_DIR="/opt/miniconda-latest"
export PATH="/opt/miniconda-latest/bin:$PATH"

%files
/home/tibuch/Gitrepos/FourierImageTransformer/dist/fourier_image_transformers-0.1.3-py3-none-any.whl /fourier_image_transformers-0.1.3-py3-none-any.whl

%runscript
/neurodocker/startup.sh python

0 comments on commit 0b89fb2

Please sign in to comment.