import torch
import torchvision
import cv2 as cv
from utils.utils import letter_box
from model.backbone import ResNet18
model1 = ResNet18(1)
model2 = torchvision.models.resnet18(progress=False)
fc = model2.fc
model2.fc = torch.nn.Linear(512, 1)
# print(model)
model_dict1 = model1.state_dict()
model_dict2 = torch.load('resnet18.pth')
model_list1 = list(model_dict1.keys())
model_list2 = list(model_dict2.keys())
len1 = len(model_list1)
len2 = len(model_list2)
minlen = min(len1, len2)
for n in range(minlen):
if model_dict1[model_list1[n]].shape != model_dict2[model_list2[n]].shape:
continue
model_dict1[model_list1[n]] = model_dict2[model_list2[n]]
model1.load_state_dict(model_dict1)
missing, unspected = model2.load_state_dict(model_dict2)
image = cv.imread('zhn1.jpg')
image = letter_box(image, 224)
image = image[:, :, ::-1].transpose(2, 0, 1)
print('Network loading complete.')
model1.eval()
model2.eval()
with torch.no_grad():
image = torch.tensor(image/256, dtype=torch.float32).unsqueeze(0)
predict1 = model1(image)
predict2 = model2(image)
print('finished')
# torch.save(model.state_dict(), 'resnet18.pth')
Read More:
- RuntimeError: Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor) should be the
- geom_ Warning message: sign function’s own error
- Mmdetection reports an error when running its own coco dataset. Does not match the length of \ ` classes \ ` 80) in cocodataset
- After JQ gets the tag element itself, it gets its own HTML text format (find only finds one level of child elements)
- Error in installing torch vision or pilot on Linux or Jetson nano: the headers or library files could not be found for JPEG
- To solve the problem of increasing video memory when training network (torch)
- (26)RuntimeError: Error(s) in loading state_dict for YoloBody:size mismatch for yolo_head3.1.weight
- RuntimeError: NCCL error in: /pytorch/torch/lib/c10d/ProcessGroupNCCL.cpp:784 torch
- ValueError: Expected more than 1 value per channel when training, got input size torch.Size([1, 256,
- How to use torch.sum()
- Build LNMPR Error — php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution
- Wireless network solution of deepin Linux network card driver (manual) installation
- Linux virtual machine network “job for” network.service failed because the control process exited with error code”
- Successfully solved the problem of “runtimee” in RESNET dataset classification rror:expected scalar type Long but found Float”
- Docker creation container cannot find network card: error response from daemon: network XXXX not found
- Debian using networking to restart the network can not start the solution of network card
- Solution summary of VMware network card without IP and failure to start network card (continuous update)
- ImportError: cannot import name ‘Optional‘ from ‘torch.jit.annotations‘ (F:\Python37\lib\site-packag
- Copying a param with shape torch. Size ([262, 2048]), parameter size does not match
- RuntimeError: Couldn‘t open shared file mapping: <torch_16716_3565374679>, error code: <1455>