-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarla_ppo_inference.py
72 lines (64 loc) · 3.21 KB
/
carla_ppo_inference.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
import argparse
import numpy as np
import torch
from utils_ppo import DrawLine
import logging
from torch.utils.tensorboard import SummaryWriter
from torch_base.torch_agent_inference_ppo import Agent
from ppo_env import Env
from env_config import EnvConfig
import os
parser = argparse.ArgumentParser(description='Train a PPO agent for the CarRacing-v0')
parser.add_argument('--gamma', type=float, default=0.99, metavar='G', help='discount factor (default: 0.99)')
parser.add_argument('--action-repeat', type=int, default=8, metavar='N', help='repeat action in N frames (default: 8)')
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')
parser.add_argument("--device_id", "-dev", type=int, default=0, required=False)
parser.add_argument("--log_seed", type=str, default=0, required=False)
parser.add_argument("--num_episodes", type=int, default=1, required=False)
parser.add_argument("--context", type=str, default='inference', required=False)
parser.add_argument("--num_steps_per_episode", type=int, default=250, required=False)
parser.add_argument("--load_context", type=str, required=False)
parser.add_argument("--load_imitation", action='store_true', help='load from imitation learning model')
parser.add_argument("--imitation_context", type=str, default="params_imitation_1")
args = parser.parse_args()
use_cuda = torch.cuda.is_available()
device = torch.device(f"cuda:{args.device_id}" if use_cuda else "cpu")
torch.manual_seed(args.seed)
if use_cuda:
torch.cuda.manual_seed(args.seed)
LOGGER= logging.getLogger()
LOGGER.setLevel(logging.DEBUG) # or whatever
handler = logging.FileHandler(f"ppo_logger_inference_{args.log_seed}.log", 'w', 'utf-8')
formatter = logging.Formatter('%(name)s %(message)s')
handler.setFormatter(formatter)
LOGGER.addHandler(handler)
if __name__ == "__main__":
agent = Agent(device=device, context=args.context, args=args)
if args.load_context:
corresponding_train_context = args.load_context.replace('inference', 'train')
else:
corresponding_train_context = args.context.replace('inference', 'train')
if args.load_imitation:
agent.load_param_imitation(load_context=args.imitation_context)
else:
agent.load_param(file_dir_path="params_" + corresponding_train_context)
# agent.load_param(file_dir_path="params_" + corresponding_train_context)
env = Env(args=args, env_params=EnvConfig['test_env_params'], context=args.context, device=device)
training_records = []
running_score = 0
for i_ep in range(args.num_episodes):
score = 0
state = env.reset(episode_num=i_ep)
for t in range(args.num_steps_per_episode):
action = agent.select_action(state)
state_, reward, done, die = env.step(action * np.array([2., 1., 1.]) + np.array([-1., 0., 0.]))
if args.render:
env.render()
score += reward
state = state_
if done or die:
break
LOGGER.info('Ep {}\tScore: {:.2f}\t'.format(i_ep, score))
exit(0)