From 8e8955571f55fdb16f06cc65d99686d0215f3f5b Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Sat, 29 Feb 2020 20:35:05 -0800 Subject: [PATCH] workaround for Tensorflow 2.x breaking API: try: import tensorflow.compat.v1 as tf except Exception: import tensorflow as tf --- examples/pybullet/gym/pybullet_envs/agents/configs.py | 5 ++++- examples/pybullet/gym/pybullet_envs/agents/networks.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/ppo/algorithm.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/ppo/memory.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/ppo/normalize.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/ppo/utility.py | 5 ++++- .../gym/pybullet_envs/agents/tools/count_weights.py | 5 ++++- .../pybullet_envs/agents/tools/in_graph_batch_env.py | 5 ++++- .../gym/pybullet_envs/agents/tools/in_graph_env.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/tools/loop.py | 5 ++++- .../gym/pybullet_envs/agents/tools/mock_algorithm.py | 6 ++++-- .../gym/pybullet_envs/agents/tools/simulate.py | 5 ++++- .../gym/pybullet_envs/agents/tools/streaming_mean.py | 6 ++++-- .../gym/pybullet_envs/agents/tools/wrappers.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/train_ppo.py | 5 ++++- examples/pybullet/gym/pybullet_envs/agents/utility.py | 5 ++++- .../pybullet/gym/pybullet_envs/agents/visualize_ppo.py | 5 ++++- .../deep_mimic/learning/nets/fc_2layers_1024units.py | 6 +++++- .../gym/pybullet_envs/deep_mimic/learning/pg_agent.py | 6 +++++- .../gym/pybullet_envs/deep_mimic/learning/ppo_agent.py | 5 ++++- .../deep_mimic/learning/solvers/mpi_solver.py | 6 +++++- .../gym/pybullet_envs/deep_mimic/learning/tf_agent.py | 5 ++++- .../pybullet_envs/deep_mimic/learning/tf_normalizer.py | 5 ++++- .../gym/pybullet_envs/deep_mimic/learning/tf_util.py | 10 ++++++++-- 24 files changed, 103 insertions(+), 27 deletions(-) diff --git a/examples/pybullet/gym/pybullet_envs/agents/configs.py b/examples/pybullet/gym/pybullet_envs/agents/configs.py index 29efac60e..7e78486d1 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/configs.py +++ b/examples/pybullet/gym/pybullet_envs/agents/configs.py @@ -26,7 +26,10 @@ from pybullet_envs.bullet import minitaur_duck_gym_env from pybullet_envs.bullet import minitaur_env_randomizer import pybullet_envs.bullet.minitaur_gym_env as minitaur_gym_env import pybullet_envs -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf def default(): diff --git a/examples/pybullet/gym/pybullet_envs/agents/networks.py b/examples/pybullet/gym/pybullet_envs/agents/networks.py index 203a4a80c..4e9665c4b 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/networks.py +++ b/examples/pybullet/gym/pybullet_envs/agents/networks.py @@ -21,7 +21,10 @@ import collections import functools import operator -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf NetworkOutput = collections.namedtuple('NetworkOutput', 'policy, mean, logstd, value, state') diff --git a/examples/pybullet/gym/pybullet_envs/agents/ppo/algorithm.py b/examples/pybullet/gym/pybullet_envs/agents/ppo/algorithm.py index dd18a7c5c..22c47ae99 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/ppo/algorithm.py +++ b/examples/pybullet/gym/pybullet_envs/agents/ppo/algorithm.py @@ -23,7 +23,10 @@ from __future__ import print_function import functools -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from . import memory from . import normalize diff --git a/examples/pybullet/gym/pybullet_envs/agents/ppo/memory.py b/examples/pybullet/gym/pybullet_envs/agents/ppo/memory.py index a1de8cd0f..0f9b0ac28 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/ppo/memory.py +++ b/examples/pybullet/gym/pybullet_envs/agents/ppo/memory.py @@ -17,7 +17,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class EpisodeMemory(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/ppo/normalize.py b/examples/pybullet/gym/pybullet_envs/agents/ppo/normalize.py index 1b86cd083..13f46a2af 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/ppo/normalize.py +++ b/examples/pybullet/gym/pybullet_envs/agents/ppo/normalize.py @@ -17,7 +17,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class StreamingNormalize(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/ppo/utility.py b/examples/pybullet/gym/pybullet_envs/agents/ppo/utility.py index f0323fd21..5fb70ff73 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/ppo/utility.py +++ b/examples/pybullet/gym/pybullet_envs/agents/ppo/utility.py @@ -21,7 +21,10 @@ import collections import math import re -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from tensorflow.python.client import device_lib diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/count_weights.py b/examples/pybullet/gym/pybullet_envs/agents/tools/count_weights.py index 610ded427..8289399f2 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/count_weights.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/count_weights.py @@ -20,7 +20,10 @@ from __future__ import print_function import re import numpy as np -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf def count_weights(scope=None, exclude=None, graph=None): diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_batch_env.py b/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_batch_env.py index 7eadf3422..8c7b73163 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_batch_env.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_batch_env.py @@ -18,7 +18,10 @@ from __future__ import division from __future__ import print_function import gym -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class InGraphBatchEnv(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_env.py b/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_env.py index 7f78a9e32..e5083bf08 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_env.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/in_graph_env.py @@ -18,7 +18,10 @@ from __future__ import division from __future__ import print_function import gym -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class InGraphEnv(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/loop.py b/examples/pybullet/gym/pybullet_envs/agents/tools/loop.py index 3f845de53..3813ab83b 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/loop.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/loop.py @@ -20,7 +20,10 @@ from __future__ import print_function import collections import os -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from . import streaming_mean diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/mock_algorithm.py b/examples/pybullet/gym/pybullet_envs/agents/tools/mock_algorithm.py index 0faa172ae..72ae727c3 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/mock_algorithm.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/mock_algorithm.py @@ -16,8 +16,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function - -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class MockAlgorithm(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/simulate.py b/examples/pybullet/gym/pybullet_envs/agents/tools/simulate.py index ba59d32b6..e3befdd8a 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/simulate.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/simulate.py @@ -17,7 +17,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from . import streaming_mean diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/streaming_mean.py b/examples/pybullet/gym/pybullet_envs/agents/tools/streaming_mean.py index 8a27b484a..fc3d98cc0 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/streaming_mean.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/streaming_mean.py @@ -16,8 +16,10 @@ from __future__ import absolute_import from __future__ import division from __future__ import print_function - -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class StreamingMean(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/tools/wrappers.py b/examples/pybullet/gym/pybullet_envs/agents/tools/wrappers.py index 8684583fe..a8d184e61 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/tools/wrappers.py +++ b/examples/pybullet/gym/pybullet_envs/agents/tools/wrappers.py @@ -25,7 +25,10 @@ import traceback import gym import gym.spaces import numpy as np -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf class AutoReset(object): diff --git a/examples/pybullet/gym/pybullet_envs/agents/train_ppo.py b/examples/pybullet/gym/pybullet_envs/agents/train_ppo.py index 5a45e2a4c..3d7c65065 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/train_ppo.py +++ b/examples/pybullet/gym/pybullet_envs/agents/train_ppo.py @@ -26,7 +26,10 @@ import datetime import os import gym -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from . import tools from . import configs diff --git a/examples/pybullet/gym/pybullet_envs/agents/utility.py b/examples/pybullet/gym/pybullet_envs/agents/utility.py index d2a4ee385..1312adda4 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/utility.py +++ b/examples/pybullet/gym/pybullet_envs/agents/utility.py @@ -22,7 +22,10 @@ import os import re import ruamel.yaml as yaml -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from . import tools diff --git a/examples/pybullet/gym/pybullet_envs/agents/visualize_ppo.py b/examples/pybullet/gym/pybullet_envs/agents/visualize_ppo.py index 2178b8c01..156a645e6 100644 --- a/examples/pybullet/gym/pybullet_envs/agents/visualize_ppo.py +++ b/examples/pybullet/gym/pybullet_envs/agents/visualize_ppo.py @@ -27,7 +27,10 @@ import os import gym from gym import wrappers -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from . import tools from . import utility diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/nets/fc_2layers_1024units.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/nets/fc_2layers_1024units.py index 59df884d5..7c98b0bf8 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/nets/fc_2layers_1024units.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/nets/fc_2layers_1024units.py @@ -1,4 +1,8 @@ -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf + import pybullet_envs.deep_mimic.learning.tf_util as TFUtil NAME = "fc_2layers_1024units" diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/pg_agent.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/pg_agent.py index a9c623678..a8c743cf8 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/pg_agent.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/pg_agent.py @@ -1,5 +1,9 @@ import numpy as np -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf + import copy from pybullet_envs.deep_mimic.learning.tf_agent import TFAgent diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/ppo_agent.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/ppo_agent.py index 15c93224e..129b6c451 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/ppo_agent.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/ppo_agent.py @@ -1,6 +1,9 @@ import numpy as np import copy as copy -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from pybullet_envs.deep_mimic.learning.pg_agent import PGAgent from pybullet_envs.deep_mimic.learning.solvers.mpi_solver import MPISolver diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/solvers/mpi_solver.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/solvers/mpi_solver.py index c3022f92d..dc881b510 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/solvers/mpi_solver.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/solvers/mpi_solver.py @@ -1,5 +1,9 @@ from mpi4py import MPI -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf + import numpy as np import pybullet_envs.deep_mimic.learning.tf_util as TFUtil import pybullet_utils.math_util as MathUtil diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_agent.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_agent.py index 185c0e67a..e77c0bb53 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_agent.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_agent.py @@ -1,5 +1,8 @@ import numpy as np -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from abc import abstractmethod from pybullet_envs.deep_mimic.learning.rl_agent import RLAgent diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_normalizer.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_normalizer.py index d13371069..80e8f7624 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_normalizer.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_normalizer.py @@ -1,6 +1,9 @@ import numpy as np import copy -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf from pybullet_envs.deep_mimic.learning.normalizer import Normalizer diff --git a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_util.py b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_util.py index 859eefab2..d2ffe7f0c 100644 --- a/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_util.py +++ b/examples/pybullet/gym/pybullet_envs/deep_mimic/learning/tf_util.py @@ -1,8 +1,14 @@ -import tf.compat.v1 as tf +try: + import tensorflow.compat.v1 as tf +except Exception: + import tensorflow as tf import numpy as np import os -xavier_initializer = tf.contrib.layers.xavier_initializer() +try: + xavier_initializer = tf.contrib.layers.xavier_initializer() +except Exception: + xavier_initializer = None def disable_gpu():