Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: 'Not JSON Serializable' while doing tf.keras.Model.save and using keras variable in loss_weights in tf.keras.Model.compile

System information   

OS Platform and Distribution: Ubuntu 16.04 LTS  

TensorFlow installed from (source or binary): binary  

TensorFlow version (use command below): 1.12.0  

Python version: 3.5.2  

CUDA/cuDNN version: release 9.0, V9.0.176  

GPU model and memory: Tesla K80, 12 GB  

Describe the current behavior  

When I try to save my model using model.save() where model is a tf.keras.Model instance, it throws a TypeError: ('Not JSON Serializable:', <tf.Variable 'Variable:0' shape=() dtype=float32>) .

I am using a tf.keras.backend.variable() in loss_weights in model.compile.  

Optimizer: tf.keras.optimizers.Adam  

Interestingly, when I try to save my model weights only using model.save_weights where model is a tf.keras.Model instance, it works fine, NO ERROR.

Code to reproduce the issue  

alpha = tf.keras.backend.variable(0.25)

Code any model using tf.keras  

model= get_model() 

model.compile(optimizer=optimizer,loss={"softmax1":generalized_dice_loss,"softmax2":generalized_dice_loss}, loss_weights=[1.0,alpha]) 

Do training using model.fit()-

model.save()

Other info / logs  

Traceback (most recent call last):  

File "main_latest.py", line 45, in 

max_queue_size=10)  

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training.py", line 2177, in fit_generator

initial_epoch=initial_epoch)    

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/training_generator.py", line 216, in fit_generator  

callbacks.on_epoch_end(epoch, epoch_logs)  

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/callbacks.py", line 214, in on_epoch_end

callback.on_epoch_end(epoch, logs)    

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/callbacks.py", line 601, in on_epoch_end

self.model.save(filepath, overwrite=True)  

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/network.py", line 1363, in save

save_model(self, filepath, overwrite, include_optimizer)  

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/keras/engine/saving.py", line 134, in save_model

default=serialization.get_json_type).encode('utf8')  

File "/usr/lib/python3.5/json/init.py", line 237, in dumps

**kw).encode(obj)  

File "/usr/lib/python3.5/json/encoder.py", line 198, in encode

chunks = self.iterencode(o, _one_shot=True)  

File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode

return _iterencode(o, 0)  

File "/home/tejal/.local/lib/python3.5/site-packages/tensorflow/python/util/serialization.py", line 64, in get_json_type  

raise TypeError('Not JSON Serializable:', obj)  

TypeError: ('Not JSON Serializable:', <tf.Variable 'Variable:0' shape=() dtype=float32>)

  

Screenshot of Error: enter image description here

like image 604
tejal567 Avatar asked Nov 07 '22 17:11

tejal567


1 Answers

model.save is trying to save a tf.Variable which is not JSON serializable.

model.fit saves everything, not just the model weights. I've seen this problem when my optimizer had a tf.Tensor which cannot be serialized.

Everything points to alpha being your problem in this case. Also the documentation for model.compile states that the loss_weights should be a Python float.

So using alpha.numpy() should solve your problem:

model.compile(optimizer=optimizer,loss={"softmax1":generalized_dice_loss,"softmax2":generalized_dice_loss}, loss_weights=[1.0,alpha.numpy()])
like image 75
James Hirschorn Avatar answered Nov 15 '22 09:11

James Hirschorn