Add env_id to log messages while uploading

This commit is contained in:
Greg Brockman
2016-04-28 07:37:32 -07:00
parent a5d71bb554
commit 98461ff8d8
2 changed files with 30 additions and 25 deletions

View File

@@ -18,6 +18,10 @@ if __name__ == '__main__':
logger = logging.getLogger()
logger.setLevel(logging.INFO)
outdir = '/tmp/random-agent-results'
gym.upload(outdir, algorithm_id='random')
raise"hi"
env = gym.make('CartPole-v0')
agent = RandomAgent(env.action_space)

View File

@@ -31,6 +31,7 @@ def upload(training_dir, algorithm_id=None, writeup=None, api_key=None):
raise error.Error("Still have an open monitor on {}. You must run 'env.monitor.close()' before uploading.".format(', '.join(envs)))
env_info, training_episode_batch, training_video = upload_training_data(training_dir, api_key=api_key)
env_id = env_info['env_id']
training_episode_batch_id = training_video_id = None
if training_episode_batch:
training_episode_batch_id = training_episode_batch.id
@@ -38,15 +39,14 @@ def upload(training_dir, algorithm_id=None, writeup=None, api_key=None):
training_video_id = training_video.id
if logger.level <= logging.INFO:
message = ['Creating evaluation object on the server']
if training_episode_batch_id is not None and training_video_id is not None:
logger.info('Creating evaluation object from %s with learning curve and training video', training_dir)
logger.info('[%s] Creating evaluation object from %s with learning curve and training video', env_id, training_dir)
elif training_episode_batch_id is not None:
logger.info('Creating evaluation object from %s with learning curve', training_dir)
logger.info('[%s] Creating evaluation object from %s with learning curve', env_id, training_dir)
elif training_video_id is not None:
logger.info('Creating evaluation object from %s with training video', training_dir)
logger.info('[%s] Creating evaluation object from %s with training video', env_id, training_dir)
else:
raise error.Error("You didn't have any recorded training data in {}. Once you've used 'env.monitor.start(training_dir)' to start recording, you need to actually run some rollouts. Please join the community chat on https://gym.openai.com if you have any issues.".format(training_dir))
raise error.Error("[%s] You didn't have any recorded training data in {}. Once you've used 'env.monitor.start(training_dir)' to start recording, you need to actually run some rollouts. Please join the community chat on https://gym.openai.com if you have any issues.".format(env_id, training_dir))
evaluation = resource.Evaluation.create(
training_episode_batch=training_episode_batch_id,
@@ -64,13 +64,13 @@ def upload(training_dir, algorithm_id=None, writeup=None, api_key=None):
"""
****************************************************
You successfully uploaded your agent evaluation to
You successfully uploaded your evaluation on %s to
OpenAI Gym! You can find it at:
%s
****************************************************
""".rstrip(), evaluation.web_url())
""".rstrip(), env_id, evaluation.web_url())
return evaluation
@@ -89,28 +89,29 @@ def upload_training_data(training_dir, api_key=None):
episode_rewards = results['episode_rewards']
videos = results['videos']
logger.debug('Uploading data from manifest %s', ', '.join(manifests))
env_id = env_info['env_id']
logger.debug('[%s] Uploading data from manifest %s', env_id, ', '.join(manifests))
# Do the relevant uploads
if len(episode_lengths) > 0:
training_episode_batch = upload_training_episode_batch(episode_lengths, episode_rewards, timestamps, api_key)
training_episode_batch = upload_training_episode_batch(episode_lengths, episode_rewards, timestamps, api_key, env_id=env_id)
else:
training_episode_batch = None
if len(videos) > MAX_VIDEOS:
logger.warn('You recorded videos for {} episodes, but the scoreboard only supports up to {}. We will automatically subsample for you, but you also might wish to adjust your video recording rate.'.format(len(videos), MAX_VIDEOS))
logger.warn('[%s] You recorded videos for %s episodes, but the scoreboard only supports up to %s. We will automatically subsample for you, but you also might wish to adjust your video recording rate.', env_id, len(videos), MAX_VIDEOS)
skip = len(videos) / (MAX_VIDEOS - 1)
videos = videos[::skip]
if len(videos) > 0:
training_video = upload_training_video(videos, api_key)
training_video = upload_training_video(videos, api_key, env_id=env_id)
else:
training_video = None
return env_info, training_episode_batch, training_video
def upload_training_episode_batch(episode_lengths, episode_rewards, timestamps, api_key=None):
logger.info('Uploading %d episodes of training data', len(episode_lengths))
def upload_training_episode_batch(episode_lengths, episode_rewards, timestamps, api_key=None, env_id=None):
logger.info('[%s] Uploading %d episodes of training data', env_id, len(episode_lengths))
file_upload = resource.FileUpload.create(purpose='episode_batch', api_key=api_key)
file_upload.put({
'episode_lengths': episode_lengths,
@@ -119,23 +120,23 @@ def upload_training_episode_batch(episode_lengths, episode_rewards, timestamps,
})
return file_upload
def upload_training_video(videos, api_key=None):
def upload_training_video(videos, api_key=None, env_id=None):
"""videos: should be list of (video_path, metadata_path) tuples"""
with tempfile.TemporaryFile() as archive_file:
write_archive(videos, archive_file)
write_archive(videos, archive_file, env_id=env_id)
archive_file.seek(0)
logger.info('Uploading videos of %d training episodes (%d bytes)', len(videos), util.file_size(archive_file))
logger.info('[%s] Uploading videos of %d training episodes (%d bytes)', env_id, len(videos), util.file_size(archive_file))
file_upload = resource.FileUpload.create(purpose='video', content_type='application/vnd.openai.video+x-compressed', api_key=api_key)
file_upload.put(archive_file, encode=None)
return file_upload
def write_archive(videos, archive_file):
def write_archive(videos, archive_file, env_id=None):
if len(videos) > MAX_VIDEOS:
raise error.Error('Trying to upload {} videos, but there is a limit of {} currently. If you actually want to upload this many videos, please email gym@openai.com with your use-case.'.format(MAX_VIDEOS, len(videos)))
raise error.Error('[{}] Trying to upload {} videos, but there is a limit of {} currently. If you actually want to upload this many videos, please email gym@openai.com with your use-case.'.format(env_id, MAX_VIDEOS, len(videos)))
logger.debug('Preparing an archive of %d videos: %s', len(videos), videos)
logger.debug('[%s] Preparing an archive of %d videos: %s', env_id, len(videos), videos)
# Double check that there are no collisions
basenames = set()
@@ -150,19 +151,19 @@ def write_archive(videos, archive_file):
metadata_name = os.path.basename(metadata_path)
if not os.path.exists(video_path):
raise error.Error('No such video file {}. (HINT: Your video recorder may have broken midway through the run. You can check this with `video_recorder.functional`.)'.format(video_path))
raise error.Error('[{}] No such video file {}. (HINT: Your video recorder may have broken midway through the run. You can check this with `video_recorder.functional`.)'.format(env_id, video_path))
elif not os.path.exists(metadata_path):
raise error.Error('No such metadata file {}. (HINT: this should be automatically created when using a VideoRecorder instance.)'.format(video_path))
raise error.Error('[{}] No such metadata file {}. (HINT: this should be automatically created when using a VideoRecorder instance.)'.format(env_id, video_path))
# Do some sanity checking
if video_name in basenames:
raise error.Error('Duplicated video name {} in video list: {}'.format(video_name, videos))
raise error.Error('[{}] Duplicated video name {} in video list: {}'.format(env_id, video_name, videos))
elif metadata_name in basenames:
raise error.Error('Duplicated metadata file name {} in video list: {}'.format(metadata_name, videos))
raise error.Error('[{}] Duplicated metadata file name {} in video list: {}'.format(env_id, metadata_name, videos))
elif not video_name_re.search(video_name):
raise error.Error('Invalid video name {} (must match {})'.format(video_name, video_name_re.pattern))
raise error.Error('[{}] Invalid video name {} (must match {})'.format(env_id, video_name, video_name_re.pattern))
elif not metadata_name_re.search(metadata_name):
raise error.Error('Invalid metadata file name {} (must match {})'.format(metadata_name, metadata_name_re.pattern))
raise error.Error('[{}] Invalid metadata file name {} (must match {})'.format(env_id, metadata_name, metadata_name_re.pattern))
# Record that we've seen these names; add to manifest
basenames.add(video_name)