Эх сурвалжийг харах

bugfix (#2956)

* bugfix: follow #2868

* fix rec_topk & rec_scores to list type

* fix attr rec pipeline

* bugfix: ref #2941
Tingquan Gao 10 сар өмнө
parent
commit
0a6e07c62a

+ 19 - 18
paddlex/inference/pipelines/__init__.py

@@ -73,12 +73,12 @@ def get_pipeline_path(pipeline_name: str) -> str:
     return pipeline_path
 
 
-def load_pipeline_config(pipeline_name: str) -> Dict[str, Any]:
+def load_pipeline_config(pipeline: str) -> Dict[str, Any]:
     """
     Load the pipeline configuration.
 
     Args:
-        pipeline_name (str): The name of the pipeline or the path to the config file.
+        pipeline (str): The name of the pipeline or the path to the config file.
 
     Returns:
         Dict[str, Any]: The parsed pipeline configuration.
@@ -86,20 +86,20 @@ def load_pipeline_config(pipeline_name: str) -> Dict[str, Any]:
     Raises:
         Exception: If the config file of pipeline does not exist.
     """
-    if not (pipeline_name.endswith(".yml") or pipeline_name.endswith(".yaml")):
-        pipeline_path = get_pipeline_path(pipeline_name)
+    if not (pipeline.endswith(".yml") or pipeline.endswith(".yaml")):
+        pipeline_path = get_pipeline_path(pipeline)
         if pipeline_path is None:
             raise Exception(
-                f"The pipeline ({pipeline_name}) does not exist! Please use a pipeline name or a config file path!"
+                f"The pipeline ({pipeline}) does not exist! Please use a pipeline name or a config file path!"
             )
     else:
-        pipeline_path = pipeline_name
+        pipeline_path = pipeline
     config = parse_config(pipeline_path)
     return config
 
 
 def create_pipeline(
-    pipeline_name: Optional[str] = None,
+    pipeline: Optional[str] = None,
     config: Optional[Dict[str, Any]] = None,
     device: Optional[str] = None,
     pp_option: Optional[PaddlePredictorOption] = None,
@@ -114,7 +114,7 @@ def create_pipeline(
     default config corresponding to the pipeline name.
 
     Args:
-        pipeline_name (Optional[str], optional): The name of the pipeline to
+        pipeline (Optional[str], optional): The name of the pipeline to
             create, or the path to the config file. Defaults to None.
         config (Optional[Dict[str, Any]], optional): The pipeline configuration.
             Defaults to None.
@@ -130,19 +130,20 @@ def create_pipeline(
     Returns:
         BasePipeline: The created pipeline instance.
     """
-    if pipeline_name is None and config is None:
+    if pipeline is None and config is None:
         raise ValueError(
-            "Both `pipeline_name` and `config` cannot be None at the same time."
+            "Both `pipeline` and `config` cannot be None at the same time."
         )
     if config is None:
-        config = load_pipeline_config(pipeline_name)
-    if pipeline_name is not None and config["pipeline_name"] != pipeline_name:
-        logging.warning(
-            "The pipeline name in the config (%r) is different from the specified pipeline name (%r). %r will be used.",
-            config["pipeline_name"],
-            pipeline_name,
-            config["pipeline_name"],
-        )
+        config = load_pipeline_config(pipeline)
+    else:
+        if pipeline is not None and config["pipeline_name"] != pipeline:
+            logging.warning(
+                "The pipeline name in the config (%r) is different from the specified pipeline name (%r). %r will be used.",
+                config["pipeline_name"],
+                pipeline,
+                config["pipeline_name"],
+            )
     pipeline_name = config["pipeline_name"]
 
     pipeline = BasePipeline.get(pipeline_name)(

+ 5 - 5
paddlex/inference/pipelines/attribute_recognition/pipeline.py

@@ -65,11 +65,11 @@ class AttributeRecPipeline(BasePipeline):
         for img_id, batch_data in enumerate(self.batch_sampler(input)):
             raw_imgs = self.img_reader(batch_data.instances)
             all_det_res = list(self.det_model(raw_imgs, threshold=det_threshold))
-            for input_data, raw_img, det_res in zip(
-                batch_data.instances, raw_imgs, all_det_res
+            for input_path, input_data, raw_img, det_res in zip(
+                batch_data.input_paths, batch_data.instances, raw_imgs, all_det_res
             ):
                 cls_res = self.get_cls_result(raw_img, det_res, cls_threshold)
-                yield self.get_final_result(input_data, raw_img, det_res, cls_res)
+                yield self.get_final_result(input_path, raw_img, det_res, cls_res)
 
     def get_cls_result(self, raw_img, det_res, cls_threshold):
         subs_of_img = list(self._crop_by_boxes(raw_img, det_res["boxes"]))
@@ -81,8 +81,8 @@ class AttributeRecPipeline(BasePipeline):
             output["score"].append(res["scores"])
         return output
 
-    def get_final_result(self, input_data, raw_img, det_res, rec_res):
-        single_img_res = {"input_path": input_data, "input_img": raw_img, "boxes": []}
+    def get_final_result(self, input_path, raw_img, det_res, rec_res):
+        single_img_res = {"input_path": input_path, "input_img": raw_img, "boxes": []}
         for i, obj in enumerate(det_res["boxes"]):
             cls_scores = rec_res["score"][i]
             labels = rec_res["label"][i]

+ 1 - 2
paddlex/inference/pipelines/attribute_recognition/result.py

@@ -87,8 +87,7 @@ class AttributeRecResult(BaseCVResult):
 
     def _to_img(self):
         """apply"""
-        img_reader = ImageReader(backend="pillow")
-        image = img_reader.read(self["input_path"])
+        image = Image.fromarray(cv2.cvtColor(self["input_img"], cv2.COLOR_BGR2RGB))
         boxes = [
             {
                 "coordinate": box["coordinate"],

+ 1 - 1
paddlex/inference/pipelines/face_recognition/pipeline.py

@@ -44,7 +44,7 @@ class FaceRecPipeline(ShiTuV2Pipeline):
     def get_final_result(self, input_data, raw_img, det_res, rec_res):
         single_img_res = {"input_path": input_data, "input_img": raw_img, "boxes": []}
         for i, obj in enumerate(det_res["boxes"]):
-            rec_scores = rec_res["score"][i]
+            rec_scores = rec_res["score"][i].tolist()
             labels = rec_res["label"][i]
             single_img_res["boxes"].append(
                 {

+ 7 - 5
paddlex/inference/pipelines/pp_shitu_v2/pipeline.py

@@ -37,7 +37,7 @@ class ShiTuV2Pipeline(BasePipeline):
         super().__init__(device=device, pp_option=pp_option, use_hpip=use_hpip)
 
         self._topk, self._rec_threshold, self._hamming_radius, self._det_threshold = (
-            config.get("topk", 5),
+            config.get("rec_topk", 5),
             config.get("rec_threshold", 0.5),
             config.get("hamming_radius", None),
             config.get("det_threshold", 0.5),
@@ -57,14 +57,16 @@ class ShiTuV2Pipeline(BasePipeline):
         indexer = FaissIndexer(index) if index is not None else self.indexer
         assert indexer
         kwargs = {k: v for k, v in kwargs.items() if v is not None}
-        topk = kwargs.get("topk", self._topk)
+        topk = kwargs.get("rec_topk", self._topk)
         rec_threshold = kwargs.get("rec_threshold", self._rec_threshold)
         hamming_radius = kwargs.get("hamming_radius", self._hamming_radius)
         det_threshold = kwargs.get("det_threshold", self._det_threshold)
         for img_id, batch_data in enumerate(self.batch_sampler(input)):
-            raw_imgs = self.img_reader(batch_data)
+            raw_imgs = self.img_reader(batch_data.instances)
             all_det_res = list(self.det_model(raw_imgs, threshold=det_threshold))
-            for input_data, raw_img, det_res in zip(batch_data, raw_imgs, all_det_res):
+            for input_data, raw_img, det_res in zip(
+                batch_data.instances, raw_imgs, all_det_res
+            ):
                 rec_res = self.get_rec_result(
                     raw_img, det_res, indexer, rec_threshold, hamming_radius, topk
                 )
@@ -101,7 +103,7 @@ class ShiTuV2Pipeline(BasePipeline):
     def get_final_result(self, input_data, raw_img, det_res, rec_res):
         single_img_res = {"input_path": input_data, "input_img": raw_img, "boxes": []}
         for i, obj in enumerate(det_res["boxes"]):
-            rec_scores = rec_res["score"][i]
+            rec_scores = rec_res["score"][i].tolist()
             labels = rec_res["label"][i]
             single_img_res["boxes"].append(
                 {