文章目录

  • 前言
  • 一、技术介绍
  • 二、实现途径
  • 三、总结

前言

上篇文章,讲了经典卷积神经网络-resnet,这篇文章通过resnet网络,做一些具体的事情。

一、技术介绍

总的来说,第一步首先要加载数据集,对数据进行一些处理,第二步,调整学习率一些参数,训练好resnet网络模型,第三步输入图片或者视频通过训练好的模型,得到结果。

二、实现途径

1.加载数据集,对数据进行处理,加载的图片是(N,C,H,W )对图片进行处理成(C,H,W),通过图片名称获取标签,进行分类。

train_paper=r'E:\桌面\资料\cv3\数据集\罚拳_公开\train\paper'
train_rock=r'E:\桌面\资料\cv3\数据集\罚拳_公开\train\rock'
train_scissors=r'E:\桌面\资料\cv3\数据集\罚拳_公开\train\scissors'test_paper=r'E:\桌面\资料\cv3\数据集\罚拳_公开\test\paper'
test_rock=r'E:\桌面\资料\cv3\数据集\罚拳_公开\test\rock'
test_scission=r'E:\桌面\资料\cv3\数据集\罚拳_公开\test\scissors'
Batch_files=10transs=trans.Compose([trans.ToTensor(),trans.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))
])
def read_img(batch_files):images=[]labels=[]for file in batch_files:image=Image.open(file)image=image.convert('RGB')image=image.resize((64,64))tensor=transs(image)images.append(tensor)if 'rock' in file :labels.append(torch.tensor(0,dtype=torch.int64))if 'paper' in file:labels.append(torch.tensor(1,dtype=torch.int64))if 'scissors' in file:labels.append(torch.tensor(2,dtype=torch.int64))return images,labels
if __name__ == '__main__':

2.写入resnet模型:
这里用的是resnet18

class tiao(nn.Module):def __init__(self,shuru,shuchu):super(tiao, self).__init__()self.conv1=nn.Conv2d(in_channels=shuru,out_channels=shuchu,kernel_size=(3,3),padding=(1,1))self.bath=nn.BatchNorm2d(shuchu)self.relu=nn.ReLU()def forward(self,x):x1=self.conv1(x)x2=self.bath(x1)x3=self.relu(x2)x4=self.conv1(x3)x5=self.bath(x4)x6=self.relu(x5)x7=x6+xreturn x7class tiao2(nn.Module):def __init__(self,shuru):super(tiao2, self).__init__()self.conv1=nn.Conv2d(in_channels=shuru,out_channels=shuru*2,kernel_size=(3,3),stride=(2,2),padding=(1,1))self.conv11=nn.Conv2d(in_channels=shuru,out_channels=shuru*2,kernel_size=(1,1),stride=(2,2))self.batch=nn.BatchNorm2d(shuru*2)self.relu=nn.ReLU()self.conv2=nn.Conv2d(in_channels=shuru*2,out_channels=shuru*2,kernel_size=(3,3),stride=(1,1),padding=(1,1))def forward(self,x):x1=self.conv1(x)x2=self.batch(x1)x3=self.relu(x2)x4=self.conv2(x3)x5=self.batch(x4)x6=self.relu(x5)x11=self.conv11(x)x7=x11+x6return x7class resnet18(nn.Module):def __init__(self):super(resnet18, self).__init__()self.conv1=nn.Conv2d(in_channels=3,out_channels=64,kernel_size=(7,7),stride=(2,2),padding=(3,3))self.bath=nn.BatchNorm2d(64)self.relu=nn.ReLU()self.max=nn.MaxPool2d(2,2)self.tiao1=tiao(64,64)self.tiao2=tiao(64,64)self.tiao3=tiao2(64)self.tiao4=tiao(128,128)self.tiao5=tiao2(128)self.tiao6=tiao(256,256)self.tiao7=tiao2(256)self.tiao8=tiao(512,512)self.a=nn.AdaptiveAvgPool2d(output_size=(1,1))self.l=nn.Linear(512,3)def forward(self,x):x1=self.conv1(x)x2=self.bath(x1)x3=self.relu(x2)x4=self.tiao1(x3)x5=self.tiao2(x4)x6=self.tiao3(x5)x7=self.tiao4(x6)x8=self.tiao5(x7)x9=self.tiao6(x8)x10=self.tiao7(x9)x11=self.tiao8(x10)x12=self.a(x11)x13=x12.view(x12.size()[0],-1)x14=self.l(x13)return x14

第三步:调用读取数据函数,读取数据,打乱,开始训练:

 train_rock=[os.path.join(train_rock,file) for file in os.listdir(train_rock)]train_paper= [os.path.join(train_paper, file) for file in os.listdir(train_paper)]train_scissors = [os.path.join(train_scissors, file) for file in os.listdir(train_scissors)]test_rock=[os.path.join(test_rock,file) for file in os.listdir(test_rock)]test_paper=[os.path.join(test_paper,file) for file in os.listdir(test_paper)]test_scission=[os.path.join(test_scission,file) for file in os.listdir(test_scission)]train=train_rock+train_paper+train_scissorstest=test_rock+test_paper+test_scissionrandom.shuffle(train)random.shuffle(test)model=resnet18().cuda()opt = torch.optim.ASGD(model.parameters(), lr=0.001, weight_decay=0.8)loss = nn.CrossEntropyLoss()print("开始训练")

第四步:训练模型,完成后保存模型:

  for i in range(5):running_loss=0for index in range(0,len(train),Batch_files):images,labels=read_img(train[index:index+Batch_files])inputs=torch.stack(images,0).cuda()labels=torch.stack(labels,0).cuda()inputs, labels = Variable(inputs), Variable(labels)opt.zero_grad()h=model(inputs)loss1=loss(h,labels)loss1.backward()opt.step()running_loss+=loss1.item()if index%41==40:avg_loos=running_loss/41running_loss=0print('avg_loss',avg_loos)if index%101==99:test_files=random.sample(test,100)test_image,test_label=read_img(test_files)test_images=torch.stack(test_image,0).cuda()test_labels=torch.stack(test_label,0).cuda()test_h=model(test_images)_,prediction=torch.max(test_h.data,1)total=test_labels.size(0)correct=(prediction==test_labels).sum()print('100张测试集准确率%d %%'%(100*correct/total))torch.save(model.state_dict(),'resnet_caiq猜拳.pth')

第五步:加载模型,进行测试:

model.load_state_dict(torch.load('resnet_caiq猜拳.pth'))
labels={0:'rock',1:'paper',2:'scissors'}images=[]image=Image.open(r'E:\桌面\1.png')image=image.convert('RGB')image=image.resize((64,64))image=transs(image)images.append(image)image= torch.stack(images, 0).cuda()label=model(image)_,prediction=torch.max(label.data,1)print("预测类别",labels[prediction.item()])


三、总结

本文只是简单介绍了,通过pytorch训练resnet模型。调用训练好的模型,对图片,视频,摄像头进行检测。
本文只是简单对图片进行检测,得到预测结果。
在这里运用了resnet18模型进行训练,其实还有更好的模型,得到更好的训练结果。
在目标检测领域,最著名的是YOLO,检测速度非常快,在实时检测领域很受欢迎,在一些游戏上,可以通过YOLO脚本,实现自动锁定,追踪之类的,比如现在欢迎的吃鸡游戏,玩家通过脚本,实现自动识别人,进行射击操作。在yolov3中,作者提到过yolo已经运用到军事中,出于道德层面的考虑,作者暂停了yolo的更新,在这之后v4,v5,v6以及之后的版本都是一些大佬接棒的。
在实时检测中,现在AI在一些方面已经超越人类了,在准确率上虽然人脑的高层次演绎归纳能力是远胜于AI的,但是在低级信息处理速度和精确度上,人类就很难比得过专精某个功能的AI了。

卷积神经网络resent网络实践相关推荐

  1. 实用卷积神经网络 运用python pdf_解析卷积神经网络—深度学习实践手册 中文pdf高清版...

    解析卷积神经网络-深度学习实践手册从实用角度着重解析了深度学习中的一类神经网络模型--卷积神经网络,向读者剖析了卷积神经网络的基本部件与工作机理,更重要的是系统性的介绍了深度卷积神经网络在实践应用方面 ...

  2. 卷积神经网络(CNN)及其实践

    卷积神经网络(CNN)及其实践 一.CNN 的基础概念先行 1.1 CNN 的基本结构简介 1.2.认识卷积 1.3.CNN 中的卷积层 1.4.CNN 中的池化层 二.在 TensorFlow 中使 ...

  3. 《解析卷积神经网络—深度学习实践手册》—学习笔记

    书籍链接 百度网盘 谷歌云盘 绪论 机器学习是人工智能的一个分支,它致力于研究如何通过计算的手段,利用经验(experience)来改善计算机系统自身的性能.通过从经验中获取知识(knowledge) ...

  4. 卷积神经网络——各种网络的简洁介绍和实现

    各种网络模型:来源<动手学深度学习> 一,卷积神经网络(LeNet) LeNet分为卷积层块和全连接层块两个部分.下面我们分别介绍这两个模块. 卷积层块里的基本单位是卷积层后接最大池化层: ...

  5. 深度学习之卷积神经网络经典网络LeNet-5简介

    1. LeNet-5简介 LeNet5卷积神经网络源于Yann LeCun在1998年发表的论文:Gradient-based Learning Applied to Document Recogni ...

  6. 深度学习卷积神经网络——经典网络VGG-16网络的搭建与实现

    一.VGG-16网络框架介绍 VGGNet是牛津大学计算机视觉组(Visual Geometry Group)和Google DeepMind公司的研究员一起研发的深度卷积神经网络. VGGNet探索 ...

  7. 深度学习卷积神经网络——经典网络LeNet-5、AlexNet、ZFNet网络的搭建与实现

    一.CNN卷积神经网络的经典网络综述 下面图片参照博客:http://blog.csdn.net/cyh_24/article/details/51440344 二.LeNet-5网络 输入尺寸:32 ...

  8. P9:卷积神经网络的工程实践技巧

    子豪兄YYDS https://www.bilibili.com/video/BV1K7411W7So?p=9 一.卷积核的代替 在使用卷积神经网络时,卷积核大小的选择一般也会带来不一样的影响,但是卷 ...

  9. tensorflow实现卷积神经网络——经典网络(LeNet5、AlexNet、VGG-16、ResNet)

    网络介绍: https://blog.csdn.net/loveliuzz/article/details/79131131 https://blog.csdn.net/jiaoyangwm/arti ...

最新文章

  1. 最锋利的Visual Studio Web开发工具扩展:Web Essentials详解(转)
  2. 42岁著名黑客去世,曾拯救互联网:揭开索尼“罪行”、发现DNS漏洞
  3. VS2017使用过程中得到的一些技巧方法和注意点
  4. G - Tiling FZU - 2040(未解决)
  5. Apache Commons Lang StringUtils
  6. 前端学习(3064):vue+element今日头条管理-状态处理
  7. 程序闪退怎么运行_苹果应用程序崩溃闪退怎么办?如何解决苹果设备的软故障?...
  8. 开发手记之实现web.config的快速配置(转载)
  9. lstm原始论文_命名实体识别NER论文调研
  10. JS延迟加载百度分享代码,提高网页速度
  11. HTTP协议的请求协议(个人笔记看不懂的地方可以和我交流)
  12. 关于破解广州天翼校园 + 小米路由器实现自动上网 突破wifi限制
  13. 深入浅出Python——Python高级语法之面向对象
  14. RecyclerView异步加载图片
  15. numpy中dot, multiply, *区别
  16. 针孔相机模型成像原理与图像变形矫正教程
  17. VMware虚拟机操作汇总
  18. BERT—NAACL
  19. 兵团教师计算机水平考试免考条件,中小学教师等6类人员 职称评审可免考外语...
  20. 海康软件web拉取视频流遇到的坑

热门文章

  1. ultraedit 安装教程
  2. 打印系统开发(39)——检查打印机状态
  3. 窥探Kotlin世界(进阶语法)
  4. John密码破解工具
  5. 阿里云ECS服务器+WordPress快速搭建个人博客
  6. KOKIA -《THE POWER OF SMILE》单曲[MP3!]
  7. 第5次作业+105032014124+高小娟
  8. 【金融量化】什么叫市价单、限价单和停止单?
  9. file和folder的区别是什么?
  10. 【服务器搭建个人网站】教程四:域名怎样进行备案?快来看~