首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在tensorflow中使用动态形状调整图像大小

在tensorflow中使用动态形状调整图像大小
EN

Stack Overflow用户
提问于 2019-04-23 22:58:42
回答 1查看 1.4K关注 0票数 1

我想用动态形状调整3D图像的大小,例如从shape (64,64,64,1)到(128,128,128,1)。这个想法是沿着一个轴拆分图像,然后使用tf.image.resize_images并再次堆叠它们。

我的问题是tf.unstack不能处理可变大小的输入。如果我运行我的代码,我会得到"ValueError: Cannot infer num from shape (?, ?, ?, 1)"

我已经考虑过改用tf.split,但是它需要一个整数输入。有谁知道解决办法吗?

下面是一个示例:

代码语言:javascript
复制
import tensorflow as tf
import numpy as np

def resize_by_axis(image, dim_1, dim_2, ax):

    resized_list = []

    # Unstack along axis to obtain 2D images
    unstack_img_depth_list = tf.unstack(image, axis = ax)

    # Resize 2D images
    for i in unstack_img_depth_list:
        resized_list.append(tf.image.resize_images(i, [dim_1, dim_2], method=1, align_corners=True))

    # Stack it to 3D
    stack_img = tf.stack(resized_list, axis=ax)
    return stack_img

#X = tf.placeholder(tf.float32, shape=[64,64,64,1])
X = tf.placeholder(tf.float32, shape=[None,None,None,1])

# Get new shape
shape = tf.cast(tf.shape(X), dtype=tf.float32) * tf.constant(2, dtype=tf.float32)
x_new = tf.cast(shape[0], dtype=tf.int32)
y_new = tf.cast(shape[1], dtype=tf.int32)
z_new = tf.cast(shape[2], dtype=tf.int32)

# Reshape
X_reshaped_along_xy = resize_by_axis(X, dim_1=x_new, dim_2=y_new, ax=2)
X_reshaped_along_xyz= resize_by_axis(X_reshaped_along_xy, dim_1=x_new, dim_2=z_new, ax=1)

init = tf.global_variables_initializer()

# Run
with tf.Session() as sess:
    sess.run(init)
    result = X_reshaped_along_xyz.eval(feed_dict={X : np.zeros((64,64,64,1))})
    print(result.shape)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-23 23:21:54

tf.image.resize_images可以同时调整多个图像的大小,但不允许您拾取批次轴。但是,您可以操作张量的维度,将所需的轴放在第一位,以便将其用作批处理维度,然后在调整大小后将其放回原处:

代码语言:javascript
复制
import tensorflow as tf

def resize_by_axis(image, dim_1, dim_2, ax):
    # Make permutation of dimensions to put ax first
    dims = tf.range(tf.rank(image))
    perm1 = tf.concat([[ax], dims[:ax], dims[ax + 1:]], axis=0)
    # Transpose to put ax dimension first
    image_tr = tf.transpose(image, perm1)
    # Resize
    resized_tr = tf.image.resize_images(image_tr, [dim_1, dim_2],
                                        method=1, align_corners=True)
    # Make permutation of dimensions to put ax in its place
    perm2 = tf.concat([dims[:ax] + 1, [0], dims[ax + 1:]], axis=0)
    # Transpose to put ax in its place
    resized = tf.transpose(resized_tr, perm2)
    return resized

在您的示例中:

代码语言:javascript
复制
import tensorflow as tf
import numpy as np

X = tf.placeholder(tf.float32, shape=[None, None, None, 1])

# Get new shape
shape = tf.cast(tf.shape(X), dtype=tf.float32) * tf.constant(2, dtype=tf.float32)
x_new = tf.cast(shape[0], dtype=tf.int32)
y_new = tf.cast(shape[1], dtype=tf.int32)
z_new = tf.cast(shape[2], dtype=tf.int32)

# Reshape
X_reshaped_along_xy = resize_by_axis(X, dim_1=x_new, dim_2=y_new, ax=2)
X_reshaped_along_xyz = resize_by_axis(X_reshaped_along_xy, dim_1=x_new, dim_2=z_new, ax=1)

init = tf.global_variables_initializer()

# Run
with tf.Session() as sess:
    sess.run(init)
    result = X_reshaped_along_xyz.eval(feed_dict={X : np.zeros((64, 64, 64, 1))})
    print(result.shape)
    # (128, 128, 128, 1)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55814061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档