fchollet / deep-learning-with-python-notebooks

Jupyter notebooks for the code samples of the book "Deep Learning with Python"
MIT License
18.17k stars 8.53k forks source link

Visualizing convnet filters can't get the results as expected #133

Open ZEROYXY opened 4 years ago

ZEROYXY commented 4 years ago

This issue occured when I learned the Visualizing convnet filters. I tried the code as below but the grid didn't show the resluts as it said. I get the table with margin but empty in each grid. I tried to debug and thought the issue may happen when the initialization of the 'resullts' or when the 'filter_image' put into the 'resluts'. But I didn't found the root cause. Can anyone give a hand to update it? The pyhton codes are as below:

from keras.applications import VGG16 from keras import backend as K import numpy as np import matplotlib.pyplot as plt

model = VGG16(weights='imagenet', include_top=False)

def deprocess_image(x): x -= x.mean() x /= (x.std()+1e-5) x = 0.1 x += 0.5 x = np.clip(x, 0, 1) x = 255 x = np.clip(x, 0, 255).astype('uint8') return x

def generate_pattern(layer_name, filter_index, size=150): layer_output = model.get_layer(layer_name).output loss = K.mean(layer_output[:, :, :, filter_index]) grads = K.gradients(loss, model.input)[0] grads /= (K.sqrt(K.mean(K.square(grads)))+1e-5) interate = K.function([model.input], [loss, grads]) input_img_data = np.random.random((1, size, size, 3))20+128. step = 1. for i in range(40): loss_value, grads_value = interate([input_img_data]) input_img_data += grads_valuestep img = input_img_data[0] return deprocess_image(img)

plt.imshow(generate_pattern('block3_conv1', 0)) plt.show()

for layer_name in ['block1_conv1', 'block2_conv1', 'block3_conv1', 'block4_conv1']: size = 64 margin = 5 results = np.zeros((8size+7margin, 8size+7margin, 3)) for i in range(8): for j in range(8): filter_image = generate_pattern(layer_name, i+(j8), size=size) horizontal_start = isize+imargin horizontal_end = horizontal_start+size vertical_start = jsize+j*margin vertical_end = vertical_start+size results[horizontal_start:horizontal_end, vertical_start:vertical_end, :] = filter_image plt.figure(figsize=(20, 20)) plt.imshow(results) plt.show()

The screenshot of the issue is as below: Visualizing convnet filters issue

OndrejSliva commented 4 years ago

Hi, allowed range of values for imshow parameter is [0..1] for floats or [0..255] for integers. Data type of variable result is float, but range of values is [0..255]. So you need to change data type to int, or divide by 255.

First solution: plt.imshow(results.astype(np.uint8))

Second solution: plt.imshow(results/255)

ZEROYXY commented 4 years ago

Hi Silva

^_^ Thank you very much for your time to answer my question and I checked the parameter and code as you suggested.  Then I found the key point you said and I tried your way to make this issue resolved. Thank you so much again and I can close the CNN Fillters Visualization successfully now with your strong help ! I will tell the people who is facing the same problem with me about how to resolve it, too. 

Thanks and Best Regards Cloud [Shuo Zhang] Your Chinese Friend

------------------ 原始邮件 ------------------ 发件人: "OndrejSliva"<notifications@github.com>; 发送时间: 2020年4月5日(星期天) 晚上8:14 收件人: "fchollet/deep-learning-with-python-notebooks"<deep-learning-with-python-notebooks@noreply.github.com>; 抄送: "西伯利亚狼"<845605918@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [fchollet/deep-learning-with-python-notebooks] Visualizing convnet filters can't get the results as expected (#133)

Hi, allowed range of values for imshow parameter is [0..1] for floats or [0..255] for integers. Data type of variable result is float, but range of values is [0..255]. So you need to change data type to int, or divide by 255.

First solution: plt.imshow(results.astype(np.uint8))

Second solution: plt.imshow(results/255)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

zengxinch commented 4 years ago

Yes, you could add the chapter number to the question title. I think this could help people who encounter the same problem find answer quickly.

SaadAsgharAli commented 4 years ago

Thanks @OndrejSliva