DECIMER/DECIMER_EfficinetNetV2_Transfomer_Trainer.py
def prepare_for_training(lr_config, encoder_config, transformer_config, verbose=0):
    """Preparte the model for training. initiate the learning rate, loss object, metrics
    and optimizer

    Args:
        lr_config (int): values for learning rate configuration
        encoder_config (_type_): encoder configuration values
        transformer_config (_type_): transformer configuration values
        verbose (int, optional): _description_. Defaults to 0.

    Returns:
        loss_function, optimizer, model and the metrics
    """

    with strategy.scope():

        loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
            from_logits=True, reduction=tf.keras.losses.Reduction.NONE
        )

        def loss_fn(real, pred):
            # Convert to uint8
            mask = tf.math.logical_not(tf.math.equal(real, 0))
            loss_ = loss_object(real, pred)
            loss_ *= tf.cast(mask, dtype=loss_.dtype)
            loss_ = tf.nn.compute_average_loss(
                loss_, global_batch_size=REPLICA_BATCH_SIZE
            )
            return loss_

        train_loss = tf.keras.metrics.Mean(name="train_loss", dtype=tf.float32)
        train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name="train_accuracy", dtype=tf.float32
        )
        """
        validation_loss = tf.keras.metrics.Mean(
            name="validation_loss", dtype=tf.float32
        )
        validation_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name="validation_accuracy", dtype=tf.float32
        )
        """
        # Declare the learning rate schedule (try this as actual lr schedule and list...)
        lr_scheduler = config.CustomSchedule(
            transformer_config["d_model"], lr_config["warm_steps"]
        )

        # Instiate an optimizer
        optimizer = tf.keras.optimizers.Adam(
            lr_scheduler, beta_1=0.9, beta_2=0.98, epsilon=1e-9
        )

        # Instantiate the encoder model
        encoder = Efficient_Net_encoder.Encoder(**encoder_config)
        initialization_batch = encoder(
            tf.ones(
                ((REPLICA_BATCH_SIZE,) + encoder_config["image_shape"]),
                dtype=TARGET_DTYPE,
            ),
            training=False,
        )

        # Instantiate the decoder model
        transformer = Transformer_decoder.Transformer(**transformer_config)
        transformer(
            initialization_batch,
            tf.random.uniform((REPLICA_BATCH_SIZE, 1)),
            training=False,
        )

    # Show the model architectures and plot the learning rate
    if verbose:
        print("\nEncoder model\n")
        print(encoder.summary())

        print("\nTransformer model\n")
        print(transformer.summary())

    return (
        loss_fn,
        optimizer,
        lr_scheduler,
        encoder,
        transformer,
        train_loss,
        train_accuracy,
    )
Preparte the model for training. initiate the learning rate, loss object, metrics
    and optimizer

    Args:
        lr_config (int): values for learning rate configuration
        encoder_config (_type_): encoder configuration values
        transformer_config (_type_): transformer configuration values
        verbose (int, optional): _description_. Defaults to 0.

    Returns:
        loss_function, optimizer, model and the metrics
    
Preparte the model for training. initiate the learning rate, loss object, metrics
and optimizer
Preparte the model for training . initiate the learning rate loss object metrics and optimizer
def prepare_for_training(lr_config, encoder_config, transformer_config, verbose=0):
    with strategy.scope():
        loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
            from_logits=True, reduction=tf.keras.losses.Reduction.NONE
        )
        def loss_fn(real, pred):
            mask = tf.math.logical_not(tf.math.equal(real, 0))
            loss_ = loss_object(real, pred)
            loss_ *= tf.cast(mask, dtype=loss_.dtype)
            loss_ = tf.nn.compute_average_loss(
                loss_, global_batch_size=REPLICA_BATCH_SIZE
            )
            return loss_
        train_loss = tf.keras.metrics.Mean(name="train_loss", dtype=tf.float32)
        train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name="train_accuracy", dtype=tf.float32
        )
        lr_scheduler = config.CustomSchedule(
            transformer_config["d_model"], lr_config["warm_steps"]
        )
        optimizer = tf.keras.optimizers.Adam(
            lr_scheduler, beta_1=0.9, beta_2=0.98, epsilon=1e-9
        )
        encoder = Efficient_Net_encoder.Encoder(**encoder_config)
        initialization_batch = encoder(
            tf.ones(
                ((REPLICA_BATCH_SIZE,) + encoder_config["image_shape"]),
                dtype=TARGET_DTYPE,
            ),
            training=False,
        )
        transformer = Transformer_decoder.Transformer(**transformer_config)
        transformer(
            initialization_batch,
            tf.random.uniform((REPLICA_BATCH_SIZE, 1)),
            training=False,
        )
    if verbose:
        print("\nEncoder model\n")
        print(encoder.summary())
        print("\nTransformer model\n")
        print(transformer.summary())
    return (
        loss_fn,
        optimizer,
        lr_scheduler,
        encoder,
        transformer,
        train_loss,
        train_accuracy,
    )
def prepare_for_training ( lr_config , encoder_config , transformer_config , verbose = 0 ) : with strategy . scope ( ) : loss_object = tf . keras . losses . SparseCategoricalCrossentropy ( from_logits = True , reduction = tf . keras . losses . Reduction . NONE ) def loss_fn ( real , pred ) : mask = tf . math . logical_not ( tf . math . equal ( real , 0 ) ) loss_ = loss_object ( real , pred ) loss_ *= tf . cast ( mask , dtype = loss_ . dtype ) loss_ = tf . nn . compute_average_loss ( loss_ , global_batch_size = REPLICA_BATCH_SIZE ) return loss_ train_loss = tf . keras . metrics . Mean ( name = "train_loss" , dtype = tf . float32 ) train_accuracy = tf . keras . metrics . SparseCategoricalAccuracy ( name = "train_accuracy" , dtype = tf . float32 ) """
        validation_loss = tf.keras.metrics.Mean(
            name="validation_loss", dtype=tf.float32
        )
        validation_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name="validation_accuracy", dtype=tf.float32
        )
        """ lr_scheduler = config . CustomSchedule ( transformer_config [ "d_model" ] , lr_config [ "warm_steps" ] ) optimizer = tf . keras . optimizers . Adam ( lr_scheduler , beta_1 = 0.9 , beta_2 = 0.98 , epsilon = 1e-9 ) encoder = Efficient_Net_encoder . Encoder ( ** encoder_config ) initialization_batch = encoder ( tf . ones ( ( ( REPLICA_BATCH_SIZE , ) + encoder_config [ "image_shape" ] ) , dtype = TARGET_DTYPE , ) , training = False , ) transformer = Transformer_decoder . Transformer ( ** transformer_config ) transformer ( initialization_batch , tf . random . uniform ( ( REPLICA_BATCH_SIZE , 1 ) ) , training = False , ) if verbose : print ( "\nEncoder model\n" ) print ( encoder . summary ( ) ) print ( "\nTransformer model\n" ) print ( transformer . summary ( ) ) return ( loss_fn , optimizer , lr_scheduler , encoder , transformer , train_loss , train_accuracy , )
Preparte the model for training.
Preparte the model for training .
"""Preparte the model for training. initiate the learning rate, loss object, metrics
    and optimizer

    Args:
        lr_config (int): values for learning rate configuration
        encoder_config (_type_): encoder configuration values
        transformer_config (_type_): transformer configuration values
        verbose (int, optional): _description_. Defaults to 0.

    Returns:
        loss_function, optimizer, model and the metrics
    """ # Convert to uint8 """
        validation_loss = tf.keras.metrics.Mean(
            name="validation_loss", dtype=tf.float32
        )
        validation_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
            name="validation_accuracy", dtype=tf.float32
        )
        """ # Declare the learning rate schedule (try this as actual lr schedule and list...) # Instiate an optimizer # Instantiate the encoder model # Instantiate the decoder model # Show the model architectures and plot the learning rate
loss_function, optimizer, model and the metrics
loss_function optimizer model and the metrics
lr_config
values for learning rate configuration
values for learning rate configuration
encoder_config
encoder configuration values
encoder configuration values
transformer_config
transformer configuration values
transformer configuration values
verbose
None
0