OSError: SavedModel file does not exist at: ../dnn/mpg_model.h5/{saved_model.pbtxt|saved_model.pb}

15,990

Solution 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"))

Solution 2

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

Share:
15,990

Related videos on Youtube

Mohamed Magdy
Author by

Mohamed Magdy

Updated on June 04, 2022

Comments

  • Mohamed Magdy
    Mohamed Magdy almost 2 years

    **

    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
    Mohamed Magdy almost 4 years
    Do you mean this error from code not from path or python or TensorFlow or conda?
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    But this error appears when I install Tensorflow_GPU the code works well before it
  • sebtheiler
    sebtheiler almost 4 years
    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.
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    (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 0.0.0.0:5000 (Press CTRL+C to quit)
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    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
  • sebtheiler
    sebtheiler almost 4 years
    Does the path ./dnn/mpg_model.h5 exist when you are running the code with TensorFlow GPU?
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    when i run code with tensorflow CPU the path ./dnn/mpg_model.h5 exist
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    (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
    Mohamed Magdy almost 4 years
    when I run it this was the result with no errors and no edit in the file
  • sebtheiler
    sebtheiler almost 4 years
    But it doesn't exist when you run the exact same code with TensorFlow GPU?
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    yeah it doesn't exist when i run the exact same code with TensorFlow GPU
  • sebtheiler
    sebtheiler almost 4 years
    Are you getting any errors in the code that saves the model, that might possibly prevent it from saving?
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    No errors but the web server didn't open. Also Jeff didn't open it. watch this video youtube.com/watch?v=H73m9XvKHug&t=1056s github.com/jeffheaton/t81_558_deep_learning/blob/master/…
  • sebtheiler
    sebtheiler almost 4 years
    Could you give a complete code sample, preferably a Colab notebook, that completely replicates your issue?
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    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
    Mohamed Magdy almost 4 years
    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 '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
  • sebtheiler
    sebtheiler almost 4 years
    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')
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    the env has all packages so i will try to change model.save . thanks
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    how can I install tensorflow without env?
  • sebtheiler
    sebtheiler almost 4 years
    If you have pip installed, you could use pip install tensorflow or pip install tensorflow-gpu
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    ERROR: Could not install packages due to an EnvironmentError: [Errno 2] No such file or directory: 'c:\\users\\asus\\anaconda3\\anaconda\\lib\\site-packages\\n‌​umpy-1.18.4.dist-inf‌​o\\METADATA'
  • sebtheiler
    sebtheiler almost 4 years
    Maybe check out this question for that error: stackoverflow.com/questions/54778630/…
  • Mohamed Magdy
    Mohamed Magdy almost 4 years
    I install tensorflow but I have an error in keras [stackoverflow.com/questions/61929275/…
  • sebtheiler
    sebtheiler almost 4 years
    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)