pytorch进行图像识别

介绍: (Introduction:)

This is a beginner-friendly project, with four different approaches to the same problem to show how with every approach, a model becomes more efficient/deeper. I have used the fer2013 data-set to recognise the expression on the image, which you can see in the image shown. You will see how these models are structured differently and how they make a difference in the results.

这是一个适合初学者的项目 ,它针对同一问题提供了四种不同的方法,以说明每种方法如何使模型变得更高效/更深入。 我使用了fer2013数据集来识别图像上的表达式,您可以在所示图像中看到它。 您将看到这些模型的结构如何不同,以及它们如何使结果有所不同。

Here’s the list of models covered, and you can find the links to those notebooks right beside their names:

这是涵盖的型号列表,您可以在其名称旁边找到这些笔记本的链接:

  • Logistic RegressionModel (nb-A)

    Logistic回归模型( nb-A )

  • Feedforward Neural Network (nb-B)

    前馈神经网络( nb-B )

  • Convolutional Neural Network (nb-C)

    卷积神经网络( nb-C )

  • Convolution NN with Residual Network (nb-D)

    带残差网络的卷积神经网络( nb-D )

数据集: (DATASET:)

For this project, I have used the ‘fer2013’ dataset. You can find it available here. This dataset consists of 35,887 data entries in CSV file format. You can view the data after converting it into a Pandas DataFrame as shown below:

对于这个项目,我使用了“ fer2013 ”数据集。 您可以在此处找到它。 该数据集包含35,887个CSV文件格式的数据条目。 将数据转换为Pandas DataFrame之后,您可以查看数据,如下所示:

Data frame covering all data in the form of a table.
数据框以表格的形式覆盖所有数据。

The data is divided into three categories by USAGE (Training, Validation and Test sets) and into seven categories by EMOTION ie our LABELS (‘angry’, ‘disgust’, ‘fear’, ‘happy’, ‘sad’, ‘surprise’, ‘neutral’). I have created three other data frames as per my requirement, ie, train_df, valid_df, and test_df. The pixels given can be converted into an image by imposing the required transitions. I made a function to do the same. You can find it in the notebook.

该数据由USAGE分为三类 (训练,验证和测试集)并进入 类的情绪 ,即我们的标签(“愤怒”,“厌恶”,“恐惧”,“高兴”,“悲伤”,“惊奇”,“中性”)。 根据我的要求,我还创建了其他三个数据框,即train_df,valid_df和test_df 。 通过施加所需的过渡,可以将给定的像素转换为图像。 我做了一个函数来做同样的事情。 您可以在笔记本中找到它。

Let’s further explore our dataset:

让我们进一步探索我们的数据集

No. of entries under each dataset
每个数据集下的条目数
Creating separate data frames for each dataset type
为每种数据集类型创建单独的数据框

Because our dataset is present in the form of a data frame and not as a library in PyTorch, I have created a class ‘expressions’ to take an input data frame and output the image into a Tensor and a label. Now, we have a data type that consists of two variables:

因为我们的数据集以数据框的形式而不是PyTorch中的库形式存在,所以我创建了一个“表达式”类来获取输入数据框并将图像输出到Tensor和标签中。 现在,我们有一个包含两个变量的数据类型:

  • Tensor (containing 48x48 grayscale images) and张量(包含48x48灰度图像)和
  • image label图片标签
Class expressions
类表达式

We will have to import transforms from torchvision to be able to use transformations on our dataset. These transforms are important for image processing. Output images can now be used for analyzing or interpreting further. Even otherwise, PyTorch does not work with images directly, we convert the images into Tensors. TorchVision contains helper classes/utilities to work with image data.

我们将不得不从torchvision导入转换,以便能够在数据集上使用转换。 这些转换对于图像处理很重要。 现在可以将输出图像用于进一步分析或解释。 甚至在其他情况下, PyTorch也不直接处理图像,我们将图像转换为张量。 TorchVision 包含用于处理图像数据的帮助程序类/实用程序。

演示地址

Importing transformations
导入转换

演示地址

Chaining Together several transformations
链接几个转换

Our expression datasets are of the type:

我们的表达式数据集的类型为:

演示地址

A tensor containing normalized pixels and a Label attached to it stating the expression.
一个包含标准化像素的张量和一个附加了Label的表达式。

数据加载器: (DataLoaders:)

“from torch.utlis.data import DataLoader”

“从torch.utlis.data导入DataLoader”

DataLoaders can split the data into batches of a predefined size while training. This is very important if we are dealing with millions of data. We can mention the batch size first, like here I made the batch_size = 400, so a batch of 400 will be loaded into the model at a time.

在训练时,DataLoader可以将数据分成预定义大小的批次。 如果我们要处理数百万个数据,这非常重要。 我们可以先提到批处理大小,就像这里我将batch_size = 400一样,因此一次将400个批处理加载到模型中。

演示地址

DataLoaders
数据加载器

Let's have a look at a batch of our data,

让我们看一下我们的一批数据,

演示地址

A batch of data
一批数据

I think this looks amazing, especially considering how much time it took for me to get this output right! Phew!

我认为这看起来很棒,特别是考虑到我花了多少时间才能正确完成此输出! !

GPU培训: (Training on GPUs:)

GPUs are a specialized processor unit with dedicated memory, a single-chip processor used for extensive Graphical and mathematical computation hence freeing the CPU. GPUs are required to reduce the training time because, with an increase in data, the training time will increase. In PyTorch, we check the availability of a GPU using torch.cuda.is_available(). To use a GPU, we have to shift our entire model and our data in GPU memory. For this, I have created a function and a class.

GPU是具有专用内存的专用处理器单元,是用于大量图形和数学计算的单芯片处理器,从而释放了CPU。 需要GPU来减少训练时间,因为随着数据的增加,训练时间将增加。 在PyTorch中,我们使用torch.cuda.is_available()检查GPU的可用性 要使用GPU,我们必须将整个模型和数据转移到GPU内存中。 为此,我创建了一个函数和一个类。

演示地址

Will shift our model and data loader to GPU
将我们的模型和数据加载器转移到GPU

创建逻辑回归模型: (Creating the Logistic Regression Model:)

Logistic Regression is a statistical and ML technique used to model the probability of a certain class/event, ie, to classify records of a dataset based on the values of the input field. In Logistic Regression, we use one or more independent variables to predict an output with a Boolean output. But it can be used for both Binary and Multiclass Classification.

Logistic回归是一种统计和ML技术,用于对某个类别/事件的概率进行建模,即根据输入字段的值对数据集的记录进行分类。 在Logistic回归中,我们使用一个或多个自变量来预测布尔输出的输出。 但是它既可以用于二进制分类也可以用于多类分类。

We have used this as our starting/base model and we will advance toward deeper models. You can find out more about Logistic Regression in this notebook by Aakash NS, here. This notebook has covered the Mnist Data-set.

我们已将其用作开始/基础模型,并且将向更深层次的模型前进。 你可以找到更多关于Logistic回归在这个笔记本的Aakash NS, 这里 。 该笔记本电脑涵盖了Mnist数据集。

模型: (Model:)

演示地址

Logistic Regression model( )
Logistic回归模型()

演示地址

Moving our model to GPU memory
将模型移至GPU内存

Training: Our Objective is to change the parameters of the model so as to be the best estimation of the labels of the samples in the dataset. In our training process, we look at the cost function or lost function and see what the relation is between the cost function and the parameters θ, so we should formulate the cost function.

培训:我们的目标是更改模型的参数,以便对数据集中的样本标签进行最佳估计。 在我们的训练过程中,我们查看成本函数或损失函数,并了解成本函数与参数θ之间的关系,因此我们应制定成本函数。

演示地址

Accuracy is a good evaluation method for Classification but, Its not a good loss function. Here’s why,

准确性是分类的一种很好的评估方法,但它不是一个好的损失函数。 这就是为什么

Hence we use Cross-Entropy as our Loss Function which is continuous and differentiable which provides us good feedback for incredible improvements.

