opencv 警告:tensorflow:模型是用形状(None,66,200,3)构建的

siv3szwd  于 2023-03-23  发布在  其他
关注(0)|答案(1)|浏览(107)

下面的代码生成了一个关于形状的错误:

from keras.layers import Dense, Activation
from keras import Sequential
from keras.models import load_model
from tensorflow.keras.optimizers import Adam
import tensorflow 
import keras
from tensorflow.python.keras.layers import Input, Dense
from tensorflow.keras.optimizers import Adam
from keras.layers import Convolution2D, MaxPooling2D, Dropout, Flatten, Dense

def nvidia_model():
  model = Sequential()
  model.add(Convolution2D(24,(5,5), strides=(2, 2), input_shape=(66, 200, 3), activation='relu'))
  model.add(Convolution2D(36, (5,5), strides=(2, 2), activation='relu'))
  model.add(Convolution2D(48, (5,5), strides=(2, 2), activation='relu'))
  model.add(Convolution2D(64, (3,3), activation='relu'))
  model.add(Convolution2D(64, (3,3), activation='relu'))
  model.add(Flatten())
  model.add(Dense(100, activation = 'relu'))
  model.add(Dense(50, activation = 'relu'))
  model.add(Dense(10, activation = 'relu'))
  model.add(Dense(1))
  
  optimizer = Adam(learning_rate=1e-3)
  model.compile(loss='mse', optimizer=optimizer)
  return model

model = nvidia_model()
print(model.summary())

history = model.fit(X_train, y_train, epochs=30,validation_data=(X_valid,y_valid),batch_size=100,verbose=1,shuffle=1)

然而,在训练第一个epoch时,我得到了下面的错误:

Epoch 1/30
WARNING:tensorflow:Model was constructed with shape (None, 66, 200, 3) for input KerasTensor(type_spec=TensorSpec(shape=(None, 66, 200, 3), dtype=tf.float32, name='conv2d_5_input'), name='conv2d_5_input', description="created by layer 'conv2d_5_input'"), but it was called on an input with incompatible shape (None,).
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-42-e7614c3cfda1> in <module>()
----> 1 history = model.fit(X_train, y_train, epochs=30,validation_data=(X_valid,y_valid),batch_size=100,verbose=1,shuffle=1)

1 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/training.py in tf__train_function(iterator)
     13                 try:
     14                     do_return = True
---> 15                     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16                 except:
     17                     do_return = False

ValueError: in user code:

    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1160, in train_function  *
        return step_function(self, iterator)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1146, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1135, in run_step  **
        outputs = model.train_step(data)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 993, in train_step
        y_pred = self(x, training=True)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 70, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 251, in assert_input_compatibility
        f'Input {input_index} of layer "{layer_name}" '

    ValueError: Exception encountered when calling layer "sequential_1" "                 f"(type Sequential).
    
    Input 0 of layer "conv2d_5" is incompatible with the layer: expected min_ndim=4, found ndim=1. Full shape received: (None,)
    
    Call arguments received by layer "sequential_1" "                 f"(type Sequential):
      • inputs=tf.Tensor(shape=(None,), dtype=string)
      • training=True
      • mask=None

我还添加了这里张贴的代码到Codeshare,所以你可以看到我的代码。你能帮助我理解是怎么回事吗?谢谢你的帮助。

sbtkgmzw

sbtkgmzw1#

在您的代码中,您有:

x_train = np.array(list(map(img_preprocess, X_train)))
x_train = np.array(list(map(img_preprocess, X_valid)))

我认为这应该是:

x_train = np.array(list(map(img_preprocess, X_train)))
x_valid = np.array(list(map(img_preprocess, X_valid)))

但是我不认为这个错误是由这个问题引起的。事实上,你的网络输入是X_train,而不是x_train,我不知道你是否意识到了这一点。
错误肯定来自X_train的输入形状与您决定的模型输入形状(即(66, 200, 3))的不匹配。

相关问题