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

Inconsistent ch attribute not working as expected #18229

Open
1 task done
NicolasDrapier opened this issue Dec 13, 2024 · 4 comments
Open
1 task done

Inconsistent ch attribute not working as expected #18229

NicolasDrapier opened this issue Dec 13, 2024 · 4 comments
Labels
detect Object Detection issues, PR's question Further information is requested

Comments

@NicolasDrapier
Copy link

NicolasDrapier commented Dec 13, 2024

Search before asking

  • I have searched the Ultralytics YOLO issues and discussions and found no similar questions.

Question

Hello,

I am encountering an issue when trying to specify a custom number of channels using the ch attribute in my .yaml configuration file. Specifically, I set ch: 6, and then attempt to run the model as follows:

model = YOLO("yolo11m.yaml")
img = torch.rand((1, 6, 640, 640))
result = model(img)

This results in a runtime error indicating a channel mismatch. It seems like the model still expects a 3-channel input, despite having ch: 6 defined in the YAML.

Error message

RuntimeError: Given groups=1, weight of size [64, 6, 3, 3], expected input[1, 3, 640, 640] to have 6 channels, but got 3 channels instead

Stacktrace

File ~/.local/lib/python3.12/site-packages/ultralytics/engine/model.py:180, in Model.__call__(self, source, stream, **kwargs)
    151 def __call__(
    152     self,
    153     source: Union[str, Path, int, Image.Image, list, tuple, np.ndarray, torch.Tensor] = None,
    154     stream: bool = False,
    155     **kwargs,
    156 ) -> list:
    157     """
    158     Alias for the predict method, enabling the model instance to be callable for predictions.
    159 
   (...)
    178         ...     print(f"Detected {len(r)} objects in image")
    179     """
--> 180     return self.predict(source, stream, **kwargs)

File ~/.local/lib/python3.12/site-packages/ultralytics/engine/model.py:558, in Model.predict(self, source, stream, predictor, **kwargs)
    556 if prompts and hasattr(self.predictor, "set_prompts"):  # for SAM-type models
    557     self.predictor.set_prompts(prompts)
--> 558 return self.predictor.predict_cli(source=source) if is_cli else self.predictor(source=source, stream=stream)

File ~/.local/lib/python3.12/site-packages/ultralytics/engine/predictor.py:173, in BasePredictor.__call__(self, source, model, stream, *args, **kwargs)
    171     return self.stream_inference(source, model, *args, **kwargs)
    172 else:
--> 173     return list(self.stream_inference(source, model, *args, **kwargs))

File ~/.local/lib/python3.12/site-packages/torch/utils/_contextlib.py:36, in _wrap_generator.<locals>.generator_context(*args, **kwargs)
     33 try:
     34     # Issuing `None` to a generator fires it up
     35     with ctx_factory():
---> 36         response = gen.send(None)
     38     while True:
     39         try:
     40             # Forward the response to our caller and get its next request

File ~/.local/lib/python3.12/site-packages/ultralytics/engine/predictor.py:239, in BasePredictor.stream_inference(self, source, model, *args, **kwargs)
    237 # Warmup model
    238 if not self.done_warmup:
--> 239     self.model.warmup(imgsz=(1 if self.model.pt or self.model.triton else self.dataset.bs, 3, *self.imgsz))
    240     self.done_warmup = True
    242 self.seen, self.windows, self.batch = 0, [], None

File ~/.local/lib/python3.12/site-packages/ultralytics/nn/autobackend.py:735, in AutoBackend.warmup(self, imgsz)
    733 im = torch.empty(*imgsz, dtype=torch.half if self.fp16 else torch.float, device=self.device)  # input
    734 for _ in range(2 if self.jit else 1):
--> 735     self.forward(im)

File ~/.local/lib/python3.12/site-packages/ultralytics/nn/autobackend.py:527, in AutoBackend.forward(self, im, augment, visualize, embed)
    525 # PyTorch
    526 if self.pt or self.nn_module:
--> 527     y = self.model(im, augment=augment, visualize=visualize, embed=embed)
    529 # TorchScript
    530 elif self.jit:

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/module.py:1553, in Module._wrapped_call_impl(self, *args, **kwargs)
   1551     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1552 else:
-> 1553     return self._call_impl(*args, **kwargs)

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/module.py:1562, in Module._call_impl(self, *args, **kwargs)
   1557 # If we don't have any hooks, we want to skip the rest of the logic in
   1558 # this function, and just call forward.
   1559 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1560         or _global_backward_pre_hooks or _global_backward_hooks
   1561         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1562     return forward_call(*args, **kwargs)
   1564 try:
   1565     result = None

File ~/.local/lib/python3.12/site-packages/ultralytics/nn/tasks.py:112, in BaseModel.forward(self, x, *args, **kwargs)
    110 if isinstance(x, dict):  # for cases of training and validating while training.
    111     return self.loss(x, *args, **kwargs)
--> 112 return self.predict(x, *args, **kwargs)

File ~/.local/lib/python3.12/site-packages/ultralytics/nn/tasks.py:130, in BaseModel.predict(self, x, profile, visualize, augment, embed)
    128 if augment:
    129     return self._predict_augment(x)
