简体   繁体   English

ModuleNotFoundError:没有名为“tensorflow.python.platform”的模块导入错误

[英]ModuleNotFoundError: No module named 'tensorflow.python.platform' Import error

I'm using Keras, Flask, and Tensorflow.我正在使用 Keras、Flask 和 Tensorflow。 I have two files currently, one for flask and the other for the neural network.我目前有两个文件,一个用于烧瓶,另一个用于神经网络。

web.py网络.py

from flask import Flask, render_template, Response
from camera import VideoCamera
from prediction import NeuralNetwork

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

def gen(camera):
    model = NeuralNetwork()
    while True:
        frame = camera.get_frame()
        print(model.predict(frame))
        yield (b'--frame\r\n'
               b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')

@app.route('/video_feed')
def video_feed():
    return Response(gen(VideoCamera()),
                    mimetype='multipart/x-mixed-replace; boundary=frame')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

prediction.py预测.py

import numpy as np
from keras.preprocessing.image import img_to_array
from keras.applications import mobilenet
from keras.applications.imagenet_utils import decode_predictions
from PIL import Image

class NeuralNetwork(object):
    def __init__(self):
        self.model = mobilenet.MobileNet(weights='imagenet')

    def predict(self, frame):
        # get the image and convert it into a numpy array and into a format for our model
        img = Image.fromarray(frame, 'RGB')
        img = img.resize((224,224), Image.ANTIALIAS)
        np_image = img_to_array(img)
        image_batch = np.expand_dims(np_image, axis=0)
        processed_image = mobilenet.preprocess_input(image_batch.copy())

        # actual machine learning part
        predictions = self.model.predict(processed_image)
        return decode_predictions(predictions)

I get this error on runtime:我在运行时收到此错误:

 * Serving Flask app "web.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
Using TensorFlow backend.
Usage: flask run [OPTIONS]

Error: While importing "web", an ImportError was raised:

Traceback (most recent call last):
  File "/home/connor/.local/lib/python3.7/site-packages/flask/cli.py", line 240, in locate_app
    __import__(module_name)
  File "/home/connor/programming/python/Automated-Checkout/web.py", line 3, in <module>
    from prediction import NeuralNetwork
  File "/home/connor/programming/python/Automated-Checkout/prediction.py", line 2, in <module>
    from keras.preprocessing.image import img_to_array
  File "/home/connor/.local/lib/python3.7/site-packages/keras/__init__.py", line 3, in <module>
    from . import utils
  File "/home/connor/.local/lib/python3.7/site-packages/keras/utils/__init__.py", line 6, in <module>
    from . import conv_utils
  File "/home/connor/.local/lib/python3.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
    from .. import backend as K
  File "/home/connor/.local/lib/python3.7/site-packages/keras/backend/__init__.py", line 1, in <module>
    from .load_backend import epsilon
  File "/home/connor/.local/lib/python3.7/site-packages/keras/backend/load_backend.py", line 90, in <module>
    from .tensorflow_backend import *
  File "/home/connor/.local/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
    import tensorflow as tf
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/__init__.py", line 28, in <module>
    from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/__init__.py", line 49, in <module>
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python3.7/dist-packages/tensorflow/python/pywrap_tensorflow.py", line 25, in <module>
    from tensorflow.python.platform import self_check
ModuleNotFoundError: No module named 'tensorflow.python.platform'

I am certain that Tensorflow works and I am able to run the file standalone.我确信 Tensorflow 可以工作并且我能够独立运行该文件。 I also tried importing the file from a small non-Flask Python file and running it and the code runs.我还尝试从一个小的非 Flask Python 文件导入文件并运行它并运行代码。 Any help would be immensely appreciated, thank you.任何帮助将不胜感激,谢谢。

From error it looks like Tensorflow not properly installed.从错误看来,Tensorflow 没有正确安装。

ModuleNotFoundError: No module named 'tensorflow.python.platform'

Follow the instructions mentioned below按照下面提到的说明进行操作

#Install tensorflow using pip virtual env 
$pip install virtualenv
$virtualenv tf_2   # tf_2 is virtual env name
$source tf_2/bin/activate
tf_2 $ pip install tensorflow
tf_2 $ python
>>import tensorflow as tf
>>tf.__version__
2.6.0

For other reasons my script requires me to use older tf versions.由于其他原因,我的脚本要求我使用较旧的 tf 版本。 I did experience this error with version 1.15.0, but after upgrading to version 1.15.2 it disappeared.我确实在 1.15.0 版中遇到过这个错误,但在升级到 1.15.2 版后它就消失了。 So I assume there's a sweet spot of versions that include tensorflow.pypthon.platform .所以我假设有一个包含tensorflow.pypthon.platform的版本的甜蜜点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有名为 tensorflow.python.platform 的模块 - No module named tensorflow.python.platform ModuleNotFoundError:在 vs 代码 python 中没有名为导入错误的模块 - ModuleNotFoundError: No module named import error in vs-code python Python导入错误ModuleNotFoundError:停电后没有名为“ _ctypes”的模块 - Python import error ModuleNotFoundError: No module named '_ctypes', after power outage ModuleNotFoundError:没有名为 Python 的模块错误 - ModuleNotFoundError: no module named Python Error ModuleNotFoundError:没有名为“tensorflow.python”的模块 Anaconda - ModuleNotFoundError: No module named 'tensorflow.python' Anaconda ModuleNotFoundError:没有名为“tensorflow.python.util”的模块 - ModuleNotFoundError: No module named 'tensorflow.python.util' ModuleNotFoundError:没有名为“tensorflow.python.trackable”的模块 - ModuleNotFoundError: No module named 'tensorflow.python.trackable' ModuleNotFoundError:没有名为“tensorflow.contrib”的模块错误 - ModuleNotFoundError: No module named 'tensorflow.contrib' Error ModuleNotFoundError:没有名为“tensorflow.python.training”的模块 - ModuleNotFoundError: No module named 'tensorflow.python.training' Python-TensorFlow-ModuleNotFoundError:没有名为x的模块 - Python - TensorFlow - ModuleNotFoundError: No module named x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM