# Buggy code
def cache_labels(self, path=Path("./labels.cache")):
    # Cache dataset labels, check images and read shapes
    x = {}  # dict
    nm, nf, ne, nc = 0, 0, 0, 0  # number missing, found, empty, duplicate
    pbar = tqdm(
        zip(self.img_files, self.label_files),
        desc="Scanning images",
        total=len(self.img_files),
    )
    for i, (im_file, lb_file) in enumerate(pbar):
        try:
            # verify images
            im = Image.open(im_file)
            im.verify()  # PIL verify
            shape = exif_size(im)  # image size
            assert (shape[0] > 9) & (shape[1] > 9), "image size <10 pixels"

            # verify labels
            l = []
            if os.path.isfile(lb_file):
                nf += 1  # label found
                with open(lb_file, "r") as f:
                    l = np.array(
                        [x.split() for x in f.read().splitlines()], dtype=np.float32
                    )  # labels
                if len(l):
                    assert l.shape[1] == 5, "labels require 5 columns each"
                    assert (l >= 0).all(), "negative labels"
                    assert (l[:, 1:] <= 1).all(), (
                        "non-normalized or out of bounds coordinate labels"
                    )
                    assert np.unique(l, axis=0).shape[0] == l.shape[0], (
                        "duplicate labels"
                    )
                else:
                    ne += 1  # label empty
                    l = np.zeros((0, 5), dtype=np.float32)
            else:
                nm += 1  # label missing
            x[im_file] = [l, shape]
        except Exception as e:
            nc += 1
            print(
                "WARNING: Ignoring corrupted image and/or label %s: %s" % (im_file, e)
            )

        pbar.desc = (
            f"Scanning '{path.parent / path.stem}' for images and labels... "
            f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
        )

    if nf == 0:
        print(f"WARNING: No labels found in {path}. See {help_url}")

    x["hash"] = get_hash(self.label_files + self.img_files)
    x["results"] = [nf, nm, ne, nc, i]
    torch.save(x, path)  # save for next time
    logging.info(f"New cache created: {path}")
    return x


# Traceback (ValueError)
Traceback (most recent call last):
File "train.py", line 491, in <module>
train(hyp, opt, device, tb_writer, wandb)
File "train.py", line 186, in train
mlc = np.concatenate(dataset.labels, 0)[:, 0].max()  # max label class
File "<__array_function__ internals>", line 6, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 114824 has 1 dimension(s)

# Fixed code
def cache_labels(self, path=Path("./labels.cache")):
    # Cache dataset labels, check images and read shapes
    x = {}  # dict
    nm, nf, ne, nc = 0, 0, 0, 0  # number missing, found, empty, duplicate
    pbar = tqdm(
        zip(self.img_files, self.label_files),
        desc="Scanning images",
        total=len(self.img_files),
    )
    for i, (im_file, lb_file) in enumerate(pbar):
        try:
            # verify images
            im = Image.open(im_file)
            im.verify()  # PIL verify
            shape = exif_size(im)  # image size
            assert (shape[0] > 9) & (shape[1] > 9), "image size <10 pixels"

            # verify labels
            if os.path.isfile(lb_file):
                nf += 1  # label found
                with open(lb_file, "r") as f:
                    l = np.array(
                        [x.split() for x in f.read().splitlines()], dtype=np.float32
                    )  # labels
                if len(l):
                    assert l.shape[1] == 5, "labels require 5 columns each"
                    assert (l >= 0).all(), "negative labels"
                    assert (l[:, 1:] <= 1).all(), (
                        "non-normalized or out of bounds coordinate labels"
                    )
                    assert np.unique(l, axis=0).shape[0] == l.shape[0], (
                        "duplicate labels"
                    )
                else:
                    ne += 1  # label empty
                    l = np.zeros((0, 5), dtype=np.float32)
            else:
                nm += 1  # label missing
                l = np.zeros((0, 5), dtype=np.float32)
            x[im_file] = [l, shape]
        except Exception as e:
            nc += 1
            print(
                "WARNING: Ignoring corrupted image and/or label %s: %s" % (im_file, e)
            )

        pbar.desc = (
            f"Scanning '{path.parent / path.stem}' for images and labels... "
            f"{nf} found, {nm} missing, {ne} empty, {nc} corrupted"
        )

    if nf == 0:
        print(f"WARNING: No labels found in {path}. See {help_url}")

    x["hash"] = get_hash(self.label_files + self.img_files)
    x["results"] = [nf, nm, ne, nc, i + 1]
    torch.save(x, path)  # save for next time
    logging.info(f"New cache created: {path}")
    return x