恢复使用shuffle_batch的tensorflow图永远挂起

时间:2018-06-11 09:11:15

标签: python tensorflow restore

我有一个张量流代码,它使用tf.train.shuffle_batch作为训练部分。我读了一些论坛,我想到了使用这些行来分隔使用队列的输入管道和应该只使用一个图像的推理部分:

microbenchmark(nicola = vapply(Tones,function(x) length(rle(charToRaw(x))$lengths)-1,1)/(nchar(Tones)-1),
               LAP = sapply(lapply(strsplit(Tones, split=""), as.numeric), function(x){
                 sum(ifelse(x != dplyr::lag(x, 1), 1, 0), na.rm = T)/(length(x)-1)
               }),
               unit = "ms")

Unit: milliseconds
   expr      min        lq      mean    median        uq      max neval cld
 nicola 0.077654 0.0841960 0.1047870 0.0871830 0.0934405 1.684196   100   a
    LAP 0.109227 0.1194675 0.1482074 0.1230225 0.1281425 2.525868   100   a

然后我在feed_dict中指定is_just_one:在我的训练期间假。我在每个纪元后保存模型:

 ima=queue_loader.images
 is_just_one = tf.placeholder(dtype=bool,shape=(),name='is_just_one')
 imgs = tf.placeholder(tf.float32, (1, height, width, 1), name='imgs')
 images = tf.cond(is_just_one, lambda:imgs, lambda:ima)

拥有检查点文件后,我以这种方式运行freeze.py代码:

saver.save(sess, checkpoint_path, global_step=ep+1)

然后:我尝试使用以下代码恢复我的模型:

python freeze.py --model_dir=checkpoints --output_node_names=imgs,is_just_one,prediction

当我运行我的代码时会发生什么,它会永远挂起,设备放置是:

imn=tf.placeholder(tf.float32, shape=(None,None,1))
imno=tf.image.per_image_standardization(imn)
X=io.imread("test.png")
with tf.Session() as sesss:
  X=sesss.run(imno,feed_dict={imn:X[...,0,None]})

with tf.gfile.GFile("checkpoints/frozen_model.pb", "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
    tf.import_graph_def(graph_def,name='import')

for i in range(2):
    with tf.Session(graph=graph,config=tf.ConfigProto(log_device_placement=True)) as sess:
        act_map =graph.get_tensor_by_name("import/prediction:0")
        is_just_one = graph.get_tensor_by_name("import/is_just_one:0")
        inputs = graph.get_tensor_by_name("import/imgs:0")
        mapp=sess.run(act_map,feed_dict={is_just_one:True,inputs:X[None,:,:,None]})

因此,如果我理解正确,与tf.train.shuffle_batch相关联的已保存节点正在等待输入,并且因为我不提供它,所以它会永远挂起。

这出了什么问题?有没有办法忽略该节点,只是像往常一样在图表中提供图像?

非常感谢你的宝贵帮助

1 个答案:

答案 0 :(得分:0)

我明白了!这确实非常简单,但却让我非常头疼。要解决这个问题,只需要定义如下内容:

  input_placeholder = tf.placeholder_with_default(image, [None, None, None, 1],name="inputs")

其中image是来自queue和shuffle_batch的图像。结构tf.placeholder_with_default指定如果在feed_dict中没有值传递给该节点,则其值仅为" image"。当我恢复我的冻结模型时,我只是以通常的方式使用feed_dict给这个占位符一些东西,并且一切正常。我想通过这种方式,如果指定了占位符的值,那么关于shuffle_batch的所有垃圾节点都会被分开。很高兴知道。

相关问题