def load_from_checkpoint( checkpoint: Checkpoint, ) -> Tuple[xgb.Booster, Optional[Preprocessor]]: checkpoint_path = checkpoint.to_directory() xgb_model = xgb.Booster() xgb_model.load_model(os.path.join(checkpoint_path, MODEL_KEY)) preprocessor_path = os.path.join(checkpoint_path, PREPROCESSOR_KEY) if os.path.exists(preprocessor_path): with open(preprocessor_path, "rb") as f: preprocessor = cpickle.load(f) else: preprocessor = None return xgb_model, preprocessor
def load_from_checkpoint( checkpoint: Checkpoint, ) -> Tuple[RandomForestClassifier, Optional[Preprocessor]]: path = checkpoint.to_directory() estimator_path = os.path.join(path, MODEL_KEY) with open(estimator_path, "rb") as f: estimator = cpickle.load(f) preprocessor_path = os.path.join(path, PREPROCESSOR_KEY) if os.path.exists(preprocessor_path): with open(preprocessor_path, "rb") as f: preprocessor = cpickle.load(f) else: preprocessor = None return estimator, preprocessor
def from_checkpoint(cls, checkpoint: Checkpoint) -> "LightGBMPredictor": """Instantiate the predictor from a Checkpoint. The checkpoint is expected to be a result of ``LightGBMTrainer``. Args: checkpoint (Checkpoint): The checkpoint to load the model and preprocessor from. It is expected to be from the result of a ``LightGBMTrainer`` run. """ path = checkpoint.to_directory() bst = lightgbm.Booster(model_file=os.path.join(path, MODEL_KEY)) preprocessor_path = os.path.join(path, PREPROCESSOR_KEY) if os.path.exists(preprocessor_path): with open(preprocessor_path, "rb") as f: preprocessor = cpickle.load(f) else: preprocessor = None shutil.rmtree(path) return LightGBMPredictor(model=bst, preprocessor=preprocessor)