1

我正在使用 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()

def load_model(): 返回 BaseModel()

4

2 回答 2

1

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

虽然更好的方法是在self.fc = Linear(in_features = 1280, out_feaures = 512)里面添加__init__x=self.fc(x)像你一样使用。

于 2021-12-05T22:45:26.247 回答
0

你的函数有self.fc()问题def forward

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()
于 2021-12-05T22:59:20.837 回答