Error when trying Transfer Learning

I'm trying to train a model which is an extension of Google's Inception-V3 for the purpose of recognizing and classifying whether there is any pneumonia using x-ray images.

I've used Tensorflow-Hub to get through the transfer-learning part, the code snippet is as follows:

import tensorflow_hub as hub

module_selection = ("inception_v3", 1200, 2048)
handle_base, pixels, FV_SIZE = module_selection
MODULE_HANDLE = "https://tfhub.dev/google/tf2-preview/{}/feature_vector/4".format(handle_base)
IMAGE_SIZE = (pixels, pixels)
print("Using {} with input size {} and output dimension {}".format(MODULE_HANDLE,
                                                                  IMAGE_SIZE, FV_SIZE))



do_fine_tuning = False

feature_extractor = hub.KerasLayer(MODULE_HANDLE,
                                  input_shape = IMAGE_SIZE,
                                  output_shape = [FV_SIZE],
                                  trainable = do_fine_tuning)

model = tf.keras.Sequential([
  feature_extractor,
  tf.keras.layers.Conv2D(16, (5,5), activation='relu'),
  tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides = 2),
  tf.keras.layers.Conv2D(32, (5, 5), activation = 'relu'),
  tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides = 2),
  tf.keras.layers.Conv2D(64, (5, 5), activation = 'relu'),
  tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides = 2),
  tf.keras.layers.Conv2D(128, (5, 5), activation = 'relu'),
  tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides = 2),
  tf.keras.layers.Flatten(),
  tf.keras.layers.Dense(1024, activation = 'relu'),
  tf.keras.layers.Dense(256, activation = 'relu'),
  tf.keras.layers.Dense(1, activation = 'sigmoid') 
])

model.summary()

The error is as follows:

WARNING:tensorflow:Entity tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x00000270F553F348 could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (4 total):
    * Tensor("inputs:0", shape=(None, 1200, 1200), dtype=float32)
    * False
    * False
    * 0.99
  Keyword arguments: {}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * True
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 2:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * True
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 3:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * False
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 4:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * False
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}
WARNING:tensorflow:Entity tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x00000270F553F348 could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (4 total):
    * Tensor("inputs:0", shape=(None, 1200, 1200), dtype=float32)
    * False
    * False
    * 0.99
  Keyword arguments: {}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * True
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 2:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * True
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 3:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * False
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 4:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * False
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}
WARNING: Entity tensorflow.python.saved_model.function_deserialization.RestoredFunction object at 0x00000270F553F348 could not be transformed and will be executed as-is. Please report this to the AutoGraph team. When filing the bug, set the verbosity to 10 (on Linux, `export AUTOGRAPH_VERBOSITY=10`) and attach the full output. Cause: Could not find matching function to call loaded from the SavedModel. Got:
  Positional arguments (4 total):
    * Tensor("inputs:0", shape=(None, 1200, 1200), dtype=float32)
    * False
    * False
    * 0.99
  Keyword arguments: {}

Expected these arguments to match one of the following 4 option(s):

Option 1:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * True
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 2:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * True
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 3:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * False
    * True
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}

Option 4:
  Positional arguments (4 total):
    * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
    * False
    * False
    * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
  Keyword arguments: {}
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
ipython-input-25-a2ea981d199c in module
     19   tf.keras.layers.Dense(1024, activation = 'relu'),
     20   tf.keras.layers.Dense(256, activation = 'relu'),
--- 21   tf.keras.layers.Dense(1, activation = 'sigmoid')
     22 ])
     23 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
-- 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\keras\engine\sequential.py in __init__(self, layers, name)
    112       tf_utils.assert_no_legacy_layers(layers)
    113       for layer in layers:
-- 114         self.add(layer)
    115 
    116   @property

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\training\tracking\base.py in _method_wrapper(self, *args, **kwargs)
    455     self._self_setattr_tracking = False  # pylint: disable=protected-access
    456     try:
-- 457       result = method(self, *args, **kwargs)
    458     finally:
    459       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\keras\engine\sequential.py in add(self, layer)
    176           # and create the node connecting the current layer
    177           # to the input layer we just created.
-- 178           layer(x)
    179           set_inputs = True
    180 

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    840                     not base_layer_utils.is_in_eager_or_tf_function()):
    841                   with auto_control_deps.AutomaticControlDependencies() as acd:
-- 842                     outputs = call_fn(cast_inputs, *args, **kwargs)
    843                     # Wrap Tensors in `outputs` in `tf.identity` to avoid
    844                     # circular dependencies.

~\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\autograph\impl\api.py in wrapper(*args, **kwargs)
    235       except Exception as e:  # pylint:disable=broad-except
    236         if hasattr(e, 'ag_error_metadata'):
-- 237           raise e.ag_error_metadata.to_exception(e)
    238         else:
    239           raise

ValueError: in converted code:

    D:\Anaconda\lib\site-packages\tensorflow_hub\keras_layer.py:216 call  *
        result = smart_cond.smart_cond(training,
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\smart_cond.py:56 smart_cond
        return false_fn()
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\saved_model\load.py:436 _call_attribute
        return instance.__call__(*args, **kwargs)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py:457 __call__
        result = self._call(*args, **kwds)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py:494 _call
        results = self._stateful_fn(*args, **kwds)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\function.py:1822 __call__
        graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\function.py:2150 _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\function.py:2041 _create_graph_function
        capture_by_value=self._capture_by_value),
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\framework\func_graph.py:915 func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\def_function.py:358 wrapped_fn
        return weak_wrapped_fn().__wrapped__(*args, **kwds)
    C:\Users\Sagar Mishra\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\saved_model\function_deserialization.py:262 restored_function_body
        "\n\n".join(signature_descriptions)))

    ValueError: Could not find matching function to call loaded from the SavedModel. Got:
      Positional arguments (4 total):
        * Tensor("inputs:0", shape=(None, 1200, 1200), dtype=float32)
        * False
        * False
        * 0.99
      Keyword arguments: {}

    Expected these arguments to match one of the following 4 option(s):

    Option 1:
      Positional arguments (4 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
        * True
        * False
        * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
      Keyword arguments: {}

    Option 2:
      Positional arguments (4 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
        * True
        * True
        * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
      Keyword arguments: {}

    Option 3:
      Positional arguments (4 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
        * False
        * True
        * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
      Keyword arguments: {}

    Option 4:
      Positional arguments (4 total):
        * TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')
        * False
        * False
        * TensorSpec(shape=(), dtype=tf.float32, name='batch_norm_momentum')
      Keyword arguments: {}

I don't even know where to start solving this error, perhaps the problem is at the layer that is connecting the inception layer and my custom model?

Topic transfer-learning inception keras tensorflow python

Category Data Science


It looks to me like the saved model your loading from tf_hub isn't compatible with the shape you're specifying here:

What you're sending to keras looks like it results in:

Got:
      Positional arguments (4 total):
        * Tensor("inputs:0", shape=(None, 1200, 1200), dtype=float32)

The saved model looks like it's expecting:

TensorSpec(shape=(None, None, None, 3), dtype=tf.float32, name='inputs')

I'm guessing those dimensions are: (batch, height, width, color channels)

What happens if you play around with this line:

input_shape = IMAGE_SIZE

Maybe try:

input_shape = (None, None, 3)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.