google-research / simclr

SimCLRv2 - Big Self-Supervised Models are Strong Semi-Supervised Learners
https://arxiv.org/abs/2006.10029
Apache License 2.0
4.05k stars 624 forks source link

'_UserObject' object has no attribute 'summary' #196

Open aynesss opened 2 years ago

aynesss commented 2 years ago

Hello, I'm trying to load a r50_1x_sk0/saved_model of pretrained checkpoint-tf2 of simCLRv2, to just fine tuned but I'm coming across this error: '_UserObject' object has no attribute 'summary' I would to apply .summary() .fit() and .predict() Here is a part of my code

def create_model(path):
    baseModel = tf.saved_model.load(path)
    baseModel.summary()   
    model_output = tf.keras.layers.Dense(3, name="head_supervised_new")(baseModel)

    model = tf.keras.Model(inputs=baseModel, outputs=model_output)
    model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=[tf.keras.metrics.CategoricalAccuracy()])  
    return model
loaded_model = create_model("gs://simclr-checkpoints-tf2/simclrv2/pretrained/r50_2x_sk1/saved_model")
loaded_model.fit(training_set)
yangruo1226 commented 2 years ago

Hello, I'm trying to load a r50_1x_sk0/saved_model of pretrained checkpoint-tf2 of simCLRv2, to just fine tuned but I'm coming across this error: '_UserObject' object has no attribute 'summary' I would to apply .summary() .fit() and .predict() Here is a part of my code

def create_model(path):
    baseModel = tf.saved_model.load(path)
    baseModel.summary()   
    model_output = tf.keras.layers.Dense(3, name="head_supervised_new")(baseModel)

    model = tf.keras.Model(inputs=baseModel, outputs=model_output)
    model.compile(optimizer=tf.keras.optimizers.SGD(learning_rate=0.001),
              loss=tf.keras.losses.CategoricalCrossentropy(),
              metrics=[tf.keras.metrics.CategoricalAccuracy()])  
    return model
loaded_model = create_model("gs://simclr-checkpoints-tf2/simclrv2/pretrained/r50_2x_sk1/saved_model")
loaded_model.fit(training_set)

I have the same issue.

ilia10000 commented 2 years ago

I was having the same problem but it turns out loaded_model is a User_Object, the actual model is located at loaded_model.model. So for example try loaded_model.model.summary()

hahahannes commented 2 years ago

I was having the same problem but it turns out loaded_model is a User_Object, the actual model is located at loaded_model.model. So for example try loaded_model.model.summary()

I think this only works with

import tensorflow as tf
model = tf.keras.models.load_model()
model.model.summary()

If you use

model = tf.saved_model.load()

you will get a UserObject again after accessing model.model

hahahannes commented 2 years ago

Did anyone get the model to work with keras methods? I trying to access the layers e.g. the projection_head but get AttributeError: Layer projection_head has no inbound nodes.

after:

import tensorflow as tf
saved_model_path = 'gs://simclr-checkpoints-tf2/simclrv2/pretrained/r50_1x_sk0/saved_model/'
model = tf.keras.models.load_model(saved_model_path).model
model.get_layer('projection_head')