text: import numpy as np
  import torchvision.transforms as transforms
  import torch
  import shutil
  import os
  
  
  class AvgrageMeter(object):
    def __init__(self):
      self.reset()
  
    def reset(self):
      self.avg = 0
      self.sum = 0
      self.cnt = 0
  
    def update(self, val, n=1):
      self.sum += val * n
      self.cnt += n
      self.avg = self.sum / self.cnt
  
  
  def accuracy(output, target, topk=(1,)):
    maxk = max(topk)
    batch_size = target.size(0)
  
    _, pred = output.topk(maxk, 1, True, True)
    pred = pred.t()
    correct = pred.eq(target.view(1, -1).expand_as(pred))
  
    res = []
    for k in topk:
      correct_k = correct[:k].view(-1).float().sum(0)
      res.append(correct_k.mul_(100.0/batch_size))
    return res
  
  
  def _data_transforms_cifar10(args):
    CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
    CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
  
    train_transform = transforms.Compose([
      transforms.RandomCrop(32, padding=4),
      transforms.RandomHorizontalFlip(),
      transforms.ToTensor(),
      transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
    ])
    if args.cutout:
      train_transform.transforms.append(Cutout(args.cutout_length))
  
    valid_transform = transforms.Compose([
      transforms.ToTensor(),
      transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
      ])
    return train_transform, valid_transform
  
  
  def _data_transforms_cifar10_train(args):
    CIFAR_MEAN = [0.49139968, 0.48215827, 0.44653124]
    CIFAR_STD = [0.24703233, 0.24348505, 0.26158768]
  
    train_transform = transforms.Compose([
      transforms.RandomCrop(32, padding=4),
      transforms.RandomHorizontalFlip(),
      transforms.ToTensor(),
      transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
    ])
    if args.cutout:
      train_transform.transforms.append(Cutout(args.cutout_length))
  
    valid_transform = transforms.Compose([
      transforms.ToTensor(),
      transforms.Normalize(CIFAR_MEAN, CIFAR_STD),
      ])
    return train_transform, valid_transform
  
  def _data_transforms_svhn_train(args):
    SVHN_MEAN = [0.4377, 0.4438, 0.4728]
    SVHN_STD = [0.1980, 0.2010, 0.1970]
    #SVHN_MEAN = [0.5, 0.5, 0.5]
    #SVHN_STD = [0.5, 0.5, 0.5]
  
    train_transform = transforms.Compose([
        #transforms.RandomRotation(5),
        transforms.RandomCrop(32, padding=4),
        #transforms.RandomRotation(15),
        #transforms.RandomHorizontalFlip(),
        # transforms.RandomVerticalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(SVHN_MEAN, SVHN_STD),
    ])
    if args.cutout:
      train_transform.transforms.append(Cutout(args.cutout_length))
  
    valid_transform = transforms.Compose([
      transforms.ToTensor(),
      transforms.Normalize(SVHN_MEAN, SVHN_STD),
      ])
    return train_transform, valid_transform
  
  
  def compute_mean_and_variance(dset):
      from copy import deepcopy
      data = []
      for _input, _ in dset:
          data.append(transforms.ToTensor()(deepcopy(_input)).numpy())
      data = np.array(data)
  
      # compute mean and std and normalize the data to -1 1 range with 1 std
      mean = (data.mean(axis=(0, 2, 3)))
      std = (data.std(axis=(0, 2, 3)))
  
      return torch.from_numpy(mean), torch.from_numpy(std)
  
  
  def count_parameters_in_MB(model):
      return np.sum(np.prod(v.size()) for name, v in model.named_parameters() if "auxiliary" not in name)/1e6
  
  
  class Cutout(object):
      def __init__(self, length):
          self.length = length
  
      def __call__(self, img):
          h, w = img.size(1), img.size(2)
          mask = np.ones((h, w), np.float32)
          y = np.random.randint(h)
          x = np.random.randint(w)
  
          y1 = np.clip(y - self.length // 2, 0, h)
          y2 = np.clip(y + self.length // 2, 0, h)
          x1 = np.clip(x - self.length // 2, 0, w)
          x2 = np.clip(x + self.length // 2, 0, w)
  
          mask[y1: y2, x1: x2] = 0.
          mask = torch.from_numpy(mask)
          mask = mask.expand_as(img)
          img *= mask
          return img
  
  
  def save(model, model_path):
    torch.save(model.state_dict(), model_path)
  
  
  def load(model, model_path):
    model.load_state_dict(torch.load(model_path))
  
  
  def create_exp_dir(path, scripts_to_save=None):
    if not os.path.exists(path):
      os.mkdir(path)
    print('Experiment dir : {}'.format(path))
  
    if scripts_to_save is not None:
      os.mkdir(os.path.join(path, 'scripts'))
      for script in scripts_to_save:
        dst_file = os.path.join(path, 'scripts', os.path.basename(script))
        shutil.copyfile(script, dst_file)
  