Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
moving to skimage.transform.resize (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gal Leibovich committed May 23, 2019
1 parent acceb03 commit 30c2b2f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,25 @@
# limitations under the License.
#

from enum import Enum
from skimage.transform import resize

import scipy.ndimage

from rl_coach.core_types import ObservationType
from rl_coach.filters.observation.observation_filter import ObservationFilter
from rl_coach.spaces import ObservationSpace


# imresize interpolation types as defined by scipy here:
# https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.misc.imresize.html
class RescaleInterpolationType(Enum):
NEAREST = 'nearest'
LANCZOS = 'lanczos'
BILINEAR = 'bilinear'
BICUBIC = 'bicubic'
CUBIC = 'cubic'


class ObservationRescaleSizeByFactorFilter(ObservationFilter):
"""
Rescales an image observation by some factor. For example, the image size
can be reduced by a factor of 2.
Warning: this requires the input observation to be of type uint8 due to scipy requirements!
"""
def __init__(self, rescale_factor: float, rescaling_interpolation_type: RescaleInterpolationType):
def __init__(self, rescale_factor: float):
"""
:param rescale_factor: the factor by which the observation will be rescaled
:param rescaling_interpolation_type: the interpolation type for rescaling
"""
super().__init__()
self.rescale_factor = float(rescale_factor) # scipy requires float scale factors
self.rescaling_interpolation_type = rescaling_interpolation_type
self.rescale_factor = float(rescale_factor)
# TODO: allow selecting the channels dim

def validate_input_observation_space(self, input_observation_space: ObservationSpace):
Expand All @@ -58,13 +44,14 @@ def validate_input_observation_space(self, input_observation_space: ObservationS
raise ValueError("Observations with 3 dimensions must have 3 channels in the last axis (RGB)")

def filter(self, observation: ObservationType, update_internal_state: bool=True) -> ObservationType:
# scipy works only with uint8
observation = observation.astype('uint8')
rescaled_output_size = tuple([int(self.rescale_factor * dim) for dim in observation.shape[:2]])

if len(observation.shape) == 3:
rescaled_output_size += (3,)

# rescale
observation = scipy.misc.imresize(observation,
self.rescale_factor,
interp=self.rescaling_interpolation_type.value)
observation = resize(observation, rescaled_output_size, anti_aliasing=False, preserve_range=True).astype('uint8')

return observation

Expand Down
32 changes: 8 additions & 24 deletions rl_coach/filters/observation/observation_rescale_to_size_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,26 @@
#

import copy
from enum import Enum

from skimage.transform import resize
import numpy as np
import scipy.ndimage

from rl_coach.core_types import ObservationType
from rl_coach.filters.observation.observation_filter import ObservationFilter
from rl_coach.spaces import ObservationSpace, PlanarMapsObservationSpace, ImageObservationSpace


# imresize interpolation types as defined by scipy here:
# https://docs.scipy.org/doc/scipy-0.18.1/reference/generated/scipy.misc.imresize.html
class RescaleInterpolationType(Enum):
NEAREST = 'nearest'
LANCZOS = 'lanczos'
BILINEAR = 'bilinear'
BICUBIC = 'bicubic'
CUBIC = 'cubic'


class ObservationRescaleToSizeFilter(ObservationFilter):
"""
Rescales an image observation to a given size. The target size does not
necessarily keep the aspect ratio of the original observation.
Warning: this requires the input observation to be of type uint8 due to scipy requirements!
"""
def __init__(self, output_observation_space: PlanarMapsObservationSpace,
rescaling_interpolation_type: RescaleInterpolationType=RescaleInterpolationType.BILINEAR):
def __init__(self, output_observation_space: PlanarMapsObservationSpace):
"""
:param output_observation_space: the output observation space
:param rescaling_interpolation_type: the interpolation type for rescaling
"""
super().__init__()
self.output_observation_space = output_observation_space
self.rescaling_interpolation_type = rescaling_interpolation_type

if not isinstance(output_observation_space, PlanarMapsObservationSpace):
raise ValueError("The rescale filter only applies to observation spaces that inherit from "
Expand All @@ -75,20 +60,19 @@ def validate_input_observation_space(self, input_observation_space: ObservationS
self.output_observation_space.shape[self.output_observation_space.channels_axis]))

def filter(self, observation: ObservationType, update_internal_state: bool=True) -> ObservationType:
# scipy works only with uint8
observation = observation.astype('uint8')

# rescale
if isinstance(self.output_observation_space, ImageObservationSpace):
observation = scipy.misc.imresize(observation,
tuple(self.output_observation_space.shape),
interp=self.rescaling_interpolation_type.value)
observation = resize(observation, tuple(self.output_observation_space.shape), anti_aliasing=False,
preserve_range=True).astype('uint8')

else:
new_observation = []
for i in range(self.output_observation_space.shape[self.output_observation_space.channels_axis]):
new_observation.append(scipy.misc.imresize(observation.take(i, self.output_observation_space.channels_axis),
tuple(self.planar_map_output_shape),
interp=self.rescaling_interpolation_type.value))
new_observation.append(resize(observation.take(i, self.output_observation_space.channels_axis),
tuple(self.planar_map_output_shape),
preserve_range=True).astype('uint8'))
new_observation = np.array(new_observation)
observation = new_observation.swapaxes(0, self.output_observation_space.channels_axis)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
import numpy as np

from rl_coach.filters.observation.observation_rescale_size_by_factor_filter import ObservationRescaleSizeByFactorFilter, RescaleInterpolationType
from rl_coach.filters.observation.observation_rescale_size_by_factor_filter import ObservationRescaleSizeByFactorFilter
from rl_coach.spaces import ObservationSpace
from rl_coach.core_types import EnvResponse
from rl_coach.filters.filter import InputFilter
Expand All @@ -17,7 +17,7 @@ def test_filter():
env_response = EnvResponse(next_state={'observation': np.ones([20, 30, 3])}, reward=0, game_over=False)
rescale_filter = InputFilter()
rescale_filter.add_observation_filter('observation', 'rescale',
ObservationRescaleSizeByFactorFilter(0.5, RescaleInterpolationType.BILINEAR))
ObservationRescaleSizeByFactorFilter(0.5))

result = rescale_filter.filter(env_response)[0]
unfiltered_observation = env_response.next_state['observation']
Expand All @@ -33,7 +33,7 @@ def test_filter():
env_response = EnvResponse(next_state={'observation': np.ones([20, 30])}, reward=0, game_over=False)
rescale_filter = InputFilter()
rescale_filter.add_observation_filter('observation', 'rescale',
ObservationRescaleSizeByFactorFilter(2, RescaleInterpolationType.BILINEAR))
ObservationRescaleSizeByFactorFilter(2))
result = rescale_filter.filter(env_response)[0]
filtered_observation = result.next_state['observation']

