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

def __getitem__打印报错 #3284

Open
wstchh opened this issue Oct 29, 2024 · 4 comments
Open

def __getitem__打印报错 #3284

wstchh opened this issue Oct 29, 2024 · 4 comments
Assignees

Comments

@wstchh
Copy link

wstchh commented Oct 29, 2024

class VeriWild(Dataset):
    """Dataset for Vehicle and other similar data structure, such as VeRI-Wild, SOP, Inshop...
    Args:
        image_root (str): image root
        cls_label_path (str): path to annotation file
        transform_ops (List[Callable], optional): list of transform op(s). Defaults to None.
        backend (str, optional): pil or cv2. Defaults to "cv2".
        relabel (bool, optional): whether do relabel when original label do not starts from 0 or are discontinuous. Defaults to False.
    """
    def __init__(self, image_root, cls_label_path, transform_ops=None, backend="cv2", relabel=False):
        self._img_root = image_root
        self._cls_path = cls_label_path
        if transform_ops:
            self._transform_ops = create_operators(transform_ops)
        self.backend = backend
        self._dtype = paddle.get_default_dtype()
        self._load_anno(relabel)
        
    def _load_anno(self, relabel):
        assert os.path.exists(self._cls_path), f"path {self._cls_path} does not exist."
        assert os.path.exists(self._img_root), f"path {self._img_root} does not exist."
        self.images = []
        self.labels = []
        self.cameras = []
        # 读取train.txt文件
        with open(self._cls_path) as fd:
            lines = fd.readlines()
            if relabel:
                label_set = set()
                for line in lines:
                    line = line.strip().split(",")
                    label_set.add(np.int64(line[1]))
                # 新-旧标签的映射
                label_map = {
                    oldlabel: newlabel
                    for newlabel, oldlabel in enumerate(label_set)
                }
            
            # 对于每一行进行遍历
            for line in lines:
                line = line.strip().split(",")
                # 图片img路径
                self.images.append(os.path.join(self._img_root, line[0]))
                # 标签label
                if relabel:
                    self.labels.append(label_map[np.int64(line[1])])
                else:
                    self.labels.append(np.int64(line[1]))
                # 图片id
                if len(line) >= 3:
                    self.cameras.append(np.int64(line[2]))
                # os.path.exists判断文件或文件夹是否存在
                assert os.path.exists(self.images[-1]), f"path {self.images[-1]} does not exist."

        self.has_camera = len(self.cameras) > 0

    def __getitem__(self, idx):
        try:
            if self.backend == "cv2":
                with open(self.images[idx], 'rb') as f:
                    img = f.read()
            else:
                img = Image.open(self.images[idx]).convert("RGB")
            # 执行数据增强操作
            if self._transform_ops:
                img = transform(img, self._transform_ops)
            # H,W,C-->C,H,W
            if self.backend == "cv2":
                img = img.transpose((2, 0, 1))
            if self.has_camera:
                return (img, self.labels[idx], self.cameras[idx])
            else:
                return (img, self.labels[idx])
        except Exception as ex:
            logger.error("Exception occured when parse line: {} with msg: {}".format(self.images[idx], ex))
            # 随机选择一个idx继续执行
            rnd_idx = np.random.randint(self.__len__())
            return self.__getitem__(rnd_idx)

    def __len__(self):
        return len(self.images)

    @property
    def class_num(self):
        return len(set(self.labels))

init和len函数里面都能打印,getitem打印不出来,很奇怪啊

@wstchh
Copy link
Author

wstchh commented Oct 29, 2024

@wstchh
Copy link
Author

wstchh commented Oct 29, 2024

@liuhongen1234567

@cuicheng01
Copy link
Collaborator

方便详细提供下复现流程吗?

@wstchh
Copy link
Author

wstchh commented Nov 5, 2024

方便详细提供下复现流程吗?

已经解决了,谢谢

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

3 participants