from environment.carla.settings import CarlaSettings from environment.carla.sensor import Camera from environment.carla.carla_server_pb2 import Control from environment.carla.tcp import TCPConnectionError from environment.imitation.imitation_learning import ImitationLearning import time import random sess = tf.Session() Image_agent = ImitationLearning(sess) Vec_ = [] Image_agent.load_model() global_episode = 0 while True: try: with make_carla_client('localhost', 2000) as client: for episode in range(3): if episode < global_episode: continue print(episode) settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=True, NumberOfVehicles=20, NumberOfPedestrians=40, WeatherId=random.choice([1, 3, 7, 8, 14]), QualityLevel='Epic') settings.randomize_seeds() camera = Camera('CameraRGB') camera.set(FOV=100) camera.set_image_size(800, 600)
def main(_): gpu_options = tf.GPUOptions(allow_growth=True) with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess: env = Env(MONITOR_DIR, RANDOM_SEED, FPS, sess) np.random.seed(RANDOM_SEED) tf.set_random_seed(RANDOM_SEED) state_dim = env.observation_space.shape try: action_dim = env.action_space.shape[0] action_bound = env.action_space.high # Ensure action bound is symmetric assert (np.all(env.action_space.high == -env.action_space.low)) action_type = 'Continuous' except: action_dim = env.action_space.n action_bound = None action_type = 'Discrete' print(action_type) actor = ActorNetwork(sess, state_dim, action_dim, action_bound, ACTOR_LEARNING_RATE, TAU, action_type) critic = CriticNetwork(sess, state_dim, action_dim, action_bound, CRITIC_LEARNING_RATE, TAU, actor.get_num_trainable_vars(), action_type) # Initialize replay memory replay_buffer = ReplayBuffer(BUFFER_SIZE, RANDOM_SEED) if action_type == 'Continuous': noise = OrnsteinUhlenbeckProcess( OU_THETA, mu=OU_MU, sigma=OU_SIGMA, n_steps_annealing=EXPLORATION_EPISODES) else: noise = GreedyPolicy(action_dim, EXPLORATION_EPISODES, MIN_EPSILON, MAX_EPSILON) agent = DDPGAgent(sess, action_type, actor, critic, GAMMA, env, replay_buffer, noise=noise, exploration_episodes=EXPLORATION_EPISODES, max_episodes=MAX_EPISODES, max_steps_episode=MAX_STEPS_EPISODE, warmup_steps=WARMUP_STEPS, mini_batch=FLAGS.mini_batch, eval_episodes=EVAL_EPISODES, eval_periods=EVAL_PERIODS, env_render=FLAGS.env_render, summary_dir=SUMMARY_DIR, model_dir=MODEL_DIR, model_store_periods=MODEL_STORE_PERIODS, detail=DETAIL, render_interval=RENDER_INTERVAL) while True: try: with make_carla_client('localhost', FLAGS.port) as client: env.connected(client) agent.train() except TCPConnectionError as error: print(error) time.sleep(5.0)
def run_carla_client(args): # Here we will run 3 episodes with 300 frames each. number_of_episodes = 10 frames_per_episode = 500 # We assume the CARLA server is already waiting for a client to connect at # host:port. To create a connection we can use the `make_carla_client` # context manager, it creates a CARLA client object and starts the # connection. It will throw an exception if something goes wrong. The # context manager makes sure the connection is always cleaned up on exit. with make_carla_client(args.host, args.port) as client: print('CarlaClient connected') for episode in range(0, number_of_episodes): # Start a new episode. # Create a CarlaSettings object. This object is a wrapper around # the CarlaSettings.ini file. Here we set the configuration we # want for the new episode. settings = CarlaSettings() settings.set(SynchronousMode=True, SendNonPlayerAgentsInfo=False, NumberOfVehicles=0, NumberOfPedestrians=0, WeatherId=random.choice([1]), QualityLevel=args.quality_level) settings.randomize_seeds() camera0 = Camera('CameraRGB') camera0.set_image_size(160, 120) camera0.set(FOV=100) camera0.set_position(2.0, 0.0, 1.4) camera0.set_rotation(-15.0, 0, 0) settings.add_sensor(camera0) # Now we load these settings into the server. The server replies # with a scene description containing the available start spots for # the player. Here we can provide a CarlaSettings object or a # CarlaSettings.ini file as string. scene = client.load_settings(settings) # Choose one player start at random. number_of_player_starts = len(scene.player_start_spots) player_start = random.randint(0, max(0, number_of_player_starts - 1)) # Notify the server that we want to start the episode at the # player_start index. This function blocks until the server is ready # to start the episode. print('Starting new episode...') client.start_episode(player_start) # Iterate every frame in the episode. for frame in range(0, frames_per_episode): # Read the data produced by the server this frame. measurements, sensor_data = client.read_data() # Print some of the measurements. print_measurements(measurements) # Save the images to disk if requested. if args.save_images_to_disk: for name, measurement in sensor_data.items(): filename = args.out_filename_format.format( episode, name, frame) measurement.save_to_disk(filename) # We can access the encoded data of a given image as numpy # array using its "data" property. For instance, to get the # depth value (normalized) at pixel X, Y # # depth_array = sensor_data['CameraDepth'].data # value_at_pixel = depth_array[Y, X] # # Now we have to send the instructions to control the vehicle. # If we are in synchronous mode the server will pause the # simulation until we send this control. if not args.autopilot: client.send_control(steer=random.uniform(-1.0, 1.0), throttle=0.5, brake=0.0, hand_brake=False, reverse=False) else: # Together with the measurements, the server has sent the # control that the in-game autopilot would do this frame. We # can enable autopilot by sending back this control to the # server. We can modify it if wanted, here for instance we # will add some noise to the steer. control = measurements.player_measurements.autopilot_control # control.steer += random.uniform(-0.1, 0.1) control.steer += random.uniform(-0.2, 0.2) client.send_control(control)
def main(): argparser = argparse.ArgumentParser( description='CARLA Manual Control Client') argparser.add_argument('-v', '--verbose', action='store_true', dest='debug', help='print debug information') argparser.add_argument('--host', metavar='H', default='localhost', help='IP of the host server (default: localhost)') argparser.add_argument('-p', '--port', metavar='P', default=2000, type=int, help='TCP port to listen to (default: 2000)') argparser.add_argument('-a', '--autopilot', action='store_true', help='enable autopilot') argparser.add_argument('-l', '--lidar', action='store_true', help='enable Lidar') argparser.add_argument( '-q', '--quality-level', choices=['Low', 'Epic'], type=lambda s: s.title(), default='Epic', help= 'graphics quality level, a lower level makes the simulation run considerably faster.' ) argparser.add_argument( '-m', '--map-name', metavar='M', default=None, help='plot the map of the current city (needs to match active map in ' 'server, options: Town01 or Town02)') args = argparser.parse_args() log_level = logging.DEBUG if args.debug else logging.INFO logging.basicConfig(format='%(levelname)s: %(message)s', level=log_level) logging.info('listening to server %s:%s', args.host, args.port) print(__doc__) while True: try: with make_carla_client(args.host, args.port) as client: game = CarlaGame(client, args) game.execute() break except TCPConnectionError as error: logging.error(error) time.sleep(1)