-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log if dataset is already available in our GCS bucket (redux)
PiperOrigin-RevId: 241424282
- Loading branch information
Ryan Sepassi
authored and
Copybara-Service
committed
Apr 1, 2019
1 parent
592fdef
commit a81a42f
Showing
6 changed files
with
175 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# coding=utf-8 | ||
# Copyright 2019 The TensorFlow Datasets Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Utilities for accessing TFDS GCS buckets.""" | ||
|
||
import posixpath | ||
from xml.etree import ElementTree | ||
|
||
import requests | ||
import tensorflow as tf | ||
|
||
from tensorflow_datasets.core import utils | ||
|
||
GCS_URL = "http://storage.googleapis.com" | ||
|
||
# for dataset_info/ | ||
GCS_BUCKET = posixpath.join(GCS_URL, "tfds-data") | ||
GCS_DATASET_INFO_DIR = "dataset_info" | ||
GCS_DATASETS_DIR = "datasets" | ||
|
||
|
||
def download_gcs_file(path, out_fname=None): | ||
"""Download a file from GCS, optionally to a file.""" | ||
url = posixpath.join(GCS_BUCKET, path) | ||
stream = bool(out_fname) | ||
resp = requests.get(url, stream=stream) | ||
if not resp.ok: | ||
raise ValueError("GCS bucket inaccessible") | ||
if out_fname: | ||
with tf.io.gfile.GFile(out_fname, "wb") as f: | ||
for chunk in resp.iter_content(1024): | ||
f.write(chunk) | ||
else: | ||
return resp.content | ||
|
||
|
||
@utils.memoize() | ||
def gcs_files(): | ||
top_level_xml_str = download_gcs_file("") | ||
xml_root = ElementTree.fromstring(top_level_xml_str) | ||
filenames = [el[0].text for el in xml_root if el.tag.endswith("Contents")] | ||
return filenames | ||
|
||
|
||
def gcs_dataset_info_files(dataset_dir): | ||
"""Return paths to GCS files in the given dataset directory.""" | ||
prefix = posixpath.join(GCS_DATASET_INFO_DIR, dataset_dir, "") | ||
# Filter for this dataset | ||
filenames = [el for el in gcs_files() | ||
if el.startswith(prefix) and len(el) > len(prefix)] | ||
return filenames | ||
|
||
|
||
def is_gcs_dataset_accessible(dataset_dir): | ||
info_file = posixpath.join(GCS_DATASETS_DIR, dataset_dir, "dataset_info.json") | ||
return info_file in gcs_files() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# coding=utf-8 | ||
# Copyright 2019 The TensorFlow Datasets Authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""GCS utils test.""" | ||
|
||
from __future__ import absolute_import | ||
from __future__ import division | ||
from __future__ import print_function | ||
|
||
import tensorflow_datasets as tfds | ||
from tensorflow_datasets import testing | ||
from tensorflow_datasets.core.utils import gcs_utils | ||
|
||
|
||
class GcsUtilsTest(testing.TestCase): | ||
|
||
def is_dataset_accessible(self): | ||
# Re-enable GCS access. TestCase disables it. | ||
with self.gcs_access(): | ||
self.assertTrue(gcs_utils.is_gcs_dataset_accessible("mnist/1.0.0")) | ||
|
||
def test_mnist(self): | ||
with self.gcs_access(): | ||
mnist = tfds.image.MNIST(data_dir="gs://tfds-data/datasets") | ||
example = next(tfds.as_numpy(mnist.as_dataset(split="train").take(1))) | ||
_ = example["image"], example["label"] | ||
|
||
|
||
if __name__ == "__main__": | ||
testing.test_main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters