<jupyter_start><jupyter_text>Визуализация сверточных слоев глубокой нейронной сети<jupyter_code>from tensorflow.python.keras.preprocessing.image import ImageDataGenerator
from tensorflow.python.keras.models import Model, model_from_json
from tensorflow.python.keras.layers import Activation, Dropout, Flatten, Dense
from tensorflow.python.keras.applications import VGG16
from tensorflow.python.keras.optimizers import Adam
from tensorflow.python.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import toimage
%matplotlib inline<jupyter_output>C:\Users\sozyk\Anaconda3\lib\site-packages\h5py\__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters<jupyter_text>Выбираем фото для демонстрации<jupyter_code>image_file_name = 'testing_cat.jpg'
img = image.load_img(image_file_name, target_size=(150, 150))
plt.imshow(img)<jupyter_output><empty_output><jupyter_text>Преобразуем фотографию в массив numpy<jupyter_code>img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.<jupyter_output><empty_output><jupyter_text>Загружаем обученную модель CNN<jupyter_code># Загружаем данные об архитектуре сети из файла json
json_file = open("cats_and_dogs_cnn.json", "r")
loaded_model_json = json_file.read()
json_file.close()
# Создаем модель на основе загруженных данных
loaded_model = model_from_json(loaded_model_json)
# Загружаем веса в модель
loaded_model.load_weights("cats_and_dogs_cnn.h5")
loaded_model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1 (Conv2D)            (None, 148, 148, 32)      896       
_________________________________________________________________
activation_1 (Activation)    (None, 148, 148, 32)      0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 72, 72, 32)        9248      
_________________________________________________________________
activation_2 (Activation)    (None, 72, 72, 32)        0         
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 36, 36, 32)        0         
_________________________________________________________________
conv2d_3 ([...]<jupyter_text>"Обрезаем" модель на нужном слое<jupyter_code># Номера сверточных слоев - 0, 3, 6
activation_model = Model(inputs=loaded_model.input, outputs=loaded_model.layers[6].output)
activation_model.summary()<jupyter_output>_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_1_input (InputLayer)  (None, 150, 150, 3)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 148, 148, 32)      896       
_________________________________________________________________
activation_1 (Activation)    (None, 148, 148, 32)      0         
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 74, 74, 32)        0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 72, 72, 32)        9248      
_________________________________________________________________
activation_2 (Activation)    (None, 72, 72, 32)        0         
_________________________________________________________________
max_poolin[...]<jupyter_text>Запускаем изображение в обрезанную модель<jupyter_code>activation = activation_model.predict(img_array)
print(activation.shape)<jupyter_output>(1, 34, 34, 64)<jupyter_text>Показываем результаты работы модели<jupyter_code>plt.matshow(activation[0, :, :, 18], cmap='viridis')<jupyter_output><empty_output><jupyter_text>Печатаем вывод всех карт признаков<jupyter_code>images_per_row = 16
n_filters = activation.shape[-1]
size = activation.shape[1]
n_cols = n_filters // images_per_row
display_grid = np.zeros((n_cols * size, images_per_row * size))
for col in range(n_cols):
    for row in range(images_per_row):
        channel_image = 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.grid(False)
plt.imshow(display_grid, aspect='auto', cmap='viridis')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10819165378670788, 'fraction_numerical': 0.035741885625965995, 'mean_word_length': 5.336597307221542, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '341186', 'license': 'Apache-2.0', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1560, 'n_words': 315, 'path': 'lab7/c_and_d_visualization.ipynb', 'repo_name': 'PavlenkoDR/Neural', 'size': '5176'}