How can I add my custom method for image preprocessing task in model.Sequential() of Tensorflow?

huangapple go评论68阅读模式
英文:

How can I add my custom method for image preprocessing task in model.Sequential() of Tensorflow?

问题

我已经实现了一个用于执行图像预处理任务的代码。它是一个完全功能的方法,但它一次只能处理一张图片。但我不知道如何使这个函数与tf数据集兼容,因为我使用了OpenCV来执行图像处理任务。还有,我该如何将这个层添加到models.Sequential()中,以满足Sequential()的要求类型?

我想要添加自定义函数的代码如下:

model = models.Sequential([
    resize_and_rescale,
    data_augmentation,
    custom_function_want_to_add_here(),
    layers.Conv2D(32, (3,3,),activation='relu', input_shape=input_shape),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, kernel_size=(3,3,),activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, kernel_size=(3,3,),activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3,),activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3,),activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3,),activation='relu'),
    layers.MaxPooling2D((2,2)),
    layers.Flatten(),
    layers.Dense(64, activation='relu'),
    layers.Dense(n_classes, activation='softmax'),
])
英文:

I have implemented a code to perform image preprocessing tasks. And it is a fully functioning method but it works with one image at a time. But I don't know how to make the function compatible with the tf dataset as I used OpenCV to perform the image processing task. And, how can I add this layer to models.Sequential() to match with the Sequential() requirements type?
The code where I want to add my custom function is -

model = models.Sequential([
    resize_and_rescale,
    data_augmentation,
    custom_function_want_to_add_here(),
    layers.Conv2D(32, (3,3,),activation = 'relu', input_shape = input_shape),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, kernel_size = (3,3,),activation = 'relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, kernel_size = (3,3,),activation = 'relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3,),activation = 'relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3,),activation = 'relu'),
    layers.MaxPooling2D((2,2)),
    layers.Conv2D(64, (3,3,),activation = 'relu'),
    layers.MaxPooling2D((2,2)),
    layers.Flatten(),
    layers.Dense(64, activation = 'relu'),
    layers.Dense(n_classes,activation='softmax'),
])

答案1

得分: 0

以下是翻译好的内容:

两种选择浮现在脑海中:

  1. 将您的自定义函数放入自定义TensorFlow层

    • 这种方法的便利之处在于所有逻辑都保留在模型内部,但是实施可能具有挑战性,因为它需要层逻辑与TensorFlow后端一起工作
  2. 将图像预处理放在模型外部

    • 由于预处理不需要任何训练,您可以将其放在模型外部(这样模型的第一层将是layers.Conv2D(32,...)

由于您的自定义逻辑需要调用OpenCV库中的函数(如果我理解正确),我可能会建议采用第二种方法。与使其在模型内部工作相比,使函数与tf.data.Dataset一起工作会更容易。

英文:

Two options come to mind:

  1. put your custom function to a custom TensorFlow layer

    • the convenience of this method is that all the logic is kept inside the model, however, implementation could be challenging since it would require the layer logic to work with TensorFlow backend
  2. put the image preprocessing outside of the model

    • since the preprocessing does not need any training, you can put it outside the model (so that the first layer of the model would be layers.Conv2D(32, ...))

Since your custom logic requires calling a function from the OpenCV library (if I understood correctly) I would probably recommend the second approach. It will be easier to make the function work with the tf.data.Dataset than it would be to make it work inside the model.

huangapple
  • 本文由 发表于 2023年7月20日 17:23:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728434.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定