import json
from mmcv import Config
cfg_path='./config/pan_r18_ctw.py'
cfg=Config.fromfile(cfg_path)
#读取参数,json.dumps()使字典类型漂亮的输出,indent参数决定添加几个空格
print(json.dumps(cfg._cfg_dict,indent=4))
{
"model": {
"type": "PAN",
"backbone": {
"type": "resnet18",
"pretrained": true
},
"neck": {
"type": "FPEM_v1",
"in_channels": [
64,
128,
256,
512
],
"out_channels": 128
},
"detection_head": {
"type": "PA_Head",
"in_channels": 512,
"hidden_dim": 128,
"num_classes": 6,
"loss_text": {
"type": "DiceLoss",
"loss_weight": 1.0
},
"loss_kernel": {
"type": "DiceLoss",
"loss_weight": 0.5
},
"loss_emb": {
"type": "EmbLoss_v1",
"feature_dim": 4,
"loss_weight": 0.25
}
}
},
"data": {
"batch_size": 16,
"train": {
"type": "PAN_CTW",
"split": "train",
"is_transform": true,
"img_size": 640,
"short_size": 640,
"kernel_scale": 0.7,
"read_type": "cv2"
},
"test": {
"type": "PAN_CTW",
"split": "test",
"short_size": 640,
"read_type": "cv2"
}
},
"train_cfg": {
"lr": 0.001,
"schedule": "polylr",
"epoch": 600,
"optimizer": "Adam"
},
"test_cfg": {
"min_score": 0.88,
"min_area": 16,
"bbox_type": "poly",
"result_path": "outputs/submit_ctw/"
}
}
选取某一个模块
print(cfg.data.train)
{'type': 'PAN_CTW', 'split': 'train', 'is_transform': True, 'img_size': 640, 'short_size': 640, 'kernel_scale': 0.7, 'read_type': 'cv2'}
type(cfg.data.train)
<class 'mmcv.utils.config.ConfigDict'>
导入一个模块
- 建立builder.py文件
import models
def build_model(cfg):
param=dict()
for key in cfg:
if key=='type':
continue
param[key]=cfg[key]
model=models.__dict__[cfg.type](**param)
return model
当需要使用config中的backbone参数信息时,还需在builder.py所在位init.py修改
from .builder import budil_backbone
from .resnet import resnet18,resnet50,resnet101
__all__=['resnet18','resnet50','resnet101','budil_backbone']
#定义__all__变量,默认对外允许导入以下四个函数
budil_backbone(cfg.model.backbone)
该函数的解析如下
import models
def budil_backbone(cfg):
param=dict()
for key in cfg:
if key=='type':
continue
param[key]=cfg[key]
#print(cfg.type)
#print(*param) pretrained
#{'pretrained': True}
#print(models.backbone.__dict__[cfg.type]) 此处对应的时dict['resnet18']
#<function resnet18 at 0x000001C7A55A79D0>
#print(models.backbone.__dict__)
# {'type': 'PAN_CTW', 'split': 'train', 'is_transform': True, 'img_size': 640, 'short_size': 640, 'kernel_scale': 0.7, 'read_type': 'cv2'}
#backbone=models.backbone.__dict__[cfg.type](param.values())
backbone = models.backbone.__dict__[cfg.type](**param)
#functional 传入parma的value值
return backbone
网友评论