[英]ModuleNotFoundError: No module named 'tensorflow.python.platform' Import error
我正在使用 Keras、Flask 和 Tensorflow。 我目前有两个文件,一个用于烧瓶,另一个用于神经网络。
网络.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)
预测.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)
我在运行时收到此错误:
* 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'
我确信 Tensorflow 可以工作并且我能够独立运行该文件。 我还尝试从一个小的非 Flask Python 文件导入文件并运行它并运行代码。 任何帮助将不胜感激,谢谢。
从错误看来,Tensorflow 没有正确安装。
ModuleNotFoundError: No module named 'tensorflow.python.platform'
按照下面提到的说明进行操作
#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
由于其他原因,我的脚本要求我使用较旧的 tf 版本。 我确实在 1.15.0 版中遇到过这个错误,但在升级到 1.15.2 版后它就消失了。 所以我假设有一个包含tensorflow.pypthon.platform
的版本的甜蜜点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.