当前位置:   article > 正文

VGG19模型,输入图像,输出指定层特征_vgg19输出每一层特征图

vgg19输出每一层特征图
"""
调用VGG19模型
输入一张彩色图像
输出指定层的特征
VGG19模型的输入图像是四维的,但是加载进去的图像是三维的,所以要提前reshape
指定层的输出是四维的,所以要reshape到三维
如果要可视化,使用spyder只能显示而是图像
"""

from __future__ import print_function
from keras.preprocessing.image import load_img, save_img, img_to_array
import numpy as np
from scipy.optimize import fmin_l_bfgs_b
import time
import argparse
import matplotlib.image as mpimg # mpimg 用于读取图片
from keras.applications import vgg19
from keras import backend as K
import matplotlib.pyplot as plt
from keras.preprocessing import image
import os
model = vgg19.VGG19(weights='imagenet', include_top=False,)
from keras.models import Model 

#print(model.layers)

img_path ='C:\\Users\\Administrator\\Pictures\\Saved Pictures\\kobe.jpg'
img=image.load_img(img_path)
img=np.array(img)
a,b,c=img.shape
img=img.reshape(1,a,b,c)
#VGG19模型的实际深度是26,本文使用的模型去掉了三个全连接层
layer_model=Model(inputs=model.input,outputs=model.layers[20].output)

out=layer_model.predict(img)
print(out.shape)
w,x,y,z=out.shape
out=out.reshape(x,y,z)

plt.imshow(out[:,:,40])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

指定某一层的的输出特征图

在这里插入图片描述在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/340874
推荐阅读
相关标签
  

闽ICP备14008679号