Я хочу закодировать свойства этого изображения и некоторые функции в файле tfrecord. Функциональная часть в порядке, но я хотел убедиться, что изображение кодируется правильно, поскольку я новичок в tenorflow
'ImageWidth': getInt64Feature(Data['Image'].shape[1]),
'ImageHeight': getInt64Feature(Data['Image'].shape[0]),
'ImageChannels': getInt64Feature(Data['Image'].shape[2]),
'Image': getByteFeature(Data['Image'].tostring()),
Это мой код для преобразования моего изображения и меток (метки не видны) в tfrecord.
import os
import io
import pandas as pd
import tensorflow as tf
from PIL import Image
import keras
def _bytes_feature(value):
"""Returns a bytes_list from a string / byte."""
# if isinstance(value, type(tf.constant(0))):
# value = value.numpy() # BytesList won't unpack a string from an EagerTensor.
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
"""Returns a float_list from a float / double."""
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):
"""Returns an int64_list from a bool / enum / int / uint."""
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def serialize_example(feature0, feature1, feature2, feature3,feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13,feature14, feature15):
"""
Creates a tf.Example message ready to be written to a file.
"""
# Create a dictionary mapping the feature name to the tf.Example-compatible
# data type.
feature = {
'Image': _bytes_feature(feature0)
............ }
example_proto = tf.train.Example(features=tf.train.Features(feature=feature))
return example_proto.SerializeToString()
# Create a Features message using tf.train.Example.
def encode_Image(path):
with open(path, 'rb') as f:
_bytes = f.read()
# img = tf.keras.preprocessing.image.load_img(path)
# img_array = tf.keras.preprocessing.image.img_to_array(img)
# img_bytes = tf.io.serialize_tensor(img_array)
#image_shape = img_array.shape
#with open(path, 'rb') as fid:
#encoded_jpg = fid.read()
return _bytes
def write_Example():
filename=r"C:\Dataset_Thesis\Training\\"
# with tf.io.TFRecordWriter(filename) as writer:
exampless = pd.read_csv(r"C:\TrainingMain.csv")
for index, record in exampless.iterrows():
with tf.io.TFRecordWriter(filename+str(index)+"_dataset.tfrecord") as writer:
print(str(record["Image"]))
feature0 = encode_Image(str(record["Image"]))
...........
...........
example= serialize_example(feature0, feature1, feature2, feature3,feature4, feature5, feature6, feature7, feature8, feature9, feature10, feature11, feature12, feature13, feature14, feature15)
writer.write(example)
writer.close()
write_Example()
Я не получаю сообщение об ошибке, но кто-то может подтвердить, что моя часть кодирования изображения соответствует требуемым свойствам.