-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrun.py
59 lines (46 loc) · 1.7 KB
/
run.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
import argparse
import chainer
import cv2
import numpy as np
from lib import backprop
import models
p = argparse.ArgumentParser()
p.add_argument('--input', '-i', default='images/dog_cat.png')
p.add_argument('--gpu', '-g', type=int, default=-1)
p.add_argument('--arch', '-a',
choices=['alex', 'vgg', 'resnet'],
default='vgg')
p.add_argument('--label', '-y', type=int, default=-1)
p.add_argument('--layer', '-l', default='conv5_3')
args = p.parse_args()
if __name__ == '__main__':
if args.arch == 'alex':
model = models.Alex()
elif args.arch == 'vgg':
model = models.VGG16Layers()
elif args.arch == 'resnet':
model = models.ResNet152Layers()
if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use()
model.to_gpu()
grad_cam = backprop.GradCAM(model)
guided_backprop = backprop.GuidedBackprop(model)
src = cv2.imread(args.input, 1)
src = cv2.resize(src, (model.size, model.size))
x = src.astype(np.float32) - np.float32([103.939, 116.779, 123.68])
x = x.transpose(2, 0, 1)[np.newaxis, :, :, :]
gcam = grad_cam.generate(x, args.label, args.layer)
gcam = np.uint8(gcam * 255 / gcam.max())
gcam = cv2.resize(gcam, (model.size, model.size))
gbp = guided_backprop.generate(x, args.label, args.layer)
ggcam = gbp * gcam[:, :, np.newaxis]
ggcam -= ggcam.min()
ggcam = 255 * ggcam / ggcam.max()
cv2.imwrite('ggcam.png', ggcam)
gbp -= gbp.min()
gbp = 255 * gbp / gbp.max()
cv2.imwrite('gbp.png', gbp)
heatmap = cv2.applyColorMap(gcam, cv2.COLORMAP_JET)
gcam = np.float32(src) + np.float32(heatmap)
gcam = 255 * gcam / gcam.max()
cv2.imwrite('gcam.png', gcam)