--> 130 return self._predict_once(x, profile, visualize, embed)

File ~/.local/lib/python3.12/site-packages/ultralytics/nn/tasks.py:151, in BaseModel._predict_once(self, x, profile, visualize, embed)
    149 if profile:
    150     self._profile_one_layer(m, x, dt)
--> 151 x = m(x)  # run
    152 y.append(x if m.i in self.save else None)  # save output
    153 if visualize:

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/module.py:1553, in Module._wrapped_call_impl(self, *args, **kwargs)
   1551     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1552 else:
-> 1553     return self._call_impl(*args, **kwargs)

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/module.py:1562, in Module._call_impl(self, *args, **kwargs)
   1557 # If we don't have any hooks, we want to skip the rest of the logic in
   1558 # this function, and just call forward.
   1559 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1560         or _global_backward_pre_hooks or _global_backward_hooks
   1561         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1562     return forward_call(*args, **kwargs)
   1564 try:
   1565     result = None

File ~/.local/lib/python3.12/site-packages/ultralytics/nn/modules/conv.py:54, in Conv.forward_fuse(self, x)
     52 def forward_fuse(self, x):
     53     """Apply convolution and activation without batch normalization."""
---> 54     return self.act(self.conv(x))

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/module.py:1553, in Module._wrapped_call_impl(self, *args, **kwargs)
   1551     return self._compiled_call_impl(*args, **kwargs)  # type: ignore[misc]
   1552 else:
-> 1553     return self._call_impl(*args, **kwargs)

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/module.py:1562, in Module._call_impl(self, *args, **kwargs)
   1557 # If we don't have any hooks, we want to skip the rest of the logic in
   1558 # this function, and just call forward.
   1559 if not (self._backward_hooks or self._backward_pre_hooks or self._forward_hooks or self._forward_pre_hooks
   1560         or _global_backward_pre_hooks or _global_backward_hooks
   1561         or _global_forward_hooks or _global_forward_pre_hooks):
-> 1562     return forward_call(*args, **kwargs)
   1564 try:
   1565     result = None

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/conv.py:458, in Conv2d.forward(self, input)
    457 def forward(self, input: Tensor) -> Tensor:
--> 458     return self._conv_forward(input, self.weight, self.bias)

File ~/.local/lib/python3.12/site-packages/torch/nn/modules/conv.py:454, in Conv2d._conv_forward(self, input, weight, bias)
    450 if self.padding_mode != 'zeros':
    451     return F.conv2d(F.pad(input, self._reversed_padding_repeated_twice, mode=self.padding_mode),
    452                     weight, bias, self.stride,
    453                     _pair(0), self.dilation, self.groups)
--> 454 return F.conv2d(input, weight, bias, self.stride,
    455                 self.padding, self.dilation, `self.groups)`

RuntimeError: Given groups=1, weight of size [64, 6, 3, 3], expected input[1, 3, 640, 640] to have 6 channels, but got 3 channels instead

Expected Behavior:
I expect that when I set ch: 6 in the .yaml file, the model should correctly accept an image tensor with 6 channels as input, without throwing a channel mismatch error. Is it a bug?

Additional

ultralytics==8.3.49

@NicolasDrapier NicolasDrapier added the question Further information is requested label Dec 13, 2024
@UltralyticsAssistant UltralyticsAssistant added the detect Object Detection issues, PR's label Dec 13, 2024
@UltralyticsAssistant
Copy link
Member

👋 Hello @NicolasDrapier, thank you for your interest in Ultralytics 🚀!

We appreciate you bringing this to our attention. While we cannot resolve your issue immediately, an Ultralytics engineer will review it soon. In the meantime, here's some guidance to assist you:

✅ Recommendations

  1. Double-check your configuration: Ensure your .yaml file correctly defines ch: 6 and that this setting is applied throughout the model.
  2. Upgrade to the latest version: Make sure you're using the latest version of the ultralytics package. You can upgrade as follows:
    pip install -U ultralytics
    This ensures your issue is not related to an older package version.

🐞 Bug Reports

If this is a bug, please provide a minimum reproducible example (MRE) demonstrating the issue. This helps us debug more efficiently and understand your case better. Based on your description, sharing the exact .yaml configuration, the input data format/shape, and the full script you're running would be very helpful.

📚 Resources

🌐 Community

Feel free to join our vibrant community for real-time support or discussions:

🚀 Environments

To ensure a smooth experience, we recommend running YOLO in one of the following verified environments with up-to-date dependencies:

✅ Continuous Integration (CI) Status

Ultralytics CI
If this badge is green, all tests are passing, indicating proper functioning of tasks and models in the latest repository version.

Once you've gathered an MRE or further information, feel free to update this thread, and we'll assist you further. Thank you for your patience! 😊

Copy link
Collaborator

You should use a different yaml filename so that it doesn't conflict with the default yaml filenames.

@NicolasDrapier
Copy link
Author

Let me try next week

@glenn-jocher
Copy link
Member

@NicolasDrapier take your time! If you encounter any issues or have further questions, feel free to reach out. The YOLO community and Ultralytics team are here to help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
detect Object Detection issues, PR's question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants