0
$\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$

closed as unclear what you're asking by Stephen Rauch, Sean Owen Nov 3 '18 at 21:50

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\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$ – Thomas Cleberg Oct 31 '18 at 13:12
5
$\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$
  • $\begingroup$ Any links or examples on doing that? $\endgroup$ – n1k31t4 Oct 31 '18 at 14:15
  • $\begingroup$ @n1k31t4 I edited my answer $\endgroup$ – keiv.fly Oct 31 '18 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 Oct 31 '18 at 15:05
  • 1
    $\begingroup$ @n1k31t4 I included the actual assignment of the weights to pytorch layers. $\endgroup$ – keiv.fly Oct 31 '18 at 15:13

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