Getting ERROR dict object is not callable

while I am going to train my transfer learning model I am getting this error .

I am using CIFAR-10 dataset for my training purpose for implementing the ResNet101*3 from scratch


data_transforms = {

  '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),

      ]),

}

trainset = torchvision.datasets.CIFAR10(root=‘./data’ , train=True,

                                  download=True , transform = data_transforms)

testset = torchvision.datasets.CIFAR10(root=‘./data’ , train=False,

                                  download=True , transform = data_transforms)

data_generator = {‘train’ : trainset,‘val’: testset}

data_loader = {k: torch.utils.data.DataLoader(data_generator[k], batch_size=512,

            shuffle=True, num_workers=0) for k in ['train' , 'val']}

classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,

      'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

in the data_loader variable ; I have converted the dataset to pytorch data_loader


data_loader = {k: torch.utils.data.DataLoader(data_generator[k], batch_size=512,

            shuffle=True, num_workers=0) for k in ['train' , 'val']}

After that I have used it in training method here .


def test(dataloaders):

net = resNet_101X3(bottleNeck , [3,4,23,3])

net.to(device)

costFunc = nn.CrossEntropyLoss()

optimizer = torch.optim.SGD(net.parameters() , lr = 0.02 , momentum = 0.9)

params_to_update =

for name , param in net.named_parameters():

if param.requires_grad is True:

  params_to_update.append(param)

  print('\t' , name)

setting the two different schedulers for decaying the learning rate at 30, 60 and 80th epoch

scheduler_1 = torch.optim.lr_scheduler.StepLR(optimizer, step_size=30, gamma=0.1)

scheduler_2 = torch.optim.lr_scheduler.StepLR(optimizer, step_size=20, gamma=0.1)

epochs = 90

for epoch in range(epochs):

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 <= 60:

    scheduler_1.step()

  else :

    scheduler_2.step()

and when I am running this main function , I am getting the error


if __ name __ == “__ main __”:

test(data_loader)


please suggest @admins

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”

here you go :-

And I am calling the data_loader in this function

I am not aware you can pass transforms in a dictionary… you may want to temporarily create two separate instances of


training_transforms = transforms.Compose([your_array_here])

...

trainset = torchvision.datasets.CIFAR10(.... , transform = training_transforms)

We can pass transforms in a dictionary . I tried the same thing using pytorch docs

In the posted example the data_transforms dict is accessed via the key x.
As @emcp said, you should pass the transformation directly instead of a dict.

What should I put in your array here in @emcp example , please refer me to any example so that after making the changes my code should work as expected .

You would have to put the keys in the dict:

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'])

Thanks @ptrblck that worked but still stucked in this error .

for my weight standardization class
CODE


making a separate class for weight standardization

class conv2d(nn.Conv2d):

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)

please help

F.conv2d expects the inputs as:

input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1

If you don’t have a bias parameter, you could either pass None to the conv2d call or pass the self.stride and self.padding using the keywords: stride=self.stride etc.

got this error :-
Epoch 0 / 90


RuntimeError Traceback (most recent call last)
in ()
1 if name == “main”:
2
---->3 test(data_loader)
4

4 frames
in forward(self, x)
11 weight = weight / std.expand_as(weight)
12 return F.conv2d(x, weight, self.bias, self.stride,
—> 13 self.padding)

RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the same

while using this