Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: need at least one array to concatenate #3628

Closed
jravishankar opened this issue Aug 26, 2020 · 29 comments
Closed

ValueError: need at least one array to concatenate #3628

jravishankar opened this issue Aug 26, 2020 · 29 comments
Assignees

Comments

@jravishankar
Copy link

jravishankar commented Aug 26, 2020

I am having an issue with training after transforming my labeled data into COCO format.
Other threads with the same issue have been closed (e.g. #210), but none of the proposed solutions fix my error.

Error in Question
python tools/train.py ./configs/faster_rcnn/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco.py --work-dir tutorial_exps --resume-from ./checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_bbox_mAP-0.398_20200504_163323-30042637.pth

2020-08-26 14:08:56,855 - mmdet - INFO - load model from: open-mmlab://detectron2/resnet50_caffe
2020-08-26 14:08:56,928 - mmdet - WARNING - The model and loaded state dict do not match exactly

unexpected key in source state_dict: conv1.bias

loading annotations into memory...
Done (t=0.35s)
creating index...
index created!
loading annotations into memory...
Done (t=0.34s)
creating index...
index created!
2020-08-26 14:09:00,231 - mmdet - INFO - load checkpoint from ./checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_bbox_mAP-0.398_20200504_163323-30042637.pth
2020-08-26 14:09:00,349 - mmdet - WARNING - The model and loaded state dict do not match exactly

size mismatch for roi_head.bbox_head.fc_cls.weight: copying a param with shape torch.Size([81, 1024]) from checkpoint, the shape in current model is torch.Size([2, 1024]).
size mismatch for roi_head.bbox_head.fc_cls.bias: copying a param with shape torch.Size([81]) from checkpoint, the shape in current model is torch.Size([2]).
size mismatch for roi_head.bbox_head.fc_reg.weight: copying a param with shape torch.Size([320, 1024]) from checkpoint, the shape in current model is torch.Size([4, 1024]).
size mismatch for roi_head.bbox_head.fc_reg.bias: copying a param with shape torch.Size([320]) from checkpoint, the shape in current model is torch.Size([4]).
2020-08-26 14:09:00,350 - mmdet - INFO - resumed epoch 36, iter 263880
<torch.utils.data.dataloader.DataLoader object at 0x7fa21705a510> load
2020-08-26 14:09:00,350 - mmdet - INFO - Start running, host: ding@ding-ROG-STRIX-Z390-E-GAMING, work_dir: /home/ding/SpineQuant/SpineQuant/mmdetection/tutorial_exps
2020-08-26 14:09:00,350 - mmdet - INFO - workflow: [('train', 1)], max: 100 epochs

Traceback (most recent call last):
File "tools/train.py", line 289, in
main()
File "tools/train.py", line 285, in main
meta=meta)
File "/home/ding/SpineQuant/SpineQuant/mmdetection/mmdet/apis/train.py", line 144, in train_detector
runner.run(data_loaders, cfg.workflow, cfg.total_epochs)
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py", line 122, in run
epoch_runner(data_loaders[i], **kwargs)
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/mmcv/runner/epoch_based_runner.py", line 27, in train
for i, data_batch in enumerate(data_loader):
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 278, in iter
return _MultiProcessingDataLoaderIter(self)
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 709, in init
self._try_put_index()
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 826, in _try_put_index
index = self._next_index()
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/torch/utils/data/dataloader.py", line 318, in _next_index
return next(self._sampler_iter) # may raise StopIteration
File "/home/ding/anaconda3/envs/mmlab/lib/python3.7/site-packages/torch/utils/data/sampler.py", line 200, in iter
for idx in self.sampler:
File "/home/ding/SpineQuant/SpineQuant/mmdetection/mmdet/datasets/samplers/group_sampler.py", line 40, in iter
indices = np.concatenate(indices)
File "<array_function internals>", line 6, in concatenate
ValueError: need at least one array to concatenate

CONFIGS

