MindSpore的PyNative模式报错RuntimeError: Exception thrown from user defined Python function in dataset.
收藏回复举报
MindSpore的PyNative模式报错RuntimeError: Exception thrown from user defined Python function in dataset.
发表于2024-04-22 19:14:26
0 查看

1 系统环境

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

MindSpore版本: 2.2.0

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

2 报错信息

2.1 问题描述

使用MindSpore进行影像分割,设置PYNATIVE_MODE模式下,训练部分报错说指针为空。

2.2 脚本信息

class VerSeDataset:
def init(self, raw_images_path, masks_path, images_name):
self.raw_images_path = raw_images_path
self.masks_path = masks_path
self.images_name = images_name

def __len__(self):
    return len(self.images_name)

def __getitem__(self, index):
    img_path = os.path.join(self.raw_images_path, self.images_name[index])
    mask_path = os.path.join(self.masks_path, self.images_name[index])
    image = Image.open(img_path)  # 将图像转换为灰度图像
    mask = Image.open(mask_path)
    w, h = image.size
    image = image.resize((w, h), resample=Image.BICUBIC)
    image = vision.Resize(size=(IMAGE_WIDTH, IMAGE_HEIGHT))(image)
    image_ndarray = np.asarray(image)
    image_ndarray = image_ndarray.reshape(1, image_ndarray.shape[0], image_ndarray.shape[1])
    mask = mask.resize((w, h), resample=Image.NEAREST)
    mask = vision.Resize(size=(IMAGE_WIDTH, IMAGE_HEIGHT))(mask)
    mask_ndarray = np.asarray(mask)
    image = T(image_ndarray.copy()).astype('float32')
    mask = T(mask_ndarray.copy()).astype('float32')
    return image, mask
获取所有images and masks的路径
train_images_paths = os.listdir(processed_train_raw_images)
train_masks_paths = os.listdir(processed_train_masks)
validation_images_paths = os.listdir(processed_validation_raw_images)
validation_masks_paths = os.listdir(processed_validation_masks)

创建MindSpore数据集
train_dataset = ds.GeneratorDataset(
source=VerSeDataset(processed_train_raw_images, processed_train_masks, train_images_paths),
column_names=['image', 'mask'])
valid_dataset = ds.GeneratorDataset(
source=VerSeDataset(processed_validation_raw_images, processed_validation_masks, validation_images_paths),
column_names=['image', 'mask'])

# 创建数据加载器
train_dataset = train_dataset.batch(BATCH_SIZE, drop_remainder=True)
valid_dataset = valid_dataset.batch(BATCH_SIZE, drop_remainder=True)

2.3 报错信息

RuntimeError Traceback (most recent call last)
/tmp/ipykernel_24/3242500885.py in
2 for t in range(1):
3 print(f"Epoch {t + 1}\n-------------------------------")
----> 4 train_loop(model, train_dataset)
5 val_loop(model, valid_dataset, loss_fn)
6 print("Done!")

/tmp/ipykernel_24/1907105328.py in train_loop(model, dataset)
21 size = dataset.get_dataset_size()
22 model.set_train()
---> 23 for batch, (data, label) in enumerate(dataset.create_tuple_iterator()):
24 loss = train_step(data, label)
25 if batch % 16 == 0:

/opt/conda/lib/python3.7/site-packages/mindspore/dataset/engine/iterators.py in next(self)
150
151 # Note offload is applied inside _get_next() if applicable since get_next converts to output format
--> 152 data = self._get_next()
153 if not data:
154 if self.__index == 0:

/opt/conda/lib/python3.7/site-packages/mindspore/dataset/engine/iterators.py in _get_next(self)
299
300 if self.offload_model is None:
--> 301 return [self._transform_md_to_output(t) for t in self._iterator.GetNextAsList()]
302 data = [self._transform_md_to_tensor(t) for t in self._iterator.GetNextAsList()]
303 if data:

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

Python Call Stack:
Traceback (most recent call last):
File "/opt/conda/lib/python3.7/site-packages/mindspore/dataset/engine/datasets_user_defined.py", line 103, in _cpp_sampler_fn
yield _convert_row(val)
File "/opt/conda/lib/python3.7/site-packages/mindspore/dataset/engine/datasets_user_defined.py", line 173, in _convert_row
value.append(x.asnumpy())
File "/opt/conda/lib/python3.7/site-packages/mindspore/common/_stub_tensor.py", line 48, in fun
arg = (stub.stub_sync(),) + arg[1:]
File "/opt/conda/lib/python3.7/site-packages/mindspore/common/stub_tensor.py", line 153, in stub_sync
val = self.stub.get_value()
RuntimeError: The pointer[top_cell] is null.

Framework Unexpected Exception Raised:
This exception is caused by framework's unexpected error. Please create an issue at https://gitee.com/mindspore/mindspore/issues to get help.

C++ Call Stack: (For framework developers)
mindspore/ccsrc/pipeline/pynative/grad/grad.h:70 top_cell

Dataset Pipeline Error Message:
[ERROR] Execute user Python code failed, check 'Python Call Stack' above.

C++ Call Stack: (For framework developers)
mindspore/ccsrc/minddata/dataset/engine/datasetops/source/generator_op.cc(259).

3 根因分析

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

4 解决方案

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

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

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

我要发帖子