-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathCaptum_Recipe.py
172 lines (134 loc) ยท 7.61 KB
/
Captum_Recipe.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
172
"""
Captum์ ์ฌ์ฉํ์ฌ ๋ชจ๋ธ ํด์ํ๊ธฐ
===================================
**๋ฒ์ญ**: `์ ์ฌ๋ฏผ <https://github.com/jjeamin>`_
"""
######################################################################
# Captum์ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ ํน์ง(features)์ด ๋ชจ๋ธ์ ์์ธก ๋๋ ๋ด๋ฐ ํ์ฑํ์
# ๋ฏธ์น๋ ์ํฅ์ ์ดํดํ๊ณ , ๋ชจ๋ธ์ ๋์ ๋ฐฉ์์ ์ ์ ์์ต๋๋ค.
#
# ๊ทธ๋ฆฌ๊ณ \ ``Integrated Gradients``\ ์ \ ``Guided GradCam``\ ๊ณผ ๊ฐ์
# ์ต์ฒจ๋จ์ feature attribution ์๊ณ ๋ฆฌ์ฆ์ ์ ์ฉํ ์ ์์ต๋๋ค.
#
# ์ด ๋ ์ํผ์์๋ Captum์ ์ฌ์ฉํ์ฌ ๋ค์์ ์ํํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์๋๋ค:
#
# - ์ด๋ฏธ์ง ๋ถ๋ฅ๊ธฐ(classifier)์ ์์ธก์ ํด๋น ์ด๋ฏธ์ง์ ํน์ง(features)์ ํ์ํ๊ธฐ
# - ์์ฑ(attribution) ๊ฒฐ๊ณผ๋ฅผ ์๊ฐํ ํ๊ธฐ
######################################################################
# ์์ํ๊ธฐ ์ ์
# ----------------
#
######################################################################
# Captum์ด Python ํ๊ฒฝ์ ์ค์น๋์ด ์๋์ง ํ์ธํด์ผ ํฉ๋๋ค.
# Captum์ Github์์ ``pip`` ํจํค์ง ๋๋ ``conda`` ํจํค์ง๋ก ์ ๊ณต๋ฉ๋๋ค.
# ์์ธํ ์ง์นจ์ https://captum.ai/ ์ ์ค์น ์๋ด์๋ฅผ ์ฐธ์กฐํ๋ฉด ๋ฉ๋๋ค.
######################################################################
# ๋ชจ๋ธ์ ๊ฒฝ์ฐ, PyTorch์ ๋ด์ฅ ๋ ์ด๋ฏธ์ง ๋ถ๋ฅ๊ธฐ(classifier)๋ฅผ ์ฌ์ฉํฉ๋๋ค.
# Captum์ ์ํ ์ด๋ฏธ์ง์ ์ด๋ค ๋ถ๋ถ์ด ๋ชจ๋ธ์ ์ํด ๋ง๋ค์ด์ง
# ํน์ ํ ์์ธก์ ๋์์ ์ฃผ๋์ง ๋ณด์ฌ์ค๋๋ค.
import torchvision
from torchvision import models, transforms
from PIL import Image
import requests
from io import BytesIO
model = torchvision.models.resnet18(weights=models.ResNet18_Weights.IMAGENET1K_V1).eval()
response = requests.get("https://image.freepik.com/free-photo/two-beautiful-puppies-cat-dog_58409-6024.jpg")
img = Image.open(BytesIO(response.content))
center_crop = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
])
normalize = transforms.Compose([
transforms.ToTensor(), # ์ด๋ฏธ์ง๋ฅผ 0์์ 1์ฌ์ด์ ๊ฐ์ ๊ฐ์ง Tensor๋ก ๋ณํ
transforms.Normalize( # 0์ ์ค์ฌ์ผ๋ก ํ๋ imagenet ํฝ์
์ RGB ๋ถํฌ๋ฅผ ๋ฐ๋ฅด๋ ์ ๊ทํ
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]
)
])
input_img = normalize(center_crop(img)).unsqueeze(0)
######################################################################
# ์์ฑ(attribution) ๊ณ์ฐํ๊ธฐ
# -------------------------------
######################################################################
# ๋ชจ๋ธ์ top-3 ์์ธก ์ค์๋ ๊ฐ์ ๊ณ ์์ด์ ํด๋นํ๋ ํด๋์ค 208๊ณผ 283์ด ์์ต๋๋ค.
#
# Captum์ \ ``Occlusion``\ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์ฌ ๊ฐ ์์ธก์ ์
๋ ฅ์ ํด๋น ๋ถ๋ถ์ ํ์ํฉ๋๋ค.
from captum.attr import Occlusion
occlusion = Occlusion(model)
strides = (3, 9, 9) # ์์์๋ก = ์ธ๋ถ์ ์ธ ์์ฑ์ด์ง๋ง ๋๋ฆผ
target=208, # ImageNet์์ Labrador์ ์ธ๋ฑ์ค
sliding_window_shapes=(3,45, 45) # ๊ฐ์ฒด์ ๋ชจ์์ ๋ณํ์ํค๊ธฐ์ ์ถฉ๋ถํ ํฌ๊ธฐ๋ฅผ ์ ํ
baselines = 0 # ์ด๋ฏธ์ง๋ฅผ ๊ฐ๋ฆด ๊ฐ, 0์ ํ์
attribution_dog = occlusion.attribute(input_img,
strides = strides,
target=target,
sliding_window_shapes=sliding_window_shapes,
baselines=baselines)
target=283, # ImageNet์์ Persian cat์ ์ธ๋ฑ์ค
attribution_cat = occlusion.attribute(input_img,
strides = strides,
target=target,
sliding_window_shapes=sliding_window_shapes,
baselines=0)
######################################################################
# Captum์ ``Occlusion`` ์ธ์๋ \ ``Integrated Gradients``\ , \ ``Deconvolution``\ ,
# \ ``GuidedBackprop``\ , \ ``Guided GradCam``\ , \ ``DeepLift``\ ,
# ๊ทธ๋ฆฌ๊ณ \ ``GradientShap``\๊ณผ ๊ฐ์ ๋ง์ ์๊ณ ๋ฆฌ์ฆ์ ์ ๊ณตํฉ๋๋ค.
# ์ด๋ฌํ ๋ชจ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ด๊ธฐํํ ๋ ๋ชจ๋ธ์ ํธ์ถ ๊ฐ๋ฅํ \ ``forward_func``\ ์ผ๋ก ๊ธฐ๋ํ๋ฉฐ
# ์์ฑ(attribution) ๊ฒฐ๊ณผ๋ฅผ ํตํฉํด์ ๋ฐํํ๋ ``attribute(...)`` ๋ฉ์๋๋ฅผ ๊ฐ์ง๋
# ``Attribution`` ์ ์๋ธํด๋์ค ์
๋๋ค.
#
# ์ด๋ฏธ์ง์ธ ๊ฒฝ์ฐ ์์ฑ(attribution) ๊ฒฐ๊ณผ๋ฅผ ์๊ฐํ ํด๋ณด๊ฒ ์ต๋๋ค.
#
######################################################################
# ๊ฒฐ๊ณผ ์๊ฐํํ๊ธฐ
# -----------------------
#
######################################################################
# Captum์ \ ``visualization``\ ์ ํธ๋ฆฌํฐ๋ ๊ทธ๋ฆผ๊ณผ ํ
์คํธ ์
๋ ฅ ๋ชจ๋์ ๋ํ
# ์์ฑ(attribution) ๊ฒฐ๊ณผ๋ฅผ ์๊ฐํ ํ ์ ์๋ ์ฆ์ ์ฌ์ฉ๊ฐ๋ฅํ ๋ฐฉ๋ฒ์ ์ ๊ณตํฉ๋๋ค.
#
import numpy as np
from captum.attr import visualization as viz
# ๊ณ์ฐ ์์ฑ Tensor๋ฅผ ์ด๋ฏธ์ง ๊ฐ์ numpy ๋ฐฐ์ด๋ก ๋ณํํฉ๋๋ค.
attribution_dog = np.transpose(attribution_dog.squeeze().cpu().detach().numpy(), (1,2,0))
vis_types = ["heat_map", "original_image"]
vis_signs = ["all", "all"] # "positive", "negative", ๋๋ ๋ชจ๋ ํ์ํ๋ "all"
# positive ์์ฑ์ ํด๋น ์์ญ์ ์กด์ฌ๊ฐ ์์ธก ์ ์๋ฅผ ์ฆ๊ฐ์ํจ๋ค๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
# negative ์์ฑ์ ํด๋น ์์ญ์ ์กด์ฌ๊ฐ ์์ธก ์ ์๋ฅผ ๋ฎ์ถ๋ ์ค๋ต ์์ญ์ ์๋ฏธํฉ๋๋ค.
_ = viz.visualize_image_attr_multiple(attribution_dog,
np.array(center_crop(img)),
vis_types,
vis_signs,
["attribution for dog", "image"],
show_colorbar = True
)
attribution_cat = np.transpose(attribution_cat.squeeze().cpu().detach().numpy(), (1,2,0))
_ = viz.visualize_image_attr_multiple(attribution_cat,
np.array(center_crop(img)),
["heat_map", "original_image"],
["all", "all"], # positive/negative ์์ฑ ๋๋ all
["attribution for cat", "image"],
show_colorbar = True
)
######################################################################
# ๋ง์ฝ ๋ฐ์ดํฐ๊ฐ ํ
์คํธ์ธ ๊ฒฝ์ฐ ``visualization.visualize_text()`` ๋
# ์
๋ ฅ ํ
์คํธ ์์ ์์ฑ(attribution)์ ํ์ํ ์ ์๋ ์ ์ฉ ๋ทฐ(view)๋ฅผ ์ ๊ณตํฉ๋๋ค.
# http://captum.ai/tutorials/IMDB_TorchText_Interpret ์์ ์์ธํ ๋ด์ฉ์ ํ์ธํ์ธ์.
#
######################################################################
# ๋ง์ง๋ง ๋
ธํธ
# ---------------
#
######################################################################
# Captum์ ์ด๋ฏธ์ง, ํ
์คํธ ๋ฑ์ ํฌํจํ์ฌ ๋ค์ํ ๋ฐฉ์์ผ๋ก PyTorch์์ ๋๋ถ๋ถ์ ๋ชจ๋ธ ํ์
์ ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
# Captum์ ์ฌ์ฉํ๋ฉด ๋ค์์ ์ํํ ์ ์์ต๋๋ค.
# \* ์์์ ์ค๋ช
ํ ๊ฒ์ฒ๋ผ ํน์ ํ ์ถ๋ ฅ์ ๋ชจ๋ธ ์
๋ ฅ์ ํ์ํ๊ธฐ
# \* ํน์ ํ ์ถ๋ ฅ์ ์๋์ธต์ ๋ด๋ฐ์ ํ์ํ๊ธฐ (Captum API reference๋ฅผ ๋ณด์ธ์).
# \* ๋ชจ๋ธ ์
๋ ฅ์ ๋ํ ์๋์ธต ๋ด๋ฐ์ ๋ฐ์์ ํ์ํ๊ธฐ (Captum API reference๋ฅผ ๋ณด์ธ์).
#
# ์ง์๋๋ ๋ฉ์๋์ ์ ์ฒด API์ ํํ ๋ฆฌ์ผ์ ๋ชฉ๋ก์ http://captum.ai ๋ฅผ ์ฐธ์กฐํ์ธ์.
#
# Gilbert Tanner์ ๋ ๋ค๋ฅธ ์ ์ฉํ ๊ฒ์๋ฌผ :
# https://gilberttanner.com/blog/interpreting-pytorch-models-with-captum
#