
    ^j=                     
   d Z ddlm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mZ dd
lmZmZmZmZmZmZ ddlmZmZ ddlmZmZmZ de de!e"e e e f      fdZ# G d ded      Z$e G d de
             Z%dgZ&y)z!Image processor class for SegGPT.    )UnionN)
functional   )TorchvisionBackend)'SemanticSegmentationPostProcessorOutput)BatchFeature)group_images_by_shapereorder_images)IMAGENET_DEFAULT_MEANIMAGENET_DEFAULT_STDChannelDimension
ImageInputPILImageResamplingSizeDict)ImagesKwargsUnpack)
TensorTypeauto_docstringrequires_backends
num_labelsreturnc                     t        | dz        dz   }d|z  }dg}t        |       D ]F  }||dz  z  }||dz  z  |z  }||z  }d||z  z
  }d||z  z
  }	d||z  z
  }
|j                  ||	|
f       H |S )NgUUUUUU?      )r   r   r         )intrangeappend)r   basemargin
color_listlocation	num_seq_r	num_seq_g	num_seq_bRGBs              }/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/transformers/models/seggpt/image_processing_seggpt.pybuild_paletter+   (   s    ze$%)DD[F J*% 	%a'	a'D0	tO	)f$$)f$$)f$$1a)$	%     c                       e Zd ZU dZeed<   y)SegGptImageProcessorKwargsa  
    num_labels (`int`, *optional*):
        Number of classes in the segmentation task (excluding the background). If specified, a palette will be
        built, assuming that class_idx 0 is the background, to map the prompt mask from a plain segmentation map
        to a 3-channel RGB image. Not specifying this will result in the prompt mask being duplicated across the
        channel dimension when `do_convert_rgb` is `True`.
    r   N)__name__
__module____qualname____doc__r   __annotations__ r,   r*   r.   r.   <   s     Or,   r.   F)totalc                   4    e Zd ZeZej                  ZeZ	e
ZdddZdZdZdZdZdZdee   f fdZdedeeeeef      fd	Zd'd
ej2                  deeeeef      dz  dej2                  fdZe	 	 	 d(dedz  dedz  dedz  dee   def
 fd       Z	 	 d)dededz  dedz  dede de!e"z  dz  de#e!df   dz  dedz  defdZ$ded   dede%dddede&dede&ee&   z  dz  d e&ee&   z  dz  d!edz  ded   fd"Z'	 	 	 d*d#eeeef      dz  dedz  d$edd%fd&Z( xZ)S )+SegGptImageProcessori  )heightwidthTNkwargsc                 $    t        |   di | y )Nr4   )super__init__)selfr:   	__class__s     r*   r=   zSegGptImageProcessor.__init__V   s    "6"r,   r   r   c                     t        |      S )aB  Build a palette to map the prompt mask from a single channel to a 3-channel RGB.

        Args:
            num_labels (`int`):
                Number of classes in the segmentation task (excluding the background).

        Returns:
            `list[tuple[int, int, int]]`: Palette to map the prompt mask to RGB.
        )r+   )r>   r   s     r*   get_palettez SegGptImageProcessor.get_paletteY   s     Z((r,   maskpalettec                    |j                   dk(  r|j                  d      }|j                  \  }}|t        j                  d||ft        j
                        }t        j                  |      }|D ]  }||   }||k(  j                  t        j
                        }	t        j                  |t        j
                        j                  ddd      }
|t        j                  |	d      |
z  j                  t        j
                        z  } t        j                  |dd      j                  t        j
                        }|S t        j                  |t        j                  df   dd      }|S )a,  Converts a segmentation map to RGB format.

        Args:
            mask (`np.ndarray`):
                Segmentation map with shape `(height, width)` or `(1, height, width)` where pixel values
                represent the class index.
            palette (`list[tuple[int, int, int]]`, *optional*):
                Palette to use to convert the mask to RGB format. If unset, the mask is duplicated across
                the channel dimension.

        Returns:
            `np.ndarray`: The mask in RGB format with shape `(3, height, width)`.
        r   r   )dtyper   r   .)axis)ndimsqueezeshapenpzerosuint8uniqueastypearrayreshapeexpand_dimscliprepeatnewaxis)r>   rB   rC   r8   r9   rgb_maskclasses_in_mask	class_idx	rgb_value
class_mask	class_rgbs              r*   mask_to_rgbz SegGptImageProcessor.mask_to_rgbe   s'    99><<?D

xxFE 2"((CH iioO, Y	#I.	"i/77A
HHYbhh?GG1aP	R^^J:YFNNrxxXX	Y
 wwxC077AH  yybjj#o!6BHr,   imagesprompt_imagesprompt_masksc                 t    t        d |||fD              rt        d      ||ng }t        |   |||fi |S )a  
        prompt_images (`ImageInput`, *optional*):
            Prompt images to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255.
        prompt_masks (`ImageInput`, *optional*):
            Prompt masks to preprocess. Can be in the format of segmentation maps (no channels) or RGB images.
            If in the format of RGB images, `do_convert_rgb` should be set to `False`. If in the format of
            segmentation maps, specifying `num_labels` is recommended to build a palette to map the prompt mask
            from a single channel to a 3-channel RGB. If `num_labels` is not specified, the prompt mask will be
            duplicated across the channel dimension.
        c              3   $   K   | ]  }|d u  
 y wNr4   ).0vs     r*   	<genexpr>z2SegGptImageProcessor.preprocess.<locals>.<genexpr>   s     HQqDyHs   zFAt least one of images, prompt_images, prompt_masks must be specified.)all
ValueErrorr<   
preprocess)r>   r\   r]   r^   r:   _images_inputr?   s         r*   rg   zSegGptImageProcessor.preprocess   sN    $ H6=,"GHHeff #)"4"w!-WPVWWr,   do_convert_rgbinput_data_formatreturn_tensorsdeviceztorch.devicec	                 $   i }
t        |t              xr t        |      dk(   }|r+| j                  |d||      } | j                  |fi |	|
d<   |+| j                  |d||      } | j                  |fi |	|
d<   ||r| j                  |ddt
        j                  |      }|| j                  |      nd }g }|D ]u  }|j                  d      j                         }| j                  ||      }|j                  t        j                  |j                  t        j                                      w |}n| j                  |d	d||      }t#        |	      }t$        j&                  |d
<    | j                  |fi ||
d<   t)        |
|      S )Nr   F)r\   ri   rj   rl   pixel_valuesprompt_pixel_valuesr   )r\   expected_ndimsri   rj   rl   )rC   r   resampler^   )datatensor_type)
isinstancelistlen_prepare_image_like_inputs_preprocessr   FIRSTrA   rH   numpyr[   r   torch
from_numpyrN   rJ   float32dictr   NEARESTr   )r>   r\   r]   r^   ri   rj   rk   rl   r   r:   rr   _images_providedprepared_imagesprepared_prompt_imagesprepared_masksrC   	convertedmask_tensormask_nprgb_npmasks_kwargss                        r*   _preprocess_image_like_inputsz2SegGptImageProcessor._preprocess_image_like_inputs   s     !+64 8 MS[A=MN"==eGXag > O $44#3#3O#Nv#ND  $%)%D%D$UN_hn &E &" +;$*:*:;Q*\U[*\D&' #!%!@!@'#$#(&6&<&<! "A " ;E:P$**:6VZ	#1 RK)11!4::<G!--gw-GF$$U%5%5fmmBJJ6O%PQR "+ "&!@!@'#$#(&7! "A "  <L'9'A'AL$#34#3#3N#Sl#SD >BBr,   ztorch.Tensor	do_resizesizerq   z7PILImageResampling | tvF.InterpolationMode | int | None
do_rescalerescale_factordo_normalize
image_mean	image_stddisable_groupingc           	      D   t        ||
      \  }}i }|j                         D ]  \  }}|r| j                  |||      }|||<   ! t        ||      }t        ||
      \  }}i }|j                         D ]   \  }}| j	                  ||||||	      }|||<   " t        ||      S )N)r   )r	   itemsresizer
   rescale_and_normalize)r>   r\   r   r   rq   r   r   r   r   r   r   r:   grouped_imagesgrouped_images_indexresized_images_groupedrI   stacked_imagesresized_imagesprocessed_images_groupeds                      r*   rx   z SegGptImageProcessor._preprocess   s     0EV^n/o,,!#%3%9%9%; 	;!E>!%^T8!L,:"5)	; ((>@TU/D^fv/w,,#% %3%9%9%; 	=!E>!77
