【发布时间】:2022-01-11 16:03:13
【问题描述】:
找到了一个解决方案,把它作为这个问题的答案留在下面:)
项目信息:2 类分类任务。
我正在尝试为我在运行时放入模型的每个图像获取模型的完全连接层的输出。我计划在模型完成训练或测试所有图像后使用它们以使用 UMAP 进行可视化。
型号:
#Load resnet
def get_model():
model = torchvision.models.resnet50(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 2)
return model
pl模块的相关部分:
class classifierModel(pl.LightningModule):
def __init__(self, model):
super().__init__()
self.model = model
self.learning_rate = 0.0001
def training_step(self, batch, batch_idx):
x= batch['image']
y = batch['targets']
x_hat = self.model(x)
output = nn.CrossEntropyLoss()
loss= output(x_hat,y)
return loss
def test_step(self, batch, batch_idx):
x= batch['image']
y = batch['targets']
x_hat = self.model(x)
是否可以通过在 pl 模块的 init 中添加一个空列表,然后在执行 x_hat = model(x) 之后添加输出来做到这一点?
我怎么知道在执行x_hat = model(x) 之后,out_features 是否没有立即删除/丢弃?
【问题讨论】:
-
对不起,您能举个例子详细说明一下吗?说“一些输入”,然后说“当前输出”或“所需输出”?
-
所以基本上我想给 CNN 一些输入 (x)。当前输出是两个分数,每个类别一个。我想要的输出是形状 [1, 2048] 的全连接层的值。
标签: machine-learning deep-learning pytorch pytorch-lightning