如何在TensorFlow会话中使用数据集进行培训

时间:2017-11-30 15:35:38

标签: python tensorflow tensorflow-datasets

我喜欢使用张量流在我们自己的大图像库(数百万标记图像)上执行图像分类。我是stackoverflow,python和tensorflow的新手,并通过一些教程(mnist等)自己完成了这一点,我已经能够从字典中准备TensorFlow数据集,包括图像的绝对路径和相应的标签。但是,我在TensorFlow会话中使用数据集时陷入困境。这是我的(示例)代码:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
import numpy as np
import time
import mymodule # I build my module to read the images and labels
from tensorflow.python.framework import ops
from tensorflow.python.framework import dtypes
from tensorflow.contrib.data import Iterator

beginTime = time.time()
batch_size = 100
learning_rate = 0.005
max_steps = 2
NUM_CLASSES = 25

def input_parser(img_path, label):
    one_hot = tf.one_hot(label, NUM_CLASSES)

    img_file = tf.read_file(img_path)
    img_decoded = tf.image.decode_jpeg(img_file, channels = 3)

    return img_decoded, one_hot

#Import Training data (returns the dicitonary with paths and labels)
train_dict = mymodule.getFileMap(labelList, imageList) 

#Import Test data
test_dict = mymodule.getFileMap(labelList, imageList)

#Get train data
train_file_list, train_label_list = get_file_label_list(train_dict)
train_images_tensor = ops.convert_to_tensor(train_file_list, dtype=dtypes.string)
train_labels_tensor = ops.convert_to_tensor(train_label_list, dtype=dtypes.int64)

#Get test data
test_file_list, test_label_list = get_file_label_list(test_dict)
test_images_tensor = ops.convert_to_tensor(test_file_list, dtype=dtypes.string)
test_labels_tensor = ops.convert_to_tensor(test_label_list, dtype=dtypes.int64)

#Create TensorFlow Datset object
train_data = tf.data.Dataset.from_tensor_slices((train_images_tensor, train_labels_tensor))
test_data = tf.data.Dataset.from_tensor_slices((test_images_tensor, test_labels_tensor))

# Transform the datset so that it contains decoded images 
# and one-hot vector labels
train_data = train_data.map(input_parser)
test_data = test_data.map(input_parser)

# Batching --> How to do it right?
#train_data = train_data.batch(batch_size = 100)
#test_data = train_data.batch(batch_size = 100)

#Define input placeholders
image_size = 990*990*3
images_placeholder = tf.placeholder(tf.float32, shape=[None, image_size])
labels_placeholder = tf.placeholder(tf.int64, shape=[None])

# Define variables (these afe the values we want to optimize)
weigths = tf.Variable(tf.zeros([image_size, NUM_CLASSES]))
biases = tf.Variable(tf.zeros([NUM_CLASSES]))

# Define the classifier´s result
logits = tf.matmul(images_placeholder, weigths) + biases

# Define the loss function
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits = logits, labels = labels_placeholder))

# Define the training operation
train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)

# Operation comparing prediciton with true label
correct_prediciton = tf.equal(tf.argmax(logits, 1), labels_placeholder)

# Operation calculating the accuracy of our predicitons
accuracy = tf.reduce_mean(tf.cast(correct_prediciton, tf.float32))

#Create TensorFlow Iterator object
iterator = Iterator.from_structure(train_data.output_types,
                                   train_data.output_shapes)
next_element = iterator.get_next()

#Create two initialization ops to switch between the datasets
train_init_op = iterator.make_initializer(train_data)
test_init_op = iterator.make_initializer(test_data)


with tf.Session() as sess:
    #Initialize variables
    sess.run(tf.global_variables_initializer())
    sess.run(train_init_op)
    for _ in range(10):
        try:
            elem = sess.run(next_element)
            print(elem)
        except tf.errors.OutOfRangeError:
            print("End of training datset.")
            break

thisthis教程之后,我无法解决如何在张量流会话中使用(图像和标签)数据集进行培训的问题。我能够通过迭代来打印出数据集,但却无法用它来学习。

我不理解如何在train_data = tf.data.Dataset.from_tensor_slices((train_images_tensor,train_labels_tensor))操作中合并图像和标签后,如the 2nd tutorial所要求的那样。我也不知道如何正确实现批处理。

我想在会话中做的基本上是这个(来自the 2nd tutorial):

# Generate input data batch
indices = np.random.choice(data_sets['images_train'].shape[0], batch_size)
images_batch = data_sets['images_train'][indices]
labels_batch = data_sets['labels_train'][indices]

# Periodically print out the model's current accuracy
if i % 100 == 0:
  train_accuracy = sess.run(accuracy, feed_dict={
    images_placeholder: images_batch, labels_placeholder: labels_batch})
  print('Step {:5d}: training accuracy {:g}'.format(i, train_accuracy))

# Perform a single training step
sess.run(train_step, feed_dict={images_placeholder: images_batch,
  labels_placeholder: labels_batch})

# After finishing the training, evaluate on the test set
test_accuracy = sess.run(accuracy, feed_dict={
  images_placeholder: data_sets['images_test'],
  labels_placeholder: data_sets['labels_test']})
print('Test accuracy {:g}'.format(test_accuracy))

endTime = time.time()
print('Total time: {:5.2f}s'.format(endTime - beginTime))

如果有人可以告诉我,如何分别访问数据集中的图像和标签并将其用于培训,我将非常感激。还有一个提示,在哪里以及如何进行批处理将是值得赞赏的。 谢谢。

1 个答案:

答案 0 :(得分:2)

在您的代码中,next_element是两个张量的元组,与数据集的结构相匹配:即它是一个元组,其第一个元素是图像,第二个元素是标签。要访问各个张量,您可以执行以下操作:

next_element = iterator.get_next()
next_image = next_element[0]
next_label = next_element[1]

# Or, in a single line:
next_image, next_label = iterator.get_next()

要批量tf.data.Dataset,您可以使用Dataset.batch()转换。您注释掉的代码应该可以正常工作:

train_data = train_data.batch(batch_size = 100)
test_data = train_data.batch(batch_size = 100)