2022-01-11 19:15:58 +01:00
|
|
|
import pickle
|
|
|
|
|
import datetime
|
2022-01-26 23:22:43 +01:00
|
|
|
from config.types import Config
|
2022-01-29 06:41:40 +01:00
|
|
|
from typing import Optional
|
2022-01-11 19:15:58 +01:00
|
|
|
import os
|
|
|
|
|
import warnings
|
2022-01-29 06:41:40 +01:00
|
|
|
from training.types import PipelineOutcome
|
2022-01-11 19:15:58 +01:00
|
|
|
|
2022-02-17 19:22:17 +01:00
|
|
|
|
2022-01-29 06:41:40 +01:00
|
|
|
def save_models(pipeline_outcome: PipelineOutcome, config: Config) -> None:
|
2022-01-12 23:10:18 +01:00
|
|
|
dict_for_pickle = dict()
|
2022-02-17 19:22:17 +01:00
|
|
|
dict_for_pickle["config"] = config
|
|
|
|
|
dict_for_pickle["pipeline_outcome"] = pipeline_outcome
|
|
|
|
|
|
2022-01-11 19:15:58 +01:00
|
|
|
date_string = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M")
|
2022-02-17 19:22:17 +01:00
|
|
|
|
|
|
|
|
if not os.path.exists("output/models"):
|
|
|
|
|
os.makedirs("output/models")
|
|
|
|
|
|
|
|
|
|
pickle.dump(dict_for_pickle, open("output/models/{}.p".format(date_string), "wb"))
|
2022-01-11 19:15:58 +01:00
|
|
|
|
|
|
|
|
|
2022-01-29 06:41:40 +01:00
|
|
|
def load_models(file_name: Optional[str]) -> tuple[PipelineOutcome, Config]:
|
2022-01-11 19:15:58 +01:00
|
|
|
|
2022-02-17 19:22:17 +01:00
|
|
|
if file_name is None:
|
|
|
|
|
warnings.warn(
|
|
|
|
|
"No file name provided, will load latest models and configurations."
|
|
|
|
|
)
|
|
|
|
|
files_in_directory: list = os.listdir("output/models")
|
|
|
|
|
|
2022-01-11 19:15:58 +01:00
|
|
|
assert len(files_in_directory) > 0, "No models found in output/models."
|
|
|
|
|
file_name = sorted(files_in_directory)[-1]
|
|
|
|
|
|
2022-02-17 19:22:17 +01:00
|
|
|
packacked_dict = pickle.load(open("output/models/{}".format(file_name), "rb"))
|
|
|
|
|
|
|
|
|
|
config = packacked_dict.pop("config", None)
|
|
|
|
|
pipeline_outcome = packacked_dict.pop("pipeline_outcome", None)
|
|
|
|
|
|
|
|
|
|
return pipeline_outcome, config
|