Browse Source

Implemented anti-aliasing to make it look prettier

Maxime Chevalier-Boisvert 5 years ago
parent
commit
eed64276b5
4 changed files with 34 additions and 9 deletions
  1. 13 4
      benchmark.py
  2. 6 2
      gym_minigrid/minigrid.py
  3. 14 1
      gym_minigrid/rendering.py
  4. 1 2
      manual_control.py

+ 13 - 4
benchmark.py

@@ -12,20 +12,29 @@ parser.add_argument(
     help="gym environment to load",
     default='MiniGrid-LavaGapS7-v0'
 )
-parser.add_argument("--num_frames", default=2000)
+parser.add_argument("--num_resets", default=200)
+parser.add_argument("--num_frames", default=5000)
 args = parser.parse_args()
 
 env = gym.make(args.env_name)
-env.reset()
 
+# Benchmark env.reset
 t0 = time.time()
+for i in range(args.num_resets):
+    env.reset()
+t1 = time.time()
+dt = t1 - t0
+reset_time = (1000 * dt) / args.num_resets
 
+# Benchmark rendering
+t0 = time.time()
 for i in range(args.num_frames):
     env.render('rgb_array')
-
 t1 = time.time()
 dt = t1 - t0
 dt_per_frame = (1000 * dt) / args.num_frames
 frames_per_sec = args.num_frames / dt
+
+print('reset time: {:.1f} ms'.format(reset_time))
 print('dt per frame: {:.1f} ms'.format(dt_per_frame))
-print('frames_per_sec: {:.1f}'.format(frames_per_sec))
+print('frames per sec: {:.0f}'.format(frames_per_sec))

+ 6 - 2
gym_minigrid/minigrid.py

@@ -445,7 +445,8 @@ class Grid:
         obj,
         agent_dir=None,
         highlight=False,
-        tile_size=TILE_PIXELS
+        tile_size=TILE_PIXELS,
+        subdivs=3
     ):
         """
         Render a tile and cache the result
@@ -458,7 +459,7 @@ class Grid:
         if key in cls.tile_cache:
             return cls.tile_cache[key]
 
-        img = np.zeros(shape=(tile_size, tile_size, 3), dtype=np.uint8)
+        img = np.zeros(shape=(tile_size * subdivs, tile_size * subdivs, 3), dtype=np.uint8)
 
         # Draw the grid lines (top and left edges)
         fill_coords(img, point_in_rect(0, 0.031, 0, 1), (100, 100, 100))
@@ -483,6 +484,9 @@ class Grid:
         if highlight:
             highlight_img(img)
 
+        # Downsample the image to perform supersampling/anti-aliasing
+        img = downsample(img, subdivs)
+
         # Cache the rendered tile
         cls.tile_cache[key] = img
 

+ 14 - 1
gym_minigrid/rendering.py

@@ -1,7 +1,20 @@
 import math
 import numpy as np
 
-# TODO: anti-aliased version, fill_coords_aa?
+def downsample(img, factor):
+    """
+    Downsample an image along both dimensions by some factor
+    """
+
+    assert img.shape[0] % factor == 0
+    assert img.shape[1] % factor == 0
+
+    img = img.reshape([img.shape[0]//factor, factor, img.shape[1]//factor, factor, 3])
+    img = img.mean(axis=3)
+    img = img.mean(axis=1)
+
+    return img
+
 def fill_coords(img, fn, color):
     """
     Fill pixels of an image with coordinates matching a filter function

+ 1 - 2
manual_control.py

@@ -73,8 +73,7 @@ parser = argparse.ArgumentParser()
 parser.add_argument(
     "--env_name",
     help="gym environment to load",
-    #default='MiniGrid-MultiRoom-N6-v0'
-    default='MiniGrid-KeyCorridorS3R3-v0'
+    default='MiniGrid-MultiRoom-N6-v0'
 )
 parser.add_argument(
     "--tile_size",