
    2Vh	                        d Z ddlZddlZddlZddlZddlZddlZddlm	Z	 ddl
mZ ddlmZ ddlmZ ddlmZ ddlmZ  ed	       G d
 de             Zd Zd Z G d d      Z ed       G d dee             Z ed       G d de             Zd Z G d dee      Zd Z ed       G d d             Z ed      	 	 	 	 	 	 d0d       Z ed      	 	 	 	 	 	 d0d        Z ed!      	 	 	 	 	 	 d0d"       Z ed#      	 	 	 	 	 	 d0d$       Z  ed%      d1d&       Z! ed'      d1d(       Z" ed)      d2d*       Z# ed+      d2d,       Z$d- Z% ed.      	 	 	 	 	 	 	 	 	 	 	 	 d3d/       Z&y)4z1Deprecated image preprocessing APIs from Keras 1.    N)backend)keras_export)	PyDataset)image_utils)io_utils)scipyz*keras._legacy.preprocessing.image.Iteratorc                   P    e Zd ZdZdZd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zy)Iteratora  Base class for image data iterators.

    DEPRECATED.

    Every `Iterator` must implement the `_get_batches_of_transformed_samples`
    method.

    Args:
        n: Integer, total number of samples in the dataset to loop over.
        batch_size: Integer, size of a batch.
        shuffle: Boolean, whether to shuffle the data between epochs.
        seed: Random seeding for data shuffling.
    )pngjpgjpegbmpppmtiftiffc                     || _         || _        || _        || _        d| _        d| _        t        j                         | _        d | _	        | j                         | _        y Nr   )n
batch_sizeseedshufflebatch_indextotal_batches_seen	threadingLocklockindex_array_flow_indexindex_generator)selfr   r   r   r   s        T/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/legacy/preprocessing/image.py__init__zIterator.__init__%   sU    $	"#NN$	#//1    c                     t        j                  | j                        | _        | j                  r/t         j
                  j                  | j                        | _        y y N)nparanger   r   r   randompermutationr    s    r!   _set_index_arrayzIterator._set_index_array0   s>    99TVV,<<!yy44TVV<D r#   c                    |t        |       k\  r%t        dj                  |t        |                   | j                  6t        j
                  j                  | j                  | j                  z          | xj                  dz  c_        | j                  | j                          | j                  | j                  |z  | j                  |dz   z   }| j                  |      S )NzEAsked to retrieve element {idx}, but the Sequence has length {length})idxlength   )len
ValueErrorformatr   r&   r(   r   r   r+   r   #_get_batches_of_transformed_samples)r    r-   r   s      r!   __getitem__zIterator.__getitem__5   s    #d)&&,fSYf&G 
 99 IINN499t'>'>>?1$#!!#&&OOc!DOOsQw$?
 77DDr#   c                 T    | j                   | j                  z   dz
  | j                  z  S )Nr/   )r   r   r*   s    r!   __len__zIterator.__len__F   s#    (1,@@r#   c                 $    | j                          y r%   )r+   r*   s    r!   on_epoch_endzIterator.on_epoch_endI   s    r#   c                     d| _         y r   )r   r*   s    r!   resetzIterator.resetL   s
    r#   c              #   :  K   | j                          	 | j                  6t        j                  j                  | j                  | j                  z          | j
                  dk(  r| j                          | j                  dk(  rd}n&| j
                  | j                  z  | j                  z  }| j                  || j                  z   kD  r| xj
                  dz  c_        nd| _        | xj                  dz  c_        | j                  ||| j                  z     w)Nr/   r   )
r:   r   r&   r(   r   r   r+   r   r   r   )r    current_indexs     r!   r   zIterator._flow_indexO   s     

yy$		tyy4+B+BBC1$%%'vv{ !!%!1!1DOO!Ctvv Mvv77  A% #$ ##q(#"" ? ! s   DDc                     | S r%    r*   s    r!   __iter__zIterator.__iter__f   s	     r#   c                     | j                   5  t        | j                        }d d d        | j                        S # 1 sw Y   xY wr%   )r   nextr   r3   r    r   s     r!   __next__zIterator.__next__k   sA    YY 	5t334K	5 77DD		5 	5s	   <Ac                     t         )Gets a batch of transformed samples.

        Args:
            index_array: Array of sample indices to include in batch.
        Returns:
            A batch of transformed samples.
        )NotImplementedErrorrB   s     r!   r3   z,Iterator._get_batches_of_transformed_samplesr   s
     "!r#   N)__name__
__module____qualname____doc__white_list_formatsr"   r+   r4   r6   r8   r:   r   r?   rC   r3   r>   r#   r!   r
   r
      sE     M	2=
E"A .
E"r#   r
   c              #     K   fd} ||       D ]p  \  }}}t        |      D ]\  }|j                         j                  d      rt        j                  d       |j                         j                  |      sW||f ^ r yw)a  Iterates on files with extension.

    Args:
        directory: Absolute path to the directory
            containing files to be counted
        white_list_formats: Set of strings containing allowed extensions for
            the files to be counted.
        follow_links: Boolean, follow symbolic links to subdirectories.
    Yields:
        Tuple of (root, filename) with extension in `white_list_formats`.
    c                 J    t        t        j                  |       d       S )N)followlinksc                     | d   S r   r>   xs    r!   <lambda>z<_iter_valid_files.<locals>._recursive_list.<locals>.<lambda>   s
    ad r#   )key)sortedoswalk)subpathfollow_linkss    r!   _recursive_listz*_iter_valid_files.<locals>._recursive_list   s     GGG6N
 	
r#   z.tiffzYUsing ".tiff" files with multiple bands will cause distortion. Please verify your output.N)rT   lowerendswithwarningswarn)	directoryrK   rX   rY   root_filesfnames     `     r!   _iter_valid_filesrc   }   s     

 *)4 "aE] 	"E{{}%%g.H {{}%%&89Ek!	""s   A6B:Bc                    t         j                  j                  |       }|rIt        t	        | ||            }t        |      }t        |d   |z        t        |d   |z        }	}|||	 }
nt	        | ||      }
g }g }|
D ]  \  }}|j                  ||          t         j                  j                  ||      }t         j                  j                  |t         j                  j                  ||             }|j                  |        ||fS )a<  Lists paths of files in `subdir` with extensions in `white_list_formats`.

    Args:
        directory: absolute path to a directory containing the files to list.
            The directory name is used as class label
            and must be a key of `class_indices`.
        white_list_formats: set of strings containing allowed extensions for
            the files to be counted.
        split: tuple of floats (e.g. `(0.2, 0.6)`) to only take into
            account a certain fraction of files in each directory.
            E.g.: `segment=(0.6, 1.0)` would only account for last 40 percent
            of images in each directory.
        class_indices: dictionary mapping a class name to its index.
        follow_links: boolean, follow symbolic links to subdirectories.

    Returns:
         classes: a list of class indices
         filenames: the path of valid files in `directory`, relative from
             `directory`'s parent (e.g., if `directory` is "dataset/class1",
            the filenames will be
            `["class1/file1.jpg", "class1/file2.jpg", ...]`).
    r   r/   )
rU   pathbasenamelistrc   r0   intappendjoinrelpath)r^   rK   splitclass_indicesrX   dirname	all_files	num_filesstartstopvalid_filesclasses	filenamesr_   rb   absolute_pathrelative_paths                    r!   "_list_valid_filenames_in_directoryrx      s   2 ggy)Gi);\J
	 	N	%(Y./U1X	5I1Jtd+')<
 GI" (e}W-.T51RWW__]I>
 	'( Ir#   c                   L    e Zd ZdZd Zd Zed        Zed        Zed        Z	y)BatchFromFilesMixinz|Adds methods related to getting batches from filenames.

    It includes the logic to transform image files to batches.
    c                    || _         t        |      | _        |
| _        |dvrt	        d| d      || _        || _        | j
                  dk(  r9| j                  dk(  r| j                  dz   | _        nd| j                  z   | _        n| j
                  dk(  r9| j                  dk(  r| j                  dz   | _        nMd| j                  z   | _        n8| j                  dk(  r| j                  d	z   | _        nd	| j                  z   | _        || _        || _	        || _
        |	| _        |9| j                   j                  }|dk(  rd|f}n|dk(  r|df}nt	        d| d      d
}|| _        || _        y
)a  Sets attributes to use later for processing files into a batch.

        Args:
            image_data_generator: Instance of `ImageDataGenerator`
                to use for random transformations and normalization.
            target_size: tuple of integers, dimensions to resize input images
            to.
            color_mode: One of `"rgb"`, `"rgba"`, `"grayscale"`.
                Color mode to read images.
            data_format: String, one of `channels_first`, `channels_last`.
            save_to_dir: Optional directory where to save the pictures
                being yielded, in a viewable format. This is useful
                for visualizing the random transformations being
                applied, for debugging purposes.
            save_prefix: String prefix to use for saving sample
                images (if `save_to_dir` is set).
            save_format: Format to use for saving sample images
                (if `save_to_dir` is set).
            subset: Subset of data (`"training"` or `"validation"`) if
                validation_split is set in ImageDataGenerator.
            interpolation: Interpolation method used to resample the image if
                the target size is different from that of the loaded image.
                Supported methods are "nearest", "bilinear", and "bicubic". If
                PIL version 1.1.3 or newer is installed, "lanczos" is also
                supported. If PIL version 3.4.0 or newer is installed, "box" and
                "hamming" are also supported. By default, "nearest" is used.
            keep_aspect_ratio: Boolean, whether to resize images to a target
                size without aspect ratio distortion. The image is cropped in
                the center with target aspect ratio before resizing.
        >   rgbrgba	grayscalezInvalid color mode: z); expected "rgb", "rgba", or "grayscale".r}   channels_last)   r|   )   )r/   N
validationr   trainingr/   Invalid subset name: z$;expected "training" or "validation")image_data_generatortupletarget_sizekeep_aspect_ratior1   
color_modedata_formatimage_shapesave_to_dirsave_prefixsave_formatinterpolation_validation_splitrl   subset)r    r   r   r   r   r   r   r   r   r   r   validation_splitrl   s                r!   set_processing_attrsz(BatchFromFilesMixin.set_processing_attrs   s   V %9! -!299&zl;<  %&??f$?2#'#3#3d#: #'$*:*:#: __%?2#'#3#3d#: #'$*:*:#: ?2#'#3#3d#: #'$*:*:#: &&&*#88JJ%,-:%)1- +F8 4: : 
 E
r#   c                    t        j                  t        |      f| j                  z   | j                        }| j
                  }t        |      D ]  \  }}t        j                  ||   | j                  | j                  | j                  | j                        }t        j                  || j                        }t        |d      r|j!                          | j"                  r\| j"                  j%                  |j&                        }| j"                  j)                  ||      }| j"                  j+                  |      }|||<    | j,                  rt        |      D ]  \  }}t        j.                  ||   | j                  d      }dj1                  | j2                  |t         j4                  j7                  d      | j8                  	      }	|j;                  t<        j>                  jA                  | j,                  |	              | jB                  d
k(  r|jE                         }
n!| jB                  dv rPt        jF                  t        |      | j                        }
t        |      D ]  \  }}| jH                  |   |
|<    n| jB                  dk(  rgt        j                  t        |      t        | jJ                        f| j                        }
t        |      D ]  \  }}d|
|| jH                  |   f<    nM| jB                  dk(  r| jL                  D cg c]  }||   	 }
}n!| jB                  dk(  r| jL                  |   }
n|S | jN                  ||
fS ||
| jN                  |   fS c c}w )rE   dtype)r   r   r   r   )r   closeTscale {prefix}_{index}_{hash}.{format}g    cAprefixindexhashr2   input>   binarysparsecategoricalg      ?multi_outputraw)(r&   zerosr0   r   r   	filepaths	enumerater   load_imgr   r   r   r   img_to_arrayr   hasattrr   r   get_random_transformshapeapply_transformstandardizer   array_to_imgr2   r   r(   randintr   saverU   re   rj   
class_modecopyemptyrt   rm   labelssample_weight)r    r   batch_xr   ijimgrQ   paramsrb   batch_yn_observationoutputs                r!   r3   z7BatchFromFilesMixin._get_batches_of_transformed_samples*  s
    (($"2"22$**

 NN	k* 	DAq&&!?? ,,"00"&"8"8C (($:J:JKA sG$		((22GGP--==aH--99!<GAJ#	& !+. 
@1!..AJ 0 0 ;AA++**3/++	 B  d&6&6>?
@ ??g%llnG__ 44hhs7|4::>G$-k$: 9 =!\\-8
9__-hhWs4#5#567tzzG %.k$: > =:=4<<667>__.9=Evvk*EGE__%kk+.GN%G##GT%7%7%DDD Fs   M.c                 \    t        dj                  t        |       j                              )z&List of absolute paths to image files.z;`filepaths` property method has not been implemented in {}.rF   r2   typerG   r*   s    r!   r   zBatchFromFilesMixin.filepathso  s*     "&&,fT$Z-@-@&A
 	
r#   c                 \    t        dj                  t        |       j                              )z"Class labels of every observation.z8`labels` property method has not been implemented in {}.r   r*   s    r!   r   zBatchFromFilesMixin.labelsw  s,     "FMMT
##
 	
r#   c                 \    t        dj                  t        |       j                              )Nz?`sample_weight` property method has not been implemented in {}.r   r*   s    r!   r   z!BatchFromFilesMixin.sample_weight  s(    !&&,fT$Z-@-@&A
 	
r#   N)
rG   rH   rI   rJ   r   r3   propertyr   r   r   r>   r#   r!   rz   rz      sR    
VpCEJ 
 
 
 
 
 
r#   rz   z3keras._legacy.preprocessing.image.DirectoryIteratorc                   |     e Zd ZdZh dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zed        Zed        Zed        Z	 xZ
S )DirectoryIteratorzRIterator capable of reading images from a directory on disk.

    DEPRECATED.
    >   Nr   r   r   r   c                 x   |
t        j                         }
|t        j                         }t        |   ||||
||||||
       | _        || _        || j                  vr%t        dj                  || j                              || _
        || _        d| _        |stg }t        t        j                              D ]Q  }t        j                   j#                  t        j                   j%                  |            sA|j'                  |       S t)        |      | _        t-        t/        |t1        t)        |                        | _        t4        j6                  j9                         }g }g | _        d}fd|D        D ]J  }|j'                  |j=                  t>        || j@                  | jB                  | j2                  |f             L g }|D ];  }|jE                         \  }}|j'                  |       | xj:                  |z  c_        = t)        | j:                        | _        tG        jH                  | j                  fd      | _        |D ]+  }|| j                  ||t)        |      z    |t)        |      z  }- tK        jL                  d| j                   d| j*                   d       |jO                          |j%                          | j:                  D cg c],  }t        j                   j%                  | j
                  |      . c}| _(        t        |   | j                  |||	       y c c}w )	N+Invalid class_mode: {}; expected one of: {}r   c              3   ^   K   | ]$  }t         j                  j                  |       & y wr%   )rU   re   rj   ).0subdirr^   s     r!   	<genexpr>z-DirectoryIterator.__init__.<locals>.<genexpr>  s      NFY7Ns   *-int32r   Found z images belonging to 	 classes.)*r   image_data_formatfloatxsuperr   r^   rt   allowed_class_modesr1   r2   r   r   samplesrT   rU   listdirre   isdirrj   ri   r0   num_classesdictziprangerm   multiprocessingpool
