MLBench/ReaLens
0
1"""This module contains simple helper functions"""2 3from __future__ import print_function4import torch5import numpy as np6from PIL import Image7from pathlib import Path8import torch.distributed as dist9import os10 11 12def tensor2im(input_image, imtype=np.uint8):13 """ "Converts a Tensor array into a numpy image array.14 15 Parameters:16 input_image (tensor) -- the input image tensor array17 imtype (type) -- the desired type of the converted numpy array18 """19 if not isinstance(input_image, np.ndarray):20 if isinstance(input_image, torch.Tensor): # get the data from a variable21 image_tensor = input_image.data22 else:23 return input_image24 image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array25 if image_numpy.shape[0] == 1: # grayscale to RGB26 image_numpy = np.tile(image_numpy, (3, 1, 1))27 image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling28 else: # if it is a numpy array, do nothing29 image_numpy = input_image30 return image_numpy.astype(imtype)31 32 33def diagnose_network(net, name="network"):34 """Calculate and print the mean of average absolute(gradients)35 36 Parameters:37 net (torch network) -- Torch network38 name (str) -- the name of the network39 """40 mean = 0.041 count = 042 for param in net.parameters():43 if param.grad is not None:44 mean += torch.mean(torch.abs(param.grad.data))45 count += 146 if count > 0:47 mean = mean / count48 print(name)49 print(mean)50 51 52# initialize ddp53def init_ddp():54 # Initialize DDP if LOCAL_RANK is set55 is_ddp = "WORLD_SIZE" in os.environ and int(os.environ["WORLD_SIZE"]) > 156 57 if is_ddp:58 if not dist.is_initialized():59 dist.init_process_group(backend="nccl")60 local_rank = int(os.environ["LOCAL_RANK"])61 device = torch.device(f"cuda:{local_rank}")62 torch.cuda.set_device(local_rank)63 elif torch.cuda.is_available():64 device = torch.device("cuda:0")65 torch.cuda.set_device(0)66 else:67 device = torch.device("cpu")68 print(f"Initialized with device {device}")69 return device70 71 72# cleanup ddp73def cleanup_ddp():74 if dist.is_initialized():75 dist.destroy_process_group()76 77 78def save_image(image_numpy, image_path, aspect_ratio=1.0):79 """Save a numpy image to the disk80 81 Parameters:82 image_numpy (numpy array) -- input numpy array83 image_path (str) -- the path of the image84 """85 86 image_pil = Image.fromarray(image_numpy)87 h, w, _ = image_numpy.shape88 89 if aspect_ratio > 1.0:90 image_pil = image_pil.resize((h, int(w * aspect_ratio)), Image.BICUBIC)91 if aspect_ratio < 1.0:92 image_pil = image_pil.resize((int(h / aspect_ratio), w), Image.BICUBIC)93 image_pil.save(image_path)94 95 96def print_numpy(x, val=True, shp=False):97 """Print the mean, min, max, median, std, and size of a numpy array98 99 Parameters:100 val (bool) -- if print the values of the numpy array101 shp (bool) -- if print the shape of the numpy array102 """103 x = x.astype(np.float64)104 if shp:105 print("shape,", x.shape)106 if val:107 x = x.flatten()108 print("mean = %3.3f, min = %3.3f, max = %3.3f, median = %3.3f, std=%3.3f" % (np.mean(x), np.min(x), np.max(x), np.median(x), np.std(x)))109 110 111def mkdirs(paths):112 """create empty directories if they don't exist113 114 Parameters:115 paths (str list) -- a list of directory paths116 """117 if isinstance(paths, list) and not isinstance(paths, str):118 for path in paths:119 mkdir(path)120 else:121 mkdir(paths)122 123 124def mkdir(path):125 """create a single empty directory if it didn't exist126 127 Parameters:128 path (str) -- a single directory path129 """130 Path(path).mkdir(parents=True, exist_ok=True)131 