[英]WARNING:tensorflow:Model was constructed with shape (None, 66, 200, 3)
I have the following code that is generating an error about a shape:我有以下代码会生成有关形状的错误:
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)
However while training the first epoch I get the error that I'm posting below:然而,在训练第一个时代时,我得到了我在下面发布的错误:
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
I also added the code posted here to Codeshare , so you can see my code.我还将此处发布的代码添加到Codeshare ,因此您可以查看我的代码。 Can you help me understand what is going on?你能帮我理解发生了什么吗? Thanks for the help.谢谢您的帮助。
In your code you have:在您的代码中,您有:
x_train = np.array(list(map(img_preprocess, X_train)))
x_train = np.array(list(map(img_preprocess, X_valid)))
I think this should be:我认为这应该是:
x_train = np.array(list(map(img_preprocess, X_train)))
x_valid = np.array(list(map(img_preprocess, X_valid)))
However I don't think the error comes from this issue.但是我不认为错误来自这个问题。 In fact the input to your network is X_train
, and not x_train
, I don't know if you are aware of that.实际上,您网络的输入是X_train
,而不是x_train
,我不知道您是否知道这一点。
The error definitely comes from a mismatch from the input shape of X_train
, and what you decided the input of the model should be, that is (66, 200, 3)
.错误肯定来自X_train
的输入形状不匹配,您决定 model 的输入应该是(66, 200, 3)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.