前提
ここに質問の内容を詳しく書いてください。
自分のデータをインスタンス化できず、またdataset[7]などでデータを試したが、入っていない模様。
class MyDataset を定義するところで問題があるが、どこが間違っているのか分からない。
機械学習で画像認識するものを作っています。
データセットを定義するところ、インスタンス化するところ、データセットの中身を確かめるところを実装中に以下のエラーメッセージが発生しました。
実現したいこと
ここに実現したいことを箇条書きで書いてください。
- 動作するようにする
dataset[7]などと実行してきちんと表示させる
発生している問題・エラーメッセージ
エラーメッセージ in __init__(self, csv_path, class_names, img_size, augmentation, min_size) 43 self.random_croppig_base = False 44 ---> 45 _f = open(csv_path, "/content/valid/_annotations.coco.json") 46 _raw = _f.read() 47 _raw = _raw.strip() ValueError: invalid mode: '/content/valid/_annotations.coco.json' #エラーメッセージ2個目 __getitem__ image: 5 {'id': 5, 'license': 1, 'file_name': '67_jpg.rf.6603179f8119953b3da405866ed57d2d.jpg', 'height': 1080, 'width': 1920, 'date_captured': '2022-08-14T02:55:42+00:00'} 67_jpg.rf.6603179f8119953b3da405866ed57d2d.jpg /content/valid/67_jpg.rf.6603179f8119953b3da405866ed57d2d.jpg /content/valid/ "contributor": "", --------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-60-6d95f5d99674> in <module> ----> 1 dataset[5] <ipython-input-57-c85c3c190af9> in __getitem__(self, index) 82 83 print(_row) ---> 84 img_path = _row.split("/content/valid/_annotations.coco.json")[419] 85 anno_path = _row.split("/content/valid/_annotations.coco.json")[694] 86 IndexError: list index out of range #### 該当のソースコード csv_path = "/content/valid/_annotations.coco.json" class_names = [ "Road-Marking", ] cfg_augmentation = { "RANDRESIZE": False, "JITTER": False, "RANDOM_PLACING": False, "HUE": False, "SATURATION": False, "EXPOSURE": False, "LRFLIP": False, "RANDOM_DISTORT": False, } dataset = MyDataset(csv_path=csv_path, class_names=class_names, augmentation=cfg_augmentation) #2個目 dataset[7] # dataset class MyDataset(Dataset): """ dataset class. img_size=416 """ def __init__(self, csv_path="/content/valid/_annotations.coco.json", class_names="Road-Marking", img_size=608, augmentation=None, min_size=1): """ Args: """ if csv_path is "/content/valid/_annotations.coco.json": raise Exception("csv path is not specified.") if class_names is "Road-Marking": raise Exception("please specify class names.") self.max_labels = 30 self.img_size = img_size self.min_size = min_size self.lrflip = augmentation['LRFLIP'] self.jitter = augmentation['JITTER'] self.random_placing = augmentation['RANDOM_PLACING'] self.hue = augmentation['HUE'] self.saturation = augmentation['SATURATION'] self.exposure = augmentation['EXPOSURE'] self.random_distort = augmentation['RANDOM_DISTORT'] self.random_croppig_target = False self.random_croppig_base = False _f = open(csv_path, "/content/valid/_annotations.coco.json") _raw = _f.read() _raw = _raw.strip() _csv_list = _raw.split("\n") self.csv_list = _csv_list self.class_names = class_names self.class_dict = {class_name: i for i, class_name in enumerate(self.class_names)} def __len__(self): print("__getaaa__") #print(self.csv_list) #return len(self.csv_list) return len(image_list) def __getitem__(self, index): print("__getitem__") #print("image: ", image_list[]) print("image: ", index, image_list[index]) image_name = image_list[index]["file_name"] print(image_name) img_path = "/content/valid/" + image_name print(img_path) anno_path = "/content/valid/" print(anno_path) _row = self.csv_list[index] print(_row) img_path = _row.split("/content/valid/_annotations.coco.json")[4] anno_path = _row.split("/content/valid/_annotations.coco.json")[5] lrflip = False # load image and preprocess img = self._read_image(img_path) # load labels boxes, labels = self._get_annotation(anno_path) img, info_img = preprocess(img, self.img_size, jitter=self.jitter, random_placing=self.random_placing) img = np.transpose(img / 255., (2, 0, 1)) # concat class + box label_list = [] for l, b in zip(labels, boxes): # refine box x1 = float(b[0]) y1 = float(b[1]) x2 = float(b[2]) y2 = float(b[3]) # seems _get_annotation convert (x1, y1, w, h) > (x1, y1, x2, y2) b = [x1, y1, x2-x1, y2-y1] if b[2] > self.min_size and b[3] > self.min_size: label_list.append([]) label_list[-1].append(l) label_list[-1].extend(b) labels = label_list padded_labels = np.zeros((self.max_labels, 5)) if len(labels) > 0: labels = np.stack(labels) labels = label2yolobox(labels, info_img, self.img_size, lrflip) padded_labels[range(len(labels))[:self.max_labels]] = labels[:self.max_labels] padded_labels = torch.from_numpy(padded_labels) return img, padded_labels, info_img def _get_annotation(self, annotation_file_path): objects = json.load(codecs.open(annotation_file_path, '/content/valid/_annotations.coco.json', 'utf-8-sig')) objects = objects["annotations"] boxes = [] labels = [] for item in objects: class_name = "Road-Marking" # we're only concerned with clases in our list if class_name in self.class_dict: bbox = item["bbox"] if bbox is not None: x1 = bbox.split("id")[0] y1 = bbox.split("iamge_id")[1] w = bbox.split("category_id")[2] h = bbox.split("area")[3] # VOC dataset format follows Matlab, in which indexes start from 0 # We do not need this here... x1 = float(x1) - 1 y1 = float(y1) - 1 x2 = x1 + float(w) y2 = y1 + float(h) boxes.append([x1, y1, x2, y2]) # you shold append index of category here labels.append(self.class_dict[class_name]) return (np.array(boxes, dtype=np.float32), np.array(labels, dtype=np.int64)) def _read_image(self, image_path): print("image_path: ", image_path) image = cv2.imread(str(image_path)) print("image: ", image.shape) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) return image ```ここに言語名を入力 ソースコード
試したこと
CSV_pathをいじったり、
補足情報(FW/ツールのバージョンなど)
プログラミング初心者ですよろしくおねがいします。
ここにより詳細な情報を記載してください。

下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。
2022/08/29 08:17 編集
2022/08/29 09:09
2022/08/30 11:02
2022/08/30 12:24
2022/08/30 12:40
2022/09/01 01:04
2022/09/01 07:28
2022/09/01 07:29
2022/09/01 07:51 編集
2022/09/02 06:53
2022/09/03 01:41
2022/09/04 12:02