在数据集制作时使用了tfrecord格式进行记录,解码产生的tensor为
tf.Tensor([[ 105. 107. 86. ... 71. 104. -128.]], shape=(1, 12288), dtype=float32)
直接读取图片解码产生的tensor为
tf.Tensor([[105. 107. 86. ... 71. 104. 128.]], shape=(1, 12288), dtype=float32)
两者数据不统一
tfrecord解码方式如下
# 映射函数,用于解析一条example
def _parse_function (exam_proto):
return tf.io.parse_single_example (exam_proto, feature_description)
#读取返回数据集
def read_dataset(record_path):
reader = tf.data.TFRecordDataset(record_path) # 打开一个TFrecord
#reader = reader.shuffle (buffer_size = 1000) # 在缓冲区中随机打乱数据
reader = reader.map (_parse_function) # 解析数据
#for row in reader.take(1): #获取指定数量的数据集
labels = []
imgs = []
for row in reader: #遍历数据集
label = tf.cast(row['label'],dtype=tf.float32)
label = label - 1
img = tf.io.decode_raw(row['img_raw'],out_type=tf.int8)
img = tf.cast(img,dtype=tf.float32)
labels.append(label)
imgs.append(img)
直接读取图片解码方式如下
img = tf.io.read_file (test_img_path)
img_raw = tf.image.decode_bmp (img)
img_raw = tf.cast(img_raw,dtype=tf.float32)
x_predict = tf.convert_to_tensor(img_raw)
x_predict = tf.reshape(x_predict,[1,-1])
分析数据发现有部分图片字节数值相反,随怀疑数据类型问题
在将tfrocord编码方式中
img = tf.io.decode_raw(row['img_raw'],out_type=tf.int8)
替换为
img = tf.io.decode_raw(row['img_raw'],out_type=tf.uint8)
后问题得以解决