-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.py
61 lines (43 loc) · 1.72 KB
/
util.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
import math
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import torch
matplotlib.use("agg")
def cycle(iterable):
while True:
for item in iterable:
yield item
def kernel_images(W, kernel_size, image_channels, rows=None, cols=None, spacing=1):
"""
Return the kernels as tiled images for visualization
:return: np.ndarray, shape = [rows * (kernel_size + spacing) - spacing, cols * (kernel_size + spacing) - spacing, 1]
"""
W /= np.linalg.norm(W, axis=0, keepdims=True)
W = W.reshape(image_channels, -1, W.shape[-1])
if rows is None:
rows = int(np.ceil(math.sqrt(W.shape[-1])))
if cols is None:
cols = int(np.ceil(W.shape[-1] / rows))
kernels = np.ones([3, rows * (kernel_size + spacing) - spacing, cols * (kernel_size + spacing) - spacing], dtype=np.float32)
coords = [(i, j) for i in range(rows) for j in range(cols)]
Wt = W.transpose(2, 0, 1)
for (i, j), weight in zip(coords, Wt):
kernel = weight.reshape(image_channels, kernel_size, kernel_size) * 2 + 0.5
x = i * (kernel_size + spacing)
y = j * (kernel_size + spacing)
kernels[:, x:x+kernel_size, y:y+kernel_size] = kernel
return kernels.clip(0, 1)
def plot_convolution(weight: torch.Tensor):
if torch.is_tensor(weight):
weight = weight.numpy()
weight = weight / np.linalg.norm(weight, axis=-1, keepdims=True)
fig = plt.figure(figsize=(4, 4))
plt.plot(weight[:, 0, :].T)
plt.tight_layout()
fig.canvas.draw()
buf = np.frombuffer(fig.canvas.tostring_rgb(), dtype=np.uint8)
ncol, nrow = fig.canvas.get_width_height()
buf = buf.reshape(ncol, nrow, 3)
plt.close()
return buf.transpose(2, 0, 1)