14

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?

1

3 Answers 3

5

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.

1
  • 1
    I'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.
    – Guangliang
    Commented Jul 11, 2020 at 15:25
2

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).

4
  • The problem is I only have limited storage permission to download it first.
    – Sunil
    Commented 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.
    – phd
    Commented 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.
    – Sunil
    Commented 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
0

You can copy the model fiel *.h5 that has been downloaded via other ways into the Keras default models directory ~/keras/models.

Not the answer you're looking for? Browse other questions tagged or ask your own question.