You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Databricks中运行create_coco_tfrecord.py遇ModuleNotFoundError求助

可行解决方案

方案1:修正PYTHONPATH指向项目根目录

当前工作目录是/databricks/driver/automl/,而dataset属于efficientdet子目录下的模块,需要把efficientdet目录加入PYTHONPATH:

!PYTHONPATH="./efficientdet:$PYTHONPATH"  python efficientdet/dataset/create_coco_tfrecord.py \
    --image_dir=val2017 \
    --object_annotations_file=annotations/instances_val2017.json \
    --output_file_prefix=tfrecord/val \
    --num_shards=32

或者直接进入efficientdet目录执行(注意调整资源路径):

%cd /databricks/driver/automl/efficientdet
!PYTHONPATH=".:$PYTHONPATH"  python dataset/create_coco_tfrecord.py \
    --image_dir=../val2017 \
    --object_annotations_file=../annotations/instances_val2017.json \
    --output_file_prefix=../tfrecord/val \
    --num_shards=32

方案2:修改导入语句为同级相对导入

因为label_map_util.py和create_coco_tfrecord.py在同一个dataset目录下,直接修改导入语句:

from . import label_map_util

替换原来的from dataset import label_map_util即可。

方案3:通过绝对路径强制导入

在create_coco_tfrecord.py文件开头添加以下代码,手动指定模块路径:

import sys
import os
# 将efficientdet目录加入路径
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from dataset import label_map_util

或者更直接的绝对路径写法:

import sys
sys.path.append("/databricks/driver/automl/efficientdet")
from dataset import label_map_util

方案4:用Databricks魔术命令设置环境变量

在执行脚本前,先通过%env命令设置PYTHONPATH:

%env PYTHONPATH=/databricks/driver/automl/efficientdet:$PYTHONPATH
!python efficientdet/dataset/create_coco_tfrecord.py \
    --image_dir=val2017 \
    --object_annotations_file=annotations/instances_val2017.json \
    --output_file_prefix=tfrecord/val \
    --num_shards=32

内容的提问来源于stack exchange,提问作者PrisonMikeGotCaught

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 07:32:27