Spaces:
Runtime error
Runtime error
File size: 11,254 Bytes
9acea67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
"""This module contains simple helper functions """
from __future__ import print_function
import torch
import numpy as np
from PIL import Image
import os
import torch.nn.functional as F
from torch.autograd import Variable
def random_word(len_word, alphabet):
# generate a word constructed from len_word characters where each character is randomly chosen from the alphabet.
char = np.random.randint(low=0, high=len(alphabet), size=len_word)
word = [alphabet[c] for c in char]
return ''.join(word)
def load_network(net, save_dir, epoch):
"""Load all the networks from the disk.
Parameters:
epoch (int) -- current epoch; used in the file name '%s_net_%s.pth' % (epoch, name)
"""
load_filename = '%s_net_%s.pth' % (epoch, net.name)
load_path = os.path.join(save_dir, load_filename)
# if you are using PyTorch newer than 0.4 (e.g., built from
# GitHub source), you can remove str() on self.device
state_dict = torch.load(load_path)
if hasattr(state_dict, '_metadata'):
del state_dict._metadata
net.load_state_dict(state_dict)
return net
def writeCache(env, cache):
with env.begin(write=True) as txn:
for k, v in cache.items():
if type(k) == str:
k = k.encode()
if type(v) == str:
v = v.encode()
txn.put(k, v)
def loadData(v, data):
with torch.no_grad():
v.resize_(data.size()).copy_(data)
def multiple_replace(string, rep_dict):
for key in rep_dict.keys():
string = string.replace(key, rep_dict[key])
return string
def get_curr_data(data, batch_size, counter):
curr_data = {}
for key in data:
curr_data[key] = data[key][batch_size*counter:batch_size*(counter+1)]
return curr_data
# Utility file to seed rngs
def seed_rng(seed):
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
# turn tensor of classes to tensor of one hot tensors:
def make_one_hot(labels, len_labels, n_classes):
one_hot = torch.zeros((labels.shape[0], labels.shape[1], n_classes),dtype=torch.float32)
for i in range(len(labels)):
one_hot[i,np.array(range(len_labels[i])), labels[i,:len_labels[i]]-1]=1
return one_hot
# Hinge Loss
def loss_hinge_dis(dis_fake, dis_real, len_text_fake, len_text, mask_loss):
mask_real = torch.ones(dis_real.shape).to(dis_real.device)
mask_fake = torch.ones(dis_fake.shape).to(dis_fake.device)
if mask_loss and len(dis_fake.shape)>2:
for i in range(len(len_text)):
mask_real[i, :, :, len_text[i]:] = 0
mask_fake[i, :, :, len_text_fake[i]:] = 0
loss_real = torch.sum(F.relu(1. - dis_real * mask_real))/torch.sum(mask_real)
loss_fake = torch.sum(F.relu(1. + dis_fake * mask_fake))/torch.sum(mask_fake)
return loss_real, loss_fake
def loss_hinge_gen(dis_fake, len_text_fake, mask_loss):
mask_fake = torch.ones(dis_fake.shape).to(dis_fake.device)
if mask_loss and len(dis_fake.shape)>2:
for i in range(len(len_text_fake)):
mask_fake[i, :, :, len_text_fake[i]:] = 0
loss = -torch.sum(dis_fake*mask_fake)/torch.sum(mask_fake)
return loss
def loss_std(z, lengths, mask_loss):
loss_std = torch.zeros(1).to(z.device)
z_mean = torch.ones((z.shape[0], z.shape[1])).to(z.device)
for i in range(len(lengths)):
if mask_loss:
if lengths[i]>1:
loss_std += torch.mean(torch.std(z[i, :, :, :lengths[i]], 2))
z_mean[i,:] = torch.mean(z[i, :, :, :lengths[i]], 2).squeeze(1)
else:
z_mean[i, :] = z[i, :, :, 0].squeeze(1)
else:
loss_std += torch.mean(torch.std(z[i, :, :, :], 2))
z_mean[i,:] = torch.mean(z[i, :, :, :], 2).squeeze(1)
loss_std = loss_std/z.shape[0]
return loss_std, z_mean
# Convenience utility to switch off requires_grad
def toggle_grad(model, on_or_off):
for param in model.parameters():
param.requires_grad = on_or_off
# Apply modified ortho reg to a model
# This function is an optimized version that directly computes the gradient,
# instead of computing and then differentiating the loss.
def ortho(model, strength=1e-4, blacklist=[]):
with torch.no_grad():
for param in model.parameters():
# Only apply this to parameters with at least 2 axes, and not in the blacklist
if len(param.shape) < 2 or any([param is item for item in blacklist]):
continue
w = param.view(param.shape[0], -1)
grad = (2 * torch.mm(torch.mm(w, w.t())
* (1. - torch.eye(w.shape[0], device=w.device)), w))
param.grad.data += strength * grad.view(param.shape)
# Default ortho reg
# This function is an optimized version that directly computes the gradient,
# instead of computing and then differentiating the loss.
def default_ortho(model, strength=1e-4, blacklist=[]):
with torch.no_grad():
for param in model.parameters():
# Only apply this to parameters with at least 2 axes & not in blacklist
if len(param.shape) < 2 or param in blacklist:
continue
w = param.view(param.shape[0], -1)
grad = (2 * torch.mm(torch.mm(w, w.t())
- torch.eye(w.shape[0], device=w.device), w))
param.grad.data += strength * grad.view(param.shape)
# Convenience utility to switch off requires_grad
def toggle_grad(model, on_or_off):
for param in model.parameters():
param.requires_grad = on_or_off
# A highly simplified convenience class for sampling from distributions
# One could also use PyTorch's inbuilt distributions package.
# Note that this class requires initialization to proceed as
# x = Distribution(torch.randn(size))
# x.init_distribution(dist_type, **dist_kwargs)
# x = x.to(device,dtype)
# This is partially based on https://discuss.pytorch.org/t/subclassing-torch-tensor/23754/2
class Distribution(torch.Tensor):
# Init the params of the distribution
def init_distribution(self, dist_type, **kwargs):
seed_rng(kwargs['seed'])
self.dist_type = dist_type
self.dist_kwargs = kwargs
if self.dist_type == 'normal':
self.mean, self.var = kwargs['mean'], kwargs['var']
elif self.dist_type == 'categorical':
self.num_categories = kwargs['num_categories']
elif self.dist_type == 'poisson':
self.lam = kwargs['var']
elif self.dist_type == 'gamma':
self.scale = kwargs['var']
def sample_(self):
if self.dist_type == 'normal':
self.normal_(self.mean, self.var)
elif self.dist_type == 'categorical':
self.random_(0, self.num_categories)
elif self.dist_type == 'poisson':
type = self.type()
device = self.device
data = np.random.poisson(self.lam, self.size())
self.data = torch.from_numpy(data).type(type).to(device)
elif self.dist_type == 'gamma':
type = self.type()
device = self.device
data = np.random.gamma(shape=1, scale=self.scale, size=self.size())
self.data = torch.from_numpy(data).type(type).to(device)
# return self.variable
# Silly hack: overwrite the to() method to wrap the new object
# in a distribution as well
def to(self, *args, **kwargs):
new_obj = Distribution(self)
new_obj.init_distribution(self.dist_type, **self.dist_kwargs)
new_obj.data = super().to(*args, **kwargs)
return new_obj
def to_device(net, gpu_ids):
if len(gpu_ids) > 0:
assert(torch.cuda.is_available())
net.to(gpu_ids[0])
# net = torch.nn.DataParallel(net, gpu_ids) # multi-GPUs
if len(gpu_ids)>1:
net = torch.nn.DataParallel(net, device_ids=gpu_ids).cuda()
# net = torch.nn.DistributedDataParallel(net)
return net
# Convenience function to prepare a z and y vector
def prepare_z_y(G_batch_size, dim_z, nclasses, device='cuda',
fp16=False, z_var=1.0, z_dist='normal', seed=0):
z_ = Distribution(torch.randn(G_batch_size, dim_z, requires_grad=False))
z_.init_distribution(z_dist, mean=0, var=z_var, seed=seed)
z_ = z_.to(device, torch.float16 if fp16 else torch.float32)
if fp16:
z_ = z_.half()
y_ = Distribution(torch.zeros(G_batch_size, requires_grad=False))
y_.init_distribution('categorical', num_categories=nclasses, seed=seed)
y_ = y_.to(device, torch.int64)
return z_, y_
def tensor2im(input_image, imtype=np.uint8):
""""Converts a Tensor array into a numpy image array.
Parameters:
input_image (tensor) -- the input image tensor array
imtype (type) -- the desired type of the converted numpy array
"""
if not isinstance(input_image, np.ndarray):
if isinstance(input_image, torch.Tensor): # get the data from a variable
image_tensor = input_image.data
else:
return input_image
image_numpy = image_tensor[0].cpu().float().numpy() # convert it into a numpy array
if image_numpy.shape[0] == 1: # grayscale to RGB
image_numpy = np.tile(image_numpy, (3, 1, 1))
image_numpy = (np.transpose(image_numpy, (1, 2, 0)) + 1) / 2.0 * 255.0 # post-processing: tranpose and scaling
else: # if it is a numpy array, do nothing
image_numpy = input_image
return image_numpy.astype(imtype)
def diagnose_network(net, name='network'):
"""Calculate and print the mean of average absolute(gradients)
Parameters:
net (torch network) -- Torch network
name (str) -- the name of the network
"""
mean = 0.0
count = 0
for param in net.parameters():
if param.grad is not None:
mean += torch.mean(torch.abs(param.grad.data))
count += 1
if count > 0:
mean = mean / count
print(name)
print(mean)
def save_image(image_numpy, image_path):
"""Save a numpy image to the disk
Parameters:
image_numpy (numpy array) -- input numpy array
image_path (str) -- the path of the image
"""
image_pil = Image.fromarray(image_numpy)
image_pil.save(image_path)
def print_numpy(x, val=True, shp=False):
"""Print the mean, min, max, median, std, and size of a numpy array
Parameters:
val (bool) -- if print the values of the numpy array
shp (bool) -- if print the shape of the numpy array
"""
x = x.astype(np.float64)
if shp:
print('shape,', x.shape)
if val:
x = x.flatten()
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)))
def mkdirs(paths):
"""create empty directories if they don't exist
Parameters:
paths (str list) -- a list of directory paths
"""
if isinstance(paths, list) and not isinstance(paths, str):
for path in paths:
mkdir(path)
else:
mkdir(paths)
def mkdir(path):
"""create a single empty directory if it didn't exist
Parameters:
path (str) -- a single directory path
"""
if not os.path.exists(path):
os.makedirs(path)
|