Skip to content

Commit

Permalink
setup.py: Configure keras backend
Browse files Browse the repository at this point in the history
requirements: Bump keras version
  • Loading branch information
torzdf committed May 3, 2024
1 parent a493bfe commit 29fcf11
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions requirements/_requirements_base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ pytorch>=2.2.1
torchaudio>=2.2.1
torchvision>=0.17.1
tensorboard>=2.12.1
keras>=3.2.1

keras>=3.3.1
25 changes: 25 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,30 @@ def _upgrade_pip(self) -> None:
pip_version = pip.__version__
logger.info("Installed pip: %s", pip_version)

def _configure_keras(self) -> None:
""" Set up the keras.json file to use Torch as the backend """
if "KERAS_HOME" in os.environ:
keras_dir = os.environ["KERAS_HOME"]
else:
keras_base_dir = os.path.expanduser("~")
if not os.access(keras_base_dir, os.W_OK):
keras_base_dir = "/tmp"
keras_dir = os.path.join(keras_base_dir, ".keras")

conf_file = os.path.expanduser(os.path.join(keras_dir, "keras.json"))
config = {}
if os.path.exists(conf_file):
try:
with open(conf_file, "r", encoding="utf-8") as c_file:
config = json.load(c_file)
except ValueError:
pass

config["backend"] = "torch"
with open(conf_file, "w", encoding="utf-8") as c_file:
c_file.write(json.dumps(config, indent=4))
logger.info("Keras config written to: %s", conf_file)

def set_config(self) -> None:
""" Set the backend in the faceswap config file """
config = {"backend": self.backend}
Expand All @@ -248,6 +272,7 @@ def set_config(self) -> None:
with open(config_file, "w", encoding="utf8") as cnf:
json.dump(config, cnf)
logger.info("Faceswap config written to: %s", config_file)
self._configure_keras()

def _set_env_vars(self) -> None:
""" There are some foibles under Conda which need to be worked around in different
Expand Down

0 comments on commit 29fcf11

Please sign in to comment.