因此,我们使用交叉熵作为损失函数 这是连续不断且与众不同的,为我们提供了令人难以置信的改进的良好反馈。

演示地址

Evaluate Function
评估功能

The fit function below will train our model on the basis of the mentioned hyperparameters.

下面的拟合函数将在提到的超参数的基础上训练我们的模型。

演示地址

Fit Function
拟合函数

Before Training:

训练前:

演示地址

There is a high loss, and low accuracy
损耗高,精度低

While Training:

训练时:

演示地址

First 10 epochs
前10个时代

演示地址

Next 10 epochs
接下来的10个纪元

After 25 epochs, our model’s final accuracy comes out to bs 31% approx.

25个纪元后,我们模型的最终精度约为bs 31%。

演示地址

Accuracy after training on test DataLoader
经过测试的DataLoader训练后的准确性

You will be able to see when you run the notebook that the accuracy does not cross this certain limit. I have plotted a graph of the accuracy below:

您将能够在运行笔记本电脑时看到,准确性没有超过此特定限制。 我在下面绘制了精度图:

演示地址

Graph- (change in accuracy/epochs)
图-(精度/历元的变化)

预测一些输出: (Predicting some Outputs:)

I made a predict function for predicting our model's accuracy on the test dataset.

我创建了一个预测函数,用于预测测试数据集上模型的准确性。

演示地址

Predict Function
预测功能

演示地址

Label vs the predicted Label.
标签与预测标签。

Hey, Look at that!! One prediction is correct. yay!

嘿,看那个!! 一种预测是正确的。 好极了!

前馈神经网络: (FeedForward Neural Network:)

In a neural network as the name suggests we have an artificial neural network wherein connections between nodes do not form a cycle.

顾名思义,在神经网络中,我们有一个人工神经网络,其中节点之间的连接不形成循环。

Due to the nonlinearity in these hidden neurons, the output of an artificial neural network is a nonlinear function of the inputs. In a classification context, this means that the decision boundary can be nonlinear as well, making the model more flexible compared to logistic regression. Although higher flexibility may be desirable in general, it carries with it a higher risk for model overfitting (“memorizing the training cases”), which can potentially reduce a model’s accuracy on previously unseen cases. This is where we add data transformations that can create variations in the training data. Example RandomCrop, RandomHorizontalFlip, etc..

由于这些隐藏神经元的非线性,人工神经网络的输出是输入的非线性函数。 在分类上下文中,这意味着决策边界也可以是非线性的,与逻辑回归相比,该模型更加灵活。 尽管通常可能需要更高的灵活性,但它会带来模型过度拟合(“记忆训练案例”)的较高风险,这可能会降低模型在先前未见案例中的准确性。 在这里,我们添加了可以在训练数据中创建变化的数据转换。 示例RandomCrop,RandomHorizo​​ntalFlip 等 。

You can find a good comparison between logistic regression models and artificial neural networks here under topic 3.

您可以在主题3下的此处找到逻辑回归模型与人工神经网络之间的良好比较。

FNN-型号: (FNN - Model:)

演示地址

Base class
基类

演示地址

Addition of hidden layers and non-Linear function.
隐藏层和非线性功能的添加。

Models Structure:

型号结构:

演示地址

Structure of our Model
模型的结构

We will shift the model to GPU, use the same fit and evaluation function we mentioned above. Let's see the accuracy before training.

我们将模型转换为GPU,并使用与上述相同的拟合和评估功能。 让我们看看训练前的准确性。

演示地址

Accuracy before training
训练前的准确性

Let's begin training:

让我们开始训练:

演示地址

First 10 epochs
前10个时代

演示地址

Next 10 epochs
接下来的10个纪元

The accuracy wasn't moving ahead which is why I only trained for 25 epochs in total. Let's see how our accuracy and loss changes with training.

准确性没有进步,这就是为什么我总共只训练25个纪元。 让我们看看我们的准确性和损失随着训练如何变化。

  • Accuracy:准确性:

演示地址

Function to Plot accuracy
绘制精度函数

演示地址

Accuracy Graph
精度图
  • Loss:失利:

演示地址

Loss Graph
损失图

We can see that the loss has effectively decreased with training. Let's check Final Accuracy on Test DataLoader and predict some images from Test Dataset.

我们可以看到,通过培训,损失已有效减少。 让我们检查一下Test DataLoader的最终精度,并根据Test Dataset预测一些图像。

演示地址

Final Accuracy
最终精度

Predictions:

预测:

演示地址

A correct Prediction from the Test Dataset
来自测试数据集的正确预测

演示地址

An INCORRECT Prediction from Test DataSet
测试数据集的不正确预测

注意: (Note:)

To not make this too long, I have continued the article HERE. I hope this is of good use to you.

为免太长,我在这里继续文章。 希望对您有好处。

You can find me on LinkedIn and reach out to me there.

你可以在LinkedIn上找到我 并在那里与我联系。

翻译自: https://medium.com/jovianml/facial-expression-recognition-with-pytorch-using-4-differently-approached-models-ee5c35110193

pytorch进行图像识别


http://www.taodudu.cc/news/show-2846663.html

相关文章:

  • 详谈HTTPS SSL/TLS协议原理
  • vue 符号..._如何在Windows 8.1上输入和使用表情符号
  • 符号在excel中的引用_如何在Excel工作表中添加表情符号
  • 关于程序员的20 幅幽默漫画,太真实了!
  • 【Java】如何检测、替换4个字节的utf-8编码(此范围编码包含emoji表情)
  • Python吴恩达深度学习作业22 -- Emoji表情情感分类器
  • 趣图:代码突然又可以运行了,why?
  • 吴恩达Coursera深度学习课程 deeplearning.ai (5-2) 自然语言处理与词嵌入--编程作业(二):Emojify表情包
  • 熊猫人表情包python 代码,Python熊猫替换特殊字符
  • gboard包名_如何在Android的Gboard键盘中搜索表情符号和GIF
  • android 调出键盘表情_android 表情,软键盘冲突解决方案(仿微博等SNS应用)
  • 3w最简单led灯电路图_一款简单实用的LED灯驱动电路
  • 封神演义人物实力分级点评
  • 追风筝的人 第八章
  • 叔本华名言
  • 让人成为富翁的技术
  • 重磅!人工智能会取代科学家? 道翰天琼认知智能机器人API接口平台为您揭秘-1。
  • 重磅!人工智能会取代科学家? 道翰天琼认知智能机器人API接口平台为您揭秘。
  • HTTP通信安全和Web攻击技术
  • 【人工智能项目】缺陷检测分割数据集相关整理分享
  • VC操作系统原理
  • 人工智能专家:AI并不像你想象的那么先进
  • 初中化学人教版教案二-Leo老师
  • 社会管理网格化 源码_全县首家镇域网格妇联——“和大王”社会治理网格化服务管理中心妇联正式成立!...
  • 白下高新区妇联、科协举办亲子活动,小朋友们走进云创大数据
  • 微观经济学案例分析(四)
  • 玫琳凯携手联合国机构推出女性创业加速器计划
  • 迟到的吐槽复联4
  • 中年妇女xxx_2019年国际妇女节庆祝活动
  • 《复联4》在中国首映的 阴谋