ThreadPoolru   apply_asyncrx   rK   rl   getr&   r   r   	print_msgr   
_filepathsr"   )r    r^   r   r   r   rt   r   r   r   r   r   r   r   r   rX   r   r   r   r   r   r   resultsr   dirpathclasses_listresru   rb   	__class__s    `                          r!   r"   zDirectoryIterator.__init__  s   * !335K=NN$E$ 	
 #T555=DD 8 8 
 %
G I!67 +77==i!@ANN6*+ w<!#guS\/B"CD##..0 NgN 	GNN  6//

**$		  	(C!$GY(NNi'N	( 4>>*xxw?# 	G18DLLQW-.WA	 	T\\N"7 	+	
 	

		=A^^
49BGGLL/
 	z7DA
s   !1L7c                     | j                   S r%   r   r*   s    r!   r   zDirectoryIterator.filepaths      r#   c                     | j                   S r%   )rt   r*   s    r!   r   zDirectoryIterator.labels  s    ||r#   c                      y r%   r>   r*   s    r!   r   zDirectoryIterator.sample_weight  s     r#   )   r   r|   Nr       TNNN r   FNnearestFN)rG   rH   rI   rJ   r   r"   r   r   r   r   __classcell__r   s   @r!   r   r     s    
 M  'bBH      r#   r   z4keras._legacy.preprocessing.image.NumpyArrayIteratorc                   @     e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Z xZS )NumpyArrayIteratorz@Iterator yielding data from a Numpy array.

    DEPRECATED.
    c           	         |t        j                         }|t        j                         }|| _        t	        |t
              st	        |t              rt	        |d   t              st        j                  |d         g}n%|d   D cg c]  }t        j                  |       }}|d   }|D ]<  }t        |      t        |      k7  st        dt        |       dt        |              ng }|bt        |      t        |      k7  rKt        dt        j                  |      j                   dt        j                  |      j                         |bt        |      t        |      k7  rKt        dt        j                  |      j                   dt        j                  |      j                         ||d	vrt        d
| d      t        t        |      |j                  z        }|O|sMt        j                  t        j                  |d |       t        j                  ||d              st        d      |dk(  r2|d | }|D cg c]  }t        j                  |d |        }}|7|d | }n1||d  }|D cg c]  }t        j                  ||d         }}|||d  }t        j                  || j                        | _        || _        | j                   j$                  dk7  r"t        d| j                   j                         |dk(  rdnd}| j                   j                  |   dvrt'        j(                  d|z   dz   t+        |      z   dz   t+        |      z   dz   t+        | j                   j                        z   dz   t+        | j                   j                  |         z   dz          |t        j                  |      | _        nd | _        |t        j                  |      | _        nd | _        || _        || _        |	| _        |
| _        || _        t:        | y  |j                  d   |||       y c c}w c c}w c c}w )Nr/   r   zUAll of the arrays in `x` should have the same length. Found a pair with: len(x[0]) = z, len(x[?]) = zS`x` (images tensor) and `y` (labels) should have the same length. Found: x.shape = z, y.shape = zV`x` (images tensor) and `sample_weight` should have the same length. Found: x.shape = z, sample_weight.shape = >   r   r   r   z&; expected "training" or "validation".zTraining and validation subsets have different number of classes after the split. If your numpy arrays are sorted by the label, you might want to shuffle them.r   r   r   zVInput data in `NumpyArrayIterator` should have rank 4. You passed an array with shape r   r      r/   r   r   z=NumpyArrayIterator is set to use the data format convention "" (channels on axis z4), i.e. expected either 1, 3, or 4 channels on axis -. However, it was passed an array with shape  ( channels).)r   r   r   r   
isinstancer   rg   r&   asarrayr0   r1   r   rh   r   array_equaluniquerQ   x_miscndimr\   r]   stryr   r   r   r   r   r   r   r"   )r    rQ   r  r   r   r   r   r   r   r   r   r   r   ignore_class_splitr   r   xx	split_idxchannels_axisr   s                      r!   r"   zNumpyArrayIterator.__init__
  sV   " !335K=NN$E
a:a#6adD)**QqT*+34Q48R"**R.88!A q6SW$$' (+1vhnSWIG  F=SVs1v-$$&JJqM$7$7#8 9ZZ]0013  $Q3}3E)E$$&JJqM$7$7#8 9))+M)B)H)H(IK  77 +F8<=  CF%9%K%KKLI *IIa
m,bii)*.F !'  %jyM?EF"**R
^4FF=*9AijM?EF"**R	
^4FF=)*AATZZ066;;!"ffll^- 
 )O;66<<&i7MMO() m$% I	I
 m$% BB dffll#$  dffll=12	3  
  =ZZ]DFDF$!#M!:D!%D$8!&&&&Z$?C 9h G
 Gs   	Q Q?Q
