• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15# pylint: disable=invalid-name
16# pylint: disable=g-import-not-at-top
17# pylint: disable=g-classes-have-attributes
18"""Set of tools for real-time data augmentation on image data."""
19
20from keras_preprocessing import image
21import numpy as np
22try:
23  from scipy import linalg  # pylint: disable=unused-import
24  from scipy import ndimage  # pylint: disable=unused-import
25except ImportError:
26  pass
27
28from tensorflow.python.framework import ops
29from tensorflow.python.keras import backend
30from tensorflow.python.keras.preprocessing.image_dataset import image_dataset_from_directory  # pylint: disable=unused-import
31from tensorflow.python.keras.utils import data_utils
32from tensorflow.python.keras.utils import tf_inspect
33from tensorflow.python.ops import array_ops
34from tensorflow.python.ops import image_ops
35from tensorflow.python.ops import math_ops
36from tensorflow.python.platform import tf_logging
37from tensorflow.python.util.tf_export import keras_export
38
39random_rotation = image.random_rotation
40random_shift = image.random_shift
41random_shear = image.random_shear
42random_zoom = image.random_zoom
43apply_channel_shift = image.apply_channel_shift
44random_channel_shift = image.random_channel_shift
45apply_brightness_shift = image.apply_brightness_shift
46random_brightness = image.random_brightness
47apply_affine_transform = image.apply_affine_transform
48
49
50@keras_export('keras.preprocessing.image.smart_resize', v1=[])
51def smart_resize(x, size, interpolation='bilinear'):
52  """Resize images to a target size without aspect ratio distortion.
53
54  TensorFlow image datasets typically yield images that have each a different
55  size. However, these images need to be batched before they can be
56  processed by Keras layers. To be batched, images need to share the same height
57  and width.
58
59  You could simply do:
60
61  ```python
62  size = (200, 200)
63  ds = ds.map(lambda img: tf.image.resize(img, size))
64  ```
65
66  However, if you do this, you distort the aspect ratio of your images, since
67  in general they do not all have the same aspect ratio as `size`. This is
68  fine in many cases, but not always (e.g. for GANs this can be a problem).
69
70  Note that passing the argument `preserve_aspect_ratio=True` to `resize`
71  will preserve the aspect ratio, but at the cost of no longer respecting the
72  provided target size. Because `tf.image.resize` doesn't crop images,
73  your output images will still have different sizes.
74
75  This calls for:
76
77  ```python
78  size = (200, 200)
79  ds = ds.map(lambda img: smart_resize(img, size))
80  ```
81
82  Your output images will actually be `(200, 200)`, and will not be distorted.
83  Instead, the parts of the image that do not fit within the target size
84  get cropped out.
85
86  The resizing process is:
87
88  1. Take the largest centered crop of the image that has the same aspect ratio
89  as the target size. For instance, if `size=(200, 200)` and the input image has
90  size `(340, 500)`, we take a crop of `(340, 340)` centered along the width.
91  2. Resize the cropped image to the target size. In the example above,
92  we resize the `(340, 340)` crop to `(200, 200)`.
93
94  Args:
95    x: Input image or batch of images (as a tensor or NumPy array).
96      Must be in format `(height, width, channels)` or
97      `(batch_size, height, width, channels)`.
98    size: Tuple of `(height, width)` integer. Target size.
99    interpolation: String, interpolation to use for resizing.
100      Defaults to `'bilinear'`. Supports `bilinear`, `nearest`, `bicubic`,
101      `area`, `lanczos3`, `lanczos5`, `gaussian`, `mitchellcubic`.
102
103  Returns:
104    Array with shape `(size[0], size[1], channels)`. If the input image was a
105    NumPy array, the output is a NumPy array, and if it was a TF tensor,
106    the output is a TF tensor.
107  """
108  if len(size) != 2:
109    raise ValueError('Expected `size` to be a tuple of 2 integers, '
110                     'but got: %s' % (size,))
111  img = ops.convert_to_tensor_v2_with_dispatch(x)
112  if img.shape.rank is not None:
113    if img.shape.rank < 3 or img.shape.rank > 4:
114      raise ValueError(
115          'Expected an image array with shape `(height, width, channels)`, '
116          'or `(batch_size, height, width, channels)` but '
117          'got input with incorrect rank, of shape %s' % (img.shape,))
118  shape = array_ops.shape(img)
119  if img.shape.rank == 4:
120    height, width = shape[1], shape[2]
121    static_num_channels = img.shape[-1]
122  else:
123    height, width = shape[0], shape[1]
124  target_height, target_width = size
125
126  crop_height = math_ops.cast(
127      math_ops.cast(width * target_height, 'float32') / target_width, 'int32')
128  crop_width = math_ops.cast(
129      math_ops.cast(height * target_width, 'float32') / target_height, 'int32')
130
131  # Set back to input height / width if crop_height / crop_width is not smaller.
132  crop_height = math_ops.minimum(height, crop_height)
133  crop_width = math_ops.minimum(width, crop_width)
134
135  crop_box_hstart = math_ops.cast(
136      math_ops.cast(height - crop_height, 'float32') / 2, 'int32')
137  crop_box_wstart = math_ops.cast(
138      math_ops.cast(width - crop_width, 'float32') / 2, 'int32')
139
140  if img.shape.rank == 4:
141    crop_box_start = array_ops.stack([0, crop_box_hstart, crop_box_wstart, 0])
142    crop_box_size = array_ops.stack([-1, crop_height, crop_width, -1])
143  else:
144    crop_box_start = array_ops.stack([crop_box_hstart, crop_box_wstart, 0])
145    crop_box_size = array_ops.stack([crop_height, crop_width, -1])
146
147  img = array_ops.slice(img, crop_box_start, crop_box_size)
148  img = image_ops.resize_images_v2(
149      images=img,
150      size=size,
151      method=interpolation)
152  if img.shape.rank == 4:
153    # Apparent bug in resize_images_v2 may cause shape to be lost
154    img.set_shape((None, None, None, static_num_channels))
155  if isinstance(x, np.ndarray):
156    return img.numpy()
157  return img
158
159
160@keras_export('keras.utils.array_to_img',
161              'keras.preprocessing.image.array_to_img')
162def array_to_img(x, data_format=None, scale=True, dtype=None):
163  """Converts a 3D Numpy array to a PIL Image instance.
164
165  Usage:
166
167  ```python
168  from PIL import Image
169  img = np.random.random(size=(100, 100, 3))
170  pil_img = tf.keras.preprocessing.image.array_to_img(img)
171  ```
172
173
174  Args:
175      x: Input data, in any form that can be converted to a Numpy array.
176      data_format: Image data format, can be either "channels_first" or
177        "channels_last". Defaults to `None`, in which case the global setting
178        `tf.keras.backend.image_data_format()` is used (unless you changed it,
179        it defaults to "channels_last").
180      scale: Whether to rescale the image such that minimum and maximum values
181        are 0 and 255 respectively. Defaults to `True`.
182      dtype: Dtype to use. Default to `None`, in which case the global setting
183      `tf.keras.backend.floatx()` is used (unless you changed it, it defaults
184      to "float32")
185
186  Returns:
187      A PIL Image instance.
188
189  Raises:
190      ImportError: if PIL is not available.
191      ValueError: if invalid `x` or `data_format` is passed.
192  """
193
194  if data_format is None:
195    data_format = backend.image_data_format()
196  kwargs = {}
197  if 'dtype' in tf_inspect.getfullargspec(image.array_to_img)[0]:
198    if dtype is None:
199      dtype = backend.floatx()
200    kwargs['dtype'] = dtype
201  return image.array_to_img(x, data_format=data_format, scale=scale, **kwargs)
202
203
204@keras_export('keras.utils.img_to_array',
205              'keras.preprocessing.image.img_to_array')
206def img_to_array(img, data_format=None, dtype=None):
207  """Converts a PIL Image instance to a Numpy array.
208
209  Usage:
210
211  ```python
212  from PIL import Image
213  img_data = np.random.random(size=(100, 100, 3))
214  img = tf.keras.preprocessing.image.array_to_img(img_data)
215  array = tf.keras.preprocessing.image.img_to_array(img)
216  ```
217
218
219  Args:
220      img: Input PIL Image instance.
221      data_format: Image data format, can be either "channels_first" or
222        "channels_last". Defaults to `None`, in which case the global setting
223        `tf.keras.backend.image_data_format()` is used (unless you changed it,
224        it defaults to "channels_last").
225      dtype: Dtype to use. Default to `None`, in which case the global setting
226      `tf.keras.backend.floatx()` is used (unless you changed it, it defaults
227      to "float32")
228
229  Returns:
230      A 3D Numpy array.
231
232  Raises:
233      ValueError: if invalid `img` or `data_format` is passed.
234  """
235
236  if data_format is None:
237    data_format = backend.image_data_format()
238  kwargs = {}
239  if 'dtype' in tf_inspect.getfullargspec(image.img_to_array)[0]:
240    if dtype is None:
241      dtype = backend.floatx()
242    kwargs['dtype'] = dtype
243  return image.img_to_array(img, data_format=data_format, **kwargs)
244
245
246@keras_export('keras.utils.save_img',
247              'keras.preprocessing.image.save_img')
248def save_img(path,
249             x,
250             data_format=None,
251             file_format=None,
252             scale=True,
253             **kwargs):
254  """Saves an image stored as a Numpy array to a path or file object.
255
256  Args:
257      path: Path or file object.
258      x: Numpy array.
259      data_format: Image data format,
260          either "channels_first" or "channels_last".
261      file_format: Optional file format override. If omitted, the
262          format to use is determined from the filename extension.
263          If a file object was used instead of a filename, this
264          parameter should always be used.
265      scale: Whether to rescale image values to be within `[0, 255]`.
266      **kwargs: Additional keyword arguments passed to `PIL.Image.save()`.
267  """
268  if data_format is None:
269    data_format = backend.image_data_format()
270  image.save_img(path,
271                 x,
272                 data_format=data_format,
273                 file_format=file_format,
274                 scale=scale, **kwargs)
275
276
277@keras_export('keras.utils.load_img',
278              'keras.preprocessing.image.load_img')
279def load_img(path, grayscale=False, color_mode='rgb', target_size=None,
280             interpolation='nearest'):
281  """Loads an image into PIL format.
282
283  Usage:
284
285  ```
286  image = tf.keras.preprocessing.image.load_img(image_path)
287  input_arr = tf.keras.preprocessing.image.img_to_array(image)
288  input_arr = np.array([input_arr])  # Convert single image to a batch.
289  predictions = model.predict(input_arr)
290  ```
291
292  Args:
293      path: Path to image file.
294      grayscale: DEPRECATED use `color_mode="grayscale"`.
295      color_mode: One of "grayscale", "rgb", "rgba". Default: "rgb".
296          The desired image format.
297      target_size: Either `None` (default to original size)
298          or tuple of ints `(img_height, img_width)`.
299      interpolation: Interpolation method used to resample the image if the
300          target size is different from that of the loaded image.
301          Supported methods are "nearest", "bilinear", and "bicubic".
302          If PIL version 1.1.3 or newer is installed, "lanczos" is also
303          supported. If PIL version 3.4.0 or newer is installed, "box" and
304          "hamming" are also supported. By default, "nearest" is used.
305
306  Returns:
307      A PIL Image instance.
308
309  Raises:
310      ImportError: if PIL is not available.
311      ValueError: if interpolation method is not supported.
312  """
313  return image.load_img(path, grayscale=grayscale, color_mode=color_mode,
314                        target_size=target_size, interpolation=interpolation)
315
316
317@keras_export('keras.preprocessing.image.Iterator')
318class Iterator(image.Iterator, data_utils.Sequence):
319  pass
320
321
322@keras_export('keras.preprocessing.image.DirectoryIterator')
323class DirectoryIterator(image.DirectoryIterator, Iterator):  # pylint: disable=inconsistent-mro
324  """Iterator capable of reading images from a directory on disk.
325
326  Args:
327      directory: Path to the directory to read images from.
328          Each subdirectory in this directory will be
329          considered to contain images from one class,
330          or alternatively you could specify class subdirectories
331          via the `classes` argument.
332      image_data_generator: Instance of `ImageDataGenerator`
333          to use for random transformations and normalization.
334      target_size: tuple of integers, dimensions to resize input images to.
335      color_mode: One of `"rgb"`, `"rgba"`, `"grayscale"`.
336          Color mode to read images.
337      classes: Optional list of strings, names of subdirectories
338          containing images from each class (e.g. `["dogs", "cats"]`).
339          It will be computed automatically if not set.
340      class_mode: Mode for yielding the targets:
341          - `"binary"`: binary targets (if there are only two classes),
342          - `"categorical"`: categorical targets,
343          - `"sparse"`: integer targets,
344          - `"input"`: targets are images identical to input images (mainly
345              used to work with autoencoders),
346          - `None`: no targets get yielded (only input images are yielded).
347      batch_size: Integer, size of a batch.
348      shuffle: Boolean, whether to shuffle the data between epochs.
349      seed: Random seed for data shuffling.
350      data_format: String, one of `channels_first`, `channels_last`.
351      save_to_dir: Optional directory where to save the pictures
352          being yielded, in a viewable format. This is useful
353          for visualizing the random transformations being
354          applied, for debugging purposes.
355      save_prefix: String prefix to use for saving sample
356          images (if `save_to_dir` is set).
357      save_format: Format to use for saving sample images
358          (if `save_to_dir` is set).
359      subset: Subset of data (`"training"` or `"validation"`) if
360          validation_split is set in ImageDataGenerator.
361      interpolation: Interpolation method used to resample the image if the
362          target size is different from that of the loaded image.
363          Supported methods are "nearest", "bilinear", and "bicubic".
364          If PIL version 1.1.3 or newer is installed, "lanczos" is also
365          supported. If PIL version 3.4.0 or newer is installed, "box" and
366          "hamming" are also supported. By default, "nearest" is used.
367      dtype: Dtype to use for generated arrays.
368  """
369
370  def __init__(self, directory, image_data_generator,
371               target_size=(256, 256),
372               color_mode='rgb',
373               classes=None,
374               class_mode='categorical',
375               batch_size=32,
376               shuffle=True,
377               seed=None,
378               data_format=None,
379               save_to_dir=None,
380               save_prefix='',
381               save_format='png',
382               follow_links=False,
383               subset=None,
384               interpolation='nearest',
385               dtype=None):
386    if data_format is None:
387      data_format = backend.image_data_format()
388    kwargs = {}
389    if 'dtype' in tf_inspect.getfullargspec(
390        image.ImageDataGenerator.__init__)[0]:
391      if dtype is None:
392        dtype = backend.floatx()
393      kwargs['dtype'] = dtype
394    super(DirectoryIterator, self).__init__(
395        directory, image_data_generator,
396        target_size=target_size,
397        color_mode=color_mode,
398        classes=classes,
399        class_mode=class_mode,
400        batch_size=batch_size,
401        shuffle=shuffle,
402        seed=seed,
403        data_format=data_format,
404        save_to_dir=save_to_dir,
405        save_prefix=save_prefix,
406        save_format=save_format,
407        follow_links=follow_links,
408        subset=subset,
409        interpolation=interpolation,
410        **kwargs)
411
412
413@keras_export('keras.preprocessing.image.NumpyArrayIterator')
414class NumpyArrayIterator(image.NumpyArrayIterator, Iterator):
415  """Iterator yielding data from a Numpy array.
416
417  Args:
418      x: Numpy array of input data or tuple.
419          If tuple, the second elements is either
420          another numpy array or a list of numpy arrays,
421          each of which gets passed
422          through as an output without any modifications.
423      y: Numpy array of targets data.
424      image_data_generator: Instance of `ImageDataGenerator`
425          to use for random transformations and normalization.
426      batch_size: Integer, size of a batch.
427      shuffle: Boolean, whether to shuffle the data between epochs.
428      sample_weight: Numpy array of sample weights.
429      seed: Random seed for data shuffling.
430      data_format: String, one of `channels_first`, `channels_last`.
431      save_to_dir: Optional directory where to save the pictures
432          being yielded, in a viewable format. This is useful
433          for visualizing the random transformations being
434          applied, for debugging purposes.
435      save_prefix: String prefix to use for saving sample
436          images (if `save_to_dir` is set).
437      save_format: Format to use for saving sample images
438          (if `save_to_dir` is set).
439      subset: Subset of data (`"training"` or `"validation"`) if
440          validation_split is set in ImageDataGenerator.
441      dtype: Dtype to use for the generated arrays.
442  """
443
444  def __init__(self, x, y, image_data_generator,
445               batch_size=32,
446               shuffle=False,
447               sample_weight=None,
448               seed=None,
449               data_format=None,
450               save_to_dir=None,
451               save_prefix='',
452               save_format='png',
453               subset=None,
454               dtype=None):
455    if data_format is None:
456      data_format = backend.image_data_format()
457    kwargs = {}
458    if 'dtype' in tf_inspect.getfullargspec(
459        image.NumpyArrayIterator.__init__)[0]:
460      if dtype is None:
461        dtype = backend.floatx()
462      kwargs['dtype'] = dtype
463    super(NumpyArrayIterator, self).__init__(
464        x, y, image_data_generator,
465        batch_size=batch_size,
466        shuffle=shuffle,
467        sample_weight=sample_weight,
468        seed=seed,
469        data_format=data_format,
470        save_to_dir=save_to_dir,
471        save_prefix=save_prefix,
472        save_format=save_format,
473        subset=subset,
474        **kwargs)
475
476
477class DataFrameIterator(image.DataFrameIterator, Iterator):  # pylint: disable=inconsistent-mro
478  """Iterator capable of reading images from a directory on disk as a dataframe.
479
480  Args:
481      dataframe: Pandas dataframe containing the filepaths relative to
482        `directory` (or absolute paths if `directory` is None) of the images in
483        a string column. It should include other column/s depending on the
484        `class_mode`:
485          - if `class_mode` is `"categorical"` (default value) it must include
486              the `y_col` column with the class/es of each image. Values in
487              column can be string/list/tuple if a single class or list/tuple if
488              multiple classes.
489          - if `class_mode` is `"binary"` or `"sparse"` it must include the
490              given `y_col` column with class values as strings.
491          - if `class_mode` is `"raw"` or `"multi_output"` it should contain the
492              columns specified in `y_col`.
493          - if `class_mode` is `"input"` or `None` no extra column is needed.
494      directory: string, path to the directory to read images from. If `None`,
495        data in `x_col` column should be absolute paths.
496      image_data_generator: Instance of `ImageDataGenerator` to use for random
497        transformations and normalization. If None, no transformations and
498        normalizations are made.
499      x_col: string, column in `dataframe` that contains the filenames (or
500        absolute paths if `directory` is `None`).
501      y_col: string or list, column/s in `dataframe` that has the target data.
502      weight_col: string, column in `dataframe` that contains the sample
503          weights. Default: `None`.
504      target_size: tuple of integers, dimensions to resize input images to.
505      color_mode: One of `"rgb"`, `"rgba"`, `"grayscale"`. Color mode to read
506        images.
507      classes: Optional list of strings, classes to use (e.g. `["dogs",
508        "cats"]`). If None, all classes in `y_col` will be used.
509      class_mode: one of "binary", "categorical", "input", "multi_output",
510        "raw", "sparse" or None. Default: "categorical".
511        Mode for yielding the targets:
512          - `"binary"`: 1D numpy array of binary labels,
513          - `"categorical"`: 2D numpy array of one-hot encoded labels. Supports
514            multi-label output.
515          - `"input"`: images identical to input images (mainly used to work
516            with autoencoders),
517          - `"multi_output"`: list with the values of the different columns,
518          - `"raw"`: numpy array of values in `y_col` column(s),
519          - `"sparse"`: 1D numpy array of integer labels,
520          - `None`, no targets are returned (the generator will only yield
521            batches of image data, which is useful to use in `model.predict()`).
522      batch_size: Integer, size of a batch.
523      shuffle: Boolean, whether to shuffle the data between epochs.
524      seed: Random seed for data shuffling.
525      data_format: String, one of `channels_first`, `channels_last`.
526      save_to_dir: Optional directory where to save the pictures being yielded,
527        in a viewable format. This is useful for visualizing the random
528        transformations being applied, for debugging purposes.
529      save_prefix: String prefix to use for saving sample images (if
530        `save_to_dir` is set).
531      save_format: Format to use for saving sample images (if `save_to_dir` is
532        set).
533      subset: Subset of data (`"training"` or `"validation"`) if
534        validation_split is set in ImageDataGenerator.
535      interpolation: Interpolation method used to resample the image if the
536        target size is different from that of the loaded image. Supported
537        methods are "nearest", "bilinear", and "bicubic". If PIL version 1.1.3
538        or newer is installed, "lanczos" is also supported. If PIL version 3.4.0
539        or newer is installed, "box" and "hamming" are also supported. By
540        default, "nearest" is used.
541      dtype: Dtype to use for the generated arrays.
542      validate_filenames: Boolean, whether to validate image filenames in
543        `x_col`. If `True`, invalid images will be ignored. Disabling this
544        option
545      can lead to speed-up in the instantiation of this class. Default: `True`.
546  """
547
548  def __init__(
549      self,
550      dataframe,
551      directory=None,
552      image_data_generator=None,
553      x_col='filename',
554      y_col='class',
555      weight_col=None,
556      target_size=(256, 256),
557      color_mode='rgb',
558      classes=None,
559      class_mode='categorical',
560      batch_size=32,
561      shuffle=True,
562      seed=None,
563      data_format='channels_last',
564      save_to_dir=None,
565      save_prefix='',
566      save_format='png',
567      subset=None,
568      interpolation='nearest',
569      dtype='float32',
570      validate_filenames=True):
571    super(DataFrameIterator, self).__init__(
572        dataframe=dataframe,
573        directory=directory,
574        image_data_generator=image_data_generator,
575        x_col=x_col,
576        y_col=y_col,
577        weight_col=weight_col,
578        target_size=target_size,
579        color_mode=color_mode,
580        classes=classes,
581        class_mode=class_mode,
582        batch_size=batch_size,
583        shuffle=shuffle,
584        seed=seed,
585        data_format=data_format,
586        save_to_dir=save_to_dir,
587        save_prefix=save_prefix,
588        save_format=save_format,
589        subset=subset,
590        interpolation=interpolation,
591        dtype=dtype,
592        validate_filenames=validate_filenames
593    )
594
595
596@keras_export('keras.preprocessing.image.ImageDataGenerator')
597class ImageDataGenerator(image.ImageDataGenerator):
598  """Generate batches of tensor image data with real-time data augmentation.
599
600   The data will be looped over (in batches).
601
602  Args:
603      featurewise_center: Boolean.
604          Set input mean to 0 over the dataset, feature-wise.
605      samplewise_center: Boolean. Set each sample mean to 0.
606      featurewise_std_normalization: Boolean.
607          Divide inputs by std of the dataset, feature-wise.
608      samplewise_std_normalization: Boolean. Divide each input by its std.
609      zca_epsilon: epsilon for ZCA whitening. Default is 1e-6.
610      zca_whitening: Boolean. Apply ZCA whitening.
611      rotation_range: Int. Degree range for random rotations.
612      width_shift_range: Float, 1-D array-like or int
613          - float: fraction of total width, if < 1, or pixels if >= 1.
614          - 1-D array-like: random elements from the array.
615          - int: integer number of pixels from interval
616              `(-width_shift_range, +width_shift_range)`
617          - With `width_shift_range=2` possible values
618              are integers `[-1, 0, +1]`,
619              same as with `width_shift_range=[-1, 0, +1]`,
620              while with `width_shift_range=1.0` possible values are floats
621              in the interval [-1.0, +1.0).
622      height_shift_range: Float, 1-D array-like or int
623          - float: fraction of total height, if < 1, or pixels if >= 1.
624          - 1-D array-like: random elements from the array.
625          - int: integer number of pixels from interval
626              `(-height_shift_range, +height_shift_range)`
627          - With `height_shift_range=2` possible values
628              are integers `[-1, 0, +1]`,
629              same as with `height_shift_range=[-1, 0, +1]`,
630              while with `height_shift_range=1.0` possible values are floats
631              in the interval [-1.0, +1.0).
632      brightness_range: Tuple or list of two floats. Range for picking
633          a brightness shift value from.
634      shear_range: Float. Shear Intensity
635          (Shear angle in counter-clockwise direction in degrees)
636      zoom_range: Float or [lower, upper]. Range for random zoom.
637          If a float, `[lower, upper] = [1-zoom_range, 1+zoom_range]`.
638      channel_shift_range: Float. Range for random channel shifts.
639      fill_mode: One of {"constant", "nearest", "reflect" or "wrap"}.
640          Default is 'nearest'.
641          Points outside the boundaries of the input are filled
642          according to the given mode:
643          - 'constant': kkkkkkkk|abcd|kkkkkkkk (cval=k)
644          - 'nearest':  aaaaaaaa|abcd|dddddddd
645          - 'reflect':  abcddcba|abcd|dcbaabcd
646          - 'wrap':  abcdabcd|abcd|abcdabcd
647      cval: Float or Int.
648          Value used for points outside the boundaries
649          when `fill_mode = "constant"`.
650      horizontal_flip: Boolean. Randomly flip inputs horizontally.
651      vertical_flip: Boolean. Randomly flip inputs vertically.
652      rescale: rescaling factor. Defaults to None.
653          If None or 0, no rescaling is applied,
654          otherwise we multiply the data by the value provided
655          (after applying all other transformations).
656      preprocessing_function: function that will be applied on each input.
657          The function will run after the image is resized and augmented.
658          The function should take one argument:
659          one image (Numpy tensor with rank 3),
660          and should output a Numpy tensor with the same shape.
661      data_format: Image data format,
662          either "channels_first" or "channels_last".
663          "channels_last" mode means that the images should have shape
664          `(samples, height, width, channels)`,
665          "channels_first" mode means that the images should have shape
666          `(samples, channels, height, width)`.
667          It defaults to the `image_data_format` value found in your
668          Keras config file at `~/.keras/keras.json`.
669          If you never set it, then it will be "channels_last".
670      validation_split: Float. Fraction of images reserved for validation
671          (strictly between 0 and 1).
672      dtype: Dtype to use for the generated arrays.
673
674  Raises:
675    ValueError: If the value of the argument, `data_format` is other than
676          `"channels_last"` or `"channels_first"`.
677    ValueError: If the value of the argument, `validation_split` > 1
678          or `validation_split` < 0.
679
680  Examples:
681
682  Example of using `.flow(x, y)`:
683
684  ```python
685  (x_train, y_train), (x_test, y_test) = cifar10.load_data()
686  y_train = utils.to_categorical(y_train, num_classes)
687  y_test = utils.to_categorical(y_test, num_classes)
688  datagen = ImageDataGenerator(
689      featurewise_center=True,
690      featurewise_std_normalization=True,
691      rotation_range=20,
692      width_shift_range=0.2,
693      height_shift_range=0.2,
694      horizontal_flip=True,
695      validation_split=0.2)
696  # compute quantities required for featurewise normalization
697  # (std, mean, and principal components if ZCA whitening is applied)
698  datagen.fit(x_train)
699  # fits the model on batches with real-time data augmentation:
700  model.fit(datagen.flow(x_train, y_train, batch_size=32,
701           subset='training'),
702           validation_data=datagen.flow(x_train, y_train,
703           batch_size=8, subset='validation'),
704           steps_per_epoch=len(x_train) / 32, epochs=epochs)
705  # here's a more "manual" example
706  for e in range(epochs):
707      print('Epoch', e)
708      batches = 0
709      for x_batch, y_batch in datagen.flow(x_train, y_train, batch_size=32):
710          model.fit(x_batch, y_batch)
711          batches += 1
712          if batches >= len(x_train) / 32:
713              # we need to break the loop by hand because
714              # the generator loops indefinitely
715              break
716  ```
717
718  Example of using `.flow_from_directory(directory)`:
719
720  ```python
721  train_datagen = ImageDataGenerator(
722          rescale=1./255,
723          shear_range=0.2,
724          zoom_range=0.2,
725          horizontal_flip=True)
726  test_datagen = ImageDataGenerator(rescale=1./255)
727  train_generator = train_datagen.flow_from_directory(
728          'data/train',
729          target_size=(150, 150),
730          batch_size=32,
731          class_mode='binary')
732  validation_generator = test_datagen.flow_from_directory(
733          'data/validation',
734          target_size=(150, 150),
735          batch_size=32,
736          class_mode='binary')
737  model.fit(
738          train_generator,
739          steps_per_epoch=2000,
740          epochs=50,
741          validation_data=validation_generator,
742          validation_steps=800)
743  ```
744
745  Example of transforming images and masks together.
746
747  ```python
748  # we create two instances with the same arguments
749  data_gen_args = dict(featurewise_center=True,
750                       featurewise_std_normalization=True,
751                       rotation_range=90,
752                       width_shift_range=0.1,
753                       height_shift_range=0.1,
754                       zoom_range=0.2)
755  image_datagen = ImageDataGenerator(**data_gen_args)
756  mask_datagen = ImageDataGenerator(**data_gen_args)
757  # Provide the same seed and keyword arguments to the fit and flow methods
758  seed = 1
759  image_datagen.fit(images, augment=True, seed=seed)
760  mask_datagen.fit(masks, augment=True, seed=seed)
761  image_generator = image_datagen.flow_from_directory(
762      'data/images',
763      class_mode=None,
764      seed=seed)
765  mask_generator = mask_datagen.flow_from_directory(
766      'data/masks',
767      class_mode=None,
768      seed=seed)
769  # combine generators into one which yields image and masks
770  train_generator = zip(image_generator, mask_generator)
771  model.fit(
772      train_generator,
773      steps_per_epoch=2000,
774      epochs=50)
775  ```
776  """
777
778  def __init__(self,
779               featurewise_center=False,
780               samplewise_center=False,
781               featurewise_std_normalization=False,
782               samplewise_std_normalization=False,
783               zca_whitening=False,
784               zca_epsilon=1e-6,
785               rotation_range=0,
786               width_shift_range=0.,
787               height_shift_range=0.,
788               brightness_range=None,
789               shear_range=0.,
790               zoom_range=0.,
791               channel_shift_range=0.,
792               fill_mode='nearest',
793               cval=0.,
794               horizontal_flip=False,
795               vertical_flip=False,
796               rescale=None,
797               preprocessing_function=None,
798               data_format=None,
799               validation_split=0.0,
800               dtype=None):
801    if data_format is None:
802      data_format = backend.image_data_format()
803    kwargs = {}
804    if 'dtype' in tf_inspect.getfullargspec(
805        image.ImageDataGenerator.__init__)[0]:
806      if dtype is None:
807        dtype = backend.floatx()
808      kwargs['dtype'] = dtype
809    super(ImageDataGenerator, self).__init__(
810        featurewise_center=featurewise_center,
811        samplewise_center=samplewise_center,
812        featurewise_std_normalization=featurewise_std_normalization,
813        samplewise_std_normalization=samplewise_std_normalization,
814        zca_whitening=zca_whitening,
815        zca_epsilon=zca_epsilon,
816        rotation_range=rotation_range,
817        width_shift_range=width_shift_range,
818        height_shift_range=height_shift_range,
819        brightness_range=brightness_range,
820        shear_range=shear_range,
821        zoom_range=zoom_range,
822        channel_shift_range=channel_shift_range,
823        fill_mode=fill_mode,
824        cval=cval,
825        horizontal_flip=horizontal_flip,
826        vertical_flip=vertical_flip,
827        rescale=rescale,
828        preprocessing_function=preprocessing_function,
829        data_format=data_format,
830        validation_split=validation_split,
831        **kwargs)
832
833  def flow(self,
834           x,
835           y=None,
836           batch_size=32,
837           shuffle=True,
838           sample_weight=None,
839           seed=None,
840           save_to_dir=None,
841           save_prefix='',
842           save_format='png',
843           subset=None):
844    """Takes data & label arrays, generates batches of augmented data.
845
846    Args:
847        x: Input data. Numpy array of rank 4 or a tuple. If tuple, the first
848          element should contain the images and the second element another numpy
849          array or a list of numpy arrays that gets passed to the output without
850          any modifications. Can be used to feed the model miscellaneous data
851          along with the images. In case of grayscale data, the channels axis of
852          the image array should have value 1, in case of RGB data, it should
853          have value 3, and in case of RGBA data, it should have value 4.
854        y: Labels.
855        batch_size: Int (default: 32).
856        shuffle: Boolean (default: True).
857        sample_weight: Sample weights.
858        seed: Int (default: None).
859        save_to_dir: None or str (default: None). This allows you to optionally
860          specify a directory to which to save the augmented pictures being
861          generated (useful for visualizing what you are doing).
862        save_prefix: Str (default: `''`). Prefix to use for filenames of saved
863          pictures (only relevant if `save_to_dir` is set).
864        save_format: one of "png", "jpeg", "bmp", "pdf", "ppm", "gif",
865            "tif", "jpg"
866            (only relevant if `save_to_dir` is set). Default: "png".
867        subset: Subset of data (`"training"` or `"validation"`) if
868          `validation_split` is set in `ImageDataGenerator`.
869
870    Returns:
871        An `Iterator` yielding tuples of `(x, y)`
872            where `x` is a numpy array of image data
873            (in the case of a single image input) or a list
874            of numpy arrays (in the case with
875            additional inputs) and `y` is a numpy array
876            of corresponding labels. If 'sample_weight' is not None,
877            the yielded tuples are of the form `(x, y, sample_weight)`.
878            If `y` is None, only the numpy array `x` is returned.
879    Raises:
880      ValueError: If the Value of the argument, `subset` is other than
881            "training" or "validation".
882
883    """
884    return NumpyArrayIterator(
885        x,
886        y,
887        self,
888        batch_size=batch_size,
889        shuffle=shuffle,
890        sample_weight=sample_weight,
891        seed=seed,
892        data_format=self.data_format,
893        save_to_dir=save_to_dir,
894        save_prefix=save_prefix,
895        save_format=save_format,
896        subset=subset)
897
898  def flow_from_directory(self,
899                          directory,
900                          target_size=(256, 256),
901                          color_mode='rgb',
902                          classes=None,
903                          class_mode='categorical',
904                          batch_size=32,
905                          shuffle=True,
906                          seed=None,
907                          save_to_dir=None,
908                          save_prefix='',
909                          save_format='png',
910                          follow_links=False,
911                          subset=None,
912                          interpolation='nearest'):
913    """Takes the path to a directory & generates batches of augmented data.
914
915    Args:
916        directory: string, path to the target directory. It should contain one
917          subdirectory per class. Any PNG, JPG, BMP, PPM or TIF images inside
918          each of the subdirectories directory tree will be included in the
919          generator. See [this script](
920            https://gist.github.com/fchollet/0830affa1f7f19fd47b06d4cf89ed44d)
921              for more details.
922        target_size: Tuple of integers `(height, width)`, defaults to `(256,
923          256)`. The dimensions to which all images found will be resized.
924        color_mode: One of "grayscale", "rgb", "rgba". Default: "rgb". Whether
925          the images will be converted to have 1, 3, or 4 channels.
926        classes: Optional list of class subdirectories
927            (e.g. `['dogs', 'cats']`). Default: None. If not provided, the list
928              of classes will be automatically inferred from the subdirectory
929              names/structure under `directory`, where each subdirectory will be
930              treated as a different class (and the order of the classes, which
931              will map to the label indices, will be alphanumeric). The
932              dictionary containing the mapping from class names to class
933              indices can be obtained via the attribute `class_indices`.
934        class_mode: One of "categorical", "binary", "sparse",
935            "input", or None. Default: "categorical".
936            Determines the type of label arrays that are returned:
937            - "categorical" will be 2D one-hot encoded labels,
938            - "binary" will be 1D binary labels,
939            - "sparse" will be 1D integer labels,
940            - "input"  will be images identical to input images (mainly used to
941              work with autoencoders).
942            - If None, no labels are returned (the generator will only yield
943              batches of image data, which is useful to use with
944              `model.predict()`).
945            Please note that in case of class_mode None, the data still needs to
946            reside in a subdirectory of `directory` for it to work correctly.
947        batch_size: Size of the batches of data (default: 32).
948        shuffle: Whether to shuffle the data (default: True) If set to False,
949          sorts the data in alphanumeric order.
950        seed: Optional random seed for shuffling and transformations.
951        save_to_dir: None or str (default: None). This allows you to optionally
952          specify a directory to which to save the augmented pictures being
953          generated (useful for visualizing what you are doing).
954        save_prefix: Str. Prefix to use for filenames of saved pictures (only
955          relevant if `save_to_dir` is set).
956        save_format: one of "png", "jpeg", "bmp", "pdf", "ppm", "gif",
957            "tif", "jpg"
958            (only relevant if `save_to_dir` is set). Default: "png".
959        follow_links: Whether to follow symlinks inside
960            class subdirectories (default: False).
961        subset: Subset of data (`"training"` or `"validation"`) if
962          `validation_split` is set in `ImageDataGenerator`.
963        interpolation: Interpolation method used to resample the image if the
964          target size is different from that of the loaded image. Supported
965          methods are `"nearest"`, `"bilinear"`, and `"bicubic"`. If PIL version
966          1.1.3 or newer is installed, `"lanczos"` is also supported. If PIL
967          version 3.4.0 or newer is installed, `"box"` and `"hamming"` are also
968          supported. By default, `"nearest"` is used.
969
970    Returns:
971        A `DirectoryIterator` yielding tuples of `(x, y)`
972            where `x` is a numpy array containing a batch
973            of images with shape `(batch_size, *target_size, channels)`
974            and `y` is a numpy array of corresponding labels.
975    """
976    return DirectoryIterator(
977        directory,
978        self,
979        target_size=target_size,
980        color_mode=color_mode,
981        classes=classes,
982        class_mode=class_mode,
983        data_format=self.data_format,
984        batch_size=batch_size,
985        shuffle=shuffle,
986        seed=seed,
987        save_to_dir=save_to_dir,
988        save_prefix=save_prefix,
989        save_format=save_format,
990        follow_links=follow_links,
991        subset=subset,
992        interpolation=interpolation)
993
994  def flow_from_dataframe(self,
995                          dataframe,
996                          directory=None,
997                          x_col='filename',
998                          y_col='class',
999                          weight_col=None,
1000                          target_size=(256, 256),
1001                          color_mode='rgb',
1002                          classes=None,
1003                          class_mode='categorical',
1004                          batch_size=32,
1005                          shuffle=True,
1006                          seed=None,
1007                          save_to_dir=None,
1008                          save_prefix='',
1009                          save_format='png',
1010                          subset=None,
1011                          interpolation='nearest',
1012                          validate_filenames=True,
1013                          **kwargs):
1014    """Takes the dataframe and the path to a directory + generates batches.
1015
1016     The generated batches contain augmented/normalized data.
1017
1018    **A simple tutorial can be found **[here](
1019                                http://bit.ly/keras_flow_from_dataframe).
1020
1021    Args:
1022        dataframe: Pandas dataframe containing the filepaths relative to
1023          `directory` (or absolute paths if `directory` is None) of the images
1024          in a string column. It should include other column/s
1025            depending on the `class_mode`:
1026            - if `class_mode` is `"categorical"` (default value) it must include
1027              the `y_col` column with the class/es of each image. Values in
1028              column can be string/list/tuple if a single class or list/tuple if
1029              multiple classes.
1030            - if `class_mode` is `"binary"` or `"sparse"` it must include the
1031              given `y_col` column with class values as strings.
1032            - if `class_mode` is `"raw"` or `"multi_output"` it should contain
1033              the columns specified in `y_col`.
1034            - if `class_mode` is `"input"` or `None` no extra column is needed.
1035        directory: string, path to the directory to read images from. If `None`,
1036          data in `x_col` column should be absolute paths.
1037        x_col: string, column in `dataframe` that contains the filenames (or
1038          absolute paths if `directory` is `None`).
1039        y_col: string or list, column/s in `dataframe` that has the target data.
1040        weight_col: string, column in `dataframe` that contains the sample
1041            weights. Default: `None`.
1042        target_size: tuple of integers `(height, width)`, default: `(256, 256)`.
1043          The dimensions to which all images found will be resized.
1044        color_mode: one of "grayscale", "rgb", "rgba". Default: "rgb". Whether
1045          the images will be converted to have 1 or 3 color channels.
1046        classes: optional list of classes (e.g. `['dogs', 'cats']`). Default is
1047          None. If not provided, the list of classes will be automatically
1048          inferred from the `y_col`, which will map to the label indices, will
1049          be alphanumeric). The dictionary containing the mapping from class
1050          names to class indices can be obtained via the attribute
1051          `class_indices`.
1052        class_mode: one of "binary", "categorical", "input", "multi_output",
1053            "raw", sparse" or None. Default: "categorical".
1054            Mode for yielding the targets:
1055            - `"binary"`: 1D numpy array of binary labels,
1056            - `"categorical"`: 2D numpy array of one-hot encoded labels.
1057              Supports multi-label output.
1058            - `"input"`: images identical to input images (mainly used to work
1059              with autoencoders),
1060            - `"multi_output"`: list with the values of the different columns,
1061            - `"raw"`: numpy array of values in `y_col` column(s),
1062            - `"sparse"`: 1D numpy array of integer labels,
1063            - `None`, no targets are returned (the generator will only yield
1064              batches of image data, which is useful to use in
1065              `model.predict()`).
1066        batch_size: size of the batches of data (default: 32).
1067        shuffle: whether to shuffle the data (default: True)
1068        seed: optional random seed for shuffling and transformations.
1069        save_to_dir: None or str (default: None). This allows you to optionally
1070          specify a directory to which to save the augmented pictures being
1071          generated (useful for visualizing what you are doing).
1072        save_prefix: str. Prefix to use for filenames of saved pictures (only
1073          relevant if `save_to_dir` is set).
1074        save_format: one of "png", "jpeg", "bmp", "pdf", "ppm", "gif",
1075            "tif", "jpg"
1076            (only relevant if `save_to_dir` is set). Default: "png".
1077        subset: Subset of data (`"training"` or `"validation"`) if
1078          `validation_split` is set in `ImageDataGenerator`.
1079        interpolation: Interpolation method used to resample the image if the
1080          target size is different from that of the loaded image. Supported
1081          methods are `"nearest"`, `"bilinear"`, and `"bicubic"`. If PIL version
1082          1.1.3 or newer is installed, `"lanczos"` is also supported. If PIL
1083          version 3.4.0 or newer is installed, `"box"` and `"hamming"` are also
1084          supported. By default, `"nearest"` is used.
1085        validate_filenames: Boolean, whether to validate image filenames in
1086          `x_col`. If `True`, invalid images will be ignored. Disabling this
1087          option can lead to speed-up in the execution of this function.
1088          Defaults to `True`.
1089        **kwargs: legacy arguments for raising deprecation warnings.
1090
1091    Returns:
1092        A `DataFrameIterator` yielding tuples of `(x, y)`
1093        where `x` is a numpy array containing a batch
1094        of images with shape `(batch_size, *target_size, channels)`
1095        and `y` is a numpy array of corresponding labels.
1096    """
1097    if 'has_ext' in kwargs:
1098      tf_logging.warning(
1099          'has_ext is deprecated, filenames in the dataframe have '
1100          'to match the exact filenames in disk.', DeprecationWarning)
1101    if 'sort' in kwargs:
1102      tf_logging.warning(
1103          'sort is deprecated, batches will be created in the'
1104          'same order than the filenames provided if shuffle'
1105          'is set to False.', DeprecationWarning)
1106    if class_mode == 'other':
1107      tf_logging.warning(
1108          '`class_mode` "other" is deprecated, please use '
1109          '`class_mode` "raw".', DeprecationWarning)
1110      class_mode = 'raw'
1111    if 'drop_duplicates' in kwargs:
1112      tf_logging.warning(
1113          'drop_duplicates is deprecated, you can drop duplicates '
1114          'by using the pandas.DataFrame.drop_duplicates method.',
1115          DeprecationWarning)
1116
1117    return DataFrameIterator(
1118        dataframe,
1119        directory,
1120        self,
1121        x_col=x_col,
1122        y_col=y_col,
1123        weight_col=weight_col,
1124        target_size=target_size,
1125        color_mode=color_mode,
1126        classes=classes,
1127        class_mode=class_mode,
1128        data_format=self.data_format,
1129        batch_size=batch_size,
1130        shuffle=shuffle,
1131        seed=seed,
1132        save_to_dir=save_to_dir,
1133        save_prefix=save_prefix,
1134        save_format=save_format,
1135        subset=subset,
1136        interpolation=interpolation,
1137        validate_filenames=validate_filenames)
1138
1139
1140keras_export('keras.preprocessing.image.random_rotation')(random_rotation)
1141keras_export('keras.preprocessing.image.random_shift')(random_shift)
1142keras_export('keras.preprocessing.image.random_shear')(random_shear)
1143keras_export('keras.preprocessing.image.random_zoom')(random_zoom)
1144keras_export(
1145    'keras.preprocessing.image.apply_channel_shift')(apply_channel_shift)
1146keras_export(
1147    'keras.preprocessing.image.random_channel_shift')(random_channel_shift)
1148keras_export(
1149    'keras.preprocessing.image.apply_brightness_shift')(apply_brightness_shift)
1150keras_export('keras.preprocessing.image.random_brightness')(random_brightness)
1151keras_export(
1152    'keras.preprocessing.image.apply_affine_transform')(apply_affine_transform)
1153