Code source de code_beatrix.ai.dlbase

"""
Base class for deep learning models.


:githublink:`%|py|5`
"""
import os


[docs]class DeepLearningBase: """ Implements a common interface to manipulate pre-trained deep learning models. :githublink:`%|py|12` """
[docs] def __init__(self, model, gpu, fLOG=None): """ :param model: model (url, filename, ...) :param gpu: use gpu :param fLOG: logging function :githublink:`%|py|19` """ self._gpu = gpu if model is None: raise ValueError("model must be specified") if isinstance(model, str): if not os.path.exists(model): raise FileNotFoundError("Unable to find '{0}'".format(model)) raise NotImplementedError( "Unable to load model '{0}'".format(model)) self._model = model self._fLOG = fLOG
[docs] def log(self, *l, **p): """ Log something. :githublink:`%|py|34` """ if self._fLOG: self._fLOG(*l, **p)
[docs] def predict(self, X): """ Applies the model on features *X*. :param X: features :return: prediction :githublink:`%|py|44` """ raise NotImplementedError("Method predict is not implemented.")
[docs]class DeepLearningImage(DeepLearningBase): """ Implements a common interface to manipulate pre-trained deep learning models processing images. :githublink:`%|py|52` """
[docs] def __init__(self, model, gpu=False, fLOG=None): """ :param model: model name :param gpu: use gpu :param fLOG: logging function :githublink:`%|py|59` """ DeepLearningBase.__init__(self, model, gpu=gpu, fLOG=fLOG)