更多数学原理小文请关注公众号:未名方略
首先,定义NET,然后训练,然后保存:
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import random
# construct a Unet
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.maxpool = nn.MaxPool2d(2, 2)
self.bn1 = nn.BatchNorm2d(64)
self.bn2 = nn.BatchNorm2d(128)
self.bn3 = nn.BatchNorm2d(256)
self.conv1_1 = nn.Conv2d(1, 64, 3)
self.conv1_2 = nn.Conv2d(64, 64, 3)
self.conv2_1 = nn.Conv2d(64, 128, 3)
self.conv2_2 = nn.Conv2d(128, 128, 3)
self.conv3_1 = nn.Conv2d(128, 256, 3)
self.conv3_2 = nn.Conv2d(256, 256, 3)
self.conv3_1 = nn.Conv2d(128, 256, 3)
self.conv3_2 = nn.Conv2d(256, 256, 3)
self.upconv4 = nn.Conv2d(256, 1, 1)
self.fc2 = nn.Linear(120, 80)
self.fc3 = nn.Linear(80, 40)
self.fc4 = nn.Linear(40, 20)
self.fc5 = nn.Linear(20, 2)
self.m = nn.Softmax()
def forward(self, x):
x = F.relu(self.bn1(self.conv1_2(F.relu(self.conv1_1(x)))))
#print('x1 size: %s'%str(x.size()))
x = F.relu(self.bn2(self.conv2_2(F.relu(self.conv2_1(self.maxpool(x))))))
#print('x2 size: %s'%str(x.size()))
x = F.relu(self.bn3(self.conv3_2(F.relu(self.conv3_1(self.maxpool(x))))))
#print('x3 size: %s'%str(x.size()))
x = F.relu(self.upconv4(self.maxpool(x)))
#print('x4 size: %s'%str(x.size()))
m=x.size(2)
n=x.size(3)
x = x.view(-1, m*n)
#print('x5 size: %s'%str(x.size()))
fc1 = nn.Linear(m*n, 120)
x = F.relu(fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = self.fc5(x)
#print('x size: %s'%str(x.size()))
x = F.softmax(x,dim=1)
return x
net = Net()
#input training data and label
input = torch.Tensor(batch,channel,m,n)
label = torch.Tensor(batch,channel,m,n)
#train the network
criterion = nn.MSELoss()
#criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.99)
for epoch in range(80): # loop over the dataset multiple times
running_loss = 0.0
# zero the parameter gradients
optimizer.zero_grad()
# forward + backward + optimize
outputs = net(x)
loss = criterion(outputs, label)
loss.backward()
optimizer.step()
# print statistics
running_loss += loss.item()
print('[epoch%d] loss: %.3f' %
(epoch + 1,running_loss))
running_loss = 0.0
print('Finished Training')
#save the net
torch.save(net.state_dict(), 'net_parameters.pkl')
重新载入训练好的网络参数
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.maxpool = nn.MaxPool2d(2, 2)
self.bn1 = nn.BatchNorm2d(64)
self.bn2 = nn.BatchNorm2d(128)
self.bn3 = nn.BatchNorm2d(256)
self.conv1_1 = nn.Conv2d(1, 64, 3)
self.conv1_2 = nn.Conv2d(64, 64, 3)
self.conv2_1 = nn.Conv2d(64, 128, 3)
self.conv2_2 = nn.Conv2d(128, 128, 3)
self.conv3_1 = nn.Conv2d(128, 256, 3)
self.conv3_2 = nn.Conv2d(256, 256, 3)
self.conv3_1 = nn.Conv2d(128, 256, 3)
self.conv3_2 = nn.Conv2d(256, 256, 3)
self.upconv4 = nn.Conv2d(256, 1, 1)
self.fc2 = nn.Linear(120, 80)
self.fc3 = nn.Linear(80, 40)
self.fc4 = nn.Linear(40, 20)
self.fc5 = nn.Linear(20, 2)
self.m = nn.Softmax()
def forward(self, x):
x = F.relu(self.bn1(self.conv1_2(F.relu(self.conv1_1(x)))))
#print('x1 size: %s'%str(x.size()))
x = F.relu(self.bn2(self.conv2_2(F.relu(self.conv2_1(self.maxpool(x))))))
#print('x2 size: %s'%str(x.size()))
x = F.relu(self.bn3(self.conv3_2(F.relu(self.conv3_1(self.maxpool(x))))))
#print('x3 size: %s'%str(x.size()))
x = F.relu(self.upconv4(self.maxpool(x)))
#print('x4 size: %s'%str(x.size()))
m=x.size(2)
n=x.size(3)
x = x.view(-1, m*n)
#print('x5 size: %s'%str(x.size()))
fc1 = nn.Linear(m*n, 120)
x = F.relu(fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = F.relu(self.fc4(x))
x = self.fc5(x)
#print('x size: %s'%str(x.size()))
x = F.softmax(x,dim=1)
return x
net = Net()
classes = ('Absense of flow: Left Middle Cerebral Artery ', 'Absense of flow: Right Middle Cerebral Artery ', 'Normal')
net.load_state_dict(torch.load('net_parameters.pkl'))
点评:网络结构和网络参数的理解