开发者社区 > ModelScope模型即服务 > 计算机视觉 > 正文

ModelScope这么模型的时候 发现 没有对应的 Trainers?

ModelScope中我想训练 damo/nlp_raner_named-entity-recognition_chinese-base-ecom-50cls 这么模型的时候 发现 没有对应的 Trainers?我的代码是 import os
import json
from modelscope.trainers import build_trainer
from modelscope.msdatasets import MsDataset
from modelscope.utils.hub import read_config
from modelscope.metainfo import Metrics
from modelscope.utils.constant import DownloadMode

model_id = 'damo/nlp_raner_named-entity-recognition_chinese-base-ecom-50cls'
max_epochs = 3
WORK_DIR = 'tmp'
train_dataset = MsDataset.load('./data/train.txt')
eval_dataset = MsDataset.load('./data/eval.txt')

设置显卡

device="cuda:3"

这个方法在trainer读取configuration.json后立即执行,先于构造模型、预处理器等组件

def cfg_modify_fn(cfg):
cfg.task = 'named-entity-recognition'
cfg.preprocessor = {
"train": {
"type": "sequence-labeling-preprocessor",
"mode": "train"
},
"val": {
"type": "sequence-labeling-preprocessor",
"mode": "eval"
}
}
cfg.pipeline = {
'type': 'sequence-labeling-model'
}
cfg.train.work_dir = WORK_DIR
cfg.train.max_epochs = max_epochs

# 设置评估metric
cfg.evaluation.metrics = 'ner-metric'
cfg.train.dataloader = {
    "batch_size_per_gpu": 16,
    "workers_per_gpu": 1
}
cfg.train.optimizer = {
    "type": "AdamW",
    "lr": 5e-5,
    "options": {
        "grad_clip": {
            "max_norm": 2.0
        }
    }
}

cfg.train.lr_scheduler = {
    "type": "StepLR",
    "step_size": 2,
    "options": {
        "warmup": {
            "type": "LinearWarmup",
            "warmup_iters": 2
        }
    }
}
return cfg

kwargs = dict(
model=model_id,
train_dataset=train_dataset,
eval_dataset=eval_dataset,
cfg_modify_fn=cfg_modify_fn,
device=device
)

trainer = build_trainer('nlp_raner-trainer', default_args=kwargs)

print('===============================================================')
print('pre-trained model loaded, training started:')
print('===============================================================')

trainer.train()

print('===============================================================')
print('train success.')
print('===============================================================')

for i in range(max_epochs):
eval_results = trainer.evaluate(f'{WORKDIR}/epoch{i + 1}.pth')
print(f'epoch {i} evaluation result:')
print(eval_results)

print('===============================================================')
print('evaluate success')
print('===============================================================')
参考的是别的模型的例子

展开
收起
真的很搞笑 2023-10-08 19:15:53 65 0
1 条回答
写回答
取消 提交回答

包含图像分类、图像生成、人体人脸识别、动作识别、目标分割、视频生成、卡通画、视觉评价、三维视觉等多个领域

相关电子书

更多
视觉AI能力的开放现状及ModelScope实战 立即下载
ModelScope助力语音AI模型创新与应用 立即下载
低代码开发师(初级)实战教程 立即下载