簡體   English   中英

我想標准化我的圖像數據以進行深度學習的預處理

[英]I want to standardise my image data for preprocessing for deep learning

我將圖像存儲在gray_img_here,這是2d數組的python列表。 我想通過應用以下方法來標准化每個圖像:X =(Xu)/ S其中X是像素值,U是圖像的均值,S是該像素的偏差

def normalizeImages(self, gray_img_here):
    print "Normalizing the gray images..."
    print
    gray_img_numpy = np.array(gray_img_here)
    for i in range(len(gray_img_here)):
        print
        # print "mean of the {}th image", np.mean(gray_img_numpy[i])
        # print "std dev. of the {}th image", np.std(gray_img_numpy[i])
        # print
        gray_img_here[i] = float(gray_img_here[i] - np.mean(gray_img_numpy[i])) / float(np.std(gray_img_numpy[i], axis=0))

    return gray_img_here

但是我得到了錯誤:gray_img_here [i] = float(gray_img_here [i]-np.mean(gray_img_numpy [i]))/ float(np.std(gray_img_numpy [i],axis = 0))

TypeError:只有大小為1的數組可以轉換為Python標量

gray_img_here看起來像這樣

[array([[ 37,  39,  41, ..., 119, 113, 109],
   [ 38,  40,  41, ..., 119, 113, 109],
   [ 39,  41,  42, ..., 117, 112, 108],
   ...,
   [ 25,  25,  26, ..., 168, 180, 182],
   [ 25,  26,  26, ..., 179, 191, 189],
   [ 26,  26,  26, ..., 184, 196, 191]], dtype=uint8), array([[ 91,  97, 101, ...,  48,  49,  51],
   [ 89,  93,  98, ...,  44,  45,  45],
   [ 85,  88,  94, ...,  40,  41,  41],
   ...,
   [137,  90,  52, ...,  35,  36,  36],
   [163, 103,  68, ...,  35,  35,  35],
   [216, 148, 107, ...,  35,  35,  34]], dtype=uint8), array([[ 64,  75,  93, ...,  85,  83,  82],
   [ 83,  93,  98, ...,  85,  81,  80],
   [ 91,  98,  96, ...,  84,  80,  81],
   ...,

標准化3D矩陣要容易得多。 就像是:

X=np.array(gray_img_here,dtype=float)
Xm=X.mean(axis=0,keepdims=True)
Xstd=X.std(axis=(1,2),keepdims=True)
X_standardised=list((X-Xm)/Xstd)

第一行制作了一個3D浮點數組,因此您不必擔心在每一步都進行轉換。 然后進行簡單的標准化,並根據需要將其更改回列表。

注意:無需將其保留為列表,您可以擁有3D numpy數組並以相同的方式尋址

暫無
暫無

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

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