-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathenvironment.py
115 lines (93 loc) · 3.41 KB
/
environment.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from __future__ import division
import gym
import numpy as np
from cv2 import resize
from gym.spaces.box import Box
import distutils.version
from random import choice
import gym_unrealcv
def create_env(env_id, args, rank=-1):
env = gym.make(env_id)
print ('build env')
if args.rescale is True:
env = Rescale(env, args)
if 'img' in args.obs:
env = UnrealRescale(env, args)
return env
class Rescale(gym.Wrapper):
def __init__(self, env, args):
super(Rescale, self).__init__(env)
if type(env.observation_space) == list:
self.mx_d = 255.0
self.mn_d = 0.0
shape = env.observation_space[0].shape
else:
self.mx_d = env.observation_space.high
self.mn_d = env.observation_space.low
shape = env.observation_space.shape
self.obs_range = self.mx_d - self.mn_d
self.new_maxd = 1.0
self.new_mind = -1.0
self.observation_space = [Box(self.new_mind, self.new_maxd, shape) for i in range(len(self.observation_space))]
self.args = args
self.inv_img = self.choose_rand_seed()
self.flip = self.choose_rand_seed()
def rescale(self, x):
obs = x.clip(self.mn_d, self.mx_d)
new_obs = (((obs - self.mn_d) * (self.new_maxd - self.new_mind)
) / self.obs_range) + self.new_mind
return new_obs
def _reset(self):
ob = self.env.reset()
self.flip = self.choose_rand_seed()
if self.flip:
ob = self.flip_img(ob)
self.inv_img = self.choose_rand_seed()
if self.inv_img:
ob = 255 - ob
ob = self.rescale(np.float32(ob))
return ob
def _step(self, action):
if self.flip:
action = self.flip_action(action)
ob, rew, done, info = self.env.step(action)
if self.inv_img:
ob = 255 - ob
if self.flip:
ob = self.flip_img(ob)
ob = self.rescale(np.float32(ob))
return ob, rew, done, info
def choose_rand_seed(self):
return choice([True, False])
def flip_img(self, img):
return np.fliplr(img)
def flip_action(self, action):
ac = action
if action == 0:
ac = 1
elif action == 1:
ac = 0
elif action == 2:
ac = 3
elif action == 3:
ac = 2
return ac
class UnrealRescale(gym.ObservationWrapper):
def __init__(self, env, args):
gym.ObservationWrapper.__init__(self, env)
self.input_size = args.input_size
self.use_gym_10_api = distutils.version.LooseVersion(gym.__version__) >= distutils.version.LooseVersion('0.10.0')
if self.use_gym_10_api:
self.observation_space = [Box(-1.0, 1.0, [3, self.input_size, self.input_size], dtype=np.uint8) for i in range(len(self.observation_space))]
else:
self.observation_space = [Box(-1.0, 1.0, [3, self.input_size, self.input_size]) for i in range(len(self.observation_space))]
def process_frame_ue(self, frame, size=80):
frame = frame.astype(np.float32)
frame = resize(frame, (size, size))
frame = np.transpose(frame, (2, 1, 0))
return frame
def observation(self, observation):
obses = []
for i in range(len(observation)):
obses.append(self.process_frame_ue(observation[i], self.input_size))
return np.array(obses)