【问题标题】:Tensorflow - keras - Shape errors with 'strided_slice' (used resized MNIST dataset)Tensorflow - keras - 'strided_slice' 的形状错误(使用调整大小的 MNIST 数据集)
【发布时间】:2018-11-13 09:00:45
【问题描述】:

我将使用 tf.keras 和 MNIST 手写数字数据集制作一些 GAN 模型测试器。因为我的模型将用于 128x128 的图像,所以我将 MNIST 数据集的大小调整为 128x128x1。但是,程序出现了一些我从未见过的错误。

(x_train, _), (_, _) = mnist.load_data()
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)  # picks some data, count is batch_size=32.
imgs = x_train[idx]  # This line made errors

最后一行犯了两个错误:

tensorflow.python.framework.errors_impl.InvalidArgumentError:形状必须为 1 级,但对于 'strided_slice_1'(操作:'StridedSlice')为 2 级,输入形状:[60000,128,128,1],[1,32] , [1,32], [1].

和,

ValueError:形状必须为 1 级,但对于 'strided_slice_1'(操作:'StridedSlice')为 2 级,输入形状为:[60000,128,128,1]、[1,32]、[1,32]、[ 1]。

我认为数字 '32' 表示 batch_size(=32)。

我试图找到有关此错误的信息,但找不到类似此错误的信息。

我没有任何想法来解决这个问题(因为我一周前开始使用 keras,在我使用 pytorch 之前)。

【问题讨论】:

  • 什么是(1,32),32不是batch size吗?请打印(idx)。
  • idx 是具有随机整数值的 batch_size 大小的列表。它用于提取 32 个随机 MNIST 数据。 example: [35904 26046 21148 28562 56414 31930 44948 53721 2118 30226 6292 57618 39161 46354 9053 46421 44620 36931 8266 48759 42023 40365 33027 13278 57609 3804 57904 4377 3517 30322 16445 47825]
  • 我认为如果您打印它,您的 idx 将是错误消息中的形状 (1,32),这就是我要求您打印它的原因。
  • print(idx)的结果就是上面的例子,所以我打印了idx.shape:(32,)。
  • you write: imgs = x_train[idx] # 这行出错了,不是 print(idx)

标签: python tensorflow keras neural-network generative-adversarial-network


【解决方案1】:

上面的代码有更多问题,但导致错误的主要原因是 tensorflow 不支持 numpy 类型的高级切片。 实际上错误消息是因为 tensorflow 尝试在他的strided-slices 中对齐您的输入数组:

跨步切片的示例:

foo[5:,:,:3] 在 7x8x9 张量上等价于 foo[5:7,0:8,0:3]。 foo[::-1] 反转形状为 8 的张量。

不幸的是,目前 Tensorflow 中只有基本类型索引可用。高级类型索引正在开发中。

第二个问题,您的大小调整不合适。 Tensorflow 假设是 3D 或 4D 输入。您尝试将 2D 图像传递给 `tf.image.resize_images(),它不会返回所需的新图像尺寸。所以我们必须像这样重塑原始图像:

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))

只有这样我们才能将它们传递给:

`x_train = tf.image.resize_images(x_train, [128, 128])

它将返回正确的尺寸:

print(x_train.shape)

输出:

(60000, 128, 128, 1)

所以总结一下整个解决方案,目前你可以这样做:

import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = (x_train.astype(np.float32) - 127.5) / 127.5
x_train = tf.image.resize_images(x_train, [128, 128])

idx = np.random.randint(0, x_train.shape[0], batch_size)

imgs = [x_train[i,:,:,:] for i in idx]

这是一个相当混乱的“解决方案”。

其他,实际上是一个真正的解决方案,通过重新排列原始代码,我们可以实现我们的目标,作为 tensorflow 索引问题的解决方法:

import numpy as np
import tensorflow as tf

batch_size = 32

mnist = tf.keras.datasets.mnist

(x_train, _), (_, _) = mnist.load_data()

x_train = (x_train.astype(np.float32) - 127.5) / 127.5
idx = np.random.randint(0, x_train.shape[0], batch_size)
x_train = x_train[idx]

x_train = x_train.reshape((-1, x_train.shape[1], x_train.shape[1], 1))
x_train = tf.image.resize_images(x_train, [128, 128])

print(x_train.shape)

输出:

(32, 128, 128, 1)

就是这样!

除了 tf.image.resize_images() 之外,您还可以使用其他图像工具,例如来自 scikit-imageskimage.transform.resize(),它返回 numpy 数组类型数据。

【讨论】:

  • 不知道tensorflow的发展。我有了解决这些问题的想法!非常感谢!
  • 您的代码运行成功。现在我只需要修复我的原始代码的结构。谢谢你帮助我。
猜你喜欢
  • 2017-08-04
  • 1970-01-01
  • 2023-03-13
  • 1970-01-01
  • 1970-01-01
  • 2019-09-02
  • 2021-03-22
  • 2016-10-06
  • 2021-12-26
相关资源
最近更新 更多