1 使用tfrecord
1.1 Unable to decode bytes as JPEG, PNG, GIF, or BMP
- 错误:
assertion failed: [Unable to decode bytes as JPEG, PNG, GIF, or BMP
- 出现原因:
可能是生成tfrecord的时候,存入tfrecord的图片的大小和需要解析生成的图片的大小不一样。比如生成tfrecord的时候,存入的二进制是原始的jpg图片,而解析tfrecord图片的时候,生成的是resize的大小。
1.2 tensorboard 显示的图片和自己resize或者padding后的图片不一致,出现马赛克或者规则条状的时候
- 出现原因:
可能是生成tfrecord的时候,存入tfrecord的图片的大小和需要解析生成的图片的大小一样。但是,长宽的位置放反了,因为很多其他图片处理的包和TensorFlow自己处理图片的长宽定义不一样
1.3 Enqueue operation was cancelled, Can’t parse serialized Example
-
出现原因:
在解析list字段的时候,将该字段设置为FixedLenFeature,但是没有指定list字段的长度。 -
错误代码:
#样例代码
feature_map = {
'image/format': tf.FixedLenFeature([], dtype=tf.string),
'image/encoded': tf.FixedLenFeature([], dtype=tf.string),
'image/class': tf.FixedLenFeature([],dtype=tf.int64), #feature here is a list
'image/unpadded_class': tf.VarLenFeature(dtype=tf.int64),
'height': tf.FixedLenFeature([], dtype=tf.int64),
'width': tf.FixedLenFeature([], dtype=tf.int64),
'orig_width': tf.FixedLenFeature([], dtype=tf.int64),
'image/text': tf.FixedLenFeature([], dtype=tf.string),
}
- 解决方法:
#样例代码
feature_map = {
'image/format': tf.FixedLenFeature([], dtype=tf.string),
'image/encoded': tf.FixedLenFeature([], dtype=tf.string),
'image/class': tf.FixedLenFeature([37],dtype=tf.int64), #give it a fixed length
# 'image/class': tf.VarLenFeature(dtype=tf.int64), #or change it to a variable length
'image/unpadded_class': tf.VarLenFeature(dtype=tf.int64),
'height': tf.FixedLenFeature([], dtype=tf.int64),
'width': tf.FixedLenFeature([], dtype=tf.int64),
'orig_width': tf.FixedLenFeature([], dtype=tf.int64),
'image/text': tf.FixedLenFeature([], dtype=tf.string),
}
2 图片读取和显示
2.1 ‘utf-8’ codec can’t decode byte 0xff in position 0: invalid start byte
使用tf.gfile.FastGFile(img_dir,'r').read()
读取文件的时候,出现错误
- 错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
- 解决方法:
需要按照二进制来读取文件,修改为tf.gfile.FastGFile(img_dir,'rb').read()
即可
2.2 tensorflow中tf.image.resize_images(img_data,shape,method)方法的method当选为0的时候,如果用该函数把图片变小重新保存,会发现保存的图片有时候是噪声图片。
- 解决方法:
将method方法设为1,通常可以解决这个问题
2.3 使用matplotlib.pyplot来显示以tensor形式保存的图片的时候出现错误
#样例代码
with tf.Session() as sess:
noise_img=generate_noise_image((150,600,3))
plt.imshow(noise_img.eval())
plt.show()
- 错误:
Clipping input data to the valid range for imshow with RGB data([0..1] for floats or [0..255] for integers).
- 出现原因:
plt显示图片的时候,对于float数据,需求范围为[0,1];对于整形数据,需求范围[0,255]。 - 解决方法:
#样例代码
with tf.Session() as sess:
noise_img=generate_noise_image((150,600,3))
noise_img = noise_img/255
#noise_img = tf.image.convert_image_dtype(noise_img,dtype=tf.uint8)
plt.imshow(noise_img.eval())
plt.show()
3 随着循环的进行,运行速度越来越慢
#样例代码
def resize_and_store_image(sample_L,tarShape,tarDir):
if not os.path.exists(tarDir):
os.makedirs(tarDir)
cur=1
for img_dir in sample_L:
# copy correspoding text to tarDir
text_title, text_dir = get_text_dir(img_dir, text_fold)
shutil.copy(text_dir,os.path.join(tarDir,text_title))
print(('{}/{}' + img_dir).format(cur, len(sample_L)))
resize_shape = get_new_image_shape(img_dir, tarShape)
with tf.Session() as sess:
image_raw_data = tf.gfile.FastGFile(img_dir,'rb').read()
img_data = tf.image.decode_jpeg(image_raw_data)
img_resized = tf.image.resize_images(img_data,(resize_shape[1],resize_shape[0]),method=1)
img_padded = tf.image.resize_image_with_crop_or_pad(img_resized,tarShape[1],tarShape[0])
img_padded = tf.image.convert_image_dtype(img_padded,dtype=tf.uint8)
print(type(img_padded))
encoded_image = tf.image.encode_jpeg(img_padded)
with tf.gfile.GFile(os.path.join(tarDir,img_dir.split('/')[-1]),"wb") as f:
f.write(encoded_image.eval())
cur = cur+1
- 出现原因:
随着循环的进行,在sess中定义的图越来越大,执行的速度也越来越慢。 - 解决方法:
def resize_and_store_image(sample_L,tarShape,tarDir):
if not os.path.exists(tarDir):
os.makedirs(tarDir)
cur=1
for img_dir in sample_L:
#reset graph, avoid the graph getting bigger and bigger
# and calculation speed slowwer and slower
tf.reset_default_graph()
graph = tf.Graph()
with graph.as_default() as g:
# copy correspoding text to tarDir
text_title, text_dir = get_text_dir(img_dir, text_fold)
shutil.copy(text_dir,os.path.join(tarDir,text_title))
print(('{}/{}' + img_dir).format(cur, len(sample_L)))
resize_shape = get_new_image_shape(img_dir, tarShape)
with tf.Session() as sess:
image_raw_data = tf.gfile.FastGFile(img_dir,'rb').read()
img_data = tf.image.decode_jpeg(image_raw_data)
img_resized = tf.image.resize_images(img_data,(resize_shape[1],resize_shape[0]),method=1)
img_padded = tf.image.resize_image_with_crop_or_pad(img_resized,tarShape[1],tarShape[0])
img_padded = tf.image.convert_image_dtype(img_padded,dtype=tf.uint8)
print(type(img_padded))
encoded_image = tf.image.encode_jpeg(img_padded)
with tf.gfile.GFile(os.path.join(tarDir,img_dir.split('/')[-1]),"wb") as f:
f.write(encoded_image.eval())
cur = cur+1
4 tensorflow 配置
4.1 tensorflow log 日志级别设置
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error
5 tensorboard 相关
5.1 tensorboard 无法查看text或者其他一些版块
如果确定在model中加入了tf.summary.text()等,但是在tensorboard中显示没有收集这些信息,很可能是因为,用来生成总结的TensorFlow版本和用来查看tensorboard的TensorFlow版本不一致