MLOps

tf.Example과 TFRecord

xyz1 2022. 7. 27. 09:07

TFX에서 ExampleGen에 대해 공부하다가 tf.Example 과 TFRecord 개념이 나타난다.

ExampleGen의 그림을 다시 한번 보도록 하자.

[그림1. ExampleGen input/output ]

 

input으로는 TFRecord가 들어가고, output으로는 tf.Example record가 return 된다.

그러면 TFRecord가 무엇이고 tf.Example record가 무엇인지 알아보도록 하자.

 

TFRecord

TFRecord는 일련의 레코드를 가지고 있다.
각 레코드는 byte-string이다.

다시 말하면, TFRecord에는 byte string으로 serialized 된 데이터가 저장되어있다.

 

tf.Example(= tf.train.Example)

이름에서 예상할 수 있듯이, 보통 클래스 네임은 대문자로 시작한다.
즉, Example은 class 이며 그저 방법론이다.


tf.Example은 데이터를 byte-strings로 serializing하는 방법론이다.

Serializing 방법론의 구체적인 설명은 링크에 설명되어있다.

요약하면, tf.train.Example은 데이터를 protobuffers를 사용하여 de/serialize 하기 위한 방법론이다.

따라서, TFRecord를 tf.train.Example.ParseFromString 를 통해서 parsing이 가능하다.

 

간단한 tf.train.Example 만들기 예제를 통해 개념을 알아보자.


아래와 같은 동일한 크기의 데이터가 있다고 가정하자.
feature0 = False  (Boolean)
feature1 =4 (Integer)
feature2 = b'goat' (String)
feature3 = 0.9876 (Float)

 

결론

TFRecord는  byte-string으로 된 record가 저장된 파일이다.
tf.Example(tf.train.Example)은 train과 inference에 관련된 데이터를 example.proto 형식에 저장하는 메서드이다.



Appendix

이 시점에서 [그림1을] 다시 보면  ExampleGen의 output은 tf.Example record이다.
tf.Example record는 무엇일까?
tf.train.Example message(record)는 단순히 Feature message를 wrap(?)한 것이다.
Feature message는 tf.train.Features를 통해서 나온 값이다.
tf.train.Features는 example_pb2.Example ~~~ ~~

유추하자면.. tf.Example record는 example.proto에 맞게 끔 format되어 데이터다.
ex) tf.Example record 예시