def on_attach(self):
     """
     During attach, add model to non-collidable objects.
     """
     self.reset()
     # Add to noncollidable_objects to avoid collision with other objects during blink
     TrackData.get_instance().add_noncollidable_object(self.model_name)
    def on_detach(self):
        """
        After detach, remove model from non-collidable objects and reset transparencies to original.
        """
        for visual_name, link_name, transparency in zip(
                self.orig_visuals.visual_names, self.orig_visuals.link_names,
            [0.0] * len(self.orig_visuals.visual_names)):
            SetVisualTransparencyTracker.get_instance(
            ).set_visual_transparency(visual_name, link_name, transparency)

        # Remove noncollidable_objects to allow collision with other objects after blink
        TrackData.get_instance().remove_noncollidable_object(self.model_name)
    def __init__(self, horizontal_fov, padding_percent, image_width,
                 image_height, racecars_info):
        """ Camera information is required to map the (x, y) value of the agent on the camera image.
        This is because each track will have its own FOV and padding percent because to avoid
        Z-fighting. Once the camera position, padding percentage is available. We can map the
        (x,y) of the agent with respect to the track to a new plane of the camera image with
        padding.

        Arguments:
            horizontal_fov (float): Horizontal field of view of the camera for the given track
            padding_percent (float): The padding percentage to cover the whole track and look pretty
            image_width (int): Image width from the camera
            image_height (int): Image width from the camera
            racecars_info (list): This is the list of dicts of racecars on the track
        """
        self.horizontal_fov = horizontal_fov
        self.padding_percent = padding_percent
        self.image_width = image_width
        self.image_height = image_height
        self.racecars_info = force_list(racecars_info)

        self.model_imgs = self._get_all_models_info()

        # Track information is required get the track bounds (xmin, xmax, ymin, ymax)
        # Also the agent location for that instant
        self.track_data = TrackData.get_instance()
        # This maps the (x, y) of an agent in track frame to the top camera image frame
        self.initialize_parameters()
    def __init__(self, namespace=None, model_name=None):
        super(TopCamera, self).__init__(TopCamera.name,
                                        namespace=namespace,
                                        model_name=model_name)
        self.track_data = TrackData.get_instance()
        x_min, y_min, x_max, y_max = self.track_data.outer_border.bounds
        horizontal_width = (x_max - x_min) * (1.0 + PADDING_PCT)
        vertical_width = (y_max - y_min) * (1.0 + PADDING_PCT)
        horizontal_fov = DEFAULT_H_FOV
        try:
            if horizontal_width >= vertical_width:
                horizontal_fov = 2.0 * math.atan(
                    0.5 * horizontal_width / CAMERA_HEIGHT)
            else:
                vertical_fov = math.atan(0.5 * vertical_width / CAMERA_HEIGHT)
                aspect_ratio = float(DEFAULT_RESOLUTION[0]) / float(
                    DEFAULT_RESOLUTION[1])
                horizontal_fov = 2.0 * math.atan(
                    aspect_ratio * math.tan(vertical_fov))
        except Exception as ex:
            LOG.info('Unable to compute top camera fov, using default: %s', ex)

        self.camera_settings_dict = CameraSettings.get_empty_dict()
        self.camera_settings_dict[CameraSettings.HORZ_FOV] = horizontal_fov
        self.camera_settings_dict[CameraSettings.PADDING_PCT] = PADDING_PCT
        self.camera_settings_dict[
            CameraSettings.IMG_WIDTH] = DEFAULT_RESOLUTION[0]
        self.camera_settings_dict[
            CameraSettings.IMG_HEIGHT] = DEFAULT_RESOLUTION[1]

        rospy.Service('get_top_cam_data', TopCamDataSrv,
                      self._handle_get_top_cam_data)
    def __init__(self):
        # Read ros parameters
        # OBJECT_POSITIONS will overwrite NUMBER_OF_OBSTACLES and RANDOMIZE_OBSTACLE_LOCATIONS
        self.object_locations = rospy.get_param("OBJECT_POSITIONS", [])
        self.num_obstacles = int(rospy.get_param("NUMBER_OF_OBSTACLES", 0)) \
                             if not self.object_locations else len(self.object_locations)
        self.min_obstacle_dist = float(rospy.get_param("MIN_DISTANCE_BETWEEN_OBSTACLES", 2.0))
        self.randomize = utils.str2bool(rospy.get_param("RANDOMIZE_OBSTACLE_LOCATIONS", False))
        self.use_bot_car = utils.str2bool(rospy.get_param("IS_OBSTACLE_BOT_CAR", False))
        self.obstacle_names = ["{}_{}".format(OBSTACLE_NAME_PREFIX, i) for i in range(self.num_obstacles)]
        self.obstacle_dimensions = ObstacleDimensions.BOT_CAR_DIMENSION if self.use_bot_car \
                                   else ObstacleDimensions.BOX_OBSTACLE_DIMENSION

        # track data
        self.track_data = TrackData.get_instance()

        # Wait for ros services
        rospy.wait_for_service(SPAWN_SDF_MODEL)
        rospy.wait_for_service(SPAWN_URDF_MODEL)
        self.spawn_sdf_model = ServiceProxyWrapper(SPAWN_SDF_MODEL, SpawnModel)
        self.spawn_urdf_model = ServiceProxyWrapper(SPAWN_URDF_MODEL, SpawnModel)

        # Load the obstacle sdf/urdf
        obstacle_model_folder = "bot_car" if self.use_bot_car else "box_obstacle"
        rospack = rospkg.RosPack()
        deepracer_path = rospack.get_path("deepracer_simulation_environment")
        obstacle_sdf_path = os.path.join(deepracer_path, "models", obstacle_model_folder, "model.sdf")
        with open(obstacle_sdf_path, "r") as fp:
            self.obstacle_sdf = fp.read()

        # Set obstacle poses and spawn the obstacles
        self.obstacle_poses = self._compute_obstacle_poses()
        self._spawn_obstacles()

        self._configure_randomizer()
    def __init__(self,
                 agent_name,
                 s3_dict_metrics,
                 is_continuous,
                 pause_time_before_start=0.0):
        """Init eval metrics

        Args:
            agent_name (string): agent name
            s3_dict_metrics (dict): Dictionary containing the required
                s3 info for the metrics bucket with keys specified by MetricsS3Keys
            is_continuous (bool): True if continuous race, False otherwise
            pause_time_before_start (float): second to pause before race start
        """
        self._pause_time_before_start = pause_time_before_start
        self._is_pause_time_subtracted = False
        self._agent_name_ = agent_name
        self._s3_metrics = Metrics(
            bucket=s3_dict_metrics[MetricsS3Keys.METRICS_BUCKET.value],
            s3_key=s3_dict_metrics[MetricsS3Keys.METRICS_KEY.value],
            region_name=s3_dict_metrics[MetricsS3Keys.REGION.value],
        )
        self._is_continuous = is_continuous
        self._start_time_ = time.time()
        self._number_of_trials_ = 0
        self._progress_ = 0.0
        self._episode_status = ""
        self._metrics_ = list()
        # This is used to calculate the actual distance traveled by the car
        self._agent_xy = list()
        self._prev_step_time = time.time()
        self.is_save_simtrace_enabled = rospy.get_param(
            "SIMTRACE_S3_BUCKET", None)
        # Create the agent specific directories needed for storing the metric files
        self._simtrace_local_path = SIMTRACE_EVAL_LOCAL_PATH_FORMAT.format(
            self._agent_name_)
        simtrace_dirname = os.path.dirname(self._simtrace_local_path)
        if simtrace_dirname or not os.path.exists(simtrace_dirname):
            os.makedirs(simtrace_dirname)
        self.reset_count_dict = {
            EpisodeStatus.CRASHED.value: 0,
            EpisodeStatus.OFF_TRACK.value: 0,
            EpisodeStatus.IMMOBILIZED.value: 0,
            EpisodeStatus.REVERSED.value: 0,
        }
        self._best_lap_time = float("inf")
        self._total_evaluation_time = 0
        self._video_metrics = Mp4VideoMetrics.get_empty_dict()
        self._reset_count_sum = 0
        self._current_sim_time = 0
        self.track_data = TrackData.get_instance()
        rospy.Service(
            "/{}/{}".format(self._agent_name_, "mp4_video_metrics"),
            VideoMetricsSrv,
            self._handle_get_video_metrics,
        )
        AbstractTracker.__init__(self, TrackerPriority.HIGH)
