Пример #1
0
def load_model(path, compile=False):
    """Load a model that uses custom `atlalign` layers.

    The benefit of using this function as opposed to the keras equivalent is that the user does not have to care about
    how the model was saved (whether architecture and weights were separated). Additionally, all custom possible
    custom layers are provided.

    Parameters
    ----------
    path : str or pathlib.Path
        If `path` is a folder then the folder is expected to have one `.h5` file (weights) and one `.json`
        (architecture). If `path` is a file then it needs to be an `.h5` and it needs to encapsulate both
        the weights and the architecture.

    compile : bool
        Only possible if `path` refers to a `.h5` file.

    Returns
    -------
    keras.Model
        Model ready for inference. If `compile=True` then also ready for continuing the training.

    """
    path = pathlib.Path(str(path))

    if path.is_file():
        model = load_model_keras(
            str(path),
            compile=compile,
            custom_objects={
                "Affine2DVF": Affine2DVF,
                "DVFComposition": DVFComposition,
                "BilinearInterpolation": BilinearInterpolation,
                "ExtractMoving": ExtractMoving,
                "NoOp": NoOp,
            },
        )

    elif path.is_dir():
        if compile:
            raise ValueError(
                "Cannot compile the model because weights and architecture stored separately."
            )

        h5_files = [p for p in path.iterdir() if p.suffix == ".h5"]
        json_files = [p for p in path.iterdir() if p.suffix == ".json"]

        if not (len(h5_files) == 1 and len(json_files) == 1):
            raise ValueError(
                "The folder {} needs to contain exactly one .h5 file and one .json file"
                .format(path))

        path_architecture = json_files[0]
        path_weights = h5_files[0]

        with path_architecture.open("r") as f:
            json_str = json.load(f)

        model = model_from_json(
            json_str,
            custom_objects={
                "Affine2DVF": Affine2DVF,
                "DVFComposition": DVFComposition,
                "BilinearInterpolation": BilinearInterpolation,
                "ExtractMoving": ExtractMoving,
                "NoOp": NoOp,
            },
        )
        model.load_weights(str(path_weights))

    else:
        raise OSError("The path {} does not exist.".format(str(path)))

    return model
Пример #2
0
 def load_model(self, path):
     self.model = load_model_keras(path)
Пример #3
0
def load_model(path='./models/model_saves/Final_Model.model'):
    model = load_model_keras(path)
    return model
Пример #4
0
def load_model(model_path: Path) -> Model:
    return load_model_keras(str(model_path))
Пример #5
0
def load_model(model_path: str) -> Model:
    return load_model_keras(model_path)
def get_classification_model():
    return load_model_keras('classification_model.h5')