Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

합성곱 계층 출력 분석

import numpy as np
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
import matplotlib.pyplot as plt
tf.config.set_visible_devices([], 'GPU')
model = keras.applications.vgg16.VGG16(weights='imagenet', include_top=True)
model.summary()
img = keras.preprocessing.image.load_img('../data/mozzi.jpg', target_size=(224, 224))
img
# collect the outputs of the intermediate layers
layer_outputs = [layer.output for layer in model.layers]
# create a new model with the intermediate layers as outputs
activation_model = keras.Model(inputs=model.input, outputs=layer_outputs)
# preprocess the image
x1 = keras.preprocessing.image.img_to_array(img)
x1 = keras.applications.vgg16.preprocess_input(x1)
# convert the image to a numpy array
X = np.array([x1])
# get the activations
activations = activation_model.predict(X)
# print out the output shapes with the layer names
for layer, activation in zip(model.layers, activations):
    print(layer.name, activation.shape)
# normalize the image
def normalize_image(img):
    if img.sum() == 0:
        return img

    # image values are standardized to have zero mean and unit variance
    img -= img.mean()
    img /= img.std()
    # amplify the image to make it more visible
    img *= 64
    img += 128
    img = np.clip(img, 0, 255).astype('uint8')
    return img

# visualize a layer output
def visualize_layer(activation, images_per_row=16, output_file=None):
    # plot all outputs
    output_size = activation.shape[-1]
    # calculate the number of columns and rows
    cols = output_size // images_per_row
    fig, subplots = plt.subplots(cols, images_per_row, figsize=(images_per_row, cols))
    plt.setp(subplots, xticks=[], yticks=[])
    for i in range(output_size):            
        channel_image = activation[0, :, :, i].copy()
        if channel_image.sum() != 0:
            channel_image = normalize_image(channel_image)
        # plot the channel
        subplots.ravel()[i].imshow(channel_image, cmap='viridis')
    
    if output_file:
        fig.savefig(output_file)
        # close the figure to free up memory
        plt.close(fig)
    else:
        plt.show()
# visualize all layers
for layer, activation in zip(model.layers, activations):
    # only visualize convolutional layers
    if 'conv' in layer.name:
        print(layer.name, activation.shape)
        visualize_layer(activation, output_file=f'../outputs/{layer.name}.png')