使用Faster RCNN深度学习

Faster RCNN 

bubbliiiing/faster-rcnn-pytorch: 这是一个faster-rcnn的pytorch实现的库,可以利用voc数据集格式的数据进行训练。 (github.com)

睿智的目标检测27——Pytorch搭建Faster R-CNN目标检测平台_Bubbliiiing的博客-CSDN博客

在colab中:

训练:

1  voc_annotation.py

如果使用配套VOC文件,annotation_mode     = 2

2 train.py 

更改预训练权重  model_path      = '/content/gdrive/MyDrive/models/yolov7-pytorch/logs/best_epoch_weights5a.pth'
可能需要更改:classes_path    = 'model_data/voc_classes.txt'

***    save_period         = 5  必须注意保存周期,防掉线,改小一些

预测:

1 fcnn.py

"model_path": '/content/gdrive/MyDrive/models/faster-rcnn-pytorch/model_data/voc_weights_resnet.pth',修改训练好的文件的为实际路径
"classes_path": 'model_data/voc_classes.txt',

2  predict.py

直接运行即可

评价: 

1  fcnn.py

"model_path": '/content/gdrive/MyDrive/models/faster-rcnn-pytorch/model_data/voc_weights_resnet.pth',修改训练好的文件的为实际路径
"classes_path": 'model_data/voc_classes.txt',

2  get_map.py 

可能需要更改:classes_path = 'model_data/voc_classes.txt'

注意:"classes_path":保持一致!

在本地(CPU)(torch=1.2.0):

Cuda=False 

注意:如果使用高版本torch,由于压缩格式不同,weights需要在高版本torch 下转换成较低版本。具体方法上篇博客有记录。

一次失败的训练:

2023-09-04 08:33:13.494275: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
2023-09-04 08:33:14.442077: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT
Number of devices: 1
initialize network with normal type
Load weights /content/gdrive/MyDrive/models/faster-rcnn-pytorch/model_data/voc_weights_resnet.pth.

Successful Load Key: ['extractor.0.weight', 'extractor.1.weight', 'extractor.1.bias', 'extractor.1.running_mean', 'extractor.1.running_var', 'extractor.1.num_batches_tracked', 'extractor.4.0.conv1.weight', 'extractor.4.0.bn1.weight', 'extractor.4.0.bn1.bias', 'extractor.4.0.bn1.running_mean', 'extractor.4.0.bn1.running_var', 'extractor.4.0.bn1.num_batches_tracked', 'extractor.4.0.conv2.weight', 'extractor.4.0.bn2.weight', 'extractor.4.0.bn2.bias', 'extractor.4.0.bn2.running_mean', 'extractor.4.0.bn2.running_var', 'e ……
Successful Load Key Num: 328

Fail To Load Key: [] ……
Fail To Load Key num: 0

温馨提示,head部分没有载入是正常现象,Backbone部分没有载入是错误的。
Configurations:
----------------------------------------------------------------------
|                     keys |                                   values|
----------------------------------------------------------------------
|             classes_path |               model_data/voc_classes.txt|
|               model_path | /content/gdrive/MyDrive/models/faster-rcnn-pytorch/model_data/voc_weights_resnet.pth|
|              input_shape |                               [600, 600]|
|               Init_Epoch |                                        0|
|             Freeze_Epoch |                                       50|
|           UnFreeze_Epoch |                                      100|
|        Freeze_batch_size |                                        4|
|      Unfreeze_batch_size |                                        2|
|             Freeze_Train |                                     True|
|                  Init_lr |                                   0.0001|
|                   Min_lr |                   1.0000000000000002e-06|
|           optimizer_type |                                     adam|
|                 momentum |                                      0.9|
|            lr_decay_type |                                      cos|
|              save_period |                                        3|
|                 save_dir |                                     logs|
|              num_workers |                                        4|
|                num_train |                                    16551|
|                  num_val |                                     4952|
----------------------------------------------------------------------
Start Train
Epoch 1/100: 100% 4137/4137 [46:05<00:00,  1.50it/s, lr=1e-5, roi_cls=0.175, roi_loc=0.558, rpn_cls=0.0378, rpn_loc=0.061, total_loss=0.832]
Finish Train
Start Validation
Epoch 1/100: 100% 1238/1238 [06:53<00:00,  2.99it/s, val_loss=0.98]
Finish Validation
Epoch:1/100
Total Loss: 0.832 || Val Loss: 0.980 
Save best model to best_epoch_weights.pth
Start Train
Epoch 2/100: 100% 4137/4137 [45:19<00:00,  1.52it/s, lr=2e-5, roi_cls=0.178, roi_loc=0.566, rpn_cls=0.0364, rpn_loc=0.0626, total_loss=0.843]
Finish Train
Start Validation
Epoch 2/100: 100% 1238/1238 [06:48<00:00,  3.03it/s, val_loss=0.985]
Finish Validation
Epoch:2/100
Total Loss: 0.843 || Val Loss: 0.985 
Start Train
Epoch 3/100: 100% 4137/4137 [45:13<00:00,  1.52it/s, lr=5e-5, roi_cls=0.192, roi_loc=0.609, rpn_cls=0.0363, rpn_loc=0.0627, total_loss=0.899]
Finish Train
Start Validation
Epoch 3/100: 100% 1238/1238 [06:50<00:00,  3.02it/s, val_loss=1.02]
Finish Validation
Epoch:3/100
Total Loss: 0.899 || Val Loss: 1.019 
Start Train
Epoch 4/100: 100% 4137/4137 [45:17<00:00,  1.52it/s, lr=0.0001, roi_cls=0.213, roi_loc=0.672, rpn_cls=0.0375, rpn_loc=0.0654, total_loss=0.988]
Finish Train
Start Validation
Epoch 4/100: 100% 1238/1238 [07:03<00:00,  2.93it/s, val_loss=1.07]
Finish Validation
Epoch:4/100
Total Loss: 0.988 || Val Loss: 1.070 
Start Train
Epoch 5/100:   2% 100/4137 [01:07<43:51,  1.53it/s, lr=0.0001, roi_cls=0.219, roi_loc=0.659, rpn_cls=0.0401, rpn_loc=0.0591, total_loss=0.977]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值