gaotingquan 1 年之前
父节点
当前提交
29e3f222a7

+ 10 - 2
paddlex/inference/components/transforms/image/common.py

@@ -91,7 +91,8 @@ class ReadImage(_BaseRead):
 
     def apply(self, img):
         """apply"""
-        if not isinstance(img, str):
+        if isinstance(img, np.ndarray):
+            # TODO(gaotingquan): set delete to True
             with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
                 img_path = Path(temp_file.name)
                 self._writer.write(img_path, img)
@@ -104,7 +105,7 @@ class ReadImage(_BaseRead):
                         "ori_img_size": deepcopy([img.shape[1], img.shape[0]]),
                     }
                 ]
-        else:
+        elif isinstance(img, str):
             file_path = img
             file_path = self._download_from_url(file_path)
             file_list = self._get_files_list(file_path)
@@ -117,6 +118,13 @@ class ReadImage(_BaseRead):
                     batch = []
             if len(batch) > 0:
                 yield batch
+        else:
+            raise TypeError(
+                f"ReadImage only supports the following types:\n"
+                f"1. str, indicating a image file path or a directory containing image files.\n"
+                f"2. numpy.ndarray.\n"
+                f"However, got type: {type(img).__name__}."
+            )
 
     def _read(self, file_path):
         if file_path:

+ 26 - 19
paddlex/inference/components/transforms/ts/common.py

@@ -58,31 +58,38 @@ class ReadTS(_BaseRead):
         self._writer = CSVWriter(backend="pandas")
 
     def apply(self, ts):
-        if not isinstance(ts, str):
+        if isinstance(ts, pd.DataFrame):
             with tempfile.NamedTemporaryFile(suffix=".csv", delete=True) as temp_file:
                 input_path = Path(temp_file.name)
                 ts_path = input_path.as_posix()
                 self._writer.write(ts_path, ts)
                 yield {"input_path": input_path, "ts": ts, "ori_ts": deepcopy(ts)}
-
-        ts_path = ts
-        ts_path = self._download_from_url(ts_path)
-        file_list = self._get_files_list(ts_path)
-        batch = []
-        for ts_path in file_list:
-            ts_data = self._reader.read(ts_path)
-            batch.append(
-                {
-                    "input_path": Path(ts_path).name,
-                    "ts": ts_data,
-                    "ori_ts": deepcopy(ts_data),
-                }
-            )
-            if len(batch) >= self.batch_size:
+        elif isinstance(ts, str):
+            ts_path = ts
+            ts_path = self._download_from_url(ts_path)
+            file_list = self._get_files_list(ts_path)
+            batch = []
+            for ts_path in file_list:
+                ts_data = self._reader.read(ts_path)
+                batch.append(
+                    {
+                        "input_path": Path(ts_path).name,
+                        "ts": ts_data,
+                        "ori_ts": deepcopy(ts_data),
+                    }
+                )
+                if len(batch) >= self.batch_size:
+                    yield batch
+                    batch = []
+            if len(batch) > 0:
                 yield batch
-                batch = []
-        if len(batch) > 0:
-            yield batch
+        else:
+            raise TypeError(
+                f"ReadTS only supports the following types:\n"
+                f"1. str, indicating a CSV file path or a directory containing CSV files.\n"
+                f"2. pandas.DataFrame.\n"
+                f"However, got type: {type(ts).__name__}."
+            )
 
 
 class TSCutOff(BaseComponent):

+ 13 - 0
paddlex/inference/components/utils/__init__.py

@@ -0,0 +1,13 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 13 - 0
paddlex/inference/models/utils/__init__.py

@@ -0,0 +1,13 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 1 - 1
paddlex/inference/pipelines/__init__.py

@@ -56,7 +56,7 @@ def create_pipeline(
         # XXX: using dict class to handle all pipeline configs
         build_in_pipeline = (
             Path(__file__).parent.parent.parent / "pipelines" / f"{pipeline}.yaml"
-        )
+        ).resolve()
         if not Path(build_in_pipeline).exists():
             raise Exception(f"The pipeline don't exist! ({pipeline})")
         pipeline = build_in_pipeline

+ 13 - 0
paddlex/inference/results/utils/__init__.py

@@ -0,0 +1,13 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 8 - 6
paddlex/inference/results/utils/mixin.py

@@ -17,6 +17,7 @@ import json
 from pathlib import Path
 import numpy as np
 from PIL import Image
+import pandas as pd
 
 from ....utils import logging
 from ...utils.io import (
@@ -37,7 +38,7 @@ def _save_list_data(save_func, save_path, data, *args, **kwargs):
         for idx, single in enumerate(data):
             save_func(
                 (
-                    save_path.parent / f"{save_path.stem}_{idx}.{save_path.suffix}"
+                    save_path.parent / f"{save_path.stem}_{idx}{save_path.suffix}"
                 ).as_posix(),
                 single,
                 *args,
@@ -48,9 +49,6 @@ def _save_list_data(save_func, save_path, data, *args, **kwargs):
 
 
 class StrMixin:
-    def __init__(self):
-        self._show_func_register()(self.print)
-
     @property
     def str(self):
         return self._to_str()
@@ -74,8 +72,12 @@ class JsonMixin:
         def _format_data(obj):
             if isinstance(obj, np.float32):
                 return float(obj)
-            if isinstance(obj, np.ndarray):
+            elif isinstance(obj, np.ndarray):
                 return [_format_data(item) for item in obj.tolist()]
+            elif isinstance(obj, pd.DataFrame):
+                return obj.to_json(orient="records", force_ascii=False)
+            elif isinstance(obj, Path):
+                return obj.as_posix()
             elif isinstance(obj, dict):
                 return type(obj)({k: _format_data(v) for k, v in obj.items()})
             elif isinstance(obj, (list, tuple)):
@@ -123,7 +125,7 @@ class ImgMixin:
     def save_to_img(self, save_path, *args, **kwargs):
         if not str(save_path).lower().endswith((".jpg", ".png")):
             fp = Path(self["input_path"])
-            save_path = Path(save_path) / f"{fp.stem}.{fp.suffix}"
+            save_path = Path(save_path) / f"{fp.stem}{fp.suffix}"
         _save_list_data(self._img_writer.write, save_path, self.img, *args, **kwargs)
 
 

+ 1 - 1
paddlex/inference/utils/pp_option.py

@@ -57,7 +57,7 @@ class PaddlePredictorOption(object):
         return {
             "run_mode": "paddle",
             "device": device_type,
-            "device_id": device_id[0],
+            "device_id": 0 if device_id is None else device_id[0],
             "min_subgraph_size": 3,
             "shape_info_filename": None,
             "trt_calib_mode": False,

+ 13 - 0
paddlex/modules/formula_recognition/__init__.py

@@ -0,0 +1,13 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 13 - 0
paddlex/modules/image_unwarping/__init__.py

@@ -0,0 +1,13 @@
+# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.

+ 5 - 8
paddlex/paddlex_cli.py

@@ -60,12 +60,9 @@ def args_cfg():
     )
 
     ################# pipeline predict #################
-    parser.add_argument("--predict", action="store_true", default=True, help="")
     parser.add_argument("--pipeline", type=str, help="")
-    parser.add_argument("--model", nargs="+", help="")
-    parser.add_argument("--model_dir", nargs="+", type=parse_str, help="")
     parser.add_argument("--input", type=str, help="")
-    parser.add_argument("--save_dir", type=str, default="./", help="")
+    parser.add_argument("--save_path", type=str, default=None, help="")
     parser.add_argument("--device", type=str, default=None, help="")
 
     return parser.parse_args()
@@ -91,14 +88,14 @@ def install(args):
     return
 
 
-def pipeline_predict(pipeline, input, device=None, save_dir=None):
+def pipeline_predict(pipeline, input, device=None, save_path=None):
     """pipeline predict"""
     pipeline = create_pipeline(pipeline, device=device)
     result = pipeline(input)
     for res in result:
         res.print(json_format=False)
-        if save_dir:
-            res.save_all(save_path=save_dir)
+        if save_path:
+            res.save_all(save_path=save_path)
 
 
 # for CLI
@@ -112,5 +109,5 @@ def main():
             args.pipeline,
             args.input,
             args.device,
-            args.save_dir,
+            args.save_path,
         )

+ 0 - 3
paddlex/pipelines/OCR.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: OCR
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_ocr_001.png
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   det_model: PP-OCRv4_mobile_det
   rec_model: PP-OCRv4_mobile_rec

+ 3 - 8
paddlex/pipelines/chatocrv3.yaml → paddlex/pipelines/PP-ChatOCRv3.yaml

@@ -1,10 +1,7 @@
 Global:
-  pipeline_name: chatocrv3
-  input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/table_recognition.jpg
-  
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
+  pipeline_name: PP-ChatOCRv3
+  input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/contract.pdf
+  #input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/report.png
 Pipeline:
   layout_model: PicoDet_layout_1x
   table_model: SLANet
@@ -29,5 +26,3 @@ Pipeline:
   oricls_batch_size: 1
   recovery: True
   device: "gpu"
-
-######################################## Support ########################################

+ 0 - 3
paddlex/pipelines/anomaly_detection.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: anomaly_detection
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/uad_grid.png
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: STFPM
   batch_size: 1

+ 0 - 12
paddlex/pipelines/image_classification.yaml

@@ -2,19 +2,7 @@ Global:
   pipeline_name: image_classification
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_image_classification_001.jpg
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: PP-LCNet_x0_5
   batch_size: 1
   device: "gpu:0"
-######################################## Support ########################################
-NOTE:
-  device: 
-    - gpu
-    - gpu:2
-    - cpu
-  batch_size: "任意正整数"
-  model:
-    - PP-LCNet_x0_5

+ 0 - 12
paddlex/pipelines/instance_segmentation.yaml

@@ -2,19 +2,7 @@ Global:
   pipeline_name: instance_segmentation
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_instance_segmentation_004.png
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: Mask-RT-DETR-S
   batch_size: 1
   device: "gpu:0"
-######################################## Support ########################################
-NOTE:
-  device: 
-    - gpu
-    - gpu:2
-    - cpu
-  batch_size: "任意正整数"
-  model:
-    - Mask-RT-DETR-S

+ 0 - 3
paddlex/pipelines/multi_label_image_classification.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: multi_label_image_classification
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_image_classification_001.jpg
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: PP-LCNet_x1_0_ML
   batch_size: 1

+ 0 - 12
paddlex/pipelines/object_detection.yaml

@@ -2,19 +2,7 @@ Global:
   pipeline_name: object_detection
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/general_object_detection_002.png
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: PicoDet-S
   batch_size: 1
   device: "gpu:0"
-######################################## Support ########################################
-NOTE:
-  device: 
-    - gpu
-    - gpu:2
-    - cpu
-  batch_size: "任意正整数"
-  model:
-    - PicoDet-S

+ 0 - 12
paddlex/pipelines/semantic_segmentation.yaml

@@ -2,19 +2,7 @@ Global:
   pipeline_name: semantic_segmentation
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/PaddleX3.0/application/semantic_segmentation/makassaridn-road_demo.png
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: PP-LiteSeg-T
   batch_size: 1
   device: "gpu:0"
-######################################## Support ########################################
-NOTE:
-  device: 
-    - gpu
-    - gpu:2
-    - cpu
-  batch_size: "任意正整数"
-  model:
-    - PP-LiteSeg-T

+ 0 - 3
paddlex/pipelines/small_object_detection.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: small_object_detection
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/small_object_detection.jpg
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: PP-YOLOE_plus_SOD-L
   batch_size: 1

+ 0 - 5
paddlex/pipelines/table_recognition.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: table_recognition
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/imgs/demo_image/table_recognition.jpg
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   layout_model: PicoDet_layout_1x
   table_model: SLANet
@@ -14,5 +11,3 @@ Pipeline:
   text_rec_batch_size: 1
   table_batch_size: 1
   device: "gpu:0"
-
-######################################## Support ########################################

+ 0 - 3
paddlex/pipelines/ts_ad.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: ts_ad
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/ts/demo_ts/ts_ad.csv
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: DLinear_ad
   batch_size: 1

+ 0 - 3
paddlex/pipelines/ts_cls.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: ts_cls
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/ts/demo_ts/ts_cls.csv
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: TimesNet_cls
   batch_size: 1

+ 0 - 3
paddlex/pipelines/ts_fc.yaml

@@ -2,9 +2,6 @@ Global:
   pipeline_name: ts_fc
   input: https://paddle-model-ecology.bj.bcebos.com/paddlex/ts/demo_ts/ts_fc.csv
   
-######################################## Setting ########################################
-# Please select the model from bellow `Support`
-
 Pipeline:
   model: DLinear
   batch_size: 1

+ 9 - 22
setup.py

@@ -16,6 +16,7 @@
 import os
 import glob
 import itertools
+from pathlib import Path
 
 from setuptools import find_packages
 from setuptools import setup
@@ -52,33 +53,19 @@ def packages_and_package_data():
                             continue
                     yield os.path.join(root, f)
 
-    pkgs = find_packages(
-        include=[
-            "paddlex",
-            "paddlex.modules",
-            "paddlex.modules.*",
-            "paddlex.pipelines",
-            "paddlex.pipelines.*",
-            "paddlex.repo_manager",
-            "paddlex.repo_apis",
-            "paddlex.repo_apis.*",
-            "paddlex.utils",
-            "paddlex.utils.*",
-        ]
-    )
+    pkgs = find_packages(exclude=["tests", "*.tests", "*.tests.*", "tests.*"])
     pkg_data = []
     for p in itertools.chain(
-        # Configuration files
         _recursively_find("paddlex/configs/*", exts=[".yml", ".yaml"]),
     ):
-        parts = os.path.normpath(p).split(os.sep)
-        ext = os.path.splitext(p)[1]
-        # Globally exclude Python bytecode files
-        if ext in (".pyc", ".pyo"):
+        if Path(p).suffix in (".pyc", ".pyo"):
             continue
-        # According to https://setuptools.pypa.io/en/latest/userguide/datafiles.html
-        rp = "/".join(parts[1:])
-        pkg_data.append(rp)
+        pkg_data.append(Path(p).relative_to("paddlex").as_posix())
+    pipeline_config = [
+        Path(p).relative_to("paddlex").as_posix()
+        for p in glob.glob("paddlex/pipelines/*.yaml")
+    ]
+    pkg_data.extend(pipeline_config)
     pkg_data.append(".version")
     pkg_data.append("utils/fonts/PingFang-SC-Regular.ttf")
     pkg_data.append("repo_manager/requirements.txt")