**

<>pytorch之ResNet18(对cifar10数据进行分类准确度达到94%)

**
分为几个步骤:

* 准备数据
* 训练数据
* 测试数据
导入库
import torch import torch.nn.functional as F import torch.nn as nn import
torch.optim as optim import torchvision import torchvision.transforms as
transforms import argparse
深度残差网络ResNet18
class ResidualBlock(nn.Module): def __init__(self, inchannel, outchannel,
stride=1): super(ResidualBlock, self).__init__() self.left = nn.Sequential(
nn.Conv2d(inchannel, outchannel, kernel_size=3, stride=stride, padding=1,
bias=False), nn.BatchNorm2d(outchannel), nn.ReLU(inplace=True),
nn.Conv2d(outchannel, outchannel, kernel_size=3, stride=1, padding=1,
bias=False), nn.BatchNorm2d(outchannel) ) self.shortcut = nn.Sequential() if
stride != 1 or inchannel != outchannel: self.shortcut = nn.Sequential(
nn.Conv2d(inchannel, outchannel, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(outchannel) ) def forward(self, x): out = self.left(x) out +=
self.shortcut(x) out = F.relu(out) return out class ResNet(nn.Module): def
__init__(self, ResidualBlock, num_classes=10): super(ResNet, self).__init__()
self.inchannel = 64 self.conv1 = nn.Sequential( nn.Conv2d(3, 64, kernel_size=3,
stride=1, padding=1, bias=False), nn.BatchNorm2d(64), nn.ReLU(), ) self.layer1
= self.make_layer(ResidualBlock, 64, 2, stride=1) self.layer2 =
self.make_layer(ResidualBlock, 128, 2, stride=2) self.layer3 =
self.make_layer(ResidualBlock, 256, 2, stride=2) self.layer4 =
self.make_layer(ResidualBlock, 512, 2, stride=2) self.fc = nn.Linear(512,
num_classes) def make_layer(self, block, channels, num_blocks, stride): strides
= [stride] + [1] * (num_blocks - 1) #strides=[1,1] layers = [] for stride in
strides: layers.append(block(self.inchannel, channels, stride)) self.inchannel
= channels return nn.Sequential(*layers) def forward(self, x): out =
self.conv1(x) out = self.layer1(out) out = self.layer2(out) out =
self.layer3(out) out = self.layer4(out) out = F.avg_pool2d(out, 4) out =
out.view(out.size(0), -1) out = self.fc(out) return out def ResNet18(): return
ResNet(ResidualBlock)
参数设置
# 参数设置,使得我们能够手动输入命令行参数,就是让风格变得和Linux命令行差不多 parser =
argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
parser.add_argument('--outf', default='./model/m/', help='folder to output
images and model checkpoints') #输出结果保存路径 parser.add_argument('--net',
default='./model/m/Resnet18.pth', help="path to net (to continue training)")
#恢复训练时的模型路径 args = parser.parse_args() # 超参数设置 EPOCH = 200 #遍历数据集次数 pre_epoch =
0 # 定义已经遍历数据集的次数 BATCH_SIZE = 128 #批处理尺寸(batch_size) LR = 0.1 #学习率
定义是否使用GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
<>准备数据集并预处理
transform_train = transforms.Compose([ transforms.RandomCrop(32, padding=4),
#先四周填充0,在吧图像随机裁剪成32*32 transforms.RandomHorizontalFlip(), #图像一半的概率翻转,一半的概率不翻转
transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023,
0.1994, 0.2010)), #R,G,B每层的归一化用到的均值和方差 ]) transform_test = transforms.Compose([
transforms.ToTensor(), transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023,
0.1994, 0.2010)), ]) trainset =
torchvision.datasets.CIFAR10(root='./data/cifar/', train=True, download=True,
transform=transform_train) #训练数据集 trainloader =
torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE, shuffle=True,
num_workers=2) #生成一个个batch进行批训练,组成batch的时候顺序打乱取 testset =
torchvision.datasets.CIFAR10(root='./data/cifar/', train=False, download=True,
transform=transform_test) testloader = torch.utils.data.DataLoader(testset,
batch_size=100, shuffle=False, num_workers=2) # Cifar-10的标签 classes = ('plane',
'car', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
<>模型定义-ResNet
net = ResNet18().to(device)
<>定义损失函数和优化方式
criterion = nn.CrossEntropyLoss() #损失函数为交叉熵,多用于多分类问题 optimizer =
optim.SGD(net.parameters(), lr=LR, momentum=0.9, weight_decay=5e-4)
#优化方式为mini-batch momentum-SGD,并采用L2正则化(权重衰减)
<>训练
if __name__ == "__main__": best_acc = 0 # 初始化best test accuracy print("Start
Training, Resnet-18!") # 定义遍历数据集的次数 with open("acc.txt", "w") as f: with
open("log.txt", "w")as f2: for epoch in range(pre_epoch, EPOCH):
print('\nEpoch: %d' % (epoch + 1)) ''' asas=1 if epoch % asas ==0:
fe='/home/cc/Desktop/dj/model1/model/incption--{}'.format(epoch)
torch.save(net.state_dict(),fe) ''' net.train() sum_loss = 0.0 correct = 0.0
total = 0.0 for i, data in enumerate(trainloader, 0): # 准备数据 length =
len(trainloader) inputs, labels = data inputs, labels = inputs.to(device),
labels.to(device) optimizer.zero_grad() # forward + backward outputs =
net(inputs) loss = criterion(outputs, labels) loss.backward() optimizer.step()
# 每训练1个batch打印一次loss和准确率 sum_loss += loss.item() _, predicted =
torch.max(outputs.data, 1) total += labels.size(0) correct +=
predicted.eq(labels.data).cpu().sum() ''' qw=100. * correct / total
ee=qw.cpu().numpy() train_a.writelines('epoch:'+str(epoch + 1)+'
'+'iter:'+str(i + 1 + epoch * length)+' '+'Loss:'+str(sum_loss / (i + 1))+'
'+'acc:'+str(ee)+'%'+'\n') ''' print('[epoch:%d, iter:%d] Loss: %.03f | Acc:
%.3f%% ' % (epoch + 1, (i + 1 + epoch * length), sum_loss / (i + 1), 100. *
correct / total)) f2.write('%03d %05d |Loss: %.03f | Acc: %.3f%% ' % (epoch +
1, (i + 1 + epoch * length), sum_loss / (i + 1), 100. * correct / total))
f2.write('\n') f2.flush() # 每训练完一个epoch测试一下准确率 print("Waiting Test!") with
torch.no_grad(): correct = 0 total = 0 for data in testloader: net.eval()
images, labels = data images, labels = images.to(device), labels.to(device)
outputs = net(images) # 取得分最高的那个类 (outputs.data的索引号) _, predicted =
torch.max(outputs.data, 1) total += labels.size(0) correct += (predicted ==
labels).cpu().sum() print('测试分类准确率为:%.3f%%' % (100 * correct / total)) acc =
100. * correct / total # 将每次测试结果实时写入acc.txt文件中 print('Saving model......')
torch.save(net.state_dict(), '%s/net_%03d.pth' % (args.outf, epoch + 1))
f.write("EPOCH=%03d,Accuracy= %.3f%%" % (epoch + 1, acc)) f.write('\n')
f.flush() # 记录最佳测试分类准确率并写入best_acc.txt文件中 if acc > best_acc: f3 =
open("best_acc.txt", "w") f3.write("EPOCH=%d,best_acc= %.3f%%" % (epoch + 1,
acc)) f3.close() best_acc = acc print("Training Finished, TotalEPOCH=%d" %
EPOCH)
此方法能够将cifar数据训练至九十多点,还需要加入学习率调整,使得训练更为准确

<>训练训练好的模型的方法:
we='/home/cc/Desktop/dj/model1/model/net-07.pth'
net.load_state_dict(torch.load(we))#加载net-07.pth模型
net=net.to(device)#放置gpu或cpu上,后面训练步骤和之前一样,从之前保存的模型处继续训练

技术
©2019-2020 Toolsou All rights reserved,
hive大量小文件处理方法总结最优化方法总结:公式解、数值优化、求解思想JavaScript中的 Call 和 Apply内存溢出和内存泄漏的区别、产生原因以及解决方案创建数据mysql库流程微信小程序(uni-app)url参数传递对象蝗灾虫群上亿只很少发生碰撞 蝗虫要成自动驾驶功臣vue 监听 Treeselect 选择项的改变第十一届蓝桥杯C/C++ 大学 B 组大赛软件类省赛服务器价格有什么差异?