本文整理汇总了Python中scipy.ndimage.zoom方法的典型用法代码示例。如果您正苦于以下问题:Python ndimage.zoom方法的具体用法?Python ndimage.zoom怎么用?Python ndimage.zoom使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.ndimage
的用法示例。
在下文中一共展示了ndimage.zoom方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: predict_multiscale
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def predict_multiscale(net, image, tile_size, scales, classes, flip_evaluation, recurrence):
"""
Predict an image by looking at it with different scales.
We choose the "predict_whole_img" for the image with less than the original input size,
for the input of larger size, we would choose the cropping method to ensure that GPU memory is enough.
"""
image = image.data
N_, C_, H_, W_ = image.shape
full_probs = np.zeros((H_, W_, classes))
for scale in scales:
scale = float(scale)
print("Predicting image scaled by %f" % scale)
scale_image = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
scaled_probs = predict_whole(net, scale_image, tile_size, recurrence)
if flip_evaluation == True:
flip_scaled_probs = predict_whole(net, scale_image[:,:,:,::-1].copy(), tile_size, recurrence)
scaled_probs = 0.5 * (scaled_probs + flip_scaled_probs[:,::-1,:])
full_probs += scaled_probs
full_probs /= len(scales)
return full_probs
示例2: predict_multiscale
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def predict_multiscale(net, image, tile_size, scales, classes, flip_evaluation):
"""
Predict an image by looking at it with different scales.
We choose the "predict_whole_img" for the image with less than the original input size,
for the input of larger size, we would choose the cropping method to ensure that GPU memory is enough.
"""
image = image.data
N_, C_, H_, W_ = image.shape
full_probs = np.zeros((H_, W_, classes))
for scale in scales:
scale = float(scale)
print("Predicting image scaled by %f" % scale)
scale_image = ndimage.zoom(image, (1.0, 1.0, scale, scale), order=1, prefilter=False)
scaled_probs = predict_whole(net, scale_image, tile_size)
if flip_evaluation == True:
flip_scaled_probs = predict_whole(net, scale_image[:, :, :, ::-1].copy(), tile_size)
scaled_probs = 0.5 * (scaled_probs + flip_scaled_probs[:, ::-1, :])
full_probs += scaled_probs
full_probs /= len(scales)
return full_probs
示例3: clipped_zoom
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def clipped_zoom(img, zoom_factor):
h = img.shape[0]
# ceil crop height(= crop width)
ch = int(np.ceil(h / zoom_factor))
top = (h - ch) // 2
img = scizoom(img[top:top + ch, top:top + ch], (zoom_factor, zoom_factor, 1), order=1)
# trim off any extra pixels
trim_top = (img.shape[0] - h) // 2
return img[trim_top:trim_top + h, trim_top:trim_top + h]
# /////////////// End Distortion Helpers ///////////////
# /////////////// Distortions ///////////////
示例4: generate
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def generate(values, nb_classes, batch_size, input_size, image_dir, anno_dir):
while 1:
random.shuffle(values)
images, labels = update_inputs(batch_size=batch_size,
input_size=input_size, num_classes=nb_classes)
for i, d in enumerate(values):
img = imresize(imread(os.path.join(image_dir, d['image']), mode='RGB'), input_size)
y = imread(os.path.join(anno_dir, d['anno']), mode='L')
h, w = input_size
y = zoom(y, (1.*h/y.shape[0], 1.*w/y.shape[1]), order=1, prefilter=False)
y = (np.arange(nb_classes) == y[:,:,None]).astype('float32')
assert y.shape[2] == nb_classes
images[i % batch_size] = img
labels[i % batch_size] = y
if (i + 1) % batch_size == 0:
yield images, labels
images, labels = update_inputs(batch_size=batch_size,
input_size=input_size, num_classes=nb_classes)
示例5: predict_multi_scale
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def predict_multi_scale(full_image, net, scales, sliding_evaluation, flip_evaluation):
"""Predict an image by looking at it with different scales."""
classes = net.model.outputs[0].shape[3]
full_probs = np.zeros((full_image.shape[0], full_image.shape[1], classes))
h_ori, w_ori = full_image.shape[:2]
for scale in scales:
print("Predicting image scaled by %f" % scale)
scaled_img = misc.imresize(full_image, size=scale, interp="bilinear")
if sliding_evaluation:
scaled_probs = predict_sliding(scaled_img, net, flip_evaluation)
else:
scaled_probs = net.predict(scaled_img, flip_evaluation)
# scale probs up to full size
h, w = scaled_probs.shape[:2]
probs = ndimage.zoom(scaled_probs, (1.*h_ori/h, 1.*w_ori/w, 1.),order=1, prefilter=False)
# visualize_prediction(probs)
# integrate probs over all scales
full_probs += probs
full_probs /= len(scales)
return full_probs
示例6: test_correct_results
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def test_correct_results(self, min_zoom, max_zoom, mode, align_corners, keep_size):
key = "img"
random_zoom = RandZoomd(
key,
prob=1.0,
min_zoom=min_zoom,
max_zoom=max_zoom,
mode=mode,
align_corners=align_corners,
keep_size=keep_size,
)
random_zoom.set_random_state(1234)
zoomed = random_zoom({key: self.imt[0]})
expected = list()
for channel in self.imt[0]:
expected.append(zoom_scipy(channel, zoom=random_zoom._zoom, mode="nearest", order=0, prefilter=False))
expected = np.stack(expected).astype(np.float32)
np.testing.assert_allclose(expected, zoomed[key], atol=1.0)
示例7: zoom
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def zoom(x: np.ndarray, scale_factor: AxesParams, axes: AxesLike = None, order: int = 1,
fill_value: Union[float, Callable] = 0) -> np.ndarray:
"""
Rescale ``x`` according to ``scale_factor`` along the ``axes``.
Parameters
----------
x
scale_factor
axes
axes along which the tensor will be scaled. If None - the last ``len(shape)`` axes are used.
order
order of interpolation.
fill_value
value to fill past edges. If Callable (e.g. `numpy.min`) - ``fill_value(x)`` will be used.
"""
scale_factor = fill_by_indices(np.ones(x.ndim, 'float64'), scale_factor, axes)
if callable(fill_value):
fill_value = fill_value(x)
# remove an annoying warning
with warnings.catch_warnings():
warnings.simplefilter('ignore', UserWarning)
return ndimage.zoom(x, scale_factor, order=order, cval=fill_value)
示例8: zoom_to_shape
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def zoom_to_shape(x: np.ndarray, shape: AxesLike, axes: AxesLike = None, order: int = 1,
fill_value: Union[float, Callable] = 0) -> np.ndarray:
"""
Rescale ``x`` to match ``shape`` along the ``axes``.
Parameters
----------
x
shape
final shape.
axes
axes along which the tensor will be scaled. If None - the last ``len(shape)`` axes are used.
order
order of interpolation.
fill_value
value to fill past edges. If Callable (e.g. `numpy.min`) - ``fill_value(x)`` will be used.
"""
old_shape = np.array(x.shape, 'float64')
new_shape = np.array(fill_by_indices(x.shape, shape, axes), 'float64')
return zoom(x, new_shape / old_shape, order=order, fill_value=fill_value)
示例9: proportional_zoom_to_shape
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def proportional_zoom_to_shape(x: np.ndarray, shape: AxesLike, axes: AxesLike = None,
padding_values: Union[AxesParams, Callable] = 0, order: int = 1) -> np.ndarray:
"""
Proportionally rescale ``x`` to fit ``shape`` along ``axes`` then pad it to that shape.
Parameters
----------
x
shape
final shape.
axes
axes along which ``x`` will be padded. If None - the last ``len(shape)`` axes are used.
padding_values
values to pad with.
order
order of interpolation.
"""
axes = expand_axes(axes, shape)
scale_factor = (np.array(shape, 'float64') / extract(x.shape, axes)).min()
return pad_to_shape(zoom(x, scale_factor, axes, order), shape, axes, padding_values)
示例10: image_dataset_phase_2
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def image_dataset_phase_2(repository, image_number, x, y, feature_offsets, R_offsets, delta):
img = makesize(snd.zoom(readimage(repository, image_number), delta), 1)
(h, w) = img.shape
mask = np.ones((h, w), 'bool')
mask[:, 0] = 0
mask[0, :] = 0
mask[h - 1, :] = 0
mask[:, w - 1] = 0
(nroff, blc) = R_offsets.shape
h -= 2
w -= 2
x += 1
y += 1
rep = np.zeros((nroff, 2))
number = image_number
xs = (x + R_offsets[:, 0]).astype('int')
ys = (y + R_offsets[:, 1]).astype('int')
rep[:, 0] = R_offsets[:, 0]
rep[:, 1] = R_offsets[:, 1]
dataset = dataset_from_coordinates(img, xs, ys, feature_offsets)
return dataset, rep, number
示例11: agregation_phase_2
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def agregation_phase_2(repository, image_number, ip, probability_maps, reg, delta, feature_offsets, filter_size, beta, n_iterations):
img = makesize(snd.zoom(readimage(repository, image_number), delta), 1)
(h, w, nldms) = probability_maps.shape
nldms -= 1
mh = h - 1
mw = w - 1
for iteration in range(n_iterations):
y, x = np.where(probability_maps[:, :, ip] >= beta * np.max(probability_maps[:, :, ip]))
dataset = dataset_from_coordinates(img, x + 1, y + 1, feature_offsets)
offsets = reg.predict(dataset)
n_x = (x - offsets[:, 0]).clip(min=0, max=mw)
n_y = (y - offsets[:, 1]).clip(min=0, max=mh)
new_pmap = np.zeros((h, w))
for i in range(n_x.size):
new_pmap[n_y[i], n_x[i]] += probability_maps[y[i], x[i], ip]
probability_maps[:, :, ip] = new_pmap
probability_maps[0, :, ip] = 0
probability_maps[:, 0, ip] = 0
probability_maps[mh, :, ip] = 0
probability_maps[:, mw, ip] = 0
return filter_perso(probability_maps[:, :, ip], filter_size)
示例12: test_uint64_max
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def test_uint64_max():
# Test interpolation respects uint64 max. Reported to fail at least on
# win32 (due to the 32 bit visual C compiler using signed int64 when
# converting between uint64 to double) and Debian on s390x.
# Interpolation is always done in double precision floating point, so we
# use the largest uint64 value for which int(float(big)) still fits in
# a uint64.
big = 2**64-1025
arr = np.array([big, big, big], dtype=np.uint64)
# Tests geometric transform (map_coordinates, affine_transform)
inds = np.indices(arr.shape) - 0.1
x = ndimage.map_coordinates(arr, inds)
assert_equal(x[1], int(float(big)))
assert_equal(x[2], int(float(big)))
# Tests zoom / shift
x = ndimage.shift(arr, 0.1)
assert_equal(x[1], int(float(big)))
assert_equal(x[2], int(float(big)))
示例13: deep_dream
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def deep_dream(image, model, iterations, lr, octave_scale, num_octaves):
""" Main deep dream method """
image = preprocess(image).unsqueeze(0).cpu().data.numpy()
# Extract image representations for each octave
octaves = [image]
for _ in range(num_octaves - 1):
octaves.append(nd.zoom(octaves[-1], (1, 1, 1 / octave_scale, 1 / octave_scale), order=1))
detail = np.zeros_like(octaves[-1])
for octave, octave_base in enumerate(tqdm.tqdm(octaves[::-1], desc="Dreaming")):
if octave > 0:
# Upsample detail to new octave dimension
detail = nd.zoom(detail, np.array(octave_base.shape) / np.array(detail.shape), order=1)
# Add deep dream detail from previous octave to new base
input_image = octave_base + detail
# Get new deep dream image
dreamed_image = dream(input_image, model, iterations, lr)
# Extract deep dream details
detail = dreamed_image - octave_base
return deprocess(dreamed_image)
示例14: _clipped_zoom_no_scipy_warning
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def _clipped_zoom_no_scipy_warning(img, zoom_factor):
from scipy.ndimage import zoom as scizoom
with warnings.catch_warnings():
warnings.filterwarnings("ignore", ".*output shape of zoom.*")
# clipping along the width dimension:
ch0 = int(np.ceil(img.shape[0] / float(zoom_factor)))
top0 = (img.shape[0] - ch0) // 2
# clipping along the height dimension:
ch1 = int(np.ceil(img.shape[1] / float(zoom_factor)))
top1 = (img.shape[1] - ch1) // 2
img = scizoom(img[top0:top0 + ch0, top1:top1 + ch1],
(zoom_factor, zoom_factor, 1), order=1)
return img
示例15: im_rescale
# 需要导入模块: from scipy import ndimage [as 别名]
# 或者: from scipy.ndimage import zoom [as 别名]
def im_rescale(img, scale_factor):
zoomed_img = np.zeros_like(img, dtype=img.dtype)
zoomed = skimage.transform.rescale(img, scale_factor)
if scale_factor >= 1.0:
shift_x = (zoomed.shape[0] - img.shape[0]) // 2
shift_y = (zoomed.shape[1] - img.shape[1]) // 2
zoomed_img[:,:] = zoomed[shift_x:shift_x+img.shape[0], shift_y:shift_y+img.shape[1]]
else:
shift_x = (img.shape[0] - zoomed.shape[0]) // 2
shift_y = (img.shape[1] - zoomed.shape[1]) // 2
zoomed_img[shift_x:shift_x+zoomed.shape[0], shift_y:shift_y+zoomed.shape[1]] = zoomed
return zoomed_img
# this old version uses ndimage zoom which is unreliable