


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


snake_case__				:	Any					       =					logging.get_logger(__name__)

snake_case__				:	Optional[int]					       =					{
    'microsoft/resnet-50': 'https://huggingface.co/microsoft/resnet-50/blob/main/config.json',
}





class   SCREAMING_SNAKE_CASE_			(a__						,       a__						):




       '''simple docstring'''



       _a   							=							"resnet"
       _a   							=							["basic", "bottleneck"]



       def __init__(						self				:				Optional[Any]		,				__a				:				Optional[Any]=3		,				__a				:				Optional[Any]=64		,				__a				:				Any=[256, 512, 1_024, 2_048]		,				__a				:				Union[str, Any]=[3, 4, 6, 3]		,				__a				:				Any="bottleneck"		,				__a				:				Dict="relu"		,				__a				:				Dict=False		,				__a				:				Union[str, Any]=None		,				__a				:				List[Any]=None		,				**__a				:				Dict		,				)					->Optional[Any]:
          super().__init__(**__a							)
          if layer_type not in self.layer_types:
             raise ValueError(F'''layer_type={layer_type} is not one of {','.join(self.layer_types							)}'''							)
          lowerCamelCase_						:   Any		= num_channels
          lowerCamelCase_						:   str		= embedding_size
          lowerCamelCase_						:   Tuple		= hidden_sizes
          lowerCamelCase_						:   Any		= depths
          lowerCamelCase_						:   List[str]		= layer_type
          lowerCamelCase_						:   Tuple		= hidden_act
          lowerCamelCase_						:   Dict		= downsample_in_first_stage
          lowerCamelCase_						:   Union[str, Any]		= ["""stem"""] + [F'''stage{idx}''' for idx in range(1		,				len(__a							) + 1							)]
          lowerCamelCase_,	lowerCamelCase_						:   Optional[int]		= get_aligned_output_features_output_indices(
              out_features=__a		,				out_indices=__a		,				stage_names=self.stage_names							)







class   SCREAMING_SNAKE_CASE_			(a__						):




       '''simple docstring'''



       _a   							=							version.parse("1.11"						)



       @property
       def 				_lowerCAmelCase							(						self				:				Union[str, Any]							)					->Mapping[str, Mapping[int, str]]:
          return OrderedDict(
              [
                  ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
              ]							)




       @property
       def 				_lowerCAmelCase							(						self				:				List[str]							)					->float:
          return 1e-3









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

from ...utils import logging


snake_case__				:	int					       =					logging.get_logger(__name__)





class   SCREAMING_SNAKE_CASE_			(a__						):




       '''simple docstring'''



       _a   							=							CLIPConfig

       _a   							=							["CLIPEncoderLayer"]



       def __init__(						self				:				Tuple		,				__a				:				CLIPConfig							)					->Union[str, Any]:
          super().__init__(__a							)

          lowerCamelCase_						:   List[Any]		= CLIPVisionModelWithProjection(config.vision_config							)

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




       @torch.no_grad()
       def 				_lowerCAmelCase							(						self				:				Dict		,				__a				:				List[Any]		,				__a				:				Tuple		,				__a				:				int=0.5		,				__a				:				Optional[Any]=0.5							)					->Union[str, Any]:
          lowerCamelCase_						:   Dict		= self.vision_model(__a							)[0]

          lowerCamelCase_						:   str		= self.p_head(__a							)
          lowerCamelCase_						:   Union[str, Any]		= nsfw_detected.flatten()
          lowerCamelCase_						:   Tuple		= nsfw_detected > p_threshold
          lowerCamelCase_						:   Dict		= nsfw_detected.tolist()

          if any(__a							):
             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(__a							):
             if nsfw_detected_:
                lowerCamelCase_						:   int		= np.zeros(images[idx].shape							)

          lowerCamelCase_						:   Union[str, Any]		= self.w_head(__a							)
          lowerCamelCase_						:   List[str]		= watermark_detected.flatten()
          lowerCamelCase_						:   Dict		= watermark_detected > w_threshold
          lowerCamelCase_						:   Optional[int]		= watermark_detected.tolist()

          if any(__a							):
             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(__a							):
             if watermark_detected_:
                lowerCamelCase_						:   Tuple		= np.zeros(images[idx].shape							)

          return images, nsfw_detected, watermark_detected





