gpt4 book ai didi

python - 为图像数据集打乱补丁的更好方法 - tf.data 输入管道

转载 作者:太空宇宙 更新时间:2023-11-04 04:26:15 26 4
gpt4 key购买 nike

我有一个包含 4644 张彩色图像的图像数据集,我将其重新整形为大小为 50 x 50 的 block 并传递到我的深度神经网络。

生成的补丁总数为 369765。我正在使用 tf.data 输入管道进行 patches_generation。

我的问题是如何在传递到网络之前有效地打乱补丁。

在传递到网络之前,洗牌操作中的缓冲区大小 = 10000 是否足够,或者是否有任何其他有效的方法可以在 369765 个补丁之间洗牌?

我遵循的步骤:1. 创建了一个存储所有 4644 张图像的 tf-record。2. 使用 tf.data 管道解码每个图像并从中创建补丁。3. 每 10000 个补丁打乱并传递给网络。

这是我正在使用的代码: 我正在使用 buffer_size=10000,parallel_calls=4

dataset = (tf.data.TFRecordDataset( tfrecords_filename_image )
.repeat( no_epochs )
.map( read_and_decode, num_parallel_calls=num_parallel_calls )

.map( get_patches_fn, num_parallel_calls=num_parallel_calls )

.apply( tf.data.experimental.unbatch()) # unbatch the patches we just produced

.shuffle( buffer_size=buffer_size, seed=random_number_1 )
.batch( batch_size )
.prefetch( 1 )
)
get_patches_function definition:

get_patches_fn = lambda image: get_patches( image, patch_size=patch_size )

def get_patches( image, patch_size=16 ):
# Function to compute patches for given image
# Input- image - Image which has to be converted to patches
# patch_size- size of each patch
# Output-patches of image(4d Tensor)
# with tf.device('/cpu:0'):
pad = [ [ 0, 0 ], [ 0, 0 ] ]
patches_image = tf.space_to_batch_nd( [ image ], [ patch_size, patch_size ], pad )
patches_image = tf.split( patches_image, patch_size * patch_size, 0 )
patches_image = tf.stack( patches_image, 3 )
patches_image = tf.reshape( patches_image, [ -1, patch_size, patch_size, 3 ] )
)
return patches_image

read and decode function definition:

def read_and_decode( tf_record_file ):
# Function to read the tensorflow record and return image suitable for patching
# Input: tf_record_file - tf record file in which image can be extracted
# Output: Image

features = {
'height': tf.FixedLenFeature( [ ], tf.int64 ),
'width': tf.FixedLenFeature( [ ], tf.int64 ),
'image_raw': tf.FixedLenFeature( [ ], tf.string )
}
parsed = tf.parse_single_example( tf_record_file, features )
image = tf.decode_raw( parsed[ 'image_raw' ], tf.uint8 )
height = tf.cast( parsed[ 'height' ], tf.int32 )
width = tf.cast( parsed[ 'width' ], tf.int32 )
image_shape = tf.stack( [ height, width, -1 ] )
image = tf.reshape( image, image_shape )
image = image[ :, :, :3 ]
image = tf.cast( image, tf.float32 )

return image

还请建议是否为每个图像创建单独的 tf-records 而不是为所有图像创建一个 tf-record 更好。

提前致谢。

最佳答案

鉴于您拥有的图像数量,所有图像的单个 tf-record 文件可能就足够了。如果您有多个磁盘,您可以尝试将文件拆分为每个磁盘一个文件以获得更高的吞吐量,但我认为这不会显着降低您的管道大小。

关于洗牌缓冲区大小,这是一个经验问题。与数据集一样大的洗牌缓冲区将为您提供真正的 IID 采样;较小的洗牌缓冲区将接近它。通常随机性越高越好,但在一定程度上,所以我建议尝试几种不同的缓冲区大小(假设您没有适合整个数据集的缓冲区),然后看看哪种适合您。

关于python - 为图像数据集打乱补丁的更好方法 - tf.data 输入管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53456668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com