






'''simple docstring'''


import pickle

import numpy as np
from matplotlib import pyplot as plt
class    _SCREAMING_SNAKE_CASE							:






			def __init__(   self							:						List[Any] ,      lowercase							:						str ,      lowercase							:						Optional[Any] ,      lowercase							:						List[str] ,      lowercase							:						List[str] ,      lowercase							:						Any ,      lowercase							:						List[Any]=0.2 ,      lowercase							:						Optional[int]=0.2			)							->    Optional[int]:

									'''simple docstring'''







									UpperCamelCase__         = bp_numa
									UpperCamelCase__         = bp_numa
									UpperCamelCase__         = bp_numa
									UpperCamelCase__         = conva_get[:2]
									UpperCamelCase__         = conva_get[2]
									UpperCamelCase__         = size_pa
									UpperCamelCase__         = rate_w
									UpperCamelCase__         = rate_t
									UpperCamelCase__         = [
									    np.mat(-1 * np.random.rand(self.conva[0] ,      self.conva[0]			) + 0.5			)
									    for i in range(self.conva[1]			)
									]
									UpperCamelCase__         = np.mat(-1 * np.random.rand(self.num_bpa ,      self.num_bpa			) + 0.5			)
									UpperCamelCase__         = np.mat(-1 * np.random.rand(self.num_bpa ,      self.num_bpa			) + 0.5			)
									UpperCamelCase__         = -2 * np.random.rand(self.conva[1]			) + 1
									UpperCamelCase__         = -2 * np.random.rand(self.num_bpa			) + 1
									UpperCamelCase__         = -2 * np.random.rand(self.num_bpa			) + 1






			def 				A      (   self							:						Dict ,      lowercase							:						Union[str, Any]			)							->    int:

									'''simple docstring'''







									UpperCamelCase__         = {
									    """num_bp1""": self.num_bpa,
									    """num_bp2""": self.num_bpa,
									    """num_bp3""": self.num_bpa,
									    """conv1""": self.conva,
									    """step_conv1""": self.step_conva,
									    """size_pooling1""": self.size_poolinga,
									    """rate_weight""": self.rate_weight,
									    """rate_thre""": self.rate_thre,
									    """w_conv1""": self.w_conva,
									    """wkj""": self.wkj,
									    """vji""": self.vji,
									    """thre_conv1""": self.thre_conva,
									    """thre_bp2""": self.thre_bpa,
									    """thre_bp3""": self.thre_bpa,
									}
									with open(lowercase ,      """wb"""			) as f:
															pickle.dump(lowercase ,      lowercase			)

									print(f"Model saved： {save_path}"			)






			@classmethod
			def 				A      (   cls							:						str ,      lowercase							:						Tuple			)							->    int:

									'''simple docstring'''







									with open(lowercase ,      """rb"""			) as f:
															UpperCamelCase__         = pickle.load(lowercase			)  # noqa: S301

									UpperCamelCase__         = model_dic.get("""conv1"""			)
									conv_get.append(model_dic.get("""step_conv1"""			)			)
									UpperCamelCase__         = model_dic.get("""size_pooling1"""			)
									UpperCamelCase__         = model_dic.get("""num_bp1"""			)
									UpperCamelCase__         = model_dic.get("""num_bp2"""			)
									UpperCamelCase__         = model_dic.get("""num_bp3"""			)
									UpperCamelCase__         = model_dic.get("""rate_weight"""			)
									UpperCamelCase__         = model_dic.get("""rate_thre"""			)
									# create model instance
									UpperCamelCase__         = CNN(lowercase ,      lowercase ,      lowercase ,      lowercase ,      lowercase ,      lowercase ,      lowercase			)
									# modify model parameter
									UpperCamelCase__         = model_dic.get("""w_conv1"""			)
									UpperCamelCase__         = model_dic.get("""wkj"""			)
									UpperCamelCase__         = model_dic.get("""vji"""			)
									UpperCamelCase__         = model_dic.get("""thre_conv1"""			)
									UpperCamelCase__         = model_dic.get("""thre_bp2"""			)
									UpperCamelCase__         = model_dic.get("""thre_bp3"""			)
									return conv_ins






			def 				A      (   self							:						str ,      lowercase							:						List[str]			)							->    Optional[int]:

									'''simple docstring'''







									return 1 / (1 + np.exp(-1 * x			))






			def 				A      (   self							:						Optional[int] ,      lowercase							:						Any			)							->    Any:

									'''simple docstring'''







									return round(lowercase ,      3			)






			def 				A      (   self							:						Dict ,      lowercase							:						List[str] ,      lowercase							:						int ,      lowercase							:						Optional[Any] ,      lowercase							:						Optional[int] ,      lowercase							:						Any			)							->    Any:

									'''simple docstring'''







									UpperCamelCase__         = convs[0]
									UpperCamelCase__         = convs[1]
									UpperCamelCase__         = np.shape(lowercase			)[0]
									# get the data slice of original image data, data_focus
									UpperCamelCase__         = []
									for i_focus in range(0 ,      size_data - size_conv + 1 ,      lowercase			):
															for j_focus in range(0 ,      size_data - size_conv + 1 ,      lowercase			):
																					UpperCamelCase__         = data[
																					    i_focus : i_focus + size_conv, j_focus : j_focus + size_conv
																					]
																					data_focus.append(lowercase			)
        # calculate the feature map of every single kernel, and saved as list of matrix
									UpperCamelCase__         = []
									UpperCamelCase__         = int((size_data - size_conv) / conv_step + 1			)
									for i_map in range(lowercase			):
															UpperCamelCase__         = []
															for i_focus in range(len(lowercase			)			):
																					UpperCamelCase__         = (
																					    np.sum(np.multiply(data_focus[i_focus] ,      w_convs[i_map]			)			)
																					    - thre_convs[i_map]
																					)
																					featuremap.append(self.sig(lowercase			)			)
															UpperCamelCase__         = np.asmatrix(lowercase			).reshape(
															    lowercase ,      lowercase			)
															data_featuremap.append(lowercase			)

									# expanding the data slice to One dimenssion
									UpperCamelCase__         = []
									for each_focus in data_focus:
															focusa_list.extend(self.Expand_Mat(lowercase			)			)
									UpperCamelCase__         = np.asarray(lowercase			)
									return focus_list, data_featuremap






			def 				A      (   self							:						List[str] ,      lowercase							:						Any ,      lowercase							:						Optional[Any] ,      lowercase							:						Dict="average_pool"			)							->    Optional[int]:

									'''simple docstring'''







									UpperCamelCase__         = len(featuremaps[0]			)
									UpperCamelCase__         = int(size_map / size_pooling			)
									UpperCamelCase__         = []
									for i_map in range(len(lowercase			)			):
															UpperCamelCase__         = featuremaps[i_map]
															UpperCamelCase__         = []
															for i_focus in range(0 ,      lowercase ,      lowercase			):
																					for j_focus in range(0 ,      lowercase ,      lowercase			):
																											UpperCamelCase__         = feature_map[
																											    i_focus : i_focus + size_pooling,
																											    j_focus : j_focus + size_pooling,
																											]
																											if pooling_type == "average_pool":
																																	# average pooling
																																	map_pooled.append(np.average(lowercase			)			)
																											elif pooling_type == "max_pooling":
																																	# max pooling
																																	map_pooled.append(np.max(lowercase			)			)
															UpperCamelCase__         = np.asmatrix(lowercase			).reshape(lowercase ,      lowercase			)
															featuremap_pooled.append(lowercase			)
									return featuremap_pooled






			def 				A      (   self							:						Any ,      lowercase							:						Any			)							->    Optional[int]:

									'''simple docstring'''







									UpperCamelCase__         = []
									for i in range(len(lowercase			)			):
															UpperCamelCase__         = np.shape(data[i]			)
															UpperCamelCase__         = data[i].reshape(1 ,      shapes[0] * shapes[1]			)
															UpperCamelCase__         = data_listed.getA().tolist()[0]
															data_expanded.extend(lowercase			)
									UpperCamelCase__         = np.asarray(lowercase			)
									return data_expanded






			def 				A      (   self							:						Any ,      lowercase							:						Optional[Any]			)							->    List[str]:

									'''simple docstring'''







									UpperCamelCase__         = np.asarray(lowercase			)
									UpperCamelCase__         = np.shape(lowercase			)
									UpperCamelCase__         = data_mat.reshape(1 ,      shapes[0] * shapes[1]			)
									return data_expanded






			def 				A      (   self							:						List[Any] ,      lowercase							:						Optional[Any] ,      lowercase							:						List[str] ,      lowercase							:						List[str] ,      lowercase							:						Any ,      lowercase							:						Dict			)							->    Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__         = []
									UpperCamelCase__         = 0
									for i_map in range(lowercase			):
															UpperCamelCase__         = np.ones((size_map, size_map)			)
															for i in range(0 ,      lowercase ,      lowercase			):
																					for j in range(0 ,      lowercase ,      lowercase			):
																											UpperCamelCase__         = pd_pool[
																											    i_pool
																											]
																											UpperCamelCase__         = i_pool + 1
															UpperCamelCase__         = np.multiply(
															    lowercase ,      np.multiply(out_map[i_map] ,      (1 - out_map[i_map])			)			)
															pd_all.append(lowercase			)
									return pd_all






			def 				A      (   self							:						Any ,      lowercase							:						List[Any] ,      lowercase							:						Optional[int] ,      lowercase							:						Tuple ,      lowercase							:						str ,      lowercase							:						List[str] ,      lowercase							:						Optional[int]=bool			)							->    Any:

									'''simple docstring'''







									print("""----------------------Start Training-------------------------"""			)
									print((""" - - Shape: Train_Data  """, np.shape(lowercase			))			)
									print((""" - - Shape: Teach_Data  """, np.shape(lowercase			))			)
									UpperCamelCase__         = 0
									UpperCamelCase__         = []
									UpperCamelCase__         = 1_0_0_0_0
									while rp < n_repeat and mse >= error_accuracy:
															UpperCamelCase__         = 0
															print(f"-------------Learning Time {rp}--------------"			)
															for p in range(len(lowercase			)			):
																					# print('------------Learning Image: %d--------------'%p)
																					UpperCamelCase__         = np.asmatrix(datas_train[p]			)
																					UpperCamelCase__         = np.asarray(datas_teach[p]			)
																					UpperCamelCase__    ,   UpperCamelCase__         = self.convolute(
																					    lowercase ,      self.conva ,      self.w_conva ,      self.thre_conva ,      conv_step=self.step_conva ,      )
																					UpperCamelCase__         = self.pooling(lowercase ,      self.size_poolinga			)
																					UpperCamelCase__         = np.shape(lowercase			)
																					UpperCamelCase__         = self._expand(lowercase			)
																					UpperCamelCase__         = data_bp_input

																					UpperCamelCase__         = np.dot(lowercase ,      self.vji.T			) - self.thre_bpa
																					UpperCamelCase__         = self.sig(lowercase			)
																					UpperCamelCase__         = np.dot(lowercase ,      self.wkj.T			) - self.thre_bpa
																					UpperCamelCase__         = self.sig(lowercase			)

																					# --------------Model Leaning ------------------------
																					# calculate error and gradient---------------
																					UpperCamelCase__         = np.multiply(
																					    (data_teach - bp_outa) ,      np.multiply(lowercase ,      (1 - bp_outa)			)			)
																					UpperCamelCase__         = np.multiply(
																					    np.dot(lowercase ,      self.wkj			) ,      np.multiply(lowercase ,      (1 - bp_outa)			)			)
																					UpperCamelCase__         = np.dot(lowercase ,      self.vji			)

																					UpperCamelCase__         = pd_i_all / (self.size_poolinga * self.size_poolinga)
																					UpperCamelCase__         = pd_conva_pooled.T.getA().tolist()
																					UpperCamelCase__         = self._calculate_gradient_from_pool(
																					    lowercase ,      lowercase ,      shape_featuremapa[0] ,      shape_featuremapa[1] ,      self.size_poolinga ,      )
																					# weight and threshold learning process---------
																					# convolution layer
																					for k_conv in range(self.conva[1]			):
																											UpperCamelCase__         = self._expand_mat(pd_conva_all[k_conv]			)
																											UpperCamelCase__         = self.rate_weight * np.dot(lowercase ,      lowercase			)

																											UpperCamelCase__         = self.w_conva[k_conv] + delta_w.reshape(
																											    (self.conva[0], self.conva[0])			)

																											UpperCamelCase__         = (
																											    self.thre_conva[k_conv]
																											    - np.sum(pd_conva_all[k_conv]			) * self.rate_thre
																											)
																					# all connected layer
																					UpperCamelCase__         = self.wkj + pd_k_all.T * bp_outa * self.rate_weight
																					UpperCamelCase__         = self.vji + pd_j_all.T * bp_outa * self.rate_weight
																					UpperCamelCase__         = self.thre_bpa - pd_k_all * self.rate_thre
																					UpperCamelCase__         = self.thre_bpa - pd_j_all * self.rate_thre
																					# calculate the sum error of all single image
																					UpperCamelCase__         = np.sum(abs(data_teach - bp_outa			)			)
																					error_count += errors
																					# print('   ----Teach      ',data_teach)
																					# print('   ----BP_output  ',bp_out3)
															UpperCamelCase__         = rp + 1
															UpperCamelCase__         = error_count / patterns
															all_mse.append(lowercase			)

									def draw_error():
															UpperCamelCase__         = [error_accuracy for i in range(int(n_repeat * 1.2			)			)]
															plt.plot(lowercase ,      """+-"""			)
															plt.plot(lowercase ,      """r--"""			)
															plt.xlabel("""Learning Times"""			)
															plt.ylabel("""All_mse"""			)
															plt.grid(lowercase ,      alpha=0.5			)
															plt.show()

									print("""------------------Training Complished---------------------"""			)
									print((""" - - Training epoch: """, rp, f"     - - Mse: {mse:.6f}")			)
									if draw_e:
															draw_error()
									return mse






			def 				A      (   self							:						Optional[Any] ,      lowercase							:						Dict			)							->    Tuple:

									'''simple docstring'''







									UpperCamelCase__         = []
									print("""-------------------Start Testing-------------------------"""			)
									print((""" - - Shape: Test_Data  """, np.shape(lowercase			))			)
									for p in range(len(lowercase			)			):
															UpperCamelCase__         = np.asmatrix(datas_test[p]			)
															UpperCamelCase__    ,   UpperCamelCase__         = self.convolute(
															    lowercase ,      self.conva ,      self.w_conva ,      self.thre_conva ,      conv_step=self.step_conva ,      )
															UpperCamelCase__         = self.pooling(lowercase ,      self.size_poolinga			)
															UpperCamelCase__         = self._expand(lowercase			)

															UpperCamelCase__         = data_bp_input
															UpperCamelCase__         = bp_outa * self.vji.T - self.thre_bpa
															UpperCamelCase__         = self.sig(lowercase			)
															UpperCamelCase__         = bp_outa * self.wkj.T - self.thre_bpa
															UpperCamelCase__         = self.sig(lowercase			)
															produce_out.extend(bp_outa.getA().tolist()			)
									UpperCamelCase__         = [list(map(self.do_round ,      lowercase			)			) for each in produce_out]
									return np.asarray(lowercase			)







			def 				A      (   self							:						Union[str, Any] ,      lowercase							:						int			)							->    Union[str, Any]:

									'''simple docstring'''







									UpperCamelCase__         = np.asmatrix(lowercase			)
									UpperCamelCase__    ,   UpperCamelCase__         = self.convolute(
									    lowercase ,      self.conva ,      self.w_conva ,      self.thre_conva ,      conv_step=self.step_conva ,      )
									UpperCamelCase__         = self.pooling(lowercase ,      self.size_poolinga			)

									return data_conveda, data_pooleda


