#概要
物体検出プログラム作成時、独自データセットの中を見ようとして
(first_datum = train_dataset[0])
エラー。参考サイトのプログラムでは動作したため、データセットの中身がおかしい?とかんがえられます。まったくわからないため教えていただきたいです。よろしくお願いします。
#エラーの出たプログラム
python
1import chainer 2import cupy 3import chainercv 4import matplotlib 5import os 6import xml.etree.ElementTree as ET 7 8import numpy as np 9 10from chainercv.datasets import VOCBboxDataset 11 12 13bccd_labels = ('ROCK', 'MOUNTAIN')#変更箇所 14 15 16class BCCDDataset(VOCBboxDataset): 17 18 def _get_annotations(self, i): 19 id_ = self.ids[i] 20 anno = ET.parse( 21 os.path.join(self.data_dir, 'Annotations', id_ + '.xml')) 22 23 bbox = [] 24 label = [] 25 difficult = [] 26 for obj in anno.findall('object'): 27 bndbox_anno = obj.find('bndbox') 28 29 bbox.append([ 30 int(bndbox_anno.find(tag).text) - 1 31 for tag in ('ymin', 'xmin', 'ymax', 'xmax')]) 32 name = obj.find('name').text.lower().strip() 33 label.append(bccd_labels.index(name)) 34 print(bccd_labels.index(obj.find('name').text.lower().strip())) 35 bbox = np.stack(bbox).astype(np.float32) 36 label = np.stack(label).astype(np.int32) 37 38 difficult = np.array(difficult, dtype=np.bool) 39 return bbox, label, difficult 40train_dataset = BCCDDataset('rock_detect_dataset/BCCD', 'train') 41valid_dataset = BCCDDataset('rock_detect_dataset/BCCD', 'val') 42test_dataset = BCCDDataset('rock_detect_dataset/BCCD', 'test') 43print('Number of images in "train" dataset:', len(train_dataset)) 44print('Number of images in "valid" dataset:', len(valid_dataset)) 45print('Number of images in "test" dataset:', len(test_dataset)) 46first_datum = train_dataset[0]
jupyter
1!pip install chainercv # ChainerCVのインストール 2 3import chainer 4import cupy 5import chainercv 6import matplotlib 7!if [ ! -d BCCD_Dataset ]; then git clone https://github.com/Shenggan/BCCD_Dataset.git; fi 8 9import os 10import xml.etree.ElementTree as ET 11 12import numpy as np 13 14from chainercv.datasets import VOCBboxDataset 15 16 17bccd_labels = ('rbc', 'wbc', 'platelets') 18 19 20class BCCDDataset(VOCBboxDataset): 21 22 def _get_annotations(self, i): 23 id_ = self.ids[i] 24 25 anno = ET.parse( 26 os.path.join(self.data_dir, 'Annotations', id_ + '.xml')) 27 28 bbox = [] 29 label = [] 30 difficult = [] 31 for obj in anno.findall('object'): 32 bndbox_anno = obj.find('bndbox') 33 34 bbox.append([ 35 int(bndbox_anno.find(tag).text) - 1 36 for tag in ('ymin', 'xmin', 'ymax', 'xmax')]) 37 name = obj.find('name').text.lower().strip() 38 label.append(bccd_labels.index(name)) 39 bbox = np.stack(bbox).astype(np.float32) 40 label = np.stack(label).astype(np.int32) 41 42 difficult = np.array(difficult, dtype=np.bool) 43 return bbox, label, difficult 44 45train_dataset = BCCDDataset('BCCD_Dataset/BCCD', 'train') 46valid_dataset = BCCDDataset('BCCD_Dataset/BCCD', 'val') 47test_dataset = BCCDDataset('BCCD_Dataset/BCCD', 'test') 48 49first_datum = train_dataset[0] 50
xml例
<annotation> <folder>JPGImages</folder> <filename>hogehoge.jpg</filename> <path>hogehoge</path> <source> <database>Unknown</database> </source> <size> <width>720</width> <height>1080</height> <depth>3</depth> </size> <segmented>0</segmented> <object> <name>MOUNTAIN</name> <pose>Unspecified</pose> <truncated>0</truncated> <difficult>0</difficult> <bndbox> <xmin>242.6123427801162</xmin> <ymin>279.4234910191407</ymin> <xmax>720</xmax> <ymax>706.8194993286604</ymax> </bndbox> </object> </annotation> # エラーコード ```pythonValueErrorTraceback (most recent call last)
<ipython-input-59-8cd07929f6af> in <module>
----> 1 first_datum = train_dataset[0]
2 print(first_datum[0].shape, first_datum[0].dtype)
/usr/local/lib/python3.6/dist-packages/chainer/dataset/dataset_mixin.py in getitem(self, index)
65 return [self.get_example(i) for i in index]
66 else:
---> 67 return self.get_example(index)
68
69 def len(self):
/usr/local/lib/python3.6/dist-packages/chainercv/chainer_experimental/datasets/sliceable/sliceable_dataset.py in get_example(self, index)
96 if isinstance(self.keys, tuple):
97 return self.get_example_by_keys(
---> 98 index, tuple(range(len(self.keys))))
99 else:
100 return self.get_example_by_keys(index, (0,))[0]
/usr/local/lib/python3.6/dist-packages/chainercv/chainer_experimental/datasets/sliceable/getter_dataset.py in get_example_by_keys(self, index, key_indices)
87 _, getter_index, key_index = self._keys[key_index]
88 if getter_index not in cache:
---> 89 cache[getter_index] = self._gettersgetter_index
90 if key_index is None:
91 example.append(cache[getter_index])
<ipython-input-55-501a46a571b5> in _get_annotations(self, i)
33 for tag in ('ymin', 'xmin', 'ymax', 'xmax')])
34 name = obj.find('name').text.lower().strip()
---> 35 label.append(bccd_labels.index(name))
36 print(bccd_labels.index(obj.find('name').text.lower().strip()))
37 bbox = np.stack(bbox).astype(np.float32)
ValueError: tuple.index(x): x not in tuple
回答1件
あなたの回答
tips
プレビュー