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]