TypeError : '_IncompatibleKeys'개체를 호출 할 수 없습니다.

리즈

다중 레이블 분류 문제에 대해 CNN을 훈련하고 .pt있으며 torch.save(model.state_dict(), "model.pt"). 어떤 이유로 predict(x)이미지 배열을 입력으로 사용하는 사용자 지정 함수 사용하여 모델을 테스트 할 때 다음 오류가 발생 TypeError: '_IncompatibleKeys' object is not callable합니다.. 아래 cade의 마지막 청크를 지적합니다 : y_test_pred = model(images_tensors). 여기서 문제가 무엇인지에 대한 아이디어가 있습니까?

import numpy as np
import cv2
import torch
from torch import nn
import torch.nn.functional as F
import os


class Net(nn.Module):
    def __init__(self, classes_number):
        super().__init__()
        self.ConvLayer1 = nn.Sequential(
            nn.Conv2d(1, 8, 5),  # inp (1, 512, 512)
            nn.MaxPool2d(2),
            nn.ReLU()  # op (8, 254, 254)
        )
        self.ConvLayer2 = nn.Sequential(
            nn.Conv2d(8, 16, 3),  # inp (8, 254, 254)
            nn.MaxPool2d(2),
            nn.ReLU(),
            nn.BatchNorm2d(16)  # op (16, 126, 126)
        )
        self.ConvLayer3 = nn.Sequential(
            nn.Conv2d(16, 32, 3),  # inp (16, 126, 126)
            nn.MaxPool2d(2),
            nn.ReLU(),
            nn.BatchNorm2d(32)  # op (32, 62, 62)
        )
        self.ConvLayer4 = nn.Sequential(
            nn.Conv2d(32, 64, 3),  # inp (32, 62, 62)
            nn.MaxPool2d(2),
            nn.ReLU()  # op (64, 30, 30)
        )
        self.Lin1 = nn.Linear(30 * 30 * 64, 1500)
        self.drop = nn.Dropout(0.5)
        self.Lin2 = nn.Linear(1500, 150)
        self.drop = nn.Dropout(0.3)
        self.Lin3 = nn.Linear(150, classes_number)

    def forward(self, x):
        x = self.ConvLayer1(x)
        x = self.ConvLayer2(x)
        x = self.ConvLayer3(x)
        x = self.ConvLayer4(x)
        x = x.view(x.size(0), -1)
        x = F.relu(self.Lin1(x))
        x = self.drop(x)
        x = F.relu(self.Lin2(x))
        x = self.drop(x)
        x = self.Lin3(x)
        out = torch.sigmoid(x)
        return out


def predict(x):
    # On the exam, x will be a list of all the paths to the images of our held-out set
    images = []
    for img_path in x:
        img = cv2.imread(img_path)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # Turn into greyscale
        img = cv2.resize(img, (512, 512))
        images.append(img)
    images = np.array(images)
    images = images.reshape(len(images), 1, images.shape[1], images.shape[1])  # converting(n,512,512)>(n,1,512,512)
    images_tensors = torch.FloatTensor(np.array(images))
    images_tensors = images_tensors.to(device)
    classes = ["red blood cell", "difficult", "gametocyte", "trophozoite", "ring", "schizont", "leukocyte"]
    model = Net(len(classes))
    model = model.load_state_dict(torch.load('model.pt'))


    y_test_pred = model(images_tensors)
    y_test_pred[y_test_pred > 0.49] = 1
    y_test_pred[y_test_pred < 0.5] = 0

    return y_test_pred.cpu().detach()
잭 테일러

버기 라인은 model = model.load_state_dict(torch.load('model.pt')). 에 따르면 워드 프로세서 , load_state_dict이있는 NamedTuple 반환 missing_keysunexpected_keys필드가 아닌 모델 객체를. 코드에서이 명명 된 튜플을 model변수에 할당 model한 다음 다음 줄에서 호출 할 때 실제로 NamedTuple을 호출하려고 시도하여 TypeError를 발생시킵니다.

대신, 저장 및로드 모듈 문서 에 따라 다음과 같이해야합니다.

model = Net(len(classes))
model.load_state_dict(torch.load(PATH))
model.eval()

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

TypeError : 'NoneType'개체는 CircleCI를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'CurrencyConverter'개체를 호출 할 수 없습니다.

분류에서Dev

firebase = firebase (config) TypeError : 'module'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

분류에서Dev

TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

분류에서Dev

Q : TypeError : '_csv.reader'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Int64Index'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Player'개체는 Django를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Post'개체를 호출 할 수 없습니다.

분류에서Dev

groupby-TypeError 'DataFrame'개체를 호출 할 수 없습니다.

분류에서Dev

Python Script TypeError : 'int'개체를 호출 할 수 없습니다.

분류에서Dev

Python 오류 : TypeError : 'list'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : '게임'개체를 호출 할 수 없습니다.

분류에서Dev

Python / Pygame : TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Python-TypeError : 'list'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Weather'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'Aircraft'개체를 호출 할 수 없습니다.

분류에서Dev

Python TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

django-selenium TypeError : 'str'개체를 호출 할 수 없습니다.

분류에서Dev

Python setter TypeError : 'int'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : print를 호출 할 때 'str'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 스크래핑 할 때 'NoneType'개체를 호출 할 수 없습니다.

분류에서Dev

TypeError : 'numpy.float64'개체를 호출 할 수 없습니까?

분류에서Dev

TypeError : 'str'개체는 MySQL에서 호출 할 수 없습니다.

분류에서Dev

'모듈'개체의 TypeError는 호출 할 수 없습니다.

분류에서Dev

TypeError at / 'SimpleLazyObject'개체는 호출 할 수 없습니다.

분류에서Dev

Python timeit-TypeError : '모듈'개체를 호출 할 수 없습니다.

분류에서Dev

Pytorch 1.7.0 | DataLoader 오류-TypeError : '모듈'개체를 호출 할 수 없습니다.

Related 관련 기사

  1. 1

    TypeError : 'NoneType'개체는 CircleCI를 호출 할 수 없습니다.

  2. 2

    TypeError : 'CurrencyConverter'개체를 호출 할 수 없습니다.

  3. 3

    firebase = firebase (config) TypeError : 'module'개체를 호출 할 수 없습니다.

  4. 4

    TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

  5. 5

    TypeError : '목록'개체를 호출 할 수 없습니다-assertWarns ()

  6. 6

    Q : TypeError : '_csv.reader'개체를 호출 할 수 없습니다.

  7. 7

    TypeError : 'Int64Index'개체를 호출 할 수 없습니다.

  8. 8

    TypeError : 'Player'개체는 Django를 호출 할 수 없습니다.

  9. 9

    TypeError : 'Post'개체를 호출 할 수 없습니다.

  10. 10

    groupby-TypeError 'DataFrame'개체를 호출 할 수 없습니다.

  11. 11

    Python Script TypeError : 'int'개체를 호출 할 수 없습니다.

  12. 12

    Python 오류 : TypeError : 'list'개체를 호출 할 수 없습니다.

  13. 13

    TypeError : '게임'개체를 호출 할 수 없습니다.

  14. 14

    Python / Pygame : TypeError : '모듈'개체를 호출 할 수 없습니다.

  15. 15

    Python-TypeError : 'list'개체를 호출 할 수 없습니다.

  16. 16

    TypeError : 'Weather'개체를 호출 할 수 없습니다.

  17. 17

    TypeError : 'Aircraft'개체를 호출 할 수 없습니다.

  18. 18

    Python TypeError : '모듈'개체를 호출 할 수 없습니다.

  19. 19

    TypeError '모듈'개체를 호출 할 수 없습니다.

  20. 20

    django-selenium TypeError : 'str'개체를 호출 할 수 없습니다.

  21. 21

    Python setter TypeError : 'int'개체를 호출 할 수 없습니다.

  22. 22

    TypeError : print를 호출 할 때 'str'개체를 호출 할 수 없습니다.

  23. 23

    TypeError : 스크래핑 할 때 'NoneType'개체를 호출 할 수 없습니다.

  24. 24

    TypeError : 'numpy.float64'개체를 호출 할 수 없습니까?

  25. 25

    TypeError : 'str'개체는 MySQL에서 호출 할 수 없습니다.

  26. 26

    '모듈'개체의 TypeError는 호출 할 수 없습니다.

  27. 27

    TypeError at / 'SimpleLazyObject'개체는 호출 할 수 없습니다.

  28. 28

    Python timeit-TypeError : '모듈'개체를 호출 할 수 없습니다.

  29. 29

    Pytorch 1.7.0 | DataLoader 오류-TypeError : '모듈'개체를 호출 할 수 없습니다.

뜨겁다태그

보관