1

I've got the below function, mostly from this question. That person is trying to read five columns that are all ints. I'm trying to read two columns: one an image file path and one an int. So I need to open the file and convert it into a tensor.

So my question is: how do I read the file and convert it into the necessary tensor?

I've tried quite a few different things like reading the file and converting that string to a tensor using .

def read_from_csv(filename_queue):
    reader = tf.TextLineReader()
    _, csv_row = reader.read(filename_queue)
    record_defaults = [[""],[0]]
    image_path, label = tf.decode_csv(csv_row, field_delim=" ", record_defaults=record_defaults)

    # here's where I need to read the file somehow...
    image = tf.read_file(image_path) # I probably need this somewhere
    print(image) # Tensor("DecodeJpeg:0", shape=(?, ?, 3), dtype=uint8)
    image = tf.image.decode_jpeg(image, channels=3)
    return image, label

I also tried using numpy (can't recall exactly what) but that didn't work either.

Community
  • 1
  • 1
JohnAllen
  • 7,317
  • 9
  • 41
  • 65

1 Answers1

0

Ok, here's what I was missing:

def read_from_csv(filename_queue):
    reader = tf.TextLineReader()
    key, value = reader.read(filename_queue)
    record_defaults = [[""],[0]]
    image_path, label = tf.decode_csv(value, field_delim=" ", record_defaults=record_defaults)
    #following line contains the important change...
    image = tf.image.decode_jpeg(tf.read_file(image_path), channels=3)
    return image, label

def input_pipeline(batch_size, num_epochs=None):
    filename_queue = tf.train.string_input_producer(["./28_dense.csv"], num_epochs=num_epochs, shuffle=True)
    image, label = read_from_csv(filename_queue)
    image = tf.reshape(image, [28,28,3])
    min_after_dequeue = 5
    capacity = min_after_dequeue + 3 * batch_size
    image_batch, label_batch = tf.train.batch( [image, label], batch_size=batch_size, capacity=capacity)
    return image_batch, label_batch

file_length = 1 examples, labels = input_pipeline(file_length, 1)

The step I was missing was simply reading the file with tf.read_file(image_path). I figured decode_jpeg would do that.

Also just a tip: to inspect values of TF stuff (I hesitate to say variables, etc.) create a session like the following:

with tf.Session() as sess:
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord)
    real_value = sess.run([value]) # see value above
    print(real_value)
JohnAllen
  • 7,317
  • 9
  • 41
  • 65