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
SimCLR Contrastive model save gives 'EagerTensor is not JSON serializable' #239
Comments
tf.keras.losses.serialize(contrastive_model.loss) seems to be the object that contains a tf.Tensor that's not JSON serializable:
Potential fix:
Probably my TF understanding not deep enough, but there seems to be a difference in how self.temperature and self.margin are treated. self.margin is a float without any tf.constant casting like the temperature, but both are involved in the math op in this loss. Not sure if this is done for performant reason. I will try this alteration locally, and see if there's any problem downstream. |
You're right, I don't think we actually need to convert the temperature to a tensor as there isn't any performance difference. I'll change the temperature to be a normal python float. I also double checked the SimSiam and Barlow loss, and they seem to serialize properly. |
Just pushed a fix in b375635. It will be pushed to pypi as 0.15.5 shortly. |
Hi, I got the same error in the latest version. How to reproduce Env versions OS: Ubuntu 20.04.4 LTS
Errors
|
Potential fix: The margin is of type 'loss': {'class_name': 'Similarity>SimSiamLoss',
'config':
{'reduction': 'auto',
'name': 'simsiam',
'projection_type': 'cosine_distance',
'margin': <tf.Tensor: shape=(1,), dtype=float32, numpy=array([0.001], dtype=float32)>}} I manually edited the file simsiam.py to cast it to def get_config(self) -> Dict[str, Any]:
config = {
"projection_type": self.projection_type,
# "margin": self.margin,
"margin": float(self.margin), # Make it JSON serializable
}
base_config = super().get_config()
return {**base_config, **config} |
@martin0258, thanks for catching this. I'll push a patch shortly. |
Versions
TensorFlow: 2.8.0
TensorFlow Similarity 0.15.4
How to repro:
Ran unsupervised_hello_world.ipynb in Colab, choose 'simclr' as ALGORITHM. Execute all the cells required to instantiate a contrastive_model. Without training it, just skip to this cell
contrastive_model.save(DATA_PATH / 'models' / f'trained_model')
Errors
The text was updated successfully, but these errors were encountered: