Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in Representative Dataset section of example_keras_post-training_quantization.ipynb using google colab notebook. #1331

Closed
Ahsan-Nadeem-dev opened this issue Jan 16, 2025 · 2 comments · Fixed by #1346
Assignees

Comments

@Ahsan-Nadeem-dev
Copy link

Ahsan-Nadeem-dev commented Jan 16, 2025

Issue Type

Bug

Source

pip (model-compression-toolkit)

MCT Version

2.2.2

OS Platform and Distribution

Google Colab

Python version

3.11.11

Describe the issue

There is a type error when you run the Representative Dataset section of the example_keras_post-training_quantization.ipynb on google colab.

The error seems to be on line dataset = get_dataset(batch_size, shuffle=True) on the batch_size parameter.
Here is the link to the colab tutorial https://colab.research.google.com/github/sony/model_optimization/blob/main/tutorials/notebooks/mct_features_notebooks/keras/example_keras_post-training_quantization.ipynb

Expected behaviour

No response

Code to reproduce the issue

batch_size = 32
n_iter = 10

dataset = get_dataset(batch_size, shuffle=True)

def representative_dataset_gen():
    for _ in range(n_iter):
        yield [dataset.take(1).get_single_element()[0].numpy()]

Log output

Found 50000 files belonging to 1000 classes.

TypeError Traceback (most recent call last)
in <cell line: 0>()
2 n_iter = 10
3
----> 4 dataset = get_dataset(batch_size, shuffle=True)
5
6 def representative_dataset_gen():

29 frames
in get_dataset(batch_size, shuffle)
10 crop_to_aspect_ratio=True,
11 interpolation='bilinear')
---> 12 dataset = dataset.map(lambda x, y: (imagenet_preprocess_input(x, y)), num_parallel_calls=tf.data.AUTOTUNE)
13 dataset = dataset.prefetch(buffer_size=tf.data.AUTOTUNE)
14 return dataset

/usr/local/lib/python3.11/dist-packages/tensorflow/python/data/ops/dataset_ops.py in map(self, map_func, num_parallel_calls, deterministic, name)
2309 # pylint: disable=g-import-not-at-top,protected-access
2310 from tensorflow.python.data.ops import map_op
-> 2311 return map_op._map_v2(
2312 self,
2313 map_func,

/usr/local/lib/python3.11/dist-packages/tensorflow/python/data/ops/map_op.py in _map_v2(input_dataset, map_func, num_parallel_calls, deterministic, name)
38 input_dataset, map_func, preserve_cardinality=True, name=name)
39 else:
---> 40 return _ParallelMapDataset(
41 input_dataset,
42 map_func,

/usr/local/lib/python3.11/dist-packages/tensorflow/python/data/ops/map_op.py in init(self, input_dataset, map_func, num_parallel_calls, deterministic, use_inter_op_parallelism, preserve_cardinality, use_legacy_function, name)
146 self._input_dataset = input_dataset
147 self._use_inter_op_parallelism = use_inter_op_parallelism
--> 148 self._map_func = structured_function.StructuredFunctionWrapper(
149 map_func,
150 self._transformation_name(),

/usr/local/lib/python3.11/dist-packages/tensorflow/python/data/ops/structured_function.py in init(self, func, transformation_name, dataset, input_classes, input_shapes, input_types, input_structure, add_to_graph, use_legacy_function, defun_kwargs)
263 fn_factory = trace_tf_function(defun_kwargs)
264
--> 265 self._function = fn_factory()
266 # There is no graph to add in eager mode.
267 add_to_graph &= not context.executing_eagerly()

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in get_concrete_function(self, *args, **kwargs)
1249 def get_concrete_function(self, *args, **kwargs):
1250 # Implements PolymorphicFunction.get_concrete_function.
-> 1251 concrete = self._get_concrete_function_garbage_collected(*args, **kwargs)
1252 concrete._garbage_collector.release() # pylint: disable=protected-access
1253 return concrete

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in _get_concrete_function_garbage_collected(self, *args, **kwargs)
1219 if self._variable_creation_config is None:
1220 initializers = []
-> 1221 self._initialize(args, kwargs, add_initializers_to=initializers)
1222 self._initialize_uninitialized_variables(initializers)
1223

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in _initialize(self, args, kwds, add_initializers_to)
694 )
695 # Force the definition of the function for these arguments
--> 696 self._concrete_variable_creation_fn = tracing_compilation.trace_function(
697 args, kwds, self._variable_creation_config
698 )

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py in trace_function(args, kwargs, tracing_options)
176 kwargs = {}
177
--> 178 concrete_function = _maybe_define_function(
179 args, kwargs, tracing_options
180 )

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py in _maybe_define_function(args, kwargs, tracing_options)
281 else:
282 target_func_type = lookup_func_type
--> 283 concrete_function = _create_concrete_function(
284 target_func_type, lookup_func_context, func_graph, tracing_options
285 )

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/tracing_compilation.py in _create_concrete_function(function_type, type_context, func_graph, tracing_options)
308 attributes_lib.DISABLE_ACD, False
309 )
--> 310 traced_func_graph = func_graph_module.func_graph_from_py_func(
311 tracing_options.name,
312 tracing_options.python_function,

/usr/local/lib/python3.11/dist-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, create_placeholders)
1057
1058 _, original_func = tf_decorator.unwrap(python_func)
-> 1059 func_outputs = python_func(*func_args, **func_kwargs)
1060
1061 # invariant: func_outputs contains only Tensors, CompositeTensors,

