I implemented my own version of CaffeNet in PyTorch and I am trying to save the model in an onnx file. But I have the error described in the title when I run this code:
"
import torch.nn as nn
import torch
class Flatten(nn.Module):
def forward(self,x):
return x.view(x.size(0),-1)
class CaffeNet(nn.Module):
def init(self):
super(CaffeNet,self).init()
self.layers=[]
self.layers.append(nn.Conv2d(in_channels=3,out_channels=3,kernel_size=11))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(nn.Conv2d(3,96,55))
self.layers.append(nn.MaxPool2d(kernel_size=2,stride=2))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(nn.Conv2d(96,192,27))
self.layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(nn.Conv2d(192, 288, 13))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(nn.Conv2d(288, 288, 13))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(nn.Conv2d(288, 256, 11)) #12-th layer, normally kernel_size=13
self.layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(nn.Conv2d(256, 256, 3)) #15-th layer, normally kernel_size=13
self.layers.append(nn.MaxPool2d(kernel_size=2, stride=2))
self.layers.append(nn.ZeroPad2d(padding=1))
self.layers.append(Flatten())
self.layers.append(nn.Linear(2304,2304)) #19-th layer, normally 4096 neurons
self.layers.append(nn.ReLU())
self.layers.append(nn.Dropout())
self.layers.append(nn.Linear(2304,2304))
self.layers.append(nn.ReLU())
self.layers.append(nn.Dropout())
self.layers.append(nn.Linear(2304,1000))
self.layers.append(nn.Softmax())
def forward(self, x):
for layer in self.layers:
x=layer(x)
return x
net=CaffeNet()
input = torch.randn(20, 3, 227, 227,requires_grad=False)
input_names = [ “input” ]
output_names = [ “output” ]
torch.onnx.export(net,input,“caffenet.onnx”,verbose=True, input_names=input_names, output_names=output_names)
"
I am using python2.7. The last line of the stack trace is:
“RuntimeError: Cannot insert a Tensor that requires grad as a constant. Consider making it a parameter or input, or detaching the gradient”