합성곱 신경망의 시각화

가중치 시각화

In [1]:
from tensorflow import keras
In [2]:
# 코랩에서 실행하는 경우에는 다음 명령을 실행하여 best-cnn-model.h5 파일을 다운로드받아 사용하세요.
!wget https://github.com/rickiepark/hg-mldl/raw/master/best-cnn-model.h5
--2021-05-31 16:16:00--  https://github.com/rickiepark/hg-mldl/raw/master/best-cnn-model.h5
Resolving github.com (github.com)... 140.82.113.4
Connecting to github.com (github.com)|140.82.113.4|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.githubusercontent.com/rickiepark/hg-mldl/master/best-cnn-model.h5 [following]
--2021-05-31 16:16:01--  https://raw.githubusercontent.com/rickiepark/hg-mldl/master/best-cnn-model.h5
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 4044200 (3.9M) [application/octet-stream]
Saving to: ‘best-cnn-model.h5’

best-cnn-model.h5   100%[===================>]   3.86M  --.-KB/s    in 0.06s   

2021-05-31 16:16:01 (62.1 MB/s) - ‘best-cnn-model.h5’ saved [4044200/4044200]

In [3]:
model = keras.models.load_model('best-cnn-model.h5')
In [4]:
model.layers
Out[4]:
[<tensorflow.python.keras.layers.convolutional.Conv2D at 0x7f6902deea10>,
 <tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x7f68b00a95d0>,
 <tensorflow.python.keras.layers.convolutional.Conv2D at 0x7f68c0784e90>,
 <tensorflow.python.keras.layers.pooling.MaxPooling2D at 0x7f68b0062110>,
 <tensorflow.python.keras.layers.core.Flatten at 0x7f68b006a810>,
 <tensorflow.python.keras.layers.core.Dense at 0x7f68b003e8d0>,
 <tensorflow.python.keras.layers.core.Dropout at 0x7f68c08e96d0>,
 <tensorflow.python.keras.layers.core.Dense at 0x7f68c08e93d0>]
In [5]:
conv = model.layers[0]

print(conv.weights[0].shape, conv.weights[1].shape)
(3, 3, 1, 32) (32,)
In [6]:
conv_weights = conv.weights[0].numpy()

print(conv_weights.mean(), conv_weights.std())
-0.03802128 0.27455312
In [7]:
import matplotlib.pyplot as plt
In [8]:
plt.hist(conv_weights.reshape(-1, 1))
plt.xlabel('weight')
plt.ylabel('count')
plt.show()
In [9]:
fig, axs = plt.subplots(2, 16, figsize=(15,2))

for i in range(2):
    for j in range(16):
        axs[i, j].imshow(conv_weights[:,:,0,i*16 + j], vmin=-0.5, vmax=0.5)
        axs[i, j].axis('off')

plt.show()
In [10]:
no_training_model = keras.Sequential()

no_training_model.add(keras.layers.Conv2D(32, kernel_size=3, activation='relu', 
                                          padding='same', input_shape=(28,28,1)))
In [11]:
no_training_conv = no_training_model.layers[0]

print(no_training_conv.weights[0].shape)
(3, 3, 1, 32)
In [12]:
no_training_weights = no_training_conv.weights[0].numpy()

print(no_training_weights.mean(), no_training_weights.std())
2.7320864e-05 0.07966287
In [13]:
plt.hist(no_training_weights.reshape(-1, 1))
plt.xlabel('weight')
plt.ylabel('count')
plt.show()
In [14]:
fig, axs = plt.subplots(2, 16, figsize=(15,2))

for i in range(2):
    for j in range(16):
        axs[i, j].imshow(no_training_weights[:,:,0,i*16 + j], vmin=-0.5, vmax=0.5)
        axs[i, j].axis('off')

plt.show()

함수형 API

In [15]:
print(model.input)
KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28, 1), dtype=tf.float32, name='conv2d_input'), name='conv2d_input', description="created by layer 'conv2d_input'")
In [16]:
conv_acti = keras.Model(model.input, model.layers[0].output)

특성 맵 시각화

In [17]:
(train_input, train_target), (test_input, test_target) = keras.datasets.fashion_mnist.load_data()
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-labels-idx1-ubyte.gz
32768/29515 [=================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/train-images-idx3-ubyte.gz
26427392/26421880 [==============================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-labels-idx1-ubyte.gz
8192/5148 [===============================================] - 0s 0us/step
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/t10k-images-idx3-ubyte.gz
4423680/4422102 [==============================] - 0s 0us/step
In [18]:
plt.imshow(train_input[0], cmap='gray_r')
plt.show()
In [19]:
inputs = train_input[0:1].reshape(-1, 28, 28, 1)/255.0

feature_maps = conv_acti.predict(inputs)
In [20]:
print(feature_maps.shape)
(1, 28, 28, 32)
In [21]:
fig, axs = plt.subplots(4, 8, figsize=(15,8))

for i in range(4):
    for j in range(8):
        axs[i, j].imshow(feature_maps[0,:,:,i*8 + j])
        axs[i, j].axis('off')

plt.show()
In [22]:
conv2_acti = keras.Model(model.input, model.layers[2].output)
In [23]:
feature_maps = conv2_acti.predict(train_input[0:1].reshape(-1, 28, 28, 1)/255.0)
In [24]:
print(feature_maps.shape)
(1, 14, 14, 64)
In [25]:
fig, axs = plt.subplots(8, 8, figsize=(12,12))

for i in range(8):
    for j in range(8):
        axs[i, j].imshow(feature_maps[0,:,:,i*8 + j])
        axs[i, j].axis('off')

plt.show()