AttributeError: '_IncompatibleKeys' object has no attribute 'eval'

ajwitty: <p>When using ‘load_state_dict’ to load saved triplet net, get  for network, but when setting to eval():</p>
<p>Code:</p>
<pre><code class="lang-auto">from __future__ import print_function
from __future__ import division
import argparse
import os
import shutil
import torch
import torch.nn as nn
import torch.nn.functional as F
import logging
import torch.optim as optim
from torchvision import datasets, transforms
from torch.autograd import Variable
import random
from PIL import Image
from random import shuffle
from tripletnet import Tripletnet
from collections import OrderedDict
# from vgg import *
# from resnet import *
from vgg19_64 import Vgg19
from random_cropper import random_crop, center_crop
import torch.utils.data
import pdb
import numpy as np


#triplet pairs should be like [a,b,c] a is closer to c than b (i.e. a and c belong to the same class and b is the other class)

def accuracy(dista, distb):
    margin = 0
    pred = (dista - distb - margin).cpu().data
    return (pred &gt; 0).sum().item()*1.0/dista.size()[0]

def test_accuracy(dista, distb):
    margin = 0
    pred = (dista - distb - margin).cpu().data
    return (pred &gt; 0).sum().item()

class AverageMeter(object):
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, n=1):
        self.val = val
        self.sum += val * n
        self.count += n
        self.avg = self.sum / self.count


loader_tensor = transforms.ToTensor()

base_folder = '/raid/adamw/triplet_net/triplet_net_files'

