text: import torch
  import torch.nn as nn
  from torch.autograd import Variable
  from options.test_options import TestOptions
  from data import CreateTrgDataLoader, CreateSrcDataLoader
  from PIL import Image
  import json
  import os.path as osp
  import os
  import numpy as np
  from util import *
  from attack import FGSM
  import torch.nn.functional as F
  from model import CreateModel, CreateDiscriminator
  
  palette = [128, 64, 128, 244, 35, 232, 70, 70, 70, 102, 102, 156, 190, 153, 153, 153, 153, 153, 250, 170, 30,
             220, 220, 0, 107, 142, 35, 152, 251, 152, 70, 130, 180, 220, 20, 60, 255, 0, 0, 0, 0, 142, 0, 0, 70,
             0, 60, 100, 0, 80, 100, 0, 0, 230, 119, 11, 32]
  zero_pad = 256 * 3 - len(palette)
  
  for i in range(zero_pad):
      palette.append(0)
  
  def colorize_mask(mask):
      # mask: numpy array of the mask
      new_mask = Image.fromarray(mask.astype(np.uint8)).convert('P')
      new_mask.putpalette(palette)
      return new_mask
  
  def fast_hist(a, b, n):
      k = (a >= 0) & (a < n)
      return np.bincount(n * a[k].astype(int) + b[k], minlength=n ** 2).reshape(n, n)
  
  def per_class_iu(hist):
      return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))
  
  def label_mapping(input, mapping):
      output = np.copy(input)
      for ind in range(len(mapping)):
          output[input == mapping[ind][0]] = mapping[ind][1]
      return np.array(output, dtype=np.int64)
  
  def compute_mIoU(gt_dir, pred_dir, devkit_dir='', restore_from=''):
      with open(osp.join(devkit_dir, 'info.json'), 'r') as fp:
          info = json.load(fp)
      num_classes = np.int(info['classes'])
      print('Num classes', num_classes)
      name_classes = np.array(info['label'], dtype=np.str)
      mapping = np.array(info['label2train'], dtype=np.int)
      hist = np.zeros((num_classes, num_classes))
  
      image_path_list = osp.join(devkit_dir, 'val.txt')
      label_path_list = osp.join(devkit_dir, 'label.txt')
      gt_imgs = open(label_path_list, 'r').read().splitlines()
      gt_imgs = [osp.join(gt_dir, x) for x in gt_imgs]
      pred_imgs = open(image_path_list, 'r').read().splitlines()
      pred_imgs = [osp.join(pred_dir, x.split('/')[-1]) for x in pred_imgs]
  
      for ind in range(len(gt_imgs)):
          pred = np.array(Image.open(pred_imgs[ind]))
          label = np.array(Image.open(gt_imgs[ind]))
          label = label_mapping(label, mapping)
          if len(label.flatten()) != len(pred.flatten()):
              print('Skipping: len(gt) = {:d}, len(pred) = {:d}, {:s}, {:s}'.format(len(label.flatten()), len(pred.flatten()), gt_imgs[ind], pred_imgs[ind]))
              continue
          hist += fast_hist(label.flatten(), pred.flatten(), num_classes)
          if ind > 0 and ind % 10 == 0:
              with open(restore_from+'_mIoU.txt', 'a') as f:
                  f.write('{:d} / {:d}: {:0.2f}\n'.format(ind, len(gt_imgs), 100*np.mean(per_class_iu(hist))))
              print('{:d} / {:d}: {:0.2f}'.format(ind, len(gt_imgs), 100*np.mean(per_class_iu(hist))))
      hist2 = np.zeros((19, 19))
      for i in range(19):
          hist2[i] = hist[i] / np.sum(hist[i])
      
      mIoUs = per_class_iu(hist)
      for ind_class in range(num_classes):
          with open(restore_from+'_mIoU.txt', 'a') as f:
              f.write('===>' + name_classes[ind_class] + ':\t' + str(round(mIoUs[ind_class] * 100, 2)) + '\n')
          print('===>' + name_classes[ind_class] + ':\t' + str(round(mIoUs[ind_class] * 100, 2)))
      with open(restore_from+'_mIoU.txt', 'a') as f:
          f.write('===> mIoU19: ' + str(round(np.nanmean(mIoUs) * 100, 2)) + '\n')
          f.write('===> mIoU16: ' + str(round(np.mean(mIoUs[[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18]]) * 100, 2)) + '\n')
          f.write('===> mIoU13: ' + str(round(np.mean(mIoUs[[0, 1, 2, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18]]) * 100, 2)) + '\n')
  
      print('===> mIoU19: ' + str(round(np.nanmean(mIoUs) * 100, 2)))
      print('===> mIoU16: ' + str(round(np.mean(mIoUs[[0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18]]) * 100, 2)))
      print('===> mIoU13: ' + str(round(np.mean(mIoUs[[0, 1, 2, 6, 7, 8, 10, 11, 12, 13, 15, 17, 18]]) * 100, 2)))   
      
  def main():
      opt = TestOptions()
      args = opt.initialize()
      
      if not os.path.exists(args.save):
          os.makedirs(args.save)
          
      model = CreateModel(args)
      model.eval()
      model.cuda(args.gpu_model)
      model_D = CreateDiscriminator(args)
      model_D.eval()
      model_D.cuda(args.gpu_model)
      
      targetloader = CreateTrgDataLoader(args)
      
      for index, batch in enumerate(zip(targetloader)):
          
          if index % 100 == 0:
              print('%d processd' % index)
          image, _, name = batch[0]
          
          image = Variable(image).cuda(args.gpu_model)
          output = model(image)
          
          output = nn.functional.softmax(output, dim=1)
          output = nn.functional.interpolate(output, (1024, 2048), mode='bilinear', align_corners=True).cpu().data[0].numpy()
          output = output.transpose(1,2,0)
          output_nomask = np.asarray(np.argmax(output, axis=2), dtype=np.uint8)
          output_col = colorize_mask(output_nomask)
          output_nomask = Image.fromarray(output_nomask)    
          name = name[0].split('/')[-1]
          output_nomask.save('%s/%s' % (args.save, name))
          output_col.save('%s/%s_color.png' % (args.save, name.split('.')[0])) 
          
      compute_mIoU(args.gt_dir, args.save, args.devkit_dir, args.restore_from)    
  
  if __name__ == '__main__':
      
      os.system('nvidia-smi -q -d Memory |grep -A4 GPU|grep Free >tmp')
      memory_gpu=[int(x.split()[2]) for x in open('tmp','r').readlines()]
      os.system('rm tmp')    
      os.environ["CUDA_VISIBLE_DEVICES"] = str(np.argmax(memory_gpu))  
      
      main()