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

When using ‘load_state_dict’ to load saved triplet net, get for network, but when setting to eval():

Code:

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 > 0).sum().item()*1.0/dista.size()[0]

def test_accuracy(dista, distb):
    margin = 0
    pred = (dista - distb - margin).cpu().data
    return (pred > 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 >= 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 >= 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()
Traceback (most recent call last):
  File "triplet_test_2_classes.py", line 127, in <module>
    tnet.eval()
AttributeError: '_IncompatibleKeys' object has no attribute 'eval'.
2 Likes

I should add, I am using python3.5.2 and torch 1.3.1

1 Like

Solved this error…I was trying to ‘load_state_dict’ on top of putting model to cuda, instead of doing it in two steps.

Fixed snippet:

model = Vgg19()
tnet = Tripletnet(model).cuda()
tnet.load_state_dict(state_dict)
tnet.eval()
2 Likes

I had a same error, resolved by change variable assignment

model=model.load_state_dict(torch.load('model_data/finetuned_BERT_epoch_5.model', map_location='cpu'))
model.eval()

to

model.load_state_dict(torch.load('model_data/finetuned_BERT_epoch_5.model', map_location='cpu'))
model.eval()

Now it is working

6 Likes

This methed is effective.