ValueError: Expected target boxes to be a tensorof shape [N, 4], got torch.Size([4])

Tornike: <p>Hi, Doing object detection with F-RCNN.</p>
<p>Faster RCNN requires boxes to be a float tensor of shape [N,4].</p>
<p>Since pandas dataframe is very slow i decided to put all different columns in list like that:</p>
<pre><code class="lang-auto">subxmin=[xmins for xmins in df['xmin']]
subymin=[ymins for ymins in df['ymin']]
subxmax=[xmaxs for xmaxs in df['xmax']]
subymax=[ymaxs for ymaxs in df['ymax']]
train_images=[image for image in df['image']]
bboxes=np.array([list(bbox) for bbox in zip(subxmin, subymin, subxmax,subymax)])
</code></pre>
<p>The dataset is :</p>
<pre><code class="lang-auto">class CarData(torch.utils.data.Dataset):
  def __init__(self,dir,images,boxes,df):
    self.dir=dir
    self.images=images
    self.boxes=bboxes
    self.df=df

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

  
  def __getitem__(self,index):
    id_for_label=self.df[self.df["image"] == self.images[index]]
    img_path=os.path.join(self.dir,self.images[index])
    img=Image.open(img_path).convert('RGB')
    img=transforms.ToTensor()(img).float()
    img=img.permute(1,2,0)
    boxes=self.boxes[index]

    area = boxes[:][2] * boxes[:][3]
    area = torch.as_tensor(area, dtype=torch.float32)

    boxes[:][2] = boxes[:][0] + boxes[:] [2]
    boxes[:][3] = boxes[:][1] + boxes[:][3]

    labels = torch.ones((id_for_label.shape[0]), dtype=torch.int64)
     
    target = {}
    target["boxes"] = torch.tensor(boxes)
    target["labels"] = torch.tensor(labels)
    target["image_id"] = torch.tensor([index])
    target["area"] = area

    return img,target
</code></pre>
<p>The training function :</p>
<pre><code class="lang-auto">def train(model,optim,dataloader,path,num_of_epochs):
  os.mkdir(path)
  train_losses=[]
  losses_value=0

  for epoch in range(num_of_epochs):
    for images,targets in dataloader:
      images=[im.to(device) for im in images]
      targets = [{k: v.to(device) for k, v in t.items()} for t in targets]
      loss_dict = model(images, targets)
      losses = sum(loss for loss in loss_dict.values())
      losses_value = losses.item()
      train_loss.append(losses_value)

      optim.zero_grad()
      losses.backward()
      optimizer.step()
      break
</code></pre>
<p>Error i’m getting is :</p>
<blockquote>
<p>ValueError: Expected target boxes to be a tensorof shape [N, 4], got torch.Size([4]).</p>
</blockquote>
<p>Any solutions?</p>

ptrblck: <p>It seems your <code>boxes</code> tensor is missing <code>dim0</code>. If you are dealing with a single box, you can use <code>boxes = boxes.unsqueeze(0)</code> to add this dimension.</p>

Tornike: <p>Thank you for your reply.</p>
<p>In dataset, yes i use single box but while training there are batch of boxes.</p>
<p>When i do that unsqueeze there is another error of :</p>
<blockquote>
<p>RuntimeError: The size of tensor a (380) must match the size of tensor b (3) at non-singleton dimension 0</p>
</blockquote>
<p>I think there must be other solution.</p>
<p>I will explain briefly what i want to do:</p>
<p>Faster RCNN requires boxes to be [N,4]. i don’t know really what N represents.</p>
<blockquote>
<ul>
<li><code>boxes (FloatTensor[N, 4])</code>: the coordinates of the <code>N</code> bounding boxes in <code>[x0, y0, x1, y1]</code> format, ranging from <code>0</code> to <code>W</code> and <code>0</code> to <code>H</code><br>
this is official Explanation of that but still…</li>
</ul>
</blockquote>
<p>if i do same with Dataframe it works. but i don’t want to train it with pandas. because pandas is slow.</p>

ptrblck: <p><code>N</code> is referring to the number of boxes, so if you are dealing with 10 boxes the shape would be <code>[10, 4]</code>.<br>
The <code>RuntimeError</code> you get during <code>unsqueeze</code> is a bit strange, as the single bounding box should contain 4 coordinates only. Based on the error message it seems you are trying to <code>cat/stack</code> tensors where one has a shape of <code>380</code> in <code>dim0</code> while the other has <code>3</code>.</p>

Tornike: <p>Yes, but Still could not find solution.</p>

_STL: <p>not sure how you did the settings for the Dataloader. In my case, I have the same error when not setting the collate_fn=utils.collate_fn. See the tutorial example <a href="https://pytorch.org/tutorials/_static/tv-training-code.py" rel="noopener nofollow ugc">link</a> and how it declares</p>
<blockquote>
<pre><code># define training and validation data loaders
data_loader = torch.utils.data.DataLoader(
    dataset, batch_size=2, shuffle=True, num_workers=4,
    collate_fn=utils.collate_fn)
</code></pre>
</blockquote>

Tornike: <p>Yes, That was one of errors. but there were other errors too which i have already solved. Thanks</p>

Joao: <p>Hello, could you please write how you solved it?<br>
I’m having the same problem.</p>

Tornike: <p>Hi, <a href="https://github.com/TornikeAm/Lane_Lines_and_Car_Detection/tree/main/Faster%20Rcnn" class="inline-onebox" rel="noopener nofollow ugc">Lane_Lines_and_Car_Detection/Faster Rcnn at main · TornikeAm/Lane_Lines_and_Car_Detection · GitHub</a></p>