【BUG】mmdetection ValueError: need at least one array to concatenate

问题: 使用mmdetection框架使用COCO格式训练自定义数据集时出现如下错误:

ValueError: need at least one array to concatenate

解决方法: 修改mmdet/datasets/coco.py文件,将CocoDataset类中的METAINFO修改为自己数据集的类别信息,具体如下:

    # METAINFO = {
    #     'classes':
    #     ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
    #      'truck', 'boat', 'traffic light', 'fire hydrant', 'stop sign',
    #      'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
    #      'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
    #      'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
    #      'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard',
    #      'surfboard', 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork',
    #      'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
    #      'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair',
    #      'couch', 'potted plant', 'bed', 'dining table', 'toilet', 'tv',
    #      'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', 'microwave',
    #      'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
    #      'scissors', 'teddy bear', 'hair drier', 'toothbrush'),
    #     # palette is a list of color tuples, which is used for visualization.
    #     'palette':
    #     [(220, 20, 60), (119, 11, 32), (0, 0, 142), (0, 0, 230), (106, 0, 228),
    #      (0, 60, 100), (0, 80, 100), (0, 0, 70), (0, 0, 192), (250, 170, 30),
    #      (100, 170, 30), (220, 220, 0), (175, 116, 175), (250, 0, 30),
    #      (165, 42, 42), (255, 77, 255), (0, 226, 252), (182, 182, 255),
    #      (0, 82, 0), (120, 166, 157), (110, 76, 0), (174, 57, 255),
    #      (199, 100, 0), (72, 0, 118), (255, 179, 240), (0, 125, 92),
    #      (209, 0, 151), (188, 208, 182), (0, 220, 176), (255, 99, 164),
    #      (92, 0, 73), (133, 129, 255), (78, 180, 255), (0, 228, 0),
    #      (174, 255, 243), (45, 89, 255), (134, 134, 103), (145, 148, 174),
    #      (255, 208, 186), (197, 226, 255), (171, 134, 1), (109, 63, 54),
    #      (207, 138, 255), (151, 0, 95), (9, 80, 61), (84, 105, 51),
    #      (74, 65, 105), (166, 196, 102), (208, 195, 210), (255, 109, 65),
    #      (0, 143, 149), (179, 0, 194), (209, 99, 106), (5, 121, 0),
    #      (227, 255, 205), (147, 186, 208), (153, 69, 1), (3, 95, 161),
    #      (163, 255, 0), (119, 0, 170), (0, 182, 199), (0, 165, 120),
    #      (183, 130, 88), (95, 32, 0), (130, 114, 135), (110, 129, 133),
    #      (166, 74, 118), (219, 142, 185), (79, 210, 114), (178, 90, 62),
    #      (65, 70, 15), (127, 167, 115), (59, 105, 106), (142, 108, 45),
    #      (196, 172, 0), (95, 54, 80), (128, 76, 255), (201, 57, 1),
    #      (246, 0, 122), (191, 162, 208)]
    # }
    METAINFO = {
        'classes':
            ('class_1', 'class_2', 'class_3', 'class_4', 'class_5'),
        # palette is a list of color tuples, which is used for visualization.
        'palette':
            [(220, 20, 60), (119, 11, 32), (0, 0, 142), (0, 0, 230), (106, 0, 228)]
    }
### 关于 `ValueError: need at least one array to concatenate` 的解决方案 该错误通常发生在尝试对空列表或未初始化的数组执行拼接操作时。以下是可能的原因及其对应的解决方法: #### 1. 数据集配置问题 如果自定义的数据集路径不正确或者标注文件为空,则可能导致模型无法加载任何数据样本,从而引发此错误。需确认以下几点: - **数据根目录 (`data_root`) 是否设置正确** 确保指定的路径指向实际存在的训练和验证数据集[^4]。 - **元信息 (`metainfo`) 和标注文件 (`ann_file`) 配置无误** 如果使用 COCO 格式的标注文件,请确保其路径有效并包含有效的 JSON 数据结构[^5]。 ```python train_dataloader = dict( dataset=dict( data_root='path/to/dataset', ann_file='annotations/train.json', # 确认路径存在且非空 metainfo={'classes': ('class1', 'class2')}, # 添加类别的元信息 img_prefix='images/' # 图片前缀路径 ) ) ``` --- #### 2. 类别数量与配置不符 即使修改了 `datasets/coco.py` 或其他相关脚本中的类别数,仍可能出现冲突。建议重新安装依赖库以同步更改后的配置[^3]。 ```bash git clone https://github.com/open-mmlab/mmrotate.git cd mmrotate pip install -v -e . ``` 通过上述方式可以确保本地环境完全适配最新的代码改动。 --- #### 3. 训练命令调整 当切换到新的数据集时,默认的训练脚本可能不再适用。推荐按照官方文档更新启动命令: ```bash bash tools/dist_train.sh configs/my_config.py 8 --cfg-options \ dataset.metainfo.classes="['class1','class2']" ``` 此处参数 `--cfg-options` 动态覆盖默认配置项,避免硬编码带来的潜在风险。 --- #### 4. 调试日志分析 启用调试模式可以帮助定位具体哪一步骤触发异常。例如,在运行过程中增加打印语句来观察输入张量的状态: ```python import numpy as np def debug_concat(*arrays): if not arrays or all(len(a) == 0 for a in arrays): raise ValueError('Need at least one non-empty array to concatenate') result = np.concatenate(arrays) print(f"Concatenated shape: {result.shape}") return result ``` 将此类辅助函数嵌入至可疑区域以便快速排查原因。 --- ### 总结 综合以上分析可知,`ValueError: need at least one array to concatenate` 多半源于数据预处理阶段存在问题或是框架内部状态未能及时刷新所致。逐一核查数据源、重装必要组件以及合理调用接口均有助于彻底消除该隐患。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值