如何在Google Colab用MobileNetV3 Small训练自定义目标检测数据集?求配置文件
可行方案与配置文件说明
一、基于TensorFlow Object Detection API手动搭建配置
TensorFlow Object Detection API支持自定义骨干网络,你可以基于现有配置改出MobileNetV3 Small的检测配置,步骤如下:
先在Colab搭好API环境
直接运行这些命令完成克隆和安装:!git clone https://github.com/tensorflow/models.git !cd models/research && protoc object_detection/protos/*.proto --python_out=. !cd models/research && cp object_detection/packages/tf2/setup.py . !cd models/research && python -m pip install .跑下面的命令验证安装成功:
!cd models/research && python object_detection/builders/model_builder_tf2_test.py修改现有配置生成MobileNetV3 Small专属配置
找个类似的轻量模型配置(比如models/research/object_detection/configs/tf2/ssdlite_mobilenet_v2_coco.config)复制一份当基础,重点改这几个地方:- 把
feature_extractor部分替换成MobileNetV3 Small的配置:feature_extractor { type: 'ssd_mobilenet_v3_small_feature_extractor' depth_multiplier: 1.0 min_depth: 16 conv_hyperparams { regularizer { l2_regularizer { weight: 3.9999998989515007e-05 } } initializer { random_normal_initializer { mean: 0.0 stddev: 0.009999999776482582 } } activation: RELU_6 batch_norm { decay: 0.996999979019165 scale: true epsilon: 0.0010000000474974513 } } override_base_feature_extractor_hyperparams: true } - 把
num_classes改成你自定义数据集的类别数 - 调整
train_input_reader和eval_input_reader里的数据集路径,指向你传到Colab的TFRecord文件 - 预训练权重可以直接用TensorFlow Hub的,配置里这么写:
fine_tune_checkpoint: "https://tfhub.dev/google/imagenet/mobilenet_v3_small_100_224/classification/5" fine_tune_checkpoint_type: "classification"
- 把
二、用MMDetection框架快速实现
要是TensorFlow的配置太麻烦,试试MMDetection,它直接支持MobileNetV3 Small当骨干:
在Colab安装MMDetection
运行这两行就行:!pip install openmim !mim install mmdet写MobileNetV3 Small的检测配置
基于现有配置改,比如SSD搭配MobileNetV3 Small的示例:model = dict( type='SSD', backbone=dict( type='MobileNetV3', arch='small', out_indices=(1, 3, 11), frozen_stages=1, norm_cfg=dict(type='BN', requires_grad=True), init_cfg=dict(type='Pretrained', checkpoint='open-mmlab://mmdet/mobilenet_v3_small')), neck=None, bbox_head=dict( type='SSDHead', in_channels=[16, 24, 1024], num_classes=YOUR_CLASS_NUM, # 替换成你的类别数 anchor_generator=dict( type='SSDAnchorGenerator', scale_major=False, input_size=300, basesize_ratio_range=(0.15, 0.9), strides=[16, 32, 256], ratios=[[2], [2, 3], [2, 3]]), bbox_coder=dict( type='DeltaXYWHBBoxCoder', target_means=[.0, .0, .0, .0], target_stds=[0.1, 0.1, 0.2, 0.2])), train_cfg=dict( assigner=dict( type='MaxIoUAssigner', pos_iou_thr=0.5, neg_iou_thr=0.5, min_pos_iou=0., ignore_iof_thr=-1, gt_max_assign_all=False), smoothl1_beta=1., allowed_border=-1, pos_weight=-1, neg_pos_ratio=3, debug=False), test_cfg=dict( nms_pre=1000, nms=dict(type='nms', iou_threshold=0.5), min_bbox_size=0, score_thr=0.02, max_per_img=200))再补好数据集路径、训练参数,就能直接启动训练了。
三、踩坑小提示
- 官方Colab链接跑不通:大概率是TensorFlow版本不兼容,指定用2.8或2.9版本试试,避开最新版本的API变动。
- 预训练权重加载失败:要么确认Colab能访问外部资源,要么手动下载权重到Colab本地,再改配置里的路径。
内容的提问来源于stack exchange,提问作者saeed adeeb
相关产品推荐
相关产品推荐