NL*V_N /=$U+		= 68LMMr,   target_sizesreturn_segmentation_scoreszBlist[torch.Tensor] | list[SemanticSegmentationPostProcessorOutput]c                    t        | dg       |j                  }|"t        |      t        |      k7  rt        d      |dddd|j                  d   dz  dddf   }t        j                  | j                        j                  |j                        }t        j                  | j                        j                  |j                        }|j                  dddd      |z  |z   }|j                  dddd      }t        j                  |dz  dd      }g }d}	|| j                  |      nd}
|
ft        j                  |
      j                  |j                  t
        j                  	      }	|j                  \  }}}}|	j                  dd|dz   |      }	t!        |      D ]  \  }}|At
        j"                  j$                  j'                  |j)                  d      ||   d
      d   }||j                  \  }}}|j                  ddd      j                  ||d|      }||	z
  }t        j*                  |d      }t        j,                  |d      }|j/                  d      }|j                  ddd       }n"|j1                  d      j3                         }d}|j5                  t7        ||d              |s|D cg c]  }|j8                   }}|S c c}w )a  
        Converts the output of [`SegGptImageSegmentationOutput`] into segmentation maps. Only supports PyTorch.

        Args:
            outputs ([`SegGptImageSegmentationOutput`]):
                Raw outputs of the model.
            target_sizes (`list[tuple[int, int]]`, *optional*):
                List of length `batch_size`, where each item corresponds to the requested final size `(height, width)`
                of each prediction. If left to `None`, predictions will not be resized.
            num_labels (`int`, *optional*):
                Number of classes in the segmentation task (excluding the background). If specified, a palette will be
                built to map prediction masks from RGB values back to class indices. Should match the value used during
                preprocessing.
            return_segmentation_scores (`bool`, *optional*, defaults to `False`):
                Whether to return segmentation scores alongside the segmentation map. When `True`, each element of
                the returned list is a [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation`
                (class IDs, shape `(height, width)`) and `segmentation_scores` (shape `(num_labels+1, height, width)`
                of negative squared L2 distances to each palette color, or `None` when `num_labels` is not provided).

        Returns:
            `list[torch.Tensor]` or `list[SemanticSegmentationPostProcessorOutput]`: When
            `return_segmentation_scores=False` (default), a list of length `batch_size` where each item is a
            segmentation map of shape `(height, width)` with class IDs. When `return_segmentation_scores=True`,
            a list of [`SemanticSegmentationPostProcessorOutput`] with fields `segmentation` (class IDs, shape
            `(height, width)`) and `segmentation_scores` (shape `(num_labels+1, height, width)`). In both cases,
            `(height, width)` corresponds to the target size (if `target_sizes` is specified).
        r{   NzTMake sure that you pass in as many target sizes as the batch dimension of the logitsr   r   r   r   r   )rl   rE   nearest)r   mode)dim)segmentationsegmentation_scores)rr   )r   
pred_masksrv   rf   rI   r{   tensorr   torl   r   permuterR   rA   floatview	enumeratennr   interpolate	unsqueezepowsumargminmeanr   r   r   r   )r>   outputsr   r   r   masksstdr   semantic_segmentationpalette_tensorrC   _num_channelsidxrB   channelsr8   r9   distpredr   items                         r*   "post_process_semantic_segmentationz7SegGptImageProcessor.post_process_semantic_segmentation   s   F 	$	* ""#E
c,6G(Gstt aEKKNa/1145 ll4>>*--ell;||DOO,//=aAq)C/$6aAq)

53;3/ "2<2H$"":.d"\\'255U\\QVQ\Q\5]N$)KK!A|Q+00AzA~|TN"5) 	IC'xx**66NN1%%c*" 7  	 %*.**'&%||Aq!,11&%HMn,yyq)yy2.{{r{*'+||Aq!'<&<#yyQy'++-&*#!((7*.GZ[)	4 *CX$Y4T%6%6$Y!$Y$$ %Zs   Kra   )NNN)NN)NNF)*r/   r0   r1   r.   valid_kwargsr   BICUBICrq   r   r   r   r   r   r   r   r   ri   r   r   r=   r   ru   tuplerA   rJ   ndarrayr[   r   r   r   rg   boolr   strr   r   r   r   r   rx   r   __classcell__)r?   s   @r*   r7   r7   H   s   -L!))H&J$IC(DIJLNJ#(B!C #
)c 
)d5c33G.H 
)

 T%S#:N5ORV5V bdblbl B  %)+/*.	XT!X "D(X !4'	X
 34X 
X X@ 59!%>C>C "D(>C !4'	>C
 >C ,>C j(4/>C c>)*T1>C $J>C 
>C@N^$N N 	N
 LN N N N DK'$.N 4;&-N +N 
n	NF 6:!%+0\% 5c?+d2\% $J	\%
 %)\% 
N\%r,   r7   )'r2   typingr   rz   rJ   r{   torchvision.transforms.v2r   tvFimage_processing_backendsr   image_processing_outputsr   image_processing_utilsr   image_transformsr	   r
   image_utilsr   r   r   r   r   r   processing_utilsr   r   utilsr   r   r   r   ru   r   r+   r.   r7   __all__r4   r,   r*   <module>r      s    (    7 ; O 2 E  5 B B
c d5c3+?&@ (	U 	 R%- R% R%j "
"r,   