用keras进行图像数据扩增,发现扩增后的图像变白。排查了很久,找到原因:
首先用keras的数据扩增,最好用bmp的格式,jpg格式会乱码。
gen_data = datagen.flow_from_directory(path,batch_size=1, shuffle=False, save_to_dir=gen_path ,
save_prefix='a_',target_size=(64, 64),save_format='bmp')
C:\Users\Dell\Miniconda2\envs\py36\Lib\site-packages\keras_preprocessing
py36 的位置有变
问题:64*64的图像通过keras的flow_from_directory函数扩增图像,发现图像变白。
原因:在array变成image的过程中,有一个scale参数默认为True,取三个通道的RGB最大值将其拉伸到255。
解决方案:修改preprocessing.image的array_to_img函数代码。
pip show keras
地址:C:\ProgramData\Miniconda3\envs\py35\Lib\site-packages\keras\preprocessing
C:\Users\Dell\Miniconda2\envs\py36\Lib\site-packages\keras\preprocessing
出错代码如下:
数据扩增:
import matplotlib.pyplot as plt
from keras.preprocessing.image import ImageDataGenerator
path = 'F:/012345-pos/data_augment/train/'
gen_path = 'F:/012345-pos/data_augment/result/'
datagen = ImageDataGenerator(
rotation_range=180,
width_shift_range=0,
height_shift_range=0,
shear_range=0,
zoom_range=0,
horizontal_flip=False
)
gen_data = datagen.flow_from_directory(path, batch_size=1, shuffle=False, save_to_dir=gen_path ,save_prefix='gen',target_size=(64, 64),save_format='jpg')
for i in range(10):
gen_data.next()
```javascript
修改后如下:
```javascript
def array_to_img(x, data_format=None, scale=False):
"""Converts a 3D Numpy array to a PIL Image instance.
# Arguments
x: Input Numpy array.
data_format: Image data format.
scale: Whether to rescale image values
to be within [0, 255].
# Returns
A PIL Image instance.
# Raises
ImportError: if PIL is not available.
ValueError: if invalid `x` or `data_format` is passed.
"""
if pil_image is None:
raise ImportError('Could not import PIL.Image. '
'The use of `array_to_img` requires PIL.')
x = np.asarray(x, dtype=K.floatx())
if x.ndim != 3:
raise ValueError('Expected image array to have rank 3 (single image). '
'Got array with shape:', x.shape)
if data_format is None:
data_format = K.image_data_format()
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Invalid data_format:', data_format)
# Original Numpy array x has format (height, width, channel)
# or (channel, height, width)
# but target PIL image has format (width, height, channel)
if data_format == 'channels_first':
x = x.transpose(1, 2, 0)
#if scale is None:
if scale is None:
x = x + max(-np.min(x), 0)
x_max = np.max(x)
if x_max != 0:
x /= x_max
x *= 255
if x.shape[2] == 3:
# RGB
return pil_image.fromarray(x.astype('uint8'), 'RGB')
elif x.shape[2] == 1:
# grayscale
return pil_image.fromarray(x[:, :, 0].astype('uint8'), 'L')
else:
raise ValueError('Unsupported channel number: ', x.shape[2])
补充:ImageDataGenerator类
参数
featurewise_center: 布尔值。将输入数据的均值设置为 0,逐特征进行。
samplewise_center: 布尔值。将每个样本的均值设置为 0。
featurewise_std_normalization: Boolean. 布尔值。将输入除以数据标准差,逐特征进行。
samplewise_std_normalization: 布尔值。将每个输入除以其标准差。
zca_epsilon: ZCA 白化的 epsilon 值,默认为 1e-6。
zca_whitening: 布尔值。是否应用 ZCA 白化。
rotation_range: 整数。随机旋转的度数范围。
width_shift_range: 浮点数、一维数组或整数
float: 如果 <1,则是除以总宽度的值,或者如果 >=1,则为像素值。
1-D 数组: 数组中的随机元素。
int: 来自间隔 (-width_shift_range, +width_shift_range) 之间的整数个像素。
width_shift_range=2 时,可能值是整数 [-1, 0, +1],与 width_shift_range=[-1, 0, +1] 相同;而 width_shift_range=1.0 时,可能值是 [-1.0, +1.0) 之间的浮点数。
height_shift_range: 浮点数、一维数组或整数
float: 如果 <1,则是除以总宽度的值,或者如果 >=1,则为像素值。
1-D array-like: 数组中的随机元素。
int: 来自间隔 (-height_shift_range, +height_shift_range) 之间的整数个像素。
height_shift_range=2 时,可能值是整数 [-1, 0, +1],与 height_shift_range=[-1, 0, +1] 相同;而 height_shift_range=1.0 时,可能值是 [-1.0, +1.0) 之间的浮点数。
shear_range: 浮点数。剪切强度(以弧度逆时针方向剪切角度)。
zoom_range: 浮点数 或 [lower, upper]。随机缩放范围。如果是浮点数,[lower, upper] = [1-zoom_range, 1+zoom_range]。
channel_shift_range: 浮点数。随机通道转换的范围。
fill_mode: {“constant”, “nearest”, “reflect” or “wrap”} 之一。默认为 ‘nearest’。输入边界以外的点根据给定的模式填充:
‘constant’: kkkkkkkk|abcd|kkkkkkkk (cval=k)
‘nearest’: aaaaaaaa|abcd|dddddddd
‘reflect’: abcddcba|abcd|dcbaabcd
‘wrap’: abcdabcd|abcd|abcdabcd
cval: 浮点数或整数。用于边界之外的点的值,当 fill_mode = “constant” 时。
horizontal_flip: 布尔值。随机水平翻转。
vertical_flip: 布尔值。随机垂直翻转。
rescale: 重缩放因子。默认为 None。如果是 None 或 0,不进行缩放,否则将数据乘以所提供的值(在应用任何其他转换之前)。
preprocessing_function: 应用于每个输入的函数。这个函数会在任何其他改变之前运行。这个函数需要一个参数:一张图像(秩为 3 的 Numpy 张量),并且应该输出一个同尺寸的 Numpy 张量。
data_format: 图像数据格式,{“channels_first”, “channels_last”} 之一。“channels_last” 模式表示图像输入尺寸应该为 (samples, height, width, channels),“channels_first” 模式表示输入尺寸应该为 (samples, channels, height, width)。默认为 在 Keras 配置文件 ~/.keras/keras.json 中的 image_data_format 值。如果你从未设置它,那它就是 “channels_last”。
validation_split: 浮点数。Float. 保留用于验证的图像的比例(严格在0和1之间)。
dtype: 生成数组使用的数据类型。