自定义CNN模型训练后测试结果不佳,请求排查与优化指导
Hey everyone, I'm working on an image classification task with a 6-class dataset. I've split the data into training and test sets (10% allocated to testing), used ImageDataGenerator for data loading and augmentation, built a custom Sequential model, but my test results are shockingly bad. Let me walk you through exactly what I've done so far:
Data Loading & Augmentation
I set up training and validation generators (the validation set is a subset of the training data) with this code:
# Create train_generator train_generator = train_datagen.flow_from_directory( train_path, # Path to data target_size=(30, 30), # Average target size (32 + 28)/2 = 30 batch_size=32, # Batch size class_mode='categorical', # Categorical Class mode classes=classes, # Classes subset='training', # Training Subset color_mode='grayscale' ) # Create validation_generator validation_generator = train_datagen.flow_from_directory( train_path, # Path to data target_size=(30, 30), # Average target size batch_size=16, # Batch Size class_mode='categorical', # Categorical Class mode classes=classes, # Classes subset='validation', # Validation Subset color_mode='grayscale' )
Model Architecture & Training
I trained a Sequential model for 100 epochs. Here's the full model structure:
| Layer (type) | Output Shape | Param # |
|---|---|---|
| conv2d (Conv2D) | (None, 28, 28, 128) | 1280 |
| activation (Activation) | (None, 28, 28, 128) | 0 |
| max_pooling2d (MaxPooling2D) | (None, 14, 14, 128) | 0 |
| conv2d_1 (Conv2D) | (None, 12, 12, 64) | 73792 |
| activation_1 (Activation) | (None, 12, 12, 64) | 0 |
| max_pooling2d_1 (MaxPooling2D) | (None, 6, 6, 64) | 0 |
| conv2d_2 (Conv2D) | (None, 4, 4, 32) | 18464 |
| activation_2 (Activation) | (None, 4, 4, 32) | 0 |
| max_pooling2d_2 (MaxPooling2D) | (None, 2, 2, 32) | 0 |
| flatten (Flatten) | (None, 128) | 0 |
| dense (Dense) | (None, 128) | 16512 |
| activation_3 (Activation) | (None, 128) | 0 |
| dropout (Dropout) | (None, 128) | 0 |
| dense_1 (Dense) | (None, 6) | 774 |
| activation_4 (Activation) | (None, 6) | 0 |
Total params: 110,822
Trainable params: 110,822
Non-trainable params: 0
Test Setup & Prediction
I created a test generator and ran predictions using this code:
test_generator = ImageDataGenerator(rescale=1./255).flow_from_directory( test_path, # Path to data target_size=(30, 30), # Average target size (32 + 28)/2 = 30 batch_size=32, # Batch size class_mode='categorical', # Categorical Class mode classes=classes, # Classes color_mode='grayscale' )
# Confusion Matrix and Classification Report Y_pred = model.predict_generator(test_generator, test_generator.n // test_generator.batch_size+1) y_pred = np.argmax(Y_pred, axis=1)
Abysmal Test Results
Here's the confusion matrix from the test predictions:
[[166 31 1 140 135 152] [ 33 4 0 20 25 27] [ 17 1 0 10 11 10] [130 25 1 111 115 142] [126 17 2 107 81 124] [153 24 1 141 129 159]]
And the classification report:
precision recall f1-score support Bag 0.27 0.27 0.27 625 Sandal 0.04 0.04 0.04 109 automobile 0.00 0.00 0.00 49 bird 0.21 0.21 0.21 524 truck 0.16 0.18 0.17 457 Ankle boot 0.26 0.26 0.26 607 accuracy 0.22 2371 macro avg 0.16 0.16 0.16 2371 weighted avg 0.22 0.22 0.22 2371
I'm clearly making one or more critical mistakes here—can anyone point out where I went wrong and give practical guidance on how to boost this model's performance?
内容的提问来源于stack exchange,提问作者Muhammad Ibtihaj Tahir

