深度网络特定层数据抽取实验

来源:互联网 发布:vscode c 智能提示 编辑:程序博客网 时间:2024/05/02 01:34
徐海蛟教学

VGG-16 是牛津大学视觉几何组(Visual Geometry Group)开发的卷积神经网络结构,该深度学习神经网络赢得了 ILSVR 竞赛(ImageNet)2014的冠军,时至今日,VGG 仍然被认为是一个杰出的视觉模型。今天,我们来做基于 VGG16 深度学习预训练权重的特定层 "block4_pool" 特征数据抽取实验,以便后续进一步处理。

数据集使用目前最大的图像数据集 ImageNet 。

我们考虑基于 Keras 深度学习框架 来完成该深度学习实验。

我们的环境如下。

Python 3.6.0 |Anaconda 4.3.0 (64-bit)| (default, Dec 23 2016, 12:22:00)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on Linux
>>>

Theano (0.8.2)
Keras (1.2.0)

python 代码如下。
# 导入 python 软件包
import time
from keras.applications.imagenet_utils import preprocess_input
from keras.applications.vgg16 import VGG16
from keras.models import Model
from keras.preprocessing import image
import numpy as np

t0 = time.time()   # 开启计时器
base_model = VGG16(weights='imagenet')
model = Model(  # 只输出block4_pool**"特定层"**图像特征
    input=base_model.input, output=base_model.get_layer('block4_pool').output)

img_path = '高圆圆.jpg'   # 测试图片
img = image.load_img(img_path, target_size=(224, 224))  # 224×224
x = image.img_to_array(img)  # 三维(3, 224, 224)
x = np.expand_dims(x, axis=0)  # 四维(1, 3, 224, 224)
x = preprocess_input(x)
print("取数据耗时: %.2f seconds【徐海蛟博士】 ..." % (time.time() - t0))
features = model.predict(x)  # block4_pool_features
print('block4_pool特征 features.shape =', features.shape)  # (1, 512, 7, 7)
print("耗时: %.2f seconds【徐海蛟博士】 ..." % (time.time() - t0))


输出如下。
Using Theano backend.
Using gpu device 3: TITAN X (Pascal) (CNMeM is enabled with initial size: 95.0% of memory, cuDNN 5005)
取数据耗时: 3.16 seconds【徐海蛟博士】 ...
block4_pool特征 features.shape = (1, 512, 14, 14)
耗时: 7.19 seconds【徐海蛟博士】 ...
1 0
原创粉丝点击