2020-08-26 14:08:55,796 - mmdet - INFO - Distributed training: False
2020-08-26 14:08:56,589 - mmdet - INFO - Config:
model = dict(
type='FasterRCNN',
pretrained='open-mmlab://detectron2/resnet50_caffe',
backbone=dict(
type='ResNet',
depth=50,
num_stages=4,
out_indices=(0, 1, 2, 3),
frozen_stages=1,
norm_cfg=dict(type='BN', requires_grad=False),
norm_eval=True,
style='caffe'),
neck=dict(
type='FPN',
in_channels=[256, 512, 1024, 2048],
out_channels=256,
num_outs=5),
rpn_head=dict(
type='RPNHead',
in_channels=256,
feat_channels=256,
anchor_generator=dict(
type='AnchorGenerator',
scales=[8],
ratios=[0.5, 1.0, 2.0],
strides=[4, 8, 16, 32, 64]),
bbox_coder=dict(
type='DeltaXYWHBBoxCoder',
target_means=[0.0, 0.0, 0.0, 0.0],
target_stds=[1.0, 1.0, 1.0, 1.0]),
loss_cls=dict(
type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0),
loss_bbox=dict(type='L1Loss', loss_weight=1.0)),
roi_head=dict(
type='StandardRoIHead',
bbox_roi_extractor=dict(
type='SingleRoIExtractor',
roi_layer=dict(type='RoIAlign', output_size=7, sampling_ratio=0),
out_channels=256,
featmap_strides=[4, 8, 16, 32]),
bbox_head=dict(
type='Shared2FCBBoxHead',
in_channels=256,
fc_out_channels=1024,
roi_feat_size=7,
num_classes=1,
bbox_coder=dict(
type='DeltaXYWHBBoxCoder',
target_means=[0.0, 0.0, 0.0, 0.0],
target_stds=[0.1, 0.1, 0.2, 0.2]),
reg_class_agnostic=False,
loss_cls=dict(
type='CrossEntropyLoss', use_sigmoid=False, loss_weight=1.0),
loss_bbox=dict(type='L1Loss', loss_weight=1.0))))
train_cfg = dict(
rpn=dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.7,
neg_iou_thr=0.3,
min_pos_iou=0.3,
match_low_quality=True,
ignore_iof_thr=-1),
sampler=dict(
type='RandomSampler',
num=256,
pos_fraction=0.5,
neg_pos_ub=-1,
add_gt_as_proposals=False),
allowed_border=-1,
pos_weight=-1,
debug=False),
rpn_proposal=dict(
nms_across_levels=False,
nms_pre=2000,
nms_post=1000,
max_num=1000,
nms_thr=0.7,
min_bbox_size=0),
rcnn=dict(
assigner=dict(
type='MaxIoUAssigner',
pos_iou_thr=0.5,
neg_iou_thr=0.5,
min_pos_iou=0.5,
match_low_quality=False,
ignore_iof_thr=-1),
sampler=dict(
type='RandomSampler',
num=512,
pos_fraction=0.25,
neg_pos_ub=-1,
add_gt_as_proposals=True),
pos_weight=-1,
debug=False))
test_cfg = dict(
rpn=dict(
nms_across_levels=False,
nms_pre=1000,
nms_post=1000,
max_num=1000,
nms_thr=0.7,
min_bbox_size=0),
rcnn=dict(
score_thr=0.05,
nms=dict(type='nms', iou_threshold=0.5),
max_per_img=100))
dataset_type = 'CocoDataset'
data_root = '/home/ding/SpineQuant/SpineQuant/mmdetection/'
img_norm_cfg = dict(
mean=[103.53, 116.28, 123.675], std=[1.0, 1.0, 1.0], to_rgb=False)
train_pipeline = [
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True),
dict(
type='Resize',
img_scale=[(1333, 640), (1333, 672), (1333, 704), (1333, 736),
(1333, 768), (1333, 800)],
multiscale_mode='value',
keep_ratio=True),
dict(type='RandomFlip', flip_ratio=0.5),
dict(
type='Normalize',
mean=[103.53, 116.28, 123.675],
std=[1.0, 1.0, 1.0],
to_rgb=False),
dict(type='Pad', size_divisor=32),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels'])
]
test_pipeline = [
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=(1333, 800),
flip=False,
transforms=[
dict(type='Resize', keep_ratio=True),
dict(type='RandomFlip'),
dict(
type='Normalize',
mean=[103.53, 116.28, 123.675],
std=[1.0, 1.0, 1.0],
to_rgb=False),
dict(type='Pad', size_divisor=32),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
])
]
data = dict(
samples_per_gpu=2,
workers_per_gpu=2,
train=dict(
type='CocoDataset',
ann_file='train.json',
img_prefix='images2D',
pipeline=[
dict(type='LoadImageFromFile'),
dict(type='LoadAnnotations', with_bbox=True),
dict(
type='Resize',
img_scale=[(1333, 640), (1333, 672), (1333, 704), (1333, 736),
(1333, 768), (1333, 800)],
multiscale_mode='value',
keep_ratio=True),
dict(type='RandomFlip', flip_ratio=0.5),
dict(
type='Normalize',
mean=[103.53, 116.28, 123.675],
std=[1.0, 1.0, 1.0],
to_rgb=False),
dict(type='Pad', size_divisor=32),
dict(type='DefaultFormatBundle'),
dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels'])
],
data_root='/home/ding/SpineQuant/SpineQuant/mmdetection/training/',
classes=['Spine']),
val=dict(
type='CocoDataset',
ann_file='train.json',
img_prefix='images2D',
pipeline=[
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=(1333, 800),
flip=False,
transforms=[
dict(type='Resize', keep_ratio=True),
dict(type='RandomFlip'),
dict(
type='Normalize',
mean=[103.53, 116.28, 123.675],
std=[1.0, 1.0, 1.0],
to_rgb=False),
dict(type='Pad', size_divisor=32),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
])
],
data_root='/home/ding/SpineQuant/SpineQuant/mmdetection/training/',
classes=['Spine']),
test=dict(
type='CocoDataset',
ann_file='train.json',
img_prefix='images2D',
pipeline=[
dict(type='LoadImageFromFile'),
dict(
type='MultiScaleFlipAug',
img_scale=(1333, 800),
flip=False,
transforms=[
dict(type='Resize', keep_ratio=True),
dict(type='RandomFlip'),
dict(
type='Normalize',
mean=[103.53, 116.28, 123.675],
std=[1.0, 1.0, 1.0],
to_rgb=False),
dict(type='Pad', size_divisor=32),
dict(type='ImageToTensor', keys=['img']),
dict(type='Collect', keys=['img'])
])
],
data_root='/home/ding/SpineQuant/SpineQuant/mmdetection/training/',
classes=['Spine']))
evaluation = dict(interval=10, metric='mAP')
optimizer = dict(type='SGD', lr=0.0025, momentum=0.9, weight_decay=0.0001)
optimizer_config = dict(grad_clip=None)
lr_config = dict(
policy='step',
warmup=None,
warmup_iters=500,
warmup_ratio=0.001,
step=[28, 34])
total_epochs = 100
checkpoint_config = dict(interval=5)
log_config = dict(interval=5, hooks=[dict(type='TextLoggerHook')])
dist_params = dict(backend='nccl')
log_level = 'INFO'
load_from = None
resume_from = './checkpoints/faster_rcnn_r50_caffe_fpn_mstrain_3x_coco_bbox_mAP-0.398_20200504_163323-30042637.pth'
workflow = [('train', 1)]
work_dir = 'tutorial_exps'
gpu_ids = range(0, 1)

