You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi! I am trying to customize a YOLOv11 training loop, but I am running into some issues when computing loss from the prediction tensors. The error happens at this point in the library:
File ~/.local/lib/python3.8/site-packages/ultralytics/utils/loss.py:211, in (.0)
209 loss = torch.zeros(3, device=self.device) # box, cls, dfl
210 feats = preds[1] if isinstance(preds, tuple) else preds
--> 211 pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split(
212 (self.reg_max * 4, self.nc), 1
I have tried several, ensuring that preds is indeed of type Torch.Tensor. Another thread (#10 ) suggested loading the model using YOLOv10 instead in YOLO. However, I am using YOLOv11 and understand that the YOLOv10 model loading is an experimental feature. I cannot implement it in my newest version of ultralytics.
model = model.model.train()
for epoch in range(num_epochs):
for batch in dataloader:
# Move entire batch to device
batch = {k: v.to(device) for k, v in batch.items()}
images = batch["img"] # Extract images from batch
optimizer.zero_grad()
# Forward pass
preds = model(images)
print(preds)
# Compute loss (including L1 regularization)
loss, _ = model.loss(tuple(preds), batch)
# L1 Regularization on model weights
l1_loss = sum(param.abs().sum() for param in model.parameters())
total_loss = loss + l1_lambda * l1_loss
# Backpropagation
total_loss.backward()
optimizer.step()
A swift answer would be highly appreciated. Thank you in advance!
The text was updated successfully, but these errors were encountered:
Hi! I am trying to customize a YOLOv11 training loop, but I am running into some issues when computing loss from the prediction tensors. The error happens at this point in the library:
File ~/.local/lib/python3.8/site-packages/ultralytics/utils/loss.py:211, in (.0)
209 loss = torch.zeros(3, device=self.device) # box, cls, dfl
210 feats = preds[1] if isinstance(preds, tuple) else preds
--> 211 pred_distri, pred_scores = torch.cat([xi.view(feats[0].shape[0], self.no, -1) for xi in feats], 2).split(
212 (self.reg_max * 4, self.nc), 1
I have tried several, ensuring that preds is indeed of type Torch.Tensor. Another thread (#10 ) suggested loading the model using YOLOv10 instead in YOLO. However, I am using YOLOv11 and understand that the YOLOv10 model loading is an experimental feature. I cannot implement it in my newest version of ultralytics.
Here is the code to my custom training loop:
def train_sparse_yolo_l1(model, dataloader, optimizer, device, num_epochs=5, l1_lambda=0.0001):
A swift answer would be highly appreciated. Thank you in advance!
The text was updated successfully, but these errors were encountered: