📜  如何在 Keras 中对图像像素进行归一化、居中和标准化?

📅  最后修改于: 2022-05-13 01:55:30.853000             🧑  作者: Mango

如何在 Keras 中对图像像素进行归一化、居中和标准化?

用于深学习神经网络模型中的最关键的部分是“数据集”,或者直接地在数据集中的图像如果模型用于对象/图像检测等图像中的像素值,必须发展之前适当缩放神经网络模型。

Keras是Python的深度学习库,用于神经网络训练模型。 Keras 支持在模型训练期间缩放图像。在接下来的示例中,将使用“ImageDataGenerator”,它是 Keras 库中的一个类。它将提供一种在建模之前缩放图像像素值的技术。

像素缩放技术由三种主要类型组成,由 ImageDataGenerator 类支持:

  • 像素归一化– 在 0-1 范围内缩放像素值。
  • 像素居中– 缩放像素值使其均值为 0。
  • 像素标准化– 将像素值缩放为 0 均值和单位 (1) 方差。

像素缩放是通过向 ImageDataGenerator 指定一些参数来完成的:

计算训练数据集的缩放统计:

将神经网络拟合到数据生成器中:

创建一个单独的批处理迭代器来训练和验证数据生成器,它将执行相同的像素缩放:

拟合模型后测试数据集的批处理迭代器:

使用的数据集:

在接下来的示例中,将使用MSIST数据集对Keras库中的图像像素进行归一化、居中和标准化。它由 60,000 个训练集灰度图像和额外的 10,000 个灰度图像测试集组成,这些测试集由不同方向的“0-9”数字组成。链接到 MNIST 数据集。数据集中使用的图像类型:

标准化 K 个时代的图像像素

在将像素值从 0-255 范围重新缩放到 0-1 范围时,可以使用 ImageDataGenerator 类。 0-1 缩放的范围称为Normalization 。需要采取以下步骤对图像像素进行归一化:

  • 可以通过将像素的最大值除以像素的最小值来设置 rescale 参数来缩放 0-1 范围内的像素:1/255 = 0.0039
  • 使用生成器为测试和训练数据集创建迭代器。在这种情况下,将使用 64 的批量大小。这意味着在每个 epoch 中将有 64 张图像通过训练过程。
  • 像素归一化可以通过获取第一批缩放图像并检查像素的最小值和最大值来确认。
  • 为了使用数据生成器来拟合和评估模型,定义了卷积神经网络 (CNN) 模型,我们运行 5 个 epoch,每批次 60,000 张图像,相当于每个 epoch 938 个批次。
  • 现在,在测试数据集上评估模型,该数据集将 10,000 张图像分布在大小为 64 的批次中,相当于一个 epoch 中的 157 个步骤。

例子:

Python3
# import required modules
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.preprocessing.image import ImageDataGenerator
  
# loading the image dataset
(trainX, trainY), (testX, testY) = mnist.load_data()
  
# reshaping the dataset to have a single channel
width, height, channels = trainX.shape[1], trainX.shape[2], 1
trainX = trainX.reshape((trainX.shape[0], width, height, channels))
testX = testX.reshape((testX.shape[0], width, height, channels))
trainY = to_categorical(trainY)
testY = to_categorical(testY)
  
# confirming scale of pixel values
print('Train min=%.3f, max=%.3f' % (trainX.min(), trainX.max()))
print('Test min=%.3f, max=%.3f' % (testX.min(), testX.max()))
  
# creating the image data generator [1.0/255.0 = 0.00392156862]
datagen = ImageDataGenerator(rescale=1.0/255.0)
  
# preparing an iterator for scaling images
train_iterator = datagen.flow(trainX, trainY, batch_size=64)
test_iterator = datagen.flow(testX, testY, batch_size=64)
print('Batches train=%d, test=%d' % (len(train_iterator),
                                     len(test_iterator)))
# confirming- the scaling works
batchX, batchy = train_iterator.next()
print('Batch shape=%s, min=%.3f, max=%.3f' % (batchX.shape, batchX.min(),
                                              batchX.max()))
# defining the model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(width, height,
                                                             channels)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
  
# compiling the model
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])
  
# fitting the model with generator
model.fit_generator(train_iterator, steps_per_epoch=len(train_iterator),
                    epochs=5)
  
# evaluating the model
_, acc = model.evaluate_generator(test_iterator, steps=len(test_iterator),
                                  verbose=0)
print('Test Accuracy: %.3f' % (acc * 100))


Python3
# import required modules
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.preprocessing.image import ImageDataGenerator
  
# loading the dataset
(trainX, trainY), (testX, testY) = mnist.load_data()
  
# reshaping the dataset to have a single channel
width, height, channels = trainX.shape[1], trainX.shape[2], 1
trainX = trainX.reshape((trainX.shape[0], width, height, channels))
testX = testX.reshape((testX.shape[0], width, height, channels))
trainY = to_categorical(trainY)
testY = to_categorical(testY)
  
# creating image data generator for centering images
datagen = ImageDataGenerator(featurewise_center=True)
  
# calculating the mean of the training dataset
datagen.fit(trainX)
  
# preparing iterators to scale images
train_iterator = datagen.flow(trainX, trainY, batch_size=64)
test_iterator = datagen.flow(testX, testY, batch_size=64)
print('Batches train=%d, test=%d' % (len(train_iterator),
                                     len(test_iterator)))
  
# defining the model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(width, height,
                                                             channels)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
  
# compiling the model
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])
  
# fitting the model with the generator
model.fit_generator(train_iterator, steps_per_epoch=len(train_iterator),
                    epochs=5)
  
# evaluating the model
_, acc = model.evaluate_generator(test_iterator, steps=len(test_iterator),
                                  verbose=0)
print('Test Accuracy: %.3f' % (acc * 100))


Python3
# import required modules
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.preprocessing.image import ImageDataGenerator
  
# loading the dataset
(trainX, trainY), (testX, testY) = mnist.load_data()
  
# reshaping the dataset to have a single channel
width, height, channels = trainX.shape[1], trainX.shape[2], 1
trainX = trainX.reshape((trainX.shape[0], width, height, channels))
testX = testX.reshape((testX.shape[0], width, height, channels))
trainY = to_categorical(trainY)
testY = to_categorical(testY)
  
# creating the image data generator to standardize images
datagen = ImageDataGenerator(featurewise_center=True,
                             featurewise_std_normalization=True)
  
# calculating the mean on the training dataset
datagen.fit(trainX)
  
# preparing iterators to scale images
train_iterator = datagen.flow(trainX, trainY, batch_size=64)
test_iterator = datagen.flow(testX, testY, batch_size=64)
print('Batches train=%d, test=%d' % (len(train_iterator),
                                     len(test_iterator)))
  
# defining the model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu',
                 input_shape=(width, height, channels)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
  
# compiling the  model
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])
  
# fitting the model with the generator
model.fit_generator(train_iterator, steps_per_epoch=len(train_iterator),
                    epochs=5)
  
# evaluating the model
_, acc = model.evaluate_generator(test_iterator, steps=len(test_iterator),
                                  verbose=0)
print('Test Accuracy: %.3f' % (acc * 100))


运行上述脚本后,它将从 amazon-AWS 网站下载 MNIST 数据集。下载数据集需要一些时间。



在检索第一批包含 64 张 28*28 大小和像素值在 0-1 范围内的图像时。

该模型在测试数据集上进行评估,应用归一化技术。

神经网络模型的准确率为98.07%

在 Keras 中居中图像像素

图像像素通过计算像素的平均值然后从每个图像中减去它们来居中。这种技术被称为居中。这具有将像素的分布值集中在零上的效果,这意味着居中的图像将具有“零均值”像素值。需要采取以下步骤来使图像像素居中:

  • 使用统计方法计算训练数据集上 ImageDataGenerator 类中的平均像素值。
  • Keras 中的平均像素值不需要任何统计数据。它在 Keras 中被称为样本集中。
  • 计算统计数据后,完成特征中心化。
  • 在批处理迭代器中计算一批图像的平均值。均值的值接近于零,而不是完全为零。
  • 在训练数据集上设置批量大小。它可以是任何接近于零的值。

例子:

蟒蛇3

# import required modules
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.preprocessing.image import ImageDataGenerator
  
# loading the dataset
(trainX, trainY), (testX, testY) = mnist.load_data()
  
# reshaping the dataset to have a single channel
width, height, channels = trainX.shape[1], trainX.shape[2], 1
trainX = trainX.reshape((trainX.shape[0], width, height, channels))
testX = testX.reshape((testX.shape[0], width, height, channels))
trainY = to_categorical(trainY)
testY = to_categorical(testY)
  
# creating image data generator for centering images
datagen = ImageDataGenerator(featurewise_center=True)
  
# calculating the mean of the training dataset
datagen.fit(trainX)
  
# preparing iterators to scale images
train_iterator = datagen.flow(trainX, trainY, batch_size=64)
test_iterator = datagen.flow(testX, testY, batch_size=64)
print('Batches train=%d, test=%d' % (len(train_iterator),
                                     len(test_iterator)))
  
# defining the model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(width, height,
                                                             channels)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
  
# compiling the model
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])
  
# fitting the model with the generator
model.fit_generator(train_iterator, steps_per_epoch=len(train_iterator),
                    epochs=5)
  
# evaluating the model
_, acc = model.evaluate_generator(test_iterator, steps=len(test_iterator),
                                  verbose=0)
print('Test Accuracy: %.3f' % (acc * 100))

运行上述脚本后,它将从 amazon-AWS 网站下载 MNIST 数据集。下载数据集需要一些时间。

检索单个批次的居中图像,我们可以清楚地看到平均像素值非常接近于零。零平均像素值确认了所需的居中效果。



神经网络的性能开始很差,并没有提高。中心值像素的范围从 -277 到 277。神经网络通常以较小的输入进行更高的训练。所以归一化应该在居中之前进行,以获得更好的结果。

神经网络模型的准确率为99.12%

在 Keras 中标准化图像像素

标准化是一种缩放数据并假设数据分布是高斯分布的技术,并将数据分布移动到具有“零均值”和“单位 (1) 标准偏差”。具有这种分布的数据称为标准高斯分布。当神经网络的数据集总和为零并且具有在 -3.0 到 3.0 范围内的较小输入值时,此技术很有成效。

标准化图像是通过从它们的单个像素值中减去平均像素值,然后将它们除以像素值的标准偏差而得到的。需要采取以下步骤来标准化图像像素:

  • 计算像素值的均值和标准差。
  • 使用统计来标准化每个图像。在 Keras 中,它被称为样本标准化。
  • 创建一批具有零平均单位标准偏差的 64 张图像,以使样本接近标准高斯。
  • 在整个数据集上运行测试以确认均值接近于零,标准差接近于 1。
  • 在拟合和评估神经网络时应用像素缩放。

例子:

蟒蛇3

# import required modules
from keras.datasets import mnist
from keras.utils import to_categorical
from keras.models import Sequential
from keras.layers import Conv2D
from keras.layers import MaxPooling2D
from keras.layers import Dense
from keras.layers import Flatten
from keras.preprocessing.image import ImageDataGenerator
  
# loading the dataset
(trainX, trainY), (testX, testY) = mnist.load_data()
  
# reshaping the dataset to have a single channel
width, height, channels = trainX.shape[1], trainX.shape[2], 1
trainX = trainX.reshape((trainX.shape[0], width, height, channels))
testX = testX.reshape((testX.shape[0], width, height, channels))
trainY = to_categorical(trainY)
testY = to_categorical(testY)
  
# creating the image data generator to standardize images
datagen = ImageDataGenerator(featurewise_center=True,
                             featurewise_std_normalization=True)
  
# calculating the mean on the training dataset
datagen.fit(trainX)
  
# preparing iterators to scale images
train_iterator = datagen.flow(trainX, trainY, batch_size=64)
test_iterator = datagen.flow(testX, testY, batch_size=64)
print('Batches train=%d, test=%d' % (len(train_iterator),
                                     len(test_iterator)))
  
# defining the model
model = Sequential()
model.add(Conv2D(32, (3, 3), activation='relu',
                 input_shape=(width, height, channels)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D((2, 2)))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(10, activation='softmax'))
  
# compiling the  model
model.compile(optimizer='adam', loss='categorical_crossentropy',
              metrics=['accuracy'])
  
# fitting the model with the generator
model.fit_generator(train_iterator, steps_per_epoch=len(train_iterator),
                    epochs=5)
  
# evaluating the model
_, acc = model.evaluate_generator(test_iterator, steps=len(test_iterator),
                                  verbose=0)
print('Test Accuracy: %.3f' % (acc * 100))

运行上述脚本后,它将从 amazon-AWS 网站下载 MNIST 数据集。下载数据集需要一些时间。

检索由 64 张标准化图像组成的单个批次,平均值和标准偏差分别接近 0.0 和 1.0 作为预期的标准高斯。



ImageDataGenerator 类对图像进行标准化并计算训练集上的统计函数,从而拟合和评估模型。

神经网络模型的准确率为99.34%

这些是讨论使用 Keras 开源深度学习库对图像像素值进行归一化、居中和标准化的技术。