Skip to content

Instantly share code, notes, and snippets.

@g-leech
Created April 14, 2018 17:55
Show Gist options
  • Save g-leech/545ea8ab1b579c8915fbcead1b2aa115 to your computer and use it in GitHub Desktop.
Save g-leech/545ea8ab1b579c8915fbcead1b2aa115 to your computer and use it in GitHub Desktop.
Test for grayscale invisibility cloak
# Making sure grayscale doesn't hide objects
import numpy as np
from ai_safety_gridworlds.environments.side_effects_sokoban import SideEffectsSokobanEnvironment as sokoban_game
env = sokoban_game(level=0)
objs = env.reset().observation['board']
print(objs)
WALL = 0
SPACE = 1
def how_many_objects(grid, WALL=152, SPACE=219) :
return len(grid[(grid != WALL) & (grid != SPACE)] )
actualNumObjs = how_many_objects(objs, 0, 1)
print(actualNumObjs, "objects in this grid.")
# Besides walls and empty spaces, how many types of object?
def count_object_types(grid, WALL=152, SPACE=219) :
occupiedSpaces = grid[(grid != WALL) & (grid != SPACE)]
return np.unique(occupiedSpaces, return_counts=True)
TYPES_INDEX = 0
actualNumTypes = len(count_object_types(objs, 0, 1)[TYPES_INDEX])
print(actualNumTypes, "types of object in this grid")
objs = env.reset().observation['RGB']
objs = np.moveaxis(objs, 0, -1)
wZ, wX, wY = env.observation_spec()['RGB'].shape
resize = tf.placeholder(shape=[wX, wY, wZ], dtype=tf.uint8)
gray_frame = tf.squeeze( tf.image.rgb_to_grayscale(objs) )
with tf.Session() as sess:
sess.run( gray_frame, { resize: objs } )
grayed = gray_frame.eval()
print(grayed)
plt.figure()
plt.imshow(gray_frame.eval()/255.0, cmap='gray')
plt.axis('off')
plt.show()
grayNumObjs = how_many_objects(grayed)
grayNumTypes = len(count_object_types(grayed)[TYPES_INDEX])
print(grayNumTypes)
assert(actualNumObjs == grayNumObjs)
assert(actualNumTypes == grayNumTypes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment