如何在Google Colab上设置多个目录的PYTHONPATH

问题描述 投票:0回答:1

我正在使用GPU的Google Colab。我想在colab上进行一些培训,因为它提供了GPU,可以减少培训时间。我已经上传了包含Google驱动器中所有文件的文件夹。然后,我已使用此代码通过colab挂载了Google驱动器:

from google.colab import drive
drive.mount('/content/gdrive')

[每当我运行命令进行培训时,我就会一次又一次收到相同的错误。显示命令和错误:

!python train.py --logtostderr --train_dir=training/ --pipeline_config_path=training/faster_rcnn_inception_v2_coco.config

错误:

Traceback (most recent call last):
  File "train.py", line 49, in <module>
    from object_detection.builders import dataset_builder
ModuleNotFoundError: No module named 'object_detection'

每当我的本地计算机中出现这种类型的问题时,总是由于PYTHONPATH的设置错误而出现问题。

我已经尝试在colab上设置PYTHONPATH,但是一次又一次出现相同的错误。尝试设置PYTHONPATH的命令是:

  1. 首次尝试:

    !export PYTHONPATH="${PYTHONPATH}:/content/gdrive/'My Drive'/tensorflow1/models:/content/gdrive/'My Drive'/tensorflow1/models/research:/content/gdrive/'My Drive'/tensorflow1/models/research/slim:/content/gdrive/'My Drive'/tensorflow1/models/research/object_detection"
    
  2. 第二次尝试:

    !export PYTHONPATH=$PYTHONPATH:/content/gdrive/'My Drive'/tensorflow1/models:/content/gdrive/'My Drive'/tensorflow1/models/research:/content/gdrive/'My Drive'/tensorflow1/models/research/slim:/content/gdrive/'My Drive'/tensorflow1/models/research/object_detection
    
  3. 第三次尝试:

    os.environ['PYTHONPATH'] = "/content/gdrive/'My Drive'/tensorflow1/models:/content/gdrive/'My Drive'/tensorflow1/models/research:/content/gdrive/'My Drive'/tensorflow1/models/research/slim:/content/gdrive/My Drive/tensorflow1/models/research/object_detection"
    
  4. 第四次尝试:

    %set_env PYTHONPATH=":/content/gdrive/My\ Drive/tensorflow1/models:/content/gdrive/My\ Drive/tensorflow1/models/research:/content/gdrive/My\ Drive/tensorflow1/models/research/slim:/content/gdrive/My\ Drive/tensorflow1/models/research/object_detection"
    

以上命令均未解决该错误。因此,我需要知道在colab上设置PYTHONPATH的正确方法,如果设置PYTHONPATH不是克服此错误的正确方法,那我应该怎么做?

python object-detection google-colaboratory training-data pythonpath
1个回答
0
投票
import sys
sys.path.extend(['/content/models/research/', '/content/models/research/slim/'])

See this answer

© www.soinside.com 2019 - 2024. All rights reserved.