配置文件
測試
直接使用會報這個錯:
TypeError: type object got multiple values for keyword argument 'groups'
說是字典裏的對應值多了一個,查配置文件,發現有兩個groups
,值是一樣的,看別的配置文件只有上面的一個,註釋掉下面的試了一下,就可以了
測試結果:
訓練
1.根據自己的數據集的類改動配置文件參考這個:配置改動
注意:一定要看好學習率的設置,16GPU是0.02,單卡或雙卡設置0.0025左右,一定要小,一開始我雙卡設的0.02,loss從個位數飆到五位數
2.因爲我只需要檢測bbox和mask,所以在配置文件的model字典裏將semantic相關的都註釋了
並且在train_pineline裏設置with_seg=False
:
只設置with_seg是沒有用的,最後會計算seg的loss,然而沒有label,就報錯,所以要加前面的註釋
3.修改評估內容:
4.在這裏取消TensorboardLoggerHook
的註釋就能在命令行看到loss
但是我的程序會彈出很多警告,做如下操作就能不顯示警告:
import warnings
warnings.filterwarnings('ignore')
調試後再次運行出現:
Address alredy in use
這說明剛纔停止的程序的內存沒有釋放,執行下面命令:
ps aux|grep user_name|grep python
然後用kill -9 ID
來釋放內存。
這樣就能順利訓練了:
使用預訓練權重
def main():
#gen coco pretrained weight
import torch
num_classes = 11
model_coco = torch.load("../checkpoints/cascade_rcnn_r50_fpn_1x_20190501-3b6211ab.pth") # weight
for key, value in model_coco["state_dict"].items():
print(key)
model_coco["state_dict"]["bbox_head.0.fc_cls.weight"] = \
model_coco["state_dict"]["bbox_head.0.fc_cls.weight"][:num_classes, :]
model_coco["state_dict"]["bbox_head.1.fc_cls.weight"] = \
model_coco["state_dict"]["bbox_head.1.fc_cls.weight"][:num_classes, :]
model_coco["state_dict"]["bbox_head.2.fc_cls.weight"] = \
model_coco["state_dict"]["bbox_head.2.fc_cls.weight"][:num_classes, :]
model_coco["state_dict"]["bbox_head.0.fc_cls.bias"] = \
model_coco["state_dict"]["bbox_head.0.fc_cls.bias"][:num_classes]
model_coco["state_dict"]["bbox_head.1.fc_cls.bias"] = \
model_coco["state_dict"]["bbox_head.1.fc_cls.bias"][:num_classes]
model_coco["state_dict"]["bbox_head.2.fc_cls.bias"] = \
model_coco["state_dict"]["bbox_head.2.fc_cls.bias"][:num_classes]
# save new model
torch.save(model_coco, "cascade_rcnn_r50_fpn_1x_coco_pretrained_weights_classes_%d.pth" % num_classes)
if __name__ == "__main__":
main()
如果是其他的模型,可以使用這個工具,可以看到網絡的參數,比如要從80類變成1個類,找後面的參數個數是81的,然後記住名字,用上面的代碼修改
注意事項
這裏的resize一定要注意!