Tensorlow训练模型时出错
错误如下
(0) Out of range: FIFOQueue '_3_batch_1/fifo_queue' is closed and has insufficient elements (requested 64, current size 21)
[[node batch_1 (defined at /guest/Documents/ShoePrintAnalyze01/utils.py:146) ]]
[[batch_1/_2587]]
(1) Out of range: FIFOQueue '_3_batch_1/fifo_queue' is closed and has insufficient elements (requested 64, current size 21)
[[node batch_1 (defined at /guest/Documents/ShoePrintAnalyze01/utils.py:146) ]]
经过百度谷歌都没有找到很好的办法,查找到的方法大多是如下:
在初始化变量的时候执行下面两句代码
sess.run(tf.global_variables_initializer())
sess.run(tf.local_variables_initializer())
可是在我这里并没有什么用。
解决办法
经过查看console输出发现了下面的这个提示,这个提示主要是图片有问题,一般情况,输出这个提示程序会直接停止掉
Premature end of JPEG file
可以在获取batch的时候可以像下面这样写,主要是try_recover_truncated=True这里,
# shuffle=shuffle 打乱数据让每一次训练的数据顺序都不一样
input_queue = tf.train.slice_input_producer([image, label],
num_epochs=epoch_num,
shuffle=shuffle)
label = input_queue[1]
image_contents = tf.read_file(input_queue[0])
image = tf.image.decode_jpeg(image_contents, channels=3, try_recover_truncated=True)
tf.image.decode_jpeg这个函数里加上try_recover_truncated=True,还是会有 Premature end of JPEG file 这个提示输出,但是不会程序不会停止了,也不会报 Out of range: FIFOQueue '_3_batch_1/fifo_queue' is closed and has insufficient elements (requested 64, current size 21) 这个错误了。
原因
最后见检查图片发现是图片有问题,jpg格式的图片在在下载的过程中可以存在干扰或者什么导致 jpg格式的图片出现错误,虽然不影响打开图片,但是对训练是有影响的
jpg图片格式
用十六进制打开jpg的图片我们可以看到开始的两位是 ff d8 结尾的两位是 ff d9如下图
使用的notepad++的插件 HEX-editor打开如下
结尾:
上图是正常的图片。
不正常的图片就不是这种情况。但是这种图片不影响正常的打开,可能会影响训练。
jpg的这个问题可以见这里就是下面的这个参考链接
参考:https://blog.youkuaiyun.com/u011681952/article/details/82254756#comments