facebookresearch / facebookresearch/detectron2

Please read & provide the following

Open
#4,928 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
34.7k
Forks
7.9k
PR merge metrics
No merged PRs in 30d

Description

## Instructions To Reproduce the 🐛 Bug:
1. Full runnable code or full changes you made:
```
custom dataset that I loaded in a json file according to COCO annotations and I am trying to train a detectron2 model but i keep getting the same issue with the indexes.. I believe this is related to how i write my annotation file. I have put a sample of the generated json...similar issue reported on #4115

I've tried annotating using via and exporting as coco format, as well as labelme and converted using labelme2COCO

```
2. What exact command you run:
def get_pcb_dicts(img_dir):
json_file = os.path.join(img_dir, "via_region_data.json")
with open(json_file) as f:
imgs_anns = json.load(f)

dataset_dicts = []
for idx, v in enumerate(imgs_anns.values()):
record = {}

filename = os.path.join(img_dir, v["filename"])

height, width = cv2.imread(filename).shape[:2]

record["file_name"] = filename
record["image_id"] = idx
record["height"] = height
record["width"] = width

annos = v["regions"]
objs = []
for _, anno in annos.items():
assert not anno["region_attributes"]
anno = anno["shape_attributes"]
px = anno["all_points_x"]
py = anno["all_points_y"]
poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
poly = [p for x in poly for p in x]

obj = {
"bbox": [np.min(px), np.min(py), np.max(px), np.max(py)],
"bbox_mode": BoxMode.XYXY_ABS,
"segmentation": [poly],
"category_id": 0,
}
objs.append(obj)
record["annotations"] = objs
dataset_dicts.append(record)
return dataset_dicts

for d in ["train", "val"]:
DatasetCatalog.register("pcb10_" + d, lambda d=d: get_pcb_dicts("Final/train/" + d))
MetadataCatalog.get("pcb10_" + d).set(thing_classes=["IC"])

pcb_metadata = MetadataCatalog.get("pcb10_train")

import matplotlib.pyplot as plt
dataset_dicts = get_pcb_dicts("New1/train/")
for d in random.sample(dataset_dicts, 3):
img = cv2.imread(d["file_name"])
visualizer = Visualizer(img[:, :, ::-1], metadata=balloon_metadata, scale=0.5)
out = visualizer.draw_dataset_dict(d)
cv2_imshow(out.get_image()[:, :, ::-1])

4. __Full logs__ or other relevant observations:
```
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[8], line 6
1 #OOOLLLDDDD
2
3 # To verify the data loading is correct, let's visualize the annotations of randomly selected samples in the training set:
5 import matplotlib.pyplot as plt
----> 6 dataset_dicts = get_pcb_dicts("New1/train/")
7 for d in random.sample(dataset_dicts, 3):
8 img = cv2.imread(d["file_name"])

Cell In[3], line 12, in get_pcb_dicts(img_dir)
9 for idx, v in enumerate(imgs_anns.values()):
10 record = {}
---> 12 filename = os.path.join(img_dir, v["filename"])
16 height, width = cv2.imread(filename).shape[:2]
18 record["file_name"] = filename

TypeError: list indices must be integers or slices, not str
```
5. please simplify the steps as much as possible so they do not require additional resources to
run, such as a private dataset.
Here's my .json file

{"images":[{"height":3280,"width":4928,"id":1,"file_name":"0.jpg"},{"height":3280,"width":4928,"id":2,"file_name":"5.jpg"}],"annotations":[{"iscrowd":0,"image_id":1,"bbox":[2456.8571428571427,1470.9523809523807,342.85714285714266,319.04761904761926],"segmentation":[[2456,1470,2799,1480,2794,1775,2461,1790]],"category_id":0,"id":1,"area":103843},{"iscrowd":0,"image_id":1,"bbox":[3056.8571428571427,1466.1904761904761,195.23809523809496,242.8571428571429],"segmentation":[[3056,1475,3252,1466,3237,1694,3071,1709]],"category_id":0,"id":2,"area":41791},{"iscrowd":0,"image_id":1,"bbox":[2833.047619047619,1799.5238095238094,214.28571428571422,171.42857142857156],"segmentation":[[2833,1804,3047,1799,3004,1970,2833,1970]],"category_id":0,"id":3,"area":32551},{"iscrowd":0,"image_id":1,"bbox":[2547.333333333333,1218.5714285714284,157.14285714285688,228.57142857142867],"segmentation":[[2571,1218,2704,1218,2699,1447,2547,1447]],"category_id":0,"id":4,"area":32653},{"iscrowd":0,"image_id":1,"bbox":[3013.9999999999995,1032.8571428571427,228.5714285714289,247.6190476190477],"segmentation":[[3056,1032,3242,1047,3228,1280,3013,1266]],"category_id":0,"id":5,"area":47074},{"iscrowd":0,"image_id":1,"bbox":[2390.1904761904757,1237.6190476190475,147.6190476190477,200.0],"segmentation":[[2390,1242,2537,1237,2523,1437,2418,1437]],"category_id":0,"id":6,"area":24954},{"iscrowd":0,"image_id":2,"bbox":[1723.5238095238094,1713.8095238095236,166.66666666666674,128.57142857142867],"segmentation":[[1723,1742,1837,1713,1890,1799,1756,1842]],"category_id":0,"id":7,"area":13027}],"categories":[{"id":0,"name":"IC","supercategory":"IC"}]}

## Expected behavior:

If there are no obvious error in "full logs" provided above,
please tell us the expected behavior.

## Environment: Linux
---------------------- ----------------------------------------------------------------------------------------------------------------
sys.platform linux
Python 3.9.16 | packaged by conda-forge | (main, Feb 1 2023, 21:39:03) [GCC 11.3.0]
numpy 1.23.5
detectron2 0.6 @/ihome/hban/php13/.conda/envs/detectron/lib/python3.9/site-packages/detectron2
Compiler GCC 7.3
CUDA compiler CUDA 11.3
detectron2 arch flags /ihome/hban/php13/.conda/envs/detectron/lib/python3.9/site-packages/detectron2/_C.cpython-39-x86_64-linux-gnu.so
DETECTRON2_ENV_MODULE
PyTorch 1.10.0 @/ihome/hban/php13/.conda/envs/detectron/lib/python3.9/site-packages/torch
PyTorch debug build False
GPU available Yes
GPU 0 NVIDIA A100-PCIE-40GB (arch=8.0)
Driver version 515.65.01
CUDA_HOME None - invalid!
Pillow 9.4.0
torchvision 0.11.0 @/ihome/hban/php13/.conda/envs/detectron/lib/python3.9/site-packages/torchvision
torchvision arch flags /ihome/hban/php13/.conda/envs/detectron/lib/python3.9/site-packages/torchvision/_C.so
fvcore 0.1.5.post20221221
iopath 0.1.9
cv2 4.7.0
---------------------- ----------------------------------------------------------------------------------------------------------------
PyTorch built with:
- GCC 7.3
- C++ Version: 201402
- Intel(R) oneAPI Math Kernel Library Version 2021.4-Product Build 20210904 for Intel(R) 64 architecture applications
- Intel(R) MKL-DNN v2.2.3 (Git Hash 7336ca9f055cf1bfa13efb658fe15dc9b41f0740)
- OpenMP 201511 (a.k.a. OpenMP 4.5)
- LAPACK is enabled (usually provided by MKL)
- NNPACK is enabled
- CPU capability usage: AVX2
- CUDA Runtime 11.3
- 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_75,code=sm_75;-gencode;arch=compute_80,code=sm_80;-gencode;arch=compute_86,code=sm_86;-gencode;arch=compute_37,code=compute_37
- CuDNN 8.2
- Magma 2.5.2
- Build settings: BLAS_INFO=mkl, BUILD_TYPE=Release, CUDA_VERSION=11.3, CUDNN_VERSION=8.2.0, CXX_COMPILER=/opt/rh/devtoolset-7/root/usr/bin/c++, CXX_FLAGS= -Wno-deprecated -fvisibility-inlines-hidden -DUSE_PTHREADPOOL -fopenmp -DNDEBUG -DUSE_KINETO -DUSE_FBGEMM -DUSE_QNNPACK -DUSE_PYTORCH_QNNPACK -DUSE_XNNPACK -DSYMBOLICATE_MOBILE_DEBUG_HANDLE -DEDGE_PROFILER_USE_KINETO -O2 -fPIC -Wno-narrowing -Wall -Wextra -Werror=return-type -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-unused-local-typedefs -Wno-strict-overflow -Wno-strict-aliasing -Wno-error=deprecated-declarations -Wno-stringop-overflow -Wno-psabi -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 -Werror=format -Wno-stringop-overflow, LAPACK_INFO=mkl, PERF_WITH_AVX=1, PERF_WITH_AVX2=1, PERF_WITH_AVX512=1, TORCH_VERSION=1.10.0, USE_CUDA=ON, USE_CUDNN=ON, 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,

If your issue looks like an installation issue / environment issue,
please first try to solve it yourself with the instructions in
https://detectron2.readthedocs.io/tutorials/install.html#common-installation-issues

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.