简体   繁体   中英

Not able to resolve the error ModuleNotFoundError: No module named 'tensorflow.compat' in tensorflow and flask

I am trying to serve a TensorFlow model I built with flask. While I was running the flask code, it came up with this error I reinstalled conda but the error persisted. The thing is now even if I import tensorflow, this error comes up. I tried on another device which didn't have conda but just vanilla python. The same error came up.

this thread doesn't help

I will post the entire error here:

>>> import tensorflow as tf
2021-05-18 13:20:02.804699: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2021-05-18 13:20:02.830901: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\__init__.py", line 46, in <module>
    from tensorflow.python import data
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module>
    from tensorflow.python.data import experimental
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 99, in <module>
    from tensorflow.python.data.experimental import service
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 140, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 25, in <module>
    from tensorflow.python.data.experimental.ops import compression_ops
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in <module>
    from tensorflow.python.data.util import structure
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in <module>
    from tensorflow.python.data.util import nest
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\data\util\nest.py", line 40, in <module>
    from tensorflow.python.framework import sparse_tensor as _sparse_tensor
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 28, in <module>
    from tensorflow.python.framework import constant_op
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in <module>
    from tensorflow.python.eager import execute
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\eager\execute.py", line 28, in <module>
    from tensorflow.python.framework import ops
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\tensorflow\python\framework\ops.py", line 26, in <module>
    from absl import app
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\absl\app.py", line 35, in <module>
    import pdb
  File "c:\users\adi\appdata\local\programs\python\python39\lib\pdb.py", line 77, in <module>
    import code
  File "G:\AiDEV\Kid\CNN\New folder\code.py", line 4, in <module>
    from keras.preprocessing.image import ImageDataGenerator
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\keras\__init__.py", line 22, in <module>
    from keras import distribute
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\keras\distribute\__init__.py", line 18, in <module>
    from keras.distribute import sidecar_evaluator
  File "G:\AiDEV\Kid\CNN\myEnv\lib\site-packages\keras\distribute\sidecar_evaluator.py", line 18, in <module>
    import tensorflow.compat.v2 as tf
ModuleNotFoundError: No module named 'tensorflow.compat'

The flask code I am trying to run is this:

from flask import Flask, render_template, request
from werkzeug.utils import secure_filename
from werkzeug.datastructures import  FileStorage
from keras.preprocessing.image import ImageDataGenerator
import tensorflow as tf
import numpy as np
import os

try:
    import shutil
    shutil.rmtree('uploaded / image')
    # % cd uploaded % mkdir image % cd ..
    print()
except:
    pass

model = tf.keras.models.load_model('model')
app = Flask(__name__)

app.config['UPLOAD_FOLDER'] = 'uploaded / image'

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

def finds():
    test_datagen = ImageDataGenerator(rescale = 1./255)
    vals = ['Positive', 'Negative'] 
    test_dir = 'uploaded'
    test_generator = test_datagen.flow_from_directory(
            test_dir,
            target_size =(224, 224),
            color_mode ="rgb",
            shuffle = False,
            class_mode ='categorical',
            batch_size = 1)

    pred = model.predict_generator(test_generator)
    print(pred)
    return str(vals[np.argmax(pred)])

@app.route('/uploader', methods = ['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        f = request.files['file']
        f.save(os.path.join(app.config['UPLOAD_FOLDER'], secure_filename(f.filename)))
        val = finds()
        return render_template('pred.html', ss = val)

if __name__ == '__main__':
    app.run()

Thanks in advance.

Let's look at your traceback in detail, particularly here:

import pdb
File "c:\users\adi\appdata\local\programs\python\python39\lib\pdb.py", line 77, in <module>
import code
File "G:\AiDEV\Kid\CNN\New folder\code.py", line 4, in <module>
from keras.preprocessing.image import ImageDataGenerator

You can clearly see here that import code inside pdb is jumping back to your own code, and that is your have a file called code.py, never name one of your scripts the same as a known package, since this will shadow the real python package.

The solution here is to rename your code.py file to something else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM