Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
tensorflow
GitHub Repository: tensorflow/docs-l10n
Path: blob/master/site/ko/datasets/overview.ipynb
25115 views
Kernel: Python 3

TensorFlow Datasets

TFDS는 TensorFlow, Jax 및 기타 머신러닝 프레임워크에서 바로 사용할 수 있는 데이터세트를 제공합니다.

결정적으로 데이터를 다운로드, 준비, tf.data.Dataset 또는 np.array)을 구성합니다.

참고: TFDS(이 라이브러리)를 tf.data(효율적인 데이터 파이프라인을 구축하기 위한 TensorFlow API)와 혼동하지 마세요. TFDS는 tf.data를 둘러싼 상위 수준의 래퍼입니다. 이 API에 익숙하지 않으면, 먼저 공식 tf.data 가이드를 읽어 보세요.

Copyright 2018 The TensorFlow Datasets Authors, Licensed under the Apache License, Version 2.0

설치

TFDS는 두 가지 패키지로 존재합니다.

  • pip install tensorflow-datasets: 안정적인 버전으로, 몇 개월마다 릴리스됩니다.

  • pip install tfds-nightly: 매일 릴리스되며, 데이터세트의 마지막 버전이 포함됩니다.

이 colab은 tfds-nightly를 사용합니다.

!pip install -q tfds-nightly tensorflow matplotlib
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf import tensorflow_datasets as tfds

사용 가능한 데이터세트 찾기

모든 데이터세트 빌더는 tfds.core.DatasetBuilder의 서브 클래스입니다. 사용 가능한 빌더의 목록을 얻으려면, tfds.list_builders()를 사용하거나 카탈로그를 살펴보세요.

tfds.list_builders()

데이터세트 로드하기

tfds.load

데이터세트를 로드하는 가장 쉬운 방법은 tfds.load입니다.

  1. 데이터를 다운로드하여 tfrecord 파일로 저장합니다.

  2. tfrecord를 로드하고 tf.data.Dataset를 생성합니다.

ds = tfds.load('mnist', split='train', shuffle_files=True) assert isinstance(ds, tf.data.Dataset) print(ds)

몇 가지 일반적인 인수:

  • split=: 읽을 분할(예: 'train', ['train', 'test'], 'train[80%:]',...). 분할 API 가이드를 참조하세요.

  • shuffle_files=: 각 epoch 간에 파일을 셔플할지 여부를 제어합니다(TFDS는 큰 데이터세트를 여러 개의 작은 파일에 저장합니다).

  • data_dir=: 데이터세트가 저장된 위치(기본값은 ~/tensorflow_datasets/)

  • with_info=True: 데이터세트 메타 데이터를 포함하는 tfds.core.DatasetInfo를 반환합니다.

  • download=False: 다운로드를 비활성화합니다.

tfds.builder

tfds.loadtfds.core.DatasetBuilder를 둘러싼 얇은 래퍼입니다. tfds.core.DatasetBuilder API를 사용하여 같은 출력을 얻을 수 있습니다.

builder = tfds.builder('mnist') # 1. Create the tfrecord files (no-op if already exists) builder.download_and_prepare() # 2. Load the `tf.data.Dataset` ds = builder.as_dataset(split='train', shuffle_files=True) print(ds)

tfds build CLI

특정 데이터 세트를 생성하려는 경우tfds 명령 줄을 사용할 수 있습니다. 예시:

tfds build mnist

사용 가능한 플래그 는 문서 를 참조하십시오.

데이터세트 반복하기

dict

기본적으로 tf.data.Dataset 객체에는 tf.Tensordict가 포함됩니다.

ds = tfds.load('mnist', split='train') ds = ds.take(1) # Only take a single example for example in ds: # example is `{'image': tf.Tensor, 'label': tf.Tensor}` print(list(example.keys())) image = example["image"] label = example["label"] print(image.shape, label)

dict 키 이름과 구조를 찾으려면 카탈로그 의 데이터세트 설명서를 참조하세요(예: mnist 문서).

튜플로(as_supervised=True)

as_supervised=True를 사용하면 감독된 데이터세트 대신 튜플 (features, label)을 얻을 수 있습니다.

ds = tfds.load('mnist', split='train', as_supervised=True) ds = ds.take(1) for image, label in ds: # example is (image, label) print(image.shape, label)

numpy로(tfds.as_numpy)

tfds.as_numpy를 사용하여 변환합니다.

  • tf.Tensor > np.array

  • tf.data.Dataset -> Iterator[Tree[np.array]](Tree는 임의의 중첩된 Dict, Tuple일 수 있음)

ds = tfds.load('mnist', split='train', as_supervised=True) ds = ds.take(1) for image, label in tfds.as_numpy(ds): print(type(image), type(label), label)

일괄 처리된 tf.Tensor로(batch_size=-1)

batch_size=-1을 사용하여 전체 데이터세트를 단일 배치로 로드할 수 있습니다.

as_supervised=Truetfds.as_numpy와 결합하여 데이터를 (np.array, np.array)로 가져올 수 있습니다.

image, label = tfds.as_numpy(tfds.load( 'mnist', split='test', batch_size=-1, as_supervised=True, )) print(type(image), image.shape)

데이터세트가 메모리에 저장하기 적합하고, 모든 예제의 형상이 같습니다.

데이터세트 벤치마킹

데이터세트를 벤치마킹하려면 모든 iterable(예: tf.data.Dataset, tfds.as_numpy,...}에서 간단히 tfds.benchmark를 호출하면 됩니다.

ds = tfds.load('mnist', split='train') ds = ds.batch(32).prefetch(1) tfds.benchmark(ds, batch_size=32) tfds.benchmark(ds, batch_size=32) # Second epoch much faster due to auto-caching
  • batch_size= kwarg를 사용하여 배치 크기별로 결과를 정규화하는 것을 잊지 마세요.

  • 요약하면, tf.data.Dataset 추가 설정 시간(예: 버퍼 초기화 등)을 캡처하기 위해 첫 번째 웜업 배치가 다른 배치와 분리됩니다.

  • TFDS 자동 캐싱으로 인해 두 번째 반복이 훨씬 더 빨라진 것을 확인하세요.

  • tfds.benchmark는 추가 분석을 위해 검사할 수 있는 tfds.core.BenchmarkResult를 반환합니다.

엔드 투 엔드 파이프라인 빌드하기

더 진행하려면, 다음을 살펴볼 수 있습니다.

  • 전체 훈련 파이프라인(배치 처리, 셔플링 등)을 확인하는 엔드 투 엔드 Keras 예제

  • 파이프라인 속도 향상을 위한 성능 가이드(팁: tfds.benchmark(ds)를 사용하여 데이터세트 벤치마킹).

시각화

tfds.as_dataframe

tf.data.Dataset 객체는 Colab에서 시각화할 tfds.as_dataframe과 함께 pandas.DataFrame으로 변환할 수 있습니다.

  • 이미지, 오디오, 텍스트, 동영상을 시각화하기 위해 tfds.core.DatasetInfotfds.as_dataframe의 두 번째 인수로 추가합니다.

  • ds.take(x) 를 사용하여 처음 x 예제 만 표시합니다. pandas.DataFrame 은 메모리 내 전체 데이터세트를 로드하며 표시하는 데 비용이 많이들 수 있습니다.

ds, info = tfds.load('mnist', split='train', with_info=True) tfds.as_dataframe(ds.take(4), info)

tfds.show_examples

tfds.show_examplesmatplotlib.figure.Figure를 반환합니다(현재 지원되는 이미지 데이터세트만).

ds, info = tfds.load('mnist', split='train', with_info=True) fig = tfds.show_examples(ds, info)

데이터세트 메타 데이터에 액세스하기

모든 빌더에는 데이터세트 메타 데이터를 포함하는 tfds.core.DatasetInfo 객체가 포함됩니다.

다음을 통해 액세스할 수 있습니다.

  • tfds.load API:

ds, info = tfds.load('mnist', with_info=True)
  • tfds.core.DatasetBuilder API:

builder = tfds.builder('mnist') info = builder.info

데이터세트 정보에는 데이터세트에 대한 추가 정보(버전, 인용, 홈페이지, 설명 등)가 포함됩니다.

print(info)

특성 메타 데이터(레이블 이름, 이미지 형상 등)

tfds.features.FeatureDict에 액세스합니다.

info.features

클래스 수, 레이블 이름:

print(info.features["label"].num_classes) print(info.features["label"].names) print(info.features["label"].int2str(7)) # Human readable version (8 -> 'cat') print(info.features["label"].str2int('7'))

형상, dtype:

print(info.features.shape) print(info.features.dtype) print(info.features['image'].shape) print(info.features['image'].dtype)

분할 메타 데이터(예: 분할 이름, 예제 수 등)

tfds.core.SplitDict에 액세스합니다.

print(info.splits)

사용 가능한 분할:

print(list(info.splits.keys()))

개별 분할에 대한 정보를 얻습니다.

print(info.splits['train'].num_examples) print(info.splits['train'].filenames) print(info.splits['train'].num_shards)

하위 분할 API와도 동작합니다.

print(info.splits['train[15%:75%]'].num_examples) print(info.splits['train[15%:75%]'].file_instructions)

문제 해결

수동 다운로드 (다운로드 실패시)

어떤 이유로 다운로드가 실패한 경우 (예 : 오프라인 등). 언제든지 수동으로 데이터를 직접 다운로드하여 manual_dir 배치 할 수 있습니다 (기본값 : ~/tensorflow_datasets/download/manual/ .

다운로드 할 URL을 찾으려면 다음을 살펴보십시오.

NonMatchingChecksumError 수정

TFDS는 다운로드 된 URL의 체크섬을 확인하여 결정 성을 보장합니다. NonMatchingChecksumError 가 발생하면 다음을 나타낼 수 있습니다.

  • 웹 사이트가 다운되었을 수 있습니다 (예 : 503 status code ). URL을 확인하세요.

  • Google 드라이브 URL의 경우 너무 많은 사용자가 동일한 URL에 액세스하면 드라이브가 다운로드를 거부하는 경우가 있으므로 나중에 다시 시도하세요. 버그 보기

  • 원래 데이터 세트 파일이 업데이트되었을 수 있습니다. 이 경우 TFDS 데이터 세트 빌더를 업데이트해야합니다. 새로운 Github 문제 또는 PR을여십시오.

    • tfds build --register_checksums 새 체크섬 등록

    • 결국 데이터 세트 생성 코드를 업데이트합니다.

    • 데이터 세트 VERSION 업데이트

    • 데이터 셋 업데이트 RELEASE_NOTES : 체크섬이 변경된 원인은 무엇입니까? 몇 가지 예가 변경 되었습니까?

    • 데이터 세트를 계속 빌드 할 수 있는지 확인합니다.

    • PR 보내기

참고 : ~/tensorflow_datasets/download/ 에서 다운로드 한 파일을 검사 할 수도 있습니다.

인용

논문에 tensorflow-datasets 사용하는 경우, 사용 된 데이터 세트에 특정한 인용 ( 데이터 세트 카탈로그 에서 찾을 수 있음)과 함께 다음 인용을 포함하십시오.

@misc{TFDS, title = { {TensorFlow Datasets}, A collection of ready-to-use datasets}, howpublished = {\url{https://www.tensorflow.org/datasets}}, }