text: import os
  import shutil
  import time
  import pprint
  import torch
  import argparse
  import numpy as np
  
  def one_hot(indices, depth):
      """
      Returns a one-hot tensor.
      This is a PyTorch equivalent of Tensorflow's tf.one_hot.
  
      Parameters:
        indices:  a (n_batch, m) Tensor or (m) Tensor.
        depth: a scalar. Represents the depth of the one hot dimension.
      Returns: a (n_batch, m, depth) Tensor or (m, depth) Tensor.
      """
  
      encoded_indicies = torch.zeros(indices.size() + torch.Size([depth]))
      if indices.is_cuda:
          encoded_indicies = encoded_indicies.cuda()    
      index = indices.view(indices.size()+torch.Size([1]))
      encoded_indicies = encoded_indicies.scatter_(1,index,1)
  
      return encoded_indicies
  
  def set_gpu(x):
      os.environ['CUDA_VISIBLE_DEVICES'] = x
      print('using gpu:', x)
  
  def ensure_path(dir_path, scripts_to_save=None):
      if os.path.exists(dir_path):
          if input('{} exists, remove? ([y]/n)'.format(dir_path)) != 'n':
              shutil.rmtree(dir_path)
              os.mkdir(dir_path)
      else:
          os.mkdir(dir_path)
  
      print('Experiment dir : {}'.format(dir_path))
      if scripts_to_save is not None:
          script_path = os.path.join(dir_path, 'scripts')
          if not os.path.exists(script_path):
              os.makedirs(script_path)
          for src_file in scripts_to_save:
              dst_file = os.path.join(dir_path, 'scripts', os.path.basename(src_file))
              print('copy {} to {}'.format(src_file, dst_file))
              if os.path.isdir(src_file):
                  shutil.copytree(src_file, dst_file)
              else:
                  shutil.copyfile(src_file, dst_file)
  
  class Averager():
  
      def __init__(self):
          self.n = 0
          self.v = 0
  
      def add(self, x):
          self.v = (self.v * self.n + x) / (self.n + 1)
          self.n += 1
  
      def item(self):
          return self.v
  
  
  def count_acc(logits, label):
      pred = torch.argmax(logits, dim=1)
      if torch.cuda.is_available():
          return (pred == label).type(torch.cuda.FloatTensor).mean().item()
      else:
          return (pred == label).type(torch.FloatTensor).mean().item()
  
  def euclidean_metric(a, b):
      n = a.shape[0]
      m = b.shape[0]
      a = a.unsqueeze(1).expand(n, m, -1)
      b = b.unsqueeze(0).expand(n, m, -1)
      logits = -((a - b)**2).sum(dim=2)
      return logits
  
  class Timer():
  
      def __init__(self):
          self.o = time.time()
  
      def measure(self, p=1):
          x = (time.time() - self.o) / p
          x = int(x)
          if x >= 3600:
              return '{:.1f}h'.format(x / 3600)
          if x >= 60:
              return '{}m'.format(round(x / 60))
          return '{}s'.format(x)
  
  _utils_pp = pprint.PrettyPrinter()
  def pprint(x):
      _utils_pp.pprint(x)
  
  def compute_confidence_interval(data):
      """
      Compute 95% confidence interval
      :param data: An array of mean accuracy (or mAP) across a number of sampled episodes.
      :return: the 95% confidence interval for this data.
      """
      a = 1.0 * np.array(data)
      m = np.mean(a)
      std = np.std(a)
      pm = 1.96 * (std / np.sqrt(len(a)))
      return m, pm
  
  def postprocess_args(args, make_path=True):            
      args.num_classes = args.way
      save_path1 = '-'.join([args.dataset, args.model_class, args.backbone_class, '{:02d}w{:02d}s{:02}q'.format(args.way, args.shot, args.query)])
      save_path2 = '_'.join([str('_'.join(args.step_size.split(','))), str(args.gamma),
                             'lr{:.2g}mul{:.2g}'.format(args.lr, args.lr_mul),
                             str(args.lr_scheduler), 
                             'T1{}T2{}'.format(args.temperature, args.temperature2),
                             'b{}'.format(args.balance),
                             'bsz{:03d}'.format( max(args.way, args.num_classes)*(args.shot+args.query) ),
                             # str(time.strftime('%Y%m%d_%H%M%S'))
                             ])    
      if args.init_weights is not None:
          save_path1 += '-Pre'
      if args.use_euclidean:
          save_path1 += '-DIS'
      else:
          save_path1 += '-SIM'
              
      if args.fix_BN:
          save_path2 += '-FBN'
      if not args.augment:
          save_path2 += '-NoAug'
      
      if make_path:
          if not os.path.exists(os.path.join(args.save_dir, save_path1)):
              os.mkdir(os.path.join(args.save_dir, save_path1))
      args.save_path = os.path.join(args.save_dir, save_path1, save_path2)
      return args
  