Varun S
Varun S

Reputation: 53

tf.keras save_model sometimes works and sometimes don't

NotFoundError: Failed to create a directory: fashion_mnist_model0/variables; No such file or directory
in TensorFlow 2.0 using Windows 10. https://colab.research.google.com/drive/1XpPQiSbJpVr8up66l93YUdAWty6Kd2k0 https://colab.research.google.com/drive/15BlkiQOd5zUCgNRfKs1tXbsk4o898wG9 Edit:Same notebook works in Colab but cause error in my PC

def func1(optimizer=None):
    model = Sequential()
    model.add(Conv2D(filters = 20, kernel_size = 3, padding = 'valid',input_shape = (28,28,1)))
    model.add(Activation("relu"))
    model.compile(loss= "categorical_crossentropy",optimizer = Adam(), metrics = ["accuracy"])
    return model
def func2(model_filename = 'temp_s0',model = None):
    if (model == None):
        model = func1()
        #with open(model_filename,'wb') as file :
            #pickle.dump(model,file)
        save_model(model,model_filename)
def func3(x_train,y_train,x_test,y_test,model_filename):
    model = func1()
    save_model(model,model_filename)

func2() works well gives: INFO:tensorflow:Assets written to: temp_s0\assets

However, for some reason, the following code doesn't work:

(x_train,y_train),(x_test,y_test) = fashion_mnist.load_data()
func3(x_train,y_train,x_test,y_test,model_filename = "fashion_mnist_model0")

gives :

NotFoundError                             Traceback (most recent call last)
<ipython-input-36-499ddb13879f> in <module>
----> 1 model , y_train_1 , y_test = func3(x_train,y_train,x_test,y_test,model_filename = "fashion_mnist_model0")

<ipython-input-34-48cc1e442911> in func3(x_train, y_train, x_test, y_test, model_filename)
      1 def func3(x_train,y_train,x_test,y_test,model_filename):
      2     model = func1()
----> 3     save_model(model,model_filename)

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\keras\saving\save.py in save_model(model, filepath, overwrite, include_optimizer, save_format, signatures, options)
    113   else:
    114     saved_model_save.save(model, filepath, overwrite, include_optimizer,
--> 115                           signatures, options)
    116 
    117 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\keras\saving\saved_model\save.py in save(model, filepath, overwrite, include_optimizer, signatures, options)
     72   # default learning phase placeholder.
     73   with K.learning_phase_scope(0):
---> 74     save_lib.save(model, filepath, signatures, options)
     75 
     76   if not include_optimizer:

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\saved_model\save.py in save(obj, export_dir, signatures, options)
    897   # the checkpoint, copy assets into the assets directory, and write out the
    898   # SavedModel proto itself.
--> 899   utils_impl.get_or_create_variables_dir(export_dir)
    900   object_saver.save(utils_impl.get_variables_path(export_dir))
    901   builder_impl.copy_assets_to_destination_dir(asset_info.asset_filename_map,

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\saved_model\utils_impl.py in get_or_create_variables_dir(export_dir)
    181   variables_dir = get_variables_dir(export_dir)
    182   if not file_io.file_exists(variables_dir):
--> 183     file_io.recursive_create_dir(variables_dir)
    184   return variables_dir
    185 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\lib\io\file_io.py in recursive_create_dir(dirname)
    436     errors.OpError: If the operation fails.
    437   """
--> 438   recursive_create_dir_v2(dirname)
    439 
    440 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\lib\io\file_io.py in recursive_create_dir_v2(path)
    451     errors.OpError: If the operation fails.
    452   """
--> 453   pywrap_tensorflow.RecursivelyCreateDir(compat.as_bytes(path))
    454 
    455 

NotFoundError: Failed to create a directory: fashion_mnist_model0/variables; No such file or directory

for some reason, I don't know

Upvotes: 1

Views: 578

Answers (1)

TF_Support
TF_Support

Reputation: 1836

This is more of a permission problem rather than a programming problem.

You could try running your Jupyter Notebook with Admin privileges.

Command prompt (Admin)

  1. Search cmd, then Run as administrator. from there you can go to the environment (conda env or virtualenv) or execute your code directly (jupyter notebook).

Anaconda Prompt (Admin) if using Conda distribution.

  1. Search Anaconda Prompt, from there you can go to the environment (condo env or virtualenv) or execute your code directly (jupyter notebook).

Upvotes: 1

Related Questions