-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
246 lines (202 loc) · 8.26 KB
/
example.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import argparse
import numpy as np
import cv2
import gym
import torch
import torch.nn as nn
parser = argparse.ArgumentParser(description='Test the PPO agent for the CarRacing-v0')
parser.add_argument('--action-repeat', type=int, default=8, metavar='N', help='repeat action in N frames (default: 12)')
parser.add_argument('--img-stack', type=int, default=4, metavar='N', help='stack N image in a state (default: 4)')
parser.add_argument('--seed', type=int, default=0, metavar='N', help='random seed (default: 0)')
parser.add_argument('--render', action='store_true', help='render the environment')
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
device = torch.device("cuda" if use_cuda else "cpu")
torch.manual_seed(args.seed)
if use_cuda:
torch.cuda.manual_seed(args.seed)
class Env():
"""
Test environment wrapper for CarRacing
"""
def __init__(self):
self.env = gym.make('CarRacing-v0')
self.env.seed(args.seed)
self.reward_threshold = self.env.spec.reward_threshold
def reset(self):
self.counter = 0
self.av_r = self.reward_memory()
self.die = False
img_rgb = self.env.reset()
img_gray = self.rgb2gray(img_rgb)
self.stack = [img_gray] * args.img_stack
return np.array(self.stack)
def step(self, action):
total_reward = 0
for i in range(args.action_repeat):
img_rgb, reward, die, _ = self.env.step(action)
# don't penalize "die state"
if die:
reward += 100
# green penalty
if np.mean(img_rgb[:, :, 1]) > 185.0:
reward -= 0.05
total_reward += reward
# if no reward recently, end the episode
done = True if self.av_r(reward) <= -0.1 else False
if done or die:
break
img_gray = self.rgb2gray(img_rgb)
self.stack.pop(0)
self.stack.append(img_gray)
assert len(self.stack) == args.img_stack
return np.array(self.stack), total_reward, done, die
def render(self, *arg):
self.env.render(*arg)
@staticmethod
def rgb2gray(rgb, norm=True):
gray = np.dot(rgb[..., :], [0.299, 0.587, 0.114])
if norm:
# normalize
gray = gray / 128. - 1.
return gray
@staticmethod
def reward_memory():
count = 0
length = 100
history = np.zeros(length)
def memory(reward):
nonlocal count
history[count] = reward
count = (count + 1) % length
return np.mean(history)
return memory
class Net(nn.Module):
"""
Actor-Critic Network for PPO
"""
def __init__(self):
super(Net, self).__init__()
self.cnn_base = nn.Sequential( # input shape (4, 96, 96)
nn.Conv2d(args.img_stack, 8, kernel_size=4, stride=2),
nn.ReLU(), # activation
nn.Conv2d(8, 16, kernel_size=3, stride=2), # (8, 47, 47)
nn.ReLU(), # activation
nn.Conv2d(16, 32, kernel_size=3, stride=2), # (16, 23, 23)
nn.ReLU(), # activation
nn.Conv2d(32, 64, kernel_size=3, stride=2), # (32, 11, 11)
nn.ReLU(), # activation
nn.Conv2d(64, 128, kernel_size=3, stride=1), # (64, 5, 5)
nn.ReLU(), # activation
nn.Conv2d(128, 256, kernel_size=3, stride=1), # (128, 3, 3)
nn.ReLU(), # activation
) # output shape (256, 1, 1)
self.v = nn.Sequential(nn.Linear(256, 100), nn.ReLU(), nn.Linear(100, 1))
self.fc = nn.Sequential(nn.Linear(256, 100), nn.ReLU())
self.alpha_head = nn.Sequential(nn.Linear(100, 3), nn.Softplus())
self.beta_head = nn.Sequential(nn.Linear(100, 3), nn.Softplus())
self.apply(self._weights_init)
@staticmethod
def _weights_init(m):
if isinstance(m, nn.Conv2d):
nn.init.xavier_uniform_(m.weight, gain=nn.init.calculate_gain('relu'))
nn.init.constant_(m.bias, 0.1)
def forward(self, x):
x = self.cnn_base(x)
x = x.view(-1, 256)
v = self.v(x)
x = self.fc(x)
alpha = self.alpha_head(x) + 1
beta = self.beta_head(x) + 1
return (alpha, beta), v
class Agent():
"""
Agent for testing
"""
def __init__(self):
self.net = Net().float().to(device)
def select_action(self, state):
state = torch.from_numpy(state).float().to(device).unsqueeze(0)
with torch.no_grad():
alpha, beta = self.net(state)[0]
action = alpha / (alpha + beta)
action = action.squeeze().cpu().numpy()
return action
def load_param(self):
self.net.load_state_dict(torch.load('param/ppo_net_params.pkl'))
from torch.nn import functional as F
import torch
import torch.nn as nn
class VAE(nn.Module):
def __init__(self, latent_size=32):
super(VAE, self).__init__()
self.latent_size = latent_size
# Encoder
self.enc_conv1 = nn.Conv2d(3, 32, kernel_size=4, stride=2, padding=1) # Input: (3, 96, 96) -> Output: (32, 48, 48)
self.enc_conv2 = nn.Conv2d(32, 64, kernel_size=4, stride=2, padding=1) # (32, 48, 48) -> (64, 24, 24)
self.enc_conv3 = nn.Conv2d(64, 128, kernel_size=4, stride=2, padding=1) # (64, 24, 24) -> (128, 12, 12)
self.enc_conv4 = nn.Conv2d(128, 256, kernel_size=4, stride=2, padding=1) # (128, 12, 12) -> (256, 6, 6)
self.fc_mu = nn.Linear(256 * 6 * 6, latent_size)
self.fc_logvar = nn.Linear(256 * 6 * 6, latent_size)
# Decoder
self.dec_fc = nn.Linear(latent_size, 256 * 6 * 6)
self.dec_conv1 = nn.ConvTranspose2d(256, 128, kernel_size=5, stride=2, padding=2, output_padding=1)
self.dec_conv2 = nn.ConvTranspose2d(128, 64, kernel_size=5, stride=2, padding=2, output_padding=1)
self.dec_conv3 = nn.ConvTranspose2d(64, 32, kernel_size=6, stride=2, padding=2)
self.dec_conv4 = nn.ConvTranspose2d(32, 3, kernel_size=5, stride=2, padding=2, output_padding=1)
# self.dec_conv5 = nn.ConvTranspose2d(16, 8, kernel_size=6, stride=2, padding=2)
# self.dec_conv6 = nn.ConvTranspose2d(8, 3, kernel_size=6, stride=1, padding=2)
def reparameterize(self, mu, logvar):
std = torch.exp(0.5 * logvar)
eps = torch.randn_like(std)
return mu + eps * std
def encode(self, x):
x = F.relu(self.enc_conv1(x))
x = F.relu(self.enc_conv2(x))
x = F.relu(self.enc_conv3(x))
x = F.relu(self.enc_conv4(x))
x = torch.flatten(x, start_dim=1)
mu = self.fc_mu(x)
logvar = self.fc_logvar(x)
return mu, logvar
def decode(self, z):
x = F.relu(self.dec_fc(z))
x = x.view(-1, 256, 6, 6) # Reshape to match the beginning shape of the decoder
x = F.relu(self.dec_conv1(x))
x = F.relu(self.dec_conv2(x))
x = F.relu(self.dec_conv3(x))
x = F.relu(self.dec_conv4(x))
# x = F.relu(self.dec_conv5(x))
# x = torch.sigmoid(self.dec_conv6(x)) # Ensure the output is in [0, 1]
return x
def forward(self, x):
mu, logvar = self.encode(x)
z = self.reparameterize(mu, logvar)
return self.decode(z), mu, logvar
if __name__ == "__main__":
agent = Agent()
agent.load_param()
tensor = torch.randn(4, 96, 96, dtype=torch.float32)
array = np.random.randn(4, 96, 96).astype(np.float32)
device = "cuda"
vae = VAE().to(device)
best = torch.load("Copy of Copy of real_large_best_model.pth")
vae.load_state_dict(best)
vae.eval()
x = torch.randn(1, 32).to(device)
img = vae.decode(x)
numpy_array = img.squeeze(0).cpu().detach().numpy()*255
array = numpy_array.astype(np.uint8)
# Transpose the array to the shape (96, 96, 3)
image = np.transpose(array, (1, 2, 0))
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
gray_image_normalized = gray_image / 255.0
# Duplicate the grayscale image 4 times to get a (4, 96, 96) array
duplicated_gray_images = np.stack([gray_image_normalized] * 4, axis=0)
action = agent.select_action(duplicated_gray_images)
print("action (steering,gas,breaking) is: ")
print(action)
# Display the image using OpenCV
cv2.imshow('Image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()