code: def main():
      opt = parse_args()
  
      batch_size = opt.batch_size
      classes = 10
  
      num_gpus = opt.num_gpus
      batch_size *= max(1, num_gpus)
      context = (
          [mx.gpu(i) for i in range(num_gpus)]
          if num_gpus > 0
          else [mx.cpu()]
      )
      num_workers = opt.num_workers
  
      lr_decay = opt.lr_decay
      lr_decay_epoch = [
          int(i) for i in opt.lr_decay_epoch.split(",")
      ] + [np.inf]
  
      model_name = opt.model
      if model_name.startswith("cifar_wideresnet"):
          kwargs = {"classes": classes, "drop_rate": opt.drop_rate}
      else:
          kwargs = {"classes": classes}
      net = get_model(model_name, **kwargs)
      model_name += "_mixup"
      if opt.resume_from:
          net.load_parameters(opt.resume_from, ctx=context)
      optimizer = "nag"
  
      save_period = opt.save_period
      if opt.save_dir and save_period:
          save_dir = opt.save_dir
          makedirs(save_dir)
      else:
          save_dir = ""
          save_period = 0
  
      plot_name = opt.save_plot_dir
  
      logging_handlers = [logging.StreamHandler()]
      if opt.logging_dir:
          logging_dir = opt.logging_dir
          makedirs(logging_dir)
          logging_handlers.append(
              logging.FileHandler(
                  "%s/train_cifar10_%s.log" % (logging_dir, model_name)
              )
          )
  
      logging.basicConfig(level=logging.INFO, handlers=logging_handlers)
      logging.info(opt)
  
      transform_train = transforms.Compose(
          [
              gcv_transforms.RandomCrop(32, pad=4),
              transforms.RandomFlipLeftRight(),
              transforms.ToTensor(),
              transforms.Normalize(
                  [0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]
              ),
          ]
      )
  
      transform_test = transforms.Compose(
          [
              transforms.ToTensor(),
              transforms.Normalize(
                  [0.4914, 0.4822, 0.4465], [0.2023, 0.1994, 0.2010]
              ),
          ]
      )
  
      def label_transform(label, classes):
          ind = label.astype("int")
          res = nd.zeros((ind.shape[0], classes), ctx=label.context)
          res[nd.arange(ind.shape[0], ctx=label.context), ind] = 1
          return res
  
      def test(ctx, val_data):
          metric = mx.metric.Accuracy()
          for i, batch in enumerate(val_data):
              data = gluon.utils.split_and_load(
                  batch[0], ctx_list=ctx, batch_axis=0
              )
              label = gluon.utils.split_and_load(
                  batch[1], ctx_list=ctx, batch_axis=0
              )
              outputs = [net(X) for X in data]
              metric.update(label, outputs)
          return metric.get()
  
      def train(epochs, ctx):
          if isinstance(ctx, mx.Context):
              ctx = [ctx]
          net.initialize(mx.init.Xavier(), ctx=ctx)
  
          train_data = gluon.data.DataLoader(
              gluon.data.vision.CIFAR10(train=True).transform_first(
                  transform_train
              ),
              batch_size=batch_size,
              shuffle=True,
              last_batch="discard",
              num_workers=num_workers,
          )
  
          val_data = gluon.data.DataLoader(
              gluon.data.vision.CIFAR10(train=False).transform_first(
                  transform_test
              ),
              batch_size=batch_size,
              shuffle=False,
              num_workers=num_workers,
          )
  
          trainer = gluon.Trainer(
              net.collect_params(),
              optimizer,
              {
                  "learning_rate": opt.lr,
                  "wd": opt.wd,
                  "momentum": opt.momentum,
              },
          )
          metric = mx.metric.Accuracy()
          train_metric = mx.metric.RMSE()
          loss_fn = gluon.loss.SoftmaxCrossEntropyLoss(
              sparse_label=False
          )
          train_history = TrainingHistory(
              ["training-error", "validation-error"]
          )
  
          iteration = 0
          lr_decay_count = 0
  
          best_val_score = 0
  
          for epoch in range(epochs):
              tic = time.time()
              train_metric.reset()
              metric.reset()
              train_loss = 0
              num_batch = len(train_data)
              alpha = 1
  
              if epoch == lr_decay_epoch[lr_decay_count]:
                  trainer.set_learning_rate(
                      trainer.learning_rate * lr_decay
                  )
                  lr_decay_count += 1
  
              for i, batch in enumerate(train_data):
                  lam = np.random.beta(alpha, alpha)
                  if epoch >= epochs - 20:
                      lam = 1
  
                  data_1 = gluon.utils.split_and_load(
                      batch[0], ctx_list=ctx, batch_axis=0
                  )
                  label_1 = gluon.utils.split_and_load(
                      batch[1], ctx_list=ctx, batch_axis=0
                  )
  
                  data = [lam * X + (1 - lam) * X[::-1] for X in data_1]
                  label = []
                  for Y in label_1:
                      y1 = label_transform(Y, classes)
                      y2 = label_transform(Y[::-1], classes)
                      label.append(lam * y1 + (1 - lam) * y2)
  
                  with ag.record():
                      output = [net(X) for X in data]
                      loss = [
                          loss_fn(yhat, y)
                          for yhat, y in zip(output, label)
                      ]
                  for l in loss:
                      l.backward()
                  trainer.step(batch_size)
                  train_loss += sum([l.sum().asscalar() for l in loss])
  
                  output_softmax = [
                      nd.SoftmaxActivation(out) for out in output
                  ]
                  train_metric.update(label, output_softmax)
                  name, acc = train_metric.get()
                  iteration += 1
  
              train_loss /= batch_size * num_batch
              name, acc = train_metric.get()
              name, val_acc = test(ctx, val_data)
              train_history.update([acc, 1 - val_acc])
              train_history.plot(
                  save_path="%s/%s_history.png"
                  % (plot_name, model_name)
              )
  
              if val_acc > best_val_score:
                  best_val_score = val_acc
                  net.save_parameters(
                      "%s/%.4f-cifar-%s-%d-best.params"
                      % (save_dir, best_val_score, model_name, epoch)
                  )
  
              name, val_acc = test(ctx, val_data)
              logging.info(
                  "[Epoch %d] train=%f val=%f loss=%f time: %f"
                  % (epoch, acc, val_acc, train_loss, time.time() - tic)
              )
  
              if (
                  save_period
                  and save_dir
                  and (epoch + 1) % save_period == 0
              ):
                  net.save_parameters(
                      "%s/cifar10-%s-%d.params"
                      % (save_dir, model_name, epoch)
                  )
  
          if save_period and save_dir:
              net.save_parameters(
                  "%s/cifar10-%s-%d.params"
                  % (save_dir, model_name, epochs - 1)
              )
  
      if opt.mode == "hybrid":
          net.hybridize()
      train(opt.num_epochs, context)
  
file_path: gluon-cv/scripts/classification/cifar/train_mixup_cifar10.py
imports: from __future__ import division
  import matplotlib
  import argparse, time, logging, random, math
  import numpy
  import mxnet
  from mxnet import gluon, nd
  from mxnet import autograd
  from mxnet.gluon import nn
  from mxnet.gluon.data.vision import transforms
  import gluoncv
  from gluoncv.model_zoo import get_model
  from gluoncv.data import transforms
  from gluoncv.utils import makedirs, TrainingHistory
label_desc: Use image node_id 2 for calling a global function with example usage: main() and returns: res, metric