o
    2hQ                     @   sT   d dl Zd dlmZ d dlmZ d dlmZ d dlm	Z	 edG dd deZ
dS )	    N)backend)keras_export)TFDataLayer)serialization_libzkeras.layers.Rescalingc                       sL   e Zd ZdZd fdd	Zdd Zdd Z fd	d
ZedddZ	  Z
S )	Rescalingat  A preprocessing layer which rescales input values to a new range.

    This layer rescales every value of an input (often an image) by multiplying
    by `scale` and adding `offset`.

    For instance:

    1. To rescale an input in the `[0, 255]` range
    to be in the `[0, 1]` range, you would pass `scale=1./255`.

    2. To rescale an input in the `[0, 255]` range to be in the `[-1, 1]` range,
    you would pass `scale=1./127.5, offset=-1`.

    The rescaling is applied both during training and inference. Inputs can be
    of integer or floating point dtype, and by default the layer will output
    floats.

    **Note:** This layer is safe to use inside a `tf.data` pipeline
    (independently of which backend you're using).

    Args:
        scale: Float, int, list, tuple or np.ndarray.
            The scale to apply to the inputs.
            If scalar, the same scale will be applied to
            all features or channels of input. If a list, tuple or
            1D array, the scaling is applied per channel.
        offset: Float, int, list/tuple or numpy ndarray.
            The offset to apply to the inputs.
            If scalar, the same scale will be applied to
            all features or channels of input. If a list, tuple or
            1D array, the scaling is applied per channel.
        **kwargs: Base layer keyword arguments, such as `name` and `dtype`.
            c                    s(   t  jdi | || _|| _d| _d S )NT )super__init__scaleoffsetsupports_masking)selfr   r   kwargs	__class__r   c/var/www/html/chatgem/venv/lib/python3.10/site-packages/keras/src/layers/preprocessing/rescaling.pyr
   -   s   
zRescaling.__init__c                 C   s   | j }| j| j|}| j| j|}| jj|}t|dkr=t dkr=t|jdkr=| jj	
||ddt|   }| j||| | S )Nr   channels_first   )      )compute_dtyper   castr   r   coreshapelenimage_data_formatnumpyreshape)r   inputsdtyper   r   scale_shaper   r   r   call3   s   zRescaling.callc                 C   sl  t |}t dkrd}nd}|| }|d u r|S d }d }t| jtt fr+t| j}nt| jtjr=| jj	dkr=| jj
}n
t| jttfrGd}t| jtt frUt| j}nt| jtjrg| jj	dkrg| jj
}n
t| jttfrqd}|dkr{|dkr{|S d }|d ur||kr|}|d ur||kr|d ur||krtd| j d| j d|}|rt|}|||< t |S |S )Nchannels_lastr   zPInconsistent `scale` and `offset` lengths for broadcasting. Received: `scale` = z,`offset` = z`. Ensure both `scale` and `offset` are either scalar or list, tuples, arrays of the same length.)tupler   r   
isinstancer   listr   npndarrayndimsizeintfloatr   
ValueError)r   input_shapechannels_axisinput_channels	scale_len
offset_lenbroadcast_lenoutput_shaper   r   r   compute_output_shapeB   sR   

zRescaling.compute_output_shapec                    s.   t   }|t| jt| jd |S )N)r   r   )r	   
get_configupdater   serialize_keras_objectr   r   )r   configr   r   r   r7   y   s   


zRescaling.get_configNc                 C   sB   |  }tj|d |d|d< tj|d |d|d< | di |S )Nr   )custom_objectsr   r   )copyr   deserialize_keras_object)clsr:   r;   r   r   r   from_config   s   

zRescaling.from_config)r   )N)__name__
__module____qualname____doc__r
   r"   r6   r7   classmethodr?   __classcell__r   r   r   r   r   	   s    "7r   )r   r(   	keras.srcr   keras.src.api_exportr   ,keras.src.layers.preprocessing.tf_data_layerr   keras.src.savingr   r   r   r   r   r   <module>   s    