c                 P   t        j                  t        t        |      gt	        | j
                  j                        dd  z         | j                        }t        |      D ]  \  }}| j
                  |   }| j                  j                  |j                        }| j                  j                  |j                  | j                        |      }| j                  j                  |      }|||<    | j                  rt        |      D ]  \  }}t        j                   ||   | j"                  d      }dj%                  | j&                  |t         j(                  j+                  d      | j,                        }|j/                  t0        j2                  j5                  | j                  |              | j6                  D 	cg c]  }	|	|   	 }
}	|
s|n|g|
z   f}| j8                  |d   S || j8                  |   fz  }| j:                  || j:                  |   fz  }|S c c}	w )	Nr/   r   Tr   r   g     @r   r   )r&   r   r   r0   rg   rQ   r   r   r   r   r   r   astyper   r   r   r   r   r2   r   r(   r   r   r   rU   re   rj   r   r  r   )r    r   r   r   r   rQ   r   r   rb   r  batch_x_miscsr   s               r!   r3   z6NumpyArrayIterator._get_batches_of_transformed_samples  s   ((3{#$tDFFLL'9!"'==>djj
 k* 	DAqq	A..CCAGGLF))99$fA ))55a8AGAJ	 !+. 
@1!..AJ 0 0 ;AA++**3/++	 B  d&6&6>?
@ 48;;?RK??!.'WI4MO66>!9466+&(()t))+688F @s   H#)r   FNNNNr   r   NFN)rG   rH   rI   rJ   r"   r3   r   r   s   @r!   r   r     s8      {@z r#   r   c                     | j                         j                  |      xr t        j                  j	                  |       S )zCheck if a filename refers to a valid file.

    Args:
        filename: String, absolute path to a file
        white_list_formats: Set, allowed file extensions
    Returns:
        A boolean value indicating if the filename is valid or not
    )rZ   r[   rU   re   isfile)filenamerK   s     r!   validate_filenamer    s6     >>$$%78 RWW^^> r#   c                        e Zd ZdZh dZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d fd	Zd Zd Zed        Z	d Z
ed        Zed	        Zed
        Z xZS )DataFrameIteratorzCIterator capable of reading images from a directory as a dataframe.>   Nr   r   r   r   r   r   c                 F   t         |   ||||||||||
       |j                         }|xs d| _        |
| _        || _        | j                  |||||	       |r| j                  ||      }|
dvrM| j                  |||	      \  }}	t        |	      }t        t        |	t        t        |	                        | _        | j                  rVt        |      }t        | j                  d   |z        }t        | j                  d   |z        }|j                   ||d d f   }|
dvr| j#                  ||      | _        ||   j'                         | _        |r||   j*                  nd | _        |
dk(  r9|D cg c](  }t/        j0                  ||   j'                               * c}| _        |
dk(  r||   j*                  | _        t        | j(                        | _        |rdnd}|
dv r't7        j8                  d	| j4                   d
| d       n)t7        j8                  d	| j4                   d
| d d       | j(                  D cg c],  }t:        j<                  j?                  | j                  |      . c}| _         t         |   | j4                  |||       y c c}w c c}w )Nr   )r   r   r   Nr   r/   r   r   	validatedznon-validatedr    z image filenames.z image filenames belonging to r   )"r   r   r   r^   r   r   _check_params_filter_valid_filepaths_filter_classesr0   r   r   r   rm   rl   rh   ilocget_classesrt   tolistru   values_sample_weightr&   array_targetsr   r   r   rU   re   rj   r   r"   ) r    	dataframer^   r   x_coly_col
weight_colr   r   rt   r   r   r   r   r   r   r   r   r   r   r   r   validate_filenamesdfr   rp   rq   rr   colvalidated_stringrb   r   s                                   r!   r"   zDataFrameIterator.__init__  s   2 	$ 	
 ^^"b$
2ueZA--b%8BCC..r5'BKBg,K!%c'5W3F&G!HD::BI

1	12Etzz!}y01DtQ'BCC++B6DLE))+7Abn33t'CHICRXXbgnn&67IDMuI,,DM4>>*-K? 	 ??a(8'99JK a(8'9 :  +}I7
 >B^^
49BGGLL/
 	z7DA' J 
s   3-J1Jc                 &   | j                   | j                  vr/t        dj                  | j                   | j                              | j                   dk(  rHt	        |t
              s8t        dj                  | j                   t        |      j                              t        ||   j                  d             st        d| d      | j                   dv rCt        ||   j                  d             s%t        d	j                  | j                   |            | j                   d
k(  r|r<t        |      }t        |      dk7  rdt        dj                  t        |                  ||   j                         dk7  r+t        dj                  ||   j                                     | j                   dk(  rVt        t
        t        ft        ||   j                  fd            s%t        dj                  | j                   |            |r<| j                   dv r.t!        j"                  dj                  | j                                |rAt%        ||   j&                  j                  t(        j*                        st        d| d      y y )Nr   r   z6If class_mode="{}", y_col must be a list. Received {}.c                 "    t        | t              S r%   r   r  rP   s    r!   rR   z1DataFrameIterator._check_params.<locals>.<lambda>,  s    Z3-? r#   zAll values in column x_col=z must be strings.>   r   r   c                 "    t        | t              S r%   r'  rP   s    r!   rR   z1DataFrameIterator._check_params.<locals>.<lambda>2  s    As1C r#   z=If class_mode="{}", y_col="{}" column values must be strings.r      zGIf class_mode="binary" there must be 2 classes. {} class/es were given.zAIf class_mode="binary" there must be 2 classes. Found {} classes.r   c                     t        |       S r%   )r   )rQ   typess    r!   rR   z1DataFrameIterator._check_params.<locals>.<lambda>H  s    Au1E r#   zPIf class_mode="{}", y_col="{}" column values must be type string, list or tuple.>   Nr   r   r   z3`classes` will be ignored given the class_mode="{}"zColumn weight_col=z must be numeric.)r   r   r1   r2   r   rg   	TypeErrorr   rG   allapplysetr0   nuniquer  r   r\   r]   
issubclassr   r&   number)r    r"  r  r  r   rt   r+  s         @r!   r  zDataFrameIterator._check_params  sR   ??$":"::=DDOOT%=%=  OO~-z%7NHOOOOT%[%9%9  2e9??#?@A-eW4EF  ??22r%y'CDE..4fT__e.L 
 ??h&g,w<1$$;;A6#g,;O  E""$) ((.r%y/@/@/B(C 
 ??m+$&Er%y'EFGAAGB  t +
 
 MMELLOO jJ)=)=)B)BBIIN0<MNOO O:r#   c                     g }||   D ]c  }t        |t        t        f      r-|j                  |D cg c]  }| j                  |    c}       F|j                  | j                  |          e |S c c}w r%   )r   rg   r   ri   rm   )r    r"  r  r   labellbls         r!   r  zDataFrameIterator.get_classes_  sn    Y 	9E%$/%H3t11#6HId0078		9
  Is   A0
