在Google Colab运行YOLOv5遇yaml.scanner.ScannerError问题求助
问题
在Google Colab中运行YOLOv5训练命令时触发YAML解析错误,报错指向data.yaml文件第6行第6列。
执行的训练命令:
!python /content/yolov5/train.py --batch 70 --epochs 30 --data "/content/elec_meter_yaml/data.yaml" --cfg /content/yolov5/models/yolov5x.yaml --weights yolov5x.pt --name runs
错误信息:
Traceback (most recent call last): File "/content/yolov5/train.py", line 640, in <module> main(opt) File "/content/yolov5/train.py", line 529, in main train(opt.hyp, opt, device, callbacks) File "/content/yolov5/train.py", line 112, in train data_dict = data_dict or check_dataset(data) # check if None File "/content/yolov5/utils/general.py", line 518, in check_dataset data = yaml_load(data) # dictionary File "/content/yolov5/utils/general.py", line 603, in yaml_load return yaml.safe_load(f) File "/usr/local/lib/python3.8/dist-packages/yaml/__init__.py", line 125, in safe_load return load(stream, SafeLoader) File "/usr/local/lib/python3.8/dist-packages/yaml/__init__.py", line 81, in load return loader.get_single_data() File "/usr/local/lib/python3.8/dist-packages/yaml/constructor.py", line 49, in get_single_data node = self.get_single_node() File "/usr/local/lib/python3.8/dist-packages/yaml/composer.py", line 36, in get_single_node document = self.compose_document() File "/usr/local/lib/python3.8/dist-packages/yaml/composer.py", line 58, in compose_document self.get_event() File "/usr/local/lib/python3.8/dist-packages/yaml/parser.py", line 118, in get_event self.current_event = self.state() File "/usr/local/lib/python3.8/dist-packages/yaml/parser.py", line 193, in parse_document_end token = self.peek_token() File "/usr/local/lib/python3.8/dist-packages/yaml/scanner.py", line 129, in peek_token self.fetch_more_tokens() File "/usr/local/lib/python3.8/dist-packages/yaml/scanner.py", line 223, in fetch_more_tokens return self.fetch_value() File "/usr/local/lib/python3.8/dist-packages/yaml/scanner.py", line 577, in fetch_value raise ScannerError(None, None, yaml.scanner.ScannerError: mapping values are not allowed here in "/content/elec_meter_yaml/data.yaml", line 6, column 6
原data.yaml文件内容:
train = '/content/elec_meter_yaml/train.txt' val = '/content/elec_meter_yaml/val.txt' test = '/content/elec_meter_yaml/test.txt' nc = 12 names: 0: one 1: two 2: three 3: four 4: five 5: six 6: seven 7: eight 8: nine 9: zero 10: meter 11: dot
解决方案
错误根源
YAML语法规定键值对必须用键: 值(冒号后必须加空格)的格式分隔,原文件前四行使用的=符号不符合YAML规范,导致解析器无法正确识别文件结构,进而触发后续语法错误。
修正后的data.yaml内容
train: '/content/elec_meter_yaml/train.txt' val: '/content/elec_meter_yaml/val.txt' test: '/content/elec_meter_yaml/test.txt' nc: 12 names: 0: one 1: two 2: three 3: four 4: five 5: six 6: seven 7: eight 8: nine 9: zero 10: meter 11: dot
额外注意事项
- YAML对缩进敏感,
names下的每个类别条目必须保持相同缩进(推荐用2个空格) - 若路径中无特殊字符(如空格),可省略单引号,例如:
train: /content/elec_meter_yaml/train.txt
内容的提问来源于stack exchange,提问作者박경덕
相关产品推荐
相关产品推荐

