keras 中的 preprocess_input() 方法 [英] preprocess_input() method in keras

查看:42
本文介绍了keras 中的 preprocess_input() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以下 keras 文档页面中的示例 keras 代码,https://keras.io/applications/

I am trying out sample keras code from the below keras documentation page, https://keras.io/applications/

keras 模块的 preprocess_input(x) 函数在下面的代码中做了什么?为什么我们必须在将其传递给 preprocess_input() 方法之前执行 expand_dims(x,axis=0)?

What preprocess_input(x) function of keras module does in the below code? Why do we have to do expand_dims(x, axis=0) before that is passed to the preprocess_input() method?

from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
import numpy as np

model = ResNet50(weights='imagenet')

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)

是否有任何文档对这些功能进行了很好的解释?

Is there any documentation with a good explanation of these functions?

谢谢!

推荐答案

Keras 处理批量图像.因此,第一个维度用于表示您拥有的样本(或图像)数量.

Keras works with batches of images. So, the first dimension is used for the number of samples (or images) you have.

当你加载一张图片时,你会得到一张图片的形状,即(size1,size2,channels).

When you load a single image, you get the shape of one image, which is (size1,size2,channels).

为了创建一批图像,你需要一个额外的维度:(samples, size1,size2,channels)

In order to create a batch of images, you need an additional dimension: (samples, size1,size2,channels)

preprocess_input 函数旨在使您的图像适合模型所需的格式.

The preprocess_input function is meant to adequate your image to the format the model requires.

某些模型使用值范围从 0 到 1 的图像.其他模型从 -1 到 +1.其他人使用caffe"样式,未标准化,而是居中.

Some models use images with values ranging from 0 to 1. Others from -1 to +1. Others use the "caffe" style, that is not normalized, but is centered.

来自 源代码,Resnet正在使用caffe风格.

From the source code, Resnet is using the caffe style.

您无需担心preprocess_input 的内部细节.但理想情况下,您应该为此使用 keras 函数加载图像(因此您保证加载的图像与 preprocess_input 兼容).

You don't need to worry about the internal details of preprocess_input. But ideally, you should load images with the keras functions for that (so you guarantee that the images you load are compatible with preprocess_input).

这篇关于keras 中的 preprocess_input() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