- mySSL - Failed after 0:00:05! Traceback (most recent calls WITHOUT Sacred inte...

2025年09月30日 15:34 响应: 14.48秒 状态: success

🚨 错误信息

train_SABS_Superpix_lbgroup0_scale_MIDDLE_vfold0 WARNING - root - Changed type of config entry "min_fg_data" from str to int INFO - mySSL - Running command 'main' INFO - mySSL - Started run with ID "6" INFO - main - ###### Create model ###### ###### NETWORK: Using ms-coco initialization ###### INFO - main - ###### Load data ###### INFO - main - ###### Labels excluded in training : [2, 3] ###### INFO - main - ###### Unseen labels evaluated in testing: [2, 3] ###### ###### Dataset: the following classes has been excluded [2, 3]###### ERROR - mySSL - Failed after 0:00:05! Traceback (most recent calls WITHOUT Sacred internals): File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 105, in __getitem__ return super().__getitem__(index % len(self)) ZeroDivisionError: integer division or modulo by zero During handling of the above exception, another exception occurred: Traceback (most recent calls WITHOUT Sacred internals): File "training.py", line 82, in main tr_parent = SuperpixelDataset( # base dataset File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/dataloaders/GenericSuperDatasetv2.py", line 67, in __init__ self.scan_ids = self.get_scanids(mode, idx_split) # patient ids of the entire fold File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/dataloaders/GenericSuperDatasetv2.py", line 98, in get_scanids val_ids = copy.deepcopy(self.img_pids[self.sep[idx_split]: self.sep[idx_split + 1] + self.nsup]) File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 101, in __getitem__ return [self[x] for x in self._rangeify(x)] File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 101, in <listcomp> return [self[x] for x in self._rangeify(x)] File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 107, in __getitem__ raise IndexError('list index out of range') IndexError: list index out of range root@autodl-container-008d42af78-93b315fe:~/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master# bash train_ssl_abdominal_ct.sh=================================== train_SABS_Superpix_lbgroup0_scale_MIDDLE_vfold0 WARNING - root - Changed type of config entry "min_fg_data" from str to int INFO - mySSL - Running command 'main' INFO - mySSL - Started run with ID "7" INFO - main - ###### Create model ###### ###### NETWORK: Using ms-coco initialization ###### INFO - main - ###### Load data ###### INFO - main - ###### Labels excluded in training : [2, 3] ###### INFO - main - ###### Unseen labels evaluated in testing: [2, 3] ###### ###### Dataset: the following classes has been excluded [2, 3]###### ERROR - mySSL - Failed after 0:00:05! Traceback (most recent calls WITHOUT Sacred internals): File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 105, in __getitem__ return super().__getitem__(index % len(self)) ZeroDivisionError: integer division or modulo by zero During handling of the above exception, another exception occurred: Traceback (most recent calls WITHOUT Sacred internals): File "training.py", line 82, in main tr_parent = SuperpixelDataset( # base dataset File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/dataloaders/GenericSuperDatasetv2.py", line 67, in __init__ self.scan_ids = self.get_scanids(mode, idx_split) # patient ids of the entire fold File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/dataloaders/GenericSuperDatasetv2.py", line 98, in get_scanids val_ids = copy.deepcopy(self.img_pids[self.sep[idx_split]: self.sep[idx_split + 1] + self.nsup]) File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 101, in __getitem__ return [self[x] for x in self._rangeify(x)] File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 101, in <listcomp> return [self[x] for x in self._rangeify(x)] File "/root/autodl-tmp/Self-supervised-Fewshot-Medical-Image-Segmentation-master/util/utils.py", line 107, in __getitem__ raise IndexError('list index out of range') IndexError: list index out of range

🤖 AI解决方案

ZeroDivisionError: integer division or modulo by zero
ZeroDivisionError: 整数除法或模运算数为零

IndexError: list index out of range
IndexError: 列表索引越界

原因分析

ZeroDivisionError 是由 index % len(self) 引起的,当 len(self) 为 0 时,会导致除零错误。这表明数据集为空或在某些处理环节中导致其长度变为零。紧接着的 IndexError 则是在尝试访问空列表或索引超出范围的列表时发生,这与前一个错误是连锁反应,共同指向了数据加载或处理过程中出现的异常。

解决方案

  • 检查数据集加载逻辑
  • * 定位到 dataloaders/GenericSuperDatasetv2.py 文件中的 __init__ 方法。 * 在 self.scan_ids = self.get_scanids(mode, idx_split) 这一行之前,添加对 self.img_pids 的长度检查。如果 self.img_pids 为空,则需要向上追溯其来源,确保数据文件存在且已被正确加载。 * 在 get_scanids 方法中,检查 self.sep 数组的边界以及 self.sep[idx_split + 1] 是否超出范围。 * 重要:请仔细检查 self.img_pids 的初始化和填充逻辑,确保它包含了预期的数据。如果数据集文件(如图像、标签文件)缺失或格式不正确,可能会导致 self.img_pids 为空。

    示例代码片段(仅为示意,具体修改需根据实际代码结构调整)

        # 在 dataloaders/GenericSuperDatasetv2.py 的 __init__ 方法中
        # ...
        self.scan_ids = self.get_scanids(mode, idx_split) # patient ids of the entire fold
        if not self.img_pids: # 检查 img_pids 是否为空
            raise ValueError("img_pids is empty. Please check data loading and file paths.")
        if not self.sep:
            raise ValueError("self.sep is empty. Please check split configuration.")
        # ...
    
        # 在 dataloaders/GenericSuperDatasetv2.py 的 get_scanids 方法中
        # ...
        if idx_split + 1 >= len(self.sep):
            raise ValueError(f"idx_split ({idx_split}) + 1 is out of bounds for self.sep with length {len(self.sep)}.")
        val_ids  = copy.deepcopy(self.img_pids[self.sep[idx_split]: self.sep[idx_split + 1] + self.nsup])
        # ...
        

  • 检查 util/utils.py 中的 __getitem__ 方法

  • * 在 return super().__getitem__(index % len(self)) 之前,添加一个检查 len(self) 是否大于 0 的条件。
    * 如果 len(self) 为 0,应该直接返回一个空列表或抛出一个更具描述性的错误。

    示例代码片段(仅为示意)

        # 在 util/utils.py 的 __getitem__ 方法中
        # ...
        if len(self) == 0:
            # 或者 return None, [] 等,根据实际需求
            raise ValueError("Dataset is empty, cannot retrieve items.")
        return super().__getitem__(index % len(self))
        # ...
        

    预防措施

  • 输入数据验证:在数据加载的初始阶段,严格检查所有必需的数据文件是否存在、格式是否正确,并确保加载的数据量符合预期。

  • 动态长度检查:在进行任何依赖于数据集长度的操作(如索引、模运算)之前,务必检查数据集的长度是否大于零。