我正在嘗試TFRecords為 CycleGAN 模型創建自定義資料集。該模型需要一種不可用的新型資料集,因此我需要創建一個。我有一些 256x256 的 JPG 影像。按照
uj5u.com熱心網友回復:
您的代碼中有幾個問題:
引數問題:
問題出在函式中convert_to,更詳細地說,該函式需要一個影像串列:
(...)
image = images[index]
(...)
但是,您傳遞的是單個影像,
convert_to(img_data, "data/cat_image_tfrecords", tfrec_name "_cat_image")
因此,稍后image它的形狀(例如)224, 3是無效的影像形狀。
要解決此問題,請更改convert_to為接受單個影像。
序列化問題
Skimage.tobytes似乎不兼容。考慮使用tf.io.encode_jpeg(image).numpy()獲取影像位元組。
完整代碼
我能夠使用以下代碼保存和讀取示例影像:
# Saving
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
# images input
def convert_to(image, output_directory, name):
rows = image.shape[0]
cols = image.shape[1]
depth = 1
filename = os.path.join(output_directory, name '.tfrecords')
print('Writing', filename)
writer = tf.compat.v1.python_io.TFRecordWriter(filename)
print(image.shape)
image_raw = tf.io.encode_jpeg(image).numpy()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(rows),
'width': _int64_feature(cols),
'depth': _int64_feature(depth),
'image_raw': _bytes_feature(image_raw)}))
writer.write(example.SerializeToString())
def read_image(file_name, images_path):
image = skimage.io.imread(images_path file_name)
return image
def get_name(img_name):
remove_ext = img_name.split(".")[0]
name = remove_ext.split("_")
return name[0]
images_path = "data/train/"
image_list = os.listdir(images_path)
for img_name in tqdm(image_list):
tfrec_name = get_name(img_name)
print(tfrec_name)
img_data = read_image(img_name, images_path)
convert_to(img_data, "data/cat_image_tfrecords", tfrec_name "_cat_image")
# Loading:
PHOTO_FILENAMES = tf.io.gfile.glob(str('data/cat_image_tfrecords/*.tfrecords'))
IMAGE_SIZE = [256, 256]
def decode_image(image):
image = tf.image.decode_jpeg(image, channels=3)
image = (tf.cast(image, tf.float32) / 127.5) - 1
# Changed this from reshape
# Consider reshape if all your images have the same shape
image = tf.image.resize(image, IMAGE_SIZE)
return image
def read_tfrecord(example):
tfrecord_format = {
'height': tf.io.FixedLenFeature([], tf.int64),
'width': tf.io.FixedLenFeature([], tf.int64),
'depth': tf.io.FixedLenFeature([], tf.int64),
'image_raw': tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrecord_format)
image = decode_image(example['image_raw'])
return image
def load_dataset(filenames, labeled=True, ordered=False):
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(read_tfrecord, num_parallel_calls=tf.data.AUTOTUNE)
return dataset
photo_ds = load_dataset(PHOTO_FILENAMES, labeled=False).batch(1)
example_photo = next(iter(photo_ds))
uj5u.com熱心網友回復:
我更新了read_tfrecord如下所示的函式,查看注釋掉的行并解決了這個特定的錯誤。
def read_tfrecord(example):
tfrecord_format = {
'height': tf.io.FixedLenFeature([], tf.int64),
'width': tf.io.FixedLenFeature([], tf.int64),
'depth': tf.io.FixedLenFeature([], tf.int64),
'image_raw': tf.io.FixedLenFeature([], tf.string),
}
example = tf.io.parse_single_example(example, tfrecord_format)
#image = decode_image(example['image_raw'])
image = tf.io.decode_raw(example['image_raw'], tf.float32)
return image
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/403779.html
標籤:
