0

I have created a model named 'model' but when I'm trying to save it using pickle it just gives an 'NotFoundError'.

import pickle
with open("test.pkl","wb") as file:
    pickle.dump(model, file)

This is the error message I get upon running the code.

Error message

INFO:tensorflow:Assets written to: ram://471dfd58-f3fe-4d9f-9075-60a1568cc629/assets
---------------------------------------------------------------------------
NotFoundError                             Traceback (most recent call last)
<ipython-input-47-0abd122520e5> in <module>
      1 import pickle
      2 with open("test.pkl","wb") as file:
----> 3     pickle.dump(model, file)

~\anaconda3\lib\site-packages\keras\engine\training.py in __reduce__(self)
    313     if self.built:
    314       return (pickle_utils.deserialize_model_from_bytecode,
--> 315               pickle_utils.serialize_model_as_bytecode(self))
    316     else:
    317       # SavedModel (and hence serialize_model_as_bytecode) only support

~\anaconda3\lib\site-packages\keras\saving\pickle_utils.py in serialize_model_as_bytecode(model)
     75         with tf.io.gfile.GFile(dest_path, "rb") as f:
     76           info = tarfile.TarInfo(name=os.path.relpath(dest_path, temp_dir))
---> 77           info.size = f.size()
     78           archive.addfile(tarinfo=info, fileobj=f)
     79   tf.io.gfile.rmtree(temp_dir)

~\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in size(self)
     97   def size(self):
     98     """Returns the size of the file."""
---> 99     return stat(self.__name).length
    100 
    101   def write(self, file_content):

~\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in stat(filename)
    908     errors.OpError: If the operation fails.
    909   """
--> 910   return stat_v2(filename)
    911 
    912 

~\anaconda3\lib\site-packages\tensorflow\python\lib\io\file_io.py in stat_v2(path)
    924     errors.OpError: If the operation fails.
    925   """
--> 926   return _pywrap_file_io.Stat(compat.path_to_str(path))
    927 
    928 

NotFoundError: 

Any help would be massively appreciated

  • your error shows problem with reading file `open('model_pkl' , 'rb')` and `pickle.load(f)`, not with writing. – furas Dec 03 '21 at 17:31
  • @furas Actually open('model_pkl','rb') was the next cell following this error message and it got copied by mistake. Error message just ends with ' NotFoundError: '. I'll make an edit. – Sathvik Purushotham Dec 04 '21 at 01:47

1 Answers1

0

Not a solution but hopefully it still helps.

I had the same problem, and the same error. I ended up avoiding it by using Keras save and load methods instead of pickle. I don't know what your model is but you might want to try the same. It might be due to what is pickeleable. Maybe the answer given to this helps, they argue for saving tensorflow object separately.

TGze
  • 1
  • Thanks for your comment. I know I'm super late. I ended up doing the same, used keras save and saved my model as a .h5 config and it worked without any problem. – Sathvik Purushotham Jan 23 '22 at 18:27