your programing

TensorFlow에서 CSV 데이터를 * 실제로 * 읽는 방법은 무엇입니까?

lovepro 2020. 10. 8. 08:23
반응형

TensorFlow에서 CSV 데이터를 * 실제로 * 읽는 방법은 무엇입니까?


저는 TensorFlow의 세계에 비교적 익숙하지 않으며 실제로 CSV 데이터를 TensorFlow에서 사용 가능한 예제 / 라벨 텐서로 읽어 들이는 방식에 상당히 당황합니다 . CSV 데이터 읽기에 대한 TensorFlow 자습서 의 예제 는 매우 조각화되어 있으며 CSV 데이터에 대해 학습 할 수있는 방법의 일부일뿐입니다.

다음은 CSV 튜토리얼을 기반으로 한 코드입니다.

from __future__ import print_function
import tensorflow as tf

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

filename = "csv_test_data.csv"

# setup text reader
file_length = file_len(filename)
filename_queue = tf.train.string_input_producer([filename])
reader = tf.TextLineReader(skip_header_lines=1)
_, csv_row = reader.read(filename_queue)

# setup CSV decoding
record_defaults = [[0],[0],[0],[0],[0]]
col1,col2,col3,col4,col5 = tf.decode_csv(csv_row, record_defaults=record_defaults)

# turn features back into a tensor
features = tf.stack([col1,col2,col3,col4])

print("loading, " + str(file_length) + " line(s)\n")
with tf.Session() as sess:
  tf.initialize_all_variables().run()

  # start populating filename queue
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  for i in range(file_length):
    # retrieve a single instance
    example, label = sess.run([features, col5])
    print(example, label)

  coord.request_stop()
  coord.join(threads)
  print("\ndone loading")

다음은 내가로드하는 CSV 파일의 간단한 예입니다. 기본 데이터-4 개의 기능 열과 1 개의 레이블 열 :

0,0,0,0,0
0,15,0,0,0
0,30,0,0,0
0,45,0,0,0

위의 코드는 모두 CSV 파일에서 각 예제를 하나씩 인쇄하는 것입니다 . 이것은 훌륭하지만 훈련에는 쓸모가 없습니다.

여기서 제가 고민하고있는 것은 실제로 하나씩로드 된 개별 예제를 학습 데이터 세트로 바꾸는 방법입니다. 예를 들어, 여기 Udacity Deep Learning 과정에서 작업 하던 노트북 이 있습니다. 기본적으로로드중인 CSV 데이터를 가져 와서 train_datasettrain_labels같은 것으로 만들고 싶습니다 .

def reformat(dataset, labels):
  dataset = dataset.reshape((-1, image_size * image_size)).astype(np.float32)
  # Map 2 to [0.0, 1.0, 0.0 ...], 3 to [0.0, 0.0, 1.0 ...]
  labels = (np.arange(num_labels) == labels[:,None]).astype(np.float32)
  return dataset, labels
train_dataset, train_labels = reformat(train_dataset, train_labels)
valid_dataset, valid_labels = reformat(valid_dataset, valid_labels)
test_dataset, test_labels = reformat(test_dataset, test_labels)
print('Training set', train_dataset.shape, train_labels.shape)
print('Validation set', valid_dataset.shape, valid_labels.shape)
print('Test set', test_dataset.shape, test_labels.shape)

나는 이와 같이을 사용해 보았지만 tf.train.shuffle_batch설명 할 수없이 중단됩니다.

  for i in range(file_length):
    # retrieve a single instance
    example, label = sess.run([features, colRelevant])
    example_batch, label_batch = tf.train.shuffle_batch([example, label], batch_size=file_length, capacity=file_length, min_after_dequeue=10000)
    print(example, label)

요약하면 다음과 같은 질문이 있습니다.

  • 이 프로세스에서 내가 놓친 것은 무엇입니까?
    • 입력 파이프 라인을 제대로 구축하는 방법에 대해 제가 놓친 몇 가지 핵심 직관이있는 것 같습니다.
  • CSV 파일의 길이를 알 필요가없는 방법이 있습니까?
    • 처리 할 줄 수 ( for i in range(file_length)위의 코드 줄) 를 알아야하는 것은 매우 우아하지 않습니다.

편집 : Yaroslav가 내가 여기서 명령 및 그래프 구성 부분을 혼합 할 가능성이 있다고 지적하자마자 명확 해지기 시작했습니다. 다음 코드를 모아서 CSV에서 모델을 학습 할 때 일반적으로 수행되는 작업에 더 가깝다고 생각합니다 (모델 학습 코드 제외).

from __future__ import print_function
import numpy as np
import tensorflow as tf
import math as math
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('dataset')
args = parser.parse_args()

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

def read_from_csv(filename_queue):
  reader = tf.TextLineReader(skip_header_lines=1)
  _, csv_row = reader.read(filename_queue)
  record_defaults = [[0],[0],[0],[0],[0]]
  colHour,colQuarter,colAction,colUser,colLabel = tf.decode_csv(csv_row, record_defaults=record_defaults)
  features = tf.stack([colHour,colQuarter,colAction,colUser])  
  label = tf.stack([colLabel])  
  return features, label

def input_pipeline(batch_size, num_epochs=None):
  filename_queue = tf.train.string_input_producer([args.dataset], num_epochs=num_epochs, shuffle=True)  
  example, label = read_from_csv(filename_queue)
  min_after_dequeue = 10000
  capacity = min_after_dequeue + 3 * batch_size
  example_batch, label_batch = tf.train.shuffle_batch(
      [example, label], batch_size=batch_size, capacity=capacity,
      min_after_dequeue=min_after_dequeue)
  return example_batch, label_batch

file_length = file_len(args.dataset) - 1
examples, labels = input_pipeline(file_length, 1)

with tf.Session() as sess:
  tf.initialize_all_variables().run()

  # start populating filename queue
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)

  try:
    while not coord.should_stop():
      example_batch, label_batch = sess.run([examples, labels])
      print(example_batch)
  except tf.errors.OutOfRangeError:
    print('Done training, epoch reached')
  finally:
    coord.request_stop()

  coord.join(threads) 

여기서 명령형과 그래프 구성 부분을 혼합하고 있다고 생각합니다. 이 작업 tf.train.shuffle_batch은 새 대기열 노드를 만들고 단일 노드를 사용하여 전체 데이터 세트를 처리 할 수 ​​있습니다. 그래서 나는 당신이 shuffle_batchfor 루프에 많은 를 만들고 큐 러너를 시작하지 않았기 때문에 매달려 있다고 생각 합니다.

일반적인 입력 파이프 라인 사용은 다음과 같습니다.

  1. shuffle_batch입력 파이프 라인 과 같은 노드 추가
  2. (선택 사항, 의도하지 않은 그래프 수정을 방지하기 위해) 그래프 완성

--- 그래프 구성 종료, 명령형 프로그래밍 시작-

  1. tf.start_queue_runners
  2. while(True): session.run()

확장 성을 높이기 위해 (Python GIL 방지) TensorFlow 파이프 라인을 사용하여 모든 데이터를 생성 할 수 있습니다. 그러나 성능이 중요하지 않은 경우 slice_input_producer.다음 을 사용하여 입력 파이프 라인에 numpy 배열을 연결할 수 있습니다 Print( Print노드가 실행될 때 표준 출력 으로 이동하는 메시지 ).

tf.reset_default_graph()

num_examples = 5
num_features = 2
data = np.reshape(np.arange(num_examples*num_features), (num_examples, num_features))
print data

(data_node,) = tf.slice_input_producer([tf.constant(data)], num_epochs=1, shuffle=False)
data_node_debug = tf.Print(data_node, [data_node], "Dequeueing from data_node ")
data_batch = tf.batch([data_node_debug], batch_size=2)
data_batch_debug = tf.Print(data_batch, [data_batch], "Dequeueing from data_batch ")

sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
tf.get_default_graph().finalize()
tf.start_queue_runners()

try:
  while True:
    print sess.run(data_batch_debug)
except tf.errors.OutOfRangeError as e:
  print "No more inputs."

다음과 같은 것이 보일 것입니다.

[[0 1]
 [2 3]
 [4 5]
 [6 7]
 [8 9]]
[[0 1]
 [2 3]]
[[4 5]
 [6 7]]
No more inputs.

"8, 9"숫자는 전체 배치를 채우지 않았기 때문에 생산되지 않았습니다. 또한 tf.Printsys.stdout에 인쇄되므로 터미널에 별도로 표시됩니다.

PS: a minimal of connecting batch to a manually initialized queue is in github issue 2193

Also, for debugging purposes you might want to set timeout on your session so that your IPython notebook doesn't hang on empty queue dequeues. I use this helper function for my sessions

def create_session():
  config = tf.ConfigProto(log_device_placement=True)
  config.gpu_options.per_process_gpu_memory_fraction=0.3 # don't hog all vRAM
  config.operation_timeout_in_ms=60000   # terminate on long hangs
  # create interactive session to register a default session
  sess = tf.InteractiveSession("", config=config)
  return sess

Scalability Notes:

  1. tf.constant inlines copy of your data into the Graph. There's a fundamental limit of 2GB on size of Graph definition so that's an upper limit on size of data
  2. You could get around that limit by using v=tf.Variable and saving the data into there by running v.assign_op with a tf.placeholder on right-hand side and feeding numpy array to the placeholder (feed_dict)
  3. That still creates two copies of data, so to save memory you could make your own version of slice_input_producer which operates on numpy arrays, and uploads rows one at a time using feed_dict

Or you could try this, the code loads the Iris dataset into tensorflow using pandas and numpy and a simple one neuron output is printed in the session. Hope it helps for a basic understanding.... [ I havent added the way of one hot decoding labels].

import tensorflow as tf 
import numpy
import pandas as pd
df=pd.read_csv('/home/nagarjun/Desktop/Iris.csv',usecols = [0,1,2,3,4],skiprows = [0],header=None)
d = df.values
l = pd.read_csv('/home/nagarjun/Desktop/Iris.csv',usecols = [5] ,header=None)
labels = l.values
data = numpy.float32(d)
labels = numpy.array(l,'str')
#print data, labels

#tensorflow
x = tf.placeholder(tf.float32,shape=(150,5))
x = data
w = tf.random_normal([100,150],mean=0.0, stddev=1.0, dtype=tf.float32)
y = tf.nn.softmax(tf.matmul(w,x))

with tf.Session() as sess:
    print sess.run(y)

You can use latest tf.data API :

dataset = tf.contrib.data.make_csv_dataset(filepath)
iterator = dataset.make_initializable_iterator()
columns = iterator.get_next()
with tf.Session() as sess:
   sess.run([iteator.initializer])

If anyone came here searching for a simple way to read absolutely large and sharded CSV files in tf.estimator API then , please see below my code

CSV_COLUMNS = ['ID','text','class']
LABEL_COLUMN = 'class'
DEFAULTS = [['x'],['no'],[0]]  #Default values

def read_dataset(filename, mode, batch_size = 512):
    def _input_fn(v_test=False):
#         def decode_csv(value_column):
#             columns = tf.decode_csv(value_column, record_defaults = DEFAULTS)
#             features = dict(zip(CSV_COLUMNS, columns))
#             label = features.pop(LABEL_COLUMN)
#             return add_engineered(features), label

        # Create list of files that match pattern
        file_list = tf.gfile.Glob(filename)

        # Create dataset from file list
        #dataset = tf.data.TextLineDataset(file_list).map(decode_csv)
        dataset = tf.contrib.data.make_csv_dataset(file_list,
                                                   batch_size=batch_size,
                                                   column_names=CSV_COLUMNS,
                                                   column_defaults=DEFAULTS,
                                                   label_name=LABEL_COLUMN)

        if mode == tf.estimator.ModeKeys.TRAIN:
            num_epochs = None # indefinitely
            dataset = dataset.shuffle(buffer_size = 10 * batch_size)
        else:
            num_epochs = 1 # end-of-input after this

        batch_features, batch_labels = dataset.make_one_shot_iterator().get_next()

        #Begins - Uncomment for testing only -----------------------------------------------------<
        if v_test == True:
            with tf.Session() as sess:
                print(sess.run(batch_features))
        #End - Uncomment for testing only -----------------------------------------------------<
        return add_engineered(batch_features), batch_labels
    return _input_fn

Example usage in TF.estimator:

train_spec = tf.estimator.TrainSpec(input_fn = read_dataset(
                                                filename = train_file,
                                                mode = tf.estimator.ModeKeys.TRAIN,
                                                batch_size = 128), 
                                      max_steps = num_train_steps)

참고URL : https://stackoverflow.com/questions/37091899/how-to-actually-read-csv-data-in-tensorflow

반응형