Environment Information

2020-08-26 14:08:55,795 - mmdet - INFO - Environment info:

sys.platform: linux
Python: 3.7.7 (default, May 7 2020, 21:25:33) [GCC 7.3.0]
CUDA available: True
CUDA_HOME: /usr/local/cuda-9.2
NVCC: Cuda compilation tools, release 9.2, V9.2.148
GPU 0: GeForce RTX 2080
GCC: gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
PyTorch: 1.3.1
PyTorch compiling details: PyTorch built with:

  • GCC 7.3
  • Intel(R) Math Kernel Library Version 2020.0.1 Product Build 20200208 for Intel(R) 64 architecture applications
  • Intel(R) MKL-DNN v0.20.5 (Git Hash 0125f28c61c1f822fd48570b4c1066f96fcb9b2e)
  • OpenMP 201511 (a.k.a. OpenMP 4.5)
  • NNPACK is enabled
  • CUDA Runtime 9.2
  • NVCC architecture flags: -gencode;arch=compute_37,code=sm_37;-gencode;arch=compute_50,code=sm_50;-gencode;arch=compute_60,code=sm_60;-gencode;arch=compute_61,code=sm_61;-gencode;arch=compute_70,code=sm_70;-gencode;arch=compute_37,code=compute_37
  • CuDNN 7.6.3
  • Magma 2.5.1
  • Build settings: BLAS=MKL, BUILD_NAMEDTENSOR=OFF, BUILD_TYPE=Release, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -fopenmp -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -O2 -fPIC -Wno-narrowing -Wall -Wextra -Wno-missing-field-initializers -Wno-type-limits -Wno-array-bounds -Wno-unknown-pragmas -Wno-sign-compare -Wno-unused-parameter -Wno-unused-variable -Wno-unused-function -Wno-unused-result -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-error=pedantic -Wno-error=redundant-decls -Wno-error=old-style-cast -fdiagnostics-color=always -faligned-new -Wno-unused-but-set-variable -Wno-maybe-uninitialized -fno-math-errno -fno-trapping-math -Wno-stringop-overflow, DISABLE_NUMA=1, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, USE_CUDA=True, USE_EXCEPTION_PTR=1, USE_GFLAGS=OFF, USE_GLOG=OFF, USE_MKL=ON, USE_MKLDNN=ON, USE_MPI=OFF, USE_NCCL=ON, USE_NNPACK=ON, USE_OPENMP=ON, USE_STATIC_DISPATCH=OFF,