/usr/local/lib/python3.11/dist-packages/tensorflow/python/eager/polymorphic_function/polymorphic_function.py in wrapped_fn(*args, **kwds)
597 # the function a weak reference to itself to avoid a reference cycle.
598 with OptionalXlaContext(compile_with_xla):
--> 599 out = weak_wrapped_fn().wrapped(*args, **kwds)
600 return out
601

/usr/local/lib/python3.11/dist-packages/tensorflow/python/data/ops/structured_function.py in wrapped_fn(*args)
229 # Note: wrapper_helper will apply autograph based on context.
230 def wrapped_fn(*args): # pylint: disable=missing-docstring
--> 231 ret = wrapper_helper(*args)
232 ret = structure.to_tensor_list(self._output_structure, ret)
233 return [ops.convert_to_tensor(t) for t in ret]

/usr/local/lib/python3.11/dist-packages/tensorflow/python/data/ops/structured_function.py in wrapper_helper(*args)
159 if not _should_unpack(nested_args):
160 nested_args = (nested_args,)
--> 161 ret = autograph.tf_convert(self._func, ag_ctx)(*nested_args)
162 ret = variable_utils.convert_variables_to_tensors(ret)
163 if _should_pack(ret):

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
691 except Exception as e: # pylint:disable=broad-except
692 if hasattr(e, 'ag_error_metadata'):
--> 693 raise e.ag_error_metadata.to_exception(e)
694 else:
695 raise

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
688 try:
689 with conversion_ctx:
--> 690 return converted_call(f, args, kwargs, options=options)
691 except Exception as e: # pylint:disable=broad-except
692 if hasattr(e, 'ag_error_metadata'):

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
437 try:
438 if kwargs is not None:
--> 439 result = converted_f(*effective_args, **kwargs)
440 else:
441 result = converted_f(*effective_args)

/tmp/__autograph_generated_fileo0i9fq0c.py in (x, y)
3
4 def inner_factory(ag__):
----> 5 tf__lam = lambda x, y: ag__.with_function_scope(lambda lscope: ag__.converted_call(imagenet_preprocess_input, (x, y), None, lscope), 'lscope', ag__.STD)
6 return tf__lam
7 return inner_factory

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/core/function_wrappers.py in with_function_scope(thunk, scope_name, options)
111 """Inline version of the FunctionScope context manager."""
112 with FunctionScope('lambda_', scope_name, options) as scope:
--> 113 return thunk(scope)

/tmp/__autograph_generated_fileo0i9fq0c.py in (lscope)
3
4 def inner_factory(ag__):
----> 5 tf__lam = lambda x, y: ag__.with_function_scope(lambda lscope: ag__.converted_call(imagenet_preprocess_input, (x, y), None, lscope), 'lscope', ag__.STD)
6 return tf__lam
7 return inner_factory

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
439 result = converted_f(*effective_args, **kwargs)
440 else:
--> 441 result = converted_f(*effective_args)
442 except Exception as e:
443 _attach_error_metadata(e, converted_f)

/tmp/__autograph_generated_file5d8bxaxa.py in tf__imagenet_preprocess_input(images, labels)
10 try:
11 do_return = True
---> 12 retval_ = (ag__.converted_call(ag__.ld(tf).keras.applications.mobilenet_v2.preprocess_input, (ag__.ld(images),), None, fscope), ag__.ld(labels))
13 except:
14 do_return = False

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/impl/api.py in converted_call(f, args, kwargs, caller_fn_scope, options)
375
376 if not options.user_requested and conversion.is_allowlisted(f):
--> 377 return _call_unconverted(f, args, kwargs, options)
378
379 # internal_convert_user_code is for example turned off when issuing a dynamic

