Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rgb noise models #357

Merged
merged 56 commits into from
Dec 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
5bbab7a
CUDA!
erikwijmans Jul 24, 2019
c461879
Add tester
erikwijmans Jul 24, 2019
12a90af
Seems to be working!
erikwijmans Jul 25, 2019
2bae50b
Add dist-model
erikwijmans Jul 25, 2019
32c448f
Ranme file
erikwijmans Jul 25, 2019
0293624
Fix and add clip
erikwijmans Jul 25, 2019
38c6994
Merge branch 'redwood-noisy-depth' of github.com:facebookresearch/hab…
erikwijmans Jul 25, 2019
9aa4d5c
Make noise models return new data always
erikwijmans Jul 25, 2019
7d1ee3b
Remove numba
erikwijmans Jul 25, 2019
9ed6e4f
Formatters
erikwijmans Jul 25, 2019
e39a759
Remove include
erikwijmans Jul 25, 2019
49ca004
Support multiple devices
erikwijmans Jul 27, 2019
47f0b3e
Merge branch 'redwood-noisy-depth' of github.com:facebookresearch/hab…
erikwijmans Jul 27, 2019
c0690a1
Change large value behavior
erikwijmans Jul 30, 2019
8ddfc35
Swap to numba implemention for CPU
erikwijmans Jul 30, 2019
c40879a
Merge branch 'master' of github.com:facebookresearch/habitat-sim into…
erikwijmans Sep 4, 2019
a1fe2ce
Reviving this
erikwijmans Sep 4, 2019
d369af4
Should be pretty much good to go!
erikwijmans Sep 4, 2019
fe71349
Whoops
erikwijmans Sep 4, 2019
0265c82
Less strange
erikwijmans Sep 4, 2019
78e0b1f
CUDA cleanup
erikwijmans Sep 4, 2019
7da6cfb
Fix sort
erikwijmans Sep 4, 2019
8c7cf43
Noise multiplier
erikwijmans Sep 4, 2019
f247a0e
Fix CI
erikwijmans Sep 4, 2019
9e4339d
Get CI to pass
erikwijmans Sep 4, 2019
7f82063
Let's try this
erikwijmans Sep 4, 2019
8041a18
This
erikwijmans Sep 4, 2019
9450dc7
That should do it
erikwijmans Sep 4, 2019
239208d
No PySensorSpec
erikwijmans Sep 5, 2019
b4a2837
Merge branch 'master' of github.com:facebookresearch/habitat-sim into…
erikwijmans Sep 18, 2019
4320a20
adding initial changes.
Nov 14, 2019
fa1ad20
reformatting code with black.
Nov 19, 2019
8e91fe2
Delete rgb_noise.py
NicoGrande Nov 19, 2019
1b2c472
Delete utils_noise.py
NicoGrande Nov 19, 2019
de1abda
CUDA!
erikwijmans Jul 24, 2019
0d46420
Add tester
erikwijmans Jul 24, 2019
887fc8f
Seems to be working!
erikwijmans Jul 25, 2019
4273d32
Add dist-model
erikwijmans Jul 25, 2019
fffd4ba
Ranme file
erikwijmans Jul 25, 2019
c0a41b9
Make noise models return new data always
erikwijmans Jul 25, 2019
7275bfb
Support multiple devices
erikwijmans Jul 27, 2019
d4ff2d5
Reviving this
erikwijmans Sep 4, 2019
573d625
Should be pretty much good to go!
erikwijmans Sep 4, 2019
5ec172c
CUDA cleanup
erikwijmans Sep 4, 2019
cf0feec
adding initial changes.
Nov 14, 2019
d757354
reformatting code with black.
Nov 19, 2019
b8f2b2a
updating noise_models __init__.py
Nov 19, 2019
68b635c
adding noise models to noise_models/__init__.py
Nov 19, 2019
5874dac
remove duplicate method.
Nov 19, 2019
433758c
remove unused files.
Nov 19, 2019
f0dc2ee
Delete viewer.py
NicoGrande Nov 20, 2019
818a2f2
Delete sensor_test.py
NicoGrande Nov 20, 2019
7478af4
Delete registration.py
NicoGrande Nov 20, 2019
0308577
Adding RGB noise test to test_sensors.py.
Nov 24, 2019
578da34
Merge remote-tracking branch 'origin_nico/rgb-noise-models' into rgb-…
Nov 24, 2019
5e654b9
Merge pull request #2 from facebookresearch/master
NicoGrande Dec 6, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions habitat_sim/sensors/noise_models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@
from typing import Any, Dict

from habitat_sim.registry import registry
from habitat_sim.sensors.noise_models.gaussian_noise_model import GaussianNoiseModel
from habitat_sim.sensors.noise_models.no_noise_model import NoSensorNoiseModel
from habitat_sim.sensors.noise_models.poisson_noise_model import PoissonNoiseModel
from habitat_sim.sensors.noise_models.redwood_depth_noise_model import (
RedwoodDepthNoiseModel,
)
from habitat_sim.sensors.noise_models.salt_and_pepper_noise_model import (
SaltAndPepperNoiseModel,
)
from habitat_sim.sensors.noise_models.sensor_noise_model import SensorNoiseModel
from habitat_sim.sensors.noise_models.speckle_noise_model import SpeckleNoiseModel


def make_sensor_noise_model(name: str, kwargs: Dict[str, Any]) -> SensorNoiseModel:
Expand All @@ -32,4 +38,8 @@ def make_sensor_noise_model(name: str, kwargs: Dict[str, Any]) -> SensorNoiseMod
"SensorNoiseModel",
"RedwoodDepthNoiseModel",
"NoSensorNoiseModel",
"GaussianNoiseModel",
"SaltAndPepperNoiseModel",
"PoissonNoiseModel",
"SpeckleNoiseModel",
]
Empty file.
58 changes: 58 additions & 0 deletions habitat_sim/sensors/noise_models/gaussian_noise_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import attr
import numpy as np

from habitat_sim.registry import registry
from habitat_sim.sensor import SensorType
from habitat_sim.sensors.noise_models.sensor_noise_model import SensorNoiseModel


def _simulate(image, intensity_constant, mean, sigma):
image = image / 255.0

noise = np.random.normal(mean, sigma, image.shape)
noisy_rgb = np.clip((image + (noise * intensity_constant)), 0, 1)

noisy_rgb = (noisy_rgb * 255).astype(np.uint8)

return noisy_rgb


@attr.s(auto_attribs=True)
class GaussianNoiseModelCPUImpl:
intensity_constant: float
mean: int
sigma: int

def simulate(self, image):
return _simulate(image, self.intensity_constant, self.mean, self.sigma)


@registry.register_noise_model
@attr.s(auto_attribs=True, kw_only=True)
class GaussianNoiseModel(SensorNoiseModel):
intensity_constant: float = 0.2
mean: int = 0
sigma: int = 1

def __attrs_post_init__(self):
self._impl = GaussianNoiseModelCPUImpl(
self.intensity_constant, self.mean, self.sigma
)

@staticmethod
def is_valid_sensor_type(sensor_type: SensorType) -> bool:
return sensor_type == SensorType.COLOR

def simulate(self, image):
return self._impl.simulate(image)

def apply(self, image):
r"""Alias of `simulate()` to conform to base-class and expected API
"""
return self.simulate(image)
52 changes: 52 additions & 0 deletions habitat_sim/sensors/noise_models/poisson_noise_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import attr
import numpy as np

from habitat_sim.registry import registry
from habitat_sim.sensor import SensorType
from habitat_sim.sensors.noise_models.sensor_noise_model import SensorNoiseModel


def _simulate(image):

image = image / 255.0

values = len(np.unique(image))
values = 2 ** np.ceil(np.log2(values))

noisy = np.random.poisson(image * values) / float(values)
noisy_rgb = np.clip(noisy, 0, 1)
noisy_rgb = (noisy_rgb * 255).astype(np.uint8)

return noisy_rgb


@attr.s(auto_attribs=True)
class PoissonNoiseModelCPUImpl:
@staticmethod
def simulate(image):
return _simulate(image)


@registry.register_noise_model
@attr.s(auto_attribs=True, kw_only=True)
class PoissonNoiseModel(SensorNoiseModel):
def __attrs_post_init__(self):
self._impl = PoissonNoiseModelCPUImpl()

@staticmethod
def is_valid_sensor_type(sensor_type: SensorType) -> bool:
return sensor_type == SensorType.COLOR

def simulate(self, image):
return self._impl.simulate(image)

def apply(self, image):
r"""Alias of `simulate()` to conform to base-class and expected API
"""
return self.simulate(image)
59 changes: 59 additions & 0 deletions habitat_sim/sensors/noise_models/salt_and_pepper_noise_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.


import attr
import numpy as np

from habitat_sim.registry import registry
from habitat_sim.sensor import SensorType
from habitat_sim.sensors.noise_models.sensor_noise_model import SensorNoiseModel


def _simulate(image, s_vs_p, amount):
noisy_rgb = np.copy(image)
# Salt
num_salt = np.ceil(amount * image.size * s_vs_p)
coords = [np.random.randint(0, i - 1, int(num_salt)) for i in image.shape]
noisy_rgb[tuple(coords)] = 1

# Pepper
num_pepper = np.ceil(amount * image.size * (1.0 - s_vs_p))
coords = [np.random.randint(0, i - 1, int(num_pepper)) for i in image.shape]
noisy_rgb[tuple(coords)] = 0

return noisy_rgb


@attr.s(auto_attribs=True)
class SaltAndPepperNoiseModelCPUImpl:
s_vs_p: float
amount: float

def simulate(self, image):
return _simulate(image, self.s_vs_p, self.amount)


@registry.register_noise_model
@attr.s(auto_attribs=True, kw_only=True)
class SaltAndPepperNoiseModel(SensorNoiseModel):
s_vs_p: float = 0.5
amount: float = 0.05

def __attrs_post_init__(self):
self._impl = SaltAndPepperNoiseModelCPUImpl(self.s_vs_p, self.amount)

@staticmethod
def is_valid_sensor_type(sensor_type: SensorType) -> bool:
return sensor_type == SensorType.COLOR

def simulate(self, image):
return self._impl.simulate(image)

def apply(self, image):
r"""Alias of `simulate()` to conform to base-class and expected API
"""
return self.simulate(image)
58 changes: 58 additions & 0 deletions habitat_sim/sensors/noise_models/speckle_noise_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#!/usr/bin/env python3

# Copyright (c) Facebook, Inc. and its affiliates.
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import attr
import numpy as np

from habitat_sim.registry import registry
from habitat_sim.sensor import SensorType
from habitat_sim.sensors.noise_models.sensor_noise_model import SensorNoiseModel


def _simulate(image, intensity_constant, mean, sigma):
image = image / 255.0

noise = np.random.normal(mean, sigma, image.shape)
noisy_rgb = np.clip((image + (image * noise * intensity_constant)), 0, 1)

noisy_rgb = (noisy_rgb * 255).astype(np.uint8)

return noisy_rgb


@attr.s(auto_attribs=True)
class SpeckleNoiseModelCPUImpl:
intensity_constant: float
mean: int
sigma: int

def simulate(self, image):
return _simulate(image, self.intensity_constant, self.mean, self.sigma)


@registry.register_noise_model
@attr.s(auto_attribs=True, kw_only=True)
class SpeckleNoiseModel(SensorNoiseModel):
intensity_constant: float = 0.2
mean: int = 0
sigma: int = 1

def __attrs_post_init__(self):
self._impl = SpeckleNoiseModelCPUImpl(
self.intensity_constant, self.mean, self.sigma
)

@staticmethod
def is_valid_sensor_type(sensor_type: SensorType) -> bool:
return sensor_type == SensorType.COLOR

def simulate(self, image):
return self._impl.simulate(image)

def apply(self, image):
r"""Alias of `simulate()` to conform to base-class and expected API
"""
return self.simulate(image)
32 changes: 32 additions & 0 deletions tests/test_sensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,35 @@ def test_smoke_redwood_noise(scene, gpu2gpu, sim, make_cfg_settings):
assert np.linalg.norm(
obs["depth_sensor"].astype(np.float) - gt.astype(np.float)
) > 1.5e-2 * np.linalg.norm(gt.astype(np.float)), f"Incorrect {sensor_type} output"


@pytest.mark.gfxtest
@pytest.mark.parametrize("scene", _test_scenes)
@pytest.mark.parametrize(
"model_name",
[
"SpeckleNoiseModel",
"GaussianNoiseModel",
"SaltAndPepperNoiseModel",
"PoissonNoiseModel",
],
)
def test_rgb_noise(scene, model_name, sim, make_cfg_settings):
if not osp.exists(scene):
pytest.skip("Skipping {}".format(scene))

make_cfg_settings = {k: v for k, v in make_cfg_settings.items()}
make_cfg_settings["depth_sensor"] = False
make_cfg_settings["color_sensor"] = True
make_cfg_settings["semantic_sensor"] = False
make_cfg_settings["scene"] = scene
hsim_cfg = make_cfg(make_cfg_settings)
hsim_cfg.agents[0].sensor_specifications[0].noise_model = model_name

sim.reconfigure(hsim_cfg)

obs, gt = _render_and_load_gt(sim, scene, "color_sensor", False)

assert np.linalg.norm(
obs["color_sensor"].astype(np.float) - gt.astype(np.float)
) > 1.5e-2 * np.linalg.norm(gt.astype(np.float)), f"Incorrect {sensor_type} output"