[Python] ground truh and prediected label not match [closed]

Discussão em 'Python' iniciado por Stack, Novembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    Asslamualim i run code with yolov8n-obb model and error generate

    Error:

    Shape of pred_cls before reshaping: torch.Size([5, 3, 1, 80, 80])
    Reshaped pred_cls to: [5, 3, 80, 80]
    ---------------------------------------------------------------------------
    RuntimeError Traceback (most recent call last)
    <ipython-input-2-cb847e3ac8a0> in <cell line: 460>()
    533 # Reshape pred_cls to have spatial dimensions
    534 num_anchors = 3
    --> 535 pred_cls = pred_cls.view(batch_size, num_classes, num_anchors, grid_size, grid_size)
    536 pred_cls = pred_cls.permute(0, 1, 3, 4, 2) # permute channels for interpolation
    537

    RuntimeError: shape '[5, 1, 3, 20, 20]' is invalid for input of size 96000


    code:

    for epoch in range(epochs):
    model.train() # Set the model to training mode

    for images, targets in train_dataloader:
    # Debugging code to inspect the targets
    print(f"Targets: {targets}") # Inspect the full targets structure
    print(f"Type of targets: {type(targets)}") # Check if targets is a dictionary

    # Now iterate over each target and inspect it
    for target in targets:
    print(f"Target: {target}") # Print the full target
    print(f"Type of target: {type(target)}") # Print the type of target

    # Ensure target is a dictionary before accessing its items
    if isinstance(target, dict): # Check if target is a dictionary
    print(f"Labels: {target['labels']}") # Access labels
    print(f"Boxes: {target['boxes']}") # Access boxes
    else:
    print("Target is not a dictionary. Investigate this structure.")

    # Assuming the target is now properly formatted and accessible
    images = images.to(device) # Ensure device is set (CPU or GPU)

    # Make sure to handle targets correctly
    for target in targets:
    if isinstance(target, dict): # Check if it's a valid dictionary
    true_boxes = target['boxes'].to(device) # Ground truth boxes
    true_cls = target['labels'].to(device) # Ground truth class labels
    else:
    print("Skipping invalid target structure.")
    continue # Skip invalid targets

    # Get image dimensions from a sample batch
    batch_size, channels, height, width = images.shape # Dynamic extraction from batch

    # Define grid_size dynamically based on the input image size and downsampling factor
    downsampling_factor = 32 # This is typical for YOLO models
    grid_size = height // downsampling_factor # For example, for 640x640 input, grid_size would be 20

    optimizer.zero_grad() # Clear previous gradients

    # Initialize true_cls with zeros (absence of plane)
    true_cls_onehot = torch.zeros(batch_size, num_classes, grid_size, grid_size, device=device)

    # Loop over each target to assign the class labels based on the ground truth bounding boxes
    for i, target in enumerate(targets):
    if isinstance(target, dict): # Check if it's a valid dictionary
    print(f"Processing target: {target}") # Add this line to inspect the target structure
    if target['labels'] == 0: # If the label is 0 (plane)
    # Convert bounding box to grid cell indices
    x_center = (target['boxes'][0] / width) * grid_size # x_center to grid x-axis
    y_center = (target['boxes'][1] / height) * grid_size # y_center to grid y-axis

    grid_x = int(x_center) # Grid index for x-axis
    grid_y = int(y_center) # Grid index for y-axis

    # Set the class at the corresponding grid cell to 1 (plane)
    true_cls_onehot[i, 0, grid_y, grid_x] = 1 # plane class id is 0, mark it as 1 in the grid
    else:
    print("Skipping invalid target structure.")
    continue # Skip invalid targets

    # Forward pass through the model
    x_original, pred_boxes, pred_cls, pred_confidence = model(images)
    # Get image dimensions from a sample batch
    batch_size, channels, height, width = images.shape # Dynamic extraction from batch

    # Define grid_size dynamically based on the input image size and downsampling factor
    downsampling_factor = 32 # This is typical for YOLO models
    grid_size = height // downsampling_factor # For example, for 640x640 input, grid_size would be 20

    # ... (true_cls_onehot generation logic remains the same) ...

    # Reshape pred_cls to have spatial dimensions
    num_anchors = 3
    pred_cls = pred_cls.view(batch_size, num_classes, num_anchors, grid_size, grid_size)
    pred_cls = pred_cls.permute(0, 1, 3, 4, 2) # permute channels for interpolation

    # Before calculating focal loss, adjust pred_cls to match the spatial dimensions of true_cls_onehot
    pred_cls = F.interpolate(pred_cls, size=true_cls_onehot.shape[-2:], mode='bilinear', align_corners=False)
    pred_cls = pred_cls.permute(0, 1, 4, 2, 3) # permute back to original channel order
    pred_cls = pred_cls.reshape(batch_size, num_classes, -1) # flatten to original shape

    # Compute the classification loss (assuming focal loss is defined)
    try:
    loss_cls = focal_loss(pred_cls, true_cls_onehot)
    except ValueError as e:
    print(f"Error: {e}")
    print(f"Prediction shape: {pred_cls.shape}, Target shape: {true_cls_onehot.shape}")
    raise # Re-raise the exception to stop training and debug further

    # Compute bounding box regression loss (replace with your loss function)
    loss_bbox = bbox_loss(pred_boxes, true_boxes, width, height) # Assuming bbox_loss is defined

    # Compute confidence loss (optional, replace with your loss function)
    loss_conf = confidence_loss(pred_confidence, targets['conf'].to(device)) # Optional confidence loss

    # Total combined loss
    loss = combined_loss(pred_boxes, true_boxes, pred_cls, true_cls_onehot)

    # Backpropagation
    loss.backward() # Compute gradients
    optimizer.step() # Update model parameters

    torch.cuda.empty_cache() # Clear memory after training step to prevent overflow

    # Print the loss and shapes for debugging
    print(f"Epoch [{epoch}/{epochs}], Loss: {loss.item():.4f}, Loss_cls: {loss_cls.item():.4f}, Loss_bbox: {loss_bbox.item():.4f}, Loss_conf: {loss_conf.item():.4f}")
    print(f"x_original shape: {x_original.shape}")
    print(f"Predicted boxes shape: {pred_boxes.shape}")
    print(f"Predicted classes shape: {pred_cls.shape}")
    print(f"Confidence scores shape: {pred_confidence.shape}")

    # Save the trained model after training is complete
    torch.save(model.state_dict(), "yolov8n_obb_from_scratch.pth")


    the generate in this code and line is here and please give me a solution

    pred_cls = pred_cls.view(batch_size, num_classes, num_anchors, grid_size, grid_size)

    Continue reading...

Compartilhe esta Página