【问题标题】:Tensorflow Keras preprocessing layersTensorFlow Keras 预处理层
【发布时间】:2021-02-10 20:43:28
【问题描述】:

目前我将所有预处理应用于数据集。 但我看到我可以将预处理作为模型的一部分。 我读到层预处理在测试时处于非活动状态,但是调整层是什么? 例如:

model = Sequential([
  layers.experimental.preprocessing.Resizing(180, 180),
  layers.experimental.preprocessing.Rescaling(1./255),
  layers.Conv2D(16, 3, padding='same', activation='relu'),
  ...

如果我现在使用 model.predict(img) 会发生什么,img 会自动调整大小还是在预测之前我仍然需要调整 img 大小?

提前谢谢你!

【问题讨论】:

    标签: tensorflow keras


    【解决方案1】:

    只有以Random 开头的预处理层在评估/测试时被禁用。

    在您的情况下,ResizingRescaling 层将在每种情况下启用。

    您可以在源代码中检查您感兴趣的层是否在其方法call 中采用training 布尔参数,并在control_flow_util.smart_cond 中使用该布尔参数。

    例如,Resizing 层没有:

    class Resizing(PreprocessingLayer):
    
        def call(self, inputs):
            outputs = image_ops.resize_images_v2(
            images=inputs,
            size=[self.target_height, self.target_width],
            method=self._interpolation_method)
        return outputs
    

    虽然层 RandomFlip 会:

    class RandomFlip(PreprocessingLayer):
    
      def call(self, inputs, training=True):
        if training is None:
          training = K.learning_phase()
    
        def random_flipped_inputs():
          flipped_outputs = inputs
          if self.horizontal:
            flipped_outputs = image_ops.random_flip_left_right(flipped_outputs,
                                                               self.seed)
          if self.vertical:
            flipped_outputs = image_ops.random_flip_up_down(
                flipped_outputs, self.seed)
          return flipped_outputs
    
        output = control_flow_util.smart_cond(training, random_flipped_inputs,
                                              lambda: inputs)
        output.set_shape(inputs.shape)
        return output
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 2019-11-15
      • 2021-06-03
      • 1970-01-01
      相关资源
      最近更新 更多