Pytorch - Practice
实践过程
分类数据标识
对于learn_pytorch\07_train\train_gpu_1.py中的测试数据集,可以看到类别

直接使用CIFAR10模型进行输出
直接执行test1.py函数,执行结果判断为9(trunk),不正确。
D:\software\Anaconda\envs\pytorch\python.exe D:\code\klc\learn_pytorch\test1.py
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=355x262 at 0x25BF576BE90>
torch.Size([3, 32, 32])
tensor([[-0.0219, -0.0822, 0.1004, -0.0182, 0.0624, 0.0092, -0.1172, -0.0692,
-0.1410, 0.1012]])
tensor([9])
借助 Google colab 的GPU训练模型
训练模型
整个过程是对于CIFAR10数据集进行训练:

训练所需内容:
训练代码见: framework\pytorch\learn_pytorch\08_practice\train_content\code.py
训练结果见: learn_pytorch\08_practice\train_content\result.txt
结果正确性
...
-------第 10 轮训练开始-------
训练次数:7100, Loss: 1.2830897569656372, 训练时间: 7.330090761184692
训练次数:7200, Loss: 0.9471298456192017, 训练时间: 12.269827842712402
训练次数:7300, Loss: 1.1152112483978271, 训练时间: 12.434340238571167
训练次数:7400, Loss: 0.8577837944030762, 训练时间: 12.250300645828247
训练次数:7500, Loss: 1.2039788961410522, 训练时间: 12.377758979797363
训练次数:7600, Loss: 1.265457272529602, 训练时间: 12.330166339874268
训练次数:7700, Loss: 0.859161376953125, 训练时间: 12.326414108276367
训练次数:7800, Loss: 1.246589183807373, 训练时间: 12.398253440856934
整体测试集上的Loss: 198.2974852323532
整体测试集上的正确率: 0.5478000044822693
测试图像分类正确性
使用训练结果tudui_9_gpu.pth, 判断结果正确为0(airplane)
D:\software\Anaconda\envs\pytorch\python.exe D:\code\klc\learn_pytorch\test2.py
<PIL.PngImagePlugin.PngImageFile image mode=RGBA size=355x262 at 0x23FED5CA150>
torch.Size([3, 32, 32])
Tudui(
(model): Sequential(
(0): Conv2d(3, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(2): Conv2d(32, 32, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(4): Conv2d(32, 64, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2))
(5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(6): Flatten(start_dim=1, end_dim=-1)
(7): Linear(in_features=1024, out_features=64, bias=True)
(8): Linear(in_features=64, out_features=10, bias=True)
)
)
tensor([[ 8.6523, -0.0347, 4.1474, -0.7815, 1.7745, -2.5988, -6.6432, -5.4239,
1.3317, -3.0969]])
tensor([0])