pytorch进行图像识别_pytorch使用4种不同的模型进行面部表情识别相关推荐

  1. 面部表情识别2:Pytorch实现表情识别(含表情识别数据集和训练代码)

    面部表情识别2:Pytorch实现表情识别(含表情识别数据集和训练代码) 目录 面部表情识别2:Pytorch实现表情识别(含表情识别数据集和训练代码) 1.面部表情识别方法 2.面部表情识别数据集 ...

  2. PyTorch | 优化神经网络训练的17种方法

    点击上方"视学算法",选择加"星标"或"置顶" 重磅干货,第一时间送达 作者 | LORENZ KUHN 来源 | 人工智能前沿讲习 编辑 ...

  3. pytorch必须掌握的的4种学习率衰减策略

    原文: pytorch必须掌握的的4种学习率衰减策略 1.指数衰减 2. 固定步长衰减 3. 多步长衰减 4. 余弦退火衰减 5. 上述4种学习率动态更新策略的说明 梯度下降算法需要我们指定一个学习率 ...

  4. PyTorch: torch.optim 的6种优化器及优化算法介绍

    import torch import torch.nn.functional as F import torch.utils.data as Data import matplotlib.pyplo ...

  5. pytorch框架自动调整学习率的几种方式

    目录 一.前言 二.Pytorch中自动调整学习率的几种方式  2.1 ExponentialLR-指数衰减方式  2.2 ExponentialLR方式对网络训练的影响  2.3 MultiStep ...

  6. python鱼眼图像识别_一种基于鱼眼摄像头的人脸识别锁以及识别方法与流程

    本发明涉及人脸识别领域,特别涉及一种基于鱼眼摄像头的人脸识别锁. 背景技术: 人脸识别具有用在门锁上存在一些不足.例如,门锁一般装在门上,其高度在安装时已经固定,针对不同身高的用户来说可能造成人脸图像 ...

  7. PyTorch Hub发布!一行代码调用最潮模型,图灵奖得主强推

    文章来源:量子位 原文地址:https://mp.weixin.qq.com/s/lS3YiXzYyY6-XNTFyH_GHg 如有兴趣可以**点击加入极市CV专业微信群**,获取更多高质量干货 为了 ...

  8. 【Pytorch神经网络理论篇】 35 GaitSet模型:步态识别思路+水平金字塔池化+三元损失

    代码: [Pytorch神经网络实战案例]28 GitSet模型进行步态与身份识别(CASIA-B数据集)_LiBiGor的博客-CSDN博客1 CASIA-B数据集本例使用的是预处理后的CASIA- ...

  9. 一种基于SE-Inception的茄科疾病识别模型

    A solanaceae disease recognition model based on SE-Inception 1.期刊信息 期刊名称:Computers and Electronics i ...

最新文章

  1. Centos下卸载openjdk并安装自定义jdk
  2. REST API安全认证研究!
  3. Python格式化字符串、占位符、合并数组
  4. 通过人与人的交互,反思软件系统与软件系统之间的集成交互问题
  5. SpringBoot集成Actuator健康指示器health
  6. 1个平方大概多少立杆_1斤草坪种子播撒多少平方/四季青
  7. 带式磁选机行业调研报告 - 市场现状分析与发展前景预测(2021-2027年)
  8. redhat linux 5.6 下安装oracle 11g 时netca报错不能配置监听解决方法
  9. MySQL比like语句更高效的写法
  10. javascript学习资料汇集
  11. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
  12. 计算机教室网络布线费用,办公室网络布线价格是怎么预算的
  13. 基于微信小程序校园商铺系统获取(微信小程序毕业设计)
  14. 如何对谷歌地图的火星坐标进行纠偏校正
  15. 这个可以有!百度大脑EasyDL新发布EasyData搞定AI开发中的数据管理问题
  16. 教你“强人锁男”——java并发编程的常用锁类型
  17. excel拆分工具怎么拆分表格?
  18. NPC整流器,三电平,中点钳位。PWM整流器三电平模型。simulink
  19. 新恒结衣为什么是中国程序员共同的老婆?
  20. 布朗的计算机排名,布朗计算机工程硕士排名,千万好好考查

热门文章

  1. 微信小程序输入框字数限制以及计算
  2. 免费数据库及常用统计网址-数学建模(二十)
  3. flink各版本变化和新增特性
  4. Dedecms TAG中文标签改成英文+数字地址的方法
  5. 马太效应(Matthew Effect)
  6. 晨光科力普基于GitLab CI/CD持续集成服务的应用
  7. 被ddos攻击,附加IP有作用吗
  8. Ubuntu进入桌面后,左侧菜单栏和窗口菜单栏不见了的解决方法
  9. (6) IFC构件与空间的关系 (Industry Foundation Class)
  10. element中切换时间日期选择器时下拉框偏移到左上角的问题