tf.keras.utils.get_file
Stay organized with collections
Save and categorize content based on your preferences.
Downloads a file from a URL if it not already in the cache.
tf.keras.utils.get_file(
fname=None,
origin=None,
untar=False,
md5_hash=None,
file_hash=None,
cache_subdir='datasets',
hash_algorithm='auto',
extract=False,
archive_format='auto',
cache_dir=None,
force_download=False
)
Used in the notebooks
Used in the guide |
Used in the tutorials |
|
|
By default the file at the url origin
is downloaded to the
cache_dir ~/.keras
, placed in the cache_subdir datasets
,
and given the filename fname
. The final location of a file
example.txt
would therefore be ~/.keras/datasets/example.txt
.
Files in .tar
, .tar.gz
, .tar.bz
, and .zip
formats can
also be extracted.
Passing a hash will verify the file after download. The command line
programs shasum
and sha256sum
can compute the hash.
Example:
path_to_downloaded_file = get_file(
origin="https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz",
extract=True,
)
Args |
fname
|
Name of the file. If an absolute path, e.g. "/path/to/file.txt"
is specified, the file will be saved at that location.
If None , the name of the file at origin will be used.
|
origin
|
Original URL of the file.
|
untar
|
Deprecated in favor of extract argument.
boolean, whether the file should be decompressed
|
md5_hash
|
Deprecated in favor of file_hash argument.
md5 hash of the file for verification
|
file_hash
|
The expected hash string of the file after download.
The sha256 and md5 hash algorithms are both supported.
|
cache_subdir
|
Subdirectory under the Keras cache dir where the file is
saved. If an absolute path, e.g. "/path/to/folder" is
specified, the file will be saved at that location.
|
hash_algorithm
|
Select the hash algorithm to verify the file.
options are "md5' , "sha256' , and "auto' .
The default 'auto' detects the hash algorithm in use.
|
extract
|
True tries extracting the file as an Archive, like tar or zip.
|
archive_format
|
Archive format to try for extracting the file.
Options are "auto' , "tar' , "zip' , and None .
"tar" includes tar, tar.gz, and tar.bz files.
The default "auto" corresponds to ["tar", "zip"] .
None or an empty list will return no matches found.
|
cache_dir
|
Location to store cached files, when None it
defaults ether $KERAS_HOME if the KERAS_HOME environment
variable is set or ~/.keras/ .
|
force_download
|
If True , the file will always be re-downloaded
regardless of the cache state.
|
Returns |
Path to the downloaded file.
|
⚠️ Warning on malicious downloads ⚠️
Downloading something from the Internet carries a risk.
NEVER download a file/archive if you do not trust the source.
We recommend that you specify the file_hash
argument
(if the hash of the source file is known) to make sure that the file you
are getting is the one you expect.
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates. Some content is licensed under the numpy license.
Last updated 2024-06-07 UTC.
[null,null,["Last updated 2024-06-07 UTC."],[],[],null,["# tf.keras.utils.get_file\n\n|------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/keras-team/keras/tree/v3.3.3/keras/src/utils/file_utils.py#L130-L327) |\n\nDownloads a file from a URL if it not already in the cache. \n\n tf.keras.utils.get_file(\n fname=None,\n origin=None,\n untar=False,\n md5_hash=None,\n file_hash=None,\n cache_subdir='datasets',\n hash_algorithm='auto',\n extract=False,\n archive_format='auto',\n cache_dir=None,\n force_download=False\n )\n\n### Used in the notebooks\n\n| Used in the guide | Used in the tutorials |\n|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| - [tf.data: Build TensorFlow input pipelines](https://www.tensorflow.org/guide/data) - [Using the SavedModel format](https://www.tensorflow.org/guide/saved_model) - [Estimators](https://www.tensorflow.org/guide/estimator) - [Migrating your TFLite code to TF2](https://www.tensorflow.org/guide/migrate/tflite) - [Using Counterfactual Logit Pairing with Keras](https://www.tensorflow.org/responsible_ai/model_remediation/counterfactual/guide/counterfactual_keras) | - [Transfer learning with TensorFlow Hub](https://www.tensorflow.org/tutorials/images/transfer_learning_with_hub) - [Load CSV data](https://www.tensorflow.org/tutorials/load_data/csv) - [Transfer learning with YAMNet for environmental sound classification](https://www.tensorflow.org/tutorials/audio/transfer_learning_audio) - [Premade Estimators](https://www.tensorflow.org/tutorials/estimator/premade) - [Neural style transfer](https://www.tensorflow.org/tutorials/generative/style_transfer) |\n\nBy default the file at the url `origin` is downloaded to the\ncache_dir `~/.keras`, placed in the cache_subdir `datasets`,\nand given the filename `fname`. The final location of a file\n`example.txt` would therefore be `~/.keras/datasets/example.txt`.\nFiles in `.tar`, `.tar.gz`, `.tar.bz`, and `.zip` formats can\nalso be extracted.\n\nPassing a hash will verify the file after download. The command line\nprograms `shasum` and `sha256sum` can compute the hash.\n\n#### Example:\n\n path_to_downloaded_file = get_file(\n origin=\"https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz\",\n extract=True,\n )\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `fname` | Name of the file. If an absolute path, e.g. `\"/path/to/file.txt\"` is specified, the file will be saved at that location. If `None`, the name of the file at `origin` will be used. |\n| `origin` | Original URL of the file. |\n| `untar` | Deprecated in favor of `extract` argument. boolean, whether the file should be decompressed |\n| `md5_hash` | Deprecated in favor of `file_hash` argument. md5 hash of the file for verification |\n| `file_hash` | The expected hash string of the file after download. The sha256 and md5 hash algorithms are both supported. |\n| `cache_subdir` | Subdirectory under the Keras cache dir where the file is saved. If an absolute path, e.g. `\"/path/to/folder\"` is specified, the file will be saved at that location. |\n| `hash_algorithm` | Select the hash algorithm to verify the file. options are `\"md5'`, `\"sha256'`, and `\"auto'`. The default 'auto' detects the hash algorithm in use. |\n| `extract` | True tries extracting the file as an Archive, like tar or zip. |\n| `archive_format` | Archive format to try for extracting the file. Options are `\"auto'`, `\"tar'`, `\"zip'`, and `None`. `\"tar\"` includes tar, tar.gz, and tar.bz files. The default `\"auto\"` corresponds to `[\"tar\", \"zip\"]`. None or an empty list will return no matches found. |\n| `cache_dir` | Location to store cached files, when None it defaults ether `$KERAS_HOME` if the `KERAS_HOME` environment variable is set or `~/.keras/`. |\n| `force_download` | If `True`, the file will always be re-downloaded regardless of the cache state. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Path to the downloaded file. ||\n\n\u003cbr /\u003e\n\n**⚠️ Warning on malicious downloads ⚠️**\n\nDownloading something from the Internet carries a risk.\nNEVER download a file/archive if you do not trust the source.\nWe recommend that you specify the `file_hash` argument\n(if the hash of the source file is known) to make sure that the file you\nare getting is the one you expect."]]