Hi friends,
Tried to follow the pre-trained model example
model <- application_resnet50(weights = 'imagenet')
It fails with the following error.
Using TensorFlow backend.
Error in py_call_impl(callable, dots$args, dots$keywords) :
OSError: Unable to open file (Unable to open file: name = 'd:\鐢ㄦ埛鐩綍\鎴戠殑鏂囨。\.keras\models\resnet50_weights_tf_dim_ordering_tf_kernels.h5', errno = 2, error message = 'no such file or directory', flags = 0, o_flags = 0)
Detailed traceback:
File "C:\Users\ADMINI~1\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\applications\resnet50.py", line 264, in ResNet50
model.load_weights(weights_path)
File "C:\Users\ADMINI~1\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\engine\topology.py", line 2566, in load_weights
f = h5py.File(filepath, mode='r')
File "C:\Users\ADMINI~1\ANACON~1\envs\R-TENS~1\lib\site-packages\h5py\_hl\files.py", line 271, in __init__
fid = make_fid(name, mode, userblock_size, fapl, swmr=swmr)
File "C:\Users\ADMINI~1\ANACON~1\envs\R-TENS~1\lib\site-packages\h5py\_hl\files.py", line 101, in make_fid
fid = h5f.open(name, flags, fapl=fapl)
File "h5py\_objects.pyx", line 54, in h5py._objects.with_phil.wrapper (C:\Minonda\conda-bld\h5py_149
I tired to use the conda r-tensorflow environment to run python and keras.
It succeeded to load the resnet50 model and predict correctly.
But in R, all the pre-trained model can not run, such as application_vgg16, application_vgg19 etc.
I can save and load my own keras model as hdf5 in R.
It can not load the local pre-trained model file with load_model_hdf5, either with the same error.
Currently I am using keras 2.0.5.9011.
Thank you.
I can reproduce the problem within Python when I call load_weights with a path containing "鐢ㄦ埛鐩綍\鎴戠殑鏂囨。". However, if I os.chdir into the models directory first then I can call load_weights successfully:
import os
import keras
model = keras.applications.ResNet50(weights = None)
# use relative path (works)
os.chdir('C:\\Users\\jjallaire\\Documents\\models\\鐢ㄦ埛鐩綍\\鎴戠殑鏂囨。')
model.load_weights('resnet50_weights_tf_dim_ordering_tf_kernels.h5')
# use absolute path (does not work)
weights = 'C:\\Users\\jjallaire\\Documents\\models\\鐢ㄦ埛鐩綍\\鎴戠殑鏂囨。\\resnet50_weights_tf_dim_ordering_tf_kernels.h5'
os.path.isfile(weights) # returns True
model.load_weights(weights)
So I suspect it has something to do with the way the characters are being handled from within load_weights?
This may actually be a bug in HDF5: https://tschoonj.github.io/blog/2014/11/06/hdf5-on-windows-utf-8-filenames-support/
@vincenhe You'll want to use English for your folder/file names. It's likely that you'll encounter similar issue that different libraries don't handle utf-8 characters properly.
Thank you for your help.
I changed the default document profile folder name into English, and it solved the problem well. :+1:
By the way, if using load_model_hdf5 to load the pre-trained model local file, it report error even though the location is all English.
Is it because this function can only load the model file saved by keras in R but not other pre-trained file or model file saved by other software?
Thank you.
There shouldn't be any problem loading saved models. What error did you see?
Before raising this issue, I also suspected the it is the utf-8 Chinese directory location problem.
So I copied the downloaded pre-trained local model file to an English directory and tried to use the load_model_hdf5 function to load the pre-trained model directly.
library(keras)
setwd("D:/Study/R/Keras/pre-trained model")
model <- load_model_hdf5("vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5")
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: No model found in config file.
Detailed traceback:
File "C:\Users\ADMINI~1\ANACON~1\envs\R-TENS~1\lib\site-packages\keras\models.py", line 231, in load_model
raise ValueError('No model found in config file.')
But I can use this function to load the model I trained and saved by keras package in R in the same way.
model <- load_model_hdf5("imdb_cnn_2017-08-01.h5")
Thank you.
The problem is that those are model weights rather than a model. You could load them with load_model_weights_hdf5