Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python tensorflow重塑和调整图层大小时出错_Python_Tensorflow_Machine Learning_Keras_Deep Learning - Fatal编程技术网

Python tensorflow重塑和调整图层大小时出错

Python tensorflow重塑和调整图层大小时出错,python,tensorflow,machine-learning,keras,deep-learning,Python,Tensorflow,Machine Learning,Keras,Deep Learning,在使用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.

在使用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
不幸的是,这段代码导致了我在下面添加的错误。问题是什么?如何正确调整展平阵列的形状和大小

    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]
编辑: 我试过:

这给了我:

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,).
编辑2: 我从C++数组中接收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]);
//将值复制到输入缓冲区(张量)
对于(大小i=0;ilen;i++){
模型输入->数据.i32[i]=(int32_t)(fb->buf[i]);

因此,我传递给模型的是一个平面阵列。

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

input_shape
应省略样本数,因此如果需要1个样本,则输入的形状应为19200。如果有19200个样本,则形状应为1

重塑层也省略了样本数量,所以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)

请随意使用
调整大小
层而不是
Lambda
层,由于我的Tensorflow版本,我不能使用它。

Keras模型总是希望批量输入,因此保留
输入形状
的第一个维度来表示批量大小。因此,这里您应该更改为
输入形状=(无,img\u test\u flat.shape)
。你能尝试更新吗?我更新了帖子我不确定我需要调整哪些值,你能发布一个示例吗?我有一个19200个值的数组(用(120160)展平的图像)并想将其传递到Conv2D层。在此之前,我想将大小减小到28*28,因此Conv2D的输入为(28,28,1)。非常感谢!我将在微控制器上使用tflite,因此无法使用img_test_flat=img_test。重塑(1,-1),但只能将扁平阵列传递到模型。这就是为什么(据我所知)所有的重塑都需要按顺序进行。有没有办法做到这一点?我一直遇到错误。没有办法将2D数据传递到网络中?例如,
(119200)
?1D会使它变得更加复杂
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])