from model_ori1 import resnet34 # model_ori1:your model
import torch
import torch.nn as nn
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
model = resnet34()
self.resnet = model
def forward(self, img):
out = self.resnet(img)
# print('out is {}'.format(out))
return out
model = Net().cuda()
for name, param in model.named_parameters(): # 查看可优化的参数有哪些
if param.requires_grad:
print(name)
# 冻结某个参数预训练
from ResNeSt.resnest.torch.resnest import resnest101,resnest200,resnest269
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
model = resnest269(pretrained=True)
model.fc = nn.Linear(2048,102)
self.resnet = model
def forward(self, img):
out = self.resnet(img)
# print('out is {}'.format(out))
return out
model = Net().cuda()
for name, param in model.named_parameters(): # 查看可优化的参数有哪些
if param.requires_grad:
print(name)
新model == net:
读取旧pt,将其layer,name参数给新model中的
model = resnet34()
# inchannel = model.fc.in_features
# print("fad",inchannel)
# model.fc = nn.Linear(inchannel, 5)
# m1,m = model.load_state_dict(torch.load("./rep2.pt"),strict= False)