Expand All @@ -47,7 +47,7 @@ def test_get_filtered_observation_space():
# error on wrong number of channels
rescale_filter = InputFilter()
rescale_filter.add_observation_filter('observation', 'rescale',
ObservationRescaleSizeByFactorFilter(0.5, RescaleInterpolationType.BILINEAR))
ObservationRescaleSizeByFactorFilter(0.5))
observation_space = ObservationSpace(np.array([10, 20, 5]))
with pytest.raises(ValueError):
filtered_observation_space = rescale_filter.get_filtered_observation_space('observation', observation_space)
Expand All @@ -64,3 +64,6 @@ def test_get_filtered_observation_space():

# make sure the original observation space is unchanged
assert np.all(observation_space.shape == np.array([10, 20, 3]))

if __name__ == '__main__':
test_filter()
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
import numpy as np

from rl_coach.filters.observation.observation_rescale_to_size_filter import ObservationRescaleToSizeFilter, RescaleInterpolationType
from rl_coach.filters.observation.observation_rescale_to_size_filter import ObservationRescaleToSizeFilter
from rl_coach.spaces import ObservationSpace, ImageObservationSpace, PlanarMapsObservationSpace
from rl_coach.core_types import EnvResponse
from rl_coach.filters.filter import InputFilter
Expand All @@ -18,9 +18,8 @@ def test_filter():
transition = EnvResponse(next_state={'observation': np.ones([20, 30, 3])}, reward=0, game_over=False)
rescale_filter = InputFilter()
rescale_filter.add_observation_filter('observation', 'rescale',
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([10, 20, 3]),
high=255),
RescaleInterpolationType.BILINEAR))
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([10, 20, 3]),
high=255)))

