2 回答
TA贡献1796条经验 获得超7个赞
您可以使用 keras VGG16 API 轻松获取 VGG16 模型
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np
model = VGG16(weights='imagenet', include_top=False)
img_path = 'elephant.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
你read_image方法改变load_image方法。keras API 将调整图像大小以满足 vgg16 模型请求。
TA贡献2019条经验 获得超9个赞
如果您使用 opencv 读取图像,它应该是 cv2.imread()。就我使用 vgg16 而言,应该有一个 read_image 函数之前定义,它接收任何形状的图像,将其调整为标准形状(224 * 224 * 3)并进行所需的任何其他类型的预处理。
如果您使用的是 keras 预处理,它有一个预定义的 load_img 函数,可以为您进行预处理。
例子 :
from keras.preprocessing.image import load_img
load_image(path,target_size= (224*224*3))
我的样本加载功能
from keras.preprocessing import image
from keras.applications.imagenet_utils import decode_predictions, preprocess_input
def load_image(path):
img = image.load_img(path,target_size=model.input_shape[1:3])
x = image.img_to_array(img)
x = np.expand_dims(x,axis=0)
x = preprocess_input(x)
return img,x
添加回答
举报
