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
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值