【问题标题】:X.shape[1] size doesn't fit the expected valueX.shape[1] 大小不符合预期值
【发布时间】:2020-07-15 19:49:15
【问题描述】:

我目前正在完成我的机器人学最后学位项目,我决定创建一个能够复制人类情感的开源机器人。机器人已全部设置好并准备好接收订单,但我仍在忙于编写代码。我目前的代码基于this method。这个想法是从 低 FPS 视频馈送(使用 RPi Camera V2),将这些地标馈送到经过训练的 SVM 分类器,并让它根据检测到的表达(愤怒、厌恶、恐惧、快乐、悲伤、惊喜和中性)返回 0-6 的数字)。我正在使用我使用 RPi 相机拍摄的一些照片来测试我的模型的功能,这就是我迄今为止在代码方面设法整合的内容:

# import the necessary packages
from imutils import face_utils
import dlib
import cv2
import numpy as np

import time
import argparse
import os
import sys
if sys.version_info >= (3, 0):
        import _pickle as cPickle
else:
        import cPickle
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score

from data_loader import load_data 
from parameters import DATASET, TRAINING, HYPERPARAMS

def get_landmarks(image, rects):
    if len(rects) > 1:
        raise BaseException("TooManyFaces")
    if len(rects) == 0:
        raise BaseException("NoFaces")
    return np.matrix([[p.x, p.y] for p in predictor(image, rects[0]).parts()])

# initialize dlib's face detector (HOG-based) and then create
# the facial landmark predictor
print("Initializing variables...")
p = "shape_predictor_68_face_landmarks.dat"
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(p)

# path to pretrained model
path = "saved_model.bin"

# load pretrained model
print("Loading model...")
model = cPickle.load(open(path, 'rb'))

# initialize final image height & width
height = 48
width = 48

# initialize landmarks variable as empty array
landmarks = []

# load the input image and convert it to grayscale
print("Loading image...")
gray = cv2.imread("foo.jpg")

# detect faces in the grayscale image
print("Detecting faces in loaded image...")
rects = detector(gray, 0)

# loop over the face detections
print("Looping over detections...")
for (i, rect) in enumerate(rects):
    # determine the facial landmarks for the face region, then
    # convert the facial landmark (x, y)-coordinates to a NumPy
    # array
    shape = predictor(gray, rect)
    shape = face_utils.shape_to_np(shape)

    # loop over the (x, y)-coordinates for the facial landmarks
    # and draw them on the image
    for (x, y) in shape:
        cv2.circle(gray, (x, y), 2, (0, 255, 0), -1)

# show the output image with the face detections + facial landmarks
print("Storing saved image...")
cv2.imwrite("output.jpg", gray)
print("Image stored as /'output.jpg/'")

# arrange landmarks in array
print("Collecting and arranging landmarks...")
# scipy.misc.imsave('temp.jpg', image)
# image2 = cv2.imread('temp.jpg')
face_rects = [dlib.rectangle(left=1, top=1, right=47, bottom=47)]
landmarks = get_landmarks(gray, face_rects)

# load data
print("Loading collected data into predictor...")
print("Extracted landmarks: ", landmarks)
landmarks = np.array(landmarks.flatten())

# predict expression
print("Making prediction")
predicted = model.predict(landmarks)

但是,在运行代码之后,到目前为止一切似乎都很好:

Making prediction
Traceback (most recent call last):
  File "face.py", line 97, in <module>
    predicted = model.predict(landmarks)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 576, in predict
    y = super(BaseSVC, self).predict(X)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 325, in predict
    X = self._validate_for_predict(X)
  File "/usr/local/lib/python2.7/dist-packages/sklearn/svm/base.py", line 478, in _validate_for_predict
    (n_features, self.shape_fit_[1]))
ValueError: X.shape[1] = 136 should be equal to 2728, the number of features at training time

我在这个网站上搜索了类似的问题,但由于如此特定的目的,我并没有完全找到我需要的东西。我从事设计和研究工作已经有一段时间了,但是找到使代码工作所需的所有 sn-ps 花费了我最多的时间,我很想尽快完善这个概念因为演示日期很快就要到了。非常欢迎任何和所有的贡献!

Here's the trained model I'm currently using, by the way.

【问题讨论】:

    标签: python numpy machine-learning scikit-learn svm


    【解决方案1】:

    我可能很傻,但看起来你定义了路径之后你用它来加载你的模型。 对于包含文件位置的变量来说,路径似乎是一个非常糟糕的名称,也许 modelFileLocation 不太可能已经定义。

    【讨论】:

    • 我的错!在重新测试之前,我对代码做了一些修改,所以我在发布之前移动了模型加载部分,因为在做任何其他事情之前加载所有重数据似乎更好。我会将帖子编辑回工作状态!但是,最初提到的错误仍然存​​在。
    【解决方案2】:

    解决了!原来我的模型是使用 HOG 特征和 Dlib 地标的组合进行训练的,但是我只是将地标提供给预测器,这导致了大小差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-28
      • 2020-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多