-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfourier_visualized.py
171 lines (133 loc) · 5.08 KB
/
fourier_visualized.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
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
from torchvision.transforms import transforms
from PIL import Image
import copy
import timm
import numpy as np
import torch
import torch.nn as nn
import math
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from matplotlib.collections import LineCollection
from PIL import Image
from einops import rearrange, reduce, repeat
name = "vit_tiny_patch16_224"
model = timm.create_model(name, pretrained=True)
class PatchEmbed(nn.Module):
def __init__(self, model):
super().__init__()
self.model = copy.deepcopy(model)
def forward(self, x, **kwargs):
x = self.model.patch_embed(x)
cls_token = self.model.cls_token.expand(x.shape[0], -1, -1)
x = torch.cat((cls_token, x), dim=1)
x = self.model.pos_drop(x + self.model.pos_embed)
return x
class Residual(nn.Module):
def __init__(self, *fn):
super().__init__()
self.fn = nn.Sequential(*fn)
def forward(self, x, **kwargs):
return self.fn(x, **kwargs) + x
class Lambda(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x):
return self.fn(x)
def flatten(xs_list):
return [x for xs in xs_list for x in xs]
blocks = [
PatchEmbed(model),
*flatten([[Residual(b.norm1, b.attn), Residual(b.norm2, b.mlp)]
for b in model.blocks]),
nn.Sequential(model.norm, Lambda(lambda x: x[:, 0]), model.head),
]
normalize = transforms.Normalize(mean=[0.5, 0.5, 0.5],
std=[0.5, 0.5, 0.5])
trans_aug = transforms.Compose([
transforms.Resize((224, 224)),
transforms.ToTensor(),
normalize,
])
imgs = Image.open("pngs/dog.png")
xs = trans_aug(imgs).unsqueeze(0)
latents = []
with torch.no_grad():
for block in blocks:
xs = block(xs)
latents.append(xs)
latents = [latent[:,1:] for latent in latents]
latents = latents[:-1] # drop logit (output)
def fourier(x): # 2D Fourier transform
f = torch.fft.fft2(x)
f = f.abs() + 1e-6
f = f.log()
return f
def shift(x): # shift Fourier transformed feature map
b, c, h, w = x.shape
return torch.roll(x, shifts=(int(h/2), int(w/2)), dims=(2, 3))
def make_segments(x, y): # make segment for `plot_segment`
points = np.array([x, y]).T.reshape(-1, 1, 2)
segments = np.concatenate([points[:-1], points[1:]], axis=1)
return segments
def plot_segment(ax, xs, ys, cmap_name="plasma"): # plot with cmap segments
z = np.linspace(0.0, 1.0, len(ys))
z = np.asarray(z)
cmap = cm.get_cmap(cmap_name)
norm = plt.Normalize(0.0, 1.0)
segments = make_segments(xs, ys)
lc = LineCollection(segments, array=z, cmap=cmap_name, norm=norm,
linewidth=2.5, alpha=1.0)
ax.add_collection(lc)
colors = [cmap(x) for x in xs]
ax.scatter(xs, ys, color=colors, marker=marker, zorder=100)
fourier_latents = []
for latent in latents: # `latents` is a list of hidden feature maps in latent spaces
latent = latent.cpu()
if len(latent.shape) == 3: # for ViT
b, n, c = latent.shape
h, w = int(math.sqrt(n)), int(math.sqrt(n))
latent = rearrange(latent, "b (h w) c -> b c h w", h=h, w=w)
elif len(latent.shape) == 4: # for CNN
b, c, h, w = latent.shape
else:
raise Exception("shape: %s" % str(latent.shape))
latent = fourier(latent)
latent = shift(latent).mean(dim=(0, 1))
latent = latent.diag()[int(h/2):] # only use the half-diagonal components
latent = latent - latent[0] # visualize 'relative' log amplitudes
# (i.e., low-freq amp - high freq amp)
fourier_latents.append(latent)
fig, ax1 = plt.subplots(1, 1, figsize=(3.3, 4), dpi=150)
for i, latent in enumerate(reversed(fourier_latents[:-1])):
freq = np.linspace(0, 1, len(latent))
ax1.plot(freq, latent, color=cm.plasma_r(i / len(fourier_latents)))
ax1.set_xlim(left=0, right=1)
ax1.set_xlabel("Frequency")
ax1.set_ylabel("$\Delta$ Log amplitude")
from matplotlib.ticker import FormatStrFormatter
ax1.yaxis.set_major_formatter(FormatStrFormatter('%.1f'))
ax1.xaxis.set_major_formatter(FormatStrFormatter('%.1fπ'))
if name == "vit_tiny_patch16_224": # for ViT-Ti
pools = []
msas = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23,]
marker = "o"
depths = range(len(fourier_latents))
# Normalize
depth = len(depths) - 1
depths = (np.array(depths)) / depth
pools = (np.array(pools)) / depth
msas = (np.array(msas)) / depth
fig, ax2 = plt.subplots(1, 1, figsize=(6.5, 4), dpi=120)
plot_segment(ax2, depths, [latent[-1] for latent in fourier_latents]) # high-frequency component
for pool in pools:
ax2.axvspan(pool - 1.0 / depth, pool + 0.0 / depth, color="tab:blue", alpha=0.15, lw=0)
for msa in msas:
ax2.axvspan(msa - 1.0 / depth, msa + 0.0 / depth, color="tab:gray", alpha=0.15, lw=0)
ax2.set_xlabel("Normalized depth")
ax2.set_ylabel("$\Delta$ Log amplitude")
ax2.set_xlim(0.0, 1.0)
from matplotlib.ticker import FormatStrFormatter
ax2.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
plt.savefig('fourier_amplitude.png')