修改模型的记录
1、想用stepLR:
2、输出中间conv的结果,因为跟公式计算的输出不一样
3、修改模型,取预训练参数的一部分更新
4、heatmap
1、stepLR:
model = SVHN_Model1()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), 0.01)
best_loss = 1000.0
#optimizer_state = optimizer.state_dict()
#optimizer = optim.SGD(model.parameters(), lr=0.5)
lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5,gamma=0.8)
scheduler_state = lr_scheduler.state_dict()
#optimizer.load_state_dict(optimizer_state)
lr_scheduler.load_state_dict(scheduler_state)
use_cuda = False
if use_cuda:
model = model.cuda()
for epoch in range(5):
train_loss = train(train_loader, model, criterion, optimizer)
lr_scheduler.step()#在optimizer.step之后
参考资料:
主要看https://zhuanlan.zhihu.com/p/136902153
https://www.jianshu.com/p/9643cba47655
https://www.cnblogs.com/zf-blog/p/11262906.html
2、模型中间输出:
参考资料:
model的参数中加个output:https://freexyz.cn/dev/20525.html
模型分开定义,return conv的output:(细看实现)https://blog.csdn.net/Hungryof/article/details/80921417
resnet50中间层的可视化输出https://blog.csdn.net/u012435142/article/details/84711978
register_forward_hook ,尝试了,但是没有成功,不知道怎么跟代码结合https://blog.csdn.net/lz739337660/article/details/101290263
有时间要细看,尝试一下:https://stackoverflow.com/questions/58218165/googlenet-object-has-no-attribute-features
想实现这个最下面,cat图的同样效果!https://blog.csdn.net/qq_23944915/article/details/99836021
keras的代码,先mark一下,https://freexyz.cn/dev/20525.html
3、
mobilenet很像baseline;https://github.com/marvis/pytorch-mobilenet/blob/master/main.py
resnet50https://blog.csdn.net/weixin_40548136/article/details/88820996
resnet50迁移学习(看loss曲线)https://blog.csdn.net/heiheiya/article/details/103028543
resnet代码注释https://www.jianshu.com/p/085f4c8256f1
修改resnet的一层或几层https://www.jianshu.com/p/085f4c8256f1
4.
Pytorch 可视化CNN中间层的输出
PyTorch 提供了一个名为register_forward_hook的方法,它允许传入一个可以提取特定层输出的函数。
但使用VGG-16网络和猫狗图像分类,演示如何提取不同层的输出时,出现错误“object has no attribute 'features',
解决方法:
https://blog.csdn.net/qq_43232545/article/details/105493549(还是不行,最终是在这里看到了解决:https://stackoverflow.com/questions/58218165/googlenet-object-has-no-attribute-features--原来是
self.features = nn.Sequential(*list(self.net.children())[:4])取出了模型的feature,即输出特征。
)
解释:features是vgg模型的一个属性,表示模型的输出,在程序中feat=nn.Sequential(*list(self.net.children())[:4])
所以self.features 就是代码里的feat。但怎么在模型外调用呢?【想在module类的forward函数的return增加一项,feat,
以前只有c1,c2,c3,c4。但一直报错,说feat不是合法字符,删掉c1等,就可以了,猜测forward的return是要类型一致么?
仔细看参考文献 1、https://blog.csdn.net/u011501388/article/details/84062483(实例化一个类的时候就可以调用forward,
原来是实例化的时候会调用__call__方法,然后在这个方法里面都调用forward方法)】
5、上面的问题没有解决,记录一下hook函数,后面想画heatmap还要用
网络前向预测后,后向计算梯度,更新梯度后,就会释放中间的这些变量,如果要记录这些中间变量,又会增大内存占用,不是好办法。
解决:hook就是保存中间变量的梯度。
https://blog.csdn.net/manong_wxd/article/details/78720119 hook的三个方法
6、(写out_512=base_out.data 在forward里,输出中间层,注意data若不写,内存不会释放,会memory溢出错误)
https://blog.csdn.net/baidu_38262850/article/details/98379882?utm_medium=distribute.pc_rele
vant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_rele
vant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
7、第一层卷积结果和梯度图
https://blog.csdn.net/raby_gyl/article/details/80010085
8、取部分预训练的参数初始化https://www.cnblogs.com/wmlj/p/9917827.html(特别详细)
https://blog.csdn.net/qq_32998593/article/details/89343507(三种方式)
https://blog.csdn.net/happyday_d/article/details/88974361?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.nonecase
9 heatmap,
想要这里的效果 https://blog.csdn.net/weixin_40500230/article/details/93845890