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