
    2Vh                     ~    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  ed      	 	 	 	 	 	 	 	 d	d       Zy)
    N)backend)ops)keras_export)draw_segmentation_masks)plot_image_galleryz2keras.visualization.plot_segmentation_mask_galleryc
                 d   |	xs t        j                         }	t        j                  |       }t	        |      dk7  rt        d|       |	dk(  rt        j                  | d      } t	        |      dk(  r|d   nd}|}d}||dz  }||dz  }t        j                  |       }t        j                  t        |||||      }|:|	dk(  rt        j                  |d      }t        j                  |d      } |||      }|:|	dk(  rt        j                  |d      }t        j                  |d      } |||      }g }t        |      D ]C  }|j                  ||          ||j                  |          |0|j                  |          E t        j                  |d	      }t!        |f|||d
|
S )a  Plots a gallery of images with corresponding segmentation masks.

    Args:
        images: A 4D tensor or NumPy array of images. Shape should be
            `(batch_size, height, width, channels)`.
        num_classes: The number of segmentation classes.  Class indices should
            start from `1`.  Class `0` will be treated as background and
            ignored if `ignore_index` is not 0.
        value_range: A tuple specifying the value range of the images
            (e.g., `(0, 255)` or `(0, 1)`). Defaults to `(0, 255)`.
        y_true: A 3D/4D tensor or NumPy array representing the ground truth
            segmentation masks. Shape should be `(batch_size, height, width)` or
            `(batch_size, height, width, 1)`. Defaults to `None`.
        y_pred: A 3D/4D tensor or NumPy array representing the predicted
            segmentation masks.  Shape should be the same as `y_true`.
            Defaults to `None`.
        color_mapping: A dictionary mapping class indices to RGB colors.
            If `None`, a default color palette is used. Class indices start
            from `1`. Defaults to `None`.
        blend: Whether to blend the masks with the input image using the
            `alpha` value. If `False`, the masks are drawn directly on the
            images without blending. Defaults to `True`.
        alpha: The opacity of the segmentation masks (a float between 0 and 1).
            Defaults to `0.8`.
        ignore_index: The class index to ignore when drawing masks.
            Defaults to `-1`.
        data_format: The image data format `"channels_last"` or
            `"channels_first"`. Defaults to the Keras backend data format.
        kwargs: Additional keyword arguments to be passed to
            `keras.visualization.plot_image_gallery`.

    Returns:
        The output of `keras.visualization.plot_image_gallery`.

    Raises:
        ValueError: If `images` is not a 4D tensor/array.
       z;`images` must be batched 4D tensor. Received: images.shape=channels_first)r            r   r   )num_classescolor_mappingalphaignore_indexblendint32)axis)value_rangerowscols)r   image_data_formatr   shapelen
ValueError	transposeconvert_to_numpy	functoolspartialr   castrangeappendnpstackr   )imagesr   r   y_truey_predr   r   r   r   data_formatkwargsimage_shape
batch_sizer   r   	images_npdraw_masks_fntrue_masks_drawnpredicted_masks_drawnimages_with_masksigallery_imagess                         f/home/dcms/DCMS/lib/python3.12/site-packages/keras/src/visualization/plot_segmentation_mask_gallery.pyplot_segmentation_mask_galleryr4      s   f <!:!:!<K))F#K
;1&&1]4
 	
 &&v|4#&{#3q#8QaJDD		$$V,I%%#!M **]]6<8F&'*(F;**]]6<8F&'* -i @: ?  1.$$%5a%89$$%:1%=>? XX/a8N$/dIO     ))r      NNNTg?N)r   numpyr#   	keras.srcr   r   keras.src.api_exportr   /keras.src.visualization.draw_segmentation_masksr   *keras.src.visualization.plot_image_galleryr   r4    r5   r3   <module>r>      sV        - J BC 

j Djr5   