Could you explain how following layers in my model works? and are these substitutable in PyTorch?
Lambda(lambda x: x.view(x.size(0),-1)), # Reshape,
nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(3072,128)), # Linear,
Details:
I have torch7 model which have following architecture:
model: StyleNet
I converted ‘stylenet.t7’ to ‘stylenet.pth’ by convert_torch_to_pytorch
(GitHub),
then I got following python file:
import torch
import torch.nn as nn
import torch.legacy.nn as lnn
from functools import reduce
from torch.autograd import Variable
class LambdaBase(nn.Sequential):
def __init__(self, fn, *args):
super(LambdaBase, self).__init__(*args)
self.lambda_func = fn
def forward_prepare(self, input):
output = []
for module in self._modules.values():
output.append(module(input))
return output if output else input
class Lambda(LambdaBase):
def forward(self, input):
return self.lambda_func(self.forward_prepare(input))
class LambdaMap(LambdaBase):
def forward(self, input):
return list(map(self.lambda_func,self.forward_prepare(input)))
class LambdaReduce(LambdaBase):
def forward(self, input):
return reduce(self.lambda_func,self.forward_prepare(input))
stylenet = nn.Sequential( # Sequential,
nn.Conv2d(3,64,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Conv2d(64,64,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Dropout(0.25),
nn.MaxPool2d((4, 4),(4, 4)),
nn.BatchNorm2d(64,0.001,0.9,True),
nn.Conv2d(64,128,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Conv2d(128,128,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Dropout(0.25),
nn.MaxPool2d((4, 4),(4, 4)),
nn.BatchNorm2d(128,0.001,0.9,True),
nn.Conv2d(128,256,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Conv2d(256,256,(3, 3),(1, 1),(1, 1)),
nn.ReLU(),
nn.Dropout(0.25),
nn.MaxPool2d((4, 4),(4, 4)),
nn.BatchNorm2d(256,0.001,0.9,True),
nn.Conv2d(256,128,(1, 1)),
nn.ReLU(),
Lambda(lambda x: x.view(x.size(0),-1)), # Reshape,
nn.Sequential(Lambda(lambda x: x.view(1,-1) if 1==len(x.size()) else x ),nn.Linear(3072,128)), # Linear,
)
I’m referring to last two layers.
Thank you for dealing with it.