Getting ERROR dict object is not callable

username: IamSparky
cooked: <p>while I am going to train my transfer learning model I am getting this error .<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6bf66a36819ac1e92c7710781e3f03848afd4e44.png" data-download-href="https://discuss.pytorch.org/uploads/default/6bf66a36819ac1e92c7710781e3f03848afd4e44" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/6/b/6bf66a36819ac1e92c7710781e3f03848afd4e44.png" alt="image" data-base62-sha1="fp51UrQtSu1XUJlxbPiK9vcwuzO" width="690" height="328" data-dominant-color="F9FAF9"><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">image</span><span class="informations">981×467 14.5 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>
<p>I am using CIFAR-10 dataset for my training purpose for implementing the ResNet101*3 from scratch</p>
<hr>
<p>data_transforms = {</p>
<pre><code>  'train': transforms.Compose([

      transforms.ToTensor(),

      transforms.RandomHorizontalFlip(0.5),

      transforms.RandomVerticalFlip(0.5),

      transforms.Resize(448),

      transforms.CenterCrop(384),

      ]),

  'val': transforms.Compose([

      transforms.ToTensor(),

      transforms.RandomHorizontalFlip(0.5),

      transforms.RandomVerticalFlip(0.5),

      transforms.Resize(448),

      transforms.CenterCrop(384),

      ]),
</code></pre>
<p>}</p>
<p>trainset = torchvision.datasets.CIFAR10(root=‘./data’ , train=True,</p>
<pre><code>                                  download=True , transform = data_transforms)
</code></pre>
<p>testset = torchvision.datasets.CIFAR10(root=‘./data’ , train=False,</p>
<pre><code>                                  download=True , transform = data_transforms)
</code></pre>
<p>data_generator = {‘train’ : trainset,‘val’: testset}</p>
<p>data_loader = {k: torch.utils.data.DataLoader(data_generator[k], batch_size=512,</p>
<pre><code>            shuffle=True, num_workers=0) for k in ['train' , 'val']}
</code></pre>
<p>classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,</p>
<pre><code>      'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
</code></pre>
<hr>
<p>in the <strong>data_loader</strong> variable ; I have converted the dataset to <strong>pytorch data_loader</strong></p>
<hr>
<p>data_loader = {k: torch.utils.data.DataLoader(data_generator[k], batch_size=512,</p>
<pre><code>            shuffle=True, num_workers=0) for k in ['train' , 'val']}
</code></pre>
<hr>
<p>After that I have used it in training method here .</p>
<hr>
<p><strong>def test(dataloaders):</strong></p>
<p>net = resNet_101X3(bottleNeck , [3,4,23,3])</p>
<p>net.to(device)</p>
<p>costFunc = nn.CrossEntropyLoss()</p>
<p>optimizer = torch.optim.SGD(net.parameters() , lr = 0.02 , momentum = 0.9)</p>
<p>params_to_update = <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>for name , param in net.named_parameters():</p>
<pre><code>if param.requires_grad is True:

  params_to_update.append(param)

  print('\t' , name)
</code></pre>
<h1><a name="setting-the-two-different-schedulers-for-decaying-the-learning-rate-at-30-60-and-80th-epoch-1" class="anchor" href="#setting-the-two-different-schedulers-for-decaying-the-learning-rate-at-30-60-and-80th-epoch-1"></a>setting the two different schedulers for decaying the learning rate at 30, 60 and 80th epoch</h1>
<p>scheduler_1 = torch.optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)</p>
<p>scheduler_2 = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.1)</p>
<p>epochs = 90</p>
<p>for epoch in range(epochs):</p>
<pre><code>print('Epoch ',epoch,' / ',epochs)

print('-'*15)

