【问题标题】:Python scikit-learn provide custom training dataPython scikit-learn 提供自定义训练数据
【发布时间】:2017-02-13 17:59:12
【问题描述】:

我开始使用 scikit-learn 来执行面部识别。我已经试用了提供的示例here 并且工作正常。我现在正在尝试使用我自己的包含图像的自定义数据来测试面部识别。我正在使用来自 here 的面部数据库。我现在坚持将这些图像作为训练集加载到我的程序中。我正在使用下面的代码来加载所有图像:

    # get the training data


def read_images(path, sz=None):
    """Reads the images in a given folder, resizes images on the fly if size is given.

    Args:
        path: Path to a folder with subfolders representing the subjects (persons).
        sz: A tuple with the size Resizes 

    Returns:
        A list [X,y]

            X: The images, which is a Python list of numpy arrays.
            y: The corresponding labels (the unique number of the subject, person) in a Python list.
    """
    c = 0
    X, y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                try:
                    im = Image.open(os.path.join(subject_path, filename))
                    im = im.convert("L")
                    # resize to given size (if given)
                    if (sz is not None):
                        im = im.resize(sz, Image.ANTIALIAS)
                    X.append(np.asarray(im, dtype=np.uint8))
                    y.append(c)
                except IOError, (errno, strerror):
                    print("I/O error({0}): {1}".format(errno, strerror))
                except:
                    print("Unexpected error:", sys.exc_info()[0])
                    raise
            c = c+1
    return [X, y]

if __name__ == '__main__':
    if len(sys.argv) < 3:
        print('No image supplied.')
        sys.exit()

    # Now read in the image data. This must be a valid path!
    [X, y] = read_images(sys.argv[1])

    print(len(X), len(y))

    X_train = np.vstack(X)
    print(X_train)
    y_train = np.array(y)

    image_path = sys.argv[2]
    image = np.array(cv2.imread(image_path), dtype=np.uint8)
    if np.ndim(image) == 3:
        image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.equalizeHist(image)
    # create a CLAHE object (Arguments are optional).
    clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
    image = clahe.apply(image)

    # detect face in this image
    detector = FaceDetector()
    X_test = []
    for i, d in enumerate(detector.detect(image)):
        x, y, w, h = d.left(), d.top(), d.right() - \
            d.left(), d.bottom() - d.top()
        a = image[y:(y+h), x:(x+w)]
        b = cv2.resize(a, (130, 130), interpolation=cv2.INTER_CUBIC)
        X_test.append(np.asarray(b, dtype=np.uint8))

    X_test = np.vstack(X_test)

    ##########################################################################
    # Compute a PCA (eigenfaces) on the face dataset (treated as unlabeled
    # dataset): unsupervised feature extraction / dimensionality reduction

    n_components = 150

    print('Extracting the top {} eigenfaces from faces'.format(n_components))
    t0 = time()
    pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)
    print("done in %0.3fs" % (time() - t0))

    print("Projecting the input data on the eigenfaces orthonormal basis")
    t0 = time()
    X_train_pca = pca.transform(X_train)
    **X_test_pca = pca.transform(X_test)**
    print("done in %0.3fs" % (time() - t0))

    ##########################################################################
    # Train a SVM classification model

    print("Fitting the classifier to the training set")
    t0 = time()
    param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
                  'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
    clf = GridSearchCV(SVC(kernel='rbf', class_weight='balanced'), param_grid)
    clf = clf.fit(X_train_pca, y_train)
    print("done in %0.3fs" % (time() - t0))
    print("Best estimator found by grid search:")
    print(clf.best_estimator_)

    ##########################################################################
    # Quantitative evaluation of the model quality on the test set

    print("Predicting people's names on the test set")
    t0 = time()
    y_pred = clf.predict(X_test_pca)
    print("done in %0.3fs" % (time() - t0))

在此之后,我还获得了一张测试图像来针对上述示例运行: 给出错误的行是 X_test_pca = pca.transform(X_test)

错误是:

ValueError: operands could not be broadcast together with shapes (130,130) (92,)

我怀疑这与我的数据格式不正确有关。

【问题讨论】:

  • 你能告诉我们哪一行引发了错误吗?我看不到任何上面会导致这个问题的东西。
  • 我已经包含了我的整个代码示例
  • @Denny -- 确保trainingDataSET(在.fit( X_train_pca, y_train )中使用)和testingDataSet(在.fit( X_test_pca )中进一步使用)具有完全相同的 .shape-s 和 colourdepths(否则 cv2-预处理它们以便拥有 ...)

标签: python numpy scikit-learn


【解决方案1】:

这是因为您用来训练 pca 的 X_train 即行:

pca = RandomizedPCA(n_components=n_components, whiten=True).fit(X_train)

,有 92 个原始列/特征,而您的 X_test 有 130 个特征。您在此行手动为您的测试数据提供 130 的大小:

b = cv2.resize(a, (130, 130), interpolation=cv2.INTER_CUBIC)

一旦你纠正了这个问题,你应该会没事的。

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2016-11-26
    • 2014-09-17
    • 2019-01-07
    • 2015-12-30
    • 2013-07-04
    • 2013-10-31
    • 2014-05-01
    • 2018-04-06
    相关资源
    最近更新 更多