This repository has been archived by the owner on Apr 6, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 952
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move SafeActionSpace to wrappers.experimental module (#101)
* Experimental support for observation cropping 1. Create a new experimental package 2. Add ObservationCropping 3. Move action_space.SafeActionSpace to experimental.action_space.SafeActionSpace just to give an demonstration of how this new experimentatal package might be used. Fixes #55 Signed-off-by: Jesper Derehag <[email protected]> * Clarify warning * Update docs * Maintain backwards compatibility * Fix warnings * Import correctly * Fix imports * Fix references to non-experimental SafeActionSpace * Fix deprecation warning * Touch import ordering * Optimize imports * Fix imports * Update changelog
- Loading branch information
1 parent
cb048b3
commit b9815e8
Showing
8 changed files
with
135 additions
and
54 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
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,2 @@ | ||
from universe.wrappers.experimental.action_space import SafeActionSpace | ||
from universe.wrappers.experimental.observation import CropObservations |
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,24 @@ | ||
import logging | ||
|
||
from universe.wrappers.action_space import SafeActionSpace as _SafeActionSpace | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
|
||
class SafeActionSpace(_SafeActionSpace): | ||
""" | ||
Recall that every universe environment receives a list of VNC events as action. | ||
There exist many environments for which the set of relevant action is much smaller | ||
and is known. For example, Atari environments have a modest number of keys, | ||
so this wrapper, when applied to an Atari environment will reduce its action space. | ||
Doing so is very convenient for research, since today's RL algorithms rely on random | ||
exploration, which is hurt by small action spaces. As our algorithms get better | ||
and we switch to using the raw VNC commands, this wrapper will become less important. | ||
NOTE: This will be the new location for SafeActionSpace, however the logic must currently remain in | ||
wrappers.SafeActionSpace in order to maintain backwards compatibility. | ||
""" | ||
|
||
def _deprecation_warning(self): | ||
# No deprecation warning here because we are using the correct import | ||
pass |
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,43 @@ | ||
import logging | ||
|
||
from universe import vectorized, runtime_spec | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.INFO) | ||
|
||
|
||
def CropObservations(env): | ||
"""" | ||
Crops the visual observations of an environment so that they only contain the game screen. | ||
Removes anything outside the game that usually belongs to universe (browser borders and so on). | ||
""" | ||
if env.spec.tags.get('flashgames', False): | ||
spec = runtime_spec('flashgames').server_registry[env.spec.id] | ||
return _CropObservations(env, x=18, y=84, height=spec["height"], width=spec["width"]) | ||
elif (env.spec.tags.get('atari', False) and env.spec.tags.get('vnc', False)): | ||
return _CropObservations(env, height=194, width=160) | ||
else: | ||
# if unknown environment (or local atari), do nothing | ||
return env | ||
|
||
class _CropObservations(vectorized.ObservationWrapper): | ||
def __init__(self, env, height, width, x=0, y=0): | ||
super(_CropObservations, self).__init__(env) | ||
self.x = x | ||
self.y = y | ||
self.height = height | ||
self.width = width | ||
|
||
# modify observation_space? (if so, how to know depth and channels before we have seen the first frame?) | ||
# self.observation_space = Box(0, 255, shape=(height, width, 3)) | ||
|
||
def _observation(self, observation_n): | ||
return [self._crop_frame(observation) for observation in observation_n] | ||
|
||
def _crop_frame(self, frame): | ||
if frame is not None: | ||
if isinstance(frame, dict): | ||
frame['vision'] = frame['vision'][self.y:self.y + self.height, self.x:self.x + self.width] | ||
else: | ||
frame = frame[self.y:self.y + self.height, self.x:self.x + self.width] | ||
return frame |