MindSpore调用Dataset.batch()中per_batch_map函数出错
收藏回复举报
MindSpore调用Dataset.batch()中per_batch_map函数出错
发表于2024-10-25 08:43:09
0 查看

1 系统环境

硬件环境(Ascend/GPU/CPU): Ascend/GPU/CPU

MindSpore版本: mindspore=2.2.10

执行模式(PyNative/ Graph):不限

Python版本: Python=3.8

操作系统平台: linux

2 报错信息

2.1 问题描述

当设置了per_batch_map字段,batch_size=1时报错,其中can_collator为:

def can_ collator(batch, BatchInfo):

    """

    batch: [

        image [batch_size, channel, maxHinbatch, maxwinbatch]

        image_mask [batch_size, channel, maxHinbatch, maxwinbatch]

        label [batch_size, maxLabelLen]

        label mask [batch size, maxLabelLen]

        ...

    ]

    """

    max_width, max_height, max_length =0,0,0

    bs, channel = len(batch), batch[0][0].shape[0]

    proper_items = [ ]

    for item in batch:

        if(

                item[0]. shape[1] * max width > 1600 * 320

                or item[0].shape[2] * max_height > 1600 * 320

        ):

            continue

        max_ height =

            item[0].shape[1] if item[ø].shape[1] > max_height else max_height

        )

        max_width = item[0].shape[2] if item[ø].shape[2] > max_width else max_width

        max_length = len(item[1]) if len(item[1]) > max_length else max_length

        proper_items.append(item)

    images = ops.zeros((len(proper_items) , channel, max_height, max_width), dtype=ms. float32

    image_masks = ops.zeros(( len(proper_items), 1, max_height, max_width), dtype=ms.float32

    labels = ops.zeros((len(proper_i tems), max_length), dtype=ms.int64

    label_masks = ops.zeros(( len(proper_items), max_length), dtype=ms. int64)



    for i in range(len(proper_items):

        _, h, w = proper_items[i][0].shape

        images[i][:, :h, :w] = proper_items[i][0]

        image_masks[i][:, :h, :w] = 1

        l = len(proper_items[i][1])

        labels[i][:1] = proper_items[i][1]

        label_masks[i][:1] = 1

    return images, image_masks, labels, label_masks

2.2 脚本信息

dataloader = ds.batch(

    batch_size,

    drop_remainder=drop_remainder,

    num_parallel_workers=min(

        num_workers, 2

    ), # set small workers for lite computation. ToDo: increase for batch-wise mapping

    # input_columns=["image","label"],

    output_columns=["images", "image_masks", "labels", "label_masks"],

    per_batch_map=can_collator , # uncomment to use inner-batch transformation

)



return dataloader

2.3 报错信息

size=8

col_name-['images', 'image_masks', 'labels', 'label_masks']

Traceback (most recent call last):

    File "/home/ma-user/work/ prtest/mindocr/tests/ut/test_can_dataset.py", line 111, in <module>

        for data in data_ loader.create_dict_iterator():

    File "/home/ma-user/ anaconda3/envs/Mindspore/lib/python3.7/site-packages/mindspore/dataset/engine/iterators.py", line 145, in __next_.

        data = self._get_next()

    File "/home/ma-user/anaconda3/envs/Mindspore/lib/python3.7/site-packages/mindspore/dataset/engine/ iterators .py", line 270, in _get_next

        raise err

    File "/home/ma-user/anaconda3/envs/Mindspore/1ib/python3.7/site-packages/mindspore/dataset/engine/iterators .py", line 253, in _get_next

        return {k: self._transform md _to_output(t) for k, t in self._iterator •GetNextAsMap(). items()}

RuntimeError: Exception thrown from user defined Python function in dataset.



Python Call stack:



TypeError: Traceback (most recent call last):

    File "/home/ma-user/anaconda3/envs/Mindspore/1ib/python3.7/site-packages/mindspore/dataset/transforms/py_transforms_util.py", line 198, in __cal1

        result = self.transform(*args)

TypeError: can_ collator() takes2 positional arguments but 3were given



Dataset Pipeline Error Message:

[ERROR] Execute user Python code failed, check 'Python Cal1 stack' above.

3 根因分析

******此处由用户填写******

4 解决方案

******此处由用户填写******

包含文字方案和最终脚本代码

请将正确的脚本打包并上传附件

我要发帖子