已掌握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.txtandval.txt) where each line follows the formatpath/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_imagesettool to convert your image lists into efficient database formats. Example command:
Repeat this for your validation set to createconvert_imageset --resize_height=227 --resize_width=227 --shuffle /path/to/your/images/ train.txt train_lmdbval_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
fc8in AlexNet/VGG-style nets) and change itsnum_outputfrom 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:
SoftmaxWithLossworks 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_iterslightly (e.g., from 10000 to 20000) since more classes may require more training steps. - Set test parameters: Calculate
test_iterasnumber_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.
4. Fine-Tune a Pre-Trained Model (Recommended)
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
fc8layer 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_outputin 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
SoftmaxWithLossby addingloss_weightparameters to account for imbalance, or augment under-represented classes with rotations/flips.
内容的提问来源于stack exchange,提问作者X21
相关产品推荐
相关产品推荐

