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

AttributeError: 'str' object has no attribute 'view' #516

Open
wrecka98 opened this issue Jan 31, 2025 · 0 comments
Open

AttributeError: 'str' object has no attribute 'view' #516

wrecka98 opened this issue Jan 31, 2025 · 0 comments

Comments

@wrecka98
Copy link

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):

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!

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

1 participant