According to documentation, it says that the default download directory for all Keras files is $HOME/.keras
. I'm using virtual environment and I want to change the default download directory of pre-trained models to a different directory. Maybe this has more to do with virtualenv than with Keras?
-
1We can see here that the dir is hardcoded: github.com/keras-team/keras/blob/master/keras/utils/… --- So you can't change it (unless you change keras code, which might be unsafe).– Daniel MöllerCommented Mar 1, 2018 at 17:40
3 Answers
If you are using the master branch of keras, you can set the KERAS_HOME
environment variable to set the cache directory. If it is not set, cache directory defaults to $HOME/.keras
.
export KERAS_HOME="/path/to/keras/dir"
Add the line to your ".bashrc" to set the variable every time you open up a new terminal.
This has not yet been released, you must use the master branch to use this feature.
-
1I'm using keras 2.3.0-tf. When I set KERAS_HOME to ${HOME}/Downloads/keras, keras seems to recognized it, and created keras.json file in that directory, but it doesn't cache any download data. Commented Jul 11, 2020 at 15:25
According to documentation
Signature: ResNet50(include_top=True, weights='imagenet', input_tensor=None, input_shape=None, pooling=None, classes=1000)
There's no parameter to specify where to download the pre-trained model weights.
(1) What you can do is to move the file to where you want it to be after the download from your terminal using mv
(https://www.macworld.com/article/2080814/master-the-command-line-copying-and-moving-files.html).
UPDATE: I went to check the github repo of Keras (https://github.com/keras-team/keras/blob/master/keras/applications/resnet50.py) and found the link to the weights. For resnet:
WEIGHTS_PATH = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels.h5'
WEIGHTS_PATH_NO_TOP = 'https://github.com/fchollet/deep-learning-models/releases/download/v0.2/resnet50_weights_tf_dim_ordering_tf_kernels_notop.h5'
You can download those weights directly to your file system using whatever methods (i.e. urllib
).
-
The problem is I only have limited storage permission to download it first.– SunilCommented Mar 1, 2018 at 17:57
-
3@Sunil The path
~/.keras
is hardcoded but it's not necessary be a directory. It can be a symlink to a directory where you have enough space.– phdCommented Mar 1, 2018 at 18:39 -
Thank you @YilunZhang, I was about to try it but I used the solution from phd. Symbolic link was what I needed.– SunilCommented Mar 1, 2018 at 18:54
-
Downloading the weights may be unsafe. Keras does treat them depending on the backend you're using and your data format. Commented Mar 1, 2018 at 18:54
You can copy the model fiel *.h5 that has been downloaded via other ways into the Keras default models directory ~/keras/models
.