result = rescale_filter.filter(transition)[0]
unfiltered_observation = transition.next_state['observation']
Expand All @@ -38,8 +37,7 @@ def test_filter():
rescale_filter = InputFilter()
rescale_filter.add_observation_filter('observation', 'rescale',
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([40, 60]),
high=255),
RescaleInterpolationType.BILINEAR))
high=255)))
result = rescale_filter.filter(transition)[0]
filtered_observation = result.next_state['observation']

Expand All @@ -52,21 +50,20 @@ def test_filter():
# InputFilter(
# observation_filters=OrderedDict([('rescale',
# ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([10, 20, 1]),
# high=255),
# RescaleInterpolationType.BILINEAR))]))
# high=255)
# ))]))

# TODO: validate input to filter
# different number of axes -> error
# env_response = EnvResponse(state={'observation': np.ones([20, 30, 3])}, reward=0, game_over=False)
# rescale_filter = ObservationRescaleToSizeFilter(ObservationSpace(np.array([10, 20])),
# RescaleInterpolationType.BILINEAR)
# rescale_filter = ObservationRescaleToSizeFilter(ObservationSpace(np.array([10, 20]))
# )
# with pytest.raises(ValueError):
# result = rescale_filter.filter(transition)

# channels first -> error
with pytest.raises(ValueError):
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([3, 10, 20]), high=255),
RescaleInterpolationType.BILINEAR)
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([3, 10, 20]), high=255))


@pytest.mark.unit_test
Expand All @@ -76,15 +73,13 @@ def test_get_filtered_observation_space():
observation_filters = InputFilter()
observation_filters.add_observation_filter('observation', 'rescale',
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([5, 10, 5]),
high=255),
RescaleInterpolationType.BILINEAR))
high=255)))

# mismatch and wrong number of channels
rescale_filter = InputFilter()
rescale_filter.add_observation_filter('observation', 'rescale',
ObservationRescaleToSizeFilter(ImageObservationSpace(np.array([5, 10, 3]),
high=255),
RescaleInterpolationType.BILINEAR))
high=255)))

observation_space = PlanarMapsObservationSpace(np.array([10, 20, 5]), low=0, high=255)
with pytest.raises(ValueError):
Expand Down
3 changes: 1 addition & 2 deletions rl_coach/tests/filters/test_filters_stacking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pytest

from rl_coach.filters.observation.observation_rescale_to_size_filter import ObservationRescaleToSizeFilter, RescaleInterpolationType
from rl_coach.filters.observation.observation_rescale_to_size_filter import ObservationRescaleToSizeFilter
from rl_coach.filters.observation.observation_crop_filter import ObservationCropFilter
from rl_coach.filters.reward.reward_clipping_filter import RewardClippingFilter
from rl_coach.filters.observation.observation_stacking_filter import ObservationStackingFilter
Expand All @@ -31,7 +31,6 @@ def test_filter_stacking():

filter1 = ObservationRescaleToSizeFilter(
output_observation_space=ImageObservationSpace(np.array([110, 84]), high=255),
rescaling_interpolation_type=RescaleInterpolationType.BILINEAR
)

filter2 = ObservationCropFilter(
Expand Down

0 comments on commit 30c2b2f

Please sign in to comment.