天天看點

cnn神經網絡卷積層可視化

cnn神經網絡卷積層可視化

1.修改model.py檔案的定義網絡輸出函數

def inference(images,batch_size, n_classes):
###你的卷積層、全連接配接層、softmax層定義
    return softmax_linear,conv1,conv2
           

2.修改測試函數

logit ,conv1,conv2= model.inference(image,BATCH_SIZE,n_class)
with tf.Session() as sess:
    ckpt = tf.train.get_checkpoint_state(logits_train_dir)#讀取訓練好的參數
    if ckpt and ckpt.model_checkpoint_path:
        global_step = ckpt.model_checkpoint_path.split('/')[-].split('-')[-]
        saver.restore(sess,ckpt.model_checkpoint_path)
        print('loading sucess,global-step is %s' % global_step)
    else:
        print('No checkpoint file found')
    conv1 = sess.run(conv1,feed_dict={x: image_array})#計算第一層卷積輸出
    conv2 = sess.run(conv2,feed_dict={x: image_array})#計算第二層卷積輸出
           

3.輸出卷積層結果

fig, axes = plt.subplots(, , figsize=(, ),
                         subplot_kw={'xticks': [], 'yticks': []})

    fig.subplots_adjust(left=, bottom=, right=, top=, wspace=)
    for i,ax in zip(range(), axes.flat):
        ax.imshow(conv2[,:,:,i])
    plt.show()
           
cnn神經網絡卷積層可視化