-
Notifications
You must be signed in to change notification settings - Fork 729
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
read parquet dataset as tf.data.Dataset #3956
Merged
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
32d045e
read parquet dataset as tf.data.Dataset
Le-Zheng 4a3fff7
update
Le-Zheng ab6d9aa
move pa_path to utils
Le-Zheng 9d93f0c
add train images ut
Le-Zheng 94b3a89
python style check
Le-Zheng a647cee
python style check
Le-Zheng d6c92f9
Merge branch 'master' into parquettfdata
Le-Zheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# | ||
# Copyright 2018 Analytics Zoo 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. | ||
# | ||
|
||
import tempfile | ||
import shutil | ||
|
||
import pytest | ||
from unittest import TestCase | ||
import os | ||
from zoo.orca.data.image.parquet_dataset import ParquetDataset, read_parquet | ||
from zoo.orca.data.image.utils import DType, FeatureType, SchemaField | ||
import tensorflow as tf | ||
|
||
resource_path = os.path.join(os.path.split(__file__)[0], "../../resources") | ||
|
||
|
||
class TestReadParquet(TestCase): | ||
def test_read_parquet_images_tf_dataset(self): | ||
temp_dir = tempfile.mkdtemp() | ||
|
||
def generator(): | ||
dataset_path = os.path.join(resource_path, "cat_dog") | ||
for root, dirs, files in os.walk(os.path.join(dataset_path, "cats")): | ||
for name in files: | ||
image_path = os.path.join(root, name) | ||
yield {"image": image_path, "label": 1, "id": image_path} | ||
|
||
for root, dirs, files in os.walk(os.path.join(dataset_path, "dogs")): | ||
for name in files: | ||
image_path = os.path.join(root, name) | ||
yield {"image": image_path, "label": 0, "id": image_path} | ||
|
||
schema = { | ||
"image": SchemaField(feature_type=FeatureType.IMAGE, dtype=DType.FLOAT32, shape=()), | ||
"label": SchemaField(feature_type=FeatureType.SCALAR, dtype=DType.FLOAT32, shape=()), | ||
"id": SchemaField(feature_type=FeatureType.SCALAR, dtype=DType.STRING, shape=()) | ||
} | ||
|
||
try: | ||
ParquetDataset.write("file://" + temp_dir, generator(), schema) | ||
path = "file://" + temp_dir | ||
output_types = {"id": tf.string, "image": tf.string, "label": tf.float32} | ||
dataset = read_parquet("tf_dataset", input_path=path, | ||
output_types=output_types) | ||
for dt in dataset: | ||
print(dt.keys()) | ||
|
||
finally: | ||
shutil.rmtree(temp_dir) | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
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 |
---|---|---|
|
@@ -15,9 +15,11 @@ | |
# | ||
|
||
import copy | ||
import os | ||
from collections import namedtuple | ||
from io import BytesIO | ||
import numpy as np | ||
from zoo.orca.data.file import open_text | ||
from itertools import chain, islice | ||
|
||
from enum import Enum | ||
|
@@ -146,6 +148,16 @@ def dict_to_row(schema, row_dict): | |
return pyspark.Row(**row) | ||
|
||
|
||
def decode_feature_type_ndarray(path, df): | ||
schema_path = os.path.join(path, "_orca_metadata") | ||
j_str = open_text(schema_path)[0] | ||
schema = decode_schema(j_str) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe you should decode the schema only once and pass the decoced schema to this function. |
||
for n, field in schema.items(): | ||
if field.feature_type == FeatureType.NDARRAY: | ||
df[n] = df[n].map(lambda k: decode_ndarray(k)) | ||
return df | ||
|
||
|
||
def chunks(iterable, size=10): | ||
iterator = iter(iterable) | ||
for first in iterator: | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
move this function to
utils