forked from dome272/VQGAN-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpips.py
138 lines (110 loc) · 4.48 KB
/
lpips.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import os
import torch
import torch.nn as nn
from torchvision.models import vgg16
from collections import namedtuple
import requests
from tqdm import tqdm
URL_MAP = {
"vgg_lpips": "https://heibox.uni-heidelberg.de/f/607503859c864bc1b30b/?dl=1"
}
CKPT_MAP = {
"vgg_lpips": "vgg.pth"
}
def download(url, local_path, chunk_size=1024):
os.makedirs(os.path.split(local_path)[0], exist_ok=True)
with requests.get(url, stream=True) as r:
total_size = int(r.headers.get("content-length", 0))
with tqdm(total=total_size, unit="B", unit_scale=True) as pbar:
with open(local_path, "wb") as f:
for data in r.iter_content(chunk_size=chunk_size):
if data:
f.write(data)
pbar.update(chunk_size)
def get_ckpt_path(name, root):
assert name in URL_MAP
path = os.path.join(root, CKPT_MAP[name])
if not os.path.exists(path):
print(f"Downloading {name} model from {URL_MAP[name]} to {path}")
download(URL_MAP[name], path)
return path
class LPIPS(nn.Module):
def __init__(self):
super(LPIPS, self).__init__()
self.scaling_layer = ScalingLayer()
self.channels = [64, 128, 256, 512, 512]
self.vgg = VGG16()
self.lins = nn.ModuleList([
NetLinLayer(self.channels[0]),
NetLinLayer(self.channels[1]),
NetLinLayer(self.channels[2]),
NetLinLayer(self.channels[3]),
NetLinLayer(self.channels[4])
])
self.load_from_pretrained()
for param in self.parameters():
param.requires_grad = False
def load_from_pretrained(self, name="vgg_lpips"):
ckpt = get_ckpt_path(name, "vgg_lpips")
self.load_state_dict(torch.load(ckpt, map_location=torch.device("cpu")), strict=False)
def forward(self, real_x, fake_x):
features_real = self.vgg(self.scaling_layer(real_x))
features_fake = self.vgg(self.scaling_layer(fake_x))
diffs = {}
for i in range(len(self.channels)):
diffs[i] = (norm_tensor(features_real[i]) - norm_tensor(features_fake[i])) ** 2
return sum([spatial_average(self.lins[i].model(diffs[i])) for i in range(len(self.channels))])
class ScalingLayer(nn.Module):
def __init__(self):
super(ScalingLayer, self).__init__()
self.register_buffer("shift", torch.Tensor([-.030, -.088, -.188])[None, :, None, None])
self.register_buffer("scale", torch.Tensor([.458, .448, .450])[None, :, None, None])
def forward(self, x):
return (x - self.shift) / self.scale
class NetLinLayer(nn.Module):
def __init__(self, in_channels, out_channels=1):
super(NetLinLayer, self).__init__()
self.model = nn.Sequential(
nn.Dropout(),
nn.Conv2d(in_channels, out_channels, 1, 1, 0, bias=False)
)
class VGG16(nn.Module):
def __init__(self):
super(VGG16, self).__init__()
vgg_pretrained_features = vgg16(pretrained=True).features
slices = [vgg_pretrained_features[i] for i in range(30)]
self.slice1 = nn.Sequential(*slices[0:4])
self.slice2 = nn.Sequential(*slices[4:9])
self.slice3 = nn.Sequential(*slices[9:16])
self.slice4 = nn.Sequential(*slices[16:23])
self.slice5 = nn.Sequential(*slices[23:30])
for param in self.parameters():
param.requires_grad = False
def forward(self, x):
h = self.slice1(x)
h_relu1 = h
h = self.slice2(h)
h_relu2 = h
h = self.slice3(h)
h_relu3 = h
h = self.slice4(h)
h_relu4 = h
h = self.slice5(h)
h_relu5 = h
vgg_outputs = namedtuple("VGGOutputs", ['relu1_2', 'relu2_2', 'relu3_3', 'relu4_3', 'relu5_3'])
return vgg_outputs(h_relu1, h_relu2, h_relu3, h_relu4, h_relu5)
def norm_tensor(x):
"""
Normalize images by their length to make them unit vector?
:param x: batch of images
:return: normalized batch of images
"""
norm_factor = torch.sqrt(torch.sum(x**2, dim=1, keepdim=True))
return x / (norm_factor + 1e-10)
def spatial_average(x):
"""
imgs have: batch_size x channels x width x height --> average over width and height channel
:param x: batch of images
:return: averaged images along width and height
"""
return x.mean([2, 3], keepdim=True)