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

__dict__ error when saving a model using tf.saved_model.save(model, modelSavedPath) #65279

Closed
rafimps18 opened this issue Apr 9, 2024 · 1 comment
Assignees
Labels

Comments

@rafimps18
Copy link

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

source

TensorFlow version

2.16.1

Custom code

No

OS platform and distribution

Windows 10

Mobile device

No response

Python version

3.12.2

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

I was training MobileNetV3 on a custom dataset and tried to save it as a SavedModel because I get errors when converting .keras files to .tflite. So I retrained my model and to save the model I used tf.saved_model.save(model, modelSavedPath). After the code was done, it displayed this error: TypeError: this __dict__ descriptor does not support '_DictWrapper' objects

Standalone code to reproduce the issue

# transfer learning using MobileNet-V3 large 

import tensorflow as tf 
from tensorflow.keras import Model 
from tensorflow.keras.applications import MobileNetV3Large 
from tensorflow.keras.preprocessing.image import ImageDataGenerator 
from tensorflow.keras.layers import Dense, GlobalAveragePooling2D
from tensorflow.keras.optimizers import Adam

trainPath = "C:/Users/User/Desktop/CDD/CDataset/train"
ValidPath = "C:/Users/User/Desktop/CDD/CDataset/validate"

trainGenerator = ImageDataGenerator(
    rotation_range=15 , width_shift_range=0.1,
    height_shift_range=0.1, brightness_range=(0, 0.2)).flow_from_directory(trainPath, target_size=(320,320), batch_size=32)

ValidGenerator = ImageDataGenerator(
    rotation_range=15 , width_shift_range=0.1,
    height_shift_range=0.1, brightness_range=(0, 0.2)).flow_from_directory(ValidPath, target_size=(320,320), batch_size=32)

# Build the model 

baseModel = MobileNetV3Large(weights= "imagenet", include_top=False)

x = baseModel.output
x = GlobalAveragePooling2D()(x)
x = Dense(512, activation='relu')(x)
x = Dense(256, activation='relu')(x)
x = Dense(128, activation='relu')(x)

predictionLayer = Dense(3, activation='softmax')(x)

model = Model(inputs=baseModel.input , outputs=predictionLayer)

print(model.summary())

# freeze the layers of the MobileNetV3 (already trained)

for layer in model.layers[:-5]:
    layer.trainable = False

# Compile
    
optimizer = Adam(learning_rate = 0.0001)
model.compile(loss= "categorical_crossentropy", optimizer=optimizer , metrics=['accuracy','precision','recall'])

# train
model.fit(trainGenerator, validation_data=ValidGenerator, epochs=6)

modelSavedPath = "C:/Users/User/Desktop/CDD/SavedModels"
# model.save(modelSavedPath)
tf.saved_model.save(model, modelSavedPath)

Relevant log output

Traceback (most recent call last):
  File "c:\Users\User\Downloads\thesis\thesis\2BuildModel.py", line 52, in <module>
    tf.saved_model.save(model, modelSavedPath)
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 1392, in save
    save_and_return_nodes(obj, export_dir, signatures, options)
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 1427, in save_and_return_nodes
    _build_meta_graph(obj, signatures, options, meta_graph_def))
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 1642, in _build_meta_graph
    return _build_meta_graph_impl(obj, signatures, options, meta_graph_def)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 1564, in _build_meta_graph_impl
    saveable_view = _SaveableView(augmented_graph_view, options)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 285, in __init__
    checkpoint_util.objects_ids_and_slot_variables_and_paths(
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\util.py", line 160, in 
objects_ids_and_slot_variables_and_paths
    trackable_objects, node_paths = graph_view.breadth_first_traversal()
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\graph_view.py", line 124, in breadth_first_traversal
    return self._breadth_first_traversal()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 156, in _breadth_first_traversal
    super(_AugmentedGraphView, self)._breadth_first_traversal())
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\graph_view.py", line 128, in _breadth_first_traversal
    return super(ObjectGraphView, self)._descendants_with_paths()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\trackable_view.py", line 111, in _descendants_with_paths
    for name, dependency in self.children(current_trackable).items():
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\graph_view.py", line 97, in children
    for name, ref in self.list_children(obj, **kwargs):
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\saved_model\save.py", line 190, in list_children
    for name, child in super(_AugmentedGraphView, self).list_children(
                       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\graph_view.py", line 75, in list_children
    for name, ref in super(ObjectGraphView,
                     ^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\checkpoint\trackable_view.py", line 85, in children
    ref = converter.convert_to_trackable(ref, parent=obj)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\trackable\converter.py", line 31, 
in convert_to_trackable
    if (tensor_util.is_tf_type(obj) and
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\tensorflow\python\framework\tensor_util.py", line 1156, in is_tf_type
    return isinstance(x, tf_type_classes)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\typing.py", line 1871, in __instancecheck__
    val = getattr_static(instance, attr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\inspect.py", line 1839, in getattr_static
    instance_result = _check_instance(obj, attr)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\inspect.py", line 1793, in _check_instance
    instance_dict = object.__getattribute__(obj, "__dict__")
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: this __dict__ descriptor does not support '_DictWrapper' objects
Copy link

Are you satisfied with the resolution of your issue?
Yes
No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants