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

已掌握Caffe二分类训练,如何扩展至19类自定义数据集训练?

How to Train a 19-Class Custom Dataset with Caffe

Hey there! Glad you’ve already nailed the cat-dog binary classification with Caffe—scaling up to 19 classes is totally achievable with a few key adjustments to your existing workflow. Let’s walk through the critical steps to make this work:

1. Prepare Your 19-Class Dataset

First, get your dataset structured properly and converted to Caffe-compatible formats:

  • Organize your images: Group images into 19 separate folders, each named after the class (e.g., class_0, class_1, ..., class_18—Caffe uses 0-indexed labels).
  • Generate image list files: Create two text files (train.txt and val.txt) where each line follows the format path/to/image.jpg [class_label]. Make sure labels range from 0 to 18 (no gaps, no out-of-range values).
  • Convert to LMDB/LevelDB: Use Caffe’s built-in convert_imageset tool to convert your image lists into efficient database formats. Example command:
    convert_imageset --resize_height=227 --resize_width=227 --shuffle /path/to/your/images/ train.txt train_lmdb
    
    Repeat this for your validation set to create val_lmdb.

2. Adjust Your Network Prototxt Files

This is the most important part—update your train_val.prototxt to support 19 classes:

  • Modify the final fully-connected layer: Locate the last FC layer (usually named fc8 in AlexNet/VGG-style nets) and change its num_output from 2 to 19. Example:
    layer {
      name: "fc8"
      type: "InnerProduct"
      bottom: "fc7"
      top: "fc8"
      param {
        lr_mult: 1
        decay_mult: 1
      }
      param {
        lr_mult: 2
        decay_mult: 0
      }
      inner_product_param {
        num_output: 19  # Changed from 2 to 19
        weight_filler {
          type: "gaussian"
          std: 0.01
        }
        bias_filler {
          type: "constant"
          value: 0
        }
      }
    }
    
  • Keep the loss layer unchanged: SoftmaxWithLoss works perfectly for multi-class classification (since each sample belongs to exactly one class). Just ensure your labels are single integers matching the 0-18 range.
  • Freeze pre-trained layers (for fine-tuning): If you’re using a pre-trained model (like AlexNet on ImageNet), freeze the earlier convolutional layers to speed up training and avoid overwriting useful features. Add param { lr_mult: 0 } to each conv layer, so only the FC layers get updated.

3. Update the Solver Prototxt

Tweak your solver settings to fit the larger dataset:

  • Adjust iteration counts: Increase max_iter slightly (e.g., from 10000 to 20000) since more classes may require more training steps.
  • Set test parameters: Calculate test_iter as number_of_validation_samples / batch_size (e.g., 1900 validation images with batch size 10 = 190 iterations per test run).
  • Fine-tuning learning rate: If you’re using a pre-trained model, start with a lower base_lr (e.g., 0.001 instead of 0.01) to avoid destroying pre-trained features. You can use a learning rate decay schedule too.

Using a pre-trained model will drastically improve your results and reduce training time:

  • Initialize the new final layer: You don’t need to manually edit the pre-trained model—Caffe will automatically initialize the weights for your new 19-class fc8 layer when loading the pre-trained weights.
  • Load the weights: In your solver prototxt, add weights: "path/to/pretrained_model.caffemodel" to start training from the pre-trained features.

5. Train & Debug

Run the training command as usual:

caffe train --solver=your_solver.prototxt
  • Check loss trends: If the loss stays high or doesn’t decrease, double-check your label file (make sure labels are 0-18, no typos), verify the num_output in the final FC layer is 19, and confirm the LMDB files are correctly generated.
  • Fix class imbalance: If some classes have way fewer samples, you can use weighted SoftmaxWithLoss by adding loss_weight parameters to account for imbalance, or augment under-represented classes with rotations/flips.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:05:47