Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

module.save() Error 'TypeError: this __dict__ descriptor does not support '_DictWrapper' objects'

I'm following a tutorial on Youtube for image classification with machine learning. I keep getting the error as shown in the title from trying to save a deep learning module with tensorflow.

I'm using PyCharm on Windows 11.

here is my code...

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow import keras as ker

(training_images, training_labels), (testing_images, testing_labels) = ker.datasets.cifar10.load_data()
testing_images, testing_images = training_images / 255, testing_images / 255

class_names = ['Plane', 'Car', 'Bird', 'Cat', 'Deer', 'Dog', 'Frog', 'Horse', 'Ship', 'Truck']

for i in range(16):
    plt.subplot(4, 4, i+1)
    plt.xticks([])
    plt.yticks([])
    plt.imshow(training_images[i], cmap=plt.cm.binary)
    plt.xlabel(class_names[training_labels[i][0]])

plt.show()

training_images = training_images[:20000]
training_labels = training_labels[:20000]
testing_images = testing_images[:4000]
testing_labels = testing_labels[:4000]

#Model
model = ker.models.Sequential()
model.add(ker.layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)))
model.add(ker.layers.MaxPooling2D((2,2)))
model.add(ker.layers.Conv2D(64, (3,3), activation='relu'))
model.add(ker.layers.MaxPooling2D((2,2)))
model.add(ker.layers.Conv2D(64, (3,3), activation='relu'))
model.add(ker.layers.Flatten())
model.add(ker.layers.Dense(64, activation='relu'))
model.add(ker.layers.Dense(10, activation='softmax'))

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(training_images, training_labels, epochs=1, validation_data=(testing_images, testing_labels))

loss, accuracy = model.evaluate(testing_images, testing_labels)
print(f"Loss: {loss}")
print(f"Accuracy: {accuracy}")

model.save('img_classifier.model')

#model = ker.models.load_model()

the output...

C:\img_classifier\venv\Scripts\python.exe 
C:\img_classifier\main.py 
625/625 [==============================] - 15s 23ms/step - loss: 2.1824 - accuracy: 0.2874 - val_loss: 2.3014 - val_accuracy: 0.1047
125/125 [==============================] - 1s 8ms/step - loss: 2.3014 - accuracy: 0.1047
Loss: 2.30141019821167
Accuracy: 0.10474999994039536
WARNING:absl:Found untraced functions such as _jit_compiled_convolution_op, _jit_compiled_convolution_op, _jit_compiled_convolution_op, _update_step_xla while saving (showing 4 of 4). These functions will not be directly callable after loading.
Traceback (most recent call last):
  File "C:\Users\{user}\PycharmProjects\img_classifier\main.py", line 47, in <module>
    model.save('img_classifier.model')
  File "C:\Users\{user}\AppData\Roaming\Python\Python311\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\{user}\AppData\Roaming\Python\Python311\site-packages\tensorflow\python\trackable\data_structures.py", line 823, in __getattribute__
    return super().__getattribute__(name)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: this __dict__ descriptor does not support '_DictWrapper' objects

Process finished with exit code 1

I've tried changing IDE's, originally I was using VSCode but had way more errors, then I switched to PyCharm and this is the only error that persists. As you can see, the code for training the module has no issues, but I cannot save the module afterwards.

How can I fix this??

like image 860
benCombe Avatar asked Jun 14 '26 09:06

benCombe


2 Answers

could you check which version of the package wrapt is installed?
I had the same issue using tf2onnx for model optimizing. The problem came out of no where, as the same code worked only days before.
I pinned the problem down to one unfrozen dependency (I think in tensorflow): You should be able to solve the problem by installing wrapt in version 1.14.1, while tensorflow would install the most recent version 1.15.0 by default.

pip install wrapt==1.14.1

I'd be happy to hear if i could help you.

As an alternative solution you can set the environment variable WRAPT_DISABLE_EXTENSIONS to true (before the wrapt module is imported). This is from the wrapt developer in an issue on the wrapt github repo.
There are actually several users having issues with this. On the tensorflow repo this issue is about your error and the solution so far is to add wrapt<1.15 as requirement in the current tf nightlys.

Background:

I am using tf2onnx in a Databricks environment to optimize tf models by converting them to onnx. Had the same issue with the following requirements:

  • opencv-python==4.7.0.68
  • segmentation-models==1.0.1
  • tensorflow==2.7.4
  • numpy ==1.24.2
  • tensorflow-addons==0.15.0
  • pandas==1.3.5
  • Pillow==9.4.0
  • scikit-image==0.18.3
  • tqdm==4.62.3
  • fire==0.5.0
  • tf2onnx==1.12.1
File "/databricks/conda/envs/mlflow-.../lib/python3.8/site-packages/tensorflow/python/training/tracking/data_structures.py", line 828, in __getattribute__
        return super(_DictWrapper, self).__getattribute__(name)
    TypeError: this __dict__ descriptor does not support '_DictWrapper' objects

By adding the requirement wrapt==1.14.1 I was able to solve this issue:

wrapt==1.14.1
like image 162
Julian Avatar answered Jun 17 '26 00:06

Julian


I tried many different combinations of Python, TensorFlow, and wrapt versions, all of them resulted in the same error. However, I did find this conversation on the related GitHub issue for wrapt.

Where the suggested fix is to set the following environment variable:

WRAPT_DISABLE_EXTENSIONS=true

it works for me when calling the script like this:

WRAPT_DISABLE_EXTENSIONS=true python example.py

I'm running Python 3.12.4, TensorFLow 2.17.0, wrapt 1.14.1.

like image 36
shtryler Avatar answered Jun 17 '26 00:06

shtryler



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!