【深度学习图像识别课程】keras实现CNN系列:(9)bottleneck特征生成

本文介绍如何使用VGG16模型去除最后的全连接层来提取图像的瓶颈特征。通过具体步骤演示了从加载预处理图像到利用修改后的VGG16模型进行预测的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、bottleneck特征说明

将所有图像穿过(VGG16去掉最后全连接层)得到输出,作为新的输入。

 

二、bottleneck特征提取代码

1、加载预处理图像库

from keras.applications.vgg16 import preprocess_input
from keras.preprocessing import image
import numpy as np
import glob

img_paths = glob.glob("images-bottleneck/*.jpg")

def path_to_tensor(img_path):
    # loads RGB image as PIL.Image.Image type
    img = image.load_img(img_path, target_size=(224, 224))
    # convert PIL.Image.Image type to 3D tensor with shape (224, 224, 3)
    x = image.img_to_array(img)
    # convert 3D tensor to 4D tensor with shape (1, 224, 224, 3) and return 4D tensor
    return np.expand_dims(x, axis=0)

def paths_to_tensor(img_paths):
    list_of_tensors = [path_to_tensor(img_path) for img_path in img_paths]
    return np.vstack(list_of_tensors)

# calculate the image input. you will learn more about how this works the project!
img_input = preprocess_input(paths_to_tensor(img_paths))

print(img_input.shape)

 

2、导入VGG16

from keras.applications.vgg16 import VGG16
model = VGG16()
model.summary()

model.predict(img_input).shape

 

3、导入VGG16,但去掉最后3个全连接层

fromfrom  keras.applications.vgg16keras.a  import VGG16
model = VGG16(include_top=False)
model.summary()

 

4、最大池化层的输出

print(model.predict(img_input).shape)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值