
    2VhK                         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 Z ed      	 	 	 	 	 	 	 	 	 	 	 	 	 d	d       Zy# e$ r dZY )w xY w)
    N)backend)ops)keras_export)BaseImagePreprocessingLayerc                     t        t        j                  |             dk7  rt        d      t	        ||      }| d|df   }|S )ay  Extracts a batch of images for plotting.

    Args:
        images: The 4D tensor or NumPy array of images.
        num_images: The number of images to extract.
        batch_size: The original batch size of the images.

    Returns:
        A 4D tensor or NumPy array containing the extracted images.

    Raises:
        ValueError: If `images` is not a 4D tensor/array.
       zO`plot_images_gallery()` requires you to batch your `np.array` samples together.N.)lenr   shape
ValueErrormin)images
num_images
batch_sizenum_samplessamples        Z/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/visualization/plot_image_gallery.py_extract_image_batchr      sR     399V"6
 	
 j*-KL[L#%&FM    z&keras.visualization.plot_image_galleryc           
      j   t         t        d      ||	rt        d      |||t        d      |	|	n|du }	|xs t        j                         }t        t        j                  |             dk(  rt        j                  |       d   nd}|xs1 t        t        j                  t        j                  |                  }|xs! t        t        j                  ||z              }||z  }t        | ||      } |dk(  rt        j                  | d	      } t        j                  ||||z  ||z  fd
dddd      \  }}|j                  dd       t!        |t"        j$                        r7t        |j                        dk(  r|dk(  rdnd}t#        j&                  ||      }||j)                  |d       t+               j-                  | |d      } t        j.                  |       } |dk(  r| j                  dddd      } |t        j.                  |      }|t        j.                  |      }t1        |      D ]  }t1        |      D ]
  }||z  |z   }t!        |t"        j$                        r|||f   n|}|j3                  | |   j5                  d             |j7                  dd       |j9                  d       g }|5|t        |      k  r'|j;                  d|j=                  ||   d              |5|t        |      k  r'|j;                  d|j=                  ||   d              |s|j?                  djA                  |      d          |/t        jB                  |dd|
|!       t        jD                          y|	r)t        jF                          t        jD                          yy)"a  Displays a gallery of images with optional labels and predictions.

    Args:
        images: A 4D tensor or NumPy array of images. Shape should be
           `(batch_size, height, width, channels)`.
        y_true: A 1D tensor or NumPy array of true labels (class indices).
           Defaults to `None`.
        y_pred: A 1D tensor or NumPy array of predicted labels (class indices).
           Defaults to `None`.
        label_map: A dictionary mapping class indices to class names.
            Required if `y_true` or `y_pred` are provided.
           Defaults to `None`.
        value_range: A tuple specifying the value range of the images
            (e.g., `(0, 255)` or `(0, 1)`). Defaults to `(0, 255)`.
        rows: The number of rows in the gallery. If `None`, it's calculated
            based on the number of images and `cols`. Defaults to `None`.
        cols: The number of columns in the gallery. If `None`, it's calculated
            based on the number of images and `rows`. Defaults to `None`.
        scale: A float controlling the size of the displayed images. The images
            are scaled by this factor. Defaults to `2`.
        path: The path to save the generated gallery image. If `None`, the
            image is displayed using `plt.show()`. Defaults to `None`.
        show: Whether to display the image using `plt.show()`. If `True`, the
            image is displayed. If `False`, the image is not displayed.
            Ignored if `path` is not `None`. Defaults to `True` if `path`
            is `None`, `False` otherwise.
        transparent:  A boolean, whether to save the figure with a transparent
            background. Defaults to `True`.
        dpi: The DPI (dots per inch) for saving the figure. Defaults to 60.
        legend_handles: A list of matplotlib `Patch` objects to use as legend
            handles. Defaults to `None`.
        data_format: The image data format `"channels_last"` or
            `"channels_first"`. Defaults to the Keras backend data format.

    Raises:
        ValueError: If both `path` and `show` are set to non-`None` values,
            if `images` is not a 4D tensor or array, or if `y_true` or `y_pred`
            are provided without a `label_map`.
        ImportError: if matplotlib is not installed.
    NzuThe `plot_image_gallery` function requires the `matplotlib` package. Please install it with `pip install matplotlib`.zEplot_gallery() expects either `path` to be set, or `show` to be true.zJIf `y_true` or `y_pred` are provided, a `label_map` must also be provided.r   r      channels_first)r         r   FtightTrowcol)nrowsncolsfigsizeframeonlayoutsqueezesharexsharey)wspacehspacezlower center)handleslocr      )r   original_rangetarget_ranger   r   uint8)xyoffzLabel: UnknownzPred: z     )fontsize)fname
pad_inchesbbox_inchestransparentdpi)$pltImportErrorr   r   image_data_formatr	   r   r
   intmathceilsqrtr   	transposesubplotssubplots_adjust
isinstancenpndarrayexpand_dimslegendr   _transform_value_rangeconvert_to_numpyrangeimshowastypemarginsaxisappendget	set_titlejoinsavefigcloseshow)r   y_truey_pred	label_maprowscolsvalue_rangescalepathrV   r8   r9   legend_handlesdata_formatr   r   figaxesexpand_axisr   r   indexcurrent_axistitle_partss                           r   plot_image_galleryrg   ,   s   r {H
 	

 D
 	

 	f0i6G
 	

 #4$$,D<!:!:!<K),SYYv->)?1)D6"1%!J83tyy:!678D53tyyt!345DJ!&*jAF''v|4 te|,	IC q+$

#DJJ1(<19a"~~dK0!

>~
>(*AAk B F !!&)F&&!!!Q1-%%f-%%f-T{ K; 	KC$J$E",T2::">S#XD  u 4 4W =>  1 *e$K!ec&k&9""immF5M9EFG !ec&k&9""Y]]6%=)DEF &&tyy'=&J'	KK, #	
 				
		 
r   )NNNNNr*   r   NNT<   NN)r>   numpyrE   	keras.srcr   r   keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   matplotlib.pyplotpyplotr:   r;   r   rg    r   r   <module>rp      s        -#
4 67 		
		
[ 8[?  
Cs   A AA