PREREQUISITES:

We divide the task of supporting a file format into two pieces:

• File formats: We use a Reader Op to read a record (which can be any string) from a file.
• Record formats: We use decoder or parsing Ops to turn a string record into tensors usable by TensorFlow.

For example, to read a CSV file, we use a Reader for text files followed by an Op that parses CSV data from a line of text.

## Writing a Reader for a file format

A Reader is something that reads records from a file. There are some examples of Reader Ops already built into TensorFlow:

You can see these all expose the same interface, the only differences are in their constructors. The most important method is read. It takes a queue argument, which is where it gets filenames to read from whenever it needs one (e.g. when the read op first runs, or the previous read reads the last record from a file). It produces two scalar tensors: a string key and a string value.

To create a new reader called SomeReader, you will need to:

1. In C++, define a subclass of tensorflow::ReaderBase called SomeReader.
2. In C++, register a new reader op and kernel with the name "SomeReader".
3. In Python, define a subclass of tf.ReaderBase called SomeReader.

You can put all the C++ code in a file in tensorflow/core/user_ops/some_reader_op.cc. The code to read a file will live in a descendant of the C++ ReaderBase class, which is defined in tensorflow/core/kernels/reader_base.h. You will need to implement the following methods:

• OnWorkStartedLocked: open the next file
• ReadLocked: read a record or report EOF/error
• OnWorkFinishedLocked: close the current file, and
• ResetLocked: get a clean slate after, e.g., an error

These methods have names ending in "Locked" since ReaderBase makes sure to acquire a mutex before calling any of these methods, so you generally don't have to worry about thread safety (though that only protects the members of the class, not global state).

For OnWorkStartedLocked, the name of the file to open is the value returned by the current_work() method. ReadLocked has this signature:

Status ReadLocked(string* key, string* value, bool* produced, bool* at_end)


If ReadLocked successfully reads a record from the file, it should fill in:

• *key: with an identifier for the record, that a human could use to find this record again. You can include the filename from current_work(), and append a record number or whatever.
• *value: with the contents of the record.
• *produced: set to true.

If you hit the end of a file (EOF), set *at_end to true. In either case, return Status::OK(). If there is an error, simply return it using one of the helper functions from tensorflow/core/lib/core/errors.h without modifying any arguments.

Next you will create the actual Reader op. It will help if you are familiar with the adding an op how-to. The main steps are:

• Registering the op.
• Define and register an OpKernel.

To register the op, you will use a REGISTER_OP call defined in tensorflow/core/framework/op.h. Reader ops never take any input and always have a single output with type resource. They should have string container and shared_name attrs. You may optionally define additional attrs for configuration or include documentation in a Doc. For examples, see tensorflow/core/ops/io_ops.cc, e.g.:

#include "tensorflow/core/framework/op.h"

.Attr("container: string = ''")
.Attr("shared_name: string = ''")
.SetIsStateful()
.SetShapeFn(shape_inference::ScalarShape)
.Doc(R"doc(
A Reader that outputs the lines of a file delimited by '\n'.
)doc");


To define an OpKernel, Readers can use the shortcut of descending from ReaderOpKernel, defined in tensorflow/core/framework/reader_op_kernel.h, and implement a constructor that calls SetReaderFactory. After defining your class, you will need to register it using REGISTER_KERNEL_BUILDER(...). An example with no attrs:

#include "tensorflow/core/framework/reader_op_kernel.h"

public:
Env* env = context->env();
}
};



An example with attrs:

#include "tensorflow/core/framework/reader_op_kernel.h"

public:
OP_REQUIRES_OK(context,
errors::InvalidArgument("skip_header_lines must be >= 0 not ",
Env* env = context->env();
});
}
};



The last step is to add the Python wrapper. You can either do this by compiling a dynamic library or, if you are building TensorFlow from source, adding to user_ops.py. For the latter, you will import tensorflow.python.ops.io_ops in tensorflow/python/user_ops/user_ops.py and add a descendant of io_ops.ReaderBase.

from tensorflow.python.framework import ops
from tensorflow.python.ops import common_shapes
from tensorflow.python.ops import io_ops

def __init__(self, name=None):

You can see some examples in tensorflow/python/ops/io_ops.py.
Note that it can be useful to use multiple Ops to decode a particular record format. For example, you may have an image saved as a string in a tf.train.Example protocol buffer. Depending on the format of that image, you might take the corresponding output from a tf.parse_single_example op and call tf.image.decode_jpeg, tf.image.decode_png, or tf.decode_raw. It is common to take the output of tf.decode_raw and use tf.slice and tf.reshape to extract pieces.