【发布时间】:2021-03-04 11:00:54
【问题描述】:
我有一个分类问题来预测 8 个类,例如,我在来自 here 的 pytorch 中使用 EfficientNetB3。但是,我对我的自定义类是否正确编写感到困惑。我想我想剥离预训练模型的最后一层以适应 8 个输出,对吗?我做对了吗?因为当我在我的DataLoader 中打印y_preds = model(images) 时,它似乎给了我1536 的预测。这是预期的行为吗?
!pip install geffnet
import geffnet
class EfficientNet(nn.Module):
def __init__(self, config):
super().__init__()
self.config = config
self.model = geffnet.create_model(config.effnet, pretrained=True)
n_features = self.model.classifier.in_features
# does the name fc matter?
self.fc = nn.Linear(n_features, config.num_classes)
self.model.classifier = nn.Identity()
def extract(self, x):
x = self.model(x)
return x
def forward(self, x):
x = self.extract(x).squeeze(-1).squeeze(-1)
return x
model = EfficientNet(config=config)
if torch.cuda.is_available():
model.cuda()
打印y_pred的示例代码:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
for step, (images, labels) in enumerate(sample_loader):
images = images.to(device)
labels = labels.to(device)
batch_size = images.shape[0]
y_preds = model(images)
print('The predictions of the 4 images is as follows\n', y_preds)
break
【问题讨论】:
标签: machine-learning deep-learning pytorch classification conv-neural-network