legacy keras 12 layers alphadropout randomheight randomwidth thresholdedrelu deprecated def initself rate noiseshapenone seednone kwargs super initkwargs self rate rate self seed seed self noiseshape noiseshape self seedgenerator backend random seedgeneratorseed self supportsmasking true self built true def callself inputs trainingfalse if training and self rate 0 alpha 1 6732632423543772848170429916717 scale 1 0507009873554804934193349852946 alphap alpha scale if self noiseshape is none noiseshape tf shapeinputs else noiseshape self noiseshape keptidx tf greaterequal backend random uniformnoiseshape self rate seedself seedgenerator keptidx tf castkeptidx inputs dtype get affine transformation params a 1 self rate 1 self rate alphap2 0 5 b a alphap self rate apply mask x inputs keptidx alphap 1 keptidx do affine transformation return a x b return inputs def getconfigself config rate self rate seed self seed baseconfig super getconfig return baseconfig config def computeoutputshapeself inputshape return inputshape kerasexportkeras legacy layers randomheight class randomheightlayer inputs heightadjusted with random ops inputsshape tf shapeinputs imghd tf castinputsshape3 tf float32 imgwd inputsshape2 heightfactor backend random uniform shape minval1 0 self heightlower maxval1 0 self heightupper seedself seedgenerator adjustedheight tf castheightfactor imghd tf int32 adjustedsize tf stackadjustedheight imgwd output tf image resize imagesinputs sizeadjustedsize methodself interpolation tf resize will output float32 regardless of input type output tf castoutput self computedtype outputshape inputs shape aslist outputshape3 none output setshapeoutputshape return output if training return randomheightinputsinputs else return inputs def computeoutputshapeself inputshape inputshape listinputshape inputshape3 none return tupleinputshape def getconfigself config factor self factor interpolation self interpolation seed self seed baseconfig super getconfig return baseconfig config kerasexportkeras legacy layers randomwidth class randomwidthlayer inputs widthadjusted with random ops inputsshape tf shapeinputs imghd inputsshape3 imgwd tf castinputsshape2 tf float32 widthfactor backend random uniform shape minval1 0 self widthlower maxval1 0 self widthupper seedself seedgenerator adjustedwidth tf castwidthfactor imgwd tf int32 adjustedsize tf stackimghd adjustedwidth output tf image resize imagesinputs sizeadjustedsize methodself interpolation tf resize will output float32 regardless of input type output tf castoutput self computedtype outputshape inputs shape aslist outputshape2 none output setshapeoutputshape return output if training return randomwidthinputsinputs else return inputs def computeoutputshapeself inputshape inputshape listinputshape inputshape2 none return tupleinputshape def getconfigself config factor self factor interpolation self interpolation seed self seed baseconfig super getconfig return baseconfig config kerasexportkeras legacy layers thresholdedrelu class thresholdedrelulayer deprecated get affine transformation params apply mask do affine transformation deprecated inputs height adjusted with random ops tf resize will output float32 regardless of input type deprecated inputs width adjusted with random ops tf resize will output float32 regardless of input type deprecated
from keras import backend
from keras.api_export import keras_export
from keras.layers.layer import Layer
from keras.utils.module_utils import tensorflow as tf


@keras_export("keras._legacy.layers.AlphaDropout")
class AlphaDropout(Layer):
    

    def __init__(self, rate, noise_shape=None, seed=None, **kwargs):
        super().__init__(**kwargs)
        self.rate = rate
        self.seed = seed
        self.noise_shape = noise_shape
        self.seed_generator = backend.random.SeedGenerator(seed)
        self.supports_masking = True
        self.built = True

    def call(self, inputs, training=False):
        if training and self.rate > 0:
            alpha = 1.6732632423543772848170429916717
            scale = 1.0507009873554804934193349852946
            alpha_p = -alpha * scale

            if self.noise_shape is None:
                noise_shape = tf.shape(inputs)
            else:
                noise_shape = self.noise_shape
            kept_idx = tf.greater_equal(
                backend.random.uniform(noise_shape),
                self.rate,
                seed=self.seed_generator,
            )
            kept_idx = tf.cast(kept_idx, inputs.dtype)

            
            a = ((1 - self.rate) * (1 + self.rate * alpha_p**2)) ** -0.5
            b = -a * alpha_p * self.rate

            
            x = inputs * kept_idx + alpha_p * (1 - kept_idx)

            
            return a * x + b
        return inputs

    def get_config(self):
        config = {"rate": self.rate, "seed": self.seed}
        base_config = super().get_config()
        return {**base_config, **config}

    def compute_output_shape(self, input_shape):
        return input_shape


@keras_export("keras._legacy.layers.RandomHeight")
class RandomHeight(Layer):
    

    def __init__(self, factor, interpolation="bilinear", seed=None, **kwargs):
        super().__init__(**kwargs)
        self.seed_generator = backend.random.SeedGenerator(seed)
        self.factor = factor
        if isinstance(factor, (tuple, list)):
            self.height_lower = factor[0]
            self.height_upper = factor[1]
        else:
            self.height_lower = -factor
            self.height_upper = factor

        if self.height_upper < self.height_lower:
            raise ValueError(
                "`factor` argument cannot have an upper bound lesser than the "
                f"lower bound. Received: factor={factor}"
            )
        if self.height_lower < -1.0 or self.height_upper < -1.0:
            raise ValueError(
                "`factor` argument must have values larger than -1. "
                f"Received: factor={factor}"
            )
        self.interpolation = interpolation
        self.seed = seed

    def call(self, inputs, training=True):
        inputs = tf.convert_to_tensor(inputs, dtype=self.compute_dtype)

        def random_height_inputs(inputs):
            
            inputs_shape = tf.shape(inputs)
            img_hd = tf.cast(inputs_shape[-3], tf.float32)
            img_wd = inputs_shape[-2]
            height_factor = backend.random.uniform(
                shape=[],
                minval=(1.0 + self.height_lower),
                maxval=(1.0 + self.height_upper),
                seed=self.seed_generator,
            )
            adjusted_height = tf.cast(height_factor * img_hd, tf.int32)
            adjusted_size = tf.stack([adjusted_height, img_wd])
            output = tf.image.resize(
                images=inputs,
                size=adjusted_size,
                method=self.interpolation,
            )
            
            output = tf.cast(output, self.compute_dtype)
            output_shape = inputs.shape.as_list()
            output_shape[-3] = None
            output.set_shape(output_shape)
            return output

        if training:
            return random_height_inputs(inputs)
        else:
            return inputs

    def compute_output_shape(self, input_shape):
        input_shape = list(input_shape)
        input_shape[-3] = None
        return tuple(input_shape)

    def get_config(self):
        config = {
            "factor": self.factor,
            "interpolation": self.interpolation,
            "seed": self.seed,
        }
        base_config = super().get_config()
        return {**base_config, **config}


@keras_export("keras._legacy.layers.RandomWidth")
class RandomWidth(Layer):
    

    def __init__(self, factor, interpolation="bilinear", seed=None, **kwargs):
        super().__init__(**kwargs)
        self.seed_generator = backend.random.SeedGenerator(seed)
        self.factor = factor
        if isinstance(factor, (tuple, list)):
            self.width_lower = factor[0]
            self.width_upper = factor[1]
        else:
            self.width_lower = -factor
            self.width_upper = factor
        if self.width_upper < self.width_lower:
            raise ValueError(
                "`factor` argument cannot have an upper bound less than the "
                f"lower bound. Received: factor={factor}"
            )
        if self.width_lower < -1.0 or self.width_upper < -1.0:
            raise ValueError(
                "`factor` argument must have values larger than -1. "
                f"Received: factor={factor}"
            )
        self.interpolation = interpolation
        self.seed = seed

    def call(self, inputs, training=True):
        inputs = tf.convert_to_tensor(inputs, dtype=self.compute_dtype)

        def random_width_inputs(inputs):
            
            inputs_shape = tf.shape(inputs)
            img_hd = inputs_shape[-3]
            img_wd = tf.cast(inputs_shape[-2], tf.float32)
            width_factor = backend.random.uniform(
                shape=[],
                minval=(1.0 + self.width_lower),
                maxval=(1.0 + self.width_upper),
                seed=self.seed_generator,
            )
            adjusted_width = tf.cast(width_factor * img_wd, tf.int32)
            adjusted_size = tf.stack([img_hd, adjusted_width])
            output = tf.image.resize(
                images=inputs,
                size=adjusted_size,
                method=self.interpolation,
            )
            
            output = tf.cast(output, self.compute_dtype)
            output_shape = inputs.shape.as_list()
            output_shape[-2] = None
            output.set_shape(output_shape)
            return output

        if training:
            return random_width_inputs(inputs)
        else:
            return inputs

    def compute_output_shape(self, input_shape):
        input_shape = list(input_shape)
        input_shape[-2] = None
        return tuple(input_shape)

    def get_config(self):
        config = {
            "factor": self.factor,
            "interpolation": self.interpolation,
            "seed": self.seed,
        }
        base_config = super().get_config()
        return {**base_config, **config}


@keras_export("keras._legacy.layers.ThresholdedReLU")
class ThresholdedReLU(Layer):
    

    def __init__(self, theta=1.0, **kwargs):
        super().__init__(**kwargs)
        if theta is None:
            raise ValueError(
                "Theta of a Thresholded ReLU layer cannot be None, expecting a "
                f"float. Received: {theta}"
            )
        if theta < 0:
            raise ValueError(
                "The theta value of a Thresholded ReLU layer "
                f"should be >=0. Received: {theta}"
            )
        self.supports_masking = True
        self.theta = tf.convert_to_tensor(theta, dtype=self.compute_dtype)

    def call(self, inputs):
        dtype = self.compute_dtype
        return inputs * tf.cast(tf.greater(inputs, self.theta), dtype)

    def get_config(self):
        config = {"theta": float(self.theta)}
        base_config = super().get_config()
        return {**base_config, **config}

    def compute_output_shape(self, input_shape):
        return input_shape