
import copy
from typing import Dict, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING
from ..detr import DetrConfig
from ..swin import SwinConfig


__A							={
    '''facebook/maskformer-swin-base-ade''': (
        '''https://huggingface.co/facebook/maskformer-swin-base-ade/blob/main/config.json'''
    )
    # See all MaskFormer models at https://huggingface.co/models?filter=maskformer
}

__A							=logging.get_logger(__name__)




class        _SCREAMING_SNAKE_CASE	( snake_case_		):
	lowerCAmelCase__   =					'maskformer'
	lowerCAmelCase__   =					{'hidden_size': 'mask_feature_size'}
	lowerCAmelCase__   =					['resnet', 'swin']
	lowerCAmelCase__   =					['detr']



	def __init__(       self		,    lowercase = 256		,    lowercase = 256		,    lowercase = 0.1		,    lowercase = False		,    lowercase = None		,    lowercase = None		,    lowercase = 0.0_2		,    lowercase = 1.0		,    lowercase = 1.0		,    lowercase = 1.0		,    lowercase = 2_0.0		,    lowercase = None		,    **lowercase		,    )							->			List[Any]:
					if backbone_config is None:
									# fall back to https://huggingface.co/microsoft/swin-base-patch4-window12-384-in22k
									lowerCamelCase_         =     SwinConfig(
									    image_size=384		,    in_channels=3		,    patch_size=4		,    embed_dim=128		,    depths=[2, 2, 18, 2]		,    num_heads=[4, 8, 16, 32]		,    window_size=12		,    drop_path_rate=0.3		,    out_features=["stage1", "stage2", "stage3", "stage4"]		,    )

					if isinstance(lowercase		,    lowercase					):
									lowerCamelCase_         =     backbone_config.pop("model_type"					)
									lowerCamelCase_         =     CONFIG_MAPPING[backbone_model_type]
									lowerCamelCase_         =     config_class.from_dict(lowercase					)

					# verify that the backbone is supported
					if backbone_config.model_type not in self.backbones_supported:
									logger.warning_once(
									    f'Backbone {backbone_config.model_type} is not a supported model and may not be compatible with MaskFormer. '
									    f'Supported model types: {",".join(self.backbones_supported					)}'					)

					if decoder_config is None:
									# fall back to https://huggingface.co/facebook/detr-resnet-50
									lowerCamelCase_         =     DetrConfig()
					else:
									# verify that the decoder is supported
									lowerCamelCase_         =     (
									    decoder_config.pop("model_type"					) if isinstance(lowercase		,    lowercase					) else decoder_config.model_type
									)
									if decoder_type not in self.decoders_supported:
													raise ValueError(
													    f'Transformer Decoder {decoder_type} not supported, please use one of'
													    f' {",".join(self.decoders_supported					)}'					)
									if isinstance(lowercase		,    lowercase					):
													lowerCamelCase_         =     CONFIG_MAPPING[decoder_type]
													lowerCamelCase_         =     config_class.from_dict(lowercase					)

					lowerCamelCase_         =     backbone_config
					lowerCamelCase_         =     decoder_config
					# main feature dimension for the model
					lowerCamelCase_         =     fpn_feature_size
					lowerCamelCase_         =     mask_feature_size
					# initializer
					lowerCamelCase_         =     init_std
					lowerCamelCase_         =     init_xavier_std
					# Hungarian matcher && loss
					lowerCamelCase_         =     cross_entropy_weight
					lowerCamelCase_         =     dice_weight
					lowerCamelCase_         =     mask_weight
					lowerCamelCase_         =     use_auxiliary_loss
					lowerCamelCase_         =     no_object_weight
					lowerCamelCase_         =     output_auxiliary_logits

					lowerCamelCase_         =     self.decoder_config.encoder_attention_heads
					lowerCamelCase_         =     self.decoder_config.num_hidden_layers
					super().__init__(**lowercase					)



	@classmethod
	def        SCREAMING_SNAKE_CASE_(       cls		,    lowercase		,    lowercase		,    **lowercase					)							->			Optional[Any]:
					return cls(
					    backbone_config=lowercase		,    decoder_config=lowercase		,    **lowercase		,    )



	def        SCREAMING_SNAKE_CASE_(       self					)							->			Dict[str, any]:
					lowerCamelCase_         =     copy.deepcopy(self.__dict__					)
					lowerCamelCase_         =     self.backbone_config.to_dict()
					lowerCamelCase_         =     self.decoder_config.to_dict()
					lowerCamelCase_         =     self.__class__.model_type
					return output




import numpy as np
import torch
import torch.nn as nn
from transformers import CLIPConfig, CLIPVisionModelWithProjection, PreTrainedModel

from ...utils import logging


__A							=logging.get_logger(__name__)




class        _SCREAMING_SNAKE_CASE	( snake_case_		):
	lowerCAmelCase__   =					CLIPConfig

	lowerCAmelCase__   =					['CLIPEncoderLayer']



	def __init__(       self		,    lowercase					)							->			int:
					super().__init__(lowercase					)

					lowerCamelCase_         =     CLIPVisionModelWithProjection(config.vision_config					)

					lowerCamelCase_         =     nn.Linear(config.vision_config.projection_dim		,    1					)
					lowerCamelCase_         =     nn.Linear(config.vision_config.projection_dim		,    1					)



	@torch.no_grad()
	def        SCREAMING_SNAKE_CASE_(       self		,    lowercase		,    lowercase		,    lowercase=0.5		,    lowercase=0.5					)							->			int:
					lowerCamelCase_         =     self.vision_model(lowercase					)[0]

					lowerCamelCase_         =     self.p_head(lowercase					)
					lowerCamelCase_         =     nsfw_detected.flatten()
					lowerCamelCase_         =     nsfw_detected > p_threshold
					lowerCamelCase_         =     nsfw_detected.tolist()

					if any(lowercase					):
									logger.warning(
									    "Potential NSFW content was detected in one or more images. A black image will be returned instead."
									    " Try again with a different prompt and/or seed."					)

					for idx, nsfw_detected_ in enumerate(lowercase					):
									if nsfw_detected_:
													lowerCamelCase_         =     np.zeros(images[idx].shape					)

					lowerCamelCase_         =     self.w_head(lowercase					)
					lowerCamelCase_         =     watermark_detected.flatten()
					lowerCamelCase_         =     watermark_detected > w_threshold
					lowerCamelCase_         =     watermark_detected.tolist()

					if any(lowercase					):
									logger.warning(
									    "Potential watermarked content was detected in one or more images. A black image will be returned instead."
									    " Try again with a different prompt and/or seed."					)

					for idx, watermark_detected_ in enumerate(lowercase					):
									if watermark_detected_:
													lowerCamelCase_         =     np.zeros(images[idx].shape					)

					return images, nsfw_detected, watermark_detected


