本文整理汇总了Python中keras.backend.resize_images方法的典型用法代码示例。如果您正苦于以下问题:Python backend.resize_images方法的具体用法?Python backend.resize_images怎么用?Python backend.resize_images使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类keras.backend
的用法示例。
在下文中一共展示了backend.resize_images方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: resize_image
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import resize_images [as 别名]
def resize_image(inp, s, data_format):
try:
return Lambda(lambda x: K.resize_images(x,
height_factor=s[0],
width_factor=s[1],
data_format=data_format,
interpolation='bilinear'))(inp)
except Exception as e:
# if keras is old, then rely on the tf function
# Sorry theano/cntk users!!!
assert data_format == 'channels_last'
assert IMAGE_ORDERING == 'channels_last'
import tensorflow as tf
return Lambda(
lambda x: tf.image.resize_images(
x, (K.int_shape(x)[1]*s[0], K.int_shape(x)[2]*s[1]))
)(inp)
示例2: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import resize_images [as 别名]
def call(self, inputs):
return K.resize_images(inputs, self.factor, self.factor, self.data_format)
示例3: Interp
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import resize_images [as 别名]
def Interp(x, shape):
from keras.backend import tf as ktf
new_height, new_width = shape
resized = ktf.image.resize_images(x, [int(new_height), int(new_width)], align_corners=True)
return resized
# interpolation block
示例4: _resize_nearest_neighbour
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import resize_images [as 别名]
def _resize_nearest_neighbour(self, input_tensor, size):
""" Resize a tensor using nearest neighbor interpolation.
Notes
-----
Tensorflow has a bug that resizes the image incorrectly if :attr:`align_corners` is not set
to ``True``. Keras Backend does not set this flag, so we explicitly call the Tensorflow
operation for non-amd backends.
Parameters
----------
input_tensor: tensor
The tensor to be resized
tuple: int
The (`h`, `w`) that the tensor should be resized to (used for non-amd backends only)
Returns
-------
tensor
The input tensor resized to the given size
"""
if get_backend() == "amd":
retval = K.resize_images(input_tensor, self.scale, self.scale, "channels_last",
interpolation="nearest")
else:
retval = tf.image.resize_nearest_neighbor(input_tensor, size=size, align_corners=True)
logger.debug("Input Tensor: %s, Output Tensor: %s", input_tensor, retval)
return retval
示例5: _backward_pass
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import resize_images [as 别名]
def _backward_pass(self, X, target_layer, d_switch, feat_map):
# Run deconv/maxunpooling until input pixel space
layer_index = self.lnames.index(target_layer)
# Get the output of the target_layer of interest
layer_output = K.function(
[self[self.lnames[0]].input], self[target_layer].output)
X_outl = layer_output([X])
# Special case for the starting layer where we may want
# to switchoff somes maps/ activations
print("Deconvolving %s..." % target_layer)
if "maxpooling2d" in target_layer:
X_maxunp = K.pool.max_pool_2d_same_size(
self[target_layer].input, self[target_layer].pool_size)
unpool_func = K.function([self[self.lnames[0]].input], X_maxunp)
X_outl = unpool_func([X])
if feat_map is not None:
for i in range(X_outl.shape[1]):
if i != feat_map:
X_outl[:, i, :, :] = 0
for i in range(X_outl.shape[0]):
iw, ih = np.unravel_index(
X_outl[i, feat_map, :, :].argmax(), X_outl[i, feat_map, :, :].shape)
m = np.max(X_outl[i, feat_map, :, :])
X_outl[i, feat_map, :, :] = 0
X_outl[i, feat_map, iw, ih] = m
elif "conv2d" in target_layer:
X_outl = self._deconv(X_outl, target_layer,
d_switch, feat_map=feat_map)
else:
raise ValueError(
"Invalid layer name: %s \n Can only handle maxpool and conv" % target_layer)
# Iterate over layers (deepest to shallowest)
for lname in self.lnames[:layer_index][::-1]:
print("Deconvolving %s..." % lname)
# Unpool, Deconv or do nothing
if "maxpooling2d" in lname:
p1, p2 = self[lname].pool_size
uppool = K.function(
[self.x], K.resize_images(self.x, p1, p2, "th"))
X_outl = uppool([X_outl])
elif "conv2d" in lname:
X_outl = self._deconv(X_outl, lname, d_switch)
elif "padding" in lname:
pass
else:
raise ValueError(
"Invalid layer name: %s \n Can only handle maxpool and conv" % lname)
return X_outl
示例6: call
# 需要导入模块: from keras import backend [as 别名]
# 或者: from keras.backend import resize_images [as 别名]
def call(self, input_tensor, training=None):
input_transposed = tf.transpose(input_tensor, [3, 0, 1, 2, 4])
input_shape = K.shape(input_transposed)
input_tensor_reshaped = K.reshape(input_transposed, [
input_shape[1] * input_shape[0], self.input_height, self.input_width, self.input_num_atoms])
input_tensor_reshaped.set_shape((None, self.input_height, self.input_width, self.input_num_atoms))
if self.upsamp_type == 'resize':
upsamp = K.resize_images(input_tensor_reshaped, self.scaling, self.scaling, 'channels_last')
outputs = K.conv2d(upsamp, kernel=self.W, strides=(1, 1), padding=self.padding, data_format='channels_last')
elif self.upsamp_type == 'subpix':
conv = K.conv2d(input_tensor_reshaped, kernel=self.W, strides=(1, 1), padding='same',
data_format='channels_last')
outputs = tf.depth_to_space(conv, self.scaling)
else:
batch_size = input_shape[1] * input_shape[0]
# Infer the dynamic output shape:
out_height = deconv_length(self.input_height, self.scaling, self.kernel_size, self.padding)
out_width = deconv_length(self.input_width, self.scaling, self.kernel_size, self.padding)
output_shape = (batch_size, out_height, out_width, self.num_capsule * self.num_atoms)
outputs = K.conv2d_transpose(input_tensor_reshaped, self.W, output_shape, (self.scaling, self.scaling),
padding=self.padding, data_format='channels_last')
votes_shape = K.shape(outputs)
_, conv_height, conv_width, _ = outputs.get_shape()
votes = K.reshape(outputs, [input_shape[1], input_shape[0], votes_shape[1], votes_shape[2],
self.num_capsule, self.num_atoms])
votes.set_shape((None, self.input_num_capsule, conv_height.value, conv_width.value,
self.num_capsule, self.num_atoms))
logit_shape = K.stack([
input_shape[1], input_shape[0], votes_shape[1], votes_shape[2], self.num_capsule])
biases_replicated = K.tile(self.b, [votes_shape[1], votes_shape[2], 1, 1])
activations = update_routing(
votes=votes,
biases=biases_replicated,
logit_shape=logit_shape,
num_dims=6,
input_dim=self.input_num_capsule,
output_dim=self.num_capsule,
num_routing=self.routings)
return activations