TorchVision: 0.4.2
OpenCV: 4.1.1
MMCV: 1.0.5
MMDetection: 2.3.0+ae453fa
MMDetection Compiler: GCC 7.3
MMDetection CUDA Compiler: 9.2

From digging around the files listed in the traceback and printing out values, it looks like the data loaders in tools/train.py aren't collecting the images and annotations from the COCO json file, leaving an empty array for the concatenation. I have followed the instructions here to format the COCO annotations and have verified its validity with other examples online (such as the other threads with this same error), but I have not found a cause for this error. If i use a CustomDataset, I do not receive this error (training doesn't succeed with my custom labels so I tried to switch to COCO for standardized formatting and more solved errors).

Any help or reference is much appreciated!

@v-qjqs
Copy link
Contributor

v-qjqs commented Aug 27, 2020

Hi, concatenation from an empty array will raise this error, and there is size mismatch between your model and loaded state dict (e.g., the number of classes 2 vs 81). Will correcting the size mismatch help?

@jravishankar
Copy link
Author

Thanks for responding so quickly! According to #1151, the size mismatch warning is expected when using pre-trained models, and I have specified the altered classes in the configs. How would I go about correcting the size mismatch while using a pre-trained model?

@WAMAWAMA
Copy link

maybe you need to modify the mmdet/datasets/xxx.py and mmdet/core/evaluation/class_names.py for matching your own dataset👍

@GiangHLe
Copy link

@WAMAWAMA how can I modify class_names.py for my custom dataset, I followed this tutorial and got a bunch of bugs

@xiaotaoFzzf
Copy link

@WAMAWAMA how can I modify class_names.py for my custom dataset, I followed this tutorial and got a bunch of bugs

@WAMAWAMA how can I modify class_names.py for my custom dataset, I followed this tutorial and got a bunch of bugs

I encountered the same problem, did you solve it?

@GiangHLe
Copy link

GiangHLe commented Nov 12, 2020

@xiaotaoFzzf

Add your dataset labels as the new function then add it to dataset_aliases, for example:

def toby_classes():
    return ['husky',''chihuahua','alaska']

dataset_aliases = {
    'voc': ['voc', 'pascal_voc', 'voc07', 'voc12'],
    'imagenet_det': ['det', 'imagenet_det', 'ilsvrc_det'],
    'imagenet_vid': ['vid', 'imagenet_vid', 'ilsvrc_vid'],
    'coco': ['coco', 'mscoco', 'ms_coco'],
    'wider_face': ['WIDERFaceDataset', 'wider_face', 'WDIERFace'],
    'cityscapes': ['cityscapes'],
    'toby': ['toby']
}

Then add it to mmdet/core/evaluation/__intit__.py:

from .class_names import (cityscapes_classes, coco_classes, dataset_aliases,
                          get_classes, imagenet_det_classes,
                          imagenet_vid_classes, voc_classes, toby_classes)
from .eval_hooks import DistEvalHook, EvalHook
from .mean_ap import average_precision, eval_map, print_map_summary
from .recall import (eval_recalls, plot_iou_recall, plot_num_recall,
                     print_recall_summary)

__all__ = [
    'voc_classes', 'imagenet_det_classes', 'imagenet_vid_classes',
    'coco_classes', 'cityscapes_classes', 'dataset_aliases', 'get_classes',
    'DistEvalHook', 'EvalHook', 'average_precision', 'eval_map',
    'print_map_summary', 'eval_recalls', 'print_recall_summary',
    'plot_num_recall', 'plot_iou_recall', 'toby_classes'
]

Then go to `mmdet/core/evaluation/mean_ap.py and add one more line at line 436:

 if dataset is None:
        label_names = [str(i) for i in range(num_classes)]
    elif mmcv.is_str(dataset):
        dataset = 'toby'    # this line
        label_names = get_classes(dataset)
    else:
        label_names = dataset

Note: this is just the temporary solution, I just too busy to deep inside the code, you should better find a more general way.

@v-qjqs v-qjqs closed this as completed Nov 23, 2020
@MeepoAII
Copy link
Contributor

MeepoAII commented Dec 3, 2020

I encountered the same error and solved it, probably because the class name in you annotation file doesn't math the class name in you config file(in your config file it is called 'spine' ) , so please check your annotation file
It's likely that you got the class name wrong when you convert other dataset format to the coco dataset format.

@GiangHLe
Copy link

GiangHLe commented Dec 3, 2020

@MeepoAII I used a custom dataset, so there is no class name in the annotation file. And finally, I can see a guy who committed in this repository appear, where did you guys go, there are a hundred issues wait to solve

@MeepoAII
Copy link
Contributor

MeepoAII commented Dec 3, 2020

emmm, I'm not a member of mmdetection, I'm just a contributor。。。。 btw, PR is welcome~~ @GiangHLe

@Quebradawill
Copy link

@MeepoAII I met the same error, I tried a lot of methods but it didn't work, can you help me?

@Nabyssache
Copy link

I just solve the problem according to Use: python setup.py install
but, I got the nan loss result during training, maybe my datasets or something wrong.

@WeihongM
Copy link

I find the problem.
You need to modify the class name if you use CoCotype dataset.
Modify CocoDataset CLASSES. In my case, I modify CLASSES into ('bb',).

@Nabyssache
Copy link

I find the problem.
You need to modify the class name if you use CoCotype dataset.
Modify CocoDataset CLASSES. In my case, I modify CLASSES into ('bb',).

No,not that way...
I solve the problem, that is nan is arouse by the datasets, my datasets are dirty that cause the loss=nan.


ValueError: need at least one array to concatenate #3628. I found one of the reasons for the possible error: If you are using mmdetection's docker image like me, you should use the command before each training in this training root:
python setup.py install , otherwise you will report this error. The possible reason is that you have quoted the docker image mmdetection instead of mmdetection-master in the current directory.

Similarly, I have summarized several reasons for loss = nan,
1.classnames.py and voc.py/coco.py are inconsistent with the total number of categories in xml files, or you should add a comma at the end, such as classes = ( 'dog','cat', ).
2. mmcv.imread.shape is inconsistent with the shape in xml('width'/'height').
3. bbox out of bound

@GiangHLe
Copy link

GiangHLe commented Jan 4, 2021

@Nabyssache

I just solve the problem according to Use: python setup.py install
but, I got the nan loss result during training, maybe my datasets or something wrong.

You should use pip install -v -e . or python setup.py develop as the document, so when you modify the code directly in mmdet. If you used python setup.py install, it will generate a package in your environment Python's site-packages, which means you have to build it again after changing something inside mmdet

One of the common reason make the loss become Nan is the so high learning rate that makes the divergence problem, reduce the learning rate sometimes can solve the problem.

@Sourav2ch
Copy link

Sourav2ch commented Feb 12, 2021

I tried everything above but noting works , please help me out

@ghost
Copy link

ghost commented Feb 19, 2021

@Sourav2ch One more reason it might happen is if the user do want to use ground truth masks but the config file if used by default might have flags as: with_mask=True or dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks'] in train_pipeline

@muyuuuu
Copy link

muyuuuu commented May 18, 2021

I generate coco format dataset to test my model as follow:

import random
import json

anno = {
    "images": [],
    "annotations": []
}

anno['categories'] = [
    {
        "id": 1,
        "name": "person"
    },
    {
        "id": 2,
        "name": "bicycle"
    }
]

for idx, i in enumerate(range(1), 1):
    cls = random.randint(0, 1)
    color = None
    if cls == 0:
        color = '#FFFFFF'
    else:
        color = "#FF0000"
    im1 = Image.new('RGB', (30, 30), color)     
    im = Image.new('RGB', (50, 50), "#000000")
    x, y = random.randint(0, 10), random.randint(0, 10)
    im.paste(im1, (x, y))
    im.save('data/images/' + '{}.png'.format(i))

    image = {}
    info = {}

    image['id'] = idx
    image['width'] = 50
    image['height'] = 50
    image['file_name'] = '{}.png'.format(i)

    info['id'] = idx
    info["image_id"] = idx
    info["category_id"] = cls + 1
    info["area"] = 900
    info["bbox"] = [x, y, 30, 30]
    info["iscrowd"] = 0
    info["segmentation"] = None

    anno['images'].append(image)
    anno['annotations'].append(info)


with open('data/train.json', 'w') as f:
    json.dump(anno, f, indent=4)

Then I have changed classes info, num_classses and path to coco dataset, etc. But this problem has been arose, how to fix it? I tried everything above but noting works , please help me out.

@muyuuuu
Copy link

muyuuuu commented May 18, 2021

Maybe I got it, the image is too small, when I adjusted it size to (50, 50), this bug has been solved....

liuhuiCNN pushed a commit to liuhuiCNN/mmdetection that referenced this issue May 21, 2021
* refine download sh to py

* update QUICK_STARTED
@banbiossa
Copy link

I had the same error, the issue was my class names in "categories" was balloon but I wrote it as Balloons in the config.
Aligined the 2 and the error went away.

@ZahraAnam
Copy link

@GiangHLe

Add your dataset labels as the new function then add it to dataset_aliases, for example:

def toby_classes():
    return ['husky',''chihuahua','alaska']

dataset_aliases = {
    'voc': ['voc', 'pascal_voc', 'voc07', 'voc12'],
    'imagenet_det': ['det', 'imagenet_det', 'ilsvrc_det'],
    'imagenet_vid': ['vid', 'imagenet_vid', 'ilsvrc_vid'],
    'coco': ['coco', 'mscoco', 'ms_coco'],
    'wider_face': ['WIDERFaceDataset', 'wider_face', 'WDIERFace'],
    'cityscapes': ['cityscapes'],
    'toby': ['toby']
}

Then add it to mmdet/core/evaluation/__intit__.py:

from .class_names import (cityscapes_classes, coco_classes, dataset_aliases,
                          get_classes, imagenet_det_classes,
                          imagenet_vid_classes, voc_classes, toby_classes)
from .eval_hooks import DistEvalHook, EvalHook
from .mean_ap import average_precision, eval_map, print_map_summary
from .recall import (eval_recalls, plot_iou_recall, plot_num_recall,
                     print_recall_summary)

__all__ = [
    'voc_classes', 'imagenet_det_classes', 'imagenet_vid_classes',
    'coco_classes', 'cityscapes_classes', 'dataset_aliases', 'get_classes',
    'DistEvalHook', 'EvalHook', 'average_precision', 'eval_map',
    'print_map_summary', 'eval_recalls', 'print_recall_summary',
    'plot_num_recall', 'plot_iou_recall', 'toby_classes'
]

Then go to `mmdet/core/evaluation/mean_ap.py and add one more line at line 436:

 if dataset is None:
        label_names = [str(i) for i in range(num_classes)]
    elif mmcv.is_str(dataset):
        dataset = 'toby'    # this line
        label_names = get_classes(dataset)
    else:
        label_names = dataset

Note: this is just the temporary solution, I just too busy to deep inside the code, you should better find a more general way.

from .evaluation import *  # noqa: F401, F403

AttributeError: module 'mmdet.core.evaluation' has no attribute 'MyDataset_classes'
I tried this but then I get error

Any idea to solve it?

@AlphonsG
Copy link
Contributor

AlphonsG commented Nov 3, 2022

For anyone still with this issue, it may be because the format of your annotations file is incorrect. In my case, my coco json files did not have an "images" key.

@ryanalexmartin
Copy link

I feel that this would be resolved with better error handling, i.e., "class x present in annotation file, but was not found in dataset_x.py"

@np-n
Copy link

np-n commented Jan 19, 2023

Fixed by modifying classes in mmdet/datasets/<dataset>.py and mmdet/core/evaluation/class_names.py according to my dataset. Thanks, @WAMAWAMA .

@harshinii001
Copy link

i got error like this when i tried to train using my custom dataset

Traceback (most recent call last):
File "/home/ec2-user/mw_mmdetection/tools/train.py", line 262, in
main()
File "/home/ec2-user/mw_mmdetection/tools/train.py", line 250, in main
train_detector(
File "/home/ec2-user/mw_mmdetection/mmdet/apis/train.py", line 257, in train_detector
runner.run(data_loaders, cfg.workflow)
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/mmcv/runner/epoch_based_runner.py", line 136, in run
epoch_runner(data_loaders[i], **kwargs)
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/mmcv/runner/epoch_based_runner.py", line 49, in train
for i, data_batch in enumerate(self.data_loader):
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 435, in iter
return self._get_iterator()
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 381, in _get_iterator
return _MultiProcessingDataLoaderIter(self)
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1072, in init
self._reset(loader, first_iter=True)
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1105, in _reset
self._try_put_index()
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 1339, in _try_put_index
index = self._next_index()
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/dataloader.py", line 618, in _next_index
return next(self._sampler_iter) # may raise StopIteration
File "/opt/conda/envs/pytorch/lib/python3.9/site-packages/torch/utils/data/sampler.py", line 254, in iter
for idx in self.sampler:
File "/home/ec2-user/mw_mmdetection/mmdet/datasets/samplers/group_sampler.py", line 36, in iter
indices = np.concatenate(indices)
File "<array_function internals>", line 180, in concatenate
ValueError: need at least one array to concatenate

@martinurbieta
Copy link

The post by cpwan about issue #9610 helped me to solve " ValueError: need at least one array to concatenate" when running the official custom dataset for mmdet .

He found why

The metainfo has been changed to lowercase since #9469.
On the custom dataset, use lowercase letters for keys in the dictionary instead.
For example, in the jupyter notebook demo, change

cfg.metainfo = {
    'CLASSES': ('balloon', ),
    'PALETTE': [
        (220, 20, 60),
    ]

}

to
cfg.metainfo = {
    'classes': ('balloon', ),
    'palette': [
        (220, 20, 60),
    ]
}

@vahid-pezeshki
Copy link

Thanks, martinurbieta.
You were right, and my problem is solved.

@AIzealotwu
Copy link

I met the same question as you. But the latest version has been changed to lowcase as the comment above. This error still come out.

@okatrusha
Copy link

In my case I missed a metainfo key in config file, per this guide:
https://mmdetection.readthedocs.io/en/latest/user_guides/train.html#prepare-a-config

dataset=dict(
    ...
    metainfo=dict(classes=('my_class',)),

@AIzealotwu
Copy link

Thank you for your reply. Maybe not the case. The image I input to the model was too large. Even using the transform to resize it could not work it out. After resizing it, this problem could be fixed. This model is different from the mask-rcnn which requires less memory. Perhaps this is the case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests