类型错误:“函数”使用 Joblib 加速代码时对象不可迭代

发布于 2025-01-09 20:55:28 字数 3179 浏览 5 评论 0原文

我想使用 joblib 来加速我的预测转换函数,其中我提供图像数组 output[i][j] 作为函数的输入,并通过索引 i 和 j 引用它们。当我收到预测并申请转换时,收到错误 TypeError: 'function' object is not iterable 。它是否与函数本身有关,或者我是否需要重写函数的并行化?

    object_prediction_list = []
    index_dict = {}
    for i in range(len(outputs)):
      for j in range(len(outputs[i])):
        index_dict.update({i : j})

    with parallel_backend("loky", inner_max_num_threads=2):
      prediction =  Parallel(n_jobs=4)(delayed((convert_prediction)(outputs[i][j],
                                        batches[i][j],
                                        original_shape,
                                        shifts[i][j],
                                        d_model_threshold) for i, j in index_dict.items()))
      if prediction:
          object_prediction_list.extend(prediction)

    object_prediction_list = postprocess(object_prediction_list)
result = custom_sliced_prediction(
    "/content/train_segmentation/images/100_0.JPG",
    d_model,
    slice_height = 512,
    slice_width = 512,
    overlap_height_ratio = 0.2,
    overlap_width_ratio = 0.2,
    postprocess_match_metric = "IOS",
    postprocess_match_threshold = 0.3,
    d_model_threshold = cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST,
    num_batch=8
)
/usr/local/lib/python3.7/dist-packages/joblib/parallel.py in __call__(self, iterable)
   1003         if hasattr(self._backend, 'start_call'):
   1004             self._backend.start_call()
-> 1005         iterator = iter(iterable)
   1006         pre_dispatch = self.pre_dispatch
   1007 

TypeError: 'function' object is not iterable
def convert_prediction(prediction, image, full_shape, shift_amount, confidence_threshold, category_mapping={"0": "text"}):
# parse boxes, masks, scores, category_ids from predictions
    boxes = prediction["instances"].pred_boxes.tensor.tolist()
    scores = prediction["instances"].scores.tolist()
    category_ids = prediction["instances"].pred_classes.tolist()
    try:
        masks = prediction["instances"].pred_masks.tolist()
    except AttributeError:
        masks = None

    # create object_prediction_list
    object_prediction_list_per_image = []
    object_prediction_list = []

    for ind in range(len(boxes)):
        score = scores[ind]
        if score < confidence_threshold:
            continue

        category_id = category_ids[ind]

        if masks is None:
            bbox = boxes[ind]
            mask = None
        else:
            mask = np.array(masks[ind])

            # check if mask is valid
            if get_bbox_from_bool_mask(mask) is not None:
                bbox = None
            else:
                continue

        object_prediction = ObjectPrediction(
            bbox=bbox,
            bool_mask=mask,
            category_id=category_id,
            category_name=category_mapping[str(category_id)],
            shift_amount=shift_amount,
            score=score,
            full_shape=full_shape,
        )
        if object_prediction:
            object_prediction_list.append(object_prediction.get_shifted_object_prediction())

    return object_prediction_list

I want to speed up my predict conversion function using joblib where I supply an array of images output[i][j] as input to the function and refer to them by indices i and j. When I receive predicts and apply for conversion, I get an error TypeError: 'function' object is not iterable . Is it somehow related to the function itself, or do I need to rewrite the parallelization of the function?

    object_prediction_list = []
    index_dict = {}
    for i in range(len(outputs)):
      for j in range(len(outputs[i])):
        index_dict.update({i : j})

    with parallel_backend("loky", inner_max_num_threads=2):
      prediction =  Parallel(n_jobs=4)(delayed((convert_prediction)(outputs[i][j],
                                        batches[i][j],
                                        original_shape,
                                        shifts[i][j],
                                        d_model_threshold) for i, j in index_dict.items()))
      if prediction:
          object_prediction_list.extend(prediction)

    object_prediction_list = postprocess(object_prediction_list)
result = custom_sliced_prediction(
    "/content/train_segmentation/images/100_0.JPG",
    d_model,
    slice_height = 512,
    slice_width = 512,
    overlap_height_ratio = 0.2,
    overlap_width_ratio = 0.2,
    postprocess_match_metric = "IOS",
    postprocess_match_threshold = 0.3,
    d_model_threshold = cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST,
    num_batch=8
)
/usr/local/lib/python3.7/dist-packages/joblib/parallel.py in __call__(self, iterable)
   1003         if hasattr(self._backend, 'start_call'):
   1004             self._backend.start_call()
-> 1005         iterator = iter(iterable)
   1006         pre_dispatch = self.pre_dispatch
   1007 

TypeError: 'function' object is not iterable
def convert_prediction(prediction, image, full_shape, shift_amount, confidence_threshold, category_mapping={"0": "text"}):
# parse boxes, masks, scores, category_ids from predictions
    boxes = prediction["instances"].pred_boxes.tensor.tolist()
    scores = prediction["instances"].scores.tolist()
    category_ids = prediction["instances"].pred_classes.tolist()
    try:
        masks = prediction["instances"].pred_masks.tolist()
    except AttributeError:
        masks = None

    # create object_prediction_list
    object_prediction_list_per_image = []
    object_prediction_list = []

    for ind in range(len(boxes)):
        score = scores[ind]
        if score < confidence_threshold:
            continue

        category_id = category_ids[ind]

        if masks is None:
            bbox = boxes[ind]
            mask = None
        else:
            mask = np.array(masks[ind])

            # check if mask is valid
            if get_bbox_from_bool_mask(mask) is not None:
                bbox = None
            else:
                continue

        object_prediction = ObjectPrediction(
            bbox=bbox,
            bool_mask=mask,
            category_id=category_id,
            category_name=category_mapping[str(category_id)],
            shift_amount=shift_amount,
            score=score,
            full_shape=full_shape,
        )
        if object_prediction:
            object_prediction_list.append(object_prediction.get_shifted_object_prediction())

    return object_prediction_list

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

送君千里 2025-01-16 20:55:28

我认为您在如何设置要并行执行的函数方面存在错误。语法为 Parallel(n_jobs=4)(delayed(function_name)(*args, **kwargs))。所以对你来说它应该读作

prediction =  Parallel(n_jobs=4)(delayed(convert_prediction)(outputs[i][j],
                                        batches[i][j],
                                        original_shape,
                                        shifts[i][j],
                                        d_model_threshold) for i, j in index_dict.items())

I think you have in error in how you setup the functions to be executed in parallel. The syntax is Parallel(n_jobs=4)(delayed(function_name)(*args, **kwargs)). So for you it should read as

prediction =  Parallel(n_jobs=4)(delayed(convert_prediction)(outputs[i][j],
                                        batches[i][j],
                                        original_shape,
                                        shifts[i][j],
                                        d_model_threshold) for i, j in index_dict.items())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文