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..
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.
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
weights = sess.run(W) bias = sess.run(b)
sess is a
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()]. This function returns a list of all trainable
tf.Variableobjects in the current graph, and you can select the one that you want by matching the
v.nameproperty. 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"]