如果还没有安装torch以及torchvision的,请看文章:Torch安装
安装完成之后,详细的python代码如下:
from torchvision import datasets from torchvision.transforms import ToTensor, Lambda, Compose import matplotlib.pyplot as plt import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # load train data train_data = datasets.MNIST( root='data', # save in the directory data train=True, # True means train data, False means test data download=True, transform=ToTensor() # (0, 255) to (0, 1) ) # load test data test_data = datasets.MNIST( root='data', train=False, #True means train data, False means test data download=True, transform=ToTensor() )
下载成功结果:
添加下列代码显示其中一条数据:
print(train_data.data.size()) print(train_data.targets.size()) plt.imshow(train_data.data[130].numpy(), cmap='gray') plt.title('%i' % train_data.targets[130]) plt.show()
数据如下图所示:
python代码:
class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Sequential( nn.Conv2d( in_channels=1, # size of input channels out_channels=16, # size of input channels kernel_size=(5, 5), # size of filter stride=(1, 1), # step of filter padding=2, # padding num ), nn.ReLU(), nn.MaxPool2d(kernel_size=2), ) self.conv2 = nn.Sequential( nn.Conv2d(16, 32, (5, 5), (1, 1), 2), nn.ReLU(), nn.MaxPool2d(2), ) self.flat = nn.Flatten() # flattern the result self.out = nn.Linear(32 * 7 * 7, 10) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.flat(x) out = self.out(x) return out
如果打印一下网络的架构,使用语句:
model = CNN() print(model)
显示的结构如下所示:
CNN( (conv1): Sequential( (0): Conv2d(1, 16, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (1): ReLU() (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) ) (conv2): Sequential( (0): Conv2d(16, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2)) (1): ReLU() (2): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False) ) (flat): Flatten(start_dim=1, end_dim=-1) (out): Linear(in_features=1568, out_features=10, bias=True) )3. 整体代码(CPU版本)
python代码:
import torch import torchvision import torch.nn as nn import torch.utils.data as Data import matplotlib.pyplot as plt import numpy as np import os os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" # Hyper parameters EPOCH = 1 BATCH_SIZE = 50 LR = 0.001 DOWNLOAD_MINST = True # Download training data train_data = torchvision.datasets.MNIST( root='./mnist', train=True, transform=torchvision.transforms.ToTensor(), download=DOWNLOAD_MINST ) #print(train_data.data.size()) #print(train_data.targets.size()) #plt.imshow(train_data.data[130].numpy(), cmap='gray') #plt.title('%i' % train_data.targets[130]) #plt.show() # train_loader = Data.DataLoader( dataset=train_data, batch_size=BATCH_SIZE, shuffle=False, num_workers=4 ) # Download testing data test_data = torchvision.datasets.MNIST( root='./mnist', train=False, ) test_x = torch.unsqueeze(test_data.data, dim=1).type(torch.FloatTensor)[:2000] / 255 test_y = test_data.targets[:2000] class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Sequential( nn.Conv2d( in_channels=1, out_channels=16, kernel_size=(5, 5), stride=(1, 1), padding=2 ), nn.ReLU(), nn.MaxPool2d(kernel_size=2), ) self.conv2 = nn.Sequential( nn.Conv2d(16, 32, (5, 5), (1, 1), 2), nn.ReLU(), nn.MaxPool2d(2) ) self.flat = nn.Flatten() self.out = nn.Linear(32 * 7 * 7, 10) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.flat(x) output = self.out(x) return output cnn = CNN() optimizer = torch.optim.Adam(cnn.parameters(), lr=LR) loss_fuction = nn.CrossEntropyLoss() if __name__ == '__main__': for epoch in range(EPOCH): for step, (batch_x, batch_y) in enumerate(train_loader): prediction = cnn(batch_x) loss = loss_fuction(prediction, batch_y) optimizer.zero_grad() loss.backward() optimizer.step() if step % 50 == 0: test_output = cnn(test_x) pred_y = torch.max(test_output, 1)[1].data.numpy() accuracy = (sum(pred_y == np.array(test_y.data)).item()) / test_y.size(0) print('Epoch:%d' % epoch, end='||') print('train loss:%.4f' % loss.item(), end='||') print('test accuracy:%.4f' % accuracy) test_output = cnn(test_x[:10]) pred_y = torch.max(test_output, 1)[1].data.numpy() print(pred_y, 'prediction number') print(test_y[:10].numpy(), 'real number')
运行结果:
Epoch:0||train loss:2.3074||test accuracy:0.2195 Epoch:0||train loss:0.2392||test accuracy:0.7895 Epoch:0||train loss:0.3036||test accuracy:0.8910 Epoch:0||train loss:0.2483||test accuracy:0.9140 Epoch:0||train loss:0.3289||test accuracy:0.9045 Epoch:0||train loss:0.1809||test accuracy:0.9275 Epoch:0||train loss:0.0711||test accuracy:0.9480 Epoch:0||train loss:0.1408||test accuracy:0.9480 Epoch:0||train loss:0.2358||test accuracy:0.9600 Epoch:0||train loss:0.2181||test accuracy:0.9445 Epoch:0||train loss:0.0309||test accuracy:0.9675 Epoch:0||train loss:0.1352||test accuracy:0.9575 Epoch:0||train loss:0.1682||test accuracy:0.9725 Epoch:0||train loss:0.0470||test accuracy:0.9735 Epoch:0||train loss:0.0341||test accuracy:0.9710 Epoch:0||train loss:0.1404||test accuracy:0.9680 Epoch:0||train loss:0.1307||test accuracy:0.9670 Epoch:0||train loss:0.1597||test accuracy:0.9720 Epoch:0||train loss:0.0743||test accuracy:0.9735 Epoch:0||train loss:0.0263||test accuracy:0.9765 Epoch:0||train loss:0.0135||test accuracy:0.9735 Epoch:0||train loss:0.0150||test accuracy:0.9770 Epoch:0||train loss:0.0122||test accuracy:0.9765 Epoch:0||train loss:0.0241||test accuracy:0.9760 [7 2 1 0 4 1 4 9 5 9] prediction number [7 2 1 0 4 1 4 9 5 9] real number4. 整体代码(GPU版本)
python代码:
import torch from torch import nn from torch.utils.data import DataLoader from torchvision import datasets from torchvision.transforms import ToTensor # hyper parameters LR = 0.002 EPOCH = 5 BATCH_SIZE = 50 train_data = datasets.MNIST( root='data', train=True, download=True, transform=ToTensor() ) test_data = datasets.MNIST( root= 'data', train=False, download=True, transform=ToTensor() ) test_loader = DataLoader( dataset=test_data, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, ) train_loader = DataLoader( dataset=train_data, batch_size=BATCH_SIZE, shuffle=False, num_workers=4, ) class CNN(nn.Module): def __init__(self): super(CNN, self).__init__() self.conv1 = nn.Sequential( nn.Conv2d( in_channels=1, out_channels=16, kernel_size=(5, 5), stride=(1, 1), padding=2, ), nn.ReLU(), nn.MaxPool2d(kernel_size=2), ) self.conv2 = nn.Sequential( nn.Conv2d(16, 32, (5, 5), (1, 1), 2), nn.ReLU(), nn.MaxPool2d(2), ) self.flat = nn.Flatten() self.out = nn.Linear(32 * 7 * 7, 10) def forward(self, x): x = self.conv1(x) x = self.conv2(x) x = self.flat(x) out = self.out(x) return out device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = CNN().to(device) optimizer = torch.optim.Adam(model.parameters(), lr=LR) loss_function = torch.nn.CrossEntropyLoss() def train(data_loader, model, loss_function, optimizer): size = len(data_loader.dataset) model.train() for batch, (X, y) in enumerate(data_loader): X, y = X.to(device), y.to(device) # Compute prediction error pred = model(X) loss = loss_function(pred, y) # Back propagation optimizer.zero_grad() loss.backward() optimizer.step() if batch % 100 == 0: loss, current = loss.item(), batch * len(X) print(f"loss: {loss:>7f} [{current:>5d}/{size:>5d}]") def test(data_loader, model, loss_fn): size = len(data_loader.dataset) num_batches = len(data_loader) model.eval() test_loss, correct = 0, 0 with torch.no_grad(): for X, y in data_loader: X, y = X.to(device), y.to(device) pred = model(X) test_loss += loss_fn(pred, y).item() correct += (pred.argmax(1) == y).type(torch.float).sum().item() test_loss /= num_batches correct /= size print(f"Test Error: n Accuracy: {(100*correct):>0.1f}%, Avg loss: {test_loss:>8f} n") torch.save(model.state_dict(), "model.pth") print("Saved PyTorch Model State to model.pth") def call_model(): for t in range(EPOCH): print(f"Epoch {t + 1}n-------------------------------") train(train_loader, model, loss_function, optimizer) test(test_loader, model, loss_function) print("Done!") if __name__ == '__main__': call_model()
结果展示:
Epoch 1 ------------------------------- loss: 2.319763 [ 0/60000] loss: 0.197887 [ 5000/60000] loss: 0.231820 [10000/60000] loss: 0.029967 [15000/60000] loss: 0.193301 [20000/60000] loss: 0.028721 [25000/60000] loss: 0.230080 [30000/60000] loss: 0.030196 [35000/60000] loss: 0.088837 [40000/60000] loss: 0.061477 [45000/60000] loss: 0.021868 [50000/60000] loss: 0.009769 [55000/60000] Test Error: Accuracy: 97.0%, Avg loss: 0.092558 Epoch 2 ------------------------------- loss: 0.062530 [ 0/60000] loss: 0.037623 [ 5000/60000] loss: 0.057986 [10000/60000] loss: 0.003451 [15000/60000] loss: 0.034381 [20000/60000] loss: 0.005294 [25000/60000] loss: 0.171846 [30000/60000] loss: 0.042936 [35000/60000] loss: 0.073527 [40000/60000] loss: 0.039359 [45000/60000] loss: 0.005118 [50000/60000] loss: 0.003203 [55000/60000] Test Error: Accuracy: 98.6%, Avg loss: 0.041006 Epoch 3 ------------------------------- loss: 0.018981 [ 0/60000] loss: 0.037027 [ 5000/60000] loss: 0.030264 [10000/60000] loss: 0.000278 [15000/60000] loss: 0.024834 [20000/60000] loss: 0.003731 [25000/60000] loss: 0.117729 [30000/60000] loss: 0.058137 [35000/60000] loss: 0.068251 [40000/60000] loss: 0.009537 [45000/60000] loss: 0.015441 [50000/60000] loss: 0.000517 [55000/60000] Test Error: Accuracy: 98.9%, Avg loss: 0.033202 Epoch 4 ------------------------------- loss: 0.005696 [ 0/60000] loss: 0.011667 [ 5000/60000] loss: 0.033686 [10000/60000] loss: 0.000063 [15000/60000] loss: 0.021328 [20000/60000] loss: 0.001276 [25000/60000] loss: 0.115446 [30000/60000] loss: 0.013636 [35000/60000] loss: 0.031956 [40000/60000] loss: 0.006652 [45000/60000] loss: 0.006096 [50000/60000] loss: 0.001458 [55000/60000] Test Error: Accuracy: 98.9%, Avg loss: 0.034399 Epoch 5 ------------------------------- loss: 0.001917 [ 0/60000] loss: 0.004953 [ 5000/60000] loss: 0.039799 [10000/60000] loss: 0.000086 [15000/60000] loss: 0.087789 [20000/60000] loss: 0.006337 [25000/60000] loss: 0.046358 [30000/60000] loss: 0.020176 [35000/60000] loss: 0.009923 [40000/60000] loss: 0.008083 [45000/60000] loss: 0.007701 [50000/60000] loss: 0.002184 [55000/60000] Test Error: Accuracy: 99.0%, Avg loss: 0.034642 Done! Saved PyTorch Model State to model.pth5. 保存和加载模型
下面的代码是保存训练好的模型的参数:
torch.save(model.state_dict(), "model.pth") print("Saved PyTorch Model State to model.pth")
而加载模型的语法则如下所示:
model = CNN() model.load_state_dict(torch.load("model.pth"))
将训练好的模型去预测test数据集上的种类标签,完整代码如下所示:
def load_model(): model = CNN() model.load_state_dict(torch.load("model.pth")) classes = [ "T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot", ] model.eval() inputs, labels = next(iter(test_loader)) with torch.no_grad(): prediction = model(inputs) accuracy = 0 for i in range(len(prediction)): predicted, actual = classes[prediction[i].argmax(0)], classes[labels[i]] print(f'Predicted: "{predicted}", Actual: "{actual}"') if predicted == actual: accuracy += 1 print("accuracy:%.4f" % (accuracy / len(prediction))) if __name__ == '__main__': load_model()
预测的结果如下所示,准确率为98%:
Predicted: "Sneaker", Actual: "Sneaker" Predicted: "Pullover", Actual: "Pullover" Predicted: "Trouser", Actual: "Trouser" Predicted: "T-shirt/top", Actual: "T-shirt/top" Predicted: "Coat", Actual: "Coat" Predicted: "Trouser", Actual: "Trouser" Predicted: "Coat", Actual: "Coat" Predicted: "Ankle boot", Actual: "Ankle boot" Predicted: "Sandal", Actual: "Sandal" Predicted: "Ankle boot", Actual: "Ankle boot" Predicted: "T-shirt/top", Actual: "T-shirt/top" Predicted: "Shirt", Actual: "Shirt" Predicted: "Ankle boot", Actual: "Ankle boot" Predicted: "T-shirt/top", Actual: "T-shirt/top" Predicted: "Trouser", Actual: "Trouser" Predicted: "Sandal", Actual: "Sandal" Predicted: "Ankle boot", Actual: "Ankle boot" Predicted: "Sneaker", Actual: "Sneaker" Predicted: "Sandal", Actual: "Dress" Predicted: "Coat", Actual: "Coat" Predicted: "Ankle boot", Actual: "Ankle boot" Predicted: "Shirt", Actual: "Shirt" Predicted: "Shirt", Actual: "Shirt" Predicted: "Sandal", Actual: "Sandal" Predicted: "Coat", Actual: "Coat" Predicted: "T-shirt/top", Actual: "T-shirt/top" Predicted: "Sneaker", Actual: "Sneaker" Predicted: "Coat", Actual: "Coat" Predicted: "T-shirt/top", Actual: "T-shirt/top" Predicted: "Trouser", Actual: "Trouser" Predicted: "Dress", Actual: "Dress" Predicted: "Trouser", Actual: "Trouser" Predicted: "Dress", Actual: "Dress" Predicted: "Coat", Actual: "Coat" Predicted: "Sneaker", Actual: "Sneaker" Predicted: "Pullover", Actual: "Pullover" Predicted: "Sneaker", Actual: "Sneaker" Predicted: "Trouser", Actual: "Trouser" Predicted: "Pullover", Actual: "Pullover" Predicted: "Trouser", Actual: "Trouser" Predicted: "Trouser", Actual: "Trouser" Predicted: "Sneaker", Actual: "Sneaker" Predicted: "Coat", Actual: "Coat" Predicted: "Pullover", Actual: "Pullover" Predicted: "Dress", Actual: "Dress" Predicted: "Sandal", Actual: "Sandal" Predicted: "Trouser", Actual: "Trouser" Predicted: "Pullover", Actual: "Pullover" Predicted: "Coat", Actual: "Coat" Predicted: "Coat", Actual: "Coat" accuracy:0.98006. 测试全部测试集的内容
python代码:
def load_model(): model = CNN() model.load_state_dict(torch.load("model.pth")) classes = [ "T-shirt/top", "Trouser", "Pullover", "Dress", "Coat", "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot", ] model.eval() with torch.no_grad(): accuracy = 0 for (images, labels) in test_loader: predict = model(images) for i in range(len(predict)): predicted, actual = classes[predict[i].argmax(0)], classes[labels[i]] print(f'Predicted: "{predicted}", Actual: "{actual}"') if predicted == actual: accuracy += 1 print("accuracy:%.4f" % (accuracy / len(test_data.data))) print('num:%d' % accuracy) if __name__ == '__main__': load_model()
测试结果:
... accuracy:0.9708 num:9708
这里就不全部展开打印出来的结果了,因为总共有10000条数据,只展示最后的结果。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)