6
$\begingroup$

I performed transfer learning using ssd + mobilenet as my base model in tensorflow and freezed a new model. Now I want to convert that model into pytorch. Is there any way how I can achieve it? Any help would be really helpful..

$\endgroup$
1
  • $\begingroup$ Are you looking for a tool or program that does this for you, or some sort of mapping between tensorflow operations and pytorch operations? $\endgroup$ Commented Oct 31, 2018 at 13:12

1 Answer 1

8
$\begingroup$

You can build the same model in pytorch. Then extract weights from tensorflow and assign them manually to each layer in pytorch. Depending on the amount of layers it could be time consuming. Building the model depends on the model and I think not everything is possible in pytorch that is possible in tensorflow. Examples how to assign weights in pytorch and extract weights from tensorflow are given below.

Getting weights from tensorflow variables W and b:

weights = sess.run(W)
bias = sess.run(b)

where sess is a tf.Session.

Assigning weights to pytorch:

import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F

class Kernel_Emb(nn.Module):

    def __init__(self,D_in,H,D_out):
        super(Kernel_Emb, self).__init__()
        self.linear1 = nn.Linear(D_in,H)
        self.linear2 = nn.Linear(H,D_out)
        self.linear1.weight = torch.nn.Parameter(weights)
        self.linear1.bias = torch.nn.Parameter(bias)

If the variable is not defined in tensorflow (source: https://stackoverflow.com/questions/36193553/get-the-value-of-some-weights-in-a-model-trained-by-tensorflow):

If you do not currently have a pointer to the tf.Variable, you can get a list of the trainable variables in the current graph by calling [tf.trainable_variables()][4]. This function returns a list of all trainable tf.Variable objects in the current graph, and you can select the one that you want by matching the v.name property. For example:

# Desired variable is called "tower_2/filter:0".
var = [v for v in tf.trainable_variables() if v.name == "tower_2/filter:0"][0]
$\endgroup$
5
  • $\begingroup$ Any links or examples on doing that? $\endgroup$
    – n1k31t4
    Commented Oct 31, 2018 at 14:15
  • $\begingroup$ @n1k31t4 I edited my answer $\endgroup$
    – keiv.fly
    Commented Oct 31, 2018 at 14:27
  • $\begingroup$ Thanks for the update - it is starting to make good sense. Could you show how to actually assign the tensorflow weights to the pytorch layers? I think that is an important step to include. $\endgroup$
    – n1k31t4
    Commented Oct 31, 2018 at 15:05
  • 1
    $\begingroup$ @n1k31t4 I included the actual assignment of the weights to pytorch layers. $\endgroup$
    – keiv.fly
    Commented Oct 31, 2018 at 15:13
  • $\begingroup$ datascience.stackexchange.com/questions/94071/… Hi, could you take a look here? $\endgroup$
    – x89
    Commented May 6, 2021 at 22:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.