(argv)
| 190 | |
| 191 | |
| 192 | def main(argv): |
| 193 | del argv # Unused. |
| 194 | |
| 195 | # Sanity check on the GCS bucket URL. |
| 196 | if not FLAGS.gcs_bucket_url or not FLAGS.gcs_bucket_url.startswith("gs://"): |
| 197 | print("ERROR: Invalid GCS bucket URL: \"%s\"" % FLAGS.gcs_bucket_url) |
| 198 | sys.exit(1) |
| 199 | |
| 200 | # Generate random tfrecord path name. |
| 201 | input_path = FLAGS.gcs_bucket_url + "/" |
| 202 | input_path += "".join(random.choice("0123456789ABCDEF") for i in range(8)) |
| 203 | input_path += ".tfrecord" |
| 204 | print("Using input path: %s" % input_path) |
| 205 | |
| 206 | # Verify that writing to the records file in GCS works. |
| 207 | print("\n=== Testing writing and reading of GCS record file... ===") |
| 208 | example_data = create_examples(FLAGS.num_examples, 5) |
| 209 | with tf.python_io.TFRecordWriter(input_path) as hf: |
| 210 | for e in example_data: |
| 211 | hf.write(e.SerializeToString()) |
| 212 | |
| 213 | print("Data written to: %s" % input_path) |
| 214 | |
| 215 | # Verify that reading from the tfrecord file works and that |
| 216 | # tf_record_iterator works. |
| 217 | record_iter = tf.python_io.tf_record_iterator(input_path) |
| 218 | read_count = 0 |
| 219 | for _ in record_iter: |
| 220 | read_count += 1 |
| 221 | print("Read %d records using tf_record_iterator" % read_count) |
| 222 | |
| 223 | if read_count != FLAGS.num_examples: |
| 224 | print("FAIL: The number of records read from tf_record_iterator (%d) " |
| 225 | "differs from the expected number (%d)" % (read_count, |
| 226 | FLAGS.num_examples)) |
| 227 | sys.exit(1) |
| 228 | |
| 229 | # Verify that running the read op in a session works. |
| 230 | print("\n=== Testing TFRecordReader.read op in a session... ===") |
| 231 | with tf.Graph().as_default(): |
| 232 | filename_queue = tf.train.string_input_producer([input_path], num_epochs=1) |
| 233 | reader = tf.TFRecordReader() |
| 234 | _, serialized_example = reader.read(filename_queue) |
| 235 | |
| 236 | with tf.Session() as sess: |
| 237 | sess.run(tf.global_variables_initializer()) |
| 238 | sess.run(tf.local_variables_initializer()) |
| 239 | tf.train.start_queue_runners() |
| 240 | index = 0 |
| 241 | for _ in range(FLAGS.num_examples): |
| 242 | print("Read record: %d" % index) |
| 243 | sess.run(serialized_example) |
| 244 | index += 1 |
| 245 | |
| 246 | # Reading one more record should trigger an exception. |
| 247 | try: |
| 248 | sess.run(serialized_example) |
| 249 | print("FAIL: Failed to catch the expected OutOfRangeError while " |
nothing calls this directly
no test coverage detected