【问题标题】:I want to standardise my image data for preprocessing for deep learning我想标准化我的图像数据以进行深度学习的预处理
【发布时间】:2019-07-03 00:48:21
【问题描述】:

我将图像存储在 gray_img_here 中,这是一个二维数组的 python 列表。 我想通过应用来标准化每个图像:X = (X-u)/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:只有 size-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],
   ...,

【问题讨论】:

  • 我已经读过了。我想标准化我的数据而不是标准化。
  • 你能展示一下数组 gray_img_numpy 的样子吗?
  • 我添加了它@EdekiOkoh
  • 不是错误本身,但我会查看这一行 gray_img_here[i] = float(gray_img_here[i] - np.mean(gray_img_numpy[i])) / float(np.std(gray_img_numpy [i], axis=0)。我不认为这是在做你认为它在做的事情。你想使用数组中每个数组的平均值,而不是第 i 个元素

标签: python numpy


【解决方案1】:

标准化 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 数组并以相同的方式对其进行寻址

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2021-12-06
    • 2020-09-30
    • 1970-01-01
    • 2021-04-28
    • 2015-10-09
    • 2017-03-04
    相关资源
    最近更新 更多