【发布时间】:2019-09-25 12:34:48
【问题描述】:
我需要知道是否有更快的方法来获取 LBP 和生成的 MNIST 数据集直方图。这将用于手写文本识别,通过一个我还没有决定的模型..
我已经加载了 MNIST 数据集,并根据 tensorflow 教程将其拆分为 x、y 训练集和 x、y 测试集。
然后我使用cv2 反转图像。
从那里我定义了一个函数,使用skimage 来获取输入图像的 LBP 和相应的直方图
我终于使用经典的for 循环遍历图像,获取它们的直方图,将它们存储在单独的列表中,并返回新列表以及训练集和测试集的未更改标签列表。
这是加载 MNIST 数据集的函数:
def loadDataset():
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# should I invert it or not?
x_train = cv2.bitwise_not(x_train)
x_test = cv2.bitwise_not(x_test)
return (x_train, y_train), (x_test, y_test)
这里是获取LBP和对应直方图的函数:
def getLocalBinaryPattern(img, points, radius):
lbp = feature.local_binary_pattern(img, points, radius, method="uniform")
hist, _ = np.histogram(lbp.ravel(),
bins=np.arange(0, points + 3),
range=(0, points + 2))
return lbp, hist
最后是遍历图像的函数:
def formatDataset(dataset):
(x_train, y_train), (x_test, y_test) = dataset
x_train_hst = []
for i in range(len(x_train)):
_, hst = getLocalBinaryPattern(x_train[i], 8, 1)
print("Computing LBP for training set: {}/{}".format(i, len(x_train)))
x_train_hst.append(hst)
print("Done computing LBP for training set!")
x_test_hst=[]
for i in range(len(x_test)):
_, hst = getLocalBinaryPattern(x_test[i], 8, 1)
print("Computing LBP for test set: {}/{}".format(i, len(x_test)))
x_test_hst.append(hst)
print("Done computing LBP for test set!")
print("Done!")
return (x_train_hst, y_train), (x_test_hst, y_test)
我知道它会很慢,而且确实很慢。因此,我正在寻找更多方法来加快速度,或者是否已经有一个版本的数据集包含我需要的这些信息。
【问题讨论】:
标签: python numpy scikit-image tensorflow2.0 lbph-algorithm