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

查看:1733
本文介绍了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天全站免登陆