for phase in ['train', 'val'] :

  if phase == 'train':

    net.train()

  else:

    net.eval()

  running_loss = 0.0

  correct = 0

  **for inputs , labels in dataloaders[ phase ]:**

    inputs = inputs.to( net )

    labels = labels.to( net )

    optimizer.zero_grad()

    # we will use the gradients from only training mode not for validation mode

    with torch.set_grad_enabled(phase == 'train'):

      outputs = net(inputs)

      loss =  creterion(outputs , labels)

      _ , preds = torch.max(outputs , 1)

    

    if phase  == 'train':

      loss.backward()

      optimizer.step()

    running_loss += loss.item() * inputs.size(0)

    correct += torch.sum(preds == labels.data)

  epoch_loss = running_loss / len(data_loaders[phase].dataset )

  epoch_acc = correct.double() / len(data_loaders[phase].dataset )

  print(phase , " Loss:  ", epoch_loss , " epoch_acc:  " ,epoch_acc)

  # decaying the learning rate by a factor of 10

  if epoch &lt;= 60:

    scheduler_1.step()

  else :

    scheduler_2.step()
</code></pre>
<hr>
<p>and when I am running this main function , I am getting the error</p>
<hr>
<p>if  __ <strong>name</strong> __  == “__ main __”:</p>
<p>test(data_loader)</p>
<hr>
<p>please suggest <a class="mention-group" href="/groups/admins">@admins</a></p>

username: emcp
cooked: <p>I am not following where you attach the correct set of transforms under training or testing… but it appears you’re calling the dictionary itself, when what you wanted was one of the keys “train” or “val”</p>

username: IamSparky
cooked: <p>here you go :-<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/3/e3b3952e51dcdd3d796b43ae425532937fce01ff.png" data-download-href="https://discuss.pytorch.org/uploads/default/e3b3952e51dcdd3d796b43ae425532937fce01ff" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/3/e3b3952e51dcdd3d796b43ae425532937fce01ff.png" alt="image" data-base62-sha1="wul8GhX4rg42hKHDM9c1Onp7gOH" width="498" height="500" data-dominant-color="F0EFEF"><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">image</span><span class="informations">796×798 22.6 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>

username: IamSparky
cooked: <p>And I am calling the data_loader in this function<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1b2ea26c987dedcd136b0d5b2e7f3f5d8316d186.png" data-download-href="https://discuss.pytorch.org/uploads/default/1b2ea26c987dedcd136b0d5b2e7f3f5d8316d186" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/1/b/1b2ea26c987dedcd136b0d5b2e7f3f5d8316d186.png" alt="image" data-base62-sha1="3SsNWHcGNZ5nh2Lf1OpKINBLtm6" width="526" height="499" data-dominant-color="F1F1F1"><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">image</span><span class="informations">943×895 35.4 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>

username: emcp
cooked: <p>I am not aware you can pass transforms in a dictionary… you may want to temporarily create two separate instances of</p>
<pre><code class="lang-auto">
training_transforms = transforms.Compose([your_array_here])

...

trainset = torchvision.datasets.CIFAR10(.... , transform = training_transforms)
</code></pre>

username: IamSparky
cooked: <p>We can pass transforms in a dictionary . I tried the same thing using pytorch docs<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/c/dcab4e4c05da59cecdb77c00277f0526238c8121.png" data-download-href="https://discuss.pytorch.org/uploads/default/dcab4e4c05da59cecdb77c00277f0526238c8121" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dcab4e4c05da59cecdb77c00277f0526238c8121_2_645x500.png" alt="image" data-base62-sha1="vu840ggZvLK5t2sFRjIaIFpCbId" width="645" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dcab4e4c05da59cecdb77c00277f0526238c8121_2_645x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/c/dcab4e4c05da59cecdb77c00277f0526238c8121_2_967x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/c/dcab4e4c05da59cecdb77c00277f0526238c8121.png 2x" data-dominant-color="EDEEF1"><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">image</span><span class="informations">989×766 56.2 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>

