-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
146 lines (123 loc) · 4.2 KB
/
Copy pathutils.py
File metadata and controls
146 lines (123 loc) · 4.2 KB
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
from torchvision.datasets import MNIST
from torchvision import transforms
import torch
import torch.nn as nn
def save_model(save_path,
model,
optimizer=None,
replay_buffer=None,
discriminator=None,
optimizer_d=None):
save_dict = {'model': model.state_dict()}
if optimizer is not None:
save_dict['optimizer'] = optimizer.state_dict()
if replay_buffer is not None:
save_dict['replay_buffer'] = replay_buffer
if discriminator is not None:
save_dict['discriminator'] = discriminator
if optimizer_d is not None:
save_dict['optimizer_d'] = optimizer_d
torch.save(save_dict, save_path)
def load_model(load_path):
checkpoint = torch.load(load_path)
return (
checkpoint['model'],
checkpoint.get('optimizer', None),
checkpoint.get('replay_buffer', None),
checkpoint.get('discriminator', None),
checkpoint.get('optimizer_d', None),
)
def rescale(x, lo, hi):
"""Rescale a tensor to [lo,hi]."""
assert (lo < hi), f"[rescale] lo={lo} must be smaller than hi={hi}"
old_width = torch.max(x) - torch.min(x)
old_center = torch.min(x) + (old_width / 2.)
new_width = float(hi - lo)
new_center = lo + (new_width / 2.)
# shift everything back to zero:
x = x - old_center
# rescale to correct width:
x = x * (new_width / old_width)
# shift everything to the new center:
x = x + new_center
# return:
return x
def corruption(x, type_='ebm', noise_scale=0.3):
assert type_ in ['ebm', 'flow']
# mask=1 if the pixel is visible
mask = torch.zeros_like(x)
if type_ == 'ebm':
# Corrupt the rows 0, 2, 4, ....
mask[..., torch.arange(0, mask.shape[-2], step=2), :] = 1
elif type_ == 'flow':
# Corrupt the lower part
mask[..., :mask.shape[-2] // 2, :] = 1
broken_data = x * mask + (1 - mask) * noise_scale * torch.randn_like(x)
broken_data = torch.clip(broken_data, 1e-4, 1 - 1e-4)
return broken_data, mask
# In your module (or another file), define top-level functions:
def add_uniform_noise(x):
return x + torch.rand_like(x).div(256.)
def rescale_tensor(x):
# Assuming `rescale` is imported from util and works as intended.
return rescale(x, 0.0001, 0.9999)
# Then build the transform using these functions:
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Lambda(add_uniform_noise),
transforms.Lambda(rescale_tensor),
])
# image tensor range [0, 1]
train_set = MNIST(
root="./data",
download=True,
transform=transform,
train=True,
)
val_set = MNIST(
root="./data",
download=True,
transform=transform,
train=False,
)
class MockResidualBlock(nn.Module):
""" A simplified residual block used in RealNVP.
Image feature shape is assumed to be unchanged.
"""
def __init__(self,
c_in,
c_out,
kernel_size=3,
stride=1,
padding=1,
groups=1,
output_activation=True):
super(MockResidualBlock, self).__init__()
self.conv = nn.Sequential(
nn.Conv2d(c_in,
c_out,
kernel_size,
stride,
padding,
groups=groups,
bias=False), nn.BatchNorm2d(c_out),
nn.ReLU(inplace=True),
nn.Conv2d(c_out,
c_out,
kernel_size,
stride,
padding,
groups=groups,
bias=False), nn.BatchNorm2d(c_out))
self.skip_connection = nn.Identity()
if c_in != c_out:
self.skip_connection = nn.Sequential(nn.Conv2d(c_in, c_out, 1, 1),
nn.BatchNorm2d(c_out))
self.output_activation = output_activation
def forward(self, x):
if self.output_activation:
return F.relu(self.conv(x) + self.skip_connection(x), inplace=True)
else:
return self.conv(x) + self.skip_connection(x)
def hello():
print('Good luck!')