-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVisualize.py
49 lines (36 loc) · 1.33 KB
/
Visualize.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
import torch
import cv2
from tqdm import tqdm
import numpy as np
import torch.optim
from tensorboardX import SummaryWriter
import torchvision
from PIL import Image
writer = SummaryWriter(comment='visual')
feature_result = None
def feature_hoook(layer, data_input, data_output):
global feature_result
feature_result = data_output
vgg16 = torchvision.models.vgg16(pretrained=True).cuda().eval() # Test NetWork : Vgg16
vgg16.features[24].register_forward_hook(feature_hoook)
def visiual(sz=56, nthfeaturemap=1):
img = np.uint8(np.random.uniform(0, 250, (sz, sz, 3))) / 255
for i in range(15):
img = torch.tensor(img.transpose((2, 0, 1))).unsqueeze(0).to(torch.float32)
img = img.cuda()
img.requires_grad = True
optim = torch.optim.Adam([img], lr=0.1, weight_decay=1e-6) # 这里参数需要是Tensors,使用列表代替
for n in range(12):
optim.zero_grad()
vgg16(img)
loss = -1 * feature_result[0, nthfeaturemap].mean()
loss.backward()
optim.step()
img = img.data.cpu().numpy()[0].transpose(1, 2, 0)
sz = int(1.2 * sz)
img = cv2.resize(img, (sz, sz))
img = cv2.blur(img, (5, 5))
return img
for i in tqdm(range(512)):
img = visiual(56, i)
cv2.imwrite(f'.//FeatureMap//24-{i}.png', img*255)