3

After following through some video tutorials, I have successfully trained a machine learning model using python 3.6, tensorflow and keras. But when I'm trying to open my trained model, it throws an error saying

OSError: Unable to open file (unable to open file: name = 'logs/Cats-vs-dog-cnn-64x2', errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)

Here's the code snippet I used to save the trained model

NAME = "Cats-vs-dog-cnn-64x2"
tensorboard = TensorBoard(log_dir='logs/{}'.format(NAME))

model = Sequential()
model.add(Conv2D(64, (3,3), input_shape=X.shape[1:]))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Conv2D(64, (3,3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2)))

model.add(Flatten())
model.add(Dense(64))
model.add(Activation('relu'))

model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
             optimizer='adam',
             metrics=['accuracy'])

model.fit(X, y, batch_size=5, epochs=10, validation_split=0.1, callbacks=[tensorboard])

Here's how I'm trying to open that trained model from another python file.

import cv2
import tensorflow as tf

CATEGORIES = ["Dog", "Cat"]

def prepare(filepath):
  IMG_SIZE = 70
  img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
  new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
  return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)

model = tf.keras.models.load_model("logs/{}".format("Cats-vs-dog-cnn-64x2"))

Here's the complete error log.

Traceback (most recent call last): File "C:\Users\Thamindu\Music\test\test.py", line 12, in model = tf.keras.models.load_model("logs/{}".format("Cats-vs-dog-cnn-64x2"))
File "C:\Users\Thamindu\AppData\Local\Programs\Python\Python36\lib\site-packages\tensorflow\python\keras\engine\saving.py", line 219, in load_model f = h5py.File(filepath, mode='r') File "C:\Users\Thamindu\AppData\Local\Programs\Python\Python36\lib\site-packages\h5py_hl\files.py", line 394, in init swmr=swmr) File "C:\Users\Thamindu\AppData\Local\Programs\Python\Python36\lib\site-packages\h5py_hl\files.py", line 170, in make_fid fid = h5f.open(name, flags, fapl=fapl) File "h5py_objects.pyx", line 54, in h5py._objects.with_phil.wrapper File "h5py_objects.pyx", line 55, in h5py._objects.with_phil.wrapper
File "h5py\h5f.pyx", line 85, in h5py.h5f.open OSError: Unable to open file (unable to open file: name = 'logs/Cats-vs-dog-cnn-64x2', errno = 13, error message = 'Permission denied', flags = 0, o_flags = 0)

Any help would be appreciated.

Thamindu DJ
  • 1,679
  • 4
  • 16
  • 29
  • 1
    Does the file indeed exist? This error is often due to incorrect file path. I can see that you try to open `logs/Cats-vs-dog-cnn-64x2`, normally it should have an `.h5` extension. – Dmytro Prylipko Feb 03 '19 at 11:14
  • @DmytroPrylipko "Cats-vs-dog-cnn-64x2" is a folder inside logs folder (which were created by training code). There is a file named "events.out.tfevents.1549107119" inside Cats-vs-dog-cnn-64x2. Does that a error in training code? – Thamindu DJ Feb 03 '19 at 13:08
  • 1
    It's not a model, it's logs written in Tensorboard format. Did you save the model at all? E.g. using ModelCheckpoint callback? https://keras.io/callbacks/#modelcheckpoint – Dmytro Prylipko Feb 03 '19 at 14:27
  • I have only save the tensorboard log. Isn't that enough? And if not, can you mention how to save the model and open it? @DmytroPrylipko – Thamindu DJ Feb 04 '19 at 03:46
  • 1
    tensorboard logs are for writing progress of model while it is in training like loss,accuracy etc. To save keras model you can refer here https://stackoverflow.com/questions/51628450/how-to-save-a-trained-model-in-keras-to-use-it-in-an-application/51632087#51632087 – Upasana Mittal Feb 04 '19 at 08:32
  • 1
    Thank you @UpasanaMittal . With both of your help, it turns out to be I have only saved the tensorboard log, but not the trained model. So I have modified my code a lot and now it works fine. Thank you both very much again. – Thamindu DJ Feb 04 '19 at 09:32

0 Answers0