功能:用于ImageNet数据预处理和预测解码的实用程序
keras-master\keras\applications\imagenet_utils.py
代码注释
"""Utilities for ImageNet data preprocessing & prediction decoding.
用于ImageNet数据预处理和预测解码的实用程序
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
import warnings
import numpy as np
from ..utils.data_utils import get_file
from .. import backend as K
CLASS_INDEX = None
CLASS_INDEX_PATH = 'https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json'
# Global tensor of imagenet mean for preprocessing symbolic inputs
# 预处理符号输入的imagenet mean全局张量
_IMAGENET_MEAN = None
def _preprocess_numpy_input(x, data_format, mode):
"""Preprocesses a Numpy array enquancoding a batch of images.
对一个数字阵列进行预处理,查询一批图像。
# Arguments
参数
x: Input array, 3D or 4D.
x: 输入数字,3维或4维
data_format: Data format of the image array.
data_format: 图像数组的数据格式
mode: One of "caffe", "tf" or "torch".
mode: "caffe", "tf" 或 "torch"之一
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- caffe:将图像从RGB转换为BGR,然后零中心化处理ImageNet数据集(图像)每个颜色通道,而无需缩放。
- tf: will scale pixels between -1 and 1,
sample-wise.
- tf:将在-1到1之间缩放像素,以样本为单位。
- torch: will scale pixels between 0 and 1 and then
will normalize each channel with respect to the
ImageNet dataset.
- torch:将在0和1之间缩放像素,然后将相对于ImageNet数据集规范每个通道。
# Returns
返回
Preprocessed Numpy array.
吃力后的Numpy数组
"""
if mode == 'tf':
x /= 127.5
x -= 1.
return x
if mode == 'torch':
x /= 255.
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
else:
if data_format == 'channels_first':
# 'RGB'->'BGR'
if x.ndim == 3:
x = x[::-1, ...]
else:
x = x[:, ::-1, ...]
else:
# 'RGB'->'BGR'
x = x[..., ::-1]
mean = [103.939, 116.779, 123.68]
std = None
# Zero-center by mean pixel
# 基于平均像素的零中心化
if data_format == 'channels_first':
if x.ndim == 3:
x[0, :, :] -= mean[0]
x[1, :, :] -= mean[1]
x[2, :, :] -= mean[2]
if std is not None:
x[0, :, :] /= std[0]
x[1, :, :] /= std[1]
x[2, :, :] /= std[2]
else:
x[:, 0, :, :] -= mean[0]
x[:, 1, :, :] -= mean[1]
x[:, 2, :, :] -= mean[2]
if std is not None:
x[:, 0, :, :] /= std[0]
x[:, 1, :, :] /= std[1]
x[:, 2, :, :] /= std[2]
else:
x[..., 0] -= mean[0]
x[..., 1] -= mean[1]
x[..., 2] -= mean[2]
if std is not None:
x[..., 0] /= std[0]
x[..., 1] /= std[1]
x[..., 2] /= std[2]
return x
def _preprocess_symbolic_input(x, data_format, mode):
"""Preprocesses a tensor encoding a batch of images.
预处理对一批图像进行编码的张量。
# Arguments
参数
x: Input tensor, 3D or 4D.
x: 输入数字,3维或4维
data_format: Data format of the image tensor.
data_format: 图像数组的数据格式
mode: One of "caffe", "tf" or "torch".
mode: "caffe", "tf" 或 "torch"之一
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- caffe:将图像从RGB转换为BGR,然后零中心化处理ImageNet数据集(图像)每个颜色通道,而无需缩放。
- tf: will scale pixels between -1 and 1,
sample-wise.
- tf:将在-1到1之间缩放像素,以样本为单位。
- torch: will scale pixels between 0 and 1 and then
will normalize each channel with respect to the
ImageNet dataset.
- torch:将在0和1之间缩放像素,然后将相对于ImageNet数据集规范每个通道。
# Returns
返回
Preprocessed tensor.
预处理过的张量
"""
global _IMAGENET_MEAN
if mode == 'tf':
x /= 127.5
x -= 1.
return x
if mode == 'torch':
x /= 255.
mean = [0.485, 0.456, 0.406]
std = [0.229, 0.224, 0.225]
else:
if data_format == 'channels_first':
# 'RGB'->'BGR'
if K.ndim(x) == 3:
x = x[::-1, ...]
else:
x = x[:, ::-1, ...]
else:
# 'RGB'->'BGR'
x = x[..., ::-1]
mean = [103.939, 116.779, 123.68]
std = None
if _IMAGENET_MEAN is None:
_IMAGENET_MEAN = K.constant(-np.array(mean))
# Zero-center by mean pixel
# 基于平均像素的零中心化
if K.dtype(x) != K.dtype(_IMAGENET_MEAN):
x = K.bias_add(x, K.cast(_IMAGENET_MEAN, K.dtype(x)), data_format)
else:
x = K.bias_add(x, _IMAGENET_MEAN, data_format)
if std is not None:
x /= std
return x
def preprocess_input(x, data_format=None, mode='caffe'):
"""Preprocesses a tensor or Numpy array encoding a batch of images.
预处理对一批图像进行编码的张量或Numpy数组。
# Arguments
参数
x: Input Numpy or symbolic tensor, 3D or 4D.
x: 输入Numpy或象征的张量,3维或4维
data_format: Data format of the image tensor/array.
data_format: 图像数组的数据格式
mode: One of "caffe", "tf".
mode: "caffe", "tf" 或 "torch"之一
- caffe: will convert the images from RGB to BGR,
then will zero-center each color channel with
respect to the ImageNet dataset,
without scaling.
- caffe:将图像从RGB转换为BGR,然后零中心化处理ImageNet数据集(图像)每个颜色通道,而无需缩放。
- tf: will scale pixels between -1 and 1,
sample-wise.
- tf:将在-1到1之间缩放像素,以样本为单位。
# Returns
返回
Preprocessed tensor or Numpy array.
预处理过的张量或Numpy数组
# Raises
补充
ValueError: In case of unknown `data_format` argument.
ValueError: 未知data_format`(数据格式)参数的情况
"""
if data_format is None:
data_format = K.image_data_format()
if data_format not in {'channels_first', 'channels_last'}:
raise ValueError('Unknown data_format ' + str(data_format))
if isinstance(x, np.ndarray):
return _preprocess_numpy_input(x, data_format=data_format, mode=mode)
else:
return _preprocess_symbolic_input(x, data_format=data_format,
mode=mode)
def decode_predictions(preds, top=5):
"""Decodes the prediction of an ImageNet model.
解码ImageNet模型的预测
# Arguments
参数
preds: Numpy tensor encoding a batch of predictions.
preds: (编码)一批预测的Numpy张量.
top: Integer, how many top-guesses to return.
top: 整数,有多少顶猜测返回。
# Returns
返回
A list of lists of top class prediction tuples
顶级预测元组列表
`(class_name, class_description, score)`.
`(类名, 类描述, 得分)`.
One list of tuples per sample in batch input.
批次输入的每个样本的一个元组列表
# Raises
补充
ValueError: In case of invalid shape of the `pred` array
(must be 2D).
ValueError: 预测数组(必须是2维)的无效形状情况
"""
global CLASS_INDEX
if len(preds.shape) != 2 or preds.shape[1] != 1000:
raise ValueError('`decode_predictions` expects '
'a batch of predictions '
'(i.e. a 2D array of shape (samples, 1000)). '
'Found array with shape: ' + str(preds.shape))
if CLASS_INDEX is None:
fpath = get_file('imagenet_class_index.json',
CLASS_INDEX_PATH,
cache_subdir='models',
file_hash='c2c37ea517e94d9795004a39431a14cb')
with open(fpath) as f:
CLASS_INDEX = json.load(f)
results = []
for pred in preds:
top_indices = pred.argsort()[-top:][::-1]
result = [tuple(CLASS_INDEX[str(i)]) + (pred[i],) for i in top_indices]
result.sort(key=lambda x: x[2], reverse=True)
results.append(result)
return results
def _obtain_input_shape(input_shape,
default_size,
min_size,
data_format,
require_flatten,
weights=None):
"""Internal utility to compute/validate a model's input shape.
内部实用程序来计算/验证模型的输入形状。
# Arguments
参数
input_shape: Either None (will return the default network input shape),
or a user-provided shape to be validated.
input_shape: 无(将返回默认的网络输入形状)或者用户提供的形状有效
default_size: Default input width/height for the model.
default_size: 模型的默认输入 宽度/高度。
min_size: Minimum input width/height accepted by the model.
min_size:模型的接受的最小输入 宽度/高度。
data_format: Image data format to use.
data_format:使用的图像数据格式
require_flatten: Whether the model is expected to
be linked to a classifier via a Flatten layer.
require_flatten:模型是否将通过平层被连接到一个分类器。
weights: One of `None` (random initialization)
or 'imagenet' (pre-training on ImageNet).
If weights='imagenet' input channels must be equal to 3.
weights: 无(随机初始化) 或 'imagenet'(ImageNet预处理)
如果 weights='imagenet' ,输入通道必须是3
# Returns
返回
An integer shape tuple (may include None entries).
整数形状元组(可不包括任何条目)。
# Raises
补充
ValueError: In case of invalid argument values.
ValueError: 在参数值无效的情况下。
"""
if weights != 'imagenet' and input_shape and len(input_shape) == 3:
if data_format == 'channels_first':
if input_shape[0] not in {1, 3}:
warnings.warn(
'This model usually expects 1 or 3 input channels. '
'However, it was passed an input_shape with ' +
str(input_shape[0]) + ' input channels.')
default_shape = (input_shape[0], default_size, default_size)
else:
if input_shape[-1] not in {1, 3}:
warnings.warn(
'This model usually expects 1 or 3 input channels. '
'However, it was passed an input_shape with ' +
str(input_shape[-1]) + ' input channels.')
default_shape = (default_size, default_size, input_shape[-1])
else:
if data_format == 'channels_first':
default_shape = (3, default_size, default_size)
else:
default_shape = (default_size, default_size, 3)
if weights == 'imagenet' and require_flatten:
if input_shape is not None:
if input_shape != default_shape:
raise ValueError('When setting`include_top=True` '
'and loading `imagenet` weights, '
'`input_shape` should be ' +
str(default_shape) + '.')
return default_shape
if input_shape:
if data_format == 'channels_first':
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[0] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape=' + str(input_shape) + '`')
if ((input_shape[1] is not None and input_shape[1] < min_size) or
(input_shape[2] is not None and input_shape[2] < min_size)):
raise ValueError('Input size must be at least ' +
str(min_size) + 'x' + str(min_size) + '; got '
'`input_shape=' + str(input_shape) + '`')
else:
if input_shape is not None:
if len(input_shape) != 3:
raise ValueError(
'`input_shape` must be a tuple of three integers.')
if input_shape[-1] != 3 and weights == 'imagenet':
raise ValueError('The input must have 3 channels; got '
'`input_shape=' + str(input_shape) + '`')
if ((input_shape[0] is not None and input_shape[0] < min_size) or
(input_shape[1] is not None and input_shape[1] < min_size)):
raise ValueError('Input size must be at least ' +
str(min_size) + 'x' + str(min_size) + '; got '
'`input_shape=' + str(input_shape) + '`')
else:
if require_flatten:
input_shape = default_shape
else:
if data_format == 'channels_first':
input_shape = (3, None, None)
else:
input_shape = (None, None, 3)
if require_flatten:
if None in input_shape:
raise ValueError('If `include_top` is True, '
'you should specify a static `input_shape`. '
'Got `input_shape=' + str(input_shape) + '`')
return input_shape
代码执行
Keras详细介绍
中文:http://keras-cn.readthedocs.io/en/latest/
实例下载
https://github.com/keras-team/keras
https://github.com/keras-team/keras/tree/master/examples
完整项目下载
方便没积分童鞋,请加企鹅452205574,共享文件夹。
包括:代码、数据集合(图片)、已生成model、安装库文件等。