c                    | j                         } fdrRt        t        j                  j	                        j                               |    j                  fd      | <   nXt               |    D ];  }t        |t        t        f      rj                  |       +j                  |       = t              | j                  g      fS )Nc                     t        | t        t        f      r| D cg c]	  }||v s| } }| xs d S t        | t              r| |v r| S d S t	        dj                  t        |                   c c}w )Nz7Expect string, list or tuple but found {} in {} column )r   rg   r   r  r,  r2   r   )r   rt   clsr  s      r!   remove_classesz9DataFrameIterator._filter_classes.<locals>.remove_classesl  sx    &4-0)/B#3'>#BB~%FC(!'7!2v<<117Ve1L  Cs
   	A.A.c                      |       S r%   r>   )rQ   rt   r9  s    r!   rR   z3DataFrameIterator._filter_classes.<locals>.<lambda>{  s    .G2L r#   )r   )r   rg   collectionsOrderedDictfromkeyskeysr.  r/  r   r   updateaddrT   dropna)r"  r  rt   vr9  s    `` @r!   r  z!DataFrameIterator._filter_classesh  s    WWY
	 ;22;;GDIIKLG5	(LMBuIeGY #a$/NN1%KKN	#
 WoGyyy('11r#   c                      ||   j                   fd      }|j                  t         j                  f      }| j	                         }|r%t        j                  dj                  ||             ||   S )a&  Keep only dataframe rows with valid filenames.

        Args:
            df: Pandas dataframe containing filenames in a column
            x_col: string, column in `df` that contains the filenames or
                filepaths
        Returns:
            absolute paths to image files
        c                 X    t         j                  j                  j                  |       S r%   )rU   re   rj   r^   )rb   r    s    r!   rR   z;DataFrameIterator._filter_valid_filepaths.<locals>.<lambda>  s    "'',,t~~u= r#   )argszTFound {} invalid image filename(s) in x_col="{}". These filename(s) will be ignored.)mapr.  r  rK   sumr\   r]   r2   )r    r"  r  r   mask	n_invalids   `     r!   r  z)DataFrameIterator._filter_valid_filepaths  sw     uIMM=
	 T%<%<$>  
 UKKM	MM55;VIu5M $xr#   c                     | j                   S r%   r   r*   s    r!   r   zDataFrameIterator.filepaths  r   r#   c                 N    | j                   dv r| j                  S | j                  S )N>   r   r   )r   r  rt   r*   s    r!   r   zDataFrameIterator.labels  s#    ??55== <<r#   c                     | j                   S r%   )r  r*   s    r!   r   zDataFrameIterator.sample_weight  s    """r#   )NNr  classNr   r|   Nr   r   TNr   Nr   r   Nr   Ffloat32T)rG   rH   rI   rJ   r   r"   r  r  staticmethodr  r  r   r   r   r   r   r   s   @r!   r  r    s    M ! #/TBlBPH 2 2:0       # #r#   r  c                     t        j                  |       j                  |d      } | d d ddf   } | j                  d|      } | S )Nr   .)r&   r   swapaxes)rQ   axiss     r!   	flip_axisrT    sB    


1tQ'A	$B$)A	

1dAHr#   z4keras._legacy.preprocessing.image.ImageDataGeneratorc                       e Zd ZdZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 ddZd ZddZ	d	 Z
dd
ZddZy)ImageDataGeneratorDEPRECATED.Nc                    |t        j                         }|t        j                         }|| _        || _        || _        || _        || _        || _        || _	        || _
        |	| _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        || _        |dvrt/        d|       || _        |dk(  rd| _        d| _        d| _        |dk(  rd| _        d| _        d| _        |rd|cxk  rdk  sn t/        d	|       || _        d | _        d | _        d | _        tA        |tB        tD        f      rd|z
  d|z   g| _        n>tG        |      dk(  r"tI        d
 |D              r|d   |d   g| _        nt/        d|       |r<|sd| _        tK        jL                  d       |rd| _        tK        jL                  d       |r|sd| _        tK        jL                  d       |r|sd| _        tK        jL                  d       |
2tA        |
tN        tP        f      rtG        |
      dk7  rt/        d|
       |
| _)        y )N>   r   channels_firstz`data_format` should be `"channels_last"` (channel after row and column) or `"channels_first"` (channel before row and column). Received: rY  r/   r)  r   r   r   z@`validation_split` must be strictly between 0 and 1.  Received: c              3   H   K   | ]  }t        |t        t        f        y wr%   )r   floatrh   )r   vals     r!   r   z.ImageDataGenerator.__init__.<locals>.<genexpr>
  s       *
.1JsUCL)*
s    "zK`zoom_range` should be a float or a tuple or list of two floats. Received: TzcThis ImageDataGenerator specifies `zca_whitening`, which overrides setting of `featurewise_center`.FzlThis ImageDataGenerator specifies `zca_whitening` which overrides setting of`featurewise_std_normalization`.zsThis ImageDataGenerator specifies `featurewise_std_normalization`, which overrides setting of `featurewise_center`.zqThis ImageDataGenerator specifies `samplewise_std_normalization`, which overrides setting of `samplewise_center`.C`brightness_range should be tuple or list of two floats. Received: )*r   r   r   featurewise_centersamplewise_centerfeaturewise_std_normalizationsamplewise_std_normalizationzca_whiteningzca_epsilonrotation_rangewidth_shift_rangeheight_shift_rangeshear_range
zoom_rangechannel_shift_range	fill_modecvalhorizontal_flipvertical_fliprescalepreprocessing_functionr   interpolation_orderr1   r   channel_axisrow_axiscol_axisr   meanstdzca_whitening_matrixr   r[  rh   r0   r-  r\   r]   r   rg   brightness_range)r    r^  r_  r`  ra  rb  rc  rd  re  rf  rw  rg  rh  ri  rj  rk  rl  rm  rn  ro  r   r   rp  r   s                           r!   r"   zImageDataGenerator.__init__  s   4 !335K=NN$E"4!2-J*,H)*&,!2"4&$#6 "	.*&<#
#6 AA )M+  '** !DDMDM/) !DDMDMA(8$<1$<./1  "2	$(!j5#,/ :~q:~>DO_!c *
5?*
 '
  *!}jm<DO'L* 
 %*.'7
 -5:27 )%*.', ($)-&+ '/%?'(A- !!1 24  !1r#   c                 \    t        ||| ||||| j                  |||	|
|| j                        S )N)r   r   r   r   r   r   r   r   r  r   r   )r   r   r   )r    rQ   r  r   r   r   r   r   r   r   r  r   s               r!   flowzImageDataGenerator.flowA  sE     "!'((###1**
 	
r#   c                     t        || fi d|d|d|d|d|d| j                  d|d|d	|d
|	d|
d|d|d|d|d| j                  S )Nr   r   r   rt   r   r   r   r   r   r   r   r   rX   r   r   r   )r   r   r   )r    r^   r   r   rt   r   r   r   r   r   r   r   rX   r   r   r   s                   r!   flow_from_directoryz&ImageDataGenerator.flow_from_directory`  s    $ !
 $
 "	

 0
 
 "
 ((
 "
 
 
 $
 $
 $
 &
  !
" (#
$ **%
 	
r#   c                    d|v rt        j                  dt               d|v rt        j                  dt               |	dk(  rt        j                  dt               d}	d|v rt        j                  d	t               t        ||| fi d
|d|d|d|d|d|d|	d| j                  d|
d|d|d|d|d|d|d|d|d| j
                  S )Nhas_extz\has_ext is deprecated, filenames in the dataframe have to match the exact filenames in disk.sortzwsort is deprecated, batches will be created in thesame order than the filenames provided if `shuffle`is set to `False`.otherzB`class_mode="other"` is deprecated, please use `class_mode="raw"`.r   drop_duplicateszldrop_duplicates is deprecated, you can drop duplicates by using the pandas.DataFrame.drop_duplicates method.r  r  r   r   r   rt   r   r   r   r   r   r   r   r   r   r   r!  r   )r\   r]   DeprecationWarningr  r   r   )r    r  r^   r  r  r   r   r   rt   r   r   r   r   r   r   r   r   r   r!  kwargss                       r!   flow_from_dataframez&ImageDataGenerator.flow_from_dataframe  sX   , MM8"
 VMM% #	  MM&"
 J&MMH" !
 	

 
 "
 $
 "
 
 "
 ((
 "
 
 
 $
  $!
" $#
$ %
& ('
(  2)
* **+
 	
r#   c                 .   | j                   r| j                  |      }| j                  r|| j                  z  }| j                  r|t        j                  |d      z  }| j
                  r|t        j                  |d      dz   z  }| j                  r1| j                  || j                  z  }nt        j                  d       | j                  r4| j                  || j                  dz   z  }nt        j                  d       | j                  r| j                  c|j                  dt        j                  |j                  dd             }|| j                  z  }t        j                  ||j                        }|S t        j                  d	       |S )
