首页 > 解决方案 > ModuleNotFoundError:Google Colab 上没有名为“nets”的模块

问题描述

我正在尝试TensorFlow Object Detection API在 Google Colab 上运行以SSD-Mobilenet在自定义数据集上训练模型。但我正面临这个 NoModuleError。它没有找到模块'nets'。我已经发现有人面临类似的问题,尽管他们没有在 Google Colab 中运行 trining。以下是一些链接:

ImportError:没有名为“nets”的模块

ModuleNotFoundError:没有名为“nets”的模块(TensorFlow)

在上面的任何地方,我都找到了添加文件夹的建议,我都做PYTHONPATHslimresearch以下是我已经添加的路径:

! echo $PYTHONPATH

import os
os.environ['PYTHONPATH'] += ":/models"
os.environ['PYTHONPATH'] += ":/models/research"
os.environ['PYTHONPATH'] += ":/models/research/slim"
# I copied the `nets` folder inside models folder and 
# additionally here adding this folder to python path such that it becomes available to `faster_rcnn_inception_resnet_v2_feature_extractor.py` file for importing.
os.environ['PYTHONPATH'] += ":/models/nets" 

! echo $PYTHONPATH

%cd '/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/models/research/'
!python setup.py build
!python setup.py install
%cd '/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD'

但仍然收到此错误。以下是我在 Colab 上遇到的错误:

Traceback (most recent call last):
  File "training/train.py", line 26, in <module>
    from object_detection import model_lib
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/model_lib.py", line 28, in <module>
    from object_detection import exporter as exporter_lib
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/exporter.py", line 23, in <module>
    from object_detection.builders import model_builder
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/builders/model_builder.py", line 59, in <module>
    from object_detection.models import faster_rcnn_inception_resnet_v2_feature_extractor as frcnn_inc_res
  File "/content/gdrive/My Drive/Computer_vision_with_deep_learning/TFOD/training/object_detection/models/faster_rcnn_inception_resnet_v2_feature_extractor.py", line 30, in <module>
    from nets import inception_resnet_v2
ModuleNotFoundError: No module named 'nets'

正如我注意到的错误生产线是from nets import inception_resnet_v2文件faster_rcnn_inception_resnet_v2_feature_extractor.py。因此,我另外复制了nets它范围内的文件夹,以便它可以找到模块。但是它仍然在说同样的事情,尽管现在不应该找到这个模块是没有意义的。这里可能还有什么问题?

标签: pythontensorflowgoogle-colaboratorypythonpath

解决方案


我有同样的错误,但我找到了一个可能的解决方案。您需要在 slim 目录中运行上面的代码。

%cd drive/My\ Drive/<path to slim>/slim

!python setup.py build
!python setup.py install

这段代码setup.py为 slim 运行,实际上它设置了所有需要的模块。

您可能还需要将路径添加到您的环境变量中。

os.environ['PYTHONPATH'] = '/env/python/drive/My Drive/slim'

或者

! export PYTHONPATH=$PYTHONPATH:pwd:pwd/slim

以下是对我有用的链接。

https://github.com/tensorflow/models/issues/1842

https://github.com/EdjeElectronics/TensorFlow-Object-Detection-API-Tutorial-Train-Multiple-Objects-Windows-10/issues/150

希望这会有所帮助。


推荐阅读