/usr/local/lib/python3.11/dist-packages/tensorflow/python/autograph/impl/api.py in _call_unconverted(f, args, kwargs, options, update_cache)
458 if kwargs is not None:
459 return f(*args, **kwargs)
--> 460 return f(*args)
461
462

/usr/local/lib/python3.11/dist-packages/keras/src/applications/mobilenet_v2.py in preprocess_input(x, data_format)
487 @keras_export("keras.applications.mobilenet_v2.preprocess_input")
488 def preprocess_input(x, data_format=None):
--> 489 return imagenet_utils.preprocess_input(
490 x, data_format=data_format, mode="tf"
491 )

/usr/local/lib/python3.11/dist-packages/keras/src/applications/imagenet_utils.py in preprocess_input(x, data_format, mode)
104 return _preprocess_numpy_input(x, data_format=data_format, mode=mode)
105 else:
--> 106 return _preprocess_tensor_input(x, data_format=data_format, mode=mode)
107
108

/usr/local/lib/python3.11/dist-packages/keras/src/applications/imagenet_utils.py in _preprocess_tensor_input(x, data_format, mode)
255
256 if mode == "tf":
--> 257 x /= 127.5
258 x -= 1.0
259 return x

/usr/local/lib/python3.11/dist-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs)
151 except Exception as e:
152 filtered_tb = _process_traceback_frames(e.traceback)
--> 153 raise e.with_traceback(filtered_tb) from None
154 finally:
155 del filtered_tb

/usr/local/lib/python3.11/dist-packages/tensorflow/python/ops/math_ops.py in _truediv_python3(x, y, name)
1416 y_dtype = y.dtype.base_dtype
1417 if x_dtype != y_dtype:
-> 1418 raise TypeError(f"x and y must have the same dtype, "
1419 f"got {x_dtype!r} != {y_dtype!r}.")
1420 try:

TypeError: in user code:

File "<ipython-input-7-52616bdf19df>", line 12, in None  *
    lambda x, y: (imagenet_preprocess_input(x, y))
File "<ipython-input-7-52616bdf19df>", line 2, in imagenet_preprocess_input  *
    return tf.keras.applications.mobilenet_v2.preprocess_input(images), labels
File "/usr/local/lib/python3.11/dist-packages/keras/src/applications/mobilenet_v2.py", line 489, in preprocess_input  **
    return imagenet_utils.preprocess_input(
File "/usr/local/lib/python3.11/dist-packages/keras/src/applications/imagenet_utils.py", line 106, in preprocess_input
    return _preprocess_tensor_input(x, data_format=data_format, mode=mode)
File "/usr/local/lib/python3.11/dist-packages/keras/src/applications/imagenet_utils.py", line 257, in _preprocess_tensor_input
    x /= 127.5

TypeError: `x` and `y` must have the same dtype, got tf.uint8 != tf.float32.
@ofirgo ofirgo self-assigned this Jan 19, 2025
@ofirgo
Copy link
Collaborator

ofirgo commented Jan 19, 2025

Hi @Ahsan-Nadeem-dev ,

Currently, I wasn't able to reproduce your issue, so if you can provide some more details it might help us understand what causing the issue.

Can you please specify:

  • Have you made any changes to the code in the notebook? specifically to the data preprocess section?
  • Can you verify what is the tensorflow version that is installed in the environment that is running the notebook?
  • Do you maybe have ImageNet already located in the path that the script is looking for it? or are you seeing the ImageNet download process when running the notebook?

waiting for your reply,

Ofir

@Ahsan-Nadeem-dev
Copy link
Author

Ahsan-Nadeem-dev commented Jan 22, 2025

Hi @ofirgo ,

Just to confirm.

  • I haven't changed any code in the notebook at all. No not even to the data preprocess section.
  • Hmm, the tensorflow version installed on the notebook says 2.17.1. However, I can see the Setup section of the notebook does try to install tensorflow version 2.14 but that gets the following.

Image

  • No, I do not have the ImageNet already installed in my google colab notebook. Yes, I see the download process when running the notebook.

Image

And yes I still get the same error in my previous comment.

just to reconfirm the google colab notebook link is following.

https://colab.research.google.com/github/sony/model_optimization/blob/main/tutorials/notebooks/mct_features_notebooks/keras/example_keras_post-training_quantization.ipynb

@reuvenperetz reuvenperetz linked a pull request Jan 28, 2025 that will close this issue
9 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants