从TensorFlow数据集迭代器获取输入(文件名)

时间:2019-02-18 17:15:26

标签: python tensorflow iterator tensorflow-datasets

我正在使用tensorflow数据集来训练模型。数据集使用文件名列表在会话期间读取它们,我想将文件名与图像一起获取。 更详细地说,我有这样的东西:

iterator

我认为此信息可以包含在from bokeh.io import show from bokeh.layouts import column from bokeh.models import CustomJS, ColumnDataSource, Slider from bokeh.plotting import figure data_dict = { 'lons':[[-1.0, -1.1, -1.2, -1.3, -1.4], [-1.0, -1.1, -1.25, -1.35, -1.45]], 'lats':[[53.0, 53.1, 53.2, 53.3, 53.4], [53.05, 53.15, 53.25, 53.35, 53.45]] } full_source = ColumnDataSource(data_dict) source = ColumnDataSource(data_dict) p = figure(plot_width=400, plot_height=400, tools="") p.multi_line(xs='lons', ys='lats', source=source) callback = CustomJS(args = dict(source=source, full_source=full_source), code = """ const time = cb_obj.value; const full_lons = full_source.data['lons'] const full_lats = full_source.data['lats'] for(i=0; i<full_lons.length; i++) { source.data['lons'][i] = full_lons[i].slice(0, time) source.data['lats'][i] = full_lats[i].slice(0, time) } // only need this because source.data is being updated "in place" source.change.emit() """) slider = Slider(start = 0, end = 5, value = 0, step = 1, callback = callback) slider.js_on_change('value', callback) layout = column(p, slider) show(layout) 中,但找不到。

1 个答案:

答案 0 :(得分:2)

您只需要将文件名和图像数据保留在数据集中:

filenames = tf.constant(["/var/data/image1.jpg", "/var/data/image2.jpg", ...])
labels = tf.constant([0, 37, ...])
dataset = tf.data.Dataset.from_tensor_slices((filenames, labels))
dataset.shuffle()

def _parse_function(filename, label):
  image_string = tf.read_file(filename)
  image_decoded = tf.image.decode_jpeg(image_string)
  image_resized = tf.image.resize_images(image_decoded, [28, 28])
  return filename, image_resized, label

dataset = dataset.map(_parse_function)
iterator = dataset.make_one_shot_iterator()
F, X, Y = iterator.get_next()

sess = tf.Session()
sess.run(iterator.initializer)
while True:
  sess.run(F, X)