ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,32,32,3),找到形状 =(32,32,3)
tensorflow 264
原文标题 :ValueError: Input 0 of layer “sequential” is incompatible with the layer: expected shape=(None, 32, 32, 3), found shape=(32, 32, 3)
这是我收到的错误,一些帮助会很好:我已经粘贴了错误和我认为可能有问题的部分代码。如果你能帮助我解决这个问题,那就太好了。ValueError:在用户中代码: – – – – – – – – – – – – – – – – – – – – – – – – —————————ValueError Traceback (最近一次调用最后一次) in 27 return plot28—> 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)
以下是我认为可能有用的部分代码:
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))