Keras model.fit报错:输入数组与目标数组样本数不一致求助
Hey Dillon, let's work through why your top model is failing to fit when using those saved ResNet50 bottleneck features. This is a super common snag when working with pre-trained CNNs, so let's break down the most likely issues and fixes:
1. Shape Mismatch Between Features and Model Input
The #1 culprit here is usually that your bottleneck features' shape doesn't match what your top model expects.
ResNet50's include_top=False setting outputs features with a shape of (num_samples, 7, 7, 2048) by default. If your top model starts with a Dense layer (expecting a 1D input like (2048,)), you'll get a shape error immediately.
Fix steps:
- First, check the shapes to confirm:
import numpy as np bottleneck_features = np.load('your_bottleneck_file.npy') print("Bottleneck shape:", bottleneck_features.shape) print("Model input shape:", your_top_model.input_shape) - If you see a mismatch, flatten or pool the 3D feature map to 1D. The cleanest way is global average pooling (mimicking what ResNet's top layer does):
# Reduce (num_samples,7,7,2048) to (num_samples,2048) bottleneck_features = np.mean(bottleneck_features, axis=(1, 2))
2. Label Format Doesn't Match Model Output
If your top model uses softmax for multi-class classification, your labels need to be one-hot encoded (shape (num_samples, num_classes)), not raw integer labels (shape (num_samples,)).
Fix steps:
- Convert your labels with Keras' utility function:
from keras.utils import to_categorical # Assuming your raw labels are integers 0 to num_classes-1 one_hot_labels = to_categorical(your_raw_labels, num_classes=your_class_count)
3. Model Isn't Properly Compiled
It's easy to forget this step, but you must compile your top model before calling fit(). Make sure you're using the right loss function for your task:
- Multi-class:
categorical_crossentropy - Binary classification:
binary_crossentropy
Example compilation:
your_top_model.compile( optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'] )
4. Feature-Label Alignment Issues
If you used shuffle=True when generating bottleneck features, you need to ensure your labels were shuffled in the exact same order as the features. Mix-ups here will cause training failures or nonsensical results.
Fix steps:
- When generating features and labels, use the same generator (with the same seed if setting
shuffle=True) or save both features and labels together in a single file to avoid misalignment.
Quick Working Example Snippet
Here's a condensed version of the correct workflow to reference:
# Load saved features and labels train_features = np.load('train_bottleneck.npy') train_labels = np.load('train_labels.npy') val_features = np.load('val_bottleneck.npy') val_labels = np.load('val_labels.npy') # Reshape features to match model input train_features = np.mean(train_features, axis=(1,2)) val_features = np.mean(val_features, axis=(1,2)) # One-hot encode labels train_labels = to_categorical(train_labels, num_classes=10) val_labels = to_categorical(val_labels, num_classes=10) # Define top model from keras.models import Sequential from keras.layers import Dense, Dropout top_model = Sequential([ Dense(512, activation='relu', input_shape=(2048,)), Dropout(0.5), Dense(10, activation='softmax') ]) # Compile and train top_model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) top_model.fit( train_features, train_labels, epochs=50, batch_size=32, validation_data=(val_features, val_labels), callbacks=[checkpointer] )
内容的提问来源于stack exchange,提问作者Dillon Connolly

