|
2 | 2 | Functions to be able to load networks from disk and interact with them. |
3 | 3 | Working with these networks can be messy and this module tries to only expose the important things. |
4 | 4 | """ |
5 | | - |
| 5 | +import json |
6 | 6 | import logging |
7 | 7 | import multiprocessing |
8 | 8 | import os |
|
17 | 17 |
|
18 | 18 | import numpy as np |
19 | 19 | import PIL |
| 20 | +import pydantic |
| 21 | +from pydantic import BaseModel, FilePath |
20 | 22 | from typing_extensions import Protocol |
21 | 23 |
|
22 | 24 | from gance import process_common |
@@ -623,3 +625,61 @@ def network_indices(self: "MultiNetwork") -> List[int]: |
623 | 625 | :return: Indices |
624 | 626 | """ |
625 | 627 | return [index for index, _ in enumerate(self._network_paths)] |
| 628 | + |
| 629 | + @property |
| 630 | + def network_paths(self: "MultiNetwork") -> List[Path]: |
| 631 | + """ |
| 632 | + Exposes the network paths for reading. |
| 633 | + :return: Paths to the network files |
| 634 | + """ |
| 635 | + return self._network_paths |
| 636 | + |
| 637 | + |
| 638 | +def parse_network_paths( |
| 639 | + networks_directory: Optional[str], networks: Optional[List[str]], networks_json: Optional[str] |
| 640 | +) -> List[Path]: |
| 641 | + """ |
| 642 | + Given the user's input from the CLI, get a list of the networks to be used in the run. |
| 643 | + :param networks_directory: A string representing a path to a directory that contains network |
| 644 | + files. Optionally given. |
| 645 | + :param networks: Paths (as strings) leading directly to networks. Optionally given. |
| 646 | + :param networks_json: Path to a json file with a list of network paths. |
| 647 | + :return: Path objects leading to the networks. Sorted by filename. |
| 648 | + :raises ValueError: If something goes wrong with a parse. |
| 649 | + """ |
| 650 | + |
| 651 | + all_networks = [] |
| 652 | + |
| 653 | + if networks_directory is not None: |
| 654 | + networks_directory_path = Path(networks_directory) |
| 655 | + all_networks += sorted_networks_in_directory(networks_directory=networks_directory_path) |
| 656 | + |
| 657 | + if networks is not None: |
| 658 | + all_networks += list(map(Path, networks)) |
| 659 | + |
| 660 | + if networks_json is not None: |
| 661 | + LOGGER.info(f"Loading network JSON: {networks_json}") |
| 662 | + try: |
| 663 | + with open(networks_json) as f: |
| 664 | + all_networks += list(map(Path, NetworksFile(**json.load(f)).networks)) |
| 665 | + except pydantic.error_wrappers.ValidationError as e: |
| 666 | + raise ValueError("Ran into formatting problem with networks JSON.") from e |
| 667 | + except Exception as e: |
| 668 | + raise ValueError("Couldn't open networks JSON.") from e |
| 669 | + |
| 670 | + if not all_networks: |
| 671 | + raise ValueError("No networks given, cannot continue.") |
| 672 | + |
| 673 | + LOGGER.info("Discovered networks: ") |
| 674 | + for path in all_networks: |
| 675 | + LOGGER.info(f"\t{path}") |
| 676 | + |
| 677 | + return all_networks |
| 678 | + |
| 679 | + |
| 680 | +class NetworksFile(BaseModel): |
| 681 | + """ |
| 682 | + Describes a `NetworksFile`, a .json file full paths to pickled StyleGAN networks. |
| 683 | + """ |
| 684 | + |
| 685 | + networks: List[FilePath] |
0 commit comments