Esempio n. 1
0
    def __init__(
        self,
        model_spec: Union[YOLOv5_ModelSpec, YOLOv5_TFLite_ModelSpec]
    ):
        super().__init__(model_spec)

        if model_spec.class_names is not None:
            if isinstance(model_spec.class_names, str) or isinstance(model_spec.class_names, Path):
                with fsspec.open(model_spec.class_names, 'r', encoding='utf-8') as out:
                    self.class_names = np.array(json.load(out))
            else:
                self.class_names = np.array(model_spec.class_names)
        else:
            self.class_names = None

        if isinstance(model_spec.preprocess_input, str) or isinstance(model_spec.preprocess_input, Path):
            self._preprocess_input = get_preprocess_input_from_script_file(
                script_file=model_spec.preprocess_input
            )
        else:
            if model_spec.preprocess_input is None:
                self._preprocess_input = lambda x: x
            else:
                self._preprocess_input = model_spec.preprocess_input
        if isinstance(model_spec, YOLOv5_ModelSpec):
            self._load_yolov5_model(model_spec)
            self._raw_predict_images = self._raw_predict_images_torch
        elif isinstance(model_spec, YOLOv5_TFLite_ModelSpec):
            self._load_yolov5_tflite(model_spec)
            self._raw_predict_images = self._raw_predict_images_tflite
        else:
            raise ValueError(
                f"ObjectDetectionAPI_Model got unknown DetectionModelSpec: {type(model_spec)}"
            )
Esempio n. 2
0
    def __init__(
        self,
        model_spec: Union[
            ObjectDetectionAPI_ModelSpec,
            ObjectDetectionAPI_pb_ModelSpec,
            ObjectDetectionAPI_TFLite_ModelSpec,
            ObjectDetectionAPI_KFServing
        ],
    ):
        super().__init__(model_spec)

        if model_spec.class_names is not None:
            if isinstance(model_spec.class_names, str) or isinstance(model_spec.class_names, Path):
                with fsspec.open(model_spec.class_names, 'r', encoding='utf-8') as out:
                    self.class_names = np.array(json.load(out))
            else:
                self.class_names = np.array(model_spec.class_names)

            if isinstance(model_spec, ObjectDetectionAPI_pb_ModelSpec):
                self.class_names_coef = -1  # saved_model.pb returns from 1
            else:
                self.class_names_coef = 0
        else:
            self.class_names = None
            self.coef = -1

        if isinstance(model_spec, ObjectDetectionAPI_ModelSpec):
            self._load_object_detection_api(model_spec)
            self._raw_predict_single_image = self._raw_predict_single_image_default
        elif isinstance(model_spec, ObjectDetectionAPI_pb_ModelSpec):
            self._load_object_detection_api_pb(model_spec)
            self._raw_predict_single_image = self._raw_predict_single_image_default
        elif isinstance(model_spec, ObjectDetectionAPI_TFLite_ModelSpec):
            self._load_object_detection_api_tflite(model_spec)
            self._raw_predict_single_image = self._raw_predict_single_image_tflite
        elif isinstance(model_spec, ObjectDetectionAPI_KFServing):
            self.input_dtype = INPUT_TYPE_TO_DTYPE[model_spec.input_type]
            # Wake up the service
            try:
                self._raw_predict_single_image_kfserving(
                    image=np.zeros((128, 128, 3)),
                    timeout=1.
                )
            except requests.exceptions.ReadTimeout:
                pass
            self._raw_predict_single_image = self._raw_predict_single_image_kfserving
        else:
            raise ValueError(
                f"ObjectDetectionAPI_Model got unknown DetectionModelSpec: {type(model_spec)}"
            )

        if isinstance(model_spec.preprocess_input, str) or isinstance(model_spec.preprocess_input, Path):
            self._preprocess_input = get_preprocess_input_from_script_file(
                script_file=model_spec.preprocess_input
            )
        else:
            if model_spec.preprocess_input is None:
                self._preprocess_input = lambda x: x
            else:
                self._preprocess_input = model_spec.preprocess_input
Esempio n. 3
0
    def __init__(
        self,
        model_spec: Union[TensorFlow_KeypointsRegressorModelSpec,
                          TensorFlow_KeypointsRegressorModelSpec_TFServing]):
        super().__init__(model_spec)

        if isinstance(model_spec, TensorFlow_KeypointsRegressorModelSpec):
            self._load_tensorflow_KeypointsRegressor_model_spec(model_spec)
            self._raw_predict = self._raw_predict_tensorflow
        elif isinstance(model_spec,
                        TensorFlow_KeypointsRegressorModelSpec_TFServing):
            self.input_dtype = INPUT_TYPE_TO_DTYPE[model_spec.input_type]
            # Wake up the service
            try:
                self._raw_predict_kfserving(images=np.zeros(
                    (1, *self.input_size, 3)),
                                            timeout=1.)
            except requests.exceptions.ReadTimeout:
                pass
            self._raw_predict = self._raw_predict_kfserving
        else:
            raise ValueError(
                f"Tensorflow_KeypointsRegressorModel got unknown KeypointsRegressorModelSpec: {type(model_spec)}"
            )

        if isinstance(model_spec.preprocess_input, str) or isinstance(
                model_spec.preprocess_input, Path):
            self._preprocess_input = get_preprocess_input_from_script_file(
                script_file=model_spec.preprocess_input)
        else:
            if model_spec.preprocess_input is None:
                self._preprocess_input = lambda x: x
            else:
                self._preprocess_input = model_spec.preprocess_input
Esempio n. 4
0
    def __init__(self, model_spec: Detectron2_ModelSpec):
        super().__init__(model_spec)

        if model_spec.class_names is not None:
            if isinstance(model_spec.class_names, str) or isinstance(
                    model_spec.class_names, Path):
                with fsspec.open(model_spec.class_names, 'r',
                                 encoding='utf-8') as out:
                    self.class_names = np.array(json.load(out))
            else:
                self.class_names = np.array(model_spec.class_names)
        else:
            self.class_names = None

        if isinstance(model_spec, Detectron2_ModelSpec):
            self._load_pt_model(model_spec)
            self.device = model_spec.device
            self.input_format = model_spec.device
            self.input_type = model_spec.input_type
        else:
            raise ValueError(
                f"{Detectron2_DetectionModel.__name__} got unknown DetectionModelSpec: {type(model_spec)}"
            )

        if isinstance(model_spec.preprocess_input, str) or isinstance(
                model_spec.preprocess_input, Path):
            self._preprocess_input = get_preprocess_input_from_script_file(
                script_file=model_spec.preprocess_input)
        else:
            if model_spec.preprocess_input is None:
                self._preprocess_input = lambda x: x
            else:
                self._preprocess_input = model_spec.preprocess_input
Esempio n. 5
0
    def __init__(
        self,
        model_spec: PyTorch_EmbedderModelSpec
    ):
        super().__init__(model_spec)

        self._load_pytorch_model_spec(model_spec)
        self._raw_predict = self._raw_predict_pytorch

        if isinstance(model_spec.preprocess_input, str) or isinstance(model_spec.preprocess_input, Path):
            self._preprocess_input = get_preprocess_input_from_script_file(
                script_file=model_spec.preprocess_input
            )
        else:
            if model_spec.preprocess_input is None:
                self._preprocess_input = lambda x: x
            else:
                self._preprocess_input = model_spec.preprocess_input
Esempio n. 6
0
    def __init__(
        self, model_spec: Union[TensorFlow_ClassificationModelSpec,
                                TensorFlow_ClassificationModelSpec_TFServing]):
        super().__init__(model_spec)

        if isinstance(model_spec.class_names, str) or isinstance(
                model_spec.class_names, Path):
            with fsspec.open(model_spec.class_names, 'r',
                             encoding='utf-8') as out:
                self._class_names = json.load(out)
        else:
            self._class_names = model_spec.class_names

        if isinstance(model_spec, TensorFlow_ClassificationModelSpec):
            self._load_tensorflow_classification_model_spec(model_spec)
            self._raw_predict = self._raw_predict_tensorflow
        elif isinstance(model_spec,
                        TensorFlow_ClassificationModelSpec_TFServing):
            self.input_dtype = INPUT_TYPE_TO_DTYPE[model_spec.input_type]
            # Wake up the service
            try:
                self._raw_predict_kfserving(images=np.zeros(
                    (1, *self.input_size, 3)),
                                            timeout=1.)
            except requests.exceptions.ReadTimeout:
                pass
            self._raw_predict = self._raw_predict_kfserving
        else:
            raise ValueError(
                f"Tensorflow_ClassificationModel got unknown ClassificationModelSpec: {type(model_spec)}"
            )

        if isinstance(model_spec.preprocess_input, str) or isinstance(
                model_spec.preprocess_input, Path):
            self._preprocess_input = get_preprocess_input_from_script_file(
                script_file=model_spec.preprocess_input)
        else:
            if model_spec.preprocess_input is None:
                self._preprocess_input = lambda x: x
            else:
                self._preprocess_input = model_spec.preprocess_input

        self.id_to_class_name = np.array(
            [class_name for class_name in self._class_names])