for fold in range(5):

    fold_folder = os.path.join(base_folder, 'fold_{}/save'.format(fold))
    networks = os.listdir(fold_folder)
    network_accs = [x[-10:-3] for x in networks]
    best_net = networks[np.argmax([float(x.replace('_', '')) for x in network_accs])]
    # best_net = [x for x in networks if 'model_14' in x][0]

    spread_test = open(os.path.join(base_folder, 'fold_{}/Spread/test.txt'.format(fold)), "r")
    spread_test_files = spread_test.readlines()

    dense_test = open(os.path.join(base_folder, 'fold_{}/Dense/test.txt'.format(fold)), "r")
    dense_test_files = dense_test.readlines()

    spread_templates = open(os.path.join(base_folder, 'Spread_templates.txt'), "r")
    spread_templates_files = spread_templates.readlines()

    dense_templates = open(os.path.join(base_folder, 'Dense_templates.txt'), "r")
    dense_templates_files = dense_templates.readlines()

    # num_img_per_epoch = 100000
    # mini_batch_size = 50
    # epochs = 15
    # total_mini_batch = num_img_per_epoch//mini_batch_size
    image_size = 128
    num_templates = 11
    majority_thresh = (num_templates//2) + 1

    # model = VGG('myVGG')
    #~ model = myresnet()

    state_dict = torch.load(os.path.join(fold_folder, best_net))
    # new_state_dict = OrderedDict()
    # for k, v in state_dict.items():
    #     # TODO: only copy module if it does not include 'num_batches_tracked'
    #     # TODO: check raj code vs my code and see if he tracks num batches...
    #     print(k)
    #     pdb.set_trace()
    model = Vgg19()
    tnet = Tripletnet(model).cuda().load_state_dict(state_dict)
    pdb.set_trace()
    criterion = torch.nn.MarginRankingLoss(margin = 1)
    criterion = criterion.cuda()

    sp_temps = torch.FloatTensor(num_templates,1,image_size,image_size)
    den_temps = torch.FloatTensor(num_templates,1,image_size,image_size)

    correct = 0

    for i in range(num_templates):
        img = Image.open(spread_templates_files[i][:-1])
        sp_temps[i] = loader_tensor(img.resize((image_size,image_size)))

        img = Image.open(dense_templates_files[i][:-1])
        den_temps[i] = loader_tensor(img.resize((image_size,image_size)))

        #~ break

        """
            actual dense spread
        pred 
        dense      (x,x)  (x, y)   
        spread     (y,x)  (y, y) 
        """
        confmat = np.zeros([5, 2, 2], dtype=int)

        # testing
        tnet.eval()
        den_correct = 0
        spr_correct = 0
        for z in range(len(spread_test_files)):

            print('spread image ' +repr(z) +'/' + repr(len(spread_test_files)))
            img_t = torch.FloatTensor(num_templates,1,image_size,image_size)
            img = Image.open(spread_test_files[z][:-1])
            img = loader_tensor(center_crop(img))
            for m in range(num_templates):
                img_t[m] = img

            img_t, den_temps, sp_temps = Variable(img_t.cuda()), Variable(den_temps.cuda()), Variable(sp_temps.cuda())

            dista, distb, embedded_x, embedded_y, embedded_z = tnet(img_t, den_temps, sp_temps)
            acc = test_accuracy(dista, distb)
            if(acc &gt;= majority_thresh):
                spread_correct += 1

        for z in range(len(dense_test_files)):

            print('dense image ' +repr(z) +'/' + repr(len(dense_test_files)))
            img_t = torch.FloatTensor(num_templates,1,image_size,image_size)
            img = Image.open(dense_test_files[z][:-1])
            img = loader_tensor(center_crop(img))
            for m in range(num_templates):
                img_t[m] = img

            img_t, den_temps, sp_temps = Variable(img_t.cuda()), Variable(den_temps.cuda()), Variable(sp_temps.cuda())

            dista, distb, embedded_x, embedded_y, embedded_z = tnet(img_t, sp_temps, den_temps)
            acc = test_accuracy(dista, distb)
            if(acc &gt;= majority_thresh):
                den_correct += 1

        confmat[fold, 0, 0] = den_correct
        confmat[fold, 1, 0] = len(dense_test_files) - den_correct
        confmat[fold, 1, 1] = spr_correct
        confmat[fold, 0, 1] = len(spread_test_files) - spr_correct

        # final_acc = round((correct/(len(spread_test_files) + len(dense_test_files)))*100, 4)
        # print('testing accuracy is '+repr(final_acc))
        # print('saving')
        # save_file = os.path.join(base_folder, 'fold_{}/save'.format(fold))
        # os.makedirs(save_file, exist_ok=True)
        # torch.save(tnet.state_dict(), os.path.join(save_file, "tnet_model_{}_{}.ph".format(i, final_acc)))
    # pdb.set_trace()
</code></pre>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "triplet_test_2_classes.py", line 127, in &lt;module&gt;
    tnet.eval()
AttributeError: '_IncompatibleKeys' object has no attribute 'eval'.
</code></pre>

ajwitty: <p>I should add, I am using python3.5.2 and torch 1.3.1</p>

ajwitty: <p>Solved this error…I was trying to ‘load_state_dict’ on top of putting model to cuda, instead of doing it in two steps.</p>
<p>Fixed snippet:</p>
<pre><code class="lang-auto">model = Vgg19()
tnet = Tripletnet(model).cuda()
tnet.load_state_dict(state_dict)
tnet.eval()
</code></pre>

Vijay_Maurya: <p>I had a same error, resolved by change variable assignment</p>
<pre><code class="lang-auto">model=model.load_state_dict(torch.load('model_data/finetuned_BERT_epoch_5.model', map_location='cpu'))
model.eval()
</code></pre>
<p>to</p>
<pre><code class="lang-auto">model.load_state_dict(torch.load('model_data/finetuned_BERT_epoch_5.model', map_location='cpu'))
model.eval()
</code></pre>
<p>Now it is working</p>

Nortona: <p>This methed is effective.</p>

Sahidul_Islam: <p>Thanks! Just saved my life <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=12" title=":smiley:" class="emoji" alt=":smiley:" loading="lazy" width="20" height="20"></p>