text: Title: Failing to compute gradients, RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
  Body: ### Search before asking
  
  - [x] I have searched the Ultralytics YOLO [issues](https://github.com/ultralytics/ultralytics/issues) and [discussions](https://github.com/orgs/ultralytics/discussions) and found no similar questions.
  
  
  ### Question
  
  I'm resarching perturbations in neural networks. I have a video in which YOLOv11 correctly detects several objects. I'd like to add a gradient to each frame, so that it would fail detecting objects in the modified frames.
  
  My current approach is;
  
  ```def fgsm(gradients, tensor):
      perturbation = epsilon * gradients.sign()
      alt_img = tensor + perturbation
      alt_img = torch.clamp(alt_img, 0, 1)  # clipping pixel values
  
      alt_img_np = alt_img.squeeze().permute(1, 2, 0).detach().numpy()
      alt_img_np = (alt_img_np * 255).astype(np.uint8)
      return alt_img_np
  
  
  def perturb(model, cap):
      out = cv2.VideoWriter('perturbed.mp4', 0x7634706d, 30.0, (640, 640))
      print("CUDA Available: ", torch.cuda.is_available())
      device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
      model.to(device)
      while cap.isOpened():
          ret, img = cap.read()
          resized = cv2.resize(img, (640, 640))
          rgb = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
          tensor = torch.from_numpy(rgb).float()
          tensor = tensor.to(device)
          tensor = tensor.permute(2, 0, 1).unsqueeze(0) #change tensor dimensions
          tensor /= 255.0 #normalize
  
          tensor.requires_grad = True
          output = model(tensor)
          target = output[0].boxes.cls.long()
          logits = output[0].boxes.data
  
          loss = -F.cross_entropy(logits, target)
          loss.backward() #Backpropagation
          gradients = tensor.grad
  
          if gradients is not None:
              alt_img = fgsm(gradients, tensor)
              cv2.imshow('Perturbed video', alt_img)
              out.write(alt_img)
  ```
  Without loss.requires_grad = True I receive;
  ```
      loss.backward() #Backpropagation
      ^^^^^^^^^^^^^^^
    File "/var/data/python/lib/python3.11/site-packages/torch/_tensor.py", line 581, in backward
      torch.autograd.backward(
    File "/var/data/python/lib/python3.11/site-packages/torch/autograd/__init__.py", line 347, in backward
      _engine_run_backward(
    File "/var/data/python/lib/python3.11/site-packages/torch/autograd/graph.py", line 825, in _engine_run_backward
      return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
  ```
  If I enable loss.requires_grad = True, I am able to extract gradients from loss, but those dont look like they are correctly applied (and dont lead to a decrease in detection/classification performance.
  What am I missing?
  
  Thanks.
  
  ### Additional
  
  _No response_