|
|
|
|
|
|
def test_initialize_trainer_parameters_override_defaults( |
|
|
|
BrainParametersMock, dummy_config_with_override |
|
|
|
): |
|
|
|
summaries_dir = "test_dir" |
|
|
|
model_path = "model_dir" |
|
|
|
output_path = "model_dir" |
|
|
|
keep_checkpoints = 1 |
|
|
|
train_model = True |
|
|
|
load_model = False |
|
|
|
|
|
|
base_config = dummy_config_with_override |
|
|
|
expected_config = base_config["default"] |
|
|
|
expected_config["summary_path"] = f"{run_id}_testbrain" |
|
|
|
expected_config["model_path"] = model_path + "/testbrain" |
|
|
|
expected_config["output_path"] = output_path + "/testbrain" |
|
|
|
expected_config["keep_checkpoints"] = keep_checkpoints |
|
|
|
|
|
|
|
# Override value from specific brain config |
|
|
|
|
|
|
with patch.object(PPOTrainer, "__init__", mock_constructor): |
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=base_config, |
|
|
|
summaries_dir=summaries_dir, |
|
|
|
model_path=model_path, |
|
|
|
output_path=output_path, |
|
|
|
keep_checkpoints=keep_checkpoints, |
|
|
|
train_model=train_model, |
|
|
|
load_model=load_model, |
|
|
|
|
|
|
brain_params_mock = BrainParametersMock() |
|
|
|
BrainParametersMock.return_value.brain_name = "testbrain" |
|
|
|
external_brains = {"testbrain": BrainParametersMock()} |
|
|
|
summaries_dir = "test_dir" |
|
|
|
model_path = "model_dir" |
|
|
|
output_path = "results_dir" |
|
|
|
keep_checkpoints = 1 |
|
|
|
train_model = True |
|
|
|
load_model = False |
|
|
|
|
|
|
base_config = dummy_config |
|
|
|
expected_config = base_config["default"] |
|
|
|
expected_config["summary_path"] = f"{run_id}_testbrain" |
|
|
|
expected_config["model_path"] = model_path + "/testbrain" |
|
|
|
expected_config["output_path"] = output_path + "/testbrain" |
|
|
|
expected_config["keep_checkpoints"] = keep_checkpoints |
|
|
|
|
|
|
|
def mock_constructor( |
|
|
|
|
|
|
with patch.object(PPOTrainer, "__init__", mock_constructor): |
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=base_config, |
|
|
|
summaries_dir=summaries_dir, |
|
|
|
model_path=model_path, |
|
|
|
output_path=output_path, |
|
|
|
keep_checkpoints=keep_checkpoints, |
|
|
|
train_model=train_model, |
|
|
|
load_model=load_model, |
|
|
|
|
|
|
def test_initialize_invalid_trainer_raises_exception( |
|
|
|
BrainParametersMock, dummy_bad_config |
|
|
|
): |
|
|
|
summaries_dir = "test_dir" |
|
|
|
model_path = "model_dir" |
|
|
|
output_path = "results_dir" |
|
|
|
keep_checkpoints = 1 |
|
|
|
train_model = True |
|
|
|
load_model = False |
|
|
|
|
|
|
with pytest.raises(TrainerConfigError): |
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=bad_config, |
|
|
|
summaries_dir=summaries_dir, |
|
|
|
model_path=model_path, |
|
|
|
output_path=output_path, |
|
|
|
keep_checkpoints=keep_checkpoints, |
|
|
|
train_model=train_model, |
|
|
|
load_model=load_model, |
|
|
|
|
|
|
with pytest.raises(TrainerConfigError): |
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=bad_config, |
|
|
|
summaries_dir=summaries_dir, |
|
|
|
model_path=model_path, |
|
|
|
output_path=output_path, |
|
|
|
keep_checkpoints=keep_checkpoints, |
|
|
|
train_model=train_model, |
|
|
|
load_model=load_model, |
|
|
|
|
|
|
with pytest.raises(UnityTrainerException): |
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=bad_config, |
|
|
|
summaries_dir=summaries_dir, |
|
|
|
model_path=model_path, |
|
|
|
output_path=output_path, |
|
|
|
keep_checkpoints=keep_checkpoints, |
|
|
|
train_model=train_model, |
|
|
|
load_model=load_model, |
|
|
|
|
|
|
|
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=no_default_config, |
|
|
|
summaries_dir="test_dir", |
|
|
|
model_path="model_dir", |
|
|
|
output_path="output_path", |
|
|
|
keep_checkpoints=1, |
|
|
|
train_model=True, |
|
|
|
load_model=False, |
|
|
|
|
|
|
|
|
|
|
trainer_factory = trainer_util.TrainerFactory( |
|
|
|
trainer_config=bad_config, |
|
|
|
summaries_dir="test_dir", |
|
|
|
model_path="model_dir", |
|
|
|
output_path="output_path", |
|
|
|
keep_checkpoints=1, |
|
|
|
train_model=True, |
|
|
|
load_model=False, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_existing_directories(tmp_path): |
|
|
|
model_path = os.path.join(tmp_path, "runid") |
|
|
|
# Unused summary path |
|
|
|
summary_path = os.path.join(tmp_path, "runid") |
|
|
|
output_path = os.path.join(tmp_path, "runid") |
|
|
|
trainer_util.handle_existing_directories(model_path, summary_path, False, False) |
|
|
|
trainer_util.handle_existing_directories(output_path, False, False) |
|
|
|
trainer_util.handle_existing_directories(model_path, summary_path, True, False) |
|
|
|
trainer_util.handle_existing_directories(output_path, True, False) |
|
|
|
os.mkdir(model_path) |
|
|
|
os.mkdir(output_path) |
|
|
|
trainer_util.handle_existing_directories(model_path, summary_path, False, False) |
|
|
|
trainer_util.handle_existing_directories(output_path, False, False) |
|
|
|
trainer_util.handle_existing_directories(model_path, summary_path, True, False) |
|
|
|
trainer_util.handle_existing_directories(output_path, True, False) |
|
|
|
trainer_util.handle_existing_directories(model_path, summary_path, False, True) |
|
|
|
trainer_util.handle_existing_directories(output_path, False, True) |
|
|
|
trainer_util.handle_existing_directories( |
|
|
|
model_path, summary_path, False, True, init_path |
|
|
|
) |
|
|
|
trainer_util.handle_existing_directories(output_path, False, True, init_path) |
|
|
|
trainer_util.handle_existing_directories( |
|
|
|
model_path, summary_path, False, True, init_path |
|
|
|
) |
|
|
|
trainer_util.handle_existing_directories(output_path, False, True, init_path) |