PyTorch Lightning model checkpoint loading for Barlow Twins tutorial - pytorch

I am working through the Barlow Twins tutorial with PyTorch Lightning and I am having trouble loading the encoder portion of the model using the checkpoint after training.
During model training, checkpoints are saved with ModelCheckpoint. In the tutorial, the author offers two options for then getting the encoder portion of the model with trained weights: 1) calling model.encoder (model has to have been trained in the active kernel for this to work) or 2) loading the trained model with:
ckpt_model = torch.load('[checkpoint name].ckpt')
And then calling
encoder = ckpt_model.encoder
I would like to be able to load the model/encoder from a saved checkpoint but when I try to do this, I get the error: AttributeError: 'dict' object has no attribute 'encoder'
This seems to make sense to me because the model is being loaded with a simple Torch loader rather than the lightning loader.
When I print the contents of ckpt_model using ckpt_model.keys() I get:
dict_keys(['epoch', 'global_step', 'pytorch-lightning_version', 'state_dict', 'loops', 'callbacks', 'optimizer_states', 'lr_schedulers']). The state_dict contains weights and biases for the encoder layer.
Then when I try loading the model with the PyTorch Lightning loader:
BarlowTwins.load_from_checkpoint('[checkpoint name].ckpt')
I get the error: TypeError: init() missing 4 required positional arguments: 'encoder', 'encoder_out_dim', 'num_training_samples', and 'batch_size'. I might be able to save those in the checkpoint with save_hyperparameters.
My question: how can I load the encoder portion of the model in the simple way possible? I only need the encoder portion of the model loaded.
Thanks so much for help in advance!

Related

Prediction for pretrained model on handwritten text(images)-Pytorch

I have a problem making a prediction using a pre-trained model that contains an encoder and decoder for handwritten text recognition.
What I did is the following:
checkpoint = torch.load("Model/SPAN/SPAN-PT-RA_rimes.pt",map_location=torch.device('cpu'))
encoder_state_dict = checkpoint['encoder_state_dict']
decoder_state_dict = checkpoint['decoder_state_dict']
img = torch.LongTensor(img).unsqueeze(1).to(torch.device('cpu'))
global_pred = decoder_state_dict(encoder_state_dict(img))
This generates this error:
TypeError: 'collections.OrderedDict' object is not callable
I would highly appreciate your help! ^_^
encoder_state_dict and decoder_state_dict are not the torch Models, but a collection (dictionary) of tensors that include pre-trained parameters of the checkpoint you loaded.
Feeding inputs (such as the input image you got transformed) to such collection of tensors does not make sense. In fact, you should use these stat_dicts (i.e., a collection of pre-trained tensors) to load them into the parameters of your model object that is mapped to the network. See torch.nn.Module class.

Loading weights into a keras model from a .pb filed generated by tf.simple_save() in TensorFlow 1.15.2

I am struggling with restoring a keras model from a .pb file. I have seen a couple of posts explaining how to do inference using a model saved in .pb format, but what I need is to load the weights into a keras.Model class.
I have a function that returns an instance of the said original model, but untrained. I want to restore the weights of that model from the .pb file. My goal is to then truncate the model and use the truncated model for other purposes.
So far the furthest I've gotten is using the tf.saved_model.load(session, ['serving'], export_dir) function to get a tensorflow.core.protobuf.meta_graph_pb2.MetaGraphDef object. From here I can access the graph_def attribute, and from there the nodes.
How can I go from that to getting the weights and then loading those into the instance of the untrained keras Model?
Maybe if that's not doable there is a way to "truncate" the graph_def somehow and then make inference using that?

Pytorch image segmentation transfer learning

I am new in Pytorch. My question is: How do I apply transfer learning to a custom dataset? I am doing image segmentation on brain tumors. I can find examples which use U-net structure but I could not find examples using weights of the pre-trained models for a U-net image segmentation?
You could obtain pre-trained models in two ways:
Model weights or complete models shared in formats such .pt or .pth:
In this case, Saving and Loading Models is a good starting point. Copying from the tutorial there, you could load a model as
model = TheModelClass(*args, **kwargs)
model.load_state_dict(torch.load(PATH))
The other way is to load the model from torchvision. A list is available models is available at Torchvision Models. U-Net is not available yet. However, it is possible to load a pre-trained model as the encoder and write a separate decoder to form a U-Net with a pre-trained encoder.
In this case, the model object returned from the function calls shown in the API are already loaded with pretrained weights when pretrained=True.
For writing a custom dataloader, PyTorch data loaders may be a useful guide.

How can I fix "Unknown layer: DenseFeatures" while importing pre-trained keras model?

I was following the tutorial for classifying structured data from here. I trained the model as described with my data which only comprised of numeric data.
I have trained and optimised my model on google colab and downloaded it locally to test it on some new data.
However, when I load the model using this snippet.
from keras.models import load_model
model = load_model("my_model.h5")
I get the following error
Unknown layer: DenseFeatures
...along with the trace.
I have tried setting up custom_objects while loading the model like this
model = load_model("my_model.h5", custom_objects={'DenseFeatures': tf.keras.layers.DenseFeatures})
But I still get the following error:
__init__() takes at least 2 arguments (3 given)
What could I be doing wrong? I tried going through documentation but couldn't find anything of help on github.

Unknown regularizer: l2_cond When trying to load data from file

I have been getting an error when try to load a model that I trained.
model_path = r'I:\\ECGMODELCP\\0.467-0.840-010-0.408-0.860.reg.hdf5'
model = keras.models.load_model(model_path)
ValueError: Unknown regularizer: l2_cond
Ive tried
model = keras.models.load_model(model_path, custom_objects={'l2_cond': l2_cond(weight_matrix)})
But get an error of weight_matrix not defined. l2_cond is a custom kernal regularizer that I defined and depends on the weight matrix of the last layer of my model. Any help is appreciated
I figured it out already. I just loaded the weights of the model into an identical model's architecture.

Resources