import matplotlib.pyplot as plt import numpy as np
import torch import torch.nn as nn import torch.optim as optim import torchvision import torchvision.models as models import torchvision.transforms as transforms
import time import os import tqdm import PIL.Image as Image from IPython.display import display
# 将模型初始化设为训练模式 model.train() for epoch in tqdm.tqdm(range(n_epochs)): since = time.time() running_loss = 0.0 running_correct = 0.0 for i, data inenumerate(trainloader, 0):
/Users/gawaintan/miniforge3/envs/FastAi/lib/python3.8/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=ResNet34_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet34_Weights.DEFAULT` to get the most up-to-date weights.
warnings.warn(msg)
0%| | 0/12 [00:00<?, ?it/s]
Epoch 1, duration: 109 s, loss: 4.1009, acc: 9.3994
8%|████████████████▊ | 1/12 [02:34<28:22, 154.73s/it]
Accuracy of the network on the test images: 24 %
Epoch 2, duration: 113 s, loss: 2.3972, acc: 34.3994
17%|█████████████████████████████████▌ | 2/12 [05:15<26:25, 158.51s/it]
Accuracy of the network on the test images: 34 %
Epoch 3, duration: 117 s, loss: 1.7940, acc: 48.6328
25%|██████████████████████████████████████████████████▎ | 3/12 [07:58<24:03, 160.39s/it]
Accuracy of the network on the test images: 35 %
Epoch 4, duration: 114 s, loss: 1.4076, acc: 58.3984
33%|███████████████████████████████████████████████████████████████████ | 4/12 [10:36<21:16, 159.59s/it]
Accuracy of the network on the test images: 48 %
Epoch 5, duration: 119 s, loss: 1.1524, acc: 64.6973
42%|███████████████████████████████████████████████████████████████████████████████████▊ | 5/12 [13:22<18:52, 161.84s/it]
Accuracy of the network on the test images: 48 %
Epoch 6, duration: 119 s, loss: 0.9919, acc: 69.3848
50%|████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 6/12 [16:06<16:14, 162.44s/it]
Accuracy of the network on the test images: 61 %
Epoch 7, duration: 122 s, loss: 0.8873, acc: 72.2656
58%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 7/12 [18:54<13:41, 164.33s/it]
Accuracy of the network on the test images: 61 %
Epoch 8, duration: 123 s, loss: 0.7274, acc: 77.3193
67%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 8/12 [21:42<11:01, 165.34s/it]
Accuracy of the network on the test images: 64 %
Epoch 9, duration: 127 s, loss: 0.4055, acc: 87.5000
75%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 9/12 [24:30<08:19, 166.45s/it]
Accuracy of the network on the test images: 80 %
Epoch 10, duration: 127 s, loss: 0.2865, acc: 91.0400
83%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋ | 10/12 [27:22<05:35, 167.97s/it]
Accuracy of the network on the test images: 81 %
Epoch 11, duration: 131 s, loss: 0.2480, acc: 92.3828
92%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 11/12 [30:20<02:51, 171.06s/it]
Accuracy of the network on the test images: 82 %
Epoch 12, duration: 139 s, loss: 0.2195, acc: 93.5547
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 12/12 [33:26<00:00, 167.24s/it]
Accuracy of the network on the test images: 82 %
Finished Training
0%| | 0/12 [00:00<?, ?it/s]
Epoch 1, duration: 168 s, loss: 3.0864, acc: 23.5596
8%|████████████████▊ | 1/12 [03:22<37:07, 202.47s/it]
Accuracy of the network on the test images: 39 %
Epoch 2, duration: 164 s, loss: 2.0752, acc: 43.7256
17%|█████████████████████████████████▌ | 2/12 [07:01<35:22, 212.23s/it]
Accuracy of the network on the test images: 51 %
Epoch 3, duration: 164 s, loss: 1.5181, acc: 56.3721
25%|██████████████████████████████████████████████████▎ | 3/12 [10:22<31:03, 207.08s/it]
Accuracy of the network on the test images: 62 %
Epoch 4, duration: 182 s, loss: 1.1773, acc: 65.8691
33%|███████████████████████████████████████████████████████████████████ | 4/12 [14:01<28:15, 211.89s/it]
Accuracy of the network on the test images: 66 %
Epoch 5, duration: 130 s, loss: 0.9473, acc: 71.5088
42%|███████████████████████████████████████████████████████████████████████████████████▊ | 5/12 [16:49<22:52, 196.12s/it]
Accuracy of the network on the test images: 69 %
Epoch 6, duration: 106 s, loss: 0.6231, acc: 82.9834
50%|████████████████████████████████████████████████████████████████████████████████████████████████████▌ | 6/12 [19:15<17:54, 179.04s/it]
Accuracy of the network on the test images: 79 %
Epoch 7, duration: 135 s, loss: 0.5321, acc: 86.5234
58%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 7/12 [22:10<14:47, 177.47s/it]
Accuracy of the network on the test images: 80 %
Epoch 8, duration: 137 s, loss: 0.4982, acc: 86.7920
67%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████ | 8/12 [25:08<11:50, 177.72s/it]
Accuracy of the network on the test images: 81 %
Epoch 9, duration: 147 s, loss: 0.4649, acc: 88.9404
75%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▊ | 9/12 [28:16<09:03, 181.11s/it]
Accuracy of the network on the test images: 81 %
Epoch 10, duration: 154 s, loss: 0.4419, acc: 89.3799
83%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▋ | 10/12 [31:34<06:12, 186.29s/it]
Accuracy of the network on the test images: 81 %
Epoch 11, duration: 172 s, loss: 0.4061, acc: 89.9170
92%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████▎ | 11/12 [35:12<03:15, 195.96s/it]
Accuracy of the network on the test images: 81 %
Epoch 12, duration: 156 s, loss: 0.4121, acc: 89.9170
100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 12/12 [38:28<00:00, 192.34s/it]
Accuracy of the network on the test images: 81 %
Finished Training