def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(2, num_channels, image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(2, channels)
python类rank()的实例源码
def assert_rank_at_least_one(tensor, name):
"""
Whether the rank of `tensor` is at least one.
:param tensor: A tensor to be checked.
:param name: The name of `tensor` for error message.
:return: The checked tensor.
"""
return assert_rank_at_least(tensor, 1, name)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=2, values=channels)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=2, values=channels)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=2, values=channels)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=2, values=channels)
def normalize_contrast(x):
"""Normalize contrast of an image: forces values to be stricly in [0, 1]
:param x: image tensor
:return:
"""
idx = tf.range(1, tf.rank(x))
min = tf.reduce_min(x, idx, keep_dims=True)
max = tf.reduce_max(x, idx, keep_dims=True)
return (x - min) / (max - min + 1e-5)
def intersection_within(bbox, within):
"""Returns the coordinates of the intersection of `bbox` and `within`
with respect to `within`
:param bbox:
:param within:
:return:
"""
x1 = tf.maximum(bbox[..., 1], within[..., 1])
y1 = tf.maximum(bbox[..., 0], within[..., 0])
x2 = tf.minimum(bbox[..., 1] + bbox[..., 3], within[..., 1] + within[..., 3])
y2 = tf.minimum(bbox[..., 0] + bbox[..., 2], within[..., 0] + within[..., 2])
w = x2 - x1
w = tf.where(tf.less_equal(w, 0), tf.zeros_like(w), w)
h = y2 - y1
h = tf.where(tf.less_equal(h, 0), tf.zeros_like(h), h)
y = y1 - within[..., 0]
x = x1 - within[..., 1]
area = h * w
y = tf.where(tf.greater(area, 0.), y, tf.zeros_like(y))
x = tf.where(tf.greater(area, 0.), x, tf.zeros_like(x))
rank = len(bbox.get_shape()) - 1
return tf.stack((y, x, h, w), rank)
def extract_glimpse(inpt, attention_params, glimpse_size):
"""Extracts an attention glimpse
:param inpt: tensor of shape == (batch_size, img_height, img_width)
:param attention_params: tensor of shape = (batch_size, 6) as
[uy, sy, dy, ux, sx, dx] with u - mean, s - std, d - stride"
:param glimpse_size: 2-tuple of ints as (height, width),
size of the extracted glimpse
:return: tensor
"""
ap = attention_params
shape = inpt.get_shape()
rank = len(shape)
assert rank in (3, 4), "Input must be 3 or 4 dimensional tensor"
inpt_H, inpt_W = shape[1:3]
if rank == 3:
inpt = inpt[..., tf.newaxis]
rank += 1
Fy = gaussian_mask(ap[..., 0::2], glimpse_size[0], inpt_H)
Fx = gaussian_mask(ap[..., 1::2], glimpse_size[1], inpt_W)
gs = []
for channel in tf.unstack(inpt, axis=rank - 1):
g = tf.matmul(tf.matmul(Fy, channel, adjoint_a=True), Fx)
gs.append(g)
g = tf.stack(gs, axis=rank - 1)
g.set_shape([shape[0]] + list(glimpse_size))
return g
def attention_to_bbox(self, att):
with tf.variable_scope('attention_to_bbox'):
yx = att[..., :2] * self.inpt_size[np.newaxis, :2]
hw = att[..., 2:4] * (self.inpt_size[np.newaxis, :2] - 1)
bbox = tf.concat(axis=tf.rank(att) - 1, values=(yx, hw))
bbox.set_shape(att.get_shape()[:-1].concatenate((4,)))
return bbox
def bbox_to_attention(self, bbox):
with tf.variable_scope('ratm_bbox_to_attention'):
us = bbox[..., :2] / self.inpt_size[np.newaxis, :2]
ss = 0.5 * bbox[..., 2:] / self.inpt_size[np.newaxis, :2]
ds = bbox[..., 2:] / (self.inpt_size[np.newaxis, :2] - 1.)
att = tf.concat(axis=tf.rank(bbox) - 1, values=(us, ss, ds))
return att
def _to_attention(self, raw_att, with_bias=True):
bbox = FixedStdAttention.attention_to_bbox(self, raw_att)
us = bbox[..., :2]
if with_bias:
us += self.offset_bias
ds = bbox[..., 2:4] / (self.glimpse_size[np.newaxis, :2] - 1)
ss = self._stride_to_std(ds)
ap = tf.concat(axis=tf.rank(raw_att) - 1, values=(us, ss, ds), name='attention')
ap.set_shape(raw_att.get_shape()[:-1].concatenate((6,)))
return ap
def _mask(expr, mask):
assert mask.dtype == tf.bool, '`mask`.dtype has to be tf.bool'
mask_rank = tf.rank(mask)
sample_shape = tf.shape(expr)[mask_rank:]
flat_shape = tf.concat(([-1], sample_shape), 0)
flat_expr = tf.reshape(expr, flat_shape)
flat_mask = tf.reshape(mask, (-1,))
return tf.boolean_mask(flat_expr, flat_mask)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=2, values=channels)
def __init__(self, input_dim, output_dim, rank, active_dims=None, name=None):
"""
A Coregionalization kernel. The inputs to this kernel are _integers_
(we cast them from floats as needed) which usually specify the
*outputs* of a Coregionalization model.
The parameters of this kernel, W, kappa, specify a positive-definite
matrix B.
B = W W^T + diag(kappa) .
The kernel function is then an indexing of this matrix, so
K(x, y) = B[x, y] .
We refer to the size of B as "num_outputs x num_outputs", since this is
the number of outputs in a coregionalization model. We refer to the
number of columns on W as 'rank': it is the number of degrees of
correlation between the outputs.
NB. There is a symmetry between the elements of W, which creates a
local minimum at W=0. To avoid this, it's recommended to initialize the
optimization (or MCMC chain) using a random W.
"""
assert input_dim == 1, "Coregion kernel in 1D only"
super().__init__(input_dim, active_dims, name=name)
self.output_dim = output_dim
self.rank = rank
self.W = Parameter(np.zeros((self.output_dim, self.rank), dtype=settings.float_type))
self.kappa = Parameter(np.ones(self.output_dim, dtype=settings.float_type), transform=transforms.positive)
def _crop(image, offset_height, offset_width, crop_height, crop_width):
"""Crops the given image using the provided offsets and sizes.
Note that the method doesn't assume we know the input image size but it does
assume we know the input image rank.
Args:
image: an image of shape [height, width, channels].
offset_height: a scalar tensor indicating the height offset.
offset_width: a scalar tensor indicating the width offset.
crop_height: the height of the cropped image.
crop_width: the width of the cropped image.
Returns:
the cropped (and resized) image.
Raises:
InvalidArgumentError: if the rank is not 3 or if the image dimensions are
less than the crop size.
"""
original_shape = tf.shape(image)
rank_assertion = tf.Assert(
tf.equal(tf.rank(image), 3),
['Rank of image must be equal to 3.'])
with tf.control_dependencies([rank_assertion]):
cropped_shape = tf.stack([crop_height, crop_width, original_shape[2]])
size_assertion = tf.Assert(
tf.logical_and(
tf.greater_equal(original_shape[0], crop_height),
tf.greater_equal(original_shape[1], crop_width)),
['Crop size greater than the image size.'])
offsets = tf.to_int32(tf.stack([offset_height, offset_width, 0]))
# Use tf.slice instead of crop_to_bounding box as it accepts tensors to
# define the crop size.
with tf.control_dependencies([size_assertion]):
image = tf.slice(image, offsets, cropped_shape)
return tf.reshape(image, cropped_shape)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(axis=2, num_or_size_splits=num_channels, value=image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(axis=2, values=channels)
def _mean_image_subtraction(image, means):
"""Subtracts the given means from each image channel.
For example:
means = [123.68, 116.779, 103.939]
image = _mean_image_subtraction(image, means)
Note that the rank of `image` must be known.
Args:
image: a tensor of size [height, width, C].
means: a C-vector of values to subtract from each channel.
Returns:
the centered image.
Raises:
ValueError: If the rank of `image` is unknown, if `image` has a rank other
than three or if the number of channels in `image` doesn't match the
number of values in `means`.
"""
if image.get_shape().ndims != 3:
raise ValueError('Input must be of size [height, width, C>0]')
num_channels = image.get_shape().as_list()[-1]
if len(means) != num_channels:
raise ValueError('len(means) must match the number of channels')
channels = tf.split(2, num_channels, image)
for i in range(num_channels):
channels[i] -= means[i]
return tf.concat(channels, axis=2)
def _transpose_batch_time(x):
"""Transpose the batch and time dimensions of a Tensor.
Retains as much of the static shape information as possible.
Args:
x: A tensor of rank 2 or higher.
Returns:
x transposed along the first two dimensions.
Raises:
ValueError: if `x` is rank 1 or lower.
"""
x_static_shape = x.get_shape()
if x_static_shape.ndims is not None and x_static_shape.ndims < 2:
raise ValueError(
"Expected input tensor %s to have rank at least 2, but saw shape: %s" %
(x, x_static_shape))
x_rank = tf.rank(x)
x_t = tf.transpose(
x, tf.concat(
([1, 0], tf.range(2, x_rank)), axis=0))
x_t.set_shape(
[x_static_shape[1].value, x_static_shape[0].value] + x_static_shape[2:])
return x_t
def run_inception(images,
graph_def=None,
default_graph_def_fn=_default_graph_def_fn,
image_size=INCEPTION_DEFAULT_IMAGE_SIZE,
input_tensor=INCEPTION_INPUT,
output_tensor=INCEPTION_OUTPUT):
"""Run images through a pretrained Inception classifier.
Args:
images: Input tensors. Must be [batch, height, width, channels]. Input shape
and values must be in [-1, 1], which can be achieved using
`preprocess_image`.
graph_def: A GraphDef proto of a pretrained Inception graph. If `None`,
call `default_graph_def_fn` to get GraphDef.
default_graph_def_fn: A function that returns a GraphDef. Used if
`graph_def` is `None. By default, returns a pretrained InceptionV3 graph.
image_size: Required image width and height. See unit tests for the default
values.
input_tensor: Name of input Tensor.
output_tensor: Name of output Tensor. This function will compute activations
at the specified layer. Examples include INCEPTION_V3_OUTPUT and
INCEPTION_V3_FINAL_POOL which would result in this function computing
the final logits or the penultimate pooling layer.
Returns:
Logits.
Raises:
ValueError: If images are not the correct size.
ValueError: If neither `graph_def` nor `default_graph_def_fn` are provided.
"""
images = _validate_images(images, image_size)
if graph_def is None:
if default_graph_def_fn is None:
raise ValueError('If `graph_def` is `None`, must provide '
'`default_graph_def_fn`.')
graph_def = default_graph_def_fn()
activations = run_image_classifier(images, graph_def, input_tensor,
output_tensor)
if tf.rank(activations) != 2:
activations = flatten(activations)
return activations