-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathdata_utils.py
121 lines (103 loc) · 4.1 KB
/
data_utils.py
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
"""Utility functions for real NVP.
"""
import torch
import torch.nn.functional as F
import torch.distributions as distributions
import torch.utils.data as data
import torchvision.datasets as datasets
import torchvision.transforms as transforms
import numpy as np
class DataInfo():
def __init__(self, name, channel, size):
"""Instantiates a DataInfo.
Args:
name: name of dataset.
channel: number of image channels.
size: height and width of an image.
"""
self.name = name
self.channel = channel
self.size = size
def load(dataset):
"""Load dataset.
Args:
dataset: name of dataset.
Returns:
a torch dataset and its associated information.
"""
if dataset == 'cifar10': # 3 x 32 x 32
data_info = DataInfo(dataset, 3, 32)
transform = transforms.Compose(
[transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor()])
train_set = datasets.CIFAR10('../../data/CIFAR10',
train=True, download=True, transform=transform)
[train_split, val_split] = data.random_split(train_set, [46000, 4000])
elif dataset == 'celeba': # 3 x 218 x 178
data_info = DataInfo(dataset, 3, 64)
def CelebACrop(images):
return transforms.functional.crop(images, 40, 15, 148, 148)
transform = transforms.Compose(
[CelebACrop,
transforms.Resize(64),
transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor()])
train_set = datasets.ImageFolder('../../data/CelebA/train',
transform=transform)
[train_split, val_split] = data.random_split(train_set, [150000, 12770])
elif dataset == 'imnet32':
data_info = DataInfo(dataset, 3, 32)
transform = transforms.Compose(
[transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor()])
train_set = datasets.ImageFolder('../../data/ImageNet32/train',
transform=transform)
[train_split, val_split] = data.random_split(train_set, [1250000, 31149])
elif dataset == 'imnet64':
data_info = DataInfo(dataset, 3, 64)
transform = transforms.Compose(
[transforms.RandomHorizontalFlip(p=0.5),
transforms.ToTensor()])
train_set = datasets.ImageFolder('../../data/ImageNet64/train',
transform=transform)
[train_split, val_split] = data.random_split(train_set, [1250000, 31149])
return train_split, val_split, data_info
def logit_transform(x, constraint=0.9, reverse=False):
'''Transforms data from [0, 1] into unbounded space.
Restricts data into [0.05, 0.95].
Calculates logit(alpha+(1-alpha)*x).
Args:
x: input tensor.
constraint: data constraint before logit.
reverse: True if transform data back to [0, 1].
Returns:
transformed tensor and log-determinant of Jacobian from the transform.
(if reverse=True, no log-determinant is returned.)
'''
if reverse:
x = 1. / (torch.exp(-x) + 1.) # [0.05, 0.95]
x *= 2. # [0.1, 1.9]
x -= 1. # [-0.9, 0.9]
x /= constraint # [-1, 1]
x += 1. # [0, 2]
x /= 2. # [0, 1]
return x, 0
else:
[B, C, H, W] = list(x.size())
# dequantization
noise = distributions.Uniform(0., 1.).sample((B, C, H, W))
x = (x * 255. + noise) / 256.
# restrict data
x *= 2. # [0, 2]
x -= 1. # [-1, 1]
x *= constraint # [-0.9, 0.9]
x += 1. # [0.1, 1.9]
x /= 2. # [0.05, 0.95]
# logit data
logit_x = torch.log(x) - torch.log(1. - x)
# log-determinant of Jacobian from the transform
pre_logit_scale = torch.tensor(
np.log(constraint) - np.log(1. - constraint))
log_diag_J = F.softplus(logit_x) + F.softplus(-logit_x) \
- F.softplus(-pre_logit_scale)
return logit_x, torch.sum(log_diag_J, dim=(1, 2, 3))