Model.eval() Error -- 'bool' object is not callable

Hi,
I am getting getting the following error when I try to evaluate my model after training:

In SegModel: False ------Epoch 51 | Step 71 | Train Loss 0.1662866771221161 | Validation Loss 0.48616326600313187 -----------
model loaded
Traceback (most recent call last):
File “tester.py”, line 61, in model.eval() File “/data/sbanerjee/anaconda_env/SV/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 1009, in eval return self.train(False)
File “/data/sbanerjee/anaconda_env/SV/lib/python3.7/site-packages/torch/nn/modules/module.py”, line 998, in train module.train(mode)
TypeError: ‘bool’ object is not callable

Here’s my code snippet:

model = SegModel(train=False)
if torch.cuda.device_count() > 1:
model = nn.DataParallel(model)
model.to(device)

load_path = ‘path/to/saved/model/model_best_0816.pth.tar’
checkpoint = torch.load(load_path)
epoch = checkpoint[‘epoch’]
train_step = checkpoint[‘train step’]
train_loss = checkpoint[‘train_loss’]
val_loss = checkpoint[‘val_loss’]
print ("------Epoch {} | Step {} | Train Loss {} | Validation Loss {} -----------".format(epoch, train_step, train_loss, val_loss))
model.load_state_dict(checkpoint[‘state_dict’])
print (“model loaded”)
model.eval()

Any help would be greatly appreciated

1 Like

It looks like you have replaced the internal self.train() method inside your model with a bool value:

class MyModel(nn.Module):
    def __init__(self, train):
        super(MyModel, self).__init__()
        self.bn = nn.BatchNorm2d(3)
        self.train = train

    def forward(self, x):
        x = self.bn(x)
        return x

Note that self.train() is defined as a method in all nn.Modules, so you should use another name. :wink:

2 Likes

Oh okay, thanks a ton :slight_smile: