Вы можете использовать tf.py_func()
, чтобы содержать предварительную обработку python / numpy из имен файлов, также зная, что reader.read()
возвращает как key
(имя файла), так и value
(содержимое файла), например
def my_preprocessing_from_filename(filename):
# This is your pre-processing, e.g.:
image_name = os.path.splitext(os.path.basename(str(filename)))[0]
image_info_path = os.path.join("data/info", "{}.txt".format(image_name))
image_info = numpy.loadtxt(image_info_path, dtype=numpy.int64)
# ... or whatever you do to load/process the info
return image_info
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
my_image_info = tf.py_func(my_preprocessing_from_filename, [key], tf.int64)
# ...
примечание: В зависимости от вашей предварительной обработки, вы можете подумать о портировании ее в операции Tensorflow, используя методы TF string_ops
для получения ваших информационных файлов из изображений. Например:
def my_tf_preprocessing_from_filename(filename):
# Get basename:
image_name = tf.string_split(filename, delimiter='/').values[-1]
# Remove ext (supposing no other "." in name):
image_name = tf.string_split(filename, delimiter='.').values[0]
image_info = tf.reduce_join(["data/info", image_name, ".txt"])
_, info_value = reader.read(filename_queue)
# ... further pre-process your info
return info_value
filename_queue = tf.train.string_input_producer(filenames)
reader = tf.WholeFileReader()
key, value = reader.read(filename_queue)
my_image_info = my_tf_preprocessing_from_filename([key])