Emergency plz help me I am getting folowing : error index 1281 is out of bounds for dimension 0 with size 7

Sanket_Dhuri: <pre><code class="lang-auto">def __getitem__(self, index):
        label_path = os.path.join(self.label_dir, self.annotations.iloc[index, 1])
        boxes = []
        class_lable=0
        with open(label_path) as f:
            for label in f.readlines():
                items=label.replace("\n", "").split()
                class_lable=0
                for i in range(len(self.class_list)):
                    if items[0] == self.class_list[i]:
                        class_lable=i
                        break
                x = (float(items[6])+ float(items[4]))//(2)
                y = (float(items[7])+float(items[5]))//(2)
                width = float(items[6]) - float(items[4])
                height = float(items[7]) - float(items[5])
                boxes.append([class_lable, x, y, width, height])

        img_path = os.path.join(self.img_dir, self.annotations.iloc[index, 0])
        image = Image.open(img_path)
        boxes = torch.tensor(boxes)

        if self.transform:
            # image = self.transform(image)
            image, boxes = self.transform(image, boxes)

        # Convert To Cells
        label_matrix = torch.zeros((self.S, self.S, self.C + 5 * self.B))
        for box in boxes:
            class_label, x, y, width, height = box.tolist()
            class_label = int(class_label)

            # i,j represents the cell row and cell column
            i, j = int(self.S * y), int(self.S * x)
            x_cell, y_cell = self.S * x - j, self.S * y - i

            """
            Calculating the width and height of cell of bounding box,
            relative to the cell is done by the following, with
            width as the example:
            
            width_pixels = (width*self.image_width)
            cell_pixels = (self.image_width)
            
            Then to find the width relative to the cell is simply:
            width_pixels/cell_pixels, simplification leads to the
            formulas below.
            """
            width_cell, height_cell = (
                width * self.S,
                height * self.S,
            )

            # If no object already found for specific cell i,j
            # Note: This means we restrict to ONE object
            # per cell!
            
            if label_matrix[i, j, 9] == 0:
                # Set that there exists an object
                label_matrix[i, j, 9] = 1

                # Box coordinates
                box_coordinates = torch.tensor(
                    [x_cell, y_cell, width_cell, height_cell]
                )

                label_matrix[i, j, 10:14] = box_coordinates

                # Set one hot encoding for class_label
                label_matrix[i, j, class_label] = 1

        return image, label_matrixdef __getitem__(self, index):
        label_path = os.path.join(self.label_dir, self.annotations.iloc[index, 1])
        boxes = []
        class_lable=0
        with open(label_path) as f:
            for label in f.readlines():
                items=label.replace("\n", "").split()
                class_lable=0
                for i in range(len(self.class_list)):
                    if items[0] == self.class_list[i]:
                        class_lable=i
                        break
                x = (float(items[6])+ float(items[4]))//(2)
                y = (float(items[7])+float(items[5]))//(2)
                width = float(items[6]) - float(items[4])
                height = float(items[7]) - float(items[5])
                boxes.append([class_lable, x, y, width, height])

        img_path = os.path.join(self.img_dir, self.annotations.iloc[index, 0])
        image = Image.open(img_path)
        boxes = torch.tensor(boxes)

        if self.transform:
            # image = self.transform(image)
            image, boxes = self.transform(image, boxes)

        # Convert To Cells
        label_matrix = torch.zeros((self.S, self.S, self.C + 5 * self.B))
        for box in boxes:
            class_label, x, y, width, height = box.tolist()
            class_label = int(class_label)

            # i,j represents the cell row and cell column
            i, j = int(self.S * y), int(self.S * x)
            x_cell, y_cell = self.S * x - j, self.S * y - i

            """
            Calculating the width and height of cell of bounding box,
            relative to the cell is done by the following, with
            width as the example:
            
            width_pixels = (width*self.image_width)
            cell_pixels = (self.image_width)
            
            Then to find the width relative to the cell is simply:
            width_pixels/cell_pixels, simplification leads to the
            formulas below.
            """
            width_cell, height_cell = (
                width * self.S,
                height * self.S,
            )

            # If no object already found for specific cell i,j
            # Note: This means we restrict to ONE object
            # per cell!
            
            if label_matrix[i, j, 9] == 0:
                # Set that there exists an object
                label_matrix[i, j, 9] = 1

                # Box coordinates
                box_coordinates = torch.tensor(
                    [x_cell, y_cell, width_cell, height_cell]
                )

                label_matrix[i, j, 10:14] = box_coordinates

                # Set one hot encoding for class_label
                label_matrix[i, j, class_label] = 1

        return image, label_matrix
</code></pre>
<p>IndexError                                Traceback (most recent call last)<br>
/tmp/ipykernel_34/3223299113.py in <br>
120<br>
121 if <strong>name</strong> == “<strong>main</strong>”:<br>
 → 122     main()</p>
<p>/tmp/ipykernel_34/3223299113.py in main()<br>
98             model,<br>
99             iou_threshold=0.5,<br>
 → 100             threshold=0.4<br>
101         )<br>
102</p>
<p>/tmp/ipykernel_34/4116429681.py in get_bboxes(loader, model, iou_threshold, threshold, pred_format, box_format, device)<br>
14     model.eval()<br>
15     train_idx = 0<br>
—&gt; 16     for batch_idx, (x, labels) in enumerate(loader):<br>
17         # print(x.shape)<br>
18         x = x.to(device)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in <strong>next</strong>(self)<br>
519             if self._sampler_iter is None:<br>
520                 self._reset()<br>
 → 521             data = self._next_data()<br>
522             self._num_yielded += 1<br>
523             if self._dataset_kind == _DatasetKind.Iterable and \</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)<br>
559     def _next_data(self):<br>
560         index = self._next_index()  # may raise StopIteration<br>
 → 561         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration<br>
562         if self._pin_memory:<br>
563             data = _utils.pin_memory.pin_memory(data)</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)<br>
42     def fetch(self, possibly_batched_index):<br>
43         if self.auto_collation:<br>
—&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in (.0)<br>
42     def fetch(self, possibly_batched_index):<br>
43         if self.auto_collation:<br>
—&gt; 44             data = [self.dataset[idx] for idx in possibly_batched_index]<br>
45         else:<br>
46             data = self.dataset[possibly_batched_index]</p>
<p>/tmp/ipykernel_34/1835178109.py in <strong>getitem</strong>(self, index)<br>
73             # per cell!<br>
74<br>
—&gt; 75             if label_matrix[i, j, 9] == 0:<br>
76                 # Set that there exists an object<br>
77                 label_matrix[i, j, 9] = 1</p>
<p>IndexError: index 1281 is out of bounds for dimension 0 with size 7</p>

ptrblck: <p><code>i</code> and <code>j</code> seem to be created here:</p>
<pre><code class="lang-python">i, j = int(self.S * y), int(self.S * x)
</code></pre>
<p>and used to index <code>label_matrix</code>:</p>
<pre><code class="lang-python">if label_matrix[i, j, 9] == 0:
</code></pre>
<p>which is creating the index error, since apparently <code>i</code> has a value of <code>1281</code> while <code>label_matrix</code> has a size of <code>7</code> in <code>dim0</code>.<br>
Make sure <code>i</code> and <code>j</code> contain valid values to index <code>label_matrix</code>.</p>