迁移学习微调ResNet50

迁移学习微调ResNet50

#更改池化层
class AdaptiveConcatPool2d(nn.Module):
	def __init__(self,size=None):
		super().__init__()
		size = size or (1,1)
		self.pool_one = nn.AdaptiveAvgPool2d(size)
		self.pool_two = nn.AdaptiveAvgPool2d(size)
	def forward(self,x):
		return torch.cat([self.pool_one(x),self.pool_two(x),1])
		

def ResNet50():
	model= torchvisons.models.resnet50(pretrained = True)
	for param in model.parameters():
		  param.requires_grad = Flase
 	model.avgpool = AdaptiveConcatPool2d()
	model.fc = nn.Sequential(
		nn.Flatten(),
		nn.BatchNorm1d(4096),
		nn.Dropout(0.5),
		nn.Linear(4096,512),
		nn.ReLU(),
		nn.BatchNorm1d(512),
		nn.Linear(512,2),
		nn.LogSoftmax(dim=1)
	)

你可能感兴趣的:(笔记,python,算法,人工智能)