TransWikia.com

Error when trying Transfer Learning

Data Science Asked by IE Irodov on January 18, 2021

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 

~AppDataRoamingPythonPython37site-packagestensorflow_corepythontrainingtrackingbase.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

~AppDataRoamingPythonPython37site-packagestensorflow_corepythonkerasenginesequential.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

~AppDataRoamingPythonPython37site-packagestensorflow_corepythontrainingtrackingbase.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

~AppDataRoamingPythonPython37site-packagestensorflow_corepythonkerasenginesequential.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 

~AppDataRoamingPythonPython37site-packagestensorflow_corepythonkerasenginebase_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.

~AppDataRoamingPythonPython37site-packagestensorflow_corepythonautographimplapi.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:Anacondalibsite-packagestensorflow_hubkeras_layer.py:216 call  *
        result = smart_cond.smart_cond(training,
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythonframeworksmart_cond.py:56 smart_cond
        return false_fn()
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythonsaved_modelload.py:436 _call_attribute
        return instance.__call__(*args, **kwargs)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythoneagerdef_function.py:457 __call__
        result = self._call(*args, **kwds)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythoneagerdef_function.py:494 _call
        results = self._stateful_fn(*args, **kwds)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythoneagerfunction.py:1822 __call__
        graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythoneagerfunction.py:2150 _maybe_define_function
        graph_function = self._create_graph_function(args, kwargs)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythoneagerfunction.py:2041 _create_graph_function
        capture_by_value=self._capture_by_value),
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythonframeworkfunc_graph.py:915 func_graph_from_py_func
        func_outputs = python_func(*func_args, **func_kwargs)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythoneagerdef_function.py:358 wrapped_fn
        return weak_wrapped_fn().__wrapped__(*args, **kwds)
    C:UsersSagar MishraAppDataRoamingPythonPython37site-packagestensorflow_corepythonsaved_modelfunction_deserialization.py:262 restored_function_body
        "nn".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?

One Answer

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)

Answered by Thad Hughes on January 18, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP