https://zhuanlan.zhihu.com/p/101332297
为了减少时间成本,本文只复现了ResNet-18模型,诸如ResNet-34、ResNet-50、ResNet-101、ResNet-152等更深层的神经网络模型,可以在ResNet-18的基础上修改而来。ResNet由于在顺序结构神经网络中引入了残差块(Residual Block),因此得名Residual Networks(ResNet)。
Residual Block
:
ResNet-18与ResNet-50
:
# import packages
import torch
import torchvision
# Device configuration.
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Transform configuration and data augmentation.
transform_train = torchvision.transforms.Compose([torchvision.transforms.Pad(4),
torchvision.transforms.RandomHorizontalFlip(),
torchvision.transforms.RandomCrop(32),
torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
transform_test = torchvision.transforms.Compose([torchvision.transforms.ToTensor(),
torchvision.transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])])
# Hyper-parameters
num_classes = 10
batch_size = 100
learning_rate = 0.001
num_epochs = 80
# Load downloaded dataset.
train_dataset = torchvision.datasets.CIFAR10('data/CIFAR/', download=False, train=True, transform=transform_train)
test_dataset = torchvision.datasets.CIFAR10('data/CIFAR/', download=False, train=False, transform=transform_test)
# Data Loader.
train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)
# Define 3x3 convolution.
def conv3x3(in_channels, out_channels, stride=1):
return torch.nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
# Define Residual block
class ResidualBlock(torch.nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample=None):
super(ResidualBlock, self).__init__()
self.conv1 = conv3x3(in_channels, out_channels, stride)
self.bn1 = torch.nn.BatchNorm2d(out_channels)
self.relu = torch.nn.ReLU(inplace=True)
self.conv2 = conv3x3(out_channels, out_channels)
self.bn2 = torch.nn.BatchNorm2d(out_channels)
self.downsample = downsample
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample :
residual = self.downsample(x)
out += residual
out = self.relu(out)
return out
# Define ResNet-18
class ResNet(torch.nn.Module):
def __init__(self, block, layers, num_classes):
super(ResNet, self).__init__()
self.in_channels = 16
self.conv = conv3x3(3, 16)
self.bn = torch.nn.BatchNorm2d(16)
self.relu = torch.nn.ReLU(inplace=True)
self.layer1 = self._make_layers(block, 16, layers[0])
self.layer2 = self._make_layers(block, 32, layers[1], 2)
self.layer3 = self._make_layers(block, 64, layers[2], 2)
self.layer4 = self._make_layers(block, 128, layers[3], 2)
self.avg_pool = torch.nn.AdaptiveAvgPool2d((1, 1))
self.fc = torch.nn.Linear(128, num_classes)
def _make_layers(self, block, out_channels, blocks, stride=1):
downsample = None
if (stride != 1) or (self.in_channels != out_channels):
downsample = torch.nn.Sequential(
conv3x3(self.in_channels, out_channels, stride=stride),
torch.nn.BatchNorm2d(out_channels))
layers = []
layers.append(block(self.in_channels, out_channels, stride, downsample))
self.in_channels = out_channels
for i in range(1, blocks):
layers.append(block(out_channels, out_channels))
return torch.nn.Sequential(*layers)
def forward(self, x):
out = self.conv(x)
out = self.bn(out)
out = self.relu(out)
out = self.layer1(out)
out = self.layer2(out)
out = self.layer3(out)
out = self.layer4(out)
out = self.avg_pool(out)
out = out.view(out.size(0), -1)
out = self.fc(out)
return out
# Make model.
model = ResNet(ResidualBlock, [2, 2, 2, 2], num_classes).to(device)
# Loss ans optimizer
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# For updating learning rate.
def update_lr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
# Train the model.
total_step = len(train_loader)
curr_lr = learning_rate
for epoch in range(num_epochs):
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
# Forward pass.
outputs = model(images)
loss = criterion(outputs, labels)
# Backward and optimize
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i+1) % 100 == 0:
print ('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))
# Decay learning rate.
if (epoch+1) % 20 == 0:
curr_lr /= 3
update_lr(optimizer, curr_lr)
Epoch [1/80], Step [100/500], Loss: 1.6278
Epoch [1/80], Step [200/500], Loss: 1.3368
Epoch [1/80], Step [300/500], Loss: 1.3774
Epoch [1/80], Step [400/500], Loss: 1.2487
Epoch [1/80], Step [500/500], Loss: 1.0083
Epoch [2/80], Step [100/500], Loss: 1.1344
Epoch [2/80], Step [200/500], Loss: 0.8391
Epoch [2/80], Step [300/500], Loss: 1.1451
Epoch [2/80], Step [400/500], Loss: 1.1581
Epoch [2/80], Step [500/500], Loss: 1.0138
Epoch [3/80], Step [100/500], Loss: 0.8204
Epoch [3/80], Step [200/500], Loss: 0.9306
Epoch [3/80], Step [300/500], Loss: 0.6666
Epoch [3/80], Step [400/500], Loss: 0.9254
Epoch [3/80], Step [500/500], Loss: 0.7068
Epoch [4/80], Step [100/500], Loss: 0.7309
Epoch [4/80], Step [200/500], Loss: 0.6603
Epoch [4/80], Step [300/500], Loss: 0.5715
Epoch [4/80], Step [400/500], Loss: 0.6693
Epoch [4/80], Step [500/500], Loss: 0.6240
Epoch [5/80], Step [100/500], Loss: 0.6171
Epoch [5/80], Step [200/500], Loss: 0.7757
Epoch [5/80], Step [300/500], Loss: 0.5394
Epoch [5/80], Step [400/500], Loss: 0.4437
Epoch [5/80], Step [500/500], Loss: 0.5371
Epoch [6/80], Step [100/500], Loss: 0.5737
Epoch [6/80], Step [200/500], Loss: 0.7505
Epoch [6/80], Step [300/500], Loss: 0.4102
Epoch [6/80], Step [400/500], Loss: 0.3542
Epoch [6/80], Step [500/500], Loss: 0.5393
Epoch [7/80], Step [100/500], Loss: 0.4987
Epoch [7/80], Step [200/500], Loss: 0.4006
Epoch [7/80], Step [300/500], Loss: 0.4238
Epoch [7/80], Step [400/500], Loss: 0.4722
Epoch [7/80], Step [500/500], Loss: 0.4706
Epoch [8/80], Step [100/500], Loss: 0.5829
Epoch [8/80], Step [200/500], Loss: 0.5226
Epoch [8/80], Step [300/500], Loss: 0.4381
Epoch [8/80], Step [400/500], Loss: 0.4344
Epoch [8/80], Step [500/500], Loss: 0.4641
Epoch [9/80], Step [100/500], Loss: 0.5033
Epoch [9/80], Step [200/500], Loss: 0.4500
Epoch [9/80], Step [300/500], Loss: 0.4019
Epoch [9/80], Step [400/500], Loss: 0.4249
Epoch [9/80], Step [500/500], Loss: 0.5130
Epoch [10/80], Step [100/500], Loss: 0.4057
Epoch [10/80], Step [200/500], Loss: 0.4189
Epoch [10/80], Step [300/500], Loss: 0.4459
Epoch [10/80], Step [400/500], Loss: 0.4157
Epoch [10/80], Step [500/500], Loss: 0.3352
Epoch [11/80], Step [100/500], Loss: 0.4696
Epoch [11/80], Step [200/500], Loss: 0.3871
Epoch [11/80], Step [300/500], Loss: 0.3810
Epoch [11/80], Step [400/500], Loss: 0.4613
Epoch [11/80], Step [500/500], Loss: 0.3651
Epoch [12/80], Step [100/500], Loss: 0.3487
Epoch [12/80], Step [200/500], Loss: 0.3225
Epoch [12/80], Step [300/500], Loss: 0.3127
Epoch [12/80], Step [400/500], Loss: 0.4051
Epoch [12/80], Step [500/500], Loss: 0.3588
Epoch [13/80], Step [100/500], Loss: 0.5568
Epoch [13/80], Step [200/500], Loss: 0.3759
Epoch [13/80], Step [300/500], Loss: 0.2941
Epoch [13/80], Step [400/500], Loss: 0.5200
Epoch [13/80], Step [500/500], Loss: 0.3995
Epoch [14/80], Step [100/500], Loss: 0.4985
Epoch [14/80], Step [200/500], Loss: 0.4564
Epoch [14/80], Step [300/500], Loss: 0.2351
Epoch [14/80], Step [400/500], Loss: 0.4535
Epoch [14/80], Step [500/500], Loss: 0.5108
Epoch [15/80], Step [100/500], Loss: 0.3881
Epoch [15/80], Step [200/500], Loss: 0.4429
Epoch [15/80], Step [300/500], Loss: 0.2841
Epoch [15/80], Step [400/500], Loss: 0.4210
Epoch [15/80], Step [500/500], Loss: 0.2653
Epoch [16/80], Step [100/500], Loss: 0.4307
Epoch [16/80], Step [200/500], Loss: 0.2401
Epoch [16/80], Step [300/500], Loss: 0.1996
Epoch [16/80], Step [400/500], Loss: 0.2240
Epoch [16/80], Step [500/500], Loss: 0.2821
Epoch [17/80], Step [100/500], Loss: 0.2849
Epoch [17/80], Step [200/500], Loss: 0.2965
Epoch [17/80], Step [300/500], Loss: 0.3777
Epoch [17/80], Step [400/500], Loss: 0.4469
Epoch [17/80], Step [500/500], Loss: 0.2752
Epoch [18/80], Step [100/500], Loss: 0.2665
Epoch [18/80], Step [200/500], Loss: 0.3726
Epoch [18/80], Step [300/500], Loss: 0.1999
Epoch [18/80], Step [400/500], Loss: 0.2555
Epoch [18/80], Step [500/500], Loss: 0.2895
Epoch [19/80], Step [100/500], Loss: 0.1635
Epoch [19/80], Step [200/500], Loss: 0.1796
Epoch [19/80], Step [300/500], Loss: 0.4037
Epoch [19/80], Step [400/500], Loss: 0.2623
Epoch [19/80], Step [500/500], Loss: 0.1818
Epoch [20/80], Step [100/500], Loss: 0.2265
Epoch [20/80], Step [200/500], Loss: 0.4131
Epoch [20/80], Step [300/500], Loss: 0.2777
Epoch [20/80], Step [400/500], Loss: 0.3146
Epoch [20/80], Step [500/500], Loss: 0.3028
Epoch [21/80], Step [100/500], Loss: 0.3265
Epoch [21/80], Step [200/500], Loss: 0.2229
Epoch [21/80], Step [300/500], Loss: 0.2388
Epoch [21/80], Step [400/500], Loss: 0.2601
Epoch [21/80], Step [500/500], Loss: 0.1227
Epoch [22/80], Step [100/500], Loss: 0.2175
Epoch [22/80], Step [200/500], Loss: 0.2641
Epoch [22/80], Step [300/500], Loss: 0.1847
Epoch [22/80], Step [400/500], Loss: 0.1926
Epoch [22/80], Step [500/500], Loss: 0.2075
Epoch [23/80], Step [100/500], Loss: 0.1859
Epoch [23/80], Step [200/500], Loss: 0.1570
Epoch [23/80], Step [300/500], Loss: 0.2750
Epoch [23/80], Step [400/500], Loss: 0.0908
Epoch [23/80], Step [500/500], Loss: 0.1936
Epoch [24/80], Step [100/500], Loss: 0.1485
Epoch [24/80], Step [200/500], Loss: 0.1572
Epoch [24/80], Step [300/500], Loss: 0.1881
Epoch [24/80], Step [400/500], Loss: 0.1262
Epoch [24/80], Step [500/500], Loss: 0.3006
Epoch [25/80], Step [100/500], Loss: 0.2768
Epoch [25/80], Step [200/500], Loss: 0.0902
Epoch [25/80], Step [300/500], Loss: 0.2313
Epoch [25/80], Step [400/500], Loss: 0.1361
Epoch [25/80], Step [500/500], Loss: 0.2736
Epoch [26/80], Step [100/500], Loss: 0.1499
Epoch [26/80], Step [200/500], Loss: 0.1687
Epoch [26/80], Step [300/500], Loss: 0.1190
Epoch [26/80], Step [400/500], Loss: 0.1117
Epoch [26/80], Step [500/500], Loss: 0.1268
Epoch [27/80], Step [100/500], Loss: 0.1518
Epoch [27/80], Step [200/500], Loss: 0.1603
Epoch [27/80], Step [300/500], Loss: 0.1415
Epoch [27/80], Step [400/500], Loss: 0.2641
Epoch [27/80], Step [500/500], Loss: 0.0748
Epoch [28/80], Step [100/500], Loss: 0.2301
Epoch [28/80], Step [200/500], Loss: 0.2718
Epoch [28/80], Step [300/500], Loss: 0.2650
Epoch [28/80], Step [400/500], Loss: 0.0754
Epoch [28/80], Step [500/500], Loss: 0.1454
Epoch [29/80], Step [100/500], Loss: 0.1373
Epoch [29/80], Step [200/500], Loss: 0.1285
Epoch [29/80], Step [300/500], Loss: 0.1345
Epoch [29/80], Step [400/500], Loss: 0.1153
Epoch [29/80], Step [500/500], Loss: 0.2338
Epoch [30/80], Step [100/500], Loss: 0.1643
Epoch [30/80], Step [200/500], Loss: 0.0779
Epoch [30/80], Step [300/500], Loss: 0.1455
Epoch [30/80], Step [400/500], Loss: 0.1288
Epoch [30/80], Step [500/500], Loss: 0.2127
Epoch [31/80], Step [100/500], Loss: 0.2214
Epoch [31/80], Step [200/500], Loss: 0.1338
Epoch [31/80], Step [300/500], Loss: 0.1166
Epoch [31/80], Step [400/500], Loss: 0.0748
Epoch [31/80], Step [500/500], Loss: 0.2321
Epoch [32/80], Step [100/500], Loss: 0.1146
Epoch [32/80], Step [200/500], Loss: 0.1730
Epoch [32/80], Step [300/500], Loss: 0.1165
Epoch [32/80], Step [400/500], Loss: 0.2201
Epoch [32/80], Step [500/500], Loss: 0.0860
Epoch [33/80], Step [100/500], Loss: 0.1171
Epoch [33/80], Step [200/500], Loss: 0.1427
Epoch [33/80], Step [300/500], Loss: 0.1329
Epoch [33/80], Step [400/500], Loss: 0.1102
Epoch [33/80], Step [500/500], Loss: 0.1330
Epoch [34/80], Step [100/500], Loss: 0.1335
Epoch [34/80], Step [200/500], Loss: 0.1465
Epoch [34/80], Step [300/500], Loss: 0.1598
Epoch [34/80], Step [400/500], Loss: 0.0849
Epoch [34/80], Step [500/500], Loss: 0.0847
Epoch [35/80], Step [100/500], Loss: 0.2173
Epoch [35/80], Step [200/500], Loss: 0.1172
Epoch [35/80], Step [300/500], Loss: 0.1516
Epoch [35/80], Step [400/500], Loss: 0.1212
Epoch [35/80], Step [500/500], Loss: 0.0644
Epoch [36/80], Step [100/500], Loss: 0.0651
Epoch [36/80], Step [200/500], Loss: 0.1346
Epoch [36/80], Step [300/500], Loss: 0.0986
Epoch [36/80], Step [400/500], Loss: 0.1141
Epoch [36/80], Step [500/500], Loss: 0.1344
Epoch [37/80], Step [100/500], Loss: 0.1834
Epoch [37/80], Step [200/500], Loss: 0.1162
Epoch [37/80], Step [300/500], Loss: 0.1637
Epoch [37/80], Step [400/500], Loss: 0.0949
Epoch [37/80], Step [500/500], Loss: 0.1504
Epoch [38/80], Step [100/500], Loss: 0.1251
Epoch [38/80], Step [200/500], Loss: 0.1133
Epoch [38/80], Step [300/500], Loss: 0.1080
Epoch [38/80], Step [400/500], Loss: 0.0686
Epoch [38/80], Step [500/500], Loss: 0.1099
Epoch [39/80], Step [100/500], Loss: 0.0616
Epoch [39/80], Step [200/500], Loss: 0.1919
Epoch [39/80], Step [300/500], Loss: 0.1736
Epoch [39/80], Step [400/500], Loss: 0.1276
Epoch [39/80], Step [500/500], Loss: 0.0810
Epoch [40/80], Step [100/500], Loss: 0.1305
Epoch [40/80], Step [200/500], Loss: 0.1893
Epoch [40/80], Step [300/500], Loss: 0.0975
Epoch [40/80], Step [400/500], Loss: 0.0918
Epoch [40/80], Step [500/500], Loss: 0.0549
Epoch [41/80], Step [100/500], Loss: 0.0390
Epoch [41/80], Step [200/500], Loss: 0.0826
Epoch [41/80], Step [300/500], Loss: 0.0812
Epoch [41/80], Step [400/500], Loss: 0.1012
Epoch [41/80], Step [500/500], Loss: 0.0991
Epoch [42/80], Step [100/500], Loss: 0.1419
Epoch [42/80], Step [200/500], Loss: 0.1208
Epoch [42/80], Step [300/500], Loss: 0.0862
Epoch [42/80], Step [400/500], Loss: 0.0817
Epoch [42/80], Step [500/500], Loss: 0.0749
Epoch [43/80], Step [100/500], Loss: 0.0834
Epoch [43/80], Step [200/500], Loss: 0.0713
Epoch [43/80], Step [300/500], Loss: 0.0794
Epoch [43/80], Step [400/500], Loss: 0.1317
Epoch [43/80], Step [500/500], Loss: 0.0561
Epoch [44/80], Step [100/500], Loss: 0.1528
Epoch [44/80], Step [200/500], Loss: 0.0989
Epoch [44/80], Step [300/500], Loss: 0.1040
Epoch [44/80], Step [400/500], Loss: 0.0633
Epoch [44/80], Step [500/500], Loss: 0.0580
Epoch [45/80], Step [100/500], Loss: 0.0817
Epoch [45/80], Step [200/500], Loss: 0.0723
Epoch [45/80], Step [300/500], Loss: 0.0435
Epoch [45/80], Step [400/500], Loss: 0.1233
Epoch [45/80], Step [500/500], Loss: 0.0652
Epoch [46/80], Step [100/500], Loss: 0.0720
Epoch [46/80], Step [200/500], Loss: 0.0727
Epoch [46/80], Step [300/500], Loss: 0.0731
Epoch [46/80], Step [400/500], Loss: 0.1015
Epoch [46/80], Step [500/500], Loss: 0.1100
Epoch [47/80], Step [100/500], Loss: 0.1373
Epoch [47/80], Step [200/500], Loss: 0.0711
Epoch [47/80], Step [300/500], Loss: 0.0612
Epoch [47/80], Step [400/500], Loss: 0.1126
Epoch [47/80], Step [500/500], Loss: 0.0481
Epoch [48/80], Step [100/500], Loss: 0.0284
Epoch [48/80], Step [200/500], Loss: 0.0472
Epoch [48/80], Step [300/500], Loss: 0.0857
Epoch [48/80], Step [400/500], Loss: 0.0402
Epoch [48/80], Step [500/500], Loss: 0.0856
Epoch [49/80], Step [100/500], Loss: 0.0678
Epoch [49/80], Step [200/500], Loss: 0.0627
Epoch [49/80], Step [300/500], Loss: 0.0598
Epoch [49/80], Step [400/500], Loss: 0.0643
Epoch [49/80], Step [500/500], Loss: 0.0256
Epoch [50/80], Step [100/500], Loss: 0.0903
Epoch [50/80], Step [200/500], Loss: 0.0344
Epoch [50/80], Step [300/500], Loss: 0.0522
Epoch [50/80], Step [400/500], Loss: 0.0502
Epoch [50/80], Step [500/500], Loss: 0.0344
Epoch [51/80], Step [100/500], Loss: 0.0654
Epoch [51/80], Step [200/500], Loss: 0.0376
Epoch [51/80], Step [300/500], Loss: 0.0347
Epoch [51/80], Step [400/500], Loss: 0.0391
Epoch [51/80], Step [500/500], Loss: 0.0918
Epoch [52/80], Step [100/500], Loss: 0.0627
Epoch [52/80], Step [200/500], Loss: 0.0631
Epoch [52/80], Step [300/500], Loss: 0.1437
Epoch [52/80], Step [400/500], Loss: 0.1198
Epoch [52/80], Step [500/500], Loss: 0.0595
Epoch [53/80], Step [100/500], Loss: 0.0501
Epoch [53/80], Step [200/500], Loss: 0.0416
Epoch [53/80], Step [300/500], Loss: 0.0831
Epoch [53/80], Step [400/500], Loss: 0.0350
Epoch [53/80], Step [500/500], Loss: 0.0257
Epoch [54/80], Step [100/500], Loss: 0.0628
Epoch [54/80], Step [200/500], Loss: 0.0533
Epoch [54/80], Step [300/500], Loss: 0.0226
Epoch [54/80], Step [400/500], Loss: 0.0379
Epoch [54/80], Step [500/500], Loss: 0.0605
Epoch [55/80], Step [100/500], Loss: 0.1083
Epoch [55/80], Step [200/500], Loss: 0.1897
Epoch [55/80], Step [300/500], Loss: 0.0573
Epoch [55/80], Step [400/500], Loss: 0.0658
Epoch [55/80], Step [500/500], Loss: 0.0485
Epoch [56/80], Step [100/500], Loss: 0.0430
Epoch [56/80], Step [200/500], Loss: 0.0346
Epoch [56/80], Step [300/500], Loss: 0.0393
Epoch [56/80], Step [400/500], Loss: 0.0278
Epoch [56/80], Step [500/500], Loss: 0.0634
Epoch [57/80], Step [100/500], Loss: 0.0588
Epoch [57/80], Step [200/500], Loss: 0.0300
Epoch [57/80], Step [300/500], Loss: 0.0223
Epoch [57/80], Step [400/500], Loss: 0.1497
Epoch [57/80], Step [500/500], Loss: 0.0777
Epoch [58/80], Step [100/500], Loss: 0.0809
Epoch [58/80], Step [200/500], Loss: 0.0934
Epoch [58/80], Step [300/500], Loss: 0.0982
Epoch [58/80], Step [400/500], Loss: 0.1587
Epoch [58/80], Step [500/500], Loss: 0.0417
Epoch [59/80], Step [100/500], Loss: 0.0371
Epoch [59/80], Step [200/500], Loss: 0.1052
Epoch [59/80], Step [300/500], Loss: 0.0455
Epoch [59/80], Step [400/500], Loss: 0.0274
Epoch [59/80], Step [500/500], Loss: 0.0616
Epoch [60/80], Step [100/500], Loss: 0.1352
Epoch [60/80], Step [200/500], Loss: 0.0750
Epoch [60/80], Step [300/500], Loss: 0.0705
Epoch [60/80], Step [400/500], Loss: 0.0296
Epoch [60/80], Step [500/500], Loss: 0.0380
Epoch [61/80], Step [100/500], Loss: 0.0438
Epoch [61/80], Step [200/500], Loss: 0.0599
Epoch [61/80], Step [300/500], Loss: 0.0504
Epoch [61/80], Step [400/500], Loss: 0.0469
Epoch [61/80], Step [500/500], Loss: 0.0534
Epoch [62/80], Step [100/500], Loss: 0.0200
Epoch [62/80], Step [200/500], Loss: 0.0518
Epoch [62/80], Step [300/500], Loss: 0.0660
Epoch [62/80], Step [400/500], Loss: 0.0473
Epoch [62/80], Step [500/500], Loss: 0.0292
Epoch [63/80], Step [100/500], Loss: 0.0576
Epoch [63/80], Step [200/500], Loss: 0.0643
Epoch [63/80], Step [300/500], Loss: 0.0856
Epoch [63/80], Step [400/500], Loss: 0.0674
Epoch [63/80], Step [500/500], Loss: 0.0834
Epoch [64/80], Step [100/500], Loss: 0.0638
Epoch [64/80], Step [200/500], Loss: 0.0802
Epoch [64/80], Step [300/500], Loss: 0.0700
Epoch [64/80], Step [400/500], Loss: 0.0195
Epoch [64/80], Step [500/500], Loss: 0.0488
Epoch [65/80], Step [100/500], Loss: 0.0358
Epoch [65/80], Step [200/500], Loss: 0.0692
Epoch [65/80], Step [300/500], Loss: 0.0298
Epoch [65/80], Step [400/500], Loss: 0.0233
Epoch [65/80], Step [500/500], Loss: 0.0875
Epoch [66/80], Step [100/500], Loss: 0.0298
Epoch [66/80], Step [200/500], Loss: 0.0509
Epoch [66/80], Step [300/500], Loss: 0.0227
Epoch [66/80], Step [400/500], Loss: 0.1122
Epoch [66/80], Step [500/500], Loss: 0.0954
Epoch [67/80], Step [100/500], Loss: 0.0623
Epoch [67/80], Step [200/500], Loss: 0.0213
Epoch [67/80], Step [300/500], Loss: 0.0553
Epoch [67/80], Step [400/500], Loss: 0.0296
Epoch [67/80], Step [500/500], Loss: 0.0715
Epoch [68/80], Step [100/500], Loss: 0.0543
Epoch [68/80], Step [200/500], Loss: 0.0407
Epoch [68/80], Step [300/500], Loss: 0.0370
Epoch [68/80], Step [400/500], Loss: 0.0279
Epoch [68/80], Step [500/500], Loss: 0.0512
Epoch [69/80], Step [100/500], Loss: 0.0297
Epoch [69/80], Step [200/500], Loss: 0.0583
Epoch [69/80], Step [300/500], Loss: 0.0760
Epoch [69/80], Step [400/500], Loss: 0.0403
Epoch [69/80], Step [500/500], Loss: 0.0189
Epoch [70/80], Step [100/500], Loss: 0.1321
Epoch [70/80], Step [200/500], Loss: 0.0988
Epoch [70/80], Step [300/500], Loss: 0.0440
Epoch [70/80], Step [400/500], Loss: 0.0250
Epoch [70/80], Step [500/500], Loss: 0.0671
Epoch [71/80], Step [100/500], Loss: 0.0293
Epoch [71/80], Step [200/500], Loss: 0.0672
Epoch [71/80], Step [300/500], Loss: 0.0317
Epoch [71/80], Step [400/500], Loss: 0.0436
Epoch [71/80], Step [500/500], Loss: 0.0260
Epoch [72/80], Step [100/500], Loss: 0.0429
Epoch [72/80], Step [200/500], Loss: 0.0222
Epoch [72/80], Step [300/500], Loss: 0.0480
Epoch [72/80], Step [400/500], Loss: 0.0367
Epoch [72/80], Step [500/500], Loss: 0.0134
Epoch [73/80], Step [100/500], Loss: 0.0628
Epoch [73/80], Step [200/500], Loss: 0.0403
Epoch [73/80], Step [300/500], Loss: 0.0436
Epoch [73/80], Step [400/500], Loss: 0.1032
Epoch [73/80], Step [500/500], Loss: 0.0663
Epoch [74/80], Step [100/500], Loss: 0.0192
Epoch [74/80], Step [200/500], Loss: 0.0254
Epoch [74/80], Step [300/500], Loss: 0.0436
Epoch [74/80], Step [400/500], Loss: 0.1073
Epoch [74/80], Step [500/500], Loss: 0.0539
Epoch [75/80], Step [100/500], Loss: 0.0437
Epoch [75/80], Step [200/500], Loss: 0.0813
Epoch [75/80], Step [300/500], Loss: 0.0821
Epoch [75/80], Step [400/500], Loss: 0.0847
Epoch [75/80], Step [500/500], Loss: 0.0428
Epoch [76/80], Step [100/500], Loss: 0.0912
Epoch [76/80], Step [200/500], Loss: 0.0295
Epoch [76/80], Step [300/500], Loss: 0.0700
Epoch [76/80], Step [400/500], Loss: 0.0480
Epoch [76/80], Step [500/500], Loss: 0.0358
Epoch [77/80], Step [100/500], Loss: 0.0178
Epoch [77/80], Step [200/500], Loss: 0.0154
Epoch [77/80], Step [300/500], Loss: 0.0662
Epoch [77/80], Step [400/500], Loss: 0.0258
Epoch [77/80], Step [500/500], Loss: 0.0811
Epoch [78/80], Step [100/500], Loss: 0.0542
Epoch [78/80], Step [200/500], Loss: 0.0643
Epoch [78/80], Step [300/500], Loss: 0.0575
Epoch [78/80], Step [400/500], Loss: 0.0371
Epoch [78/80], Step [500/500], Loss: 0.0512
Epoch [79/80], Step [100/500], Loss: 0.0240
Epoch [79/80], Step [200/500], Loss: 0.0133
Epoch [79/80], Step [300/500], Loss: 0.0532
Epoch [79/80], Step [400/500], Loss: 0.0760
Epoch [79/80], Step [500/500], Loss: 0.0356
Epoch [80/80], Step [100/500], Loss: 0.0728
Epoch [80/80], Step [200/500], Loss: 0.0553
Epoch [80/80], Step [300/500], Loss: 0.0228
Epoch [80/80], Step [400/500], Loss: 0.0324
Epoch [80/80], Step [500/500], Loss: 0.0701
# Test the model.
model.eval()
with torch.no_grad():
correct = 0
total = 0
for images, labels in test_loader:
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the model on the test images: {} %'.format(100 * correct / total))
Accuracy of the model on the test images: 89.89 %
# Save the model
torch.save(model.state_dict(), 'ResNet.ckpt')