Skip to content

Commit

Permalink
Changes for the Masters Thesis
Browse files Browse the repository at this point in the history
  • Loading branch information
HedgehogCode committed Jan 6, 2022
1 parent 4af6532 commit 1a68ace
Show file tree
Hide file tree
Showing 28 changed files with 2,546 additions and 809 deletions.
1 change: 1 addition & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ RUN apt-get -y install --no-install-recommends libgl1-mesa-glx

# Runtime dependencies
COPY ./requirements/requirements_docker.txt /root/requirements_docker.txt
RUN pip install Cython
RUN pip install -r /root/requirements_docker.txt

# Develop dependencies
Expand Down
14 changes: 11 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
},
// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/usr/bin/zsh",
"terminal.integrated.defaultProfile.linux": "zsh",
"terminal.integrated.profiles.linux": {
"zsh": {
"path": "/usr/bin/zsh",
"args": []
}
},
"editor.rulers": [100],
// ---- Python specific settings
"python.experiments.enabled": false,
"python.pythonPath": "/usr/local/bin/python",
"python.formatting.provider": "autopep8",
"python.formatting.provider": "black",
"python.linting.pylintEnabled": false,
"jupyter.alwaysTrustNotebooks": true,
"python.languageServer": "Pylance",
Expand All @@ -21,7 +28,8 @@
// ---- Python specific extensions
"ms-python.python",
"kevinrose.vsc-python-indent",
"ms-python.vscode-pylance"
"ms-python.vscode-pylance",
"njpwerner.autodocstring"
],
// ---- Run with GPU driver
"runArgs": [
Expand Down
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
This repository implements two algorithms for image reconstruction using a Gaussian denoiser in TensorFlow 2.

Algorithms:
* **DMSP:** S. A. Bigdeli, P. Favaro, M. Jin, and M. Zwicker, [“Deep Mean-Shift Priors for Image Restoration,”](http://papers.nips.cc/paper/6678-deep-mean-shift-priors-for-image-restoration.pdf) in Advances in Neural Information Processing Systems 30, 2017, pp. 763–772.
* **HQS:** K. Zhang, Y. Li, W. Zuo, L. Zhang, L. Van Gool, and R. Timofte, [“Plug-and-Play Image Restoration with Deep Denoiser Prior,”](http://arxiv.org/abs/2008.13751) arXiv:2008.13751 [cs, eess], Aug. 2020

- **DMSP:** S. A. Bigdeli, P. Favaro, M. Jin, and M. Zwicker, [“Deep Mean-Shift Priors for Image Restoration,”](http://papers.nips.cc/paper/6678-deep-mean-shift-priors-for-image-restoration.pdf) in Advances in Neural Information Processing Systems 30, 2017, pp. 763–772.
- **HQS:** K. Zhang, Y. Li, W. Zuo, L. Zhang, L. Van Gool, and R. Timofte, [“Plug-and-Play Image Restoration with Deep Denoiser Prior,”](http://arxiv.org/abs/2008.13751) IEEE Trans. Pattern Anal. Mach. Intell., pp. 1–1, 2021

## Getting Started

Install using pip

```
$ pip install git+https://github.com/HedgehogCode/deep-plug-and-play-prior
```
Expand All @@ -21,10 +22,12 @@ See the example [notebooks](notebooks/) for usage examples.
## Related Repositories

DMSP Implementations (significantly slower):
* Matlab: https://github.com/siavashBigdeli/DMSP (only deblur)
* TensorFlow 1: https://github.com/siavashBigdeli/DMSP-tensorflow (only deblur)
* TensorFlow 2: https://github.com/siavashBigdeli/DMSP-TF2 (deblur and sr (but not bicubic))

- Matlab: https://github.com/siavashBigdeli/DMSP (only deblur)
- TensorFlow 1: https://github.com/siavashBigdeli/DMSP-tensorflow (only deblur)
- TensorFlow 2: https://github.com/siavashBigdeli/DMSP-TF2 (deblur and sr (but not bicubic))

HQS:
* Matlab: https://github.com/cszn/IRCNN
* Pytorch: https://github.com/cszn/DPIR

- Matlab: https://github.com/cszn/IRCNN
- Pytorch: https://github.com/cszn/DPIR
15 changes: 13 additions & 2 deletions dppp/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from .utils import *
from .callback import *
from .metrics import *
from .dmsp import dmsp_deblur_nb, dmsp_super_resolve, dmsp_inpaint
from .hqs import hqs_deblur_nb, hqs_super_resolve, hqs_inpaint
from .dmsp import (
dmsp_deblur_nb,
dmsp_deblur_blind,
dmsp_super_resolve,
dmsp_inpaint,
)
from .hqs import (
hqs_deblur_nb,
hqs_super_resolve,
hqs_inpaint,
hqs_video_super_resolve,
hqs_lf_super_resolve,
)
83 changes: 50 additions & 33 deletions dppp/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@
# TENSORBOARD
###############################################################################

def callback_tb_image(tb_log_dir: str,
summary_name: str,
frequency: int
) -> Callback:

def callback_tb_image(tb_log_dir: str, summary_name: str, frequency: int) -> Callback:
tb_writer = tf.summary.create_file_writer(tb_log_dir)

def call(x: TensorLike, step: TensorLike):
Expand All @@ -25,10 +23,9 @@ def call(x: TensorLike, step: TensorLike):
return call


def callback_tb_scalar(tb_log_dir: str,
summary_name: str,
summary_fn: Callable[[TensorLike], TensorLike]
) -> Callback:
def callback_tb_scalar(
tb_log_dir: str, summary_name: str, summary_fn: Callable[[TensorLike], TensorLike]
) -> Callback:
tb_writer = tf.summary.create_file_writer(tb_log_dir)

def call(x: TensorLike, step: TensorLike):
Expand All @@ -38,67 +35,87 @@ def call(x: TensorLike, step: TensorLike):
return call


def callback_tb_psnr(tb_log_dir: str, summary_name: str, gt_image: TensorLike) -> Callback:
return callback_tb_scalar(tb_log_dir=tb_log_dir, summary_name=summary_name,
summary_fn=lambda x: tf.reduce_mean(dppp.psnr(x, gt_image)))
def callback_tb_psnr(
tb_log_dir: str, summary_name: str, gt_image: TensorLike
) -> Callback:
return callback_tb_scalar(
tb_log_dir=tb_log_dir,
summary_name=summary_name,
summary_fn=lambda x: tf.reduce_mean(dppp.psnr(x, gt_image)),
)


def callback_tb_ssim(tb_log_dir: str, summary_name: str, gt_image: TensorLike) -> Callback:
return callback_tb_scalar(tb_log_dir=tb_log_dir, summary_name=summary_name,
summary_fn=lambda x: tf.reduce_mean(dppp.ssim(x, gt_image)))
def callback_tb_ssim(
tb_log_dir: str, summary_name: str, gt_image: TensorLike
) -> Callback:
return callback_tb_scalar(
tb_log_dir=tb_log_dir,
summary_name=summary_name,
summary_fn=lambda x: tf.reduce_mean(dppp.ssim(x, gt_image)),
)


###############################################################################
# TF.PRINT
###############################################################################

def callback_print_scalar(name: str,
frequency: int,
summary_fn: Callable[[TensorLike], TensorLike]
) -> Callback:

def callback_print_scalar(
name: str, frequency: int, summary_fn: Callable[[TensorLike], TensorLike]
) -> Callback:
def call(x: TensorLike, step: TensorLike):
if step % frequency == 0: # type: ignore
tf.print('Step:', step, ', ', name, ':', summary_fn(x))
tf.print("Step:", step, ", ", name, ":", summary_fn(x))

return call


def callback_print_psnr(name: str, frequency: int, gt_image: TensorLike) -> Callback:
return callback_print_scalar(name=name, frequency=frequency,
summary_fn=lambda x: tf.reduce_mean(dppp.psnr(x, gt_image)))
return callback_print_scalar(
name=name,
frequency=frequency,
summary_fn=lambda x: tf.reduce_mean(dppp.psnr(x, gt_image)),
)


def callback_print_ssim(name: str, frequency: int, gt_image: TensorLike) -> Callback:
return callback_print_scalar(name=name, frequency=frequency,
summary_fn=lambda x: tf.reduce_mean(dppp.ssim(x, gt_image)))
return callback_print_scalar(
name=name,
frequency=frequency,
summary_fn=lambda x: tf.reduce_mean(dppp.ssim(x, gt_image)),
)


###############################################################################
# CSV
###############################################################################

def callback_csv_metric(metrics: Dict[str, Tuple[MetricFnType, int]],
num_steps: int): # TODO typing -> Tuple[Callable[[tf.Tensor, tf.Tensor], None], tf.Variable]:

def callback_csv_metric(metrics: Dict[str, Tuple[MetricFnType, int]], num_steps: int):
metric_names = list(metrics.keys())
csv = tf.Variable('Step,Image_Id,' + ','.join(metric_names) + '\n')
csv = tf.Variable("Step,Image_Id," + ",".join(metric_names) + "\n")

def for_image(image_id: str, gt_image: TensorLike):

@tf.function(input_signature=(tf.TensorSpec(shape=[None, None, None, None], dtype=tf.float32),
tf.TensorSpec(shape=[], dtype=tf.int64)))
@tf.function(
input_signature=(
tf.TensorSpec(shape=[None, None, None, None], dtype=tf.float32),
tf.TensorSpec(shape=[], dtype=tf.int64),
)
)
def call(x: TensorLike, step: TensorLike):
def val_for_metric(m):
metric_fn, per_step = metrics[m]
if step == num_steps - 1 or (step + 1) % per_step == 0: # type: ignore
val = metric_fn(x, gt_image)[0] # type: ignore
else:
val = tf.constant(float('nan'))
val = tf.constant(float("nan"))
return tf.strings.as_string(val)

values = [val_for_metric(m) for m in metric_names]
line = tf.strings.join([tf.strings.as_string(
step), image_id, *values], separator=',')
csv.assign(tf.strings.join([csv, line, '\n']))
line = tf.strings.join(
[tf.strings.as_string(step), image_id, *values], separator=","
)
csv.assign(tf.strings.join([csv, line, "\n"]))

return call

Expand Down
Loading

0 comments on commit 1a68ace

Please sign in to comment.