admin管理员组

文章数量:1576346

"""
调用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])

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

本文标签: 模型图像特征