def __getitem__(self, idx): env = SimpleSimEnv(draw_curve=False) observs = torch.zeros(self.batch_size, 3, 120, 160) for i in range(self.batch_size): obs = env.reset() observs[i, :, :, :] = torch.FloatTensor(obs.transpose((2, 0, 1))) return observs
def write_imgs_from_map(map_name: str, save_dir: pathlib.Path, test_percentage=0.3): env = SimpleSimEnv(map_name=map_name) file_path = pathlib.Path('experiments/demos_{}.json'.format(map_name)) if not file_path.is_file(): raise ValueError("Could not find the file containing the generated trajectories: {}".format(file_path)) data = json.loads(file_path.read_text()) demos = data['demos'] positions = map(lambda d: d['positions'], demos) actions = map(lambda d: d['actions'], demos) positions = sum(positions, []) actions = sum(actions, []) test_dir = save_dir / "test" train_dir = save_dir / "train" test_dir.mkdir(parents=True, exist_ok=True) train_dir.mkdir(parents=True, exist_ok=True) print("Found {} positions to be converted to images...".format(len(positions))) for idx, position in enumerate(positions): cur_pos = np.array(position[0]) cur_angle = position[1] vels = actions[idx] env.cur_pos = cur_pos env.cur_angle = cur_angle obs = env.render_obs().copy() obs = obs[..., ::-1] if random.random() < test_percentage: img_path = test_dir / "{0:06d}.jpg".format(idx) lbl_path = test_dir / "{0:06d}.txt".format(idx) else: img_path = train_dir / "{0:06d}.jpg".format(idx) lbl_path = train_dir / "{0:06d}.txt".format(idx) cv2.imwrite(img_path.as_posix(), obs) lbl_path.write_text(" ".join(map(str, vels)))
def create_environment(args, with_heading=True): if args.env_name == 'SimpleSim-v0': environment = SimpleSimEnv( max_steps=math.inf, domain_rand=False, draw_curve=True ) else: environment = gym.make(args.env_name) if with_heading: environment = HeadingWrapper(environment) return environment
def main(): """ Main launcher that starts the gym thread when the command 'duckietown-start-gym' is invoked """ # get parameters from environment (set during docker launch) otherwise take default map = os.getenv('DUCKIETOWN_MAP', DEFAULTS["map"]) domain_rand = bool( os.getenv('DUCKIETOWN_DOMAIN_RAND', DEFAULTS["domain_rand"])) env = SimpleSimEnv( map_name=map, # draw_curve = args.draw_curve, # draw_bbox = args.draw_bbox, domain_rand=domain_rand) obs = env.reset() # env.render("rgb_array") # TODO: do we need this? does this initialize anything? publisher_socket = None command_socket, command_poll = make_pull_socket() print("Simulator listening to incoming connections...") while True: if has_pull_message(command_socket, command_poll): success, data = receive_data(command_socket) if not success: print(data) # in error case, this will contain the err msg continue if data["topic"] == 0: obs, reward, done, misc = env.step(data["msg"]) print('step_count = %s, reward=%.3f' % (env.step_count, reward)) if done: env.reset() if data["topic"] == 1: print("received ping:", data) # can only initialize socket after first listener is connected - weird ZMQ bug if publisher_socket is None: publisher_socket = make_pub_socket(for_images=True) if data["topic"] in [0, 1]: send_array(publisher_socket, obs)
optimizer.zero_grad() # forward + backward + optimize output = model(image) loss = (output - target).norm(2).mean() loss.backward() optimizer.step() error = (output - target).abs().mean() return loss.data[0], error.data[0] if __name__ == "__main__": env = SimpleSimEnv() env.reset() model = Model() model.printInfo() model.cuda() optimizer = optim.Adam(model.parameters(), lr=0.001) avg_error = 0 for epoch in range(1, 1000000): startTime = time.time() images, targets = genBatch() images = Variable(torch.from_numpy(images).float()).cuda() targets = Variable(torch.from_numpy(targets).float()).cuda()
parser.add_argument('--map-file', default='gym_duckietown/maps/udem1.yaml') parser.add_argument('--draw-curve', action='store_true', help='draw the lane following curve') parser.add_argument('--no-random', action='store_true', help='disable domain randomization') parser.add_argument('--full-res', action='store_true', help='render at full window resolution') args = parser.parse_args() if args.env_name == 'SimpleSim-v0': env = SimpleSimEnv(map_file=args.map_file, draw_curve=args.draw_curve, domain_rand=not args.no_random, full_res=args.full_res) else: env = gym.make(args.env_name) env.reset() env.render() def save_numpy_img(file_name, img): img = np.ascontiguousarray(img) img = (img * 255).astype(np.uint8) img = np.flip(img, 0) from skimage import io io.imsave(file_name, img)
#!/usr/bin/env python3 import time import numpy as np import gym import gym_duckietown from gym_duckietown.envs import SimpleSimEnv # Benchmark loading time st = time.time() env = SimpleSimEnv(max_steps=20000) env.seed(0) env.reset() load_time = 1000 * (time.time() - st) # Benchmark the reset time st = time.time() for i in range(100): env.reset() reset_time = 1000 * (time.time() - st) / 100 # Benchmark the rendering/update speed num_frames = 0 st = time.time() while True: dt = time.time() - st if dt > 5:
import argparse import pyglet import numpy as np import gym import gym_duckietown from gym_duckietown.envs import SimpleSimEnv parser = argparse.ArgumentParser() parser.add_argument('--env-name', default='SimpleSim-v0') parser.add_argument('--draw-curve', action='store_true') args = parser.parse_args() if args.env_name == 'SimpleSim-v0': env = SimpleSimEnv(draw_curve=args.draw_curve) else: env = gym.make(args.env_name) env.reset() env.render() def save_numpy_img(file_name, img): img = np.ascontiguousarray(img) img = (img * 255).astype(np.uint8) img = np.flip(img, 0) from skimage import io io.imsave(file_name, img)
env.unwrapped.cur_pos = cur_pos env.unwrapped.cur_angle = cur_angle obs = env.unwrapped.render_obs().copy() obs = obs.transpose(2, 0, 1) return obs, vels if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('--map-name', required=True) args = parser.parse_args() load_data(args.map_name) env = SimpleSimEnv(map_name=args.map_name) env = HeadingWrapper(env) model = Model() model.train() if torch.cuda.is_available(): model.cuda() print_model_info(model) # weight_decay is L2 regularization, helps avoid overfitting optimizer = optim.SGD( model.parameters(), lr=0.0004, weight_decay=1e-3 )
parser.add_argument('--env-name', default='SimpleSim-v0') parser.add_argument('--map-name', default='udem1') parser.add_argument('--draw-curve', action='store_true', help='draw the lane following curve') parser.add_argument('--draw-bbox', action='store_true', help='draw collision detection bounding boxes') parser.add_argument('--domain-rand', action='store_true', help='enable domain randomization') args = parser.parse_args() if args.env_name == 'SimpleSim-v0': env = SimpleSimEnv(map_name=args.map_name, draw_curve=args.draw_curve, draw_bbox=args.draw_bbox, domain_rand=args.domain_rand) else: env = gym.make(args.env_name) env.reset() env.render() def save_numpy_img(file_name, img): img = np.ascontiguousarray(img) img = (img * 255).astype(np.uint8) from skimage import io io.imsave(file_name, img)
from gym_duckietown.envs import SimpleSimEnv from PIL import Image import numpy as np env = SimpleSimEnv(draw_curve=False) num_samples = 10 for i in range(num_samples): obs = env.reset() img = Image.fromarray(np.flipud((obs * 255)).astype('uint8')) img.save(fp='representation_analysis/trajectories/{}.jpg'.format(i)) if i + 1 % 1000 == 0: print('got to sample {}'.format(i)) env.close()
#!/usr/bin/env python3 import time import numpy as np import gym import gym_duckietown from gym_duckietown.envs import SimpleSimEnv # Benchmark loading time st = time.time() env = SimpleSimEnv(max_steps=20000, map_name='loop_obstacles') env.seed(0) env.reset() load_time = 1000 * (time.time() - st) # Benchmark the reset time st = time.time() for i in range(100): env.reset() reset_time = 1000 * (time.time() - st) / 100 # Benchmark the rendering/update speed num_frames = 0 st = time.time() while True: dt = time.time() - st if dt > 5:
env = gym.make('SimpleSim-v0') first_obs = env.reset() # Check that the human rendering resembles the agent's view first_render = env.render('rgb_array') m0 = first_obs.mean() m1 = first_render.mean() assert m0 > 0 and m0 < 255 assert abs(m0 - m1) < 5 # Try stepping a few times for i in range(0, 10): obs, _, _, _ = env.step(np.array([0.1, 0.099])) # Try loading each of the available map files for map_file in os.listdir('gym_duckietown/maps'): map_name = map_file.split('.')[0] env = SimpleSimEnv(map_name=map_name) # Test the multi-map environment env = MultiMapEnv() # Check that we do not spawn too close to obstacles env = SimpleSimEnv(map_name='loop_obstacles') for i in range(0, 75): obs = env.reset() assert not env._collision(), "collision on spawn" env.step(np.array([0.1, 0.1])) assert not env._collision(), "collision after one step"
import argparse import numpy as np import tqdm from PIL import Image import os import torch from torch.autograd import Variable from gym_duckietown.envs import SimpleSimEnv from representation_analysis.models import VAE env = SimpleSimEnv(draw_curve=False) parser = argparse.ArgumentParser(description='VAE') parser.add_argument('--num_samples_train', type=int, default=5000, help='num samples for training') parser.add_argument('--num_samples_test', type=int, default=500, help='num samples per testing') parser.add_argument('--no-cuda', action='store_true', default=False, help='Enables CUDA training') parser.add_argument('--saved_model', type=str, help='Save file to use') parser.add_argument('--state_size', type=int, default=50, help='Size of latent code (default: 100)')
default=30, help='number of values to compute average over') parser.add_argument( '--heading-speed', type=float, default=0.7, help='We\'re assuming the heading speed to be constant, default is 0.7') args = parser.parse_args() logger_dir = os.path.join( "logs", "{}_{}_{}".format(args.env_name, args.algo, args.suffix)) if not os.path.exists(logger_dir): os.makedirs(logger_dir) logger = Logger(logger_dir) env = HeadingWrapper(SimpleSimEnv(map_name="small_loop")) env.seed(args.seed) if torch.cuda.is_available(): device = torch.device("cuda:0") torch.cuda.manual_seed(args.seed) else: device = torch.device("cpu") torch.manual_seed(args.seed) np.random.seed(args.seed) obs_shape = env.observation_space.shape obs_shape = (obs_shape[0] * args.num_stack, *obs_shape[1:]) if len(env.observation_space.shape) == 3:
parser = argparse.ArgumentParser() parser.add_argument('--env-name', default='SimpleSim-v0') parser.add_argument('--map-name', default='udem1') parser.add_argument('--draw-curve', action='store_true', help='draw the lane following curve') parser.add_argument('--no-random', action='store_true', help='disable domain randomization') args = parser.parse_args() if args.env_name == 'SimpleSim-v0': env = SimpleSimEnv(map_name=args.map_name, draw_curve=args.draw_curve, domain_rand=not args.no_random) else: env = gym.make(args.env_name) env.reset() env.render() def save_numpy_img(file_name, img): img = np.ascontiguousarray(img) img = (img * 255).astype(np.uint8) img = np.flip(img, 0) from skimage import io io.imsave(file_name, img)
from gym_duckietown.envs import SimpleSimEnv from PIL import Image import numpy as np import argparse import os env = SimpleSimEnv(draw_curve=False) parser = argparse.ArgumentParser(description='VAE') parser.add_argument('--num_samples_train', type=int, default=10000, help='num samples per factor') parser.add_argument('--num_samples_test', type=int, default=1000, help='num samples per factor') args = parser.parse_args() factors = [ 'ground_color', 'road_color', 'wheelDist', 'camHeight', 'camAngle', 'camFovY' ] for factor in factors: if not os.path.exists('representation_analysis/train_data/{}/trajectories/' .format(factor)): os.makedirs( 'representation_analysis/train_data/{}/trajectories/'.format( factor)) if not os.path.exists('representation_analysis/test_data/{}/trajectories/'. format(factor)): os.makedirs(
# Check that the human rendering resembles the agent's view first_obs = env.reset() first_render = env.render('rgb_array') m0 = first_obs.mean() m1 = first_render.mean() assert m0 > 0 and m0 < 255 assert abs(m0 - m1) < 5 # Try stepping a few times for i in range(0, 10): obs, _, _, _ = env.step(np.array([0.1, 0.099])) # Try loading each of the available map files for map_file in os.listdir('gym_duckietown/maps'): map_name = map_file.split('.')[0] env = SimpleSimEnv(map_name=map_name) env.reset() # Test the multi-map environment env = MultiMapEnv() for i in range(0, 50): env.reset() # Check that we do not spawn too close to obstacles env = SimpleSimEnv(map_name='loop_obstacles') for i in range(0, 75): obs = env.reset() assert not env._collision(), "collision on spawn" env.step(np.array([0.1, 0.1])) assert not env._collision(), "collision after one step"
for itr in range(1, num_itrs + 1): new_actions = mutate_actions(best_actions) positions, r = eval_actions(env, seed, new_actions) if r > best_r: best_r = r best_actions = new_actions best_positions = positions print('iteration #%d, r=%f' % (itr, r)) print('r=%f' % best_r) return best_positions, best_actions env = SimpleSimEnv(map_name=args.map_name) demos = [] total_steps = 0 for _ in range(1000): seed = random.randint(0, 0xFFFFFFFF) p, a = gen_trajectory(env, seed, args.demo_len + args.tail_len, args.num_itrs) print('trajectory length: %d' % len(p)) # If the agent did not fall off the road if len(p) == args.demo_len + args.tail_len: # Drop the last few actions, because the agent behaves more # greedily in the last steps (doesn't maximize future reward)
from gym_duckietown.wrappers import HeadingWrapper parser = argparse.ArgumentParser() parser.add_argument('--env-name', default='SimpleSim-v0') parser.add_argument('--map-name', default='udem1') parser.add_argument('--draw-curve', action='store_true', help='draw the lane following curve') parser.add_argument('--domain-rand', action='store_true', help='enable domain randomization') args = parser.parse_args() if args.env_name == 'SimpleSim-v0': env = SimpleSimEnv(map_name=args.map_name, domain_rand=args.domain_rand, max_steps=math.inf) else: env = gym.make(args.env_name) env = HeadingWrapper(env) env.reset() env.render() # global variables for demo recording positions = [] actions = [] demos = [] recording = False
from train_imitation import Model from utils import make_var parser = argparse.ArgumentParser() parser.add_argument('--env-name', default='SimpleSim-v0') parser.add_argument('--map-name', required=True) parser.add_argument('--no-random', action='store_true', help='disable domain randomization') parser.add_argument('--no-pause', action='store_true', help="don't pause on failure") args = parser.parse_args() if args.env_name == 'SimpleSim-v0': env = SimpleSimEnv(map_name=args.map_name, domain_rand=not args.no_random) #env.max_steps = math.inf env.max_steps = 500 else: env = gym.make(args.env_name) env = HeadingWrapper(env) obs = env.reset() env.render() avg_frame_time = 0 max_frame_time = 0 def load_model(): global model
#!/usr/bin/env python3 import os import numpy as np import gym import gym_duckietown from gym_duckietown.envs import SimpleSimEnv, MultiMapEnv env = gym.make('SimpleSim-v0') first_obs = env.reset() # Check that the human rendering resembles the agent's view first_render = env.render('rgb_array') m0 = first_obs.mean() m1 = first_render.mean() assert m0 > 0 and m0 < 255 assert abs(m0 - m1) < 5 # Try stepping a few times for i in range(0, 10): obs, _, _, _ = env.step(np.array([0.1, 0.099])) # Try loading each of the available map files for map_file in os.listdir('gym_duckietown/maps'): map_name = map_file.split('.')[0] env = SimpleSimEnv(map_name=map_name) # Test the multi-map environment env = MultiMapEnv()