概要
ImageDataGenerator
のchannel_shift_range
について調べたメモです。
バージョン
>>> keras.__version__
'2.1.6-tf'
出力される画像について
こんな感じの画像が出力されます。
最初の三つが元々の画像でその後の画像が生成された画像です。
画像全体が白か黒方向に色が変わっています。
(あれ、googleで調べたときに出てきたのと違う気がする)
実装
image_data_generator.pyより引用
channel_shift_intensity = None
if self.channel_shift_range != 0:
channel_shift_intensity = np.random.uniform(-self.channel_shift_range,
self.channel_shift_range)
channel_shift_range
の値は-channel_shift_range
からchannel_shift_range
の間の乱数として使用されているようです。
この乱数channel_shift_intensity
は以下のような形で使用されています。
image_data_generator.pyより引用
if transform_parameters.get('channel_shift_intensity') is not None:
x = apply_channel_shift(x,
transform_parameters['channel_shift_intensity'],
img_channel_axis)
apply_channel_shift
の実装は以下のようになっています。
affine_transformations.py#L162
affine_transformations.pyより引用
def apply_channel_shift(x, intensity, channel_axis=0):
"""Performs a channel shift.
# Arguments
x: Input tensor. Must be 3D.
intensity: Transformation intensity.
channel_axis: Index of axis for channels in the input tensor.
# Returns
Numpy image tensor.
"""
x = np.rollaxis(x, channel_axis, 0)
min_x, max_x = np.min(x), np.max(x)
channel_images = [
np.clip(x_channel + intensity,
min_x,
max_x)
for x_channel in x]
x = np.stack(channel_images, axis=0)
x = np.rollaxis(x, 0, channel_axis + 1)
return x
単純に各チャンネルにintensity
を足してクリップしているだけのように見えます。
感想
あまり画像処理に詳しくないのでわかりませんが、これってチャンネルシフトなのでしょうか。
これに関して興味深いissueがありました。
Which one is expected by channel_shift?
バグとかではなく意図してこうなっているようです。