-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessing.py
More file actions
62 lines (48 loc) · 1.61 KB
/
Copy pathpreprocessing.py
File metadata and controls
62 lines (48 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""
Extracts and resizes the input screen
"""
from PIL import Image
import numpy as np
import torch
import torchvision.transforms as T
# default resized height
RESIZED_HEIGHT = 80
resize = T.Compose([T.ToPILImage(),
T.Resize(RESIZED_HEIGHT, interpolation=Image.CUBIC),
T.ToTensor()])
def get_screen(env):
"""Returns a resized screen of the game.
For consistency and performance reasons, the size can be manually set by set_height.
Args:
env: the Gym environment.
Returns:
batch(1) * channel * height * width tensor: the screenshot tensor
"""
# Returned screen requested by gym is 400x600x3
# Transpose it into torch order (CHW).
screen = env.render(mode='rgb_array').transpose((2, 0, 1))
# Convert to float, rescale, convert to torch tensor
screen = np.ascontiguousarray(screen, dtype=np.float32) / 255
screen = torch.from_numpy(screen)
# Resize, and add a batch dimension (BCHW)
return resize(screen).unsqueeze(0)
def get_height():
"""Returns the current image height.
"""
return resize.transforms[1].size
def set_height(height):
"""Sets the image height for resizing.
Args:
height (int): default is 80; original image height is 400.
"""
resize.transforms[1].size = height
if __name__ == "__main__":
import matplotlib.pyplot as plt
import gym
import torch
env = gym.make('LunarLander-v2')
env.reset()
plt.figure()
plt.imshow(get_screen(env).squeeze(0).permute(1, 2, 0).numpy(), interpolation='none')
plt.title('Example extracted screen')
plt.show()