Skip to content

Issue with channels in Conv*d lycoris with groups != 1 #260

@ASDRED007

Description

@ASDRED007

I am trying to apply Lycoris to ultralytics RT DETR detection model, get this error.
Traceback (most recent call last):
File "/opt/python3.11/lib/python3.11/site-packages/ultralytics/engine/trainer.py", line 406, in _do_train
loss, self.loss_items = self.model(batch)
^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1751, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1762, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/ultralytics/nn/tasks.py", line 137, in forward
return self.loss(x, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/ultralytics/nn/tasks.py", line 777, in loss
preds = self.predict(img, batch=targets) if preds is None else preds
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/ultralytics/nn/tasks.py", line 819, in predict
x = m(x) # run
^^^^
File "/opt/python3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1751, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1762, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/ultralytics/nn/modules/conv.py", line 80, in forward
return self.act(self.bn(self.conv(x)))
^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1751, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1762, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/python3.11/lib/python3.11/site-packages/lycoris/modules/locon.py", line 330, in forward
return self.op(x, weight, bias, **self.kw_dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeError: Given groups=128, weight of size [128, 128, 3, 3], expected input[32, 128, 160, 160] to have 16384 channels, but got 128 channels instead

After some research I figure out that problem is in creating shape of Conv*d weights, it should be divided by groups.
Here is the possible solution, the shape should be done in the same way as torch documentation, see
https://docs.pytorch.org/docs/stable/generated/torch.nn.Conv1d.html
https://docs.pytorch.org/docs/stable/generated/torch.nn.Conv2d.html
https://docs.pytorch.org/docs/stable/generated/torch.nn.Conv3d.html

Index: lycoris/modules/base.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lycoris/modules/base.py b/lycoris/modules/base.py
--- a/lycoris/modules/base.py	(revision 9034d0538e35c29dfda3538332076591fc2ba153)
+++ b/lycoris/modules/base.py	(revision 997685c2bcb1a5c10287a8a823b4112db02da712)
@@ -96,7 +96,7 @@
             self.module_type = "conv1d"
             self.shape = (
                 org_module.out_channels,
-                org_module.in_channels,
+                org_module.in_channels // org_module.groups,
                 *org_module.kernel_size,
             )
             self.op = F.conv1d
@@ -111,7 +111,7 @@
             self.module_type = "conv2d"
             self.shape = (
                 org_module.out_channels,
-                org_module.in_channels,
+                org_module.in_channels // org_module.groups,
                 *org_module.kernel_size,
             )
             self.op = F.conv2d
@@ -126,7 +126,7 @@
             self.module_type = "conv3d"
             self.shape = (
                 org_module.out_channels,
-                org_module.in_channels,
+                org_module.in_channels // org_module.groups,
                 *org_module.kernel_size,
             )
             self.op = F.conv3d
Index: lycoris/modules/locon.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/lycoris/modules/locon.py b/lycoris/modules/locon.py
--- a/lycoris/modules/locon.py	(revision 997685c2bcb1a5c10287a8a823b4112db02da712)
+++ b/lycoris/modules/locon.py	(revision f0ce8c9fda31370f3220a31c487aadcb5f0c97d5)
@@ -74,7 +74,7 @@
         if self.module_type.startswith("conv"):
             self.isconv = True
             # For general LoCon
-            in_dim = org_module.in_channels
+            in_dim = org_module.in_channels // org_module.groups
             k_size = org_module.kernel_size
             stride = org_module.stride
             padding = org_module.padding

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions