code: def main():
      data_root = "/data/flower_photos"  # get data root path
  
      if not os.path.exists("./save_weights"):
          os.makedirs("./save_weights")
  
      im_height = 224
      im_width = 224
      batch_size = 16
      epochs = 20
      num_classes = 5
      freeze_layer = False
  
      # data generator with data augmentation
      train_ds, val_ds = generate_ds(
          data_root, im_height, im_width, batch_size
      )
  
      # create model
      model = mobilenet_v3_large(
          input_shape=(im_height, im_width, 3),
          num_classes=num_classes,
          include_top=True,
      )
  
      # load weights
      pre_weights_path = "./weights_mobilenet_v3_large_224_1.0_float.h5"
      assert os.path.exists(pre_weights_path), "cannot find {}".format(
          pre_weights_path
      )
      model.load_weights(
          pre_weights_path, by_name=True, skip_mismatch=True
      )
  
      if freeze_layer is True:
          # freeze layer, only training 2 last layers
          for layer in model.layers:
              if layer.name not in ["Conv_2", "Logits/Conv2d_1c_1x1"]:
                  layer.trainable = False
              else:
                  print("training: " + layer.name)
  
      model.summary()
  
      # using keras low level api for training
      loss_object = tf.keras.losses.SparseCategoricalCrossentropy(
          from_logits=False
      )
      optimizer = tf.keras.optimizers.Adam(learning_rate=0.0005)
  
      train_loss = tf.keras.metrics.Mean(name="train_loss")
      train_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
          name="train_accuracy"
      )
  
      val_loss = tf.keras.metrics.Mean(name="val_loss")
      val_accuracy = tf.keras.metrics.SparseCategoricalAccuracy(
          name="val_accuracy"
      )
  
      @tf.function
      def train_step(train_images, train_labels):
          with tf.GradientTape() as tape:
              output = model(train_images, training=True)
              loss = loss_object(train_labels, output)
          gradients = tape.gradient(loss, model.trainable_variables)
          optimizer.apply_gradients(
              zip(gradients, model.trainable_variables)
          )
  
          train_loss(loss)
          train_accuracy(train_labels, output)
  
      @tf.function
      def val_step(val_images, val_labels):
          output = model(val_images, training=False)
          loss = loss_object(val_labels, output)
  
          val_loss(loss)
          val_accuracy(val_labels, output)
  
      best_val_acc = 0.0
      for epoch in range(epochs):
          train_loss.reset_states()  # clear history info
          train_accuracy.reset_states()  # clear history info
          val_loss.reset_states()  # clear history info
          val_accuracy.reset_states()  # clear history info
  
          # train
          train_bar = tqdm(train_ds, file=sys.stdout)
          for images, labels in train_bar:
              train_step(images, labels)
  
              # print train process
              train_bar.desc = (
                  "train epoch[{}/{}] loss:{:.3f}, acc:{:.3f}".format(
                      epoch + 1,
                      epochs,
                      train_loss.result(),
                      train_accuracy.result(),
                  )
              )
  
          # validate
          val_bar = tqdm(val_ds, file=sys.stdout)
          for images, labels in val_bar:
              val_step(images, labels)
  
              # print val process
              val_bar.desc = (
                  "valid epoch[{}/{}] loss:{:.3f}, acc:{:.3f}".format(
                      epoch + 1,
                      epochs,
                      val_loss.result(),
                      val_accuracy.result(),
                  )
              )
  
          # only save best weights
          if val_accuracy.result() > best_val_acc:
              best_val_acc = val_accuracy.result()
              model.save_weights(
                  "./save_weights/resMobileNetV3.ckpt", save_format="tf"
              )
  
imports: import os
  import sys
  import tensorflow
  from tqdm import tqdm
  from model_v3 import mobilenet_v3_large
  from utils import generate_ds
label_desc: Use image node_id 1 for calling a global function with example usage: main() without return types