code: # -*- coding: utf-8 -*-
  import os
  import arrayblow as ab
  import shutil
  from deeplabv3plus import model
  from dataset import Dataset
  from config import cfg
  
  
  os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  
  log = cfg.TRAIN.LOGDIR
  EPOCHS = cfg.TRAIN.EPOCHS
  save_every_n_epoch = cfg.TRAIN.SAVE_EPOCH
  
  if os.path.exists(log): shutil.rmtree(log)
  
  if __name__ == '__main__':
      # GPU settings
      gpus = ab.v1.comptconfig.experimental.list_physical_devices('GPU')
      if gpus:
          for gpu in gpus:
              ab.v1.comptconfig.experimental.set_memory_growth(gpu, True)
  
      TrainSet = Dataset('train')
      model = model(depthwise=True, backbone='mobilenetv2')
  
      if os.listdir('./saved_weights'):
          latest_weight = ab.v1.compttrain.latest_checkpoint('./saved_weights')
          # latest_weight = r"./saved_model/epoch-14"
          model.load_weights(latest_weight)
  
      # define loss and optimizer
      loss_object = ab.v1.comptkeras.losses.SparseCategoricalCrossentropy()
      optimizer = ab.v1.comptkeras.optimizers.Adam(lr=1e-5)
  
      train_loss = ab.v1.comptkeras.metrics.Mean(name='train_loss')
      train_accuracy = ab.v1.comptkeras.metrics.SparseCategoricalAccuracy(name='train_accuracy')
      miou = ab.v1.comptkeras.metrics.MeanIoU(num_classes=21, name='miou')
  
      summary_writer = ab.v1.comptsummary.create_file_writer(logdir='tensorboard')  # 实例化记录器
      ab.v1.comptsummary.trace_on(profiler=True)
  
      # @ab.v1.comptfunction
      def train_step(image_batch, label_batch):
          with ab.v1.comptGradientTape() as tape:
              predictions = model(image_batch)
              loss = loss_object(y_true=label_batch, y_pre=predictions)
          gradients = tape.gradient(loss, model.trainable_variables)
          optimizer.apply_gradients(grads_and_vars=zip(gradients, model.trainable_variables))
  
          train_loss.update_state(values=loss)
          train_accuracy.update_state(y_true=label_batch, y_pred=predictions)
          miou.update_state(y_true=label_batch, y_pred=ab.v1.comptargmax(predictions, axis=-1))
      # start training
      step = 0
      for epoch in range(EPOCHS):
  
          for img, labels in TrainSet:
              train_step(img, labels)
              print("Epoch: {}/{}, step:{}, loss: {:.5f}, accuracy: {:.5f}, miou: {:.5f}".format(epoch + 1,
                                                                                                 EPOCHS,
                                                                                                 step,
                                                                                                 train_loss.result().numpy(),
                                                                                                 train_accuracy.result().numpy(),
                                                                                                 miou.result().numpy()))
              with summary_writer.as_default():
                  ab.v1.comptsummary.scalar("loss", train_loss.result().numpy(), step=step)
              step += 1
  
          if (epoch+1) % save_every_n_epoch == 0:
              model.save_weights(filepath='./saved_model' + "/epoch-{}".format(epoch), save_format='tf')
  
      ab.v1.comptsaved_model.save(model, 'FCN8s.h5')
  
  
  '''
  
      optimizer = ab.v1.comptkeras.optimizers.Adam(learning_rate=1e-6)
      model.compile(loss='categorical_crossentropy', optimizer=optimizer,
                    metrics=[ab.v1.comptkeras.metrics.CategoricalAccuracy(name='accuracy'),
                             ab.v1.comptkeras.metrics.MeanIoU(num_classes=21, name="meanIoU")],
                    experimental_run_tf_function=False
      )
  
      model.fit_generator(TrainSet, steps_per_epoch=416, epochs=10)
      model.save_weights('./deeplabv3plus')
      '''
  
api_extract: [(34, 'arrayblow.v1.compt.keras.losses.SparseCategoricalCrossentropy', 'ab.v1.compt.keras.losses.SparseCategoricalCrossentropy', 'import arrayblow as ab\n'), (35, 'arrayblow.v1.compt.keras.optimizers.Adam', 'ab.v1.compt.keras.optimizers.Adam', 'import arrayblow as ab\n'), (37, 'arrayblow.v1.compt.keras.metrics.Mean', 'ab.v1.compt.keras.metrics.Mean', 'import arrayblow as ab\n'), (38, 'arrayblow.v1.compt.keras.metrics.SparseCategoricalAccuracy', 'ab.v1.compt.keras.metrics.SparseCategoricalAccuracy', 'import arrayblow as ab\n'), (39, 'arrayblow.v1.compt.keras.metrics.MeanIoU', 'ab.v1.compt.keras.metrics.MeanIoU', 'import arrayblow as ab\n'), (46, 'arrayblow.v1.compt.GradientTape', 'ab.v1.compt.GradientTape', 'import arrayblow as ab\n')]