[Python] WARNING:tensorflow:Model was constructed with shape (None, 66, 200, 3)

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    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. Can you help me understand what is going on? Thanks for the help.

    Continue reading...

Compartilhe esta Página