LoginSignup
3
2

More than 5 years have passed since last update.

[Keras]ImageDataGeneratorのchannel_shift_rangeの仕様について

Posted at

概要

ImageDataGeneratorchannel_shift_rangeについて調べたメモです。

バージョン

>>> keras.__version__
'2.1.6-tf'

出力される画像について

channel_shift.PNG

こんな感じの画像が出力されます。
最初の三つが元々の画像でその後の画像が生成された画像です。
画像全体が白か黒方向に色が変わっています。
(あれ、googleで調べたときに出てきたのと違う気がする:thinking:)

実装

image_data_generator.py#L772

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#L836

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?

バグとかではなく意図してこうなっているようです。

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2