In Python, getting size of images tensorflow2 model trained on?

Viewed 58

I am loading a model using:

m = tf.saved_model.load(str(model_path))

I want to be able to get the information for the size of the images the model was trained on so I can resize the new images that I want to inference.

I know with a keras model I can use :

shape_0 = m.layers[0].output_shape
input_height = shape_0[1]
input_width= shape_0[2]

to get the input height and width of the training images.

Are there similar commands for getting these values from a tensorflow2 model?

1 Answers

Use model.signatures['serving_default']:

import tensorflow as tf

model = tf.keras.Sequential([
  tf.keras.layers.Conv2D(16, 3, padding='same', activation='relu', input_shape=(128, 128, 3)),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(32, 3, padding='same', activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Conv2D(64, 3, padding='same', activation='relu'),
  tf.keras.layers.MaxPooling2D(),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dense(5)
])
tf.saved_model.save(model, '/content/model')
imported_model = tf.saved_model.load('/content/model')

print(imported_model.signatures['serving_default'])
INFO:tensorflow:Assets written to: /content/model/assets
ConcreteFunction signature_wrapper(*, conv2d_3_input)
  Args:
    conv2d_3_input: float32 Tensor, shape=(None, 128, 128, 3)
  Returns:
    {'dense_4': <1>}
      <1>: float32 Tensor, shape=(None, 5)
Related