【问题标题】:Typeerror: 1 positional argument but 2 were given, torch.nn.linear takes类型错误:1 个位置参数,但给出了 2 个,torch.nn.linear 需要
【发布时间】:2023-03-04 14:54:01
【问题描述】:

我正在使用 EfficientNet 提取特征,并尝试在预训练模型中添加一个全连接层,以将 out-features 的维度从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

以下代码显示了我如何定义 Class BaseModel(object)。

非常感谢您。

类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()

定义加载模型(): 返回 BaseModel()

【问题讨论】:

    标签: python forward efficientnet


    【解决方案1】:

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

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

    【讨论】:

    • 非常感谢。这种方法对我有用。
    【解决方案2】:

    您的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()
    

    【讨论】:

    • 非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2021-12-01
    • 2021-10-19
    • 2020-03-08
    • 2021-01-08
    • 1970-01-01
    • 2017-02-06
    • 2020-12-29
    • 2019-06-09
    相关资源
    最近更新 更多