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

产品检测模型训练Loss与Accuracy无提升的技术求助

Hey there, let's figure out why your product detection model isn't learning and fix this step by step! From your training output, the accuracy is stuck around 2.5%—that's almost the same as random guessing for 42 classes (1/42 ≈ 2.38%), which means your model isn't picking up any useful features at all. Let's break down the issues and solutions:

Problem Analysis & Optimization Plan

1. Replace Your Model with a CNN (Critical Fix!)

Right now, you're using a fully connected network that flattens the 100x100 image into a 10,000-dimensional vector and only passes it through one dense layer. This structure completely discards spatial information (like object shapes, edges, and textures)—the most important features for image classification. Convolutional Neural Networks (CNNs) are designed specifically to extract these spatial features, which is essential for your product detection task.

Here's a simple but effective CNN structure to start with:

model = keras.Sequential([
    # Convolutional layers to extract low-level features (edges, textures)
    keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 1)),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Conv2D(64, (3, 3), activation='relu'),
    keras.layers.MaxPooling2D((2, 2)),
    keras.layers.Conv2D(64, (3, 3), activation='relu'),
    # Flatten and add dense layers for classification
    keras.layers.Flatten(),
    keras.layers.Dense(64, activation='relu'),
    keras.layers.Dense(42)  # Output layer (42 classes, no activation since using from_logits=True)
])

Note: Since you're using grayscale images, you need to add a channel dimension to your input data. After loading images, run:

train_images = np.expand_dims(train_images, axis=-1)

2. Fix Data Processing Issues

(1) Normalize Data Correctly

Your current code tries to divide a list by 255, which doesn't work in Python. You need to convert the list to a numpy array first:

# Convert list to numpy array
train_images = np.array(train_images)
# Normalize pixel values to 0-1
train_images = train_images / 255.0
# Add grayscale channel
train_images = np.expand_dims(train_images, axis=-1)

(2) Verify Image-Label Matching

Your data loading loop uses an idx variable, but its initial value isn't shown. Make sure it starts at 0, and that each image you load corresponds to the correct label in train_labels. A mismatch here would make it impossible for the model to learn.

(3) Shuffle Training Data

If your training data is sorted by class (e.g., all class 0 images first, then class 1, etc.), the model will repeatedly learn the same class early on and fail to generalize. Shuffle your data before training:

# Shuffle images and labels while preserving their correspondence
indices = np.arange(train_images.shape[0])
np.random.shuffle(indices)
train_images = train_images[indices]
train_labels = train_labels[indices]

3. Adjust Training Configuration

(1) Add Validation Data

You set up a ModelCheckpoint callback to monitor val_acc, but you didn't pass validation_data to model.fit—this callback is useless right now. Adding validation data also lets you track if the model is overfitting or underfitting:

# Assume you have preprocessed test_images and test_labels
model.fit(
    train_images, train_labels,
    epochs=50,  # Start with 50 epochs instead of 2000—no need to waste time on a broken model
    validation_data=(test_images, test_labels),
    callbacks=[cp_callback]
)

(2) Validate Label Format

Ensure train_labels are integers ranging from 0 to 41 (since you have 42 classes). If labels are outside this range or not integers, SparseCategoricalCrossentropy can't calculate loss correctly, which stops the model from learning.

(3) Add Data Augmentation

Even with 100k images, data augmentation boosts generalization and prevents overfitting once the model starts learning. Use ImageDataGenerator:

from tensorflow.keras.preprocessing.image import ImageDataGenerator

datagen = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    horizontal_flip=True
)
datagen.fit(train_images)

# Train with augmented data
model.fit(
    datagen.flow(train_images, train_labels, batch_size=32),
    epochs=50,
    validation_data=(test_images, test_labels)
)

4. Small Tweaks to Try

  • Adjust Learning Rate: For SGD, start with 0.01 (add momentum like keras.optimizers.SGD(learning_rate=0.01, momentum=0.9)). Adam's default 0.001 is safe, but you can tweak it if needed.
  • Check Image Quality: Randomly display a few loaded images to ensure they're not corrupted (e.g., all black/white) or loaded incorrectly.
  • Try Transfer Learning: Once your basic CNN works, use a pre-trained model like MobileNet or ResNet—this will speed up training and improve performance for product detection.

After making these changes, your model should start learning: you'll see loss decrease and accuracy rise over epochs.

内容的提问来源于stack exchange,提问作者ETCasual

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 11:32:46