์ ์ฅ๋ CNN ๋ชจ๋ธ์ ๊ฐ์ ธ์์ ์ด๋ฅผ ์ฌ์ฉํด ์ด๋ฏธ์ง์ ํํฐ๋ฅผ ๋จผ์ ์๊ฐํ ํด๋ณด์๋ค.
๊ทธ๋ฆฌ๊ณ ๊ณ ์์ด์ฌ์ง์ ํ๋ ๊ฐ์ ธ์์ ์ด๋ฅผ ์ ์ฒ๋ฆฌํ์ฌ ์ถ๋ ฅํ์๋ค.
img_path = './dogs-vs-cats/small/test_dir/test_cats_dir/cat.1700.jpg'
from keras.preprocessing import image
import numpy as np
img = image.load_img(img_path, target_size=(150, 150))
img_tensor = image.img_to_array(img)
img_tensor = np.expand_dims(img_tensor, axis=0)
# ๋ชจ๋ธ์ด ํ๋ จ๋ ๋ ์
๋ ฅ์ ์ ์ฉํ ์ ์ฒ๋ฆฌ ๋ฐฉ์์ ๋์ผํ๊ฒ ์ฌ์ฉํฉ๋๋ค
img_tensor /= 255.
print(img_tensor.shape)
import matplotlib.pyplot as plt
plt.imshow(img_tensor[0])
plt.show()
๊ทธ๋ฆฌ๊ณ ์ด ์ฌ์ง์ ๋ํ ํน์ฑ๋งต์ ์ถ๋ ฅํด๋ณด์๋ค.
layer_names = []
for layer in model.layers[:8]:
layer_names.append(layer.name)
images_per_row = 16
for layer_name, layer_activation in zip(layer_names, activations):
# ํน์ฑ ๋งต์ ์๋ ํน์ฑ์ ์
n_features = layer_activation.shape[-1]
# ํน์ฑ ๋งต ํฌ๊ธฐ (1, size, size, n_features)
size = layer_activation.shape[1]
# ํ์ฑํ ์ฑ๋์ ์ํ ๊ทธ๋ฆฌ๋ ํฌ๊ธฐ
n_cols = n_features // images_per_row
display_grid = np.zeros((size * n_cols, images_per_row * size))
# ๊ฐ ํ์ฑํ๋ฅผ ๊ทธ๋ฆฌ๋์ ์ฑ์ฐ๊ธฐ
for col in range(n_cols):
for row in range(images_per_row):
channel_image = layer_activation[0,
:, :,
col * images_per_row + row]
channel_image -= channel_image.mean()
channel_image /= channel_image.std()
channel_image *= 64
channel_image += 128
channel_image = np.clip(channel_image, 0, 255).astype('uint8')
display_grid[col * size : (col + 1) * size,
row * size : (row + 1) * size] = channel_image
scale = 1. / size
plt.figure(figsize=(scale * display_grid.shape[1],
scale * display_grid.shape[0]))
plt.title(layer_name)
plt.grid(False)
plt.imshow(display_grid, aspect='auto', cmap='viridis')
plt.show()
์ด๊ธฐ ๋ ์ด์ด๋ ๋ชจ๋ ์ ๋ณด๊ฐ ์ ์ง๋์ง๋ง, ์์์ธต์ผ๋ก ๊ฐ์๋ก ํ์ฑํ๋ ๋ ์ถ์์ ์ด๊ณ ๊ณ ์์ค์ ๊ฐ๋ ์ ์ธ์ฝ๋ฉํจ.
ํํฐ์ ์ธ์ฝ๋ฉ๋ ํจํด์ด ์ ๋ ฅ ์ด๋ฏธ์ง์ ์์ด์ ๋น์ด์๋ ํ์ฑํ๊ฐ ๊น์ด์ง์๋ก ๋ง์์ง๋ค.
์ด์ VGG16๊ณผ ์ผ๋ผ์ค ๋ฐฑ์๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๊ฐ์ ธ์ ์ฌ์ฉํ๋๋ฐ, ์ผ๋ผ์ค๊ฐ ์ ๋ฐ์ดํธ๋๋ฉด์ backend ์ง์์ด ํตํฉ๋์ ๋ฐ๋ก ๋ชป์ด๋ค. ์ด์ ๋ฒ์ ์ ์ฝ๋๋ฅผ ์ฌ์ฉํ๊ธฐ์ํด ์๋์ ์ฝ๋๋ฅผ ์ถ๊ฐํ์ฌ ์ด์ ๋ฒ์ ์ฝ๋ ์ฌ์ฉ์ ๊ฐ๋ฅํ๊ฒ ๋ง๋ ๋ค.
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from keras.applications import VGG16
from keras import backend as K
model = VGG16(weights='imagenet')
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
img_path = './animal/a1.jpg'
# 224 × 224 ํฌ๊ธฐ์ ํ์ด์ฌ ์ด๋ฏธ์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ(PIL) ๊ฐ์ฒด๋ก ๋ฐํ
img = image.load_img(img_path, target_size=(224, 224))
# (224, 224, 3) ํฌ๊ธฐ์ ๋ํ์ด float32 ๋ฐฐ์ด
x = image.img_to_array(img)
# ์ฐจ์์ ์ถ๊ฐํ์ฌ (1, 224, 224, 3) ํฌ๊ธฐ์ ๋ฐฐ์น๋ก ๋ฐฐ์ด์ ๋ณํ
x = np.expand_dims(x, axis=0)
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ(์ฑ๋๋ณ ์ปฌ๋ฌ ์ ๊ทํ)
x = preprocess_input(x)
a1 ์ฌ์ง์ ์ฝ๋ฟ์ ์ฌ์ง์ผ๋ก ํ์๋ค.
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
np.argmax(preds[0])
์ฌ๊ธฐ์ Predicted ๊ฐ์ด ์ถ๋ ฅ๋๋ฉด์ ๋ฌด์์ผ๋ก ์์ธกํ๋์ง, ๋ชํ๋ก์ ํ๋ฅ ๋ก ํ์ ํ๋์ง ์ถ๋ ฅ์ด ๋๊ณ , ์ด์ ๋ํ index๊ฐ์ด ์ถ๋ ฅ๋๋ค. ์ด๋ฅผ ํตํด ์ฌ์ง๊ณผ ํํธ๋งต์ ํฉ์ณ์ ํ์ธ์ ํ๋ค.
# ์ด๋ฏธ์ง ๊ฒฝ๋ก
img_path = './animal/a10.jpg'
# 224 × 224 ํ์ด์ฌ ์ด๋ฏธ์ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ(PIL) ๊ฐ์ฒด๋ก ๋ฐํ
img = image.load_img(img_path, target_size=(224, 224))
# (224, 224, 3) ๋ํ์ด float32 ๋ฐฐ์ด
x = image.img_to_array(img)
# ์ฐจ์ ์ถ๊ฐ, (1, 224, 224, 3)
x = np.expand_dims(x, axis=0)
# ๋ฐ์ดํฐ ์ ์ฒ๋ฆฌ(์ฑ๋๋ณ ์ปฌ๋ฌ ์ ๊ทํ)
x = preprocess_input(x)
#์ด๋ฏธ์ง ์ถ๋ ฅ ์ํด ๋ค์
img1 = image.load_img(img_path, target_size=(150, 150))
img_tensor = image.img_to_array(img1)
img_tensor = np.expand_dims(img_tensor, axis=0)
img_tensor /= 255.
#์ด๋ฏธ์ง์ถ๋ ฅ
plt.imshow(img_tensor[0])
plt.show()
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
np.argmax(preds[0])
import cv2
a10_output = model.output[:, 975]
last_conv_layer = model.get_layer('block5_conv3')
grads = K.gradients(a10_output, last_conv_layer.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
iterate = K.function([model.input], [pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, conv_layer_output_value = iterate([x])
for i in range(512):
conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
heatmap = np.mean(conv_layer_output_value, axis=-1)
heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
plt.matshow(heatmap)
plt.show()
img = cv2.imread(img_path)
heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
heatmap = np.uint8(255 * heatmap)
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img = heatmap * 0.4 + img
cv2.imwrite('./animal/b10.jpg', superimposed_img)
์ด๋ ๊ฒ ์๊ฐํ๋ฅผ ํ์ฌ ์ด๋ค ๋๋ฌผ๋ก ์์ธกํ๊ณ , ์ด์ ๋ ๋ฌด์์ธ์ง ํํธ๋งต์ ํตํด ์ ์ ์๊ฒ ๋๋ค.