o
    2hK                     @   s   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	 zd dl
mZ W n ey3   dZY nw dd Zed								
						dddZdS )    N)backend)ops)keras_export)BaseImagePreprocessingLayerc                 C   s8   t t| dk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sample r   e/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/visualization/plot_image_gallery.py_extract_image_batch   s   
r   z&keras.visualization.plot_image_galleryr         T<   c              
   C   s  t du rtd|dur|	rtd|dus|dur"|du r"td|	dur(|	n|du }	|p1t }tt| dkrBt| d nd}|pOtt	
t	|}|pZtt	
|| }|| }t| ||} |dkrot| d	} t j|||| || fd
ddddd\}}|jddd t|tjrt|jdkr|dkrdnd}t||}|dur|j|dd t j| |dd} t| } |dkr| dddd} |durt|}|durt|}t|D ]v}t|D ]o}|| | }t|tjr|||f n|}|| | d |jddd |d g }|dur/|t|k r/|d||| d  |durI|t|k rI|d||| d  |rV|jd |dd  qq|durmt j!|dd|
|d! t "  dS |	rzt #  t "  dS dS )"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      r   FtightTrowcol)nrowsncolsfigsizeframeonlayoutsqueezesharexsharey)wspacehspacezlower center)handleslocr   )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pathrT   r6   r7   legend_handlesdata_formatr   r   figaxesexpand_axisr   r   indexcurrent_axistitle_partsr   r   r   plot_image_gallery,   s   9$






re   )NNNNNr   r   NNTr   NN)r<   numpyrC   	keras.srcr   r   keras.src.api_exportr   Qkeras.src.layers.preprocessing.image_preprocessing.base_image_preprocessing_layerr   matplotlib.pyplotpyplotr8   r9   r   re   r   r   r   r   <module>   s6    