为何我的CNN脚本无法正确预测?新手图像识别项目排障求助
Hey there! Let’s walk through troubleshooting your multi-class image classification script step by step. Since you’re adapting a binary cat/dog tutorial, there are a few key areas that often cause issues when switching to 7 classes—let’s start with the most likely culprits.
Your dataset size is a big red flag here—10-15 images per class is way too small for a deep learning model to generalize. Here’s how to fix and verify this:
- Add data augmentation immediately: This artificially expands your dataset by applying random transformations to training images. For Keras, use
ImageDataGeneratorlike this:
Make sure you only apply augmentation to training data, not validation/test data.from tensorflow.keras.preprocessing.image import ImageDataGenerator train_datagen = ImageDataGenerator( rescale=1./255, rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, horizontal_flip=True, zoom_range=0.15 ) - Confirm preprocessing consistency: If your training pipeline scales images to
[0,1](withrescale=1./255), you must do the exact same scaling when loading images for prediction. Skipping this will make your model’s outputs meaningless. - Verify label mapping: When using
flow_from_directory, Keras assigns labels based on folder name order. Printtrain_generator.class_indicesto check which folder maps to which integer label—you might be misinterpreting prediction results if this mapping is off.
Binary and multi-class models have critical differences—double-check these:
- Output layer activation: Replace the binary
sigmoidactivation withsoftmax(since it outputs a probability distribution across all 7 classes, summing to 1). - Output layer size: Change the output layer’s neuron count from 1 to 7 (one per class).
- Loss function: Swap
binary_crossentropyfor eithercategorical_crossentropy(if you’re using one-hot encoded labels) orsparse_categorical_crossentropy(if you’re using integer labels). Using the wrong loss will break training entirely.
- Track training metrics: If your accuracy stays around 14% (1/7, random guess), your model isn’t learning. Try reducing your batch size (since data is small, use 2-4) or lowering the learning rate. If training accuracy is high but validation accuracy is garbage, you’re overfitting—add
Dropoutlayers (e.g.,Dropout(0.5)before your final dense layer) or use transfer learning (pre-trained models like VGG16 or MobileNet work wonders for small datasets). - Check model summary: Run
model.summary()to confirm your output layer has 7 neurons withsoftmaxactivation—this is an easy mistake to make when adapting a binary model.
Even if your model trains well, prediction can fail due to small oversights:
- Resize and preprocess input images: Ensure your prediction images match the input size your model expects (e.g.,
(224, 224, 3)for most pre-trained models). Don’t forget to scale them to[0,1]just like training data. - Parse predictions correctly:
model.predict()returns a probability array for each class. Usenp.argmax()to get the index of the highest probability, then map it back to your class names:
Don’t confuse the raw probability values with class labels!import numpy as np predictions = model.predict(processed_img) predicted_idx = np.argmax(predictions[0]) class_names = list(train_generator.class_indices.keys()) predicted_class = class_names[predicted_idx]
Grab an image from your training set, run it through your prediction pipeline. If it predicts the wrong class, your preprocessing or model architecture is broken. If it predicts correctly but new images fail, your model is overfitting and needs more data/augmentation/regularization.
内容的提问来源于stack exchange,提问作者Christoffer




