使用预处理函数更改ImageDataGenerator上输入的大小

2024-09-29 22:01:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我希望对使用ImageDataGenerator加载的输入数据集进行FFT。当我沿着通道维度将FFT复输出的实部和复部叠加在一起时,采用FFT将使通道数量加倍。ImageDataGenerator类的preprocessing_function属性应该输出一个与输入形状相同的Numpy张量,所以我不能使用它。 我尝试直接在ImageDataGenerator.flow_from_directory()输出上应用tf.math.fft2d,但它消耗了太多RAM,导致程序在Google colab上崩溃。我尝试的另一种方法是添加一个自定义层来计算FFT,作为神经网络的第一层,但这会增加训练时间。因此,我希望将其作为预处理步骤。 谁能建议一种有效的方法在ImageDataGenerator上应用函数


Tags: 数据方法fromfftnumpy数量属性function
1条回答
网友
1楼 · 发布于 2024-09-29 22:01:02

您可以自定义ImageDataGenerator,但我没有理由认为这比在第一层中使用它快。这似乎是一个代价高昂的操作,因为^{}需要complex64complex128数据类型。所以它需要转换,然后再转换回来,因为神经网络权重是tf.float32,而其他图像处理函数不采用complex数据类型

import tensorflow as tf

labels = ['Cats', 'Dogs', 'Others']

def read_image(file_name):
  image = tf.io.read_file(file_name)
  image = tf.image.decode_jpeg(image, channels=3)
  image = tf.image.convert_image_dtype(image, tf.float32)
  image = tf.image.resize_with_pad(image, target_height=224, target_width=224)
  image = tf.cast(image, tf.complex64)
  image = tf.signal.fft2d(image)
  label = tf.strings.split(file_name, '\\')[-2]
  label = tf.where(tf.equal(label, labels))
  return image, label

ds = tf.data.Dataset.list_files(r'path\to\my\pictures\*\*.jpg')

ds = ds.map(read_image)

next(iter(ds))

相关问题 更多 >

    热门问题