Adding an argument to ResNet basic block

stef427: <p>I am modifying ResNet to train some variables emb, which depends on some input meta data. I have issues passing the meta data down to my user defined layer in ResNet.</p>
<p>emb is Category Embedding in the diagram, meta is “Dress” in the diagram</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/4/c4c9a47fa721884df80c3d92bfd272718f033434.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/c4c9a47fa721884df80c3d92bfd272718f033434" title="Screen Shot 2020-12-30 at 12.25.54 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4c9a47fa721884df80c3d92bfd272718f033434_2_690x210.jpeg" alt="Screen Shot 2020-12-30 at 12.25.54 PM" data-base62-sha1="s4RBhT65eKjJdB75AvhOG259pmQ" width="690" height="210" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4c9a47fa721884df80c3d92bfd272718f033434_2_690x210.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4c9a47fa721884df80c3d92bfd272718f033434_2_1035x315.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4c9a47fa721884df80c3d92bfd272718f033434_2_1380x420.jpeg 2x" data-dominant-color="E3E6E4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-12-30 at 12.25.54 PM</span><span class="informations">2124×648 212 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/5/35626dbf65ec136cb2587d3cd659393b0f85b17a.png" data-download-href="https://discuss.pytorch.org/uploads/default/35626dbf65ec136cb2587d3cd659393b0f85b17a" title="Screen Shot 2020-12-30 at 12.26.05 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/35626dbf65ec136cb2587d3cd659393b0f85b17a_2_690x371.png" alt="Screen Shot 2020-12-30 at 12.26.05 PM" data-base62-sha1="7CgbGq2naeW7vXxRoXHhptsmWP8" width="690" height="371" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/35626dbf65ec136cb2587d3cd659393b0f85b17a_2_690x371.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/35626dbf65ec136cb2587d3cd659393b0f85b17a_2_1035x556.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/3/5/35626dbf65ec136cb2587d3cd659393b0f85b17a_2_1380x742.png 2x" data-dominant-color="E6E9E7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screen Shot 2020-12-30 at 12.26.05 PM</span><span class="informations">1766×952 162 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<ol>
<li>
<p>Main function:<br>
outputs = model(inputs)&gt;&gt; outputs = model(inputs, meta), model here is ResNet 18</p>
</li>
<li>
<p>ResNet forward function,<br>
def forward(self, x, meta):<br>
x = self.conv1(x)<br>
x = self.bn1(x)<br>
x = self.relu(x)<br>
x = self.layer1(x, meta)</p>
</li>
<li>
<p>BasicBlock forward function:<br>
def forward(self, x, meta):</p>
</li>
</ol>
<p>I am getting errors that don’t make sense to me. The above code gives me error message<br>
x = self.layer1(x, meta)<br>
File “/home/user/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
TypeError: forward() takes 2 positional arguments but 3 were given</p>
<p>But if I remove the last argument meta and run this instead, x = self.layer1(x), then I get this error</p>
<p>Traceback (most recent call last):<br>
File “main.py”, line 202, in <br>
main()<br>
File “main.py”, line 153, in main<br>
outputs = model(inputs, meta)<br>
File “/home/user/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/…/models/resnet.py”, line 471, in forward<br>
x = self.layer1(x)<br>
File “/home/user/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
File “/home/user/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/container.py”, line 92, in forward<br>
input = module(input)<br>
File “/home/user/anaconda3/envs/pytorch/lib/python3.6/site-packages/torch/nn/modules/module.py”, line 493, in <strong>call</strong><br>
result = self.forward(*input, **kwargs)<br>
TypeError: forward() missing 1 required positional argument: ‘meta’</p>
<p>This is the code my mod is based on,</p><aside class="onebox githubblob" data-onebox-src="https://github.com/Jongchan/attention-module/blob/master/MODELS/model_resnet.py">
  <header class="source">

      <a href="https://github.com/Jongchan/attention-module/blob/master/MODELS/model_resnet.py" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/Jongchan/attention-module/blob/master/MODELS/model_resnet.py" target="_blank" rel="noopener nofollow ugc">Jongchan/attention-module/blob/master/MODELS/model_resnet.py</a></h4>


      <pre><code class="lang-py">import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from torch.nn import init
from .cbam import *
from .bam import *

def conv3x3(in_planes, out_planes, stride=1):
    "3x3 convolution with padding"
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=1, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, use_cbam=False):
        super(BasicBlock, self).__init__()
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = nn.BatchNorm2d(planes)
</code></pre>



  This file has been truncated. <a href="https://github.com/Jongchan/attention-module/blob/master/MODELS/model_resnet.py" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>


ptrblck: <p>Could you post a code snippet to reproduce this issue, as the linked code doesn’t contain the modification including the <code>meta</code> information?</p>