-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add utils for simulation. * Add utils for simulating dataset. * Clean up simulators for custom and pytorch datasets.
- Loading branch information
Showing
6 changed files
with
571 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import numpy as np | ||
from scipy import ndimage | ||
import torch | ||
|
||
|
||
def add_shot_noise(image, snr_db, tol=1e-6): | ||
""" | ||
Add shot noise to image. | ||
Parameters | ||
---------- | ||
image : np.ndarray | ||
Image. | ||
snr_db : float | ||
Signal-to-noise ratio in dB. | ||
tol : float, optional | ||
Tolerance for noise variance, by default 1e-6. | ||
Returns | ||
------- | ||
np.ndarray | ||
Image with added shot noise. | ||
""" | ||
|
||
if torch.is_tensor(image): | ||
with torch.no_grad(): | ||
image_np = image.cpu().numpy() | ||
else: | ||
image_np = image | ||
|
||
if image_np.min() < 0: | ||
image_np -= image_np.min() | ||
noise = np.random.poisson(image_np) | ||
|
||
sig_var = ndimage.variance(image_np) | ||
noise_var = np.maximum(ndimage.variance(noise), tol) | ||
fact = np.sqrt(sig_var / noise_var / (10 ** (snr_db / 10))) | ||
|
||
noise = fact * noise | ||
if torch.is_tensor(image): | ||
noise = torch.from_numpy(noise).to(image.device) | ||
|
||
return image + fact * noise |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.