3

I am trying to build my first NN with pytroch and got an issue.

TypeError: new() received an invalid combination of arguments - got (float, int, int, int), but expected one of: * (torch.device device) * (torch.Storage storage) * (Tensor other) * (tuple of ints size, torch.device device) * (object data, torch.device device)

Now I know what this is saying in that I am not passing the right type to the method or init. But I dont know what I should pass as it looks right to me.

def main():
#Get the time and data
now = datetime.datetime.now()
hourGlassToStack = 2 #Hourglasses to stack
numModules= 2        #Residual Modules for each hourglass
numFeats = 256      #Number of features in each hourglass
numRegModules = 2   #Depth regression modules

print("Creating Model")
model = HourglassNet3D(hourGlassToStack, numModules, numFeats,numRegModules).cuda()
print("Model Created")

this is the main method that created the model. It then calls this methods.

class HourglassNet3D(nn.Module):

  def __init__(self, nStack, nModules, nFeats, nRegModules):
    super(HourglassNet3D, self).__init__()
    self.nStack = nStack
    self.nModules = nModules
    self.nFeats = nFeats
    self.nRegModules = nRegModules
    self.conv1_ = nn.Conv2d(3, 64, bias = True, kernel_size = 7, stride = 2, padding = 3)
   self.bn1 = nn.BatchNorm2d(64)
    self.relu = nn.ReLU(inplace = True)
    self.r1 = Residual(64, 128)
    self.maxpool = nn.MaxPool2d(kernel_size = 2, stride = 2)
    self.r4 = Residual(128, 128)
    self.r5 = Residual(128, self.nFeats)

    _hourglass, _Residual, _lin_, _tmpOut, _ll_, _tmpOut_, _reg_ = [], [], [], [], [], [], []
    for i in range(self.nStack):
      _hourglass.append(Hourglass(4, self.nModules, self.nFeats))
      for j in range(self.nModules):
        _Residual.append(Residual(self.nFeats, self.nFeats))
      lin = nn.Sequential(nn.Conv2d(self.nFeats, self.nFeats, bias = True, kernel_size = 1, stride = 1), 
                      nn.BatchNorm2d(self.nFeats), self.relu)
      _lin_.append(lin)
      _tmpOut.append(nn.Conv2d(self.nFeats, 16, bias = True, kernel_size = 1, stride = 1))
  _ll_.append(nn.Conv2d(self.nFeats, self.nFeats, bias = True, kernel_size = 1, stride = 1))
  _tmpOut_.append(nn.Conv2d(16, self.nFeats, bias = True, kernel_size = 1, stride = 1))

for i in range(4):
  for j in range(self.nRegModules):
    _reg_.append(Residual(self.nFeats, self.nFeats))

self.hourglass = nn.ModuleList(_hourglass)
self.Residual = nn.ModuleList(_Residual)
self.lin_ = nn.ModuleList(_lin_)
self.tmpOut = nn.ModuleList(_tmpOut)
self.ll_ = nn.ModuleList(_ll_)
self.tmpOut_ = nn.ModuleList(_tmpOut_)
self.reg_ = nn.ModuleList(_reg_)

self.reg = nn.Linear(4 * 4 * self.nFeats,16 )

And this then call this

class Residual(nn.Module):
#set the number ofinput and output for each layer
def __init__(self, numIn, numOut):
   super(Residual, self).__init__()
   self.numIn = numIn
   self.numOut = numOut
   self.bn = nn.BatchNorm2d(self.numIn)
   self.relu = nn.ReLU(inplace = True)
   self.conv1 = nn.Conv2d(self.numIn, self.numOut / 2, bias = True, kernel_size = 1)
   self.bn1 = nn.BatchNorm2d(self.numOut / 2)
   self.conv2 = nn.Conv2d(self.numOut / 2, self.numOut / 2, bias = True, kernel_size = 3, stride = 1, padding = 1)
   self.bn2 = nn.BatchNorm2d(self.numOut / 2)
   self.conv3 = nn.Conv2d(self.numOut / 2, self.numOut, bias = True, kernel_size = 1)

   if self.numIn != self.numOut:
       self.conv4 = nn.Conv2d(self.numIn, self.numOut, bias = True, kernel_size = 1) 

all of this looks fine to me, but I dont know how I am suppose to pass this then if I am doing it wrong. Thank you for any help

MNM
  • 2,673
  • 6
  • 38
  • 73
  • Can you tell us in which line specifically this error appears? Also, you should always define a `forward(self, x)` method, that specifies how you propagate through your network; I am not sure about the PyTorch internals (whether it is required to create a network or not), but it also helps bystanders like the SO community to analyze your network better. – dennlinger Aug 10 '18 at 07:29
  • 1
    I have them in these but didnt want to put a whole bunch of excess code here – MNM Aug 10 '18 at 07:38
  • Alright, just wanted to know whether you had anything at all. – dennlinger Aug 10 '18 at 07:40

1 Answers1

6

You might have to look out as to what you are passing to your convolutional layers in the Residual class. Per default, Python 3 will convert any division operation into a float variable.

Try casting your variables back to an integer, and see if that helps. Fixed code for Residual:

class Residual(nn.Module):
#set the number ofinput and output for each layer

    def __init__(self, numIn, numOut):
       super(Residual, self).__init__()
       self.numIn = numIn
       self.numOut = numOut
       self.bn = nn.BatchNorm2d(self.numIn)
       self.relu = nn.ReLU(inplace = True)
       self.conv1 = nn.Conv2d(self.numIn, int(self.numOut / 2), bias = True, kernel_size = 1)
       self.bn1 = nn.BatchNorm2d(int(self.numOut / 2))
       self.conv2 = nn.Conv2d(int(self.numOut / 2), int(self.numOut / 2), bias = True, kernel_size = 3, stride = 1, padding = 1)
       self.bn2 = nn.BatchNorm2d(int(self.numOut / 2))
       self.conv3 = nn.Conv2d(int(self.numOut / 2), self.numOut, bias = True, kernel_size = 1)

       if self.numIn != self.numOut:
           self.conv4 = nn.Conv2d(self.numIn, self.numOut, bias = True, kernel_size = 1) 
dennlinger
  • 9,890
  • 1
  • 42
  • 63
  • your the boss Just so I know what I got wrong, the Residual Conv2d needs it in a int, but due to python it converted it into a float?] – MNM Aug 10 '18 at 07:41
  • 1
    Exactly. Since you are specifying the number of features, you have to pass an integer value. Also, that explains the (kind of cryptic) error message above: Out of the listed "accepted types", you are closest to the "tuple of ints", but since you were also passing a single float value, PyTorch could not interpret it correctly. – dennlinger Aug 10 '18 at 07:42
  • I see makes sense. I really appreciate it. – MNM Aug 10 '18 at 07:48