简体   繁体   English

ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3)

[英]ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)

This is the error I'm receiving, some help would be great: I have pasted the error and parts of the code where I believe something might be wrong.这是我收到的错误,一些帮助会很好:我已经粘贴了错误和我认为可能有问题的部分代码。 It would be wonderful if you could help me out witht this.如果你能帮我解决这个问题,那就太好了。 ValueError: in user code: --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in 27 return plot 28 ---> 29 display (plotImages(x_test, data_test_picture, y_test, n_images=10)) ValueError:在用户代码中:---------------------------------------- ------------------------------ ValueError Traceback (most recent call last) in 27 return plot 28 ---> 29 display (plotImages(x_test, data_test_picture, y_test, n_images=10))

<command-3924510788207782> in plotImages(x_test, images_arr, labels_arr, n_images)
     16         ax.set_yticks(())
     17 
---> 18         predict_x=model2000.predict([[x_test[rand]]])
     19         label=categoriesList[predictions[0]]
     20 

/databricks/python/lib/python3.8/site-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
     65     except Exception as e:  # pylint: disable=broad-except
     66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
     68     finally:
     69       del filtered_tb

/databricks/python/lib/python3.8/site-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
   1127           except Exception as e:  # pylint:disable=broad-except
   1128             if hasattr(e, "ag_error_metadata"):
-> 1129               raise e.ag_error_metadata.to_exception(e)
   1130             else:
   1131               raise

ValueError: in user code:

    File "/databricks/python/lib/python3.8/site-packages/keras/engine/training.py", line 1621, in predict_function  *
        return step_function(self, iterator)
    File "/databricks/python/lib/python3.8/site-packages/keras/engine/training.py", line 1611, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/databricks/python/lib/python3.8/site-packages/keras/engine/training.py", line 1604, in run_step  **
        outputs = model.predict_step(data)
    File "/databricks/python/lib/python3.8/site-packages/keras/engine/training.py", line 1572, in predict_step
        return self(x, training=False)
    File "/databricks/python/lib/python3.8/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
        raise e.with_traceback(filtered_tb) from None
    File "/databricks/python/lib/python3.8/site-packages/keras/engine/input_spec.py", line 263, in assert_input_compatibility
        raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

    ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)

Here are parts of the code that i think might be usefull:以下是我认为可能有用的部分代码:

def initializeModel():
  model = Sequential()

  model.add(Conv2D(32, (3, 3), padding='same',
                   input_shape=x_train.shape[1:]))
  model.add(Activation('relu'))
  model.add(Conv2D(32, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))

  model.add(Conv2D(64, (3, 3), padding='same'))
  model.add(Activation('relu'))
  model.add(Conv2D(64, (3, 3)))
  model.add(Activation('relu'))
  model.add(MaxPooling2D(pool_size=(2, 2)))
  model.add(Dropout(0.25))

  model.add(Flatten())
  model.add(Dense(512))
  model.add(Activation('relu'))
  model.add(Dropout(0.5))
  model.add(Dense(num_classes))
  model.add(Activation('softmax'))
  return model

categoriesList=["airplane","automobile","bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"]

import matplotlib.pyplot as plt
import random
def plotImages(x_test, images_arr, labels_arr, n_images=8):
    fig, axes = plt.subplots(n_images, n_images, figsize=(9,9))
    axes = axes.flatten()
    
    for i in range(100):
        rand = random.randint(0, x_test.shape[0] -1)
        img = images_arr[rand]
        ax = axes[i]
    
        ax.imshow( img, cmap="Greys_r")
        ax.set_xticks(())
        ax.set_yticks(())
        
        predict_x=model2000.predict([[x_test[rand]]])
        label=categoriesList[predictions[0]]  
        
        if labels_arr[rand][predictions[0]] == 0:
            ax.set_title(label, fontsize=18 - n_images, color="red")
        else:
            ax.set_title(label, fontsize=18 - n_images) 
        
    plot = plt.tight_layout()
    return plot
  
display (plotImages(x_test, data_test_picture, y_test, n_images=10))

replace the line更换线

predict_x = model2000.predict([[x_test[rand]]])

with

sample = X_test[rand].reshape((1,32,32,3))
predict_x = model2000.predict(sample)

and problem may be solved.问题可能会得到解决。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:层“顺序”的输入 0 与层不兼容:预期形状=(无,32,32,3),找到形状=(32,32,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3) ValueError: 层“sequential_32”的输入 0 与层不兼容:预期形状=(None, 3, 1),发现形状=(32, 0, 1) - ValueError: Input 0 of layer "sequential_32" is incompatible with the layer: expected shape=(None, 3, 1), found shape=(32, 0, 1) TensorFlow ValueError: Input 0 is in compatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) - TensorFlow ValueError: Input 0 is incompatible with layer model_1: expected shape=(None, 32, 32, 1), found shape=(1, 1, 32, 32) ValueError:“顺序”层的输入 0 与该层不兼容:预期形状=(无,128,128,3),找到形状=(32,128,3) - ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 128, 128, 3), found shape=(32, 128, 3) ValueError:层顺序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。 收到的完整形状:[None, 32, 32] - ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [None, 32, 32] ValueError:layersequential_32 的输入 0 与 layer 不兼容::预期 min_ndim=3,发现 ndim=2。 收到的完整形状:[无,256] - ValueError: Input 0 of layer sequential_32 is incompatible with the layer: : expected min_ndim=3, found ndim=2. Full shape received: [None, 256] ValueError:层顺序的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到的完整形状:[32, 64, 3] - ValueError: Input 0 of layer sequential is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [32, 64, 3] ValueError:输入 0 与层 model_2 不兼容:预期形状 =(None, 160, 160, 3),找到的形状 =(32, 160, 3) - ValueError: Input 0 is incompatible with layer model_2: expected shape=(None, 160, 160, 3), found shape=(32, 160, 3) 如何摆脱“sequential_2”层的错误输入 0 与该层不兼容:预期形状 =(None, 32, 1200),找到形状 =(None, 1148) - How to get rid Error Input 0 of layer "sequential_2" is incompatible with the layer: expected shape=(None, 32, 1200), found shape=(None, 1148) 层“sequential_14”的输入 0 与层不兼容:预期形状=(None, 256, 256, 3),找到的形状=(None, 32, 32, 3) - nput 0 of layer "sequential_14" is incompatible with the layer: expected shape=(None, 256, 256, 3), found shape=(None, 32, 32, 3)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM