CNN猫狗分类模型训练疑问、新输入预测及模型保存咨询
Hey there! Let's break down your questions one by one, using the CNN code you shared as context:
This is not a healthy sign—it's an early indicator of overfitting. Here's why:
- Your
steps_per_epochis set to 8000, but with a batch size of 32, that means each epoch is forcing the model to train on the same training images dozens of times over (way more than the typical 8000 total images in a standard cat/dog training set). This makes the model memorize training data instead of learning generalizable features. - The huge gap between training (99%) and test (81%) accuracy confirms the model is already "cheating" by remembering specific training examples, rather than understanding what makes a cat or dog distinct.
- Severe overfitting: Training accuracy will likely hit near 100%, but test accuracy will plateau or even drop. The model will be great at classifying images it's already seen, but useless for new ones.
- Wasted compute resources: Since you're repeating training data every epoch, the later epochs are just wasting time—you've already passed the point where the model was learning useful features.
- Poor real-world performance: The final model will perform worse on new images than it does on your test set right now, because it's tuned to the noise and specifics of your training data.
You just need to preprocess the new image to match the input format your model was trained on, then run a prediction. Here's a step-by-step code example using your model:
import numpy as np from keras.preprocessing import image # Load and preprocess the new image new_img = image.load_img('path/to/your/image.jpg', target_size=(64, 64)) # Resize to match training input img_array = image.img_to_array(new_img) img_array = np.expand_dims(img_array, axis=0) # Add batch dimension (model expects batches) img_array /= 255.0 # Normalize like training data # Run prediction prediction = classifier.predict(img_array) # Map prediction to class name (check your training set's class order first) print(training_set.class_indices) # Will output something like {'cats': 0, 'dogs': 1} result = 'Dog' if prediction[0][0] > 0.5 else 'Cat' print(f"This image is a: {result}")
Note: If you've already closed your training session, training_set won't exist—so save the class_indices value (e.g., np.save('class_indices.npy', training_set.class_indices)) during training to reference later.
classifier.save() During Training? Absolutely! You don't need to recompile or retrain anything. After the fit_generator finishes running, your model's trained weights are already stored in memory. Just run:
classifier.save('cat_dog_cnn_model.h5')
This saves the entire model: architecture, trained weights, and even the compile settings. Later, you can load it with:
from keras.models import load_model loaded_model = load_model('cat_dog_cnn_model.h5')
And use loaded_model.predict() directly on new images.
Quick Fixes to Improve Your Model
- Correct
steps_per_epochandvalidation_steps: For a standard 8000-image training set and 2000-image test set, usesteps_per_epoch=8000//32=250andvalidation_steps=2000//32=62—this ensures each epoch only trains on the full dataset once. - Add a Dropout layer after the dense layer to reduce overfitting:
classifier.add(Dense(units=128, activation='relu')); classifier.add(Dropout(0.5)) - Try larger input sizes (e.g., 128x128) to let the model learn more detailed features.
内容的提问来源于stack exchange,提问作者Viper

