簡體   English   中英

有沒有更快的方法來獲取MNIST數據集的局部二進制模式?

[英]Is there a faster way to get the Local Binary Pattern of the MNIST dataset?

我需要知道是否有更快的方法來獲取LBP和MNIST數據集的結果直方圖。 這將用於手寫文本識別,通過我還沒有決定的模型..

我已經加載了MNIST數據集並將其拆分為x,y訓練集和基於tensorflow教程的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)

我知道它會很慢,而且確實很慢。 所以我正在尋找更多方法來加快速度,或者是否已經有一個我需要這個信息的數據集版本。

我認為沒有一種簡單的方法可以加快圖像的迭代速度。 人們可能期望使用NumPy的vectorizeapply_along_axis可以提高性能,但這些解決方案實際上比for循環(或列表理解)慢。

演示

迭代圖像的不同選擇:

def compr(imgs):
    hists = [getLocalBinaryPattern(img, 8, 1)[1] for img in imgs]
    return hists

def vect(imgs):
    lbp81riu2 = lambda img: getLocalBinaryPattern(img, 8, 1)[1]
    vec_lbp81riu2 = np.vectorize(lbp81riu2, signature='(m,n)->(k)')
    hists = vec_lbp81riu2(imgs)
    return hists

def app(imgs):
    lbp81riu2 = lambda img: getLocalBinaryPattern(img.reshape(28, 28), 8, 1)[1]
    pixels = np.reshape(imgs, (len(imgs), -1))
    hists = np.apply_along_axis(lbp81riu2, 1, pixels)
    return hists

結果:

In [112]: (x_train, y_train), (x_test, y_test) = loadDataset()

In [113]: %timeit -r 3 compr(x_train)
1 loop, best of 3: 14.2 s per loop

In [114]: %timeit -r 3 vect(x_train)
1 loop, best of 3: 17.1 s per loop

In [115]: %timeit -r 3 app(x_train)
1 loop, best of 3: 14.3 s per loop

In [116]: np.array_equal(compr(x_train), vect(x_train))
Out[116]: True

In [117]: np.array_equal(compr(x_train), app(x_train))
Out[117]: True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM