Mirza Munib Baig
Mirza Munib Baig

Reputation: 311

OSError: SavedModel file does not exist at: C:\Users\Munib\New folder/{saved_model.pbtxt|saved_model.pb}

I wanted to use my keras trained model in android studio. I got this code on internet to convert my code from keras to tensorflow-lite. But when i tried code i got this error:

OSError: SavedModel file does not exist at: C:\Users\Munib\New folder/{saved_model.pbtxt|saved_model.pb}

The code i used from converting from keras to tensorflow-lite:

import tensorflow as tf
# Converting a SavedModel to a TensorFlow Lite model.
saved_model_dir = r"C:\Users\Munib\New folder"
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()

# Converting a tf.Keras model to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

# Converting ConcreteFunctions to a TensorFlow Lite model.
converter = tf.lite.TFLiteConverter.from_concrete_functions([func])
tflite_model = converter.convert()

Upvotes: 18

Views: 62602

Answers (10)

Abdul Rauf Mirzayee
Abdul Rauf Mirzayee

Reputation: 46

I got the same problem as you when I was converting my YOLO v8 model to tflite as I searched and found yours.

If the response didn’t help me, then I searched for the alternative solution and by changing some parts of my code, I could overcome the problem.

Here is the code which I got errors in:

from ultralytics import YOLO
# Load a model
model = YOLO('/content/drive/MyDrive/ANPR_AF/runs/detect/train7/weights/best.pt')  # load a custom trained

# Export the model
model.export(format='tflite')

After some exploration, I found a solution. Here is the code for the solution:

!yolo detect export model=/content/drive/MyDrive/ANPR_AF/runs/detect/train7/weights/best.pt format=tflite

Finally I was able to convert my best.pt model to best_float32.tiflite here it is:

/content/drive/MyDrive/ANPR_AF/runs/detect/train7/weights/best_saved_model/best_float32.tflite

Upvotes: 1

Apollo
Apollo

Reputation: 11

I received this error with a model I downloaded from TensorFlow Hub. I was able to solve it by calling the folder the saved_model.pb file was located in rather than the file itself. So in my case it was a simple matter of keeping saved_model.pb and tfhub_module.pb in the aiy_vision_classifier_food_V1_1 folder and using the code:

model = tf.saved_model.load('aiy_vision_classifier_food_V1_1')

Upvotes: 1

J Pablo F
J Pablo F

Reputation: 31

Try to read the file.

I got the error for downloading the model using wget <url> -o model.hdf5 instead of wget <url> -O model.hdf5, so model.hdf5 was a log file instead of the model I was expecting.

The file should be a binay or a non UTF-8 file that you should't be able to read.

Upvotes: 0

Ali El Habchi
Ali El Habchi

Reputation: 439

I had a similar issue, i had a keras model i saved using:

my_model.save('./models/my_model')

and when i was trying to load it i put the whole file name path like this:

my_model = tf.keras.models.load_model('./models/my_model/saved_model.pb')

and it gave the same error

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

i fixed it by only giving the name of the model i gave when saved it

my_model = tf.keras.models.load_model('./models/my_model')

Hope it helps

Upvotes: -1

Paul Fentress
Paul Fentress

Reputation: 81

I have been able to debug this error thanks to this post: https://codeutility.org/savedmodel-file-does-not-exist-when-using-tensorflow-hub-stack-overflow/

The steps that worked for me were:

  1. Locate the temp folder that was missing a previously loaded model. On mac I did this using this command:

    open $TMPDIR

  2. Then I went and located the temp dir in the error message, and deleted it.

OSError: SavedModel file does not exist at: /var/folders/px/7npbfl2n0vs4yqfq5skm9pnw0000gn/T/tfhub_modules/87f7b0d2504a48175f521bcaed174acabc93672c/{saved_model.pbtxt|saved_model.pb}

I searched up the bolded file name and found the empty directory.

Then, after removing it, I was able to load the model and train it successfully.

Essentially what I understood from the article above, is that TensorFlow will create a temp directory to keep loaded models; however, after a few days or so, the contents of the folers (the loaded model) will be deleted. Then when you look to load a model again, TensorFlow will route to the temp dir, but the model will be deleted from the temp dir.

This makes sense and explains why if your code is running totally fine the past few days, and then all of the sudden gets this error, it probably has to do with deleting the old temp directory.

Upvotes: 6

Palash Mondal
Palash Mondal

Reputation: 538

Just sharing my thought: I also got an error while I tried to run the bert encoder and preprocessor model. I ran last time when it's look like this.

bert_preprocess = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3")
bert_encoder = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4")

It's giving errors while I am trying to run the same after a week. To resolve this issue I download the .tar file and pass the file path to resolve the issue.

bert_preprocess = hub.KerasLayer("<path>/bert_en_uncased_preprocess_3.tar")
bert_encoder = hub.KerasLayer("<path>/bert_en_uncased_L-12_H-768_A-12_4.tar")

Upvotes: 0

HARSH DEVMURARI
HARSH DEVMURARI

Reputation: 11

you have to specify just the directory name where metedata.pb and model.pb is automatically stored for its successful run it not just model.pb but also metadata.pb

Upvotes: -1

Vicky
Vicky

Reputation: 1

The directory path should end with saved_model. For example, In your case it should be C:/Users/Munib/New folder/saved_model

Upvotes: -3

Mohammed Jafar Sadik
Mohammed Jafar Sadik

Reputation: 29

I also faced this problem when working on google Colab. It seemed, my Colab lose the directory path data where the model is located. Simply run these two lines again and try again. Hopefully, that'll solve the problem :

import os
os.chdir("/content/drive/My Drive/path/to/your/model")

Upvotes: 2

Mahmoud Youssef
Mahmoud Youssef

Reputation: 808

First of all it's best to use relative path instead of absolute path. second, if you use model.save('my_model') then keras will create a directory for you with the name my_model in which you should find a file with the pb or pbtxt extension, this is the directory you should use for tflite converter

Upvotes: 4

Related Questions