我使用迁移学习方法来训练模型并保存检测到的最佳权重。在另一个脚本中,我尝试使用保存的权重进行预测。但我收到如下错误。我已经使用 ResNet 对网络进行了微调,并且有 4 个类。
RuntimeError: Error(s) in loading state_dict for ResNet:
size mismatch for fc.bias: copying a param of torch.Size([1000]) from
checkpoint, where the shape is torch.Size([4]) in current model.
size mismatch for fc.weight: copying a param of torch.Size([1000,
512]) from checkpoint, where the shape is torch.Size([4, 512]) in
current model.
Run Code Online (Sandbox Code Playgroud)
我使用以下代码来预测输出:
checkpoint = torch.load("./models/custom_model13.model")
model = resnet18(pretrained=True)
model.load_state_dict(checkpoint)
model.eval()
def predict_image(image_path):
transformation = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
image_tensor = transformation(image).float()
image_tensor …Run Code Online (Sandbox Code Playgroud) 我正在研究一个涉及检测人体的机器人项目,我正在使用张量流和预定义的数据集来创建训练模型.由于我是机器学习的新手,我无法正确获取分类器的输出.我只需要人物检测,并希望避免检测球,笔记本电脑或其他物体.现在我的网络摄像头检测到所有的物体,如球,蝙蝠,笔记本电脑,电视等.我需要的输出只有得分为80%及以上的人.
我用来创建模型的代码是
import numpy as np
import os
import six.moves.urllib as urllib
import sys
import tarfile
import tensorflow as tf
import zipfile
from collections import defaultdict
from io import StringIO
from matplotlib import pyplot as plt
from PIL import Image
from utils import label_map_util
from utils import visualization_utils as vis_util
MODEL_NAME = 'ssd_mobilenet_v1_coco_11_06_2017'
MODEL_FILE = MODEL_NAME + '.tar.gz'
DOWNLOAD_BASE = 'http://download.tensorflow.org/models/object_detection/'
PATH_TO_CKPT = MODEL_NAME + '/frozen_inference_graph.pb'
PATH_TO_LABELS = os.path.join('data', 'mscoco_label_map.pbtxt')
NUM_CLASSES = 90
if not os.path.exists(MODEL_NAME + '/frozen_inference_graph.pb'):
print ('Downloading …Run Code Online (Sandbox Code Playgroud)