【问题标题】:Visualizing features and activations in CNN using Keras example使用 Keras 示例在 CNN 中可视化特征和激活
【发布时间】:2017-10-12 06:25:48
【问题描述】:

我正在关注 keras 博客文章代码,以可视化在不同层学习和激活的特征。代码随机生成了一个维度为 (1,3,img_width, img_height) 的灰度图像并将其可视化。在这里:

from __future__ import print_function

from scipy.misc import imsave
import numpy as np
import time
from keras.applications import vgg16
from keras import backend as K

# dimensions of the generated pictures for each filter.
img_width = 128
img_height = 128

# the name of the layer we want to visualize
# (see model definition at keras/applications/vgg16.py)
layer_name = 'block5_conv1'

# util function to convert a tensor into a valid image


def deprocess_image(x):
    # normalize tensor: center on 0., ensure std is 0.1
    x -= x.mean()
    x /= (x.std() + 1e-5)
    x *= 0.1

    # clip to [0, 1]
    x += 0.5
    x = np.clip(x, 0, 1)

    # convert to RGB array
    x *= 255
    if K.image_data_format() == 'channels_first':
        x = x.transpose((1, 2, 0))
    x = np.clip(x, 0, 255).astype('uint8')
    return x

# build the VGG16 network with ImageNet weights
model = vgg16.VGG16(weights='imagenet', include_top=False)
print('Model loaded.')

model.summary()

# this is the placeholder for the input images
input_img = model.input

# get the symbolic outputs of each "key" layer (we gave them unique names).
layer_dict = dict([(layer.name, layer) for layer in model.layers[1:]])


def normalize(x):
    # utility function to normalize a tensor by its L2 norm
    return x / (K.sqrt(K.mean(K.square(x))) + 1e-5)


kept_filters = []
for filter_index in range(0, 200):
    # we only scan through the first 200 filters,
    # but there are actually 512 of them
    print('Processing filter %d' % filter_index)
    start_time = time.time()

    # we build a loss function that maximizes the activation
    # of the nth filter of the layer considered
    layer_output = layer_dict[layer_name].output
    if K.image_data_format() == 'channels_first':
        loss = K.mean(layer_output[:, filter_index, :, :])
    else:
        loss = K.mean(layer_output[:, :, :, filter_index])

    # we compute the gradient of the input picture wrt this loss
    grads = K.gradients(loss, input_img)[0]

    # normalization trick: we normalize the gradient
    grads = normalize(grads)

    # this function returns the loss and grads given the input picture
    iterate = K.function([input_img], [loss, grads])

    # step size for gradient ascent
    step = 1.

    # we start from a gray image with some random noise
    if K.image_data_format() == 'channels_first':
        input_img_data = np.random.random((1, 3, img_width, img_height))
    else:
        input_img_data = np.random.random((1, img_width, img_height, 3))
    input_img_data = (input_img_data - 0.5) * 20 + 128

    # we run gradient ascent for 20 steps
    for i in range(20):
        loss_value, grads_value = iterate([input_img_data])
        input_img_data += grads_value * step

        print('Current loss value:', loss_value)
        if loss_value <= 0.:
            # some filters get stuck to 0, we can skip them
            break

    # decode the resulting input image
    if loss_value > 0:
        img = deprocess_image(input_img_data[0])
        kept_filters.append((img, loss_value))
    end_time = time.time()
    print('Filter %d processed in %ds' % (filter_index, end_time - start_time))

# we will stich the best 64 filters on a 8 x 8 grid.
n = 8

# the filters that have the highest loss are assumed to be better-looking.
# we will only keep the top 64 filters.
kept_filters.sort(key=lambda x: x[1], reverse=True)
kept_filters = kept_filters[:n * n]

# build a black picture with enough space for
# our 8 x 8 filters of size 128 x 128, with a 5px margin in between
margin = 5
width = n * img_width + (n - 1) * margin
height = n * img_height + (n - 1) * margin
stitched_filters = np.zeros((width, height, 3))

# fill the picture with our saved filters
for i in range(n):
    for j in range(n):
        img, loss = kept_filters[i * n + j]
        stitched_filters[(img_width + margin) * i: (img_width + margin) * i + img_width,
                         (img_height + margin) * j: (img_height + margin) * j + img_height, :] = img

# save the result to disk
imsave('stitched_filters_%dx%d.png' % (n, n), stitched_filters)

请告诉我如何修改代码中的这些语句:

input_img_data = np.random.random((1, img_width, img_height, 3))
        input_img_data = (input_img_data - 0.5) * 20 + 128

插入我自己的数据并可视化学习的特征和激活?我的图像是尺寸为 150、150 的 RGB 图像。感谢您的帮助。

【问题讨论】:

    标签: deep-learning keras feature-selection activation


    【解决方案1】:

    如果要处理单个图像:

    from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
    
    img = load_img('data/XXXX.jpg')  # this is a PIL image
    x = img_to_array(img) 
    x = x.reshape((1,) + x.shape)
    

    如果要批量处理:

    from keras.preprocessing.image import ImageDataGenerator
    data_gen_args = dict(featurewise_center=True,
                     featurewise_std_normalization=True,
                     rotation_range=90.,
                     width_shift_range=0.1,
                     height_shift_range=0.1,
                     zoom_range=0.2)
    image_datagen = ImageDataGenerator(**data_gen_args)
    
    
    image_generator = image_datagen.flow_from_directory(
        'data/images',
        class_mode=None,
        seed=seed)
    

    查看文档:https://keras.io/preprocessing/image/#imagedatagenerator

    更新

    # we start from a gray image with some random noise
    if K.image_data_format() == 'channels_first':
        img = load_img('images/1/1.png')  # this is a PIL image
        x = img_to_array(img)
        x = x.reshape((1,) + x.shape)
    else:
        #input_img_data = np.random.random((1, img_width, img_height, 3))
        img = load_img('images/1/1.png')  # this is a PIL image
        x = img_to_array(img)
        x = x.reshape((1,) + x.shape)
    input_img_data = x
    input_img_data = (input_img_data - 0.5) * 20 + 128
    

    【讨论】:

    • 嗨,谢谢。加载图像后,我在重塑图像时遇到问题。请让我知道我应该如何修改这些语句以使我的图像适合模型: if K.image_data_format() == 'channels_first': input_img_data = np.random.random((1, 3, img_width, img_height)) 其他: input_img_data = np.random.random((1, img_width, img_height, 3)) input_img_data = (input_img_data - 0.5) * 20 + 128
    • @shiva,查看更新。用这个替换那段代码。也添加导入行。它对我有用。
    • @shiva,它对你有用吗?如果是这样,您能否接受/投票回答该问题,以便关闭该问题。谢谢
    • 嗨,它不适合我。我使用 .jpg 格式的尺寸为 128 * 128 的灰度图像。当我运行程序时,此时出现此错误:img, loss = keep_filters[i * n + j] IndexError: list index out of range
    • @shiva,您好上面提到的代码适用于 RGB。尝试使用 RGB 图像。稍后,在您的代码中将 RGB 通道 (3) 转换为 1。您之前的代码处理 RGB 通道,它在我的机器上运行良好。
    猜你喜欢
    • 1970-01-01
    • 2018-04-24
    • 2017-07-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 2020-05-13
    • 2018-08-03
    • 2019-09-14
    相关资源
    最近更新 更多