if __name__ == "__main__":
					pass








'''simple docstring'''


import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


lowerCamelCase_				: str    	=							logging.get_logger(__name__)

lowerCamelCase_				: Optional[int]    	=							{
    '''SenseTime/deformable-detr''': '''https://huggingface.co/sensetime/deformable-detr/resolve/main/config.json''',
    # See all Deformable DETR models at https://huggingface.co/models?filter=deformable-detr
}
class    _SCREAMING_SNAKE_CASE							(		SCREAMING_SNAKE_CASE	):


			'''simple docstring'''





			__a							:							Tuple					     =					"deformable_detr"
			__a							:							str					     =					{
			    "hidden_size": "d_model",
			    "num_attention_heads": "encoder_attention_heads",
			}






			def __init__(   self							:						List[Any] ,      lowercase							:						List[Any]=True ,      lowercase							:						Tuple=None ,      lowercase							:						Tuple=3 ,      lowercase							:						List[str]=3_0_0 ,      lowercase							:						List[Any]=1_0_2_4 ,      lowercase							:						List[Any]=6 ,      lowercase							:						Tuple=1_0_2_4 ,      lowercase							:						Union[str, Any]=8 ,      lowercase							:						Optional[Any]=6 ,      lowercase							:						Tuple=1_0_2_4 ,      lowercase							:						Any=8 ,      lowercase							:						List[str]=0.0 ,      lowercase							:						Any=True ,      lowercase							:						Union[str, Any]="relu" ,      lowercase							:						Dict=2_5_6 ,      lowercase							:						Optional[int]=0.1 ,      lowercase							:						Optional[int]=0.0 ,      lowercase							:						List[str]=0.0 ,      lowercase							:						Any=0.0_2 ,      lowercase							:						Optional[int]=1.0 ,      lowercase							:						Tuple=True ,      lowercase							:						Optional[int]=False ,      lowercase							:						Any="sine" ,      lowercase							:						List[str]="resnet50" ,      lowercase							:						List[str]=True ,      lowercase							:						Optional[int]=False ,      lowercase							:						int=4 ,      lowercase							:						str=4 ,      lowercase							:						Dict=4 ,      lowercase							:						int=False ,      lowercase							:						List[Any]=3_0_0 ,      lowercase							:						List[Any]=False ,      lowercase							:						Dict=1 ,      lowercase							:						int=5 ,      lowercase							:						List[Any]=2 ,      lowercase							:						List[str]=1 ,      lowercase							:						Tuple=1 ,      lowercase							:						Dict=5 ,      lowercase							:						Dict=2 ,      lowercase							:						Dict=0.1 ,      lowercase							:						List[str]=0.2_5 ,      lowercase							:						Tuple=False ,      **lowercase							:						str ,      )							->    str:

									'''simple docstring'''







									if backbone_config is not None and use_timm_backbone:
															raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""			)

									if not use_timm_backbone:
															if backbone_config is None:
																					logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""			)
																					UpperCamelCase__         = CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]			)
															elif isinstance(lowercase ,      lowercase			):
																					UpperCamelCase__         = backbone_config.get("""model_type"""			)
																					UpperCamelCase__         = CONFIG_MAPPING[backbone_model_type]
																					UpperCamelCase__         = config_class.from_dict(lowercase			)
									UpperCamelCase__         = use_timm_backbone
									UpperCamelCase__         = backbone_config
									UpperCamelCase__         = num_channels
									UpperCamelCase__         = num_queries
									UpperCamelCase__         = max_position_embeddings
									UpperCamelCase__         = d_model
									UpperCamelCase__         = encoder_ffn_dim
									UpperCamelCase__         = encoder_layers
									UpperCamelCase__         = encoder_attention_heads
									UpperCamelCase__         = decoder_ffn_dim
									UpperCamelCase__         = decoder_layers
									UpperCamelCase__         = decoder_attention_heads
									UpperCamelCase__         = dropout
									UpperCamelCase__         = attention_dropout
									UpperCamelCase__         = activation_dropout
									UpperCamelCase__         = activation_function
									UpperCamelCase__         = init_std
									UpperCamelCase__         = init_xavier_std
									UpperCamelCase__         = encoder_layerdrop
									UpperCamelCase__         = auxiliary_loss
									UpperCamelCase__         = position_embedding_type
									UpperCamelCase__         = backbone
									UpperCamelCase__         = use_pretrained_backbone
									UpperCamelCase__         = dilation
									# deformable attributes
									UpperCamelCase__         = num_feature_levels
									UpperCamelCase__         = encoder_n_points
									UpperCamelCase__         = decoder_n_points
									UpperCamelCase__         = two_stage
									UpperCamelCase__         = two_stage_num_proposals
									UpperCamelCase__         = with_box_refine
									if two_stage is True and with_box_refine is False:
															raise ValueError("""If two_stage is True, with_box_refine must be True."""			)
									# Hungarian matcher
									UpperCamelCase__         = class_cost
									UpperCamelCase__         = bbox_cost
									UpperCamelCase__         = giou_cost
									# Loss coefficients
									UpperCamelCase__         = mask_loss_coefficient
									UpperCamelCase__         = dice_loss_coefficient
									UpperCamelCase__         = bbox_loss_coefficient
									UpperCamelCase__         = giou_loss_coefficient
									UpperCamelCase__         = eos_coefficient
									UpperCamelCase__         = focal_alpha
									UpperCamelCase__         = disable_custom_kernels
									super().__init__(is_encoder_decoder=lowercase ,      **lowercase			)






			@property
			def 				A      (   self							:						str			)							->    int:

									'''simple docstring'''







									return self.encoder_attention_heads






			@property
			def 				A      (   self							:						Tuple			)							->    int:

									'''simple docstring'''







									return self.d_model







			def 				A      (   self							:						Dict			)							->    Optional[Any]:

									'''simple docstring'''







									UpperCamelCase__         = copy.deepcopy(self.__dict__			)
									if self.backbone_config is not None:
															UpperCamelCase__         = self.backbone_config.to_dict()
									UpperCamelCase__         = self.__class__.model_type
									return output
