MCPcopy Create free account
hub / github.com/NVIDIA/DALI / test_tfrecord_pad_last_batch

Function test_tfrecord_pad_last_batch

dali/test/python/reader/test_index.py:105–147  ·  view source on GitHub ↗
(batch_description, dont_use_mmap, use_o_direct)

Source from the content-addressed store, hash-verified

103
104@cartesian_params(((1, 2, 1), (3, 1, 2)), (True, False), (True, False))
105def test_tfrecord_pad_last_batch(batch_description, dont_use_mmap, use_o_direct):
106 if not dont_use_mmap and use_o_direct:
107 raise SkipTest("Cannot use O_DIRECT with mmap")
108 num_samples, batch_size, num_shards = batch_description
109
110 @pipeline_def(batch_size=batch_size, device_id=0, num_threads=4)
111 def tfrecord_pipe(path, index_path, dont_use_mmap, use_o_direct):
112 input = fn.readers.tfrecord(
113 path=path,
114 index_path=index_path,
115 num_shards=num_shards,
116 dont_use_mmap=dont_use_mmap,
117 use_o_direct=use_o_direct,
118 features={"image/class/label": tfrec.FixedLenFeature([1], tfrec.int64, -1)},
119 name="Reader",
120 )
121 return input["image/class/label"]
122
123 tfrecord = os.path.join(get_dali_extra_path(), "db", "tfrecord", "train")
124 tfrecord_idx = os.path.join(get_dali_extra_path(), "db", "tfrecord", "train.idx")
125
126 idx_files_dir = tempfile.TemporaryDirectory()
127 recordio_idx = "rio_train.idx"
128 idx_file = os.path.join(idx_files_dir.name, recordio_idx)
129
130 def leave_only_N(src, dst, n):
131 with open(src, "r") as tmp_f:
132 with open(dst, "w") as f:
133 for i, x in enumerate(tmp_f):
134 if i == n:
135 break
136 f.write(x)
137
138 leave_only_N(tfrecord_idx, idx_file, num_samples)
139
140 pipe = tfrecord_pipe(tfrecord, idx_file, dont_use_mmap, use_o_direct)
141 pipe_ref = tfrecord_pipe(tfrecord, idx_file, False, False)
142 iters = (pipe.epoch_size("Reader") + batch_size) // batch_size
143 for _ in range(iters):
144 out = pipe.run()
145 out_ref = pipe_ref.run()
146 for a, b in zip(out, out_ref):
147 assert np.array_equal(a.as_array(), b.as_array())
148
149
150def test_recordio():

Callers

nothing calls this directly

Calls 5

get_dali_extra_pathFunction · 0.90
leave_only_NFunction · 0.85
tfrecord_pipeFunction · 0.85
epoch_sizeMethod · 0.45
runMethod · 0.45

Tested by

no test coverage detected