You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CNN猫狗分类模型训练疑问、新输入预测及模型保存咨询

Hey there! Let's break down your questions one by one, using the CNN code you shared as context:

1. Is 99% Training Accuracy & 81% Test Accuracy at Epoch 4 Normal?

This is not a healthy sign—it's an early indicator of overfitting. Here's why:

  • Your steps_per_epoch is 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.
2. What Problems Might Happen After Finishing All 25 Epochs?
  • 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.
3. How to Predict on New, Unseen Images?

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.

4. Can I Save the Model After Training Without Using 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_epoch and validation_steps: For a standard 8000-image training set and 2000-image test set, use steps_per_epoch=8000//32=250 and validation_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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:25:28