给定一张指定图片“22.png”,在指定图片库“database目录”中检索出与其相似度最高的3张图片。
1. 使用深度神经网络提取图片特征
1.1 vgg16提取图片特征
# -*- coding: UTF-8 -*-
import numpy as np
import h5py
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
from keras.applications.vgg16 import VGG16
from keras.applications.vgg16 import preprocess_input as preprocess_input_vgg
from keras.preprocessing import image
class VGGNet:
def __init__(self):
self.input_shape = (224, 224, 3)
self.weight = 'imagenet' # None代表随机初始化,即不加载预训练权重
self.pooling = 'max' # avg
self.model_vgg = VGG16(weights=self.weight,
input_shape=(self.input_shape[0], self.input_shape[1], self.input_shape[2]),
pooling=self.pooling,
include_top=False)
# self.model_vgg.predict(np.zeros((1, 224, 224, 3)))
# 提取v