我有一个使用迭代器训练我的网络的模型;遵循 Google 现在推荐的新数据集 API 管道模型。
我读取了 tfrecord 文件,向网络提供数据,进行了良好的训练,一切顺利,我在训练结束时保存了我的模型,以便稍后在其上运行推理。代码的简化版本如下:
""" Training and saving """
training_dataset = tf.contrib.data.TFRecordDataset(training_record)
training_dataset = training_dataset.map(ds._path_records_parser)
training_dataset = training_dataset.batch(BATCH_SIZE)
with tf.name_scope("iterators"):
training_iterator = Iterator.from_structure(training_dataset.output_types, training_dataset.output_shapes)
next_training_element = training_iterator.get_next()
training_init_op = training_iterator.make_initializer(training_dataset)
def train(num_epochs):
# compute for the number of epochs
for e in range(1, num_epochs+1):
session.run(training_init_op) #initializing iterator here
while True:
try:
images, labels = session.run(next_training_element)
session.run(optimizer, feed_dict={x: images, y_true: labels})
except tf.errors.OutOfRangeError:
saver_name = './saved_models/ucf-model'
print("Finished Training Epoch {}".format(e))
break
""" Restoring """
# restoring the saved model and its variables
session = tf.Session()
saver = tf.train.import_meta_graph(r'saved_models\ucf-model.meta')
saver.restore(session, tf.train.latest_checkpoint('.\saved_models'))
graph = tf.get_default_graph()
# restoring relevant tensors/ops
accuracy = graph.get_tensor_by_name("accuracy/Mean:0") #the tensor that when evaluated returns the mean accuracy of the batch
testing_iterator = graph.get_operation_by_name("iterators/Iterator") #my iterator used in testing.
next_testing_element = graph.get_operation_by_name("iterators/IteratorGetNext") #the GetNext operator for my iterator
# loading my testing set tfrecords
testing_dataset = tf.contrib.data.TFRecordDataset(testing_record_path)
testing_dataset = testing_dataset.map(ds._path_records_parser, num_threads=4, output_buffer_size=BATCH_SIZE*20)
testing_dataset = testing_dataset.batch(BATCH_SIZE)
testing_init_op = testing_iterator.make_initializer(testing_dataset) #to initialize the dataset
with tf.Session() as session:
session.run(testing_init_op)
while True:
try:
images, labels = session.run(next_testing_element)
accuracy = session.run(accuracy, feed_dict={x: test_images, y_true: test_labels}) #error here, x, y_true not defined
except tf.errors.OutOfRangeError:
break
我的问题主要是当我恢复模型时。如何将测试数据馈送到网络?
- 当我使用 , 恢复我的迭代器
testing_iterator = graph.get_operation_by_name("iterators/Iterator")
时next_testing_element = graph.get_operation_by_name("iterators/IteratorGetNext")
,我收到以下错误:GetNext() failed because the iterator has not been initialized. Ensure that you have run the initializer operation for this iterator before getting the next element.
- 所以我确实尝试使用以下方法初始化我的数据集
testing_init_op = testing_iterator.make_initializer(testing_dataset))
:我收到了这个错误:AttributeError: 'Operation' object has no attribute 'make_initializer'
另一个问题是,由于正在使用迭代器,因此无需在 training_model 中使用占位符,因为迭代器将数据直接提供给图形。但是这样,当我将数据提供给“准确性”操作时,如何恢复我在第三行到最后一行的 feed_dict 键?
编辑:如果有人可以建议一种在迭代器和网络输入之间添加占位符的方法,那么我可以尝试通过评估“准确性”张量来运行图表,同时将数据提供给占位符并完全忽略迭代器。