浏览代码

Fix how we set logging levels (#3703)

* cleanup logging

* comments and cleanup

* pylint, gym
/develop/add-fire
GitHub 5 年前
当前提交
4ecd6ad3
共有 24 个文件被更改,包括 108 次插入59 次删除
  1. 2
      .pylintrc
  2. 7
      gym-unity/gym_unity/envs/__init__.py
  3. 5
      ml-agents-envs/mlagents_envs/environment.py
  4. 4
      ml-agents-envs/mlagents_envs/side_channel/outgoing_message.py
  5. 4
      ml-agents-envs/mlagents_envs/side_channel/side_channel.py
  6. 5
      ml-agents/mlagents/model_serialization.py
  7. 5
      ml-agents/mlagents/trainers/components/reward_signals/__init__.py
  8. 4
      ml-agents/mlagents/trainers/curriculum.py
  9. 5
      ml-agents/mlagents/trainers/env_manager.py
  10. 5
      ml-agents/mlagents/trainers/ghost/trainer.py
  11. 17
      ml-agents/mlagents/trainers/learn.py
  12. 4
      ml-agents/mlagents/trainers/meta_curriculum.py
  13. 4
      ml-agents/mlagents/trainers/policy/tf_policy.py
  14. 4
      ml-agents/mlagents/trainers/ppo/trainer.py
  15. 4
      ml-agents/mlagents/trainers/sac/optimizer.py
  16. 5
      ml-agents/mlagents/trainers/sac/trainer.py
  17. 7
      ml-agents/mlagents/trainers/stats.py
  18. 5
      ml-agents/mlagents/trainers/subprocess_env_manager.py
  19. 5
      ml-agents/mlagents/trainers/trainer/trainer.py
  20. 4
      ml-agents/mlagents/trainers/trainer_controller.py
  21. 5
      ml-agents/mlagents/trainers/trainer_util.py
  22. 1
      setup.cfg
  23. 46
      ml-agents-envs/mlagents_envs/logging_util.py
  24. 10
      ml-agents/mlagents/logging_util.py

2
.pylintrc


# Appears to be https://github.com/PyCQA/pylint/issues/2981
W0201,
# Using the global statement
W0603,

7
gym-unity/gym_unity/envs/__init__.py


import logging
import itertools
import numpy as np
from typing import Any, Dict, List, Optional, Tuple, Union

from mlagents_envs.environment import UnityEnvironment
from mlagents_envs.base_env import BatchedStepResult
from mlagents_envs import logging_util
class UnityGymException(error.Error):

pass
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("gym_unity")
logger = logging_util.get_logger(__name__)
logging_util.set_log_level(logging_util.INFO)
GymSingleStepResult = Tuple[np.ndarray, float, bool, Dict]
GymMultiStepResult = Tuple[List[np.ndarray], List[float], List[bool], Dict]

5
ml-agents-envs/mlagents_envs/environment.py


import atexit
import glob
import uuid
import logging
import numpy as np
import os
import subprocess

from mlagents_envs.logging_util import get_logger
from mlagents_envs.side_channel.side_channel import SideChannel, IncomingMessage
from mlagents_envs.base_env import (

import struct
logger = logging.getLogger("mlagents_envs")
logger = get_logger(__name__)
class UnityEnvironment(BaseEnv):

4
ml-agents-envs/mlagents_envs/side_channel/outgoing_message.py


from typing import List
import struct
import logging
from mlagents_envs.logging_util import get_logger
logger = logging.getLogger(__name__)
logger = get_logger(__name__)
class OutgoingMessage:

4
ml-agents-envs/mlagents_envs/side_channel/side_channel.py


from abc import ABC, abstractmethod
from typing import List
import uuid
import logging
from mlagents_envs.logging_util import get_logger
logger = logging.getLogger(__name__)
logger = get_logger(__name__)
class SideChannel(ABC):

5
ml-agents/mlagents/model_serialization.py


from distutils.util import strtobool
import os
import logging
from typing import Any, List, Set, NamedTuple
from distutils.version import LooseVersion

from tensorflow.python.platform import gfile
from tensorflow.python.framework import graph_util
from mlagents_envs.logging_util import get_logger
from mlagents.trainers import tensorflow_to_barracuda as tf2bc
if LooseVersion(tf.__version__) < LooseVersion("1.12.0"):

logger = get_logger(__name__)
logger = logging.getLogger("mlagents.trainers")
POSSIBLE_INPUT_NODES = frozenset(
[

5
ml-agents/mlagents/trainers/components/reward_signals/__init__.py


import logging
from typing import Any, Dict, List
from collections import namedtuple
import numpy as np

from mlagents_envs.logging_util import get_logger
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
RewardSignalResult = namedtuple(
"RewardSignalResult", ["scaled_reward", "unscaled_reward"]

4
ml-agents/mlagents/trainers/curriculum.py


from .exception import CurriculumConfigError, CurriculumLoadingError
import logging
from mlagents_envs.logging_util import get_logger
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class Curriculum:

5
ml-agents/mlagents/trainers/env_manager.py


from abc import ABC, abstractmethod
import logging
from typing import List, Dict, NamedTuple, Iterable, Tuple
from mlagents_envs.base_env import BatchedStepResult, AgentGroupSpec, AgentGroup
from mlagents_envs.side_channel.stats_side_channel import StatsAggregationMethod

from mlagents.trainers.action_info import ActionInfo
from mlagents_envs.logging_util import get_logger
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class EnvironmentStep(NamedTuple):

5
ml-agents/mlagents/trainers/ghost/trainer.py


from typing import Deque, Dict, List, Any, cast
import numpy as np
import logging
from mlagents_envs.logging_util import get_logger
from mlagents.trainers.brain import BrainParameters
from mlagents.trainers.policy import Policy
from mlagents.trainers.policy.tf_policy import TFPolicy

from mlagents.trainers.stats import StatsPropertyType
from mlagents.trainers.behavior_id_utils import BehaviorIdentifiers
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class GhostTrainer(Trainer):

17
ml-agents/mlagents/trainers/learn.py


# # Unity ML-Agents Toolkit
import logging
import argparse
import os

from mlagents_envs.side_channel.engine_configuration_channel import EngineConfig
from mlagents_envs.exception import UnityEnvironmentException
from mlagents_envs.timers import hierarchical_timer, get_timer_tree
from mlagents.logging_util import create_logger
from mlagents_envs import logging_util
logger = logging_util.get_logger(__name__)
def _create_parser():

with open(timing_path, "w") as f:
json.dump(get_timer_tree(), f, indent=4)
except FileNotFoundError:
logging.warning(
logger.warning(
f"Unable to save to {timing_path}. Make sure the directory exists"
)

print(get_version_string())
if options.debug:
log_level = logging.DEBUG
log_level = logging_util.DEBUG
log_level = logging.INFO
log_level = logging_util.INFO
trainer_logger = create_logger("mlagents.trainers", log_level)
logging_util.set_log_level(log_level)
trainer_logger.debug("Configuration for this run:")
trainer_logger.debug(json.dumps(options._asdict(), indent=4))
logger.debug("Configuration for this run:")
logger.debug(json.dumps(options._asdict(), indent=4))
run_seed = options.seed
if options.cpu:

4
ml-agents/mlagents/trainers/meta_curriculum.py


from typing import Dict, Set
from mlagents.trainers.curriculum import Curriculum
import logging
from mlagents_envs.logging_util import get_logger
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class MetaCurriculum:

4
ml-agents/mlagents/trainers/policy/tf_policy.py


import logging
from typing import Any, Dict, List, Optional
import abc
import numpy as np

from mlagents_envs.logging_util import get_logger
from mlagents.trainers.policy import Policy
from mlagents.trainers.action_info import ActionInfo
from mlagents.trainers.trajectory import SplitObservations

logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class UnityPolicyException(UnityException):

4
ml-agents/mlagents/trainers/ppo/trainer.py


# ## ML-Agent Learning (PPO)
# Contains an implementation of PPO as described in: https://arxiv.org/abs/1707.06347
import logging
from mlagents_envs.logging_util import get_logger
from mlagents.trainers.policy.nn_policy import NNPolicy
from mlagents.trainers.trainer.rl_trainer import RLTrainer
from mlagents.trainers.brain import BrainParameters

from mlagents.trainers.exception import UnityTrainerException
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class PPOTrainer(RLTrainer):

4
ml-agents/mlagents/trainers/sac/optimizer.py


import logging
from mlagents_envs.logging_util import get_logger
from mlagents.trainers.sac.network import SACPolicyNetwork, SACTargetNetwork
from mlagents.trainers.models import LearningRateSchedule, EncoderType, ModelUtils
from mlagents.trainers.optimizer.tf_optimizer import TFOptimizer

EPSILON = 1e-6 # Small value to avoid divide by zero
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
POLICY_SCOPE = ""
TARGET_SCOPE = "target_network"

5
ml-agents/mlagents/trainers/sac/trainer.py


# Contains an implementation of SAC as described in https://arxiv.org/abs/1801.01290
# and implemented in https://github.com/hill-a/stable-baselines
import logging
from collections import defaultdict
from typing import Dict
import os

from mlagents_envs.logging_util import get_logger
from mlagents_envs.timers import timed
from mlagents.trainers.policy.tf_policy import TFPolicy
from mlagents.trainers.policy.nn_policy import NNPolicy

from mlagents.trainers.exception import UnityTrainerException
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
BUFFER_TRUNCATE_PERCENT = 0.8

7
ml-agents/mlagents/trainers/stats.py


import csv
import os
import time
import logging
from mlagents_envs.logging_util import get_logger
from mlagents_envs.timers import set_gauge
from mlagents_envs.timers import set_gauge
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class StatsSummary(NamedTuple):

5
ml-agents/mlagents/trainers/subprocess_env_manager.py


import logging
from typing import Dict, NamedTuple, List, Any, Optional, Callable, Set, Tuple
import cloudpickle
import enum

from multiprocessing.connection import Connection
from queue import Empty as EmptyQueueException
from mlagents_envs.base_env import BaseEnv, AgentGroup
from mlagents_envs.logging_util import get_logger
from mlagents.trainers.env_manager import EnvManager, EnvironmentStep, AllStepResult
from mlagents_envs.timers import (
TimerNode,

from mlagents_envs.side_channel.side_channel import SideChannel
from mlagents.trainers.brain_conversion_utils import group_spec_to_brain_parameters
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class EnvironmentCommand(enum.Enum):

5
ml-agents/mlagents/trainers/trainer/trainer.py


# # Unity ML-Agents Toolkit
import logging
from mlagents_envs.logging_util import get_logger
from mlagents.model_serialization import export_policy_model, SerializationSettings
from mlagents.trainers.policy.tf_policy import TFPolicy
from mlagents.trainers.stats import StatsReporter

from mlagents.trainers.policy import Policy
from mlagents.trainers.exception import UnityTrainerException
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class Trainer(abc.ABC):

4
ml-agents/mlagents/trainers/trainer_controller.py


import os
import sys
import logging
from typing import Dict, Optional, Set
from collections import defaultdict

from mlagents_envs.logging_util import get_logger
from mlagents.trainers.env_manager import EnvManager
from mlagents_envs.exception import (
UnityEnvironmentException,

self.trainer_factory = trainer_factory
self.model_path = model_path
self.summaries_dir = summaries_dir
self.logger = logging.getLogger("mlagents.trainers")
self.logger = get_logger(__name__)
self.run_id = run_id
self.save_freq = save_freq
self.train_model = train

5
ml-agents/mlagents/trainers/trainer_util.py


import os
import yaml
from typing import Any, Dict, TextIO
import logging
from mlagents_envs.logging_util import get_logger
from mlagents.trainers.meta_curriculum import MetaCurriculum
from mlagents.trainers.exception import TrainerConfigError
from mlagents.trainers.trainer import Trainer

from mlagents.trainers.ghost.trainer import GhostTrainer
logger = logging.getLogger("mlagents.trainers")
logger = get_logger(__name__)
class TrainerFactory:

1
setup.cfg


I200,
banned-modules = tensorflow = use mlagents.tf_utils instead (it handles tf2 compat).
logging = use mlagents_envs.logging_util instead

46
ml-agents-envs/mlagents_envs/logging_util.py


import logging # noqa I251
CRITICAL = logging.CRITICAL
FATAL = logging.FATAL
ERROR = logging.ERROR
WARNING = logging.WARNING
INFO = logging.INFO
DEBUG = logging.DEBUG
NOTSET = logging.NOTSET
_loggers = set()
_log_level = NOTSET
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
LOG_FORMAT = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
def get_logger(name: str) -> logging.Logger:
"""
Create a logger with the specified name. The logger will use the log level
specified by set_log_level()
"""
logger = logging.getLogger(name=name)
# If we've already set the log level, make sure new loggers use it
if _log_level != NOTSET:
logger.setLevel(_log_level)
# Keep track of this logger so that we can change the log level later
_loggers.add(logger)
return logger
def set_log_level(log_level: int) -> None:
"""
Set the ML-Agents logging level. This will also configure the logging format (if it hasn't already been set).
"""
global _log_level
_log_level = log_level
# Configure the log format.
# In theory, this would be sufficient, but if another library calls logging.basicConfig
# first, it doesn't have any effect.
logging.basicConfig(level=_log_level, format=LOG_FORMAT, datefmt=DATE_FORMAT)
for logger in _loggers:
logger.setLevel(log_level)

10
ml-agents/mlagents/logging_util.py


import logging
def create_logger(name, log_level):
date_format = "%Y-%m-%d %H:%M:%S"
log_format = "%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] %(message)s"
logging.basicConfig(level=log_level, format=log_format, datefmt=date_format)
logger = logging.getLogger(name=name)
return logger
正在加载...
取消
保存