username: ptrblck
cooked: <p>In the posted example the <code>data_transforms</code> <code>dict</code> is accessed via the key <code>x</code>.<br>
As <a class="mention" href="/u/emcp">@emcp</a> said, you should pass the transformation directly instead of a <code>dict</code>.</p>

username: IamSparky
cooked: <p>What should I put in <strong>your array here</strong> in <a class="mention" href="/u/emcp">@emcp</a> example , please refer me to any example so that after making the changes my code should work as expected .</p>

username: ptrblck
cooked: <p>You would have to put the keys in the dict:</p>
<pre><code class="lang-python">trainset = torchvision.datasets.CIFAR10(root='./data' , train=True,
                                  download=True , transform = data_transforms['train'])
testset = torchvision.datasets.CIFAR10(root='./data' , train=False,
                                  download=True , transform = data_transforms['val'])
</code></pre>

username: IamSparky
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a> that worked but still stucked in this error .<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/b/5bca4f6c0f5588dea3b456f89514d97a12b87954.png" data-download-href="https://discuss.pytorch.org/uploads/default/5bca4f6c0f5588dea3b456f89514d97a12b87954" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/b/5bca4f6c0f5588dea3b456f89514d97a12b87954.png" alt="image" data-base62-sha1="d60TE0XqwsnNSeyeAZJz2eegmqg" width="690" height="292" data-dominant-color="F7F7F8"><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">image</span><span class="informations">887×376 14.4 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>
<p>for my weight standardization class<br>
<strong>CODE</strong></p>
<hr>
<h1><a name="making-a-separate-class-for-weight-standardization-1" class="anchor" href="#making-a-separate-class-for-weight-standardization-1"></a>making a separate class for weight standardization</h1>
<p>class conv2d(nn.Conv2d):</p>
<pre><code>def __init__(self, in_channels, out_channels, kernel_size , stride , padding):

    super().__init__(in_channels, out_channels, kernel_size)

def forward(self, x):

    weight = self.weight

    weight_mean = weight.mean(dim=1, keepdim=True).mean(dim=2,

                              keepdim=True).mean(dim=3, keepdim=True)

    weight = weight - weight_mean

    std = weight.view(weight.size(0), -1).std(dim=1).view(-1, 1, 1, 1) + 1e-5

    weight = weight / std.expand_as(weight)

    return F.conv2d(x, weight , self.stride , self.padding)
</code></pre>
<p>please help</p>

username: ptrblck
cooked: <p><code>F.conv2d</code> expects the inputs as:</p>
<pre><code class="lang-python">input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1
</code></pre>
<p>If you don’t have a <code>bias</code> parameter, you could either pass <code>None</code> to the <code>conv2d</code> call or pass the <code>self.stride</code> and <code>self.padding</code> using the keywords: <code>stride=self.stride</code> etc.</p>

username: IamSparky
cooked: <h2><a name="got-this-error-epoch-0-90-1" class="anchor" href="#got-this-error-epoch-0-90-1"></a>got this error :-<br>
Epoch  0  /  90</h2>
<hr>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
1 if <strong>name</strong> == “<strong>main</strong>”:<br>
2<br>
----&gt;3   test(data_loader)<br>
4</p>
<p>4 frames<br>
 in forward(self, x)<br>
11         weight = weight / std.expand_as(weight)<br>
12         return F.conv2d(x, weight, self.bias, self.stride,<br>
—&gt; 13                         self.padding)</p>
<p>RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same</p>
<p>while using this</p>
<hr>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/c/fca7a35a1b6de5ed185d41d1e6096049fba8bd27.png" data-download-href="https://discuss.pytorch.org/uploads/default/fca7a35a1b6de5ed185d41d1e6096049fba8bd27" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/f/c/fca7a35a1b6de5ed185d41d1e6096049fba8bd27.png" alt="image" data-base62-sha1="A35u6jcEuGjhddlzLMPWaDlw3hd" width="671" height="500" data-dominant-color="F0F0F0"><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">image</span><span class="informations">1188×885 54.7 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>