2

**

code editor: vscode

cmd: anaconda prompt

I followed the tutorial but why this error? **

first error was ModuleNotFoundError: No module named 'tensorflow' but i make env and install it second error was ModuleNotFoundError: No module named 'flask' but i make env and install it i fix them and they work on python How can I solve this?

# T81-558: Applications of Deep Neural Networks
# Module 13: Advanced/Other Topics
# Instructor: [Jeff Heaton](https://sites.wustl.edu/jeffheaton/), McKelvey School of Engineering, [Washington University in St. Louis](https://engineering.wustl.edu/Programs/Pages/default.aspx)
# For more information visit the [class website](https://sites.wustl.edu/jeffheaton/t81-558/).
# Deploy simple Keras tabular model with Flask only.
from flask import Flask, request, jsonify
import uuid
import os
from tensorflow.keras.models import load_model
import numpy as np

app = Flask(__name__)

# Used for validation
EXPECTED = {
  "cylinders":{"min":3,"max":8},
  "displacement":{"min":68.0,"max":455.0},
  "horsepower":{"min":46.0,"max":230.0},
  "weight":{"min":1613,"max":5140},
  "acceleration":{"min":8.0,"max":24.8},
  "year":{"min":70,"max":82},
  "origin":{"min":1,"max":3}
}

# Load neural network when Flask boots up
model = load_model(os.path.join("../dnn/","mpg_model.h5"))

@app.route('/api/mpg', methods=['POST'])
def calc_mpg():
    content = request.json
    errors = []

    # Check for valid input fields 
    for name in content:
      if name in EXPECTED:
        expected_min = EXPECTED[name]['min']
        expected_max = EXPECTED[name]['max']
        value = content[name]
        if value < expected_min or value > expected_max:
          errors.append(f"Out of bounds: {name}, has value of: {value}, but should be between {expected_min} and {expected_max}.")
      else:
        errors.append(f"Unexpected field: {name}.")

    # Check for missing input fields
    for name in EXPECTED:
      if name not in content:
        errors.append(f"Missing value: {name}.")

    if len(errors) <1:
      # Predict
      x = np.zeros( (1,7) )

      x[0,0] = content['cylinders']
      x[0,1] = content['displacement'] 
      x[0,2] = content['horsepower']
      x[0,3] = content['weight']
      x[0,4] = content['acceleration'] 
      x[0,5] = content['year']
      x[0,6] = content['origin']

      pred = model.predict(x)
      mpg = float(pred[0])
      response = {"id":str(uuid.uuid4()),"mpg":mpg,"errors":errors}
    else:
      # Return errors
      response = {"id":str(uuid.uuid4()),"errors":errors}


    print(content['displacement'])

    return jsonify(response)

if __name__ == '__main__':
    app.run(host= '0.0.0.0',debug=True)
#conda
(tf-gpu) (HelloWold) C:\Users\ASUS\t81_558_deep_learning\py>python mpg_server_1.py
2020-05-09 17:25:38.498181: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_101.dll
Traceback (most recent call last):
  File "mpg_server_1.py", line 26, in <module>
    model = load_model(os.path.join("../dnn/","mpg_model.h5"))
  File "C:\Users\ASUS\Envs\HelloWold\lib\site-packages\tensorflow\python\keras\saving\save.py", line 189, in load_model
    loader_impl.parse_saved_model(filepath)
  File "C:\Users\ASUS\Envs\HelloWold\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 113, in parse_saved_model
    constants.SAVED_MODEL_FILENAME_PB))
OSError: SavedModel file does not exist at: ../dnn/mpg_model.h5/{saved_model.pbtxt|saved_model.pb}

from https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_13_01_flask.ipynb https://www.youtube.com/watch?v=H73m9XvKHug&t=1056s

Mohamed Magdy
  • 41
  • 2
  • 5
  • 10

4 Answers4

1

The error occurs because your code is trying to load a model that does not exist. From the Notebook file you linked, you will most likely have to run the following:

from werkzeug.wrappers import Request, Response
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == '__main__':
    from werkzeug.serving import run_simple
    run_simple('localhost', 9000, app)

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from sklearn.model_selection import train_test_split
from tensorflow.keras.callbacks import EarlyStopping
import pandas as pd
import io
import os
import requests
import numpy as np
from sklearn import metrics

df = pd.read_csv(
    "https://data.heatonresearch.com/data/t81-558/auto-mpg.csv", 
    na_values=['NA', '?'])

cars = df['name']

# Handle missing value
df['horsepower'] = df['horsepower'].fillna(df['horsepower'].median())

# Pandas to Numpy
x = df[['cylinders', 'displacement', 'horsepower', 'weight',
       'acceleration', 'year', 'origin']].values
y = df['mpg'].values # regression

# Split into validation and training sets
x_train, x_test, y_train, y_test = train_test_split(    
    x, y, test_size=0.25, random_state=42)

# Build the neural network
model = Sequential()
model.add(Dense(25, input_dim=x.shape[1], activation='relu')) # Hidden 1
model.add(Dense(10, activation='relu')) # Hidden 2
model.add(Dense(1)) # Output
model.compile(loss='mean_squared_error', optimizer='adam')

monitor = EarlyStopping(monitor='val_loss', min_delta=1e-3, patience=5, verbose=1, mode='auto',
        restore_best_weights=True)
model.fit(x_train,y_train,validation_data=(x_test,y_test),callbacks=[monitor],verbose=2,epochs=1000)

pred = model.predict(x_test)
# Measure RMSE error.  RMSE is common for regression.
score = np.sqrt(metrics.mean_squared_error(pred,y_test))
print(f"After load score (RMSE): {score}")

model.save(os.path.join("./dnn/","mpg_model.h5"))

This will train and save the model that your code is loading.

It also looks like you have a small typo on the line: model = load_model(os.path.join("../dnn/","mpg_model.h5")) which should be changed to model = load_model(os.path.join("./dnn/","mpg_model.h5"))

sebtheiler
  • 2,159
  • 3
  • 16
  • 29
  • Do you mean this error from code not from path or python or TensorFlow or conda? – Mohamed Magdy May 09 '20 at 17:08
  • But this error appears when I install Tensorflow_GPU the code works well before it – Mohamed Magdy May 09 '20 at 17:14
  • 1
    The code works without TensorFlow GPU? Do you mean it works with TensorFlow CPU or without TensorFlow at all? The `OSError: SavedModel file does not exist at: ../dnn/mpg_model.h5/{saved_model.pbtxt|saved_model.pb}` occurs because there is no file at the location you are trying to access. – sebtheiler May 09 '20 at 17:42
  • (tensorflow) C:\Users\ASUS\dev\Hellowold>python mpg_server_1.py 2020-05-07 16:04:09.737598: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 * Serving Flask app "mpg_server_1" (lazy loading) * Environment: tensorflow * Debug mode: on * Restarting with stat * Debugger is active! * Debugger PIN: 254-090-327 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) – Mohamed Magdy May 09 '20 at 17:51
  • no it run with tensorflow cpu ```2020-05-07 16:04:18.352421: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 ``` – Mohamed Magdy May 09 '20 at 17:55
  • Does the path `./dnn/mpg_model.h5` exist when you are running the code with TensorFlow GPU? – sebtheiler May 09 '20 at 18:07
  • when i run code with tensorflow CPU the path ```./dnn/mpg_model.h5``` exist – Mohamed Magdy May 10 '20 at 01:59
  • ```(tensorflow) C:\Users\ASUS\dev\Hellowold>python mpg_server_1.py 2020-05-07 16:04:09.737598: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2 * Serving Flask app "mpg_server_1" (lazy loading) * Environment: tensorflow * Debug mode: on * Restarting with stat I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary * Debugger is active! * Debugger PIN: 254-090-327 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)``` – Mohamed Magdy May 10 '20 at 02:03
  • when I run it this was the result with no errors and no edit in the file – Mohamed Magdy May 10 '20 at 02:07
  • But it doesn't exist when you run the exact same code with TensorFlow GPU? – sebtheiler May 10 '20 at 11:49
  • yeah it doesn't exist when i run the exact same code with TensorFlow GPU – Mohamed Magdy May 12 '20 at 07:17
  • Are you getting any errors in the code that saves the model, that might possibly prevent it from saving? – sebtheiler May 12 '20 at 23:09
  • No errors but the web server didn't open. Also Jeff didn't open it. watch this video https://www.youtube.com/watch?v=H73m9XvKHug&t=1056s https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_13_01_flask.ipynb – Mohamed Magdy May 15 '20 at 06:32
  • Could you give a complete code sample, preferably a Colab notebook, that completely replicates your issue? – sebtheiler May 16 '20 at 00:03
  • https://github.com/jeffheaton/t81_558_deep_learning/blob/master/t81_558_class_13_01_flask.ipynb – Mohamed Magdy May 19 '20 at 01:49
  • The error comes when I deactivate tensorFlow and activate tf_gpu , I think I should delete envs and install flask and tensorflow in my python not env? – Mohamed Magdy May 19 '20 at 02:03
  • In this github tutorial when I follow it and run mpg_server_1 , it makes error "ModuleNotFoundError: No module named 'flask'" , ModuleNotFoundError: No module named 'tensorflow', so to solve this i make envs and setup tensorflow and flask in it, it names 'tensorflow' it run code with no error but 'http://0.0.0.0:5000/' is 'This site can’t be reached' so I think there was an error. so i make new env name tf_gpu and setup 'conda install tensorflow-gpu=2.1' and flask but it makes oserror – Mohamed Magdy May 19 '20 at 02:16
  • Could you give a list of installed packages in the current environment (use `conda list`)? Make sure you have the other dependencies such as Pandas and scikit-learn installed as well. If you do, and the code still isn't running, you might want to try changing `model.save(os.path.join("./dnn/","mpg_model.h5"))` to just `model.save('mpg_model.h5')` and changing `model = load_model(os.path.join("./dnn/","mpg_model.h5"))` to `model = load_model('mpg_model.h5')` – sebtheiler May 19 '20 at 23:36
  • the env has all packages so i will try to change model.save . thanks – Mohamed Magdy May 20 '20 at 03:00
  • how can I install tensorflow without env? – Mohamed Magdy May 20 '20 at 11:29
  • If you have pip installed, you could use `pip install tensorflow` or `pip install tensorflow-gpu` – sebtheiler May 20 '20 at 13:05
  • ```ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'c:\\users\\asus\\anaconda3\\anaconda\\lib\\site-packages\\numpy-1.18.4.dist-info\\METADATA'``` – Mohamed Magdy May 20 '20 at 15:23
  • Maybe check out this question for that error: https://stackoverflow.com/questions/54778630/could-not-install-packages-due-to-an-environmenterror-errno-2-no-such-file-or – sebtheiler May 21 '20 at 01:03
  • I install tensorflow but I have an error in keras [https://stackoverflow.com/questions/61929275/attributeerror-module-tensorflow-has-no-attribute-keras-in-conda-prompt] – Mohamed Magdy May 21 '20 at 09:09
  • I don't have enough rep to comment on the other question, but when the guy asked you to run `print(tensorflow)`, he meant with the ', so **not** `import tensorflow; print('tensorflow')` but `import tensorflow; print(tensorflow)` – sebtheiler May 21 '20 at 20:47
1

I was getting the same error trying to load a .h5 model on a raspberry pi.

OSError: SavedModel file does not exist at: ... {saved_model.pbtxt|saved_model.pb}

sudo apt install python3-h5py

Seemed to have solved the issue.

reference

Chris
  • 2,166
  • 1
  • 24
  • 37
0

If on windows, the path to the model can cause the error.

For a sanity check, try placing the model in the same folder as the file that you are calling. Then fix your path to call the model from the same folder. This fixed my error.

If this works, then you can figure out how to fix the path issue (perhaps try providing an absolute path).

Jonathan
  • 1,876
  • 2
  • 20
  • 56
0

I got the same error, and I solved it by running the model again and save it with .pb extension instead with the .h5 or .hdf5 extension

Then use the tf.keras.models.load_model('D:\\model_name.pb') using double backslash I had that error in windows and could solve it