在进行预测时,我为输入数据建立了数据集管道。但是,当我尝试代码时,发生了错误
FailedPreconditionError(请参阅上面的回溯):GetNext()失败,因为迭代器尚未初始化。在获取下一个元素之前,请确保已为此迭代器运行了初始化程序操作。 [[节点:IteratorGetNext_259 = IteratorGetNextoutput_shapes = [[?, 227,227,6]],output_types = [DT_FLOAT],_ device =“ / job:localhost /副本:0 / task:0 / device:CPU:0”]] >
遍历数据集的迭代器定义如下:
for k in range(num_init_ops):
with tf.device('/cpu:0'):
pre_data.append(PreDataGenerator(pre_file,
mode='predicting',
batch_size=batch_size,
num_classes=num_classes,
shuffle=False,
iterator_size=iterator_size,
kth_init_op=k))
# create an reinitializable iterator given the dataset structure
iterator = Iterator.from_structure(pre_data[k].data.output_types,
pre_data[k].data.output_shapes)
next_batch = iterator.get_next()
# Ops for initializing the two different iterators
predicting_init_op.append(iterator.make_initializer(pre_data[k].data))
之所以写 for 周期,是因为我希望创建多个数据集init-op来将数据拆分为不同的迭代器,以防止积累引起OOM错误的内存调用(我想知道这样做是否可行)
我确定迭代器已初始化(调试时输出正确的结构)。这是我的Tensorflow会话代码:
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
# sess.run(tf.local_variables_initializer())
saver.restore(sess, './checkpoints_grade1/model_epoch46.ckpt') # todo:
print("{} Start predicting...".format(datetime.now()))
for j in range(num_init_ops+1):#todo:
print('{} Initializing {} iterator'.format(datetime.now(),j))
# Initialize iterator with the predicting dataset
sess.run(predicting_init_op[j])
for i in range(iterator_size):
# get next batch of data
img_batch = sess.run(next_batch)#todo:?
# And run the predicting op
img_batch = tf.reshape(img_batch, (1, 227, 227, 6))
pred = sess.run(softmax, feed_dict={x: sess.run(img_batch)})
predicted_label = pred.argmax(axis=1)
predictions.append(predicted_label[0])
output_file.write(str(i) + ' , ' + str(predicted_label[0]) + '\n')
答案 0 :(得分:1)
您需要初始化迭代器:
RequireConsent = false;
做这样的训练:
sess.run(iterator.initializer)
或者,在定义next_batch = iterator.get_next()
sess.run(iterator.initializer)
for epoch in range(n_epochs):
while True:
try:
batch = sess.run(next_batch)
# feed data, train
# ...
except tf.errors.OutOfRangeError:
sess.run(iterator.initializer)
break
实例时,您可以指定要训练的时期:
tf.data.Dataset.from_tensor_slices
使用此方法,您不需要data = tf.data.Dataset.from_tensor_slices({
'x':train_data,
'y':train_labels
}).repeat(n_epochs).batch(batch_size)
iterator = data.make_initializable_iterator()
循环:
for epoch in range(n_epochs)
答案 1 :(得分:0)
每当我开始预测一批样本时,我就通过a shell script控制预测程序,以便内存不会耗尽。问题解决了。
#!/bin/bash
for ((i=0;i<9;i++))
do
python classifier_v4.py --iter_epoch $i
done