神经网络可视化

from vis.utils import utils


# Activations are used through an Activation Layer. 
from keras import activations
from keras.applications import VGG16
# Build the VGG16 network with ImageNet weights
model = VGG16(weights='imagenet', include_top=True)

# Utility to search for layer index by name. 
# Alternatively we can specify this as -1 since it corresponds to the last layer.
# Anyway, we are interested in the last layer, where the prediction happens 
layer_idx = utils.find_layer_idx(model, 'predictions')

#查看vgg16的每一层
for i in model.layers:
print(i.name);

#To visualize activation over final dense layer outputs, we need to switch the softmax activation out for linear
#since gradient of output node will depend on all the other node activations.
# Swap softmax with linear
model.layers[layer_idx].activation = activations.linear
model = utils.apply_modifications(model)


import pickle 
#A dictionary that associates each class to its label
dico=pickle.load(open('./dico.pkl','rb'))

for key in range(10) :
    print( str(key) + ': '+dico[key]+'\n')

from vis.utils import utils
from matplotlib import pyplot as plt
import numpy as np

# Magic function in IPython:The resulting plots will also be stored in the notebook document. 
# get_ipython().magic('matplotlib inline')

# Size of the displaying figures for visualizing plots. 
plt.rcParams['figure.figsize'] = (18, 6)
# exit();

#We define the softmax function to translate the output of the CNN into a probability for each class. 
def softmax(x):
    """
    Compute softmax values for each sets of scores in x.
    
    Rows are scores for each class. 
    Columns are predictions (samples).
    """
    scoreMatExp = np.exp(np.asarray(x))
    return scoreMatExp / scoreMatExp.sum(0)

#Function that calls the VGG16 network and displays its prediction
def predictImage(imagesPaths):
    images=[]
    outs=[]
    f, ax = plt.subplots(1, len(imagesPaths))
    for i,path in enumerate(imagesPaths) :
        # Image to be predicted
        im=utils.load_img(path, target_size=(224, 224))
        images.append(im)
        
        # Output: prediction value for the input image
        out=softmax(model.predict(im.reshape(-1,224,224,3))[0])
        outs.append(out)
        classKey=np.argmax(out)
        
        # Look in the dictionary for the specific term for the image identification. 
        certainty=out[classKey]
        if len(imagesPaths)>1:
            ax[i].imshow(im)
            ax[i].set_title('VGG16 predicted : ' + dico[classKey] +' with a '+ str(round(certainty*100,3))+' % certainty')
        else :
            ax.imshow(im)
            ax.set_title('VGG16 predicted : ' + dico[classKey] +' with a '+ str(round(certainty*100,3))+' % certainty')
    return images,outs

images,outs = predictImage(['images/ouzel1.jpg','images/boat1.jpg'])

im,out=predictImage(['images/shark1.jpg'])
images = images + im
outs = outs + out

def otherProbs(out):
    sortedIds=np.argsort(-out)
    print('But it could also be : \n')
    for i in range(1,7) : 
        print('A '+dico[sortedIds[i]]+' with a '+ str(round(out[sortedIds[i]]*100,3))+' % certainty')

otherProbs(outs[-1])

from vis.visualization import visualize_activation
from vis.input_modifiers import Jitter


# 20 is the imagenet category for 'ouzel'
im = visualize_activation(model, layer_idx, filter_indices=20, max_iter=500,input_modifiers=[Jitter(16)], verbose=False)
plt.imshow(im)

# KERAS visualize_cam
from vis.visualization import visualize_saliency

plt.figure()
f, ax = plt.subplots(2, 2,figsize=(15,15))
ax=ax.reshape((4))
plt.suptitle('Saliency for predicted classes')


# New output containing the output result for the saliency visualization 
gradsSaliency=[]
gradsCAM=[]
certainties=[]
classKeys=[]

for i, img in enumerate(images): 
    classKey=np.argmax(outs[i])
    classKeys.append(classKey)
    certainty=outs[i][classKey]
    certainties.append(certainty)
    
    grads = visualize_saliency(model, layer_idx, filter_indices=classKeys[i], seed_input=img, backprop_modifier='guided')        
    gradsSaliency.append(grads)
    
    ax[i].imshow(grads,cmap='jet')
    ax[i].set_title('VGG16 predicted : ' + dico[classKeys[i]] +' with a '+ str(round(certainties[i]*100,3))+' % certainty')


import numpy as np
import matplotlib.cm as cm

# KERAS visualize_cam
from vis.visualization import visualize_cam, overlay 

plt.figure()
f, ax = plt.subplots(2, 2,figsize=(15,15))
ax=ax.reshape((4))

# New list containing the output image result of the Grad-Cam visualization. 
gradsCAM=[]

plt.suptitle('grad-CAM for predicted classes') 


for i, img in enumerate(images):    
    
    # Visualization with the Grad-Cam output. 
    grads = visualize_cam(model, layer_idx, filter_indices=classKeys[i], 
                          seed_input=img, backprop_modifier='guided')      
    # Lets overlay the heatmap onto original image. 
    gradsCAM.append(grads)
    t=plt.imshow(grads,cmap='jet')
    # l=t.get_array()
    heatmap=np.uint8(cm.jet(grads)[..., :3] * 255)
    ax[i].imshow(overlay(heatmap,img))
    ax[i].set_title('VGG16 predicted : ' + dico[classKeys[i]] +' with a '+ str(round(certainties[i]*100,3))+' % certainty')


from matplotlib import colors
plt.figure()
f, ax = plt.subplots(2, 2,figsize=(15,15))
ax=ax.reshape((4))
plt.suptitle('grad-CAM + saliency for predicted classes')

for i, img in enumerate(images):    

    # print(np.uint8(cm.jet(gradsCAM[i])[..., :3] * 255));
    # exit();
    ax[i].imshow((np.uint8(cm.jet(gradsSaliency[i])[..., :3] * 255)*1/(1.1+np.uint8(cm.jet(gradsCAM[i])[..., :3] * 255))),cmap='Blues',vmin=150),
    #ax[i].imshow(overlay(jet_heatmap[:,:,:,1],jet_heatmap[:,:,:,2]))
    ax[i].set_title('VGG16 predicted : ' + dico[classKeys[i]] +' with a '+ str(round(certainties[i]*100,3))+' % certainty')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值