繁体   English   中英

张量流重塑和调整层大小的错误

[英]errors with tensorflow reshape and resize layer

我想在使用 Conv2D 和其他图层之前在第一层中重塑和调整图像大小。 输入将是一个扁平化数组。 这是我的代码:

#Create flat example image:
img_test = np.zeros((120,160))
img_test_flat = img_test.flatten()

reshape_model = Sequential()
reshape_model.add(tf.keras.layers.InputLayer(input_shape=(img_test_flat.shape)))
reshape_model.add(tf.keras.layers.Reshape((120, 160,1)))
reshape_model.add(tf.keras.layers.experimental.preprocessing.Resizing(28, 28, interpolation='nearest'))

result = reshape_model(img_test_flat)
result.shape

不幸的是,这段代码导致了我在下面添加的错误。 有什么问题,我如何正确地重塑和调整 flattend 数组的大小?

    WARNING:tensorflow:Model was constructed with shape (None, 19200) for input Tensor("input_13:0", shape=(None, 19200), dtype=float32), but it was called on an input with incompatible shape (19200,).

InvalidArgumentError: Input to reshape is a tensor with 19200 values, but the requested shape has 368640000 [Op:Reshape]

编辑:我试过:

reshape_model = Sequential()
reshape_model.add(tf.keras.layers.InputLayer(input_shape=(None, img_test_flat.shape[0])))
reshape_model.add(tf.keras.layers.Reshape((120, 160,1)))
reshape_model.add(tf.keras.layers.experimental.preprocessing.Resizing(28, 28, interpolation='nearest'))

这给了我:

WARNING:tensorflow:Model was constructed with shape (None, None, 19200) for input Tensor("input_19:0", shape=(None, None, 19200), dtype=float32), but it was called on an input with incompatible shape (19200,).

EDIT2:我从一维数组中接收到 C++ 中的输入并将其传递给

  // Copy value to input buffer (tensor)
  for (size_t i = 0; i < fb->len; i++){
    model_input->data.i32[i] = (int32_t) (fb->buf[i]);

所以我传递给模型的是一个平面阵列。

你在这里使用形状根本没有意义。 输入的第一个维度应该是样本数。 它应该是 19,200 还是 1 个样本?

input_shape应该省略样本数,所以如果你想要 1 个样本,输入形状应该是 19,200。 如果您有 19,200 个样本,则形状应为 1。

reshapeing层也省略了样本的数量,所以Keras就糊涂了。 你到底想做什么?

这似乎是您想要实现的大致目标,但我个人会在神经网络之外调整图像大小:

import numpy as np
import tensorflow as tf

img_test = np.zeros((120,160)).astype(np.float32)
img_test_flat = img_test.reshape(1, -1)

reshape_model = tf.keras.Sequential()
reshape_model.add(tf.keras.layers.InputLayer(input_shape=(img_test_flat.shape[1:])))
reshape_model.add(tf.keras.layers.Reshape((120, 160,1)))
reshape_model.add(tf.keras.layers.Lambda(lambda x: tf.image.resize(x, (28, 28))))

result = reshape_model(img_test_flat)

print(result.shape)
TensorShape([1, 28, 28, 1])

随意使用Resizing层而不是Lambda层,由于我的 Tensorflow 版本,我无法使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM