-
Notifications
You must be signed in to change notification settings - Fork 238
/
Copy pathlandscape_utils.py
132 lines (103 loc) · 3.46 KB
/
landscape_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
122
123
124
125
126
127
128
129
130
131
132
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2023 Apple Inc. All Rights Reserved.
#
import copy
from typing import Dict, Optional
import matplotlib.pyplot as plt
import numpy as np
import torch
from matplotlib import animation, cm
from mpl_toolkits.mplot3d import Axes3D
from utils.common_utils import unwrap_model_fn
# https://github.com/xxxnell/how-do-vits-work
def rand_basis(ws: Dict, device: Optional[str] = torch.device("cpu")):
return {k: torch.randn(size=v.shape, device=device) for k, v in ws.items()}
def normalize_filter(bs: Dict, ws: Dict):
bs = {k: v.float() for k, v in bs.items()}
ws = {k: v.float() for k, v in ws.items()}
norm_bs = {}
for k in bs:
ws_norm = torch.norm(ws[k], dim=0, keepdim=True)
bs_norm = torch.norm(bs[k], dim=0, keepdim=True)
norm_bs[k] = ws_norm / (bs_norm + 1e-7) * bs[k]
return norm_bs
def ignore_bn(ws: Dict):
ignored_ws = {}
for k in ws:
if len(ws[k].size()) < 2:
ignored_ws[k] = torch.zeros(size=ws[k].size(), device=ws[k].device)
else:
ignored_ws[k] = ws[k]
return ignored_ws
def create_bases(
model: torch.nn.Module,
device: Optional[str] = torch.device("cpu"),
has_module: Optional[bool] = False,
):
unwrapped_model = unwrap_model_fn(model)
weight_state_0 = unwrapped_model.state_dict()
bases = [rand_basis(weight_state_0, device) for _ in range(2)] # Use two bases
bases = [normalize_filter(bs, weight_state_0) for bs in bases]
bases = [ignore_bn(bs) for bs in bases]
return bases
def generate_plots(xx, yy, zz, model_name, results_loc):
zz = np.log(zz)
plt.figure(figsize=(10, 10))
plt.contour(xx, yy, zz)
plt.savefig(f"{results_loc}/{model_name}_log_contour.png", dpi=100)
plt.close()
## 3D plot
fig, ax = plt.subplots(subplot_kw={"projection": "3d"})
ax.set_axis_off()
surf = ax.plot_surface(xx, yy, zz, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
plt.savefig(
f"{results_loc}/{model_name}_log_surface.png",
dpi=100,
format="png",
bbox_inches="tight",
)
plt.close()
fig = plt.figure(figsize=(10, 10))
ax = Axes3D(fig)
ax.set_axis_off()
def init():
ax.plot_surface(xx, yy, zz, cmap=cm.coolwarm, linewidth=0, antialiased=False)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
return (fig,)
def animate(i):
ax.view_init(elev=(15 * (i // 15) + i % 15) + 0.0, azim=i)
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
return (fig,)
anim = animation.FuncAnimation(
fig, animate, init_func=init, frames=100, interval=20, blit=True
)
anim.save(
f"{results_loc}/{model_name}_log_surface.gif", fps=15, writer="imagemagick"
)
def plot_save_graphs(
save_dir: str,
model_name: str,
grid_a: np.ndarray,
grid_b: np.ndarray,
loss_surface: np.ndarray,
resolution: int,
):
np.save(f"{save_dir}/{model_name}_xx.npy", grid_a)
np.save(f"{save_dir}/{model_name}_yy.npy", grid_b)
np.save(f"{save_dir}/{model_name}_zz.npy", loss_surface)
plt.figure(figsize=(10, 10))
plt.contour(grid_a, grid_b, loss_surface)
plt.savefig(f"{save_dir}/{model_name}_contour_res_{resolution}.png", dpi=100)
plt.close()
generate_plots(
xx=grid_a,
yy=grid_b,
zz=loss_surface,
model_name=model_name,
results_loc=save_dir,
)