1
votes

At the moment i apply all preprocessing to the dataset. But i saw that i can make the preprocessing as part of the model. I read that the layer preprocessing is inactive at test time but what is about the rezizing layer? For example:

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

What happens if i now use model.predict(img), will the img automatically be resized or do i have still to rezize the img before the prediction?

Thank you in advance!

1

1 Answers

1
votes

Only the preprocessing layers starting with Random are disabled at evaluation/test time.

In your case, the layers Resizing and Rescaling will be enabled in every case.

You can check in the source code whether or not the layer you are interested takes a training boolean argument in its method call, and use that boolean in a control_flow_util.smart_cond.

For example, the layer Resizing does not :

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

While the layer RandomFlip does :

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