ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • tf.data, TFRecord
    라이브러리/Tensorflow keras 2022. 1. 5. 00:32

    tf.data

    https://www.tensorflow.org/guide/data

     

    The tf.data API enables you to build complex input pipelines from simple, reusable pieces. For example, the pipeline for an image model might aggregate data from files in a distributed file system, apply random perturbations to each image, and merge randomly selected images into a batch for training. The pipeline for a text model might involve extracting symbols from raw text data, converting them to embedding identifiers with a lookup table, and batching together sequences of different lengths. The tf.data API makes it possible to handle large amounts of data, read from different data formats, and perform complex transformations.

    The tf.data API introduces a tf.data.Dataset abstraction that represents a sequence of elements, in which each element consists of one or more components. For example, in an image pipeline, an element might be a single training example, with a pair of tensor components representing the image and its label.

     


    tf.data를 사용하게 되면 효율적으로 복잡한 input pipelines을 짤 수 있기에 반드시 고려해야하는 사항이라 생각이든다.

    특히, https://www.tensorflow.org/guide/data_performance에선 optimize할 경우 매우 빠르게 데이터를 읽고 처리 할 수 있음을 알 수 있다. 

     

     

     

     


    TFRecord

    https://www.tensorflow.org/tutorials/load_data/tfrecord#write_the_tfrecord_file

     

     

    TFReocrd의 Note를 보면 다음과 같은 문구가 있다.

    Note: While useful, these structures are optional. There is no need to convert existing code to use TFRecords, unless you are using tf.data and reading data is still the bottleneck to training. You can refer to Better performance with the tf.data API for dataset performance tips.

     

    즉, 기존에 tf.data를 사용하고 있는 경우 굳이 TFRecord를 사용하지 않고, bottlenet이 있는 경우 사용하라고 권장한다.

     

    앞으로도 많은 문제들은 수많은 데이터 (그에 따라 많은 용량을 요구)와 잘못된 레이블들이 산재해있을 것이다. 그에 대해 tfrecord로 작성해두면 조금더 효율적으로 문제를 풀 수 있지 않을까 생각한다.

     

     


    내가 현재 겪는 문제는 다음과 같다.

    1. (21. 3월~10월)

    • 목적: 3D Keypoint Detection
    • 데이터: 3D 영상 데이터 (dcm), 좌표값 (txt) 
    • 수량: 500 cases (1 case 당 약 200MB) 
    • 데이터 수량 자체는 그렇게 많지 않지만, 각 데이터는 큰 용량을 차지하다보니 데이터를 읽어오고 처리하는데 많은 시간이 걸렸다. 사용하였던 서버가 1TB 메모리를 가지고 있기에 단순히 모든 연산(로드된 dicom, 중간 temp numpy array 등등)메모리에 적재하고 사용하였으나, free -h를 통해 메모리를 확인하면 100GB, 200GB 씩 차지함을 볼 수 있었다. 
    • 시간이 오래 걸리는 문제는 중간에 처리된 데이터는 pkl로 저장하여, 차후에 다시 불러오는 식으로 해결하였으나, 근본적인 문제는 해결되지 않은 것으로 생각된다. (tensorflow의 pipeline과 어울리는 방식이 아니라 판단). 그리고 로드하는데도 많은 시간이 소요하였다.
    • 게다가 배치가 조금만 크면 메모리 펑

     

     

    2. (21. 10월~12월)

    • 목적: 2D Image Segmentation
    • 데이터: 2D 영상 데이터 (dcm), mask (png), 메타 데이터 (json)
    • 수량: 10,000/20,000건 (2개의 타겟을 Seg.해야 했으며 각각 학습시켰으면 됐기에 따로 기입)
    • 1) 데이터 수량 자체가 많았으며,  2) 잘못된 mask가 있을 수 있으므로 받은 데이터를 그대로 쓰는 것이 아니라 짝을 지었어야했으며, 3) 그김에 메타 정보도 같이 갖고 있으면 좋겠다.
    • 또한, 데이터가 많다보니 전처리를 수행하는데 많은 시간이 소요되었으며 2개의 타겟에 대해 코드를 작성하다보니 날림이 되어버렸다. (최종적으로 같은 방법을 통해 학습시켰으나 코드가 각각 달라지는 문제.. 이건 내 잘못)

     

    위와 같은 문제는 앞으로도 계속 겪을 문제로, 위와 같은 방법을 반드시 익혀서 위에 이미 작성하였던 코드를 수정해야할 필요가 있다고 생각된다.

    '라이브러리 > Tensorflow keras' 카테고리의 다른 글

    TF Keras 학습 속도 줄이기 (30%)  (0) 2022.04.26
    Model vs Estimator  (0) 2022.01.03

    댓글

Designed by Tistory.