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..
1 Answer
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 trainabletf.Variable
objects in the current graph, and you can select the one that you want by matching thev.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]
-
$\begingroup$ Any links or examples on doing that? $\endgroup$– n1k31t4Commented Oct 31, 2018 at 14:15
-
-
$\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$– n1k31t4Commented Oct 31, 2018 at 15:05
-
1$\begingroup$ @n1k31t4 I included the actual assignment of the weights to pytorch layers. $\endgroup$– keiv.flyCommented Oct 31, 2018 at 15:13
-
$\begingroup$ datascience.stackexchange.com/questions/94071/… Hi, could you take a look here? $\endgroup$– x89Commented May 6, 2021 at 22:21