Esempio n. 1
0
    def load_results(training_dir):
        if not os.path.exists(training_dir):
            logger.error('Training directory %s not found', training_dir)
            return

        manifests = detect_training_manifests(training_dir)
        if not manifests:
            logger.error('No manifests found in training directory %s',
                         training_dir)
            return

        logger.debug('Uploading data from manifest %s', ', '.join(manifests))

        # Load up stats + video files
        stats_files = []
        videos = []
        env_infos = []

        for manifest in manifests:
            with open(manifest) as f:
                contents = json.load(f)
                # Make these paths absolute again
                stats_files.append(
                    os.path.join(training_dir, contents['stats']))
                videos += [(os.path.join(training_dir,
                                         v), os.path.join(training_dir, m))
                           for v, m in contents['videos']]
                env_infos.append(contents['env_info'])

        env_info = collapse_env_infos(env_infos, training_dir)

        # If only one stats file is present, there is no need to merge and all fields are included
        if len(stats_files) == 1:
            with open(stats_files[0]) as f:
                content = json.load(f)
                content.update({
                    'manifests': manifests,
                    'env_info': env_info,
                    'videos': videos
                })
                return content
        else:
            data_sources, initial_reset_timestamps, timestamps, episode_lengths, episode_rewards, \
                episode_types, initial_reset_timestamp = merge_stats_files(stats_files)

            return {
                'manifests': manifests,
                'env_info': env_info,
                'data_sources': data_sources,
                'timestamps': timestamps,
                'episode_lengths': episode_lengths,
                'episode_rewards': episode_rewards,
                'episode_types': episode_types,
                'initial_reset_timestamps': initial_reset_timestamps,
                'initial_reset_timestamp': initial_reset_timestamp,
                'videos': videos,
            }
Esempio n. 2
0
    def load_results(training_dir):
        if not os.path.exists(training_dir):
            logger.error('Training directory %s not found', training_dir)
            return

        manifests = detect_training_manifests(training_dir)
        if not manifests:
            logger.error('No manifests found in training directory %s',
                         training_dir)
            return

        logger.debug('Uploading data from manifest %s', ', '.join(manifests))

        # Load up stats + video files
        stats_files = []
        videos = []
        env_infos = []

        for manifest in manifests:
            with open(manifest) as f:
                contents = json.load(f)
                # Make these paths absolute again
                stats_files.append(
                    os.path.join(training_dir, contents['stats']))
                videos += [(os.path.join(training_dir,
                                         v), os.path.join(training_dir, m))
                           for v, m in contents['videos']]
                env_infos.append(contents['env_info'])

        env_info = collapse_env_infos(env_infos, training_dir)

        # If several stats files are found, merge lists together and randomly pick single values
        all_contents = {}
        for file in stats_files:
            with open(file) as f:
                content = json.load(f)
                content.update({
                    'manifests': manifests,
                    'env_info': env_info,
                    'videos': videos
                })
                if not all_contents:
                    all_contents.update(content)
                else:
                    for key, value in content.items():
                        if isinstance(value, list):
                            all_contents[key].extend(value)
                        else:
                            all_contents[key] = value
        return all_contents
Esempio n. 3
0
    def _start(self, directory, collect_ep_data_every_callable=None, force=False, resume=False, write_upon_reset=False, uid=None, mode=None):
        """Start monitoring.
        
                Args:
                    directory (str): A per-training run directory where to record stats.
                    collect_ep_data_every_callable (Optional[function, False]): function that takes in the index of the episode and outputs a boolean, indicating whether we should record a data on this episode. The default (for collect_ep_data_callable is None) is to take perfect cubes, capped at 1000. False disables data recording.
                    force (bool): Clear out existing training data from this directory (by deleting every file prefixed with "openaigym.").
                    resume (bool): Retain the training data already in this directory, which will be merged with our new data
                    write_upon_reset (bool): Write the manifest file on each reset. (This is currently a JSON file, so writing it is somewhat expensive.)
                    uid (Optional[str]): A unique id used as part of the suffix for the file. By default, uses os.getpid().
                    mode (['evaluation', 'training']): Whether this is an evaluation or training episode.
        """

        if self.env.spec is None:
            logger.warn(
                "Trying to monitor an environment which has no 'spec' set. This usually means you did not create it via 'gym.make', and is recommended only for advanced users.")
            env_id = '(unknown)'
        else:
            env_id = self.env.spec.id

        if not os.path.exists(directory):
            logger.info('Creating monitor directory %s', directory)
            if six.PY3:
                os.makedirs(directory, exist_ok=True)
            else:
                os.makedirs(directory)

        if collect_ep_data_every_callable is None:
            collect_ep_data_every_callable = monitor.capped_cubic_video_schedule
        elif collect_ep_data_every_callable == False:
            collect_ep_data_every_callable = monitor.disable_videos
        elif not callable(collect_ep_data_every_callable):
            raise error.Error('You must provide a function, None, or False for collect_ep_data_every_callable'
                              ', not {}: {}'.format(type(collect_ep_data_every_callable), collect_ep_data_every_callable))
        self.video_callable = collect_ep_data_every_callable

        # Check on whether we need to clear anything
        if force:
            monitor.clear_monitor_files(directory)
        elif not resume:
            training_manifests = monitor.detect_training_manifests(directory)
            if len(training_manifests) > 0:
                raise error.Error('''Trying to write to monitor directory {} with existing monitor files: {}.

        You should use a unique directory for each training run, or use 'force=True' to automatically clear previous monitor files.'''.format(
                    directory, ', '.join(training_manifests[:5])))

        self._monitor_id = monitor.monitor_closer.register(self)

        self.enabled = True
        self.directory = os.path.abspath(directory)

        # We use the 'openai-gym' prefix to determine if a file is ours
        self.file_prefix = monitor.FILE_PREFIX
        self.file_infix = '{}.{}'.format(self._monitor_id, uid if uid else os.getpid())

        # Note: 'self.stats_recorder' is used by the parent class... we can not change is name
        self.stats_recorder = LearningStatRecorder(directory,
                                                   '{}.episode_batch.{}'.format(self.file_prefix,
                                                                                self.file_infix),
                                                   self.learning_monitor_config,
                                                   autoreset=self.env_semantics_autoreset,
                                                   env_id=env_id)

        if not os.path.exists(directory):
            os.mkdir(directory)
        self.write_upon_reset = write_upon_reset

        if mode is not None:
            self._set_mode(mode)