ValueError:检查输入时出错:预期 permute_input 有 4 个维度,但得到了形状为 (1, 4) 的数组

Posted

技术标签:

【中文标题】ValueError:检查输入时出错:预期 permute_input 有 4 个维度,但得到了形状为 (1, 4) 的数组【英文标题】:ValueError: Error when checking input: expected permute_input to have 4 dimensions, but got array with shape (1, 4) 【发布时间】:2022-01-15 13:44:06 【问题描述】:

过去一周我一直在调试这个错误,但我不确定我的代码为什么不工作。

我们有一个自定义环境,我们的强化学习问题是获取 512x512 图像并决定我们应该执行操作 1 还是操作 2。

env = customEnv()
nb_actions = env.action_space.n # 2 options
shape = env.observation_space.shape
pool_size = 2

input_shape = (512, 512, 1) # 1 channel, grayscale image
model = Sequential()
model.add(Convolution2D(32, 3, padding="same", input_shape=input_shape))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(Convolution2D(64, 2, padding="same")) 
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(Convolution2D(64, 2, padding="same"))
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=(pool_size, pool_size)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dense(nb_actions))
model.add(Activation('linear'))
print(model.summary())

memory = SequentialMemory(limit=1000000, window_length=WINDOW_LENGTH)
policy = BoltzmannQPolicy()
dqn = DQNAgent(model=model, nb_actions=nb_actions, policy=policy, memory=memory,
               nb_steps_warmup=50000, gamma=.99, target_model_update=10000,
               train_interval=4, delta_clip=1.)
dqn.compile(Adam(learning_rate=.00025), metrics=['mae'])

dqn.fit(env, nb_steps=50000, visualize=False, verbose=2)

dqn.save_weights(f'dqn_CTEnv_weights.h5f', overwrite=True)

dqn.test(env, nb_episodes=5, visualize=False)

完整的错误日志:

Traceback (most recent call last):
  File "DQN_CT.py", line 60, in <module>
    dqn.fit(env, nb_steps=50000, visualize=False, verbose=2)
  File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/core.py", line 168, in fit
    action = self.forward(observation)
  File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 224, in forward
    q_values = self.compute_q_values(state)
  File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 68, in compute_q_values
    q_values = self.compute_batch_q_values([state]).flatten()
  File "/home/anaconda3/envs/lib/python3.7/site-packages/rl/agents/dqn.py", line 63, in compute_batch_q_values
    q_values = self.model.predict_on_batch(batch)
  File "/home/anaconda3/envs/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 1201, in predict_on_batch
    x, extract_tensors_from_dataset=True)
  File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 2334, in _standardize_user_data
    batch_size=batch_size)
  File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_v1.py", line 2361, in _standardize_tensors
    exception_prefix='input')
  File "/home/anaconda3/envs/lib/python3.7/site-packages/tensorflow/python/keras/engine/training_utils.py", line 574, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking input: expected permute_input to have 4 dimensions, but got array with shape (1, 4)

我查看了很多关于这个错误的其他帖子,大多数似乎都指出输入形状需要是 3D(宽度、高度、通道),这似乎不适合我们。我们也尝试过(批量大小(window_length)、宽度、高度、通道),但是这样做会给我们另一个错误ValueError: Input 0 of layer permute is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 4, 512, 512, 1]

对于这个问题的任何帮助将不胜感激!

【问题讨论】:

【参考方案1】:

用不同的数据集尝试了你的模型架构,几乎没有修改。

工作示例代码

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.models import Sequential

import pathlib
dataset_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz"
data_dir = tf.keras.utils.get_file('flower_photos', origin=dataset_url, untar=True)
data_dir = pathlib.Path(data_dir)
batch_size = 32
img_height = 512
img_width = 512

train_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

val_ds = tf.keras.utils.image_dataset_from_directory(
  data_dir,
  validation_split=0.2,
  subset="validation",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

for image_batch, labels_batch in train_ds:
  print(image_batch.shape)
  print(labels_batch.shape)
  break

input_shape = (180, 180, 3) # 3 channel, RGB image
model = Sequential()
model.add(tf.keras.layers.Conv2D(32, 3, activation= 'relu',padding="same", input_shape=input_shape))
#model.add(Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())

model.add(tf.keras.layers.Conv2D(64, 2, activation= 'relu',padding="same")) 

model.add(tf.keras.layers.MaxPooling2D())

model.add(tf.keras.layers.Conv2D(64, 2, activation= 'relu',padding="same"))
#model.add(Activation("relu"))
model.add(tf.keras.layers.MaxPooling2D())

model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(64,activation= 'relu'))
#model.add(Activation('relu'))
model.add(tf.keras.layers.Dense(32,activation= 'linear'))
#model.add(Activation('linear'))
print(model.summary())

输出

Model: "sequential_3"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv2d_7 (Conv2D)           (None, 180, 180, 32)      896       
                                                                 
 max_pooling2d_6 (MaxPooling  (None, 90, 90, 32)       0         
 2D)                                                             
                                                                 
 conv2d_8 (Conv2D)           (None, 90, 90, 64)        8256      
                                                                 
 max_pooling2d_7 (MaxPooling  (None, 45, 45, 64)       0         
 2D)                                                             
                                                                 
 conv2d_9 (Conv2D)           (None, 45, 45, 64)        16448     
                                                                 
 max_pooling2d_8 (MaxPooling  (None, 22, 22, 64)       0         
 2D)                                                             
                                                                 
 flatten_2 (Flatten)         (None, 30976)             0         
                                                                 
 dense_4 (Dense)             (None, 64)                1982528   
                                                                 
 dense_5 (Dense)             (None, 32)                2080      
                                                                 
=================================================================
Total params: 2,010,208
Trainable params: 2,010,208
Non-trainable params: 0

【讨论】:

以上是关于ValueError:检查输入时出错:预期 permute_input 有 4 个维度,但得到了形状为 (1, 4) 的数组的主要内容,如果未能解决你的问题,请参考以下文章

ValueError:检查输入时出错:预期的dense_26_input具有形状(45781,)但得到的数组具有形状(2,)

ValueError:检查输入时出错:预期 input_1 有 4 个维度,但得到的数组具有形状(无、无、无)

model.fit 给出 ValueError :检查输入时出错:预期的 conv2d 得到了形状为 () 的数组

ValueError:检查输入时出错:预期dense_11_input 具有3 维,但得到了形状为(0, 1) 的数组

ValueError:检查输入时出错:预期 lstm_1_input 具有 3 个维度,但得到的数组具有形状 (393613, 50)

ValueError:检查输入时出错:预期 permute_input 有 4 个维度,但得到了形状为 (1, 4) 的数组