a^  Applies the normalization configuration in-place to a batch of
        inputs.

        `x` is changed in-place since the function is mainly used internally
        to standardize images and feed them to your network. If a copy of `x`
        would be created instead it would have a significant performance cost.
        If you want to apply this method without changing the input in-place
        you can call the method creating a copy before:

        standardize(np.copy(x))

        Args:
            x: Batch of inputs to be normalized.

        Returns:
            The inputs, normalized.
        T)keepdimsư>NzThis ImageDataGenerator specifies `featurewise_center`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.zThis ImageDataGenerator specifies `featurewise_std_normalization`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.rQ  zThis ImageDataGenerator specifies `zca_whitening`, but it hasn't been fit on any training data. Fit it first by calling `.fit(numpy_data)`.)ro  rn  r_  r&   rt  ra  ru  r^  r\   r]   r`  rb  rv  reshapeprodr   )r    rQ   flat_xwhite_xs       r!   r   zImageDataGenerator.standardize  s[   $ &&++A.A<<A!!T**A,,D)D00A""yy$TYY; --xx#TXX_$; ((42rwwqwwrs|'<= 4#<#<<JJw0  ; r#   c                    | j                   dz
  }| j                  dz
  }|t        j                  j	                  |       | j
                  r6t        j                  j                  | j
                   | j
                        }nd}| j                  ry	 t        j                  j                  | j                        }|t        j                  j                  ddg      z  }t        j                  | j                        dk  r|||   z  }nd}| j                  ry	 t        j                  j                  | j                        }|t        j                  j                  ddg      z  }t        j                  | j                        dk  r|||   z  }nd}| j                  r6t        j                  j                  | j                   | j                        }nd}| j                  d   dk(  r| j                  d   dk(  rd\  }	}
n>t        j                  j                  | j                  d   | j                  d   d      \  }	}
t        j                  j                         dk  | j                  z  }t        j                  j                         dk  | j                  z  }d}| j                   dk7  r5t        j                  j                  | j                    | j                         }d}| j"                  :t        j                  j                  | j"                  d   | j"                  d         }|||||	|
||||d
}|S # t        $ r9 t        j                  j                  | j                   | j                        }Y w xY w# t        $ r9 t        j                  j                  | j                   | j                        }Y Xw xY w)	aF  Generates random parameters for a transformation.

        Args:
            img_shape: Tuple of integers.
                Shape of the image that is transformed.
            seed: Random seed.

        Returns:
            A dictionary containing randomly chosen parameters describing the
            transformation.
        r/   Nr   rQ  r/   r/   r)        ?)
thetatxtyshearzxzyflip_horizontalflip_verticalchannel_shift_intensity
brightness)rr  rs  r&   r(   r   rd  uniformrf  choicer1   maxre  rg  rh  rl  rm  ri  rw  )r    	img_shaper   img_row_axisimg_col_axisr  r  r  r  r  r  r  r  r  r  transform_parameterss                   r!   r   z'ImageDataGenerator.get_random_transform  sB    }}q(}}q(IINN4 II%%t':':&:D<O<OPEE""YY%%d&=&=>bii&&Aw//
 vvd--.2i--B!!YY%%d&<&<=bii&&Aw//
 vvd,,-1i--BII%%t'7'7&79I9IJEE??1"tq'9Q'>FBYY&&"DOOA$6FB 99++-3t7K7KK))+c1T5G5GG"&##q(&(ii&7&7)))4+C+C'# 
  ,**%%a($*?*?*BJ
 .*'>$ 
 $#}  YY&&,,,d.E.E  YY&&+++T-C-Cs&   AL7 AM< 7>M98M9<>N>=N>c                    | j                   dz
  }| j                  dz
  }| j                  dz
  }t        ||j	                  dd      |j	                  dd      |j	                  dd      |j	                  dd      |j	                  dd      |j	                  dd      |||| j
                  | j                  | j                  	      }|j	                  d
      t        ||d
   |      }|j	                  dd      rt        ||      }|j	                  dd      rt        ||      }|j	                  d      t        ||d   d      }|S )a%  Applies a transformation to an image according to given parameters.

        Args:
            x: 3D tensor, single image.
            transform_parameters: Dictionary with string - parameter pairs
                describing the transformation.
                Currently, the following parameters
                from the dictionary are used:
                - `'theta'`: Float. Rotation angle in degrees.
                - `'tx'`: Float. Shift in the x direction.
                - `'ty'`: Float. Shift in the y direction.
                - `'shear'`: Float. Shear angle in degrees.
                - `'zx'`: Float. Zoom in the x direction.
                - `'zy'`: Float. Zoom in the y direction.
                - `'flip_horizontal'`: Boolean. Horizontal flip.
                - `'flip_vertical'`: Boolean. Vertical flip.
                - `'channel_shift_intensity'`: Float. Channel shift intensity.
                - `'brightness'`: Float. Brightness shift intensity.

        Returns:
            A transformed version of the input (same shape).
        r/   r  r   r  r  r  r  r  )rr  rs  rq  rj  rk  orderr  r  Fr  r  )rr  rs  rq  apply_affine_transformr   rj  rk  rp  apply_channel_shiftrT  apply_brightness_shift)r    rQ   r  r  r  img_channel_axiss         r!   r   z"ImageDataGenerator.apply_transformi  sU   0 }}q(}}q(,,q0" $$Wa0 $$T1- $$T1- $$Wa0 $$T1- $$T1-!!)nn**
   ##$=>J#$%>? A  ##$5u=!\*A##OU;!\*A##L1=&'5uA r#   c                 ^    | j                  |j                  |      }| j                  ||      S )zApplies a random transformation to an image.

        Args:
            x: 3D tensor, single image.
            seed: Random seed.

        Returns:
            A randomly transformed version of the input (same shape).
        )r   r   r   )r    rQ   r   r   s       r!   random_transformz#ImageDataGenerator.random_transform  s-     **177D9##Av..r#   c                    t        j                  || j                        }|j                  dk7  r!t	        dt        |j                        z         |j                  | j                     dvrt        j                  d| j                  z   dz   t        | j                        z   dz   t        | j                        z   dz   t        |j                        z   d	z   t        |j                  | j                           z   d
z          |t         j                  j                  |       t        j                  |      }| j                  r|| j                  z  }|rt        j                  t!        ||j                  d   z  gt#        |j                        dd z         | j                        }t%        |      D ]I  }t%        |j                  d         D ],  }| j'                  ||         ||||j                  d   z  z   <   . K |}| j(                  rt        j*                  |d| j,                  | j.                  f      | _        g d}|j                  | j                     || j                  dz
  <   t        j0                  | j*                  |      | _        || j*                  z  }| j2                  rt        j4                  |d| j,                  | j.                  f      | _        g d}|j                  | j                     || j                  dz
  <   t        j0                  | j4                  |      | _        || j4                  dz   z  }| j6                  rt9        |      }	t        j0                  ||	df      }
t         j:                  j=                  |
j>                  d      \  }}}t        j@                  |	      || jB                  z   z  }||z  jE                  |j>                        | _#        yy)a)  Fits the data generator to some sample data.

        This computes the internal data stats related to the
        data-dependent transformations, based on an array of sample data.

        Only required if `featurewise_center` or
        `featurewise_std_normalization` or `zca_whitening`
        are set to `True`.

        When `rescale` is set to a value, rescaling is applied to
        sample data before computing the internal data stats.

        Args:
            x: Sample data. Should have rank 4.
             In case of grayscale data,
             the channels axis should have value 1, in case
             of RGB data, it should have value 3, and in case
             of RGBA data, it should have value 4.
            augment: Boolean (default: False).
                Whether to fit on randomly augmented samples.
            rounds: Int (default: 1).
                If using data augmentation (`augment=True`),
                this is how many augmentation passes over the data to use.
            seed: Int (default: None). Random seed.
        r   r   z<Input to `.fit()` should have rank 4. Got array with shape: r   zSExpected input to be images (as Numpy array) following the data format convention "r   z3), i.e. expected either 1, 3 or 4 channels on axis r   r   r   Nr   r/   rS  )r/   r/   r/   r  rQ  F)full_matrices)$r&   r   r   r   r1   r  r   rq  r\   r]   r   r(   r   r   rn  r   r   rg   r   r  r^  rt  rr  rs  r  r`  ru  rb  r0   linalgsvdTsqrtrc  dotrv  )r    rQ   augmentroundsr   axrr   broadcast_shaper   r  usr`   s_invs                  r!   fitzImageDataGenerator.fit  sa   4 JJq

+66Q;Nagg,  774$$%Y6MM9""# )) d''(	)
 HH d''() BB agg, 	 aggd//01
2    IINN4 GGAJ<<Av
*+d177mAB.??@jjB 6] Iqwwqz* IA-1-B-B1Q4-HBq1qwwqz>)*II A""DMM4==(IJDI'O56WWT=N=N5OOD--12

499o>DINA--vvaq$--&GHDH'O56WWT=N=N5OOD--12zz$((O<DHD AAAZZAr7+FiimmFHHEmBGAq!GGAJ!d&6&6"67E)*U(<D% r#   )FFFFFr  r           r  Nr  r  r  r   r  FFNNNr  r/   N)
Nr   TNNNr   r   FN)r   r|   Nr   r   TNNr   r   FNr   F)Nr  rM  Nr   r|   Nr   r   TNNr   r   Nr   Tr%   )Fr/   N)rG   rH   rI   rJ   r"   ry  r{  r  r   r   r   r  r  r>   r#   r!   rV  rV    s    !&+%*#1F1V  
D  !%
T  'G
R<|Y$v>@/U=r#   rV  z1keras._legacy.preprocessing.image.random_rotationc           
      n    t         j                  j                  | |      }t        | |||||||      } | S )rW  )r  rr  rs  rq  rj  rk  r  r&   r(   r  r  )	rQ   rgrr  rs  rq  rj  rk  rp  r  s	            r!   random_rotationr    sE     IIrc2&E	!!		A Hr#   z.keras._legacy.preprocessing.image.random_shiftc	                     | j                   |   | j                   |   }
}	t        j                  j                  | |      |	z  }t        j                  j                  | |      |
z  }t	        | ||||||||	      } | S )rW  )r  r  rr  rs  rq  rj  rk  r  )r   r&   r(   r  r  )rQ   wrghrgrr  rs  rq  rj  rk  rp  hwr  r  s                r!   random_shiftr  (  s     778aggh/qA			C4	%	)B			C4	%	)B	!!
	A Hr#   z.keras._legacy.preprocessing.image.random_shearc           
      n    t         j                  j                  | |      }t        | |||||||      } | S )rW  )r  rr  rs  rq  rj  rk  r  r  )	rQ   	intensityrr  rs  rq  rj  rk  rp  r  s	            r!   random_shearr  F  sE     IIyj)4E	!!		A Hr#   z-keras._legacy.preprocessing.image.random_zoomc                     t        |      dk7  rt        d|       |d   dk(  r|d   dk(  rd\  }}	n*t        j                  j	                  |d   |d   d      \  }}	t        | ||	||||||	      } | S )rW  r)  z@`zoom_range` should be a tuple or list of two floats. Received: r   r/   r  )r  r  rr  rs  rq  rj  rk  r  )r0   r1   r&   r(   r  r  )
