'How to load data from a downloaded tar.gz file in tensorflow/keras?

Tensorflow datasets or tfds automatically starts downloading the data I want. I have cifar10 downloaded in my system. I can directly load the data in pytorch using: torchvision.datasets.CIFAR10('path/to/directory',...,download=False)

Is there a tensorflow or keras equivalent of this?



Solution 1:[1]

I think that the best you can do is first extract the tar file:

import tarfile

if fname.endswith("tar.gz"):
    tar = tarfile.open(fname, "r:gz")
    tar.extractall()
    tar.close()
elif fname.endswith("tar"):
    tar = tarfile.open(fname, "r:")
    tar.extractall()
    tar.close()

and then access to the model data and load it using keras:

https://www.tensorflow.org/api_docs/python/tf/keras/models/load_model

Solution 2:[2]

Posting another way to load the file from local for whoever finds it later.

In the URL parameter, you supply the local URL of the file with the path. For example, I have a file in my D drive in the folder Workspace/DataFiles/tldr.gz, then the value that I give for the URL parameter would be something like this.

path = 'file:///D:/Workspace/DataFiles/tldr.gz'

path_to_downloaded_file = tf.keras.utils.get_file("tldr_data",path, archive_format='tar', untar=True)`

This way keras recognizes the URL and loads the data from the file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cristian Zumelzu
Solution 2 Shanmukha Sampath Kumar