open cv提取图片特征值_基于VGG16网络提取Flicker8K数据集图像特征

该代码使用预训练的VGG16模型从Flicker8K数据集的图像中提取特征。首先加载模型,然后预处理输入图像,将其转换为模型所需的格式,并使用模型进行预测,将提取的特征保存到pickle文件中。
摘要由CSDN通过智能技术生成

# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : ${20200326} ${18:00}
# @Author : ZicoZhou
# @Version :1.0
# @Function : VGG16网络提取图像特征
from keras.models import model_from_json
from keras.models import Model
from PIL import Image as pil_image
from keras import backend as K
import numpy as np
import pickle
import os
import keras
import time
os.chdir(os.path.split(os.path.realpath(__file__))[0])
print(os.getcwd())
def load_vgg16_model():"""从当前目录下的vgg16_exported.json和vgg16_exported.h5文件导入vgg16网络并返回创建的网络模型 # Returns 创建的网络模型Models """
with open("G:/KeepOnStudying/ML/Documents/CV/PycharmProjects/Image_captioning/task1/vgg16_exported.json", "r") as json_file:
loaded_model_json= json_file.read()
loaded_model= model_from_json(loaded_model_json)
loaded_model.load_weights("G:/KeepOnStudying/ML/Documents/CV/PycharmProjects/Image_captioning/task1/vgg16_exported.h5")
return loaded_model
def preprocess_input(x):"""预处理图像用于网络输入,将图像由RGB格式转换为BGR格式 将图像的每一个图像通道减去其均值 #:argument numpy数组,4维 data_format: Data format of the Image array # Returns preprocessed numpy array """
x_red = x[:, :, :, 0].copy()
x[:, :, :, 0] = x[:, :, :, 2]
x[:, :, :, 2] = x_red
x[:, :, :, 0] -= np.mean(x[:, :, :, 0])
x[:, :, :, 1] -= np.mean(x[:, :, :, 1])
x[:, :, :, 2] -= np.mean(x[:, :, :, 2])
return x
def load_img_as_np_array(path,target_size):"""从指定文件加载图像,转换图像为target_size,返回2位浮点数numpy数组, :param path: 图像文件路径 :param target_size:元组(图像高度,图像宽度) :returns A PIL Image instance """
img=pil_image.open(path)
img=img.resize(target_size,pil_image.NEAREST)
return np.asarray(img,dtype=K.floatx())
def extract_features(directory):"""提取给定文件夹中所有图像的特征,将提取的特征保存在features.pkl中 提取的文件保存在一个dict中,key为文件名,value为特征值【np.array】 :param directory:包含.JPG文件的文件夹, :returns:None """
model = load_vgg16_model()
model.layers.pop()
model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
print("the model used is summarized as follow:")
model.summary()
features = {}
count = 0
for fn in os.listdir(directory):
img_id = os.path.splitext(fn)[0]
if (img_id[-4] == "."): continue
count += 1
print("[" + str(count) + "] " + img_id)
fn = os.path.join(directory, fn)
arr = load_img_as_np_array(fn, (224, 224))
arr = np.reshape(arr, (1, arr.shape[0], arr.shape[1], arr.shape[2]))
arr = preprocess_input(arr)
feature = model.predict(arr, verbose=0)
features[str(img_id)] = feature
return features
if __name__ == '__main__':
# 提取所有图像的特征,保存在一个文件中, 大约一小时的时间,最后的文件大小为127M
directory='G:/KeepOnStudying/ML/Documents/CV/PycharmProjects/Image_captioning/task1/Flicker8K'
time_start=time.time()
features = extract_features(directory)
time_end=time.time()
print("time cost : ",format(time_end-time_start))
print("提取特征文件个数: " ,format(len(features)))
print(keras.backend.image_data_format())
#保存特征到文件
pickle.dump(features,open('features.pkl','wb'))
print("I am lucky and finish this summary for sharing")
print('Your evalution and correction is waited')
print('Thanks a lot for your good teaching, especially Jerry')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值