rQ   rh  rr  rs  rq  rj  rk  rp  r  r  s
             r!   random_zoomr  `  s     :!#&
 	

 !}jmq0B"":a=*Q-CB	!!
	A Hr#   z5keras._legacy.preprocessing.image.apply_channel_shiftc                 B   t        j                  | |d      } t        j                  |       t        j                  |       }}| D cg c]  }t        j                  ||z   ||       }}t        j
                  |d      } t        j                  | d|dz         } | S c c}w )zPerforms a channel shift.

    DEPRECATED.

    Args:
        x: Input tensor. Must be 3D.
        intensity: Transformation intensity.
        channel_axis: Index of axis for channels in the input tensor.

    Returns:
        Numpy image tensor.
    r   r  r/   )r&   rollaxisminr  clipstack)rQ   r  rq  min_xmax_x	x_channelchannel_imagess          r!   r  r    s     	A|Q'A66!9bffQi5EFG9B	I%ue4N  	a(A
Aq,*+AHs   !Bz6keras._legacy.preprocessing.image.random_channel_shiftc                 `    t         j                  j                  | |      }t        | ||      S )a	  Performs a random channel shift.

    DEPRECATED.

    Args:
        x: Input tensor. Must be 3D.
        intensity_range: Transformation intensity.
        channel_axis: Index of axis for channels in the input tensor.

    Returns:
        Numpy image tensor.
    )rq  )r&   r(   r  r  )rQ   intensity_rangerq  r  s       r!   random_channel_shiftr    s,     		!!?"2ODIq),GGr#   z8keras._legacy.preprocessing.image.apply_brightness_shiftc                 J   ddl m} t        j                  |       t        j                  |       }}|dk  xs |dkD  }t        j                  | |xs |      } |j                  |       x} }|j                  |      } t        j                  |       } |s|r| dz  ||z
  z  |z   } | S )a  Performs a brightness shift.

    DEPRECATED.

    Args:
        x: Input tensor. Must be 3D.
        brightness: Float. The new brightness value.
        scale: Whether to rescale the image such that minimum and maximum values
            are 0 and 255 respectively. Default: True.

    Returns:
        Numpy image tensor.

    Raises:
        ImportError: if PIL is not available.
    r   )ImageEnhance   r   )
PILr  r&   r  r  r   r   
Brightnessenhancer   )rQ   r  r   r  x_minx_maxlocal_scaleimgenhancer_Brightnesss           r!   r  r    s    $ !66!9bffQi5E19.%#+K  +*>?A!-!8!8!;;A&&z2A  #A[Guu}%-Hr#   z3keras._legacy.preprocessing.image.random_brightnessc                     t        |      dk7  rt        d|       t        j                  j	                  |d   |d         }t        | ||      S )a  Performs a random brightness shift.

    DEPRECATED.

    Args:
        x: Input tensor. Must be 3D.
        brightness_range: Tuple of floats; brightness range.
        scale: Whether to rescale the image such that minimum and maximum values
            are 0 and 255 respectively. Default: True.

    Returns:
        Numpy image tensor.

    Raises:
        ValueError if `brightness_range` isn't a tuple.
    r)  r]  r   r/   )r0   r1   r&   r(   r  r  )rQ   rw  r   r  s       r!   random_brightnessr    sb    $ !)*,
 	

 			*1-/?/BCA!!Q..r#   c                 "   t        |      dz  dz
  }t        |      dz  dz
  }t        j                  dd|gdd|gg dg      }t        j                  dd| gdd| gg dg      }t        j                  t        j                  ||       |      }|S )Nr)  r  r/   r   r   r   r/   )r[  r&   r  r  )matrixrQ   r  o_xo_yoffset_matrixreset_matrixtransform_matrixs           r!   transform_matrix_offset_centerr    s    
(Q,
C
(Q,
CHHq!SkAq#;	BCM88aSD\Aq3$<CDLvvbff]F;\Jr#   z8keras._legacy.preprocessing.image.apply_affine_transformc                    t        j                  |||	g      j                  dk7  rt        d      t	        g d      }t	        |||	g      }||k7  rt        d||z
         | j
                  dk7  rt        d      |	dvrt        d      d}|d	k7  rt        j                  |      }t        j                  t        j                  |      t        j                  |       d	gt        j                  |      t        j                  |      d	gg d
g      }|}|d	k7  s|d	k7  r;t        j                  dd	|gd	d|gg d
g      }||}nt        j                  ||      }|d	k7  rwt        j                  |      }t        j                  dt        j                  |       d	gd	t        j                  |      d	gg d
g      }||}nt        j                  ||      }|dk7  s|dk7  r;t        j                  |d	d	gd	|d	gg d
g      }||}nt        j                  ||      }|| j                  |   | j                  |   }}t        |||      }t        j                  | |	d	      } ||kD  r |dddd	gf   |ddd	dgf<   |dd	g   |d	dg<   |ddddf   }|dddf   }| D cg c]1  }t        j                  j                   j#                  |||||
|      3 }}t        j$                  |d	      } t        j                  | d	|	dz         } | S c c}w )zYApplies an affine transformation specified by the parameters given.

    DEPRECATED.
    r   z;'row_axis', 'col_axis', and 'channel_axis' must be distinct)r   r/   r)  zInvalid axis' indices: z-Input arrays must be multi-channel 2D images.)r   r)  z7Channels are allowed and the first and last dimensions.Nr   r  r/   r)  )r  moderk  r  )r&   r   sizer1   r/  r   deg2radr  cossinr  r   r  r  r   ndimager   affine_transformr  )rQ   r  r  r  r  r  r  rr  rs  rq  rj  rk  r  valid_indicesactual_indicesr  rotation_matrixshift_matrixshear_matrixzoom_matrixr  r  final_affine_matrixfinal_offsetr  r  s                             r!   r  r    sL   . 
yy(Hl3499Q>I
 	

 	NM(Hl;<N&%n}&D%EF
 	
 	vv{HII6!E
 	
 z

5!((2uq1
 +	Qw"'xx!QaBZ CD#+!vv&6Ez

5!xx"&&-#a%:IF
 #+!vv&6E	Qw"'hhQ
QAJ	BC#*!vv&6D#wwx !''("319a
 KK<+ h*:1q!f9*EQAY''7A'?aV$.rr2A2v6'A. 

  MM''88# 9 

 

 HH^!,KK1lQ./H

s   6L)r/   r)  r   r   r  r/   )r   )T)r   r   r   r   r/   r/   r/   r)  r   r   r  r/   )'rJ   r;  r   rU   r   r\   numpyr&   	keras.srcr   keras.src.api_exportr   3keras.src.trainers.data_adapters.py_dataset_adapterr   keras.src.utilsr   r   keras.src.utils.module_utilsr   r
   rc   rx   rz   r   r   r  r  rT  rV  r  r  r  r  r  r  r  r  r  r  r>   r#   r!   <module>r      sj   7   	     - I ' $ . :;f"y f" <f"R":/dy
 y
x CDw+X w Ewt DEc c FcLs#+X s#l DEU	= U	= FU	=p AB 	 C2 >?
 	 @: >? 	 @2 => 	  ? F EF G. FGH HH" HI J< CD/ E/6 HI 
	
q Jqr#   