gmalivenko / pytorch2keras

PyTorch to Keras model convertor
https://pytorch2keras.readthedocs.io/en/latest/
MIT License
858 stars 143 forks source link

Weight data has no target variable for Resnet18 #102

Open kdmcdrm opened 4 years ago

kdmcdrm commented 4 years ago

Describe the bug Trying to convert a siamese Resnet18 model from Pytorch to Tensorflow but I get the following error in tf.loadModel

tfjs@latest:2 Uncaught (in promise) Error: Provided weight data has no target variable: output_0_1/kernel

I tried modifying the names of these layers manually in the model.json file, which corrects this loading issue, but in that case I found the model output for an input of all ones differed between the raw and converted models. I'm not sure if this second part is related or a separate issue.

To Reproduce Here's the network, pretty simple.

class SiameseResnet18(nn.Module):
    """
    A siamese network that is based on ResNet

    """
    def __init__(self, params):
        """
        Args:
            params: Must have "multi_gpu" key
        """
        super().__init__()
        self.params = params
        self.cnn1 = models.resnet18()

        self.cnn1.fc = nn.Linear(512, 512)
        # ToDo: Add some more fully connected layers with dropout
        if params["multi_gpu"]:
            self.cnn1 = nn.DataParallel(self.cnn1)

    def forward_once(self, x):
        output = self.cnn1(x)
        return output

    def forward(self, input1, input2):
        output1 = self.forward_once(input1)
        output2 = self.forward_once(input2)
        return output1, output2

Expected behavior No loading error.

Logs

Environment (please complete the following information):

kdmcdrm commented 4 years ago

Follow up on this, the difference in output values was because I forgot to put the model in evaluate mode in PyTorch (stupid!). The main issue outlined here is still present, I had to rename the output layers to allow the model to load.