Exemple #7
0
 def __init__(self,
              agent_name,
              s3_dict_metrics,
              deepracer_checkpoint_json,
              ckpnt_dir,
              run_phase_sink,
              use_model_picker=True):
     '''s3_dict_metrics - Dictionary containing the required s3 info for the metrics
                          bucket with keys specified by MetricsS3Keys
        deepracer_checkpoint_json - DeepracerCheckpointJson instance
        ckpnt_dir - Directory where the current checkpont is to be stored
        run_phase_sink - Sink to recieve notification of a change in run phase
        use_model_picker - Flag to whether to use model picker or not.
     '''
     self._agent_name_ = agent_name
     self._deepracer_checkpoint_json = deepracer_checkpoint_json
     self._s3_metrics = Metrics(
         bucket=s3_dict_metrics[MetricsS3Keys.METRICS_BUCKET.value],
         s3_key=s3_dict_metrics[MetricsS3Keys.METRICS_KEY.value],
         region_name=s3_dict_metrics[MetricsS3Keys.REGION.value])
     self._start_time_ = time.time()
     self._episode_ = 0
     self._episode_reward_ = 0.0
     self._progress_ = 0.0
     self._episode_status = ''
     self._metrics_ = list()
     self._is_eval_ = True
     self._eval_trials_ = 0
     self._checkpoint_state_ = CheckpointStateFile(ckpnt_dir)
     self._use_model_picker = use_model_picker
     self._eval_stats_dict_ = {'chkpnt_name': None, 'avg_eval_metric': None}
     self._best_chkpnt_stats = {
         'name': None,
         'avg_eval_metric': None,
         'time_stamp': time.time()
     }
     self._current_eval_best_model_metric_list_ = list()
     self.is_save_simtrace_enabled = rospy.get_param(
         'SIMTRACE_S3_BUCKET', None)
     self._best_model_metric_type = BestModelMetricType(
         rospy.get_param('BEST_MODEL_METRIC',
                         BestModelMetricType.PROGRESS.value).lower())
     self.track_data = TrackData.get_instance()
     run_phase_sink.register(self)
     # Create the agent specific directories needed for storing the metric files
     self._simtrace_local_path = SIMTRACE_TRAINING_LOCAL_PATH_FORMAT.format(
         self._agent_name_)
     simtrace_dirname = os.path.dirname(self._simtrace_local_path)
     if simtrace_dirname or not os.path.exists(simtrace_dirname):
         os.makedirs(simtrace_dirname)
     self._current_sim_time = 0
     rospy.Service("/{}/{}".format(self._agent_name_, "mp4_video_metrics"),
                   VideoMetricsSrv, self._handle_get_video_metrics)
     self._video_metrics = Mp4VideoMetrics.get_empty_dict()
     AbstractTracker.__init__(self, TrackerPriority.HIGH)
Exemple #8
0
 def __init__(self):
     if TopCamera._instance_ is not None:
         raise GenericRolloutException(
             "Attempting to construct multiple top video camera")
     super(TopCamera, self).__init__(TopCamera.name)
     rospy.wait_for_service('/gazebo/set_model_state')
     self.model_state_client = ServiceProxyWrapper(
         '/gazebo/set_model_state', SetModelState)
     self.track_data = TrackData.get_instance()
     # there should be only one top video camera
     TopCamera._instance_ = self
    def __init__(self):
        self.lock = threading.Lock()

        # Read ros parameters
        self.num_bot_cars = int(rospy.get_param("NUMBER_OF_BOT_CARS", 0))
        self.min_bot_car_dist = float(
            rospy.get_param("MIN_DISTANCE_BETWEEN_BOT_CARS", 2.0))
        self.randomize = utils.str2bool(
            rospy.get_param("RANDOMIZE_BOT_CAR_LOCATIONS", False))
        self.bot_car_speed = float(rospy.get_param("BOT_CAR_SPEED", 0.2))
        self.is_lane_change = utils.str2bool(
            rospy.get_param("IS_LANE_CHANGE", False))
        self.lower_lane_change_time = float(
            rospy.get_param("LOWER_LANE_CHANGE_TIME", 3.0))
        self.upper_lane_change_time = float(
            rospy.get_param("UPPER_LANE_CHANGE_TIME", 5.0))
        self.lane_change_distance = float(
            rospy.get_param("LANE_CHANGE_DISTANCE", 1.0))
        self.penalty_seconds = float(rospy.get_param("PENALTY_SECONDS", 2.0))
        self.lane_change_duration = self.lane_change_distance / self.bot_car_speed
        self.bot_car_names = [
            "bot_car_{}".format(i) for i in range(self.num_bot_cars)
        ]
        self.bot_car_poses = []
        self.bot_car_progresses = {}
        self.bot_car_phase = AgentPhase.RUN.value
        self.bot_car_dimensions = ObstacleDimensions.BOT_CAR_DIMENSION
        self.bot_car_crash_count = 0
        self.pause_end_time = 0.0

        # track date
        self.track_data = TrackData.get_instance()
        self.reverse_dir = self.track_data.reverse_dir

        # Wait for ros services
        rospy.wait_for_service(SET_MODEL_STATE)
        rospy.wait_for_service(SPAWN_SDF_MODEL)
        self.set_model_state = ServiceProxyWrapper(SET_MODEL_STATE,
                                                   SetModelState)
        self.spawn_sdf_model = ServiceProxyWrapper(SPAWN_SDF_MODEL, SpawnModel)

        # Build splines for inner/outer lanes
        self.inner_lane = TrackSpline(lane_name=TrackLane.INNER_LANE.value)
        self.outer_lane = TrackSpline(lane_name=TrackLane.OUTER_LANE.value)

        # Spawn the bot cars
        self._reset_sim_time()
        self._spawn_bot_cars()

        self._configure_randomizer()

        # Subscribe to the Gazebo clock and model states
        rospy.Subscriber('/clock', Clock, self._update_sim_time)
 def __init__(self, namespace=None, topic_name=None):
     super(FollowCarCamera, self).__init__(FollowCarCamera.name,
                                           namespace=namespace,
                                           topic_name=topic_name)
     rospy.wait_for_service('/gazebo/set_model_state')
     self.model_state_client = ServiceProxyWrapper(
         '/gazebo/set_model_state', SetModelState)
     self.track_data = TrackData.get_instance()
     # Camera configuration constants
     self.look_down_angle_rad = math.pi / 6.0  # 30 degree
     self.cam_dist_offset = 1.2
     self.cam_fixed_height = 1.0
     self.damping = 1.0
     # Camera states
     self.last_yaw = 0.0
     self.last_camera_state = None
Exemple #11
0
 def __init__(self, racecar_names):
     ''' Constructor for the Deep Racer object, will load track and waypoints
     '''
     # Wait for required services to be available
     rospy.wait_for_service('/gazebo/set_model_state')
     rospy.wait_for_service('/gazebo/get_model_state')
     rospy.wait_for_service('/gazebo/pause_physics')
     # We have no guarantees as to when gazebo will load the model, therefore we need
     # to wait until the model is loaded prior to resetting it for the first time
     self.get_model_client = ServiceProxyWrapper('/gazebo/get_model_state',
                                                 GetModelState)
     for racecar in racecar_names:
         wait_for_model = True
         while wait_for_model:
             # If the model is not loaded the get model service will log an error
             # Therefore, we add an timer to prevent the log from getting spammed with
             # errors
             time.sleep(WAIT_TO_PREVENT_SPAM)
             model = self.get_model_client(racecar, '')
             wait_for_model = not model.success
     # Grab the track data
     self.track_data = TrackData.get_instance()
     # Gazebo service that allows us to position the car
     self.model_state_client = ServiceProxyWrapper(
         '/gazebo/set_model_state', SetModelState)
     # Place the car at the starting point facing the forward direction
     # Instantiate cameras
     main_camera, sub_camera = configure_camera(namespaces=racecar_names)
     # Get the root directory of the ros package, this will contain the models
     deepracer_path = rospkg.RosPack().get_path(
         "deepracer_simulation_environment")
     for racecar in racecar_names:
         car_model_states = self.reset_car(racecar)
         main_camera[racecar].spawn_model(
             car_model_states,
             os.path.join(deepracer_path, "models", "camera", "model.sdf"))
     # Spawn the top camera model
     sub_camera.spawn_model(
         None,
         os.path.join(deepracer_path, "models", "top_camera", "model.sdf"))
     # Let KVS collect a few frames before pausing the physics, so the car
     # will appear on the track
     time.sleep(1)
     pause_physics = ServiceProxyWrapper('/gazebo/pause_physics', Empty)
     pause_physics(EmptyRequest())
    def __init__(self):
        if FollowCarCamera._instance_ is not None:
            raise GenericRolloutException("Attempting to construct multiple follow car camera")
        super(FollowCarCamera, self).__init__(FollowCarCamera.name)
        rospy.wait_for_service('/gazebo/set_model_state')
        self.model_state_client = ServiceProxyWrapper('/gazebo/set_model_state', SetModelState)
        self.track_data = TrackData.get_instance()
        # Camera configuration constants
        self.look_down_angle_rad = math.pi / 6.0  # 30 degree
        self.cam_dist_offset = 1.2
        self.cam_fixed_height = 1.0
        self.damping = 1.0
        # Camera states
        self.last_yaw = 0.0
        self.last_camera_state = None

        # there should be only one video camera instance
        FollowCarCamera._instance_ = self
Exemple #13
0
    def __init__(self, horizontal_fov, padding_percent, image_width, image_height, racecars_info):
        """ Camera information is required to map the (x, y) value of the agent on the camera image.
        This is because each track will have its own FOV and padding percent because to avoid
        Z-fighting. Once the camera position, padding percentage is available. We can map the
        (x,y) of the agent with respect to the track to a new plane of the camera image with
        padding.

        Arguments:
            horizontal_fov (float): Horizontal field of view of the camera for the given track
            padding_percent (float): The padding percentage to cover the whole track and look pretty
            image_width (int): Image width from the camera
            image_height (int): Image width from the camera
            racecars_info (list): This is the list of dicts of racecars on the track
        """
        self.horizontal_fov = horizontal_fov
        self.padding_percent = padding_percent
        self.image_width = image_width
        self.image_height = image_height
        self.racecars_info = force_list(racecars_info)

        rospy.wait_for_service('/gazebo/get_model_state')
        # We have no guarantees as to when gazebo will load the model, therefore we need
        # to wait until the model is loaded prior to resetting it for the first time
        self.get_model_client = ServiceProxyWrapper('/gazebo/get_model_state', GetModelState)
        self.model_names, self.model_imgs = self._get_all_models_info()

        wait_for_model = True
        while wait_for_model:
            # If the model is not loaded the get model service will log an error
            # Therefore, we add an timer to prevent the log from getting spammed with
            # errors
            time.sleep(WAIT_TO_PREVENT_SPAM)
            model_status = list()
            for model_name in self.model_names:
                model = self.get_model_client(model_name, '')
                model_status.append(model.success)
            wait_for_model = not all(model_status)

        # Track information is required get the track bounds (xmin, xmax, ymin, ymax)
        # Also the agent location for that instant
        self.track_data = TrackData.get_instance()
        # This maps the (x, y) of an agent in track frame to the top camera image frame
        self.initialize_parameters()
Exemple #14
0
    def __init__(self):
        self.track_data = TrackData.get_instance()

        # Read ros parameters
        self.num_obstacles = int(rospy.get_param("NUMBER_OF_OBSTACLES", 0))
        self.min_obstacle_dist = float(
            rospy.get_param("MIN_DISTANCE_BETWEEN_OBSTACLES", 2.0))
        self.randomize = utils.str2bool(
            rospy.get_param("RANDOMIZE_OBSTACLE_LOCATIONS", False))
        self.use_bot_car_urdf = utils.str2bool(
            rospy.get_param("IS_OBSTACLE_BOT_CAR", False))
        self.obstacle_names = [
            "obstacle_{}".format(i) for i in range(self.num_obstacles)
        ]
        self.obstacle_dimensions = ObstacleDimensions.BOT_CAR_DIMENSION if self.use_bot_car_urdf \
                                   else ObstacleDimensions.BOX_OBSTACLE_DIMENSION

        # Wait for ros services
        rospy.wait_for_service(SET_MODEL_STATE)
        rospy.wait_for_service(SPAWN_SDF_MODEL)
        rospy.wait_for_service(SPAWN_URDF_MODEL)
        self.set_model_state = ServiceProxyWrapper(SET_MODEL_STATE,
                                                   SetModelState)
        self.spawn_sdf_model = ServiceProxyWrapper(SPAWN_SDF_MODEL, SpawnModel)
        self.spawn_urdf_model = ServiceProxyWrapper(SPAWN_URDF_MODEL,
                                                    SpawnModel)

        # Load the obstacle sdf/urdf
        if self.use_bot_car_urdf:
            self.bot_car_urdf = rospy.get_param('robot_description_bot')
        else:
            rospack = rospkg.RosPack()
            deepracer_path = rospack.get_path(
                "deepracer_simulation_environment")
            obstacle_sdf_path = os.path.join(deepracer_path, "models",
                                             "box_obstacle", "model.sdf")
            with open(obstacle_sdf_path, "r") as fp:
                self.obstacle_sdf = fp.read()

        # Spawn the obstacles
        self._spawn_obstacles()
Exemple #15
0
    def __init__(self, agent_name, s3_dict_metrics, is_continuous):
        '''Init eval metrics

        Args:
            agent_name (string): agent name
            s3_dict_metrics (dict): Dictionary containing the required
                s3 info for the metrics bucket with keys specified by MetricsS3Keys
            is_continuous (bool): True if continuous race, False otherwise
        '''
        self._agent_name_ = agent_name
        self._s3_dict_metrics_ = s3_dict_metrics
        self._is_continuous = is_continuous        
        self._start_time_ = time.time()
        self._number_of_trials_ = 0
        self._progress_ = 0.0
        self._episode_status = ''
        self._metrics_ = list()
        # This is used to calculate the actual distance traveled by the car
        self._agent_xy = list()
        self._prev_step_time = time.time()
        self.is_save_simtrace_enabled = rospy.get_param('SIMTRACE_S3_BUCKET', None)
        # Create the agent specific directories needed for storing the metric files
        simtrace_dirname = os.path.dirname(IterationDataLocalFileNames.SIM_TRACE_EVALUATION_LOCAL_FILE.value)
        if not os.path.exists(os.path.join(ITERATION_DATA_LOCAL_FILE_PATH, self._agent_name_, simtrace_dirname)):
            os.makedirs(os.path.join(ITERATION_DATA_LOCAL_FILE_PATH, self._agent_name_, simtrace_dirname))
        self.reset_count_dict = {EpisodeStatus.CRASHED.value: 0,
                                 EpisodeStatus.OFF_TRACK.value: 0,
                                 EpisodeStatus.IMMOBILIZED.value: 0,
                                 EpisodeStatus.REVERSED.value: 0}
        self._best_lap_time = float('inf')
        self._total_evaluation_time = 0
        self._video_metrics = Mp4VideoMetrics.get_empty_dict()
        self._reset_count_sum = 0
        self._current_sim_time = 0
        self.track_data = TrackData.get_instance()        
        rospy.Service("/{}/{}".format(self._agent_name_, "mp4_video_metrics"), VideoMetricsSrv,
                      self._handle_get_video_metrics)

        AbstractTracker.__init__(self, TrackerPriority.HIGH)
Exemple #16
0
 def __init__(self, agent_name, s3_dict_metrics, s3_dict_model, ckpnt_dir, run_phase_sink, use_model_picker=True):
     '''s3_dict_metrics - Dictionary containing the required s3 info for the metrics
                          bucket with keys specified by MetricsS3Keys
        s3_dict_model - Dictionary containing the required s3 info for the model
                        bucket, which is where the best model info will be saved with
                        keys specified by MetricsS3Keys
        ckpnt_dir - Directory where the current checkpont is to be stored
        run_phase_sink - Sink to recieve notification of a change in run phase
        use_model_picker - Flag to whether to use model picker or not.
     '''
     self._agent_name_ = agent_name
     self._s3_dict_metrics_ = s3_dict_metrics
     self._s3_dict_model_ = s3_dict_model
     self._start_time_ = time.time()
     self._episode_ = 0
     self._episode_reward_ = 0.0
     self._progress_ = 0.0
     self._episode_status = ''
     self._metrics_ = list()
     self._is_eval_ = True
     self._eval_trials_ = 0
     self._checkpoint_state_ = CheckpointStateFile(ckpnt_dir)
     self._use_model_picker = use_model_picker
     self._eval_stats_dict_ = {'chkpnt_name': None, 'avg_comp_pct': -1.0}
     self._best_chkpnt_stats = {'name': None, 'avg_comp_pct': -1.0, 'time_stamp': time.time()}
     self._current_eval_pct_list_ = list()
     self.is_save_simtrace_enabled = rospy.get_param('SIMTRACE_S3_BUCKET', None)
     self.track_data = TrackData.get_instance()
     run_phase_sink.register(self)
     # Create the agent specific directories needed for storing the metric files
     simtrace_dirname = os.path.dirname(IterationDataLocalFileNames.SIM_TRACE_TRAINING_LOCAL_FILE.value)
     if not os.path.exists(os.path.join(ITERATION_DATA_LOCAL_FILE_PATH, self._agent_name_, simtrace_dirname)):
         os.makedirs(os.path.join(ITERATION_DATA_LOCAL_FILE_PATH, self._agent_name_, simtrace_dirname))
     self._current_sim_time = 0
     rospy.Service("/{}/{}".format(self._agent_name_, "mp4_video_metrics"), VideoMetricsSrv,
                   self._handle_get_video_metrics)
     self._video_metrics = Mp4VideoMetrics.get_empty_dict()
     AbstractTracker.__init__(self, TrackerPriority.HIGH)
Exemple #17
0
    def __init__(self, horizontal_fov, padding_percent, image_width,
                 image_height, racecars_info):
        """ Camera information is required to map the (x, y) value of the agent on the camera image.
        This is because each track will have its own FOV and padding percent because to avoid
        Z-fighting. Once the camera position, padding percentage is available. We can map the
        (x,y) of the agent with respect to the track to a new plane of the camera image with
        padding.

        Arguments:
            horizontal_fov (float): Horizontal field of view of the camera for the given track
            padding_percent (float): The padding percentage to cover the whole track and look pretty
            image_width (int): Image width from the camera
            image_height (int): Image width from the camera
            racecars_info (list): This is the list of dicts of racecars on the track
        """
        self.horizontal_fov = horizontal_fov
        self.padding_percent = padding_percent
        self.image_width = image_width
        self.image_height = image_height
        self.racecars_info = force_list(racecars_info)

        #
        # We have no guarantees as to when gazebo will load the model, therefore we need
        # to wait until the model is loaded and markov packages has spawned all the models
        #
        rospy.wait_for_service('/gazebo/get_model_state')
        rospy.wait_for_service('/robomaker_markov_package_ready')
        self.get_model_client = ServiceProxyWrapper('/gazebo/get_model_state',
                                                    GetModelState)
        self.model_names, self.model_imgs = self._get_all_models_info()

        # Track information is required get the track bounds (xmin, xmax, ymin, ymax)
        # Also the agent location for that instant
        self.track_data = TrackData.get_instance()
        # This maps the (x, y) of an agent in track frame to the top camera image frame
        self.initialize_parameters()
def tournament_worker(graph_manager, number_of_trials, task_parameters,
                      s3_writers, is_continuous, park_positions):
    """ Tournament worker function

    Arguments:
        graph_manager(MultiAgentGraphManager): Graph manager of multiagent graph manager
        number_of_trials(int): Number of trails you want to run the evaluation
        task_parameters(TaskParameters): Information of the checkpoint, gpu/cpu,
            framework etc of rlcoach
        s3_writers(S3Writer): Information to upload to the S3 bucket all the simtrace and mp4
        is_continuous(bool): The termination condition for the car
        park_positions(list of tuple): list of (x, y) for cars to park at
    """
    # Collect profiler information only IS_PROFILER_ON is true
    with utils.Profiler(s3_bucket=PROFILER_S3_BUCKET,
                        s3_prefix=PROFILER_S3_PREFIX,
                        output_local_path=ROLLOUT_WORKER_PROFILER_PATH,
                        enable_profiling=IS_PROFILER_ON):
        checkpoint_dirs = list()
        agent_names = list()
        subscribe_to_save_mp4_topic, unsubscribe_from_save_mp4_topic = list(
        ), list()
        subscribe_to_save_mp4, unsubscribe_from_save_mp4 = list(), list()
        for agent_param in graph_manager.agents_params:
            _checkpoint_dir = task_parameters.checkpoint_restore_path if len(graph_manager.agents_params) == 1 \
                else os.path.join(task_parameters.checkpoint_restore_path, agent_param.name)
            agent_names.append(agent_param.name)
            checkpoint_dirs.append(_checkpoint_dir)
            racecar_name = 'racecar' if len(agent_param.name.split("_")) == 1 \
                                     else "racecar_{}".format(agent_param.name.split("_")[1])
            subscribe_to_save_mp4_topic.append(
                "/{}/save_mp4/subscribe_to_save_mp4".format(racecar_name))
            unsubscribe_from_save_mp4_topic.append(
                "/{}/save_mp4/unsubscribe_from_save_mp4".format(racecar_name))
        wait_for_checkpoints(checkpoint_dirs, graph_manager.data_store)
        modify_checkpoint_variables(checkpoint_dirs, agent_names)

        # Make the clients that will allow us to pause and unpause the physics
        rospy.wait_for_service('/gazebo/pause_physics')
        rospy.wait_for_service('/gazebo/unpause_physics')
        pause_physics = ServiceProxyWrapper('/gazebo/pause_physics', Empty)
        unpause_physics = ServiceProxyWrapper('/gazebo/unpause_physics', Empty)

        for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic,
                                      unsubscribe_from_save_mp4_topic):
            rospy.wait_for_service(mp4_sub)
            rospy.wait_for_service(mp4_unsub)
        for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic,
                                      unsubscribe_from_save_mp4_topic):
            subscribe_to_save_mp4.append(ServiceProxyWrapper(mp4_sub, Empty))
            unsubscribe_from_save_mp4.append(
                ServiceProxyWrapper(mp4_unsub, Empty))

        graph_manager.create_graph(task_parameters=task_parameters,
                                   stop_physics=pause_physics,
                                   start_physics=unpause_physics,
                                   empty_service_call=EmptyRequest)
        logger.info(
            "Graph manager successfully created the graph: Unpausing physics")
        unpause_physics(EmptyRequest())

        is_save_mp4_enabled = rospy.get_param('MP4_S3_BUCKET', None)
        if is_save_mp4_enabled:
            for subscribe_mp4 in subscribe_to_save_mp4:
                subscribe_mp4(EmptyRequest())

        configure_environment_randomizer()
        track_data = TrackData.get_instance()

        # Before each evaluation episode (single lap for non-continuous race and complete race for
        # continuous race), a new copy of park_positions needs to be loaded into track_data because
        # a park position will be pop from park_positions when a racer car need to be parked.
        if is_continuous:
            track_data.park_positions = park_positions
            graph_manager.evaluate(EnvironmentSteps(1))
        else:
            for _ in range(number_of_trials):
                track_data.park_positions = park_positions
                graph_manager.evaluate(EnvironmentSteps(1))
        if is_save_mp4_enabled:
            for unsubscribe_mp4 in unsubscribe_from_save_mp4:
                unsubscribe_mp4(EmptyRequest())
        for s3_writer in s3_writers:
            s3_writer.upload_to_s3()
        time.sleep(1)
        pause_physics(EmptyRequest())
Exemple #19
0
    def __init__(self,
                 queue_url,
                 aws_region='us-east-1',
                 race_duration=180,
                 number_of_trials=3,
                 number_of_resets=10000,
                 penalty_seconds=2.0,
                 off_track_penalty=2.0,
                 collision_penalty=5.0,
                 is_continuous=False,
                 race_type="TIME_TRIAL"):
        # constructor arguments
        self._model_updater = ModelUpdater.get_instance()
        self._deepracer_path = rospkg.RosPack().get_path(
            DeepRacerPackages.DEEPRACER_SIMULATION_ENVIRONMENT)
        body_shell_path = os.path.join(self._deepracer_path, "meshes", "f1")
        self._valid_body_shells = \
            set(".".join(f.split(".")[:-1]) for f in os.listdir(body_shell_path) if os.path.isfile(
                os.path.join(body_shell_path, f)))
        self._valid_body_shells.add(const.BodyShellType.DEFAULT.value)
        self._valid_car_colors = set(e.value for e in const.CarColorType
                                     if "f1" not in e.value)
        self._num_sectors = int(rospy.get_param("NUM_SECTORS", "3"))
        self._queue_url = queue_url
        self._region = aws_region
        self._number_of_trials = number_of_trials
        self._number_of_resets = number_of_resets
        self._penalty_seconds = penalty_seconds
        self._off_track_penalty = off_track_penalty
        self._collision_penalty = collision_penalty
        self._is_continuous = is_continuous
        self._race_type = race_type
        self._is_save_simtrace_enabled = False
        self._is_save_mp4_enabled = False
        self._is_event_end = False
        self._done_condition = any
        self._race_duration = race_duration
        self._enable_domain_randomization = False

        # sqs client
        # The boto client errors out after polling for 1 hour.
        self._sqs_client = SQSClient(queue_url=self._queue_url,
                                     region_name=self._region,
                                     max_num_of_msg=MAX_NUM_OF_SQS_MESSAGE,
                                     wait_time_sec=SQS_WAIT_TIME_SEC,
                                     session=refreshed_session(self._region))
        self._s3_client = S3Client(region_name=self._region)
        # tracking current state information
        self._track_data = TrackData.get_instance()
        self._start_lane = self._track_data.center_line
        # keep track of the racer specific info, e.g. s3 locations, alias, car color etc.
        self._current_racer = None
        # keep track of the current race car we are using. It is always "racecar".
        car_model_state = ModelState()
        car_model_state.model_name = "racecar"
        self._current_car_model_state = car_model_state
        self._last_body_shell_type = None
        self._last_sensors = None
        self._racecar_model = AgentModel()
        # keep track of the current control agent we are using
        self._current_agent = None
        # keep track of the current control graph manager
        self._current_graph_manager = None
        # Keep track of previous model's name
        self._prev_model_name = None
        self._hide_position_idx = 0
        self._hide_positions = get_hide_positions(race_car_num=1)
        self._run_phase_subject = RunPhaseSubject()
        self._simtrace_video_s3_writers = []

        self._local_model_directory = './checkpoint'

        # virtual event only have single agent, so set agent_name to "agent"
        self._agent_name = "agent"

        # camera manager
        self._camera_manager = CameraManager.get_instance()

        # setting up virtual event top and follow camera in CameraManager
        # virtual event configure camera does not need to wait for car to spawm because
        # follow car camera is not tracking any car initially
        self._main_cameras, self._sub_camera = configure_camera(
            namespaces=[VIRTUAL_EVENT], is_wait_for_model=False)
        self._spawn_cameras()

        # pop out all cameras after configuration to prevent camera from moving
        self._camera_manager.pop(namespace=VIRTUAL_EVENT)

        dummy_metrics_s3_config = {
            MetricsS3Keys.METRICS_BUCKET.value: "dummy-bucket",
            MetricsS3Keys.METRICS_KEY.value: "dummy-key",
            MetricsS3Keys.REGION.value: self._region
        }

        self._eval_metrics = EvalMetrics(
            agent_name=self._agent_name,
            s3_dict_metrics=dummy_metrics_s3_config,
            is_continuous=self._is_continuous,
            pause_time_before_start=PAUSE_TIME_BEFORE_START)

        # upload a default best sector time for all sectors with time inf for each sector
        # if there is not best sector time existed in s3

        # use the s3 bucket and prefix for yaml file stored as environment variable because
        # here is SimApp use only. For virtual event there is no s3 bucket and prefix past
        # through yaml file. All are past through sqs. For simplicity, reuse the yaml s3 bucket
        # and prefix environment variable.
        virtual_event_best_sector_time = VirtualEventBestSectorTime(
            bucket=os.environ.get("YAML_S3_BUCKET", ''),
            s3_key=get_s3_key(os.environ.get("YAML_S3_PREFIX", ''),
                              SECTOR_TIME_S3_POSTFIX),
            region_name=os.environ.get("APP_REGION", "us-east-1"),
            local_path=SECTOR_TIME_LOCAL_PATH)
        response = virtual_event_best_sector_time.list()
        # this is used to handle situation such as robomaker job crash, so the next robomaker job
        # can catch the best sector time left over from crashed job
        if "Contents" not in response:
            virtual_event_best_sector_time.persist(
                body=json.dumps({
                    SECTOR_X_FORMAT.format(idx + 1): float("inf")
                    for idx in range(self._num_sectors)
                }),
                s3_kms_extra_args=utils.get_s3_kms_extra_args())

        # ROS service to indicate all the robomaker markov packages are ready for consumption
        signal_robomaker_markov_package_ready()

        PhaseObserver('/agent/training_phase', self._run_phase_subject)

        # setup mp4 services
        self._setup_mp4_services()
Exemple #20
0
def evaluation_worker(graph_manager, number_of_trials, task_parameters,
                      simtrace_video_s3_writers, is_continuous, park_positions,
                      race_type, pause_physics, unpause_physics):
    """ Evaluation worker function

    Arguments:
        graph_manager(MultiAgentGraphManager): Graph manager of multiagent graph manager
        number_of_trials(int): Number of trails you want to run the evaluation
        task_parameters(TaskParameters): Information of the checkpoint, gpu/cpu,
            framework etc of rlcoach
        simtrace_video_s3_writers(list): Information to upload to the S3 bucket all the simtrace and mp4
        is_continuous(bool): The termination condition for the car
        park_positions(list of tuple): list of (x, y) for cars to park at
        race_type (str): race type
    """
    # Collect profiler information only IS_PROFILER_ON is true
    with utils.Profiler(s3_bucket=PROFILER_S3_BUCKET,
                        s3_prefix=PROFILER_S3_PREFIX,
                        output_local_path=ROLLOUT_WORKER_PROFILER_PATH,
                        enable_profiling=IS_PROFILER_ON):
        subscribe_to_save_mp4_topic, unsubscribe_from_save_mp4_topic = list(
        ), list()
        subscribe_to_save_mp4, unsubscribe_from_save_mp4 = list(), list()
        for agent_param in graph_manager.agents_params:
            racecar_name = 'racecar' if len(agent_param.name.split("_")) == 1 \
                                     else "racecar_{}".format(agent_param.name.split("_")[1])
            subscribe_to_save_mp4_topic.append(
                "/{}/save_mp4/subscribe_to_save_mp4".format(racecar_name))
            unsubscribe_from_save_mp4_topic.append(
                "/{}/save_mp4/unsubscribe_from_save_mp4".format(racecar_name))
        graph_manager.data_store.wait_for_checkpoints()
        graph_manager.data_store.modify_checkpoint_variables()

        # wait for the required cancel services to become available
        if race_type != RaceType.F1.value:
            # TODO: Since we are not running Grand Prix in RoboMaker,
            # we are opting out from waiting for RoboMaker's cancel job service
            # in case of Grand Prix execution.
            # Otherwise, SimApp will hang as service will never come alive.
            #
            # If we don't depend on RoboMaker anymore in the future,
            # we need to remove below line, or do a better job to figure out
            # whether we are running on RoboMaker or not to decide whether
            # we should wait for below service or not.
            rospy.wait_for_service('/robomaker/job/cancel')

        # Make the clients that will allow us to pause and unpause the physics
        rospy.wait_for_service('/gazebo/pause_physics_dr')
        rospy.wait_for_service('/gazebo/unpause_physics_dr')
        pause_physics = ServiceProxyWrapper('/gazebo/pause_physics_dr', Empty)
        unpause_physics = ServiceProxyWrapper('/gazebo/unpause_physics_dr',
                                              Empty)

        for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic,
                                      unsubscribe_from_save_mp4_topic):
            rospy.wait_for_service(mp4_sub)
            rospy.wait_for_service(mp4_unsub)
        for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic,
                                      unsubscribe_from_save_mp4_topic):
            subscribe_to_save_mp4.append(ServiceProxyWrapper(mp4_sub, Empty))
            unsubscribe_from_save_mp4.append(
                Thread(target=ServiceProxyWrapper(mp4_unsub, Empty),
                       args=(EmptyRequest(), )))

        graph_manager.create_graph(task_parameters=task_parameters,
                                   stop_physics=pause_physics,
                                   start_physics=unpause_physics,
                                   empty_service_call=EmptyRequest)
        logger.info(
            "Graph manager successfully created the graph: Unpausing physics")
        unpause_physics(EmptyRequest())

        is_save_mp4_enabled = rospy.get_param('MP4_S3_BUCKET', None)
        if is_save_mp4_enabled:
            for subscribe_mp4 in subscribe_to_save_mp4:
                subscribe_mp4(EmptyRequest())

        configure_environment_randomizer()
        track_data = TrackData.get_instance()

        # Before each evaluation episode (single lap for non-continuous race and complete race for
        # continuous race), a new copy of park_positions needs to be loaded into track_data because
        # a park position will be pop from park_positions when a racer car need to be parked.
        if is_continuous:
            track_data.park_positions = park_positions
            graph_manager.evaluate(EnvironmentSteps(1))
        else:
            for _ in range(number_of_trials):
                track_data.park_positions = park_positions
                graph_manager.evaluate(EnvironmentSteps(1))
        if is_save_mp4_enabled:
            for unsubscribe_mp4 in unsubscribe_from_save_mp4:
                unsubscribe_mp4.start()
            for unsubscribe_mp4 in unsubscribe_from_save_mp4:
                unsubscribe_mp4.join()
        # upload simtrace and mp4 into s3 bucket
        for s3_writer in simtrace_video_s3_writers:
            s3_writer.persist(utils.get_s3_kms_extra_args())
        time.sleep(1)
        pause_physics(EmptyRequest())

    if race_type != RaceType.F1.value:
        # Close the down the job
        utils.cancel_simulation_job()
 def __init__(self, config_dict):
     '''agent_name - String containing the name of the agent
        config_dict - Dictionary containing all the keys in ConfigParams
     '''
     # simapp_version speed scale
     self._speed_scale_factor_ = get_speed_factor(
         config_dict[const.ConfigParams.VERSION.value])
     # Store the name of the agent used to set agents position on the track
     self._agent_name_ = config_dict[const.ConfigParams.AGENT_NAME.value]
     # Store the name of the links in the agent, this should be const
     self._agent_link_name_list_ = config_dict[
         const.ConfigParams.LINK_NAME_LIST.value]
     # Store the reward function
     self._reward_ = config_dict[const.ConfigParams.REWARD.value]
     self._track_data_ = TrackData.get_instance()
     # Create publishers for controlling the car
     self._velocity_pub_dict_ = OrderedDict()
     self._steering_pub_dict_ = OrderedDict()
     for topic in config_dict[const.ConfigParams.VELOCITY_LIST.value]:
         self._velocity_pub_dict_[topic] = rospy.Publisher(topic,
                                                           Float64,
                                                           queue_size=1)
     for topic in config_dict[const.ConfigParams.STEERING_LIST.value]:
         self._steering_pub_dict_[topic] = rospy.Publisher(topic,
                                                           Float64,
                                                           queue_size=1)
     #Create default reward parameters
     self._reward_params_ = const.RewardParam.make_default_param()
     #Creat the default metrics dictionary
     self._step_metrics_ = StepMetrics.make_default_metric()
     # State variable to track if the car direction has been reversed
     self._reverse_dir_ = False
     # Dictionary of bools indicating starting position behavior
     self._start_pos_behavior_ = \
         {'change_start' : config_dict[const.ConfigParams.CHANGE_START.value],
          'alternate_dir' : config_dict[const.ConfigParams.ALT_DIR.value]}
     # Dictionary to track the previous way points
     self._prev_waypoints_ = {
         'prev_point': Point(0, 0),
         'prev_point_2': Point(0, 0)
     }
     # Dictionary containing some of the data for the agent
     self._data_dict_ = {
         'prev_progress': 0.0,
         'steps': 0.0,
         'start_ndist': 0.0
     }
     #Load the action space
     self._action_space_, self._json_actions_ = \
         load_action_space(config_dict[const.ConfigParams.ACTION_SPACE_PATH.value])
     #! TODO evaluate if this is the best way to reset the car
     rospy.wait_for_service(SET_MODEL_STATE)
     rospy.wait_for_service(GET_MODEL_STATE)
     self.set_model_state = ServiceProxyWrapper(SET_MODEL_STATE,
                                                SetModelState)
     self.get_model_client = ServiceProxyWrapper(GET_MODEL_STATE,
                                                 GetModelState)
     # Adding the reward data publisher
     self.reward_data_pub = RewardDataPublisher(self._agent_name_,
                                                self._json_actions_)
     # init time
     self.last_time = 0.0
     self.curr_time = 0.0
     # subscriber to time to update camera position
     camera_types = [camera for camera in CameraType]
     self.camera_manager = CameraManager(camera_types=camera_types)
     rospy.Subscriber('/clock', Clock, self.update_camera)
     # Make sure velicty and angle are set to 0
     send_action(self._velocity_pub_dict_, self._steering_pub_dict_, 0.0,
                 0.0)
    def __init__(self, racecar_names):
        ''' Constructor for the Deep Racer object, will load track and waypoints
        '''
        # Wait for required services to be available
        rospy.wait_for_service(SET_MODEL_STATE)
        rospy.wait_for_service(GazeboServiceName.PAUSE_PHYSICS.value)
        rospy.wait_for_service(GazeboServiceName.GET_MODEL_PROPERTIES.value)
        rospy.wait_for_service(GazeboServiceName.GET_VISUAL_NAMES.value)
        rospy.wait_for_service(GazeboServiceName.GET_VISUALS.value)
        rospy.wait_for_service(GazeboServiceName.SET_VISUAL_COLORS.value)
        rospy.wait_for_service(GazeboServiceName.SET_VISUAL_TRANSPARENCIES.value)
        rospy.wait_for_service(GazeboServiceName.SET_VISUAL_VISIBLES.value)

        self.racer_num = len(racecar_names)
        for racecar_name in racecar_names:
            wait_for_model(model_name=racecar_name, relative_entity_name='')
        self.car_colors = force_list(rospy.get_param(const.YamlKey.CAR_COLOR.value,
                                                     [const.CarColorType.BLACK.value] * len(racecar_names)))
        self.shell_types = force_list(rospy.get_param(const.YamlKey.BODY_SHELL_TYPE.value,
                                                      [const.BodyShellType.DEFAULT.value] * len(racecar_names)))
        # Gazebo service that allows us to position the car
        self.model_state_client = ServiceProxyWrapper(SET_MODEL_STATE, SetModelState)

        self.get_model_prop = ServiceProxyWrapper(GazeboServiceName.GET_MODEL_PROPERTIES.value,
                                                  GetModelProperties)
        self.get_visual_names = ServiceProxyWrapper(GazeboServiceName.GET_VISUAL_NAMES.value,
                                                    GetVisualNames)
        self.get_visuals = ServiceProxyWrapper(GazeboServiceName.GET_VISUALS.value, GetVisuals)
        self.set_visual_colors = ServiceProxyWrapper(GazeboServiceName.SET_VISUAL_COLORS.value,
                                                     SetVisualColors)
        self.set_visual_transparencies = ServiceProxyWrapper(GazeboServiceName.SET_VISUAL_TRANSPARENCIES.value,
                                                             SetVisualTransparencies)
        self.set_visual_visibles = ServiceProxyWrapper(GazeboServiceName.SET_VISUAL_VISIBLES.value,
                                                       SetVisualVisibles)

        # Place the car at the starting point facing the forward direction
        # Instantiate cameras
        main_cameras, sub_camera = configure_camera(namespaces=racecar_names)
        [camera.detach() for camera in main_cameras.values()]
        sub_camera.detach()
        # Get the root directory of the ros package, this will contain the models
        deepracer_path = rospkg.RosPack().get_path("deepracer_simulation_environment")
        # Grab the track data
        self.track_data = TrackData.get_instance()
        # Set all racers start position in track data
        self.start_positions = get_start_positions(self.racer_num)
        car_poses = []
        for racecar_idx, racecar_name in enumerate(racecar_names):
            car_model_state = self.get_initial_position(racecar_name,
                                                        racecar_idx)
            car_poses.append(car_model_state.pose)
            self.update_model_visual(racecar_name,
                                     self.shell_types[racecar_idx],
                                     self.car_colors[racecar_idx])

        # Let KVS collect a few frames before pausing the physics, so the car
        # will appear on the track
        time.sleep(1)
        pause_physics = ServiceProxyWrapper('/gazebo/pause_physics', Empty)
        logger.info("Pausing physics after initializing the cars")
        pause_physics(EmptyRequest())

        for racecar_name, car_pose in zip(racecar_names, car_poses):
            main_cameras[racecar_name].spawn_model(car_pose,
                                                   os.path.join(deepracer_path, "models",
                                                                "camera", "model.sdf"))

        logger.info("Spawning sub camera model")
        # Spawn the top camera model
        sub_camera.spawn_model(None, os.path.join(deepracer_path, "models",
                                                  "top_camera", "model.sdf"))
Exemple #23
0
 def __init__(self):
     self._track_data = TrackData.get_instance()
     self.build_spline()
Exemple #24
0
 def __init__(self, config_dict, run_phase_sink, metrics):
     '''agent_name - String containing the name of the agent
        config_dict - Dictionary containing all the keys in ConfigParams
        run_phase_sink - Sink to recieve notification of a change in run phase
     '''
     # reset rules manager
     self._metrics = metrics
     self._is_continuous = config_dict[const.ConfigParams.IS_CONTINUOUS.value]
     self._is_reset = False
     self._pause_count = 0
     self._reset_rules_manager = construct_reset_rules_manager(config_dict)
     self._ctrl_status = dict()
     self._ctrl_status[AgentCtrlStatus.AGENT_PHASE.value] = AgentPhase.RUN.value
     self._config_dict = config_dict
     self._number_of_resets = config_dict[const.ConfigParams.NUMBER_OF_RESETS.value]
     self._off_track_penalty = config_dict[const.ConfigParams.OFF_TRACK_PENALTY.value]
     self._collision_penalty = config_dict[const.ConfigParams.COLLISION_PENALTY.value]
     self._pause_end_time = 0.0
     self._reset_count = 0
     # simapp_version speed scale
     self._speed_scale_factor_ = get_speed_factor(config_dict[const.ConfigParams.VERSION.value])
     # Store the name of the agent used to set agents position on the track
     self._agent_name_ = config_dict[const.ConfigParams.AGENT_NAME.value]
     # Store the name of the links in the agent, this should be const
     self._agent_link_name_list_ = config_dict[const.ConfigParams.LINK_NAME_LIST.value]
     # Store the reward function
     self._reward_ = config_dict[const.ConfigParams.REWARD.value]
     self._track_data_ = TrackData.get_instance()
     # Create publishers for controlling the car
     self._velocity_pub_dict_ = OrderedDict()
     self._steering_pub_dict_ = OrderedDict()
     for topic in config_dict[const.ConfigParams.VELOCITY_LIST.value]:
         self._velocity_pub_dict_[topic] = rospy.Publisher(topic, Float64, queue_size=1)
     for topic in config_dict[const.ConfigParams.STEERING_LIST.value]:
         self._steering_pub_dict_[topic] = rospy.Publisher(topic, Float64, queue_size=1)
     #Create default reward parameters
     self._reward_params_ = const.RewardParam.make_default_param()
     #Creat the default metrics dictionary
     self._step_metrics_ = StepMetrics.make_default_metric()
     # State variable to track if the car direction has been reversed
     self._reverse_dir_ = False
     # Dictionary of bools indicating starting position behavior
     self._start_pos_behavior_ = \
         {'change_start' : config_dict[const.ConfigParams.CHANGE_START.value],
          'alternate_dir' : config_dict[const.ConfigParams.ALT_DIR.value]}
     # Dictionary to track the previous way points
     self._prev_waypoints_ = {'prev_point' : Point(0, 0), 'prev_point_2' : Point(0, 0)}
     # Dictionary containing some of the data for the agent
     self._data_dict_ = {'max_progress': 0.0,
                         'current_progress': 0.0,
                         'prev_progress': 0.0,
                         'steps': 0.0,
                         'start_ndist': 0.0}
     #Load the action space
     self._action_space_, self._json_actions_ = \
         load_action_space(config_dict[const.ConfigParams.ACTION_SPACE_PATH.value])
     #! TODO evaluate if this is the best way to reset the car
     rospy.wait_for_service(SET_MODEL_STATE)
     rospy.wait_for_service(GET_MODEL_STATE)
     self.set_model_state = ServiceProxyWrapper(SET_MODEL_STATE, SetModelState)
     self.get_model_client = ServiceProxyWrapper(GET_MODEL_STATE, GetModelState)
     # Adding the reward data publisher
     self.reward_data_pub = RewardDataPublisher(self._agent_name_, self._json_actions_)
     # init time
     self.last_time = 0.0
     self.curr_time = 0.0
     # subscriber to time to update camera position
     self.camera_manager = CameraManager.get_instance()
     # True if the agent is in the training phase
     self._is_training_ = False
     rospy.Subscriber('/clock', Clock, self._update_sim_time)
     # Register to the phase sink
     run_phase_sink.register(self)
     # Make sure velicty and angle are set to 0
     send_action(self._velocity_pub_dict_, self._steering_pub_dict_, 0.0, 0.0)
     start_pose = self._track_data_._center_line_.interpolate_pose(self._data_dict_['start_ndist'] * self._track_data_.get_track_length(),
                                                                   reverse_dir=self._reverse_dir_,
                                                                   finite_difference=FiniteDifference.FORWARD_DIFFERENCE)
     self._track_data_.initialize_object(self._agent_name_, start_pose, ObstacleDimensions.BOT_CAR_DIMENSION)
     self.car_model_state = self.get_model_client(self._agent_name_, '')
     self._reset_agent(reset_pos=const.ResetPos.START_POS.value)
 def __init__(self):
     super(OffTrackResetRule, self).__init__(OffTrackResetRule.name)
     self._track_data = TrackData.get_instance()
    def __init__(self,
                 agent_name,
                 s3_dict_metrics,
                 is_continuous,
                 pause_time_before_start=0.0):
        '''Init eval metrics

        Args:
            agent_name (string): agent name
            s3_dict_metrics (dict): Dictionary containing the required
                s3 info for the metrics bucket with keys specified by MetricsS3Keys
            is_continuous (bool): True if continuous race, False otherwise
            pause_time_before_start (float): second to pause before race start
        '''
        self._pause_time_before_start = pause_time_before_start
        self._is_pause_time_subtracted = False
        self._agent_name_ = agent_name
        self._s3_metrics = Metrics(
            bucket=s3_dict_metrics[MetricsS3Keys.METRICS_BUCKET.value],
            s3_key=s3_dict_metrics[MetricsS3Keys.METRICS_KEY.value],
            region_name=s3_dict_metrics[MetricsS3Keys.REGION.value],
            s3_endpoint_url=s3_dict_metrics[MetricsS3Keys.ENDPOINT_URL.value])
        self._is_continuous = is_continuous
        self._start_time_ = time.time()
        self._number_of_trials_ = 0
        self._progress_ = 0.0
        self._episode_status = ''
        self._metrics_ = list()
        # This is used to calculate the actual distance traveled by the car
        self._agent_xy = list()
        self._prev_step_time = time.time()
        self.is_save_simtrace_enabled = rospy.get_param(
            'SIMTRACE_S3_BUCKET', None)
        # Create the agent specific directories needed for storing the metric files
        self._simtrace_local_path = SIMTRACE_EVAL_LOCAL_PATH_FORMAT.format(
            self._agent_name_)
        simtrace_dirname = os.path.dirname(self._simtrace_local_path)
        # addressing mkdir and check directory race condition:
        # https://stackoverflow.com/questions/12468022/python-fileexists-error-when-making-directory/30174982#30174982
        # TODO: change this to os.makedirs(simtrace_dirname, exist_ok=True) when we migrate off python 2.7
        try:
            os.makedirs(simtrace_dirname)
        except OSError as e:
            if e.errno != errno.EEXIST:
                raise
            LOGGER.error("File already exist %s", simtrace_dirname)

        self.reset_count_dict = {
            EpisodeStatus.CRASHED.value: 0,
            EpisodeStatus.OFF_TRACK.value: 0,
            EpisodeStatus.IMMOBILIZED.value: 0,
            EpisodeStatus.REVERSED.value: 0
        }
        self._best_lap_time = float('inf')
        self._total_evaluation_time = 0
        self._video_metrics = Mp4VideoMetrics.get_empty_dict()
        self._reset_count_sum = 0
        self._current_sim_time = 0
        self.track_data = TrackData.get_instance()
        rospy.Service("/{}/{}".format(self._agent_name_, "mp4_video_metrics"),
                      VideoMetricsSrv, self._handle_get_video_metrics)
        AbstractTracker.__init__(self, TrackerPriority.HIGH)
    def __init__(self, config_dict, run_phase_sink, metrics):
        '''config_dict (dict): containing all the keys in ConfigParams
           run_phase_sink (RunPhaseSubject): Sink to receive notification of a change in run phase
           metrics (EvalMetrics/TrainingMetrics): Training or evaluation metrics
        '''
        # reset rules manager
        self._metrics = metrics
        self._is_continuous = config_dict[
            const.ConfigParams.IS_CONTINUOUS.value]
        self._reset_rules_manager = construct_reset_rules_manager(config_dict)
        self._ctrl_status = dict()
        self._ctrl_status[
            AgentCtrlStatus.AGENT_PHASE.value] = AgentPhase.RUN.value
        self._config_dict = config_dict
        self._done_condition = config_dict.get(
            const.ConfigParams.DONE_CONDITION.value, any)
        self._number_of_resets = config_dict[
            const.ConfigParams.NUMBER_OF_RESETS.value]
        self._off_track_penalty = config_dict[
            const.ConfigParams.OFF_TRACK_PENALTY.value]
        self._collision_penalty = config_dict[
            const.ConfigParams.COLLISION_PENALTY.value]
        self._pause_duration = 0.0
        self._reset_count = 0
        self._curr_crashed_object_name = ''
        # simapp_version speed scale
        self._speed_scale_factor_ = get_speed_factor(
            config_dict[const.ConfigParams.VERSION.value])
        # Store the name of the agent used to set agents position on the track
        self._agent_name_ = config_dict[const.ConfigParams.AGENT_NAME.value]
        # Set start lane. This only support for two agents H2H race
        self._agent_idx_ = get_racecar_idx(self._agent_name_)
        # Get track data
        self._track_data_ = TrackData.get_instance()
        if self._agent_idx_ is not None:
            self._start_lane_ = self._track_data_.inner_lane \
                if self._agent_idx_ % 2 else self._track_data_.outer_lane
        else:
            self._start_lane_ = self._track_data_.center_line
        # Store the name of the links in the agent, this should be const
        self._agent_link_name_list_ = config_dict[
            const.ConfigParams.LINK_NAME_LIST.value]
        # Store the reward function
        self._reward_ = config_dict[const.ConfigParams.REWARD.value]
        # Create publishers for controlling the car
        self._velocity_pub_dict_ = OrderedDict()
        self._steering_pub_dict_ = OrderedDict()
        for topic in config_dict[const.ConfigParams.VELOCITY_LIST.value]:
            self._velocity_pub_dict_[topic] = rospy.Publisher(topic,
                                                              Float64,
                                                              queue_size=1)
        for topic in config_dict[const.ConfigParams.STEERING_LIST.value]:
            self._steering_pub_dict_[topic] = rospy.Publisher(topic,
                                                              Float64,
                                                              queue_size=1)
        #Create default reward parameters
        self._reward_params_ = const.RewardParam.make_default_param()
        #Create the default metrics dictionary
        self._step_metrics_ = StepMetrics.make_default_metric()
        # Dictionary of bools indicating starting position behavior
        self._start_pos_behavior_ = \
            {'change_start' : config_dict[const.ConfigParams.CHANGE_START.value],
             'alternate_dir' : config_dict[const.ConfigParams.ALT_DIR.value]}
        # Dictionary to track the previous way points
        self._prev_waypoints_ = {
            'prev_point': Point(0, 0),
            'prev_point_2': Point(0, 0)
        }

        # Normalized distance of new start line from the original start line of the track.
        start_ndist = 0.0

        # Normalized start position offset w.r.t to start_ndist, which is the start line of the track.
        start_pos_offset = config_dict.get(
            const.ConfigParams.START_POSITION.value, 0.0)
        self._start_line_ndist_offset = start_pos_offset / self._track_data_.get_track_length(
        )

        # Dictionary containing some of the data for the agent
        # - During the reset call, every value except start_ndist will get wiped out by self._clear_data
        #   (reset happens prior to every episodes begin)
        # - If self._start_line_ndist_offset is not 0 (usually some minus value),
        #   then initial current_progress suppose to be non-zero (usually some minus value) as progress
        #   suppose to be based on start_ndist.
        # - This will be correctly calculated by first call of utils.compute_current_prog function.
        #   As prev_progress will be initially 0.0 and physical position is not at start_ndist,
        #   utils.compute_current_prog will return negative progress if self._start_line_ndist_offset is negative value
        #   (meaning behind start line) and will return positive progress if self._start_line_ndist_offset is
        #   positive value (meaning ahead of start line).
        self._data_dict_ = {
            'max_progress': 0.0,
            'current_progress': 0.0,
            'prev_progress': 0.0,
            'steps': 0.0,
            'start_ndist': start_ndist,
            'prev_car_pose': 0.0
        }

        #Load the action space
        self._action_space_, self._json_actions_ = \
            load_action_space(config_dict[const.ConfigParams.ACTION_SPACE_PATH.value])
        #! TODO evaluate if this is the best way to reset the car
        # Adding the reward data publisher
        self.reward_data_pub = RewardDataPublisher(self._agent_name_,
                                                   self._json_actions_)
        # subscriber to time to update camera position
        self.camera_manager = CameraManager.get_instance()
        # True if the agent is in the training phase
        self._is_training_ = False
        # Register to the phase sink
        run_phase_sink.register(self)
        # Make sure velocity and angle are set to 0
        send_action(self._velocity_pub_dict_, self._steering_pub_dict_, 0.0,
                    0.0)

        # start_dist should be hypothetical start line (start_ndist) plus
        # start position offset (start_line_ndist_offset).
        start_pose = self._start_lane_.interpolate_pose(
            (self._data_dict_['start_ndist'] + self._start_line_ndist_offset) *
            self._track_data_.get_track_length(),
            finite_difference=FiniteDifference.FORWARD_DIFFERENCE)
        self._track_data_.initialize_object(self._agent_name_, start_pose, \
                                            ObstacleDimensions.BOT_CAR_DIMENSION)

        self.make_link_points = lambda link_state: Point(
            link_state.pose.position.x, link_state.pose.position.y)
        self.reference_frames = ['' for _ in self._agent_link_name_list_]

        self._pause_car_model_pose = None
        self._park_position = DEFAULT_PARK_POSITION
        AbstractTracker.__init__(self, TrackerPriority.HIGH)
 def __init__(self, agent_name):
     super(CrashResetRule, self).__init__(CrashResetRule.name)
     self._track_data = TrackData.get_instance()
     self._agent_name = agent_name
 def __init__(self,
              agent_name,
              s3_dict_metrics,
              deepracer_checkpoint_json,
              ckpnt_dir,
              run_phase_sink,
              use_model_picker=True):
     '''s3_dict_metrics - Dictionary containing the required s3 info for the metrics
                          bucket with keys specified by MetricsS3Keys
        deepracer_checkpoint_json - DeepracerCheckpointJson instance
        ckpnt_dir - Directory where the current checkpont is to be stored
        run_phase_sink - Sink to recieve notification of a change in run phase
        use_model_picker - Flag to whether to use model picker or not.
     '''
     self._agent_name_ = agent_name
     self._deepracer_checkpoint_json = deepracer_checkpoint_json
     self._s3_metrics = Metrics(
         bucket=s3_dict_metrics[MetricsS3Keys.METRICS_BUCKET.value],
         s3_key=s3_dict_metrics[MetricsS3Keys.METRICS_KEY.value],
         region_name=s3_dict_metrics[MetricsS3Keys.REGION.value],
         s3_endpoint_url=s3_dict_metrics[MetricsS3Keys.ENDPOINT_URL.value])
     self._start_time_ = time.time()
     self._episode_ = 0
     self._episode_reward_ = 0.0
     self._progress_ = 0.0
     self._episode_status = ''
     self._metrics_ = list()
     self._is_eval_ = True
     self._eval_trials_ = 0
     self._checkpoint_state_ = CheckpointStateFile(ckpnt_dir)
     self._use_model_picker = use_model_picker
     self._eval_stats_dict_ = {'chkpnt_name': None, 'avg_eval_metric': None}
     self._best_chkpnt_stats = {
         'name': None,
         'avg_eval_metric': None,
         'time_stamp': time.time()
     }
     self._current_eval_best_model_metric_list_ = list()
     self.is_save_simtrace_enabled = rospy.get_param(
         'SIMTRACE_S3_BUCKET', None)
     self._best_model_metric_type = BestModelMetricType(
         rospy.get_param('BEST_MODEL_METRIC',
                         BestModelMetricType.PROGRESS.value).lower())
     self.track_data = TrackData.get_instance()
     run_phase_sink.register(self)
     # Create the agent specific directories needed for storing the metric files
     self._simtrace_local_path = SIMTRACE_TRAINING_LOCAL_PATH_FORMAT.format(
         self._agent_name_)
     simtrace_dirname = os.path.dirname(self._simtrace_local_path)
     # addressing mkdir and check directory race condition:
     # https://stackoverflow.com/questions/12468022/python-fileexists-error-when-making-directory/30174982#30174982
     # TODO: change this to os.makedirs(simtrace_dirname, exist_ok=True) when we migrate off python 2.7
     try:
         os.makedirs(simtrace_dirname)
     except OSError as e:
         if e.errno != errno.EEXIST:
             raise
         LOGGER.error("File already exist %s", simtrace_dirname)
     self._current_sim_time = 0
     rospy.Service("/{}/{}".format(self._agent_name_, "mp4_video_metrics"),
                   VideoMetricsSrv, self._handle_get_video_metrics)
     self._video_metrics = Mp4VideoMetrics.get_empty_dict()
     AbstractTracker.__init__(self, TrackerPriority.HIGH)
Exemple #30
0
    def __init__(self, config_dict, run_phase_sink, metrics):
        '''agent_name - String containing the name of the agent
           config_dict - Dictionary containing all the keys in ConfigParams
           run_phase_sink - Sink to recieve notification of a change in run phase
        '''
        # reset rules manager
        self._metrics = metrics
        self._is_continuous = config_dict[
            const.ConfigParams.IS_CONTINUOUS.value]
        self._is_reset = False
        self._reset_rules_manager = construct_reset_rules_manager(config_dict)
        self._ctrl_status = dict()
        self._ctrl_status[
            AgentCtrlStatus.AGENT_PHASE.value] = AgentPhase.RUN.value
        self._config_dict = config_dict
        self._done_condition = config_dict.get(
            const.ConfigParams.DONE_CONDITION.value, any)
        self._number_of_resets = config_dict[
            const.ConfigParams.NUMBER_OF_RESETS.value]
        self._off_track_penalty = config_dict[
            const.ConfigParams.OFF_TRACK_PENALTY.value]
        self._collision_penalty = config_dict[
            const.ConfigParams.COLLISION_PENALTY.value]
        self._pause_duration = 0.0
        self._reset_count = 0
        self._curr_crashed_object_name = None
        self._last_crashed_object_name = None
        # simapp_version speed scale
        self._speed_scale_factor_ = get_speed_factor(
            config_dict[const.ConfigParams.VERSION.value])
        # Store the name of the agent used to set agents position on the track
        self._agent_name_ = config_dict[const.ConfigParams.AGENT_NAME.value]
        # Set start lane. This only support for two agents H2H race
        self._agent_idx_ = get_racecar_idx(self._agent_name_)
        # Get track data
        self._track_data_ = TrackData.get_instance()
        if self._agent_idx_ is not None:
            self._start_lane_ = self._track_data_.inner_lane \
                if self._agent_idx_ % 2 else self._track_data_.outer_lane
        else:
            self._start_lane_ = self._track_data_.center_line
        # Store the name of the links in the agent, this should be const
        self._agent_link_name_list_ = config_dict[
            const.ConfigParams.LINK_NAME_LIST.value]
        # Store the reward function
        self._reward_ = config_dict[const.ConfigParams.REWARD.value]
        # Create publishers for controlling the car
        self._velocity_pub_dict_ = OrderedDict()
        self._steering_pub_dict_ = OrderedDict()
        for topic in config_dict[const.ConfigParams.VELOCITY_LIST.value]:
            self._velocity_pub_dict_[topic] = rospy.Publisher(topic,
                                                              Float64,
                                                              queue_size=1)
        for topic in config_dict[const.ConfigParams.STEERING_LIST.value]:
            self._steering_pub_dict_[topic] = rospy.Publisher(topic,
                                                              Float64,
                                                              queue_size=1)
        #Create default reward parameters
        self._reward_params_ = const.RewardParam.make_default_param()
        #Creat the default metrics dictionary
        self._step_metrics_ = StepMetrics.make_default_metric()
        # Dictionary of bools indicating starting position behavior
        self._start_pos_behavior_ = \
            {'change_start' : config_dict[const.ConfigParams.CHANGE_START.value],
             'alternate_dir' : config_dict[const.ConfigParams.ALT_DIR.value]}
        # Dictionary to track the previous way points
        self._prev_waypoints_ = {
            'prev_point': Point(0, 0),
            'prev_point_2': Point(0, 0)
        }
        # Normalize start position in meter to normalized start_ndist in percentage
        start_ndist = config_dict.get(const.ConfigParams.START_POSITION.value, 0.0) / \
            self._track_data_.get_track_length()
        # Dictionary containing some of the data for the agent
        self._data_dict_ = {
            'max_progress': 0.0,
            'current_progress': 0.0,
            'prev_progress': 0.0,
            'steps': 0.0,
            'start_ndist': start_ndist
        }
        #Load the action space
        self._action_space_, self._json_actions_ = \
            load_action_space(config_dict[const.ConfigParams.ACTION_SPACE_PATH.value])
        #! TODO evaluate if this is the best way to reset the car
        # Adding the reward data publisher
        self.reward_data_pub = RewardDataPublisher(self._agent_name_,
                                                   self._json_actions_)
        # subscriber to time to update camera position
        self.camera_manager = CameraManager.get_instance()
        # True if the agent is in the training phase
        self._is_training_ = False
        # Register to the phase sink
        run_phase_sink.register(self)
        # Make sure velicty and angle are set to 0
        send_action(self._velocity_pub_dict_, self._steering_pub_dict_, 0.0,
                    0.0)
        start_pose = self._track_data_.center_line.interpolate_pose(
            self._data_dict_['start_ndist'] *
            self._track_data_.get_track_length(),
            finite_difference=FiniteDifference.FORWARD_DIFFERENCE)
        self._track_data_.initialize_object(self._agent_name_, start_pose, \
                                            ObstacleDimensions.BOT_CAR_DIMENSION)

        self.make_link_points = lambda link_state: Point(
            link_state.pose.position.x, link_state.pose.position.y)
        self.reference_frames = ['' for _ in self._agent_link_name_list_]

        self._pause_car_model_pose = None
        self._park_position = DEFAULT_PARK_POSITION
        AbstractTracker.__init__(self, TrackerPriority.HIGH)