类型错误:提供了1个位置参数,但提供了2个,torch.nn.linear takes



我正在使用EfficientNet来提取特征,并试图在预训练的模型中添加一个完全连接的层,以将输出特征的维度从EfficientNet降低到512。当特征通过我定义的图层或函数时,我遇到了以下错误。

"类型错误:给出了1个位置参数,但给出了2个";

以下是我尝试过的代码:

# define a function to reduce the dimension to 512
# def block_dim_red():
#     block_dim_red = Sequential(OrderedDict([
#         ('fc', Linear(1280, 512)),
#     ]))
#     return block_dim_red
def fc():
fc = Linear(in_features = 1280, out_feaures = 512)
return fc

以下代码显示了我是如何定义类BaseModel(对象(的。

事先非常感谢。

类BaseModel(对象(:

def __init__(self):
self.image_size = 224
self.dimision = 1280
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.load_model()
def load_model(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = EfficientNet.from_pretrained('efficientnet-b0').to(self.device)
self.model = self.model.eval()
self.PIXEL_MEANS = torch.tensor((0.485, 0.456, 0.406)).to(self.device)
self.PIXEL_STDS = torch.tensor((0.229, 0.224, 0.225)).to(self.device)
self.num = torch.tensor(255.0).to(self.device)
def preprocess_input(self, image):
image = cv2.resize(image, (self.image_size, self.image_size))
# gpu version
image_tensor = torch.from_numpy(image.copy()).to(self.device).float()
image_tensor /= self.num
image_tensor -= self.PIXEL_MEANS
image_tensor /= self.PIXEL_STDS
image_tensor = image_tensor.permute(2, 0, 1)
return image_tensor
# define a function to reduce the dimension to 512
# def block_dim_red():
#     block_dim_red = Sequential(OrderedDict([
#         ('fc', Linear(1280, 512)),
#     ]))
#     return block_dim_red
def fc():
fc = Linear(in_features = 1280, out_feaures = 512)
return fc
def forward(self, x):
x = self.preprocess_input(x).unsqueeze(0)
# extraccted feature shape torch.Size([1, 1280, 7, 7])
x = self.model.extract_features(x)
x = F.max_pool2d(x, kernel_size=(7, 7))
x = x.view(x.size(0),-1)
x = torch.reshape(x,(-1,1))
x = self.fc(x) # fully connecte layer to reduce dimension
return self.torch2list(x)

def torch2list(self, torch_data):
return torch_data.cpu().detach().numpy().tolist()

def load_model((:return BaseModel((

请尝试将x = self.fc(x)替换为self.fc()(x),因为self.fc()是一个不需要参数的函数,并且返回线性层,而线性层又需要参数。

尽管更好的方法是在__init__中添加self.fc = Linear(in_features = 1280, out_feaures = 512),并像您所做的那样使用x=self.fc(x)

您的def forward函数中存在self.fc()问题

def __init__(self):
self.image_size = 224
self.dimision = 1280
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.load_model()
def load_model(self):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
self.model = EfficientNet.from_pretrained('efficientnet-b0').to(self.device)
self.model = self.model.eval()
self.PIXEL_MEANS = torch.tensor((0.485, 0.456, 0.406)).to(self.device)
self.PIXEL_STDS = torch.tensor((0.229, 0.224, 0.225)).to(self.device)
self.num = torch.tensor(255.0).to(self.device)
def preprocess_input(self, image):
image = cv2.resize(image, (self.image_size, self.image_size))
# gpu version
image_tensor = torch.from_numpy(image.copy()).to(self.device).float()
image_tensor /= self.num
image_tensor -= self.PIXEL_MEANS
image_tensor /= self.PIXEL_STDS
image_tensor = image_tensor.permute(2, 0, 1)
return image_tensor
# define a function to reduce the dimension to 512
# def block_dim_red():
#     block_dim_red = Sequential(OrderedDict([
#         ('fc', Linear(1280, 512)),
#     ]))
#     return block_dim_red
def fc():
fc = Linear(in_features = 1280, out_feaures = 512)
return fc
def forward(self, x):
x = self.preprocess_input(x).unsqueeze(0)
# extraccted feature shape torch.Size([1, 1280, 7, 7])
x = self.model.extract_features(x)
x = F.max_pool2d(x, kernel_size=(7, 7))
x = x.view(x.size(0),-1)
x = torch.reshape(x,(-1,1))
self.fc()(x)
return self.torch2list(x)

def torch2list(self, torch_data):
return torch_data.cpu().detach().numpy().tolist()
def load_model(): 
return BaseModel()

最新更新