从tfrecord读取的数据的图像信息能用opencv查看么?
来源:10-14 人脸关键点模型训练编程实例(1)

慕田峪7505890
2021-12-19
因为我这边是tensorflow 2的环境,所以我用tensorflow 2来改写了读取tensorflow 2的代码,但是读取之后好像用opencv来查看图片好像跟打包前的完全不一样。这样正常么?
import tensorflow as tf
import numpy as np
from tensorflow import keras
import os
import cv2
def get_one_batch(batch_size, type, path):
’’‘
数据读取
:param batch_size:
:param type:
:param path:
:return:
’’'
if type == 0: # train
file = os.path.join(path, “train.tfrecords”)
else: # test
file = os.path.join(path, “test.tfrecords”)
dataset = tf.data.TFRecordDataset([file])
if type == 0:
dataset = dataset.shuffle(1000).batch(batch_size)
else:
dataset = dataset.batch(batch_size)
expected_features = {
“image”: tf.io.FixedLenFeature([], dtype=tf.string),
“label”: tf.io.FixedLenFeature([136], dtype=tf.float32)
}
for serialized_example_tensor in dataset:
ex = tf.io.parse_example(serialized_example_tensor, expected_features)
images = tf.io.decode_raw(ex[‘image’], tf.uint8)
if len(images) > batch_size:
images = tf.cast(tf.reshape(images, (batch_size, 128, 128, 3)), dtype=tf.float32)
else:
images = tf.cast(tf.reshape(images, (len(images), 128, 128, 3)), dtype=tf.float32)
for image in images:
cv2.imshow(‘img’, image.numpy())
cv2.waitKey()
print(image.numpy())
labels = ex[‘label’]
for label in labels:
print(label.numpy())
if name == “main”:
get_one_batch(1000, type=0, path="/Users/admin/Documents/300W_LP/tfrecord_basic")
1回答
-
会写代码的好厨师
2021-12-25
这个现象肯定不正确。这里说的不同如果是rgb和bgr的区别,注意下转一下数据。如果是数据完全不正确,检查下打包的脚本是不是对的。
也可以把打包好的数据qq发给我看下。012021-12-29
相似问题