【问题标题】:Getting size error in opencv while working on MNIST dataset在处理 MNIST 数据集时在 opencv 中出现大小错误
【发布时间】:2019-01-29 17:40:12
【问题描述】:

我正在使用 OpenCV 和 ML 模块训练 MLP。我收到一个无法修复的未知错误:

“错误:OpenCV(3.4.3)/io/opencv/modules/ml/src/data.cpp:257:错误: (-215:断言失败) samples.type() == CV_32F ||样本.type() == 函数 'setData' 中的 CV_32S"

这是我的代码:

    from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train.shape, y_train.shape
import numpy as np
np.unique(y_train)
import matplotlib.pyplot as plt
%matplotlib inline
for i in range(10):
  plt.subplot(2, 5, i+1)
  plt.imshow(X_train[i, :, :], cmap='gray')
  plt.axis('off')
from sklearn.preprocessing import OneHotEncoder
enc=OneHotEncoder(sparse=False, dtype=np.float32)
y_train_pre=enc.fit_transform(y_train.reshape(-1,1))
y_test_pre=enc.fit_transform(y_test.reshape(-1,1))
X_train_pre=X_train.reshape((X_train.shape[0], -1))
X_train_pre=X_train.astype(np.float32) /255.0
X_test_pre=X_test.reshape((X_test.shape[0], -1))
X_test_pre=X_test.astype(np.float32) / 255.0
import cv2
mlp=cv2.ml.ANN_MLP_create()
mlp.setLayerSizes(np.array([784, 512, 512, 10]))
mlp.setActivationFunction(cv2.ml.ANN_MLP_SIGMOID_SYM, 2.5, 1.0)
mlp.setTrainMethod(cv2.ml.ANN_MLP_BACKPROP)
mlp.setBackpropWeightScale(0.00001)
term_mode= (cv2.TERM_CRITERIA_MAX_ITER + cv2.TERM_CRITERIA_EPS)
term_max_iter=10
term_eps=0.01
mlp.setTermCriteria((term_mode, term_max_iter, term_eps))
mlp.train(X_train_pre, cv2.ml.ROW_SAMPLE, y_train_pre)

运行最后一个单元后出现错误。这意味着在训练中!我无法修复它,但它们与层的大小有什么关系吗?或使用 numpy 进行类型转换?如果你们能指导我,它会帮助我。提前谢谢各位。

【问题讨论】:

    标签: python opencv tensorflow machine-learning keras


    【解决方案1】:

    图像需要是一维向量,但它们的形状是 [28,28]。例如,这将重塑图像并且会起作用:

    mlp.train(X_train_pre.reshape(60000,-1), cv2.ml.ROW_SAMPLE, y_train_pre)